| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ArgsHolder |
|
| 1.7142857142857142;1.714 |
| 1 | /* | |
| 2 | * Copyright (C) 1998-2000 Semiotek Inc. All Rights Reserved. | |
| 3 | * | |
| 4 | * Redistribution and use in source and binary forms, with or without | |
| 5 | * modification, are permitted under the terms of either of the following | |
| 6 | * Open Source licenses: | |
| 7 | * | |
| 8 | * The GNU General Public License, version 2, or any later version, as | |
| 9 | * published by the Free Software Foundation | |
| 10 | * (http://www.fsf.org/copyleft/gpl.html); | |
| 11 | * | |
| 12 | * or | |
| 13 | * | |
| 14 | * The Semiotek Public License (http://webmacro.org/LICENSE.) | |
| 15 | * | |
| 16 | * This software is provided "as is", with NO WARRANTY, not even the | |
| 17 | * implied warranties of fitness to purpose, or merchantability. You | |
| 18 | * assume all risks and liabilities associated with its use. | |
| 19 | * | |
| 20 | * See www.webmacro.org for more information on the WebMacro project. | |
| 21 | */ | |
| 22 | ||
| 23 | package org.webmacro.directive; | |
| 24 | ||
| 25 | import org.webmacro.engine.BuildContext; | |
| 26 | import org.webmacro.engine.BuildException; | |
| 27 | import org.webmacro.engine.Builder; | |
| 28 | ||
| 29 | /** | |
| 30 | * ArgsHolder is a container for directive arguments. The parser creates | |
| 31 | * and populates the ArgsHolder based on the contents of the directive | |
| 32 | * descriptor. The directive, in the build() method, will call the getArg() | |
| 33 | * methods to retrieve the arguments and build the directive. | |
| 34 | * @author Brian Goetz | |
| 35 | */ | |
| 36 | ||
| 37 | public final class ArgsHolder implements DirectiveArgs | |
| 38 | { | |
| 39 | ||
| 40 | // The argument descriptors for the arguments we hold | |
| 41 | private Directive.ArgDescriptor[] args; | |
| 42 | ||
| 43 | // The parser will call setArg(), and we'll store them here. For | |
| 44 | // each argument in the argument list, the corresponding argument data | |
| 45 | // will be in the same index into buildArgs. | |
| 46 | private Object[] buildArgs; | |
| 47 | ||
| 48 | ||
| 49 | public ArgsHolder (Directive.ArgDescriptor[] args) | |
| 50 | 1624 | { |
| 51 | 1624 | this.args = args; |
| 52 | 1624 | buildArgs = new Object[args.length]; |
| 53 | 1624 | } |
| 54 | ||
| 55 | private final int findArgIndex (int id) | |
| 56 | throws BuildException | |
| 57 | { | |
| 58 | 18624 | for (int i = 0; i < args.length; i++) |
| 59 | { | |
| 60 | 18624 | if (args[i].id == id) |
| 61 | 7024 | return i; |
| 62 | } | |
| 63 | 0 | throw new BuildException("Invalid argument ID " + id + " requested "); |
| 64 | } | |
| 65 | ||
| 66 | /** | |
| 67 | * Get the argument at the specified <code>index</code>. This is different than getting | |
| 68 | * an argument by it's <code>id</code> | |
| 69 | */ | |
| 70 | public Object getExactArg (int idx) throws BuildException | |
| 71 | { | |
| 72 | 0 | return buildArgs[idx]; |
| 73 | } | |
| 74 | ||
| 75 | /** | |
| 76 | * How many arguments does this ArgsHolder have? | |
| 77 | */ | |
| 78 | public final int getArgCount () | |
| 79 | { | |
| 80 | 0 | return buildArgs.length; |
| 81 | } | |
| 82 | ||
| 83 | /** | |
| 84 | * Retrieve the argument whose id is the specified id. | |
| 85 | */ | |
| 86 | public final Object getArg (int id) | |
| 87 | throws BuildException | |
| 88 | { | |
| 89 | 0 | int index = findArgIndex(id); |
| 90 | 0 | return buildArgs[index]; |
| 91 | } | |
| 92 | ||
| 93 | /** | |
| 94 | * Retrieve the argument whose id is the specified id, and if it is a | |
| 95 | * Builder, build it with the specified build context. | |
| 96 | */ | |
| 97 | public final Object getArg (int id, BuildContext bc) | |
| 98 | throws BuildException | |
| 99 | { | |
| 100 | 3472 | int index = findArgIndex(id); |
| 101 | ||
| 102 | 3472 | Object o = buildArgs[index]; |
| 103 | 3472 | return (o instanceof Builder) |
| 104 | ? ((Builder) o).build(bc) | |
| 105 | : o; | |
| 106 | } | |
| 107 | ||
| 108 | /** | |
| 109 | * Set the argument whose id is the specified id. If the argument has | |
| 110 | * already been set, it is overwritten. Generally not used by directives, | |
| 111 | * only used by the parser. | |
| 112 | */ | |
| 113 | ||
| 114 | public final void setArg (int id, Object o) | |
| 115 | throws BuildException | |
| 116 | { | |
| 117 | 3552 | int index = findArgIndex(id); |
| 118 | 3552 | buildArgs[index] = o; |
| 119 | 3552 | } |
| 120 | ||
| 121 | } | |
| 122 |