001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.xml.handlers;
017:
018: import java.net.URI;
019: import java.util.Map;
020:
021: import javax.naming.OperationNotSupportedException;
022:
023: import org.geotools.xml.XMLElementHandler;
024: import org.geotools.xml.schema.Element;
025: import org.geotools.xml.schema.ElementValue;
026: import org.geotools.xml.schema.SimpleType;
027: import org.xml.sax.Attributes;
028: import org.xml.sax.SAXException;
029: import org.xml.sax.helpers.AttributesImpl;
030:
031: /**
032: * <p>
033: * This class is intended to handle parsing an xml element from an instance
034: * document for elements who's type is both known and simple. This handler is
035: * used within the XMLSAXHandler to handle sax events generated by the SAX
036: * parser.
037: * </p>
038: *
039: * @author dzwiers www.refractions.net
040: *
041: * @see SimpleType
042: * @see XMLElementHandler
043: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/xml/src/main/java/org/geotools/xml/handlers/SimpleElementHandler.java $
044: */
045: public class SimpleElementHandler extends XMLElementHandler {
046:
047: /** <code>serialVersionUID</code> field */
048: private static final long serialVersionUID = SimpleElementHandler.class
049: .hashCode();
050:
051: private SimpleType type; // save casting all over
052: private Element elem;
053: private String text = "";
054: private Object value = null;
055: private Attributes attr = null;
056:
057: /**
058: * Creates a new SimpleElementHandler object.
059: *
060: * @param st Element the simple element which we will parse
061: */
062: public SimpleElementHandler(Element st) {
063: elem = st;
064: type = (SimpleType) st.getType();
065: }
066:
067: /**
068: * @see org.geotools.xml.XMLElementHandler#getElement()
069: */
070: public Element getElement() {
071: return elem;
072: }
073:
074: /**
075: * @see org.geotools.xml.XMLElementHandler#getHandler(java.lang.String,
076: * java.lang.String)
077: */
078: public XMLElementHandler getHandler(URI namespaceURI,
079: String localName, Map hints) throws SAXException {
080: throw new SAXException(
081: "Should not have any children - this is a simpleType");
082: }
083:
084: /**
085: * @see org.geotools.xml.XMLElementHandler#getValue()
086: */
087: public Object getValue() {
088: return value;
089: }
090:
091: /**
092: * @see org.geotools.xml.XMLElementHandler#getName()
093: */
094: public String getName() {
095: return elem.getName();
096: }
097:
098: /**
099: * @see org.geotools.xml.XMLElementHandler#characters(java.lang.String)
100: */
101: public void characters(String text1) {
102: if (this .text != null) {
103: this .text = this .text.concat(text1);
104: } else {
105: this .text = text1;
106: }
107: }
108:
109: /**
110: * @throws SAXException
111: * @throws OperationNotSupportedException
112: * @see org.geotools.xml.XMLElementHandler#endElement(java.lang.String,
113: * java.lang.String)
114: */
115: public void endElement(URI namespaceURI, String localName, Map hints)
116: throws OperationNotSupportedException, SAXException {
117: text = (text == null) ? null : text.trim();
118:
119: ElementValue[] vals;
120: vals = new ElementValue[1];
121: vals[0] = new DefaultElementValue(text, elem);
122: value = type.getValue(elem, vals, attr, hints);
123: attr = null;
124: text = null;
125: }
126:
127: /**
128: * @see org.geotools.xml.XMLElementHandler#startElement(java.lang.String,
129: * java.lang.String, org.xml.sax.Attributes)
130: */
131: public void startElement(URI namespaceURI, String localName,
132: Attributes attr1) {
133: this .attr = new AttributesImpl(attr1);
134: }
135:
136: /**
137: * <p>
138: * Default Implementation used to pass values to type instances
139: * </p>
140: *
141: * @author dzwiers
142: *
143: * @see ElementValue
144: */
145: private static class DefaultElementValue implements ElementValue {
146: private String value = "";
147: private Element t;
148:
149: /**
150: * Stores the two values for use within the specified type
151: *
152: * @param value String
153: * @param t Element
154: *
155: * @see SimpleElementHandler#endElement(String, String)
156: */
157: public DefaultElementValue(String value, Element t) {
158: this .value = value;
159: this .t = t;
160: }
161:
162: /**
163: * @see org.geotools.xml.xsi.ElementValue#getElement()
164: */
165: public Element getElement() {
166: return t;
167: }
168:
169: /**
170: * @see org.geotools.xml.xsi.ElementValue#getValue()
171: */
172: public Object getValue() {
173: return value;
174: }
175: }
176: }
|