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 org.geotools.xml.XMLElementHandler;
022: import org.geotools.xml.schema.Element;
023: import org.geotools.xml.schema.Schema;
024: import org.xml.sax.Attributes;
025: import org.xml.sax.SAXException;
026: import org.xml.sax.SAXNotRecognizedException;
027:
028: /**
029: * <p>
030: * Represents the start of an XML document ... serves up elements wrapped in
031: * handlers for a specified schema.
032: * </p>
033: *
034: * @author dzwiers www.refractions.net
035: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/xml/src/main/java/org/geotools/xml/handlers/DocumentHandler.java $
036: */
037: public class DocumentHandler extends XMLElementHandler {
038: public final static String DEFAULT_NAMESPACE_HINT_KEY = "org.geotools.xml.handlers.DocumentHandler.DEFAULT_NAMESPACE_HINT_KEY";
039: private XMLElementHandler xeh = null;
040: private ElementHandlerFactory ehf;
041:
042: /**
043: * Creates a new DocumentHandler object.
044: *
045: * @param ehf ElementHandlerFactory
046: */
047: public DocumentHandler(ElementHandlerFactory ehf) {
048: this .ehf = ehf;
049: }
050:
051: /**
052: * @see org.geotools.xml.XMLElementHandler#getElement()
053: */
054: public Element getElement() {
055: return null;
056: }
057:
058: /**
059: * @see org.geotools.xml.XMLElementHandler#endElement(java.lang.String,
060: * java.lang.String)
061: */
062: public void endElement(URI namespaceURI, String localName, Map hints) {
063: // do nothing
064: }
065:
066: /**
067: * @see org.geotools.xml.XMLElementHandler#getHandler(java.lang.String,
068: * java.lang.String)
069: */
070: public XMLElementHandler getHandler(URI namespaceURI,
071: String localName, Map hints) throws SAXException {
072: if (xeh != null) {
073: throw new SAXNotRecognizedException(
074: "XML Documents may only have one top-level element");
075: }
076: if (hints != null
077: && hints.containsKey(DEFAULT_NAMESPACE_HINT_KEY)) {
078: Object t = hints.get(DEFAULT_NAMESPACE_HINT_KEY);
079: if (t instanceof Schema)
080: ehf.startPrefixMapping("", (Schema) t);
081: else
082: ehf.startPrefixMapping("", t.toString());
083: }
084: xeh = ehf.createElementHandler(namespaceURI, localName);
085:
086: return xeh;
087: }
088:
089: /**
090: * @see org.geotools.xml.XMLElementHandler#startElement(java.lang.String,
091: * java.lang.String, org.xml.sax.Attributes)
092: */
093: public void startElement(URI namespaceURI, String localName,
094: Attributes attr) {
095: // do nothing
096: }
097:
098: /**
099: * @see org.geotools.xml.XMLElementHandler#getValue()
100: */
101: public Object getValue() throws SAXException {
102: return (xeh == null) ? null : xeh.getValue();
103: }
104:
105: /**
106: * @see org.geotools.xml.XMLElementHandler#getName()
107: */
108: public String getName() {
109: return "";
110: }
111: }
|