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;
017:
018: import java.io.IOException;
019: import java.io.InputStream;
020: import java.net.URI;
021: import java.util.Map;
022: import java.util.logging.Level;
023:
024: import javax.xml.parsers.ParserConfigurationException;
025: import javax.xml.parsers.SAXParser;
026: import javax.xml.parsers.SAXParserFactory;
027:
028: import org.xml.sax.SAXException;
029:
030: /**
031: * SchemaFactory purpose.
032: *
033: * <p>
034: * This is the main entry point into the XSI parsing routines.
035: * </p>
036: *
037: * <p>
038: * Example Use:
039: * <pre><code>
040: *
041: *
042: *
043: * Object x = DocumentFactory.getInstance(new URI("MyInstanceDocumentURI");
044: *
045: *
046: *
047: * </code></pre>
048: * </p>
049: *
050: * @author dzwiers, Refractions Research, Inc. http://www.refractions.net
051: * @author $Author:$ (last modification)
052: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/xml/src/main/java/org/geotools/xml/DocumentFactory.java $
053: * @version $Id: DocumentFactory.java 22347 2006-10-24 01:57:23Z jdeolive $
054: */
055: public class DocumentFactory {
056:
057: /**
058: * When this hint is contained and set to Boolean.FALSE, element ordering will not
059: * be validated. This key may also affect data validation within the parse routines.
060: * The inherent safety of the resulting objects is weekend by turning this param to false.
061: */
062: public static final String VALIDATION_HINT = "DocumentFactory_VALIDATION_HINT";
063:
064: /**
065: * <p>
066: * calls getInstance(URI,Level) with Level.WARNING
067: * </p>
068: *
069: * @param desiredDocument
070: * @param hints May be null.
071: *
072: * @return Object
073: *
074: * @throws SAXException
075: *
076: * @see DocumentFactory#getInstance(URI, Map, Level)
077: */
078: public static Object getInstance(URI desiredDocument, Map hints)
079: throws SAXException {
080: return getInstance(desiredDocument, hints, Level.WARNING);
081: }
082:
083: /**
084: * <p>
085: * Parses the instance data provided. This method assumes that the XML
086: * document is fully described using XML Schemas. Failure to be fully
087: * described as Schemas will result in errors, as opposed to a vid parse.
088: * </p>
089: *
090: * @param desiredDocument
091: * @param hints May be null.
092: * @param level
093: *
094: * @return Object
095: *
096: * @throws SAXException
097: */
098: public static Object getInstance(URI desiredDocument, Map hints,
099: Level level) throws SAXException {
100: SAXParser parser = getParser();
101:
102: XMLSAXHandler xmlContentHandler = new XMLSAXHandler(
103: desiredDocument, hints);
104: XMLSAXHandler.setLogLevel(level);
105:
106: try {
107: parser.parse(desiredDocument.toString(), xmlContentHandler);
108: } catch (IOException e) {
109: throw new SAXException(e);
110: }
111:
112: return xmlContentHandler.getDocument();
113: }
114:
115: /**
116: * <p>
117: * Parses the instance data provided. This method assumes that the XML
118: * document is fully described using XML Schemas. Failure to be fully
119: * described as Schemas will result in errors, as opposed to a vid parse.
120: * </p>
121: *
122: * @param is
123: * @param hints May be null.
124: * @param level
125: *
126: * @return Object
127: *
128: * @throws SAXException
129: */
130: public static Object getInstance(InputStream is, Map hints,
131: Level level) throws SAXException {
132: SAXParser parser = getParser();
133:
134: XMLSAXHandler xmlContentHandler = new XMLSAXHandler(hints);
135: XMLSAXHandler.setLogLevel(level);
136:
137: try {
138: parser.parse(is, xmlContentHandler);
139: } catch (IOException e) {
140: XMLSAXHandler.logger.warning(e.toString());
141: throw new SAXException(e);
142: }
143:
144: return xmlContentHandler.getDocument();
145: }
146:
147: /*
148: * convinience method to create an instance of a SAXParser if it is null.
149: */
150: private static SAXParser getParser() throws SAXException {
151: SAXParserFactory spf = SAXParserFactory.newInstance();
152: spf.setNamespaceAware(true);
153: spf.setValidating(false);
154:
155: try {
156: // spf.setFeature("http://xml.org/sax/features/external-general-entities", false);
157: // spf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
158:
159: SAXParser sp = spf.newSAXParser();
160: return sp;
161: } catch (ParserConfigurationException e) {
162: throw new SAXException(e);
163: }
164: }
165: }
|