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: import java.lang.reflect.Method;
021: import java.util.HashMap;
022: import java.util.Map;
023:
024: /**
025: * Abstract base class for reflection-based push-parsing of XML.
026: */
027: public abstract class Schema {
028:
029: private final Map convertersByType = new HashMap();
030:
031: private Schema parent;
032:
033: private int lineNumber;
034:
035: /**
036: * Finds the most recent converter in the schema chain that can convert the
037: * specified type.
038: */
039: public AttributeConverter getAttributeConverter(Class type) {
040: AttributeConverter converter = (AttributeConverter) convertersByType
041: .get(type);
042: if (converter != null) {
043: return converter;
044: } else if (parent != null) {
045: return parent.getAttributeConverter(type);
046: }
047:
048: throw new IllegalStateException(
049: "Unable to find an attribute converter for type "
050: + type.getName());
051: }
052:
053: public int getLineNumber() {
054: return lineNumber;
055: }
056:
057: public void onBadAttributeValue(int line, String elem, String attr,
058: String value, Class paramType)
059: throws UnableToCompleteException {
060: if (parent != null) {
061: parent.onBadAttributeValue(line, elem, attr, value,
062: paramType);
063: }
064: }
065:
066: public void onHandlerException(int line, String elem,
067: Method method, Throwable e)
068: throws UnableToCompleteException {
069: if (parent != null) {
070: parent.onHandlerException(line, elem, method, e);
071: }
072: }
073:
074: public void onMissingAttribute(int line, String elem, String attr)
075: throws UnableToCompleteException {
076: if (parent != null) {
077: parent.onMissingAttribute(line, elem, attr);
078: }
079: }
080:
081: public void onUnexpectedAttribute(int line, String elem,
082: String attr, String value) throws UnableToCompleteException {
083: if (parent != null) {
084: parent.onUnexpectedAttribute(line, elem, attr, value);
085: }
086: }
087:
088: public void onUnexpectedChild(int line, String elem)
089: throws UnableToCompleteException {
090: if (parent != null) {
091: parent.onUnexpectedChild(line, elem);
092: }
093: }
094:
095: public void onUnexpectedElement(int line, String elem)
096: throws UnableToCompleteException {
097: if (parent != null) {
098: parent.onUnexpectedElement(line, elem);
099: }
100: }
101:
102: public void registerAttributeConverter(Class type,
103: AttributeConverter converter) {
104: convertersByType.put(type, converter);
105: }
106:
107: public void setLineNumber(int lineNumber) {
108: this .lineNumber = lineNumber;
109: }
110:
111: public void setParent(Schema parent) {
112: this.parent = parent;
113: }
114: }
|