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.net.URISyntaxException;
020: import java.util.HashMap;
021: import java.util.Map;
022: import java.util.logging.Logger;
023:
024: import org.geotools.xml.SchemaFactory;
025: import org.geotools.xml.XMLElementHandler;
026: import org.geotools.xml.schema.ComplexType;
027: import org.geotools.xml.schema.Element;
028: import org.geotools.xml.schema.Schema;
029: import org.geotools.xml.schema.SimpleType;
030: import org.geotools.xml.schema.Type;
031: import org.xml.sax.SAXException;
032:
033: /**
034: * <p>
035: * This class is used to create handlers for child elements based on the
036: * currently defined namespaces. This class is called by the XMLSAXHandler to
037: * help act as a library of prefix -> Schema mappings.
038: * </p>
039: *
040: * @author dzwiers www.refractions.net
041: *
042: * @see org.geotools.xml.XMLSAXHandler
043: * @see Schema
044: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/xml/src/main/java/org/geotools/xml/handlers/ElementHandlerFactory.java $
045: */
046: public class ElementHandlerFactory {
047: public static final String KEY = "org.geotools.xml.handlers.ElementHandlerFactory_KEY";
048: private Logger logger;
049: private Map targSchemas = new HashMap(); // maps prefix -->> Schema
050: private Map prefixURIs = new HashMap(); // maps prefix -->> URI
051: protected URI defaultNS = null;
052:
053: /**
054: * Creates a new ElementHandlerFactory object.
055: *
056: * @param l Logger
057: */
058: public ElementHandlerFactory(Logger l) {
059: logger = l;
060: }
061:
062: /**
063: * @see org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String)
064: */
065: public void endPrefixMapping(String prefix) {
066: URI s = (URI) prefixURIs.remove(prefix);
067:
068: if (s != null) {
069: targSchemas.remove(s);
070: }
071: }
072:
073: /**
074: * @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String,
075: * java.lang.String)
076: */
077: public void startPrefixMapping(String prefix, String targ, URI uri)
078: throws SAXException {
079: logger.finest("Target == '" + targ + "'");
080: logger.finest("URI == '" + uri + "'");
081:
082: try {
083: URI tns = new URI(targ);
084: Schema s = SchemaFactory.getInstance(tns, uri, logger
085: .getLevel());
086:
087: if (s != null) {
088: if ((prefix == null) || "".equalsIgnoreCase(prefix)) {
089: defaultNS = s.getTargetNamespace();
090: }
091:
092: targSchemas.put(s.getTargetNamespace(), s);
093: prefixURIs.put(prefix, tns);
094: } else {
095: prefixURIs.put(prefix, tns);
096: }
097: } catch (URISyntaxException e) {
098: logger.warning(e.toString());
099: throw new SAXException(e);
100: }
101: }
102:
103: /**
104: * @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String,
105: * java.lang.String)
106: */
107: public void startPrefixMapping(String prefix, String targ)
108: throws SAXException {
109: logger.finest("Target == '" + targ + "'");
110:
111: try {
112: URI tns = new URI(targ);
113: Schema s = SchemaFactory.getInstance(tns);
114:
115: if (s == null) {
116: prefixURIs.put(prefix, tns);
117: return;
118: }
119:
120: if ((prefix == null) || "".equalsIgnoreCase(prefix)) {
121: defaultNS = s.getTargetNamespace();
122: }
123:
124: targSchemas.put(s.getTargetNamespace(), s);
125: prefixURIs.put(prefix, tns);
126: } catch (URISyntaxException e) {
127: logger.warning(e.toString());
128: throw new SAXException(e);
129: }
130: }
131:
132: /**
133: * @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String,
134: * java.lang.String)
135: */
136: protected void startPrefixMapping(String prefix, Schema targ) {
137: logger.finest("Target == '" + targ + "'");
138: if ((prefix == null) || "".equalsIgnoreCase(prefix)) {
139: defaultNS = targ.getTargetNamespace();
140: }
141: targSchemas.put(targ.getTargetNamespace(), targ);
142: prefixURIs.put(prefix, targ.getTargetNamespace());
143: }
144:
145: /**
146: * Creates an element handler for the element specified by name and
147: * namespace. Will return null if a suitable handler is not found.
148: *
149: * @param namespaceURI
150: * @param localName
151: *
152: *
153: * @throws SAXException
154: *
155: * @see ElementHandlerFactory#createElementHandler(Element)
156: */
157: public XMLElementHandler createElementHandler(URI namespaceURI,
158: String localName) throws SAXException {
159:
160: if (localName == null) {
161: return null;
162: }
163:
164: if (namespaceURI == null || "".equals(namespaceURI.toString())) {
165: namespaceURI = defaultNS;
166: }
167:
168: logger.finest("Trying to create an element handler for "
169: + localName + " :: " + namespaceURI);
170:
171: Schema s = (Schema) targSchemas.get(namespaceURI);
172:
173: if (s == null) {
174: logger.finest("Could not find Schema " + namespaceURI);
175:
176: return null;
177: }
178:
179: logger.finest("Found Schema " + s.getTargetNamespace());
180:
181: Element[] eth = s.getElements();
182:
183: if (eth == null) {
184: return null;
185: }
186:
187: for (int i = 0; i < eth.length; i++) {
188: String name = eth[i].getName();
189: if (localName.equalsIgnoreCase(name)
190: || name.equals(IgnoreHandler.NAME)) {
191: return createElementHandler(eth[i]);
192: }
193: }
194:
195: //TODO search the nesting import stuff
196: return null;
197: }
198:
199: /**
200: * Creates an element handler based on the element provided.
201: *
202: * @param eth Element
203: *
204: *
205: * @throws SAXException
206: */
207: public XMLElementHandler createElementHandler(Element eth)
208: throws SAXException {
209: Type type = eth.getType();
210:
211: if (type == null) {
212: throw new SAXException("Type not found for "
213: + eth.getName() + " ");
214: }
215:
216: if (type instanceof SimpleType) {
217: return new SimpleElementHandler(eth);
218: }
219: if (type instanceof ComplexType) {
220: return new ComplexElementHandler(this , eth);
221: }
222:
223: return new IgnoreHandler(eth);
224: }
225:
226: public URI getNamespace(String prefix) {
227: URI s = (URI) prefixURIs.get(prefix);
228: return s;
229: }
230: }
|