001: /*
002: * Copyright 2006 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.dev.util.xml;
017:
018: import com.google.gwt.core.ext.UnableToCompleteException;
019:
020: /**
021: * A set of args for a given set of parameters, some of which may be set to
022: * default values.
023: */
024: public class HandlerArgs {
025:
026: // The real (non-normalized) names of the attributes, used to report errors.
027: private final String[] attrNames;
028:
029: private final String[] argValues;
030:
031: private final HandlerParam[] handlerParams;
032:
033: private final int lineNumber;
034:
035: private final Schema schema;
036:
037: private final String elemName;
038:
039: public HandlerArgs(Schema schema, int lineNumber, String elemName,
040: HandlerParam[] handlerParams) {
041: this .schema = schema;
042: this .lineNumber = lineNumber;
043: this .elemName = elemName;
044: this .handlerParams = handlerParams;
045: attrNames = new String[handlerParams.length];
046: argValues = new String[handlerParams.length];
047:
048: // Set default values.
049: //
050: for (int i = 0, n = handlerParams.length; i < n; ++i) {
051: argValues[i] = this .handlerParams[i]
052: .getDefaultValue(schema);
053: }
054: }
055:
056: /**
057: * @return the argument converted to a form that is expected to compatible
058: * with the associated parameter and will work for a reflection
059: * "invoke()" call
060: */
061: public Object convertToArg(int i) throws UnableToCompleteException {
062: String value = argValues[i];
063: if (value != null) {
064: AttributeConverter converter = schema
065: .getAttributeConverter(handlerParams[i]
066: .getParamType());
067: return converter.convertToArg(schema, lineNumber, elemName,
068: attrNames[i], value);
069: } else {
070: return new NullPointerException("Argument " + i
071: + " was null");
072: }
073: }
074:
075: public int getArgCount() {
076: return handlerParams.length;
077: }
078:
079: public String getArgName(int i) {
080: return handlerParams[i].getNormalizedName();
081: }
082:
083: public boolean isArgSet(int i) {
084: if (argValues[i] != null) {
085: return true;
086: } else {
087: return false;
088: }
089: }
090:
091: /**
092: * @return <code>true</code> if the param for the specified attribute was
093: * set; <code>false</code> if no matching param was found
094: */
095: public boolean setArg(String attrName, String attrValue) {
096: String normalizedAttrName = normalizeAttrName(attrName);
097: for (int i = 0, n = handlerParams.length; i < n; ++i) {
098: Object testParamName = handlerParams[i].getNormalizedName();
099: if (testParamName.equals(normalizedAttrName)) {
100: // Set it, but don't convert it yet.
101: attrNames[i] = attrName;
102: argValues[i] = attrValue;
103: return true;
104: }
105: }
106: return false;
107: }
108:
109: private String normalizeAttrName(String attrName) {
110: // NOTE: this is where other characters would be folded to '_'.
111: return attrName.replace('-', '_');
112: }
113: }
|