KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > dev > util > xml > HandlerArgs


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.dev.util.xml;
17
18 import com.google.gwt.core.ext.UnableToCompleteException;
19
20 /**
21  * A set of args for a given set of parameters, some of which may be set to
22  * default values.
23  */

24 public class HandlerArgs {
25
26   // The real (non-normalized) names of the attributes, used to report errors.
27
private final String JavaDoc[] attrNames;
28
29   private final String JavaDoc[] argValues;
30
31   private final HandlerParam[] handlerParams;
32
33   private final int lineNumber;
34
35   private final Schema schema;
36
37   private final String JavaDoc elemName;
38
39   public HandlerArgs(Schema schema, int lineNumber, String JavaDoc elemName,
40       HandlerParam[] handlerParams) {
41     this.schema = schema;
42     this.lineNumber = lineNumber;
43     this.elemName = elemName;
44     this.handlerParams = handlerParams;
45     attrNames = new String JavaDoc[handlerParams.length];
46     argValues = new String JavaDoc[handlerParams.length];
47
48     // Set default values.
49
//
50
for (int i = 0, n = handlerParams.length; i < n; ++i) {
51       argValues[i] = this.handlerParams[i].getDefaultValue(schema);
52     }
53   }
54
55   /**
56    * @return the argument converted to a form that is expected to compatible
57    * with the associated parameter and will work for a reflection
58    * "invoke()" call
59    */

60   public Object JavaDoc convertToArg(int i) throws UnableToCompleteException {
61     String JavaDoc value = argValues[i];
62     if (value != null) {
63       AttributeConverter converter = schema.getAttributeConverter(handlerParams[i].getParamType());
64       return converter.convertToArg(schema, lineNumber, elemName, attrNames[i],
65           value);
66     } else {
67       return new NullPointerException JavaDoc("Argument " + i + " was null");
68     }
69   }
70
71   public int getArgCount() {
72     return handlerParams.length;
73   }
74
75   public String JavaDoc getArgName(int i) {
76     return handlerParams[i].getNormalizedName();
77   }
78
79   public boolean isArgSet(int i) {
80     if (argValues[i] != null) {
81       return true;
82     } else {
83       return false;
84     }
85   }
86
87   /**
88    * @return <code>true</code> if the param for the specified attribute was
89    * set; <code>false</code> if no matching param was found
90    */

91   public boolean setArg(String JavaDoc attrName, String JavaDoc attrValue) {
92     String JavaDoc normalizedAttrName = normalizeAttrName(attrName);
93     for (int i = 0, n = handlerParams.length; i < n; ++i) {
94       Object JavaDoc testParamName = handlerParams[i].getNormalizedName();
95       if (testParamName.equals(normalizedAttrName)) {
96         // Set it, but don't convert it yet.
97
attrNames[i] = attrName;
98         argValues[i] = attrValue;
99         return true;
100       }
101     }
102     return false;
103   }
104
105   private String JavaDoc normalizeAttrName(String JavaDoc attrName) {
106     // NOTE: this is where other characters would be folded to '_'.
107
return attrName.replace('-', '_');
108   }
109 }
110
Popular Tags