001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * // Copyright (c) 1998, 2007, Oracle. All rights reserved.
005: *
006: *
007: * The contents of this file are subject to the terms of either the GNU
008: * General Public License Version 2 only ("GPL") or the Common Development
009: * and Distribution License("CDDL") (collectively, the "License"). You
010: * may not use this file except in compliance with the License. You can obtain
011: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
012: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
013: * language governing permissions and limitations under the License.
014: *
015: * When distributing the software, include this License Header Notice in each
016: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
017: * Sun designates this particular file as subject to the "Classpath" exception
018: * as provided by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the License
020: * Header, with the fields enclosed by brackets [] replaced by your own
021: * identifying information: "Portions Copyrighted [year]
022: * [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * If you wish your version of this file to be governed by only the CDDL or
027: * only the GPL Version 2, indicate your decision by adding "[Contributor]
028: * elects to include this software in this distribution under the [CDDL or GPL
029: * Version 2] license." If you don't indicate a single choice of license, a
030: * recipient has the option to distribute your version of this file under
031: * either the CDDL, the GPL Version 2 or to extend the choice of license to
032: * its licensees as provided above. However, if you add GPL Version 2 code
033: * and therefore, elected the GPL Version 2 license, then the option applies
034: * only if the new code is made subject to such option by the copyright
035: * holder.
036: */
037: package oracle.toplink.essentials.platform.xml.jaxp;
038:
039: import java.io.File;
040: import java.io.InputStream;
041: import java.io.IOException;
042: import java.io.Reader;
043: import java.net.URL;
044: import javax.xml.parsers.DocumentBuilder;
045: import javax.xml.parsers.DocumentBuilderFactory;
046: import javax.xml.parsers.ParserConfigurationException;
047: import javax.xml.transform.Source;
048: import javax.xml.transform.Transformer;
049: import javax.xml.transform.TransformerException;
050: import javax.xml.transform.TransformerFactory;
051: import javax.xml.transform.dom.DOMResult;
052: import org.w3c.dom.Document;
053: import org.xml.sax.EntityResolver;
054: import org.xml.sax.ErrorHandler;
055: import org.xml.sax.InputSource;
056: import org.xml.sax.SAXException;
057: import org.xml.sax.SAXParseException;
058: import oracle.toplink.essentials.platform.xml.XMLParser;
059: import oracle.toplink.essentials.platform.xml.XMLPlatformException;
060:
061: public class JAXPParser implements XMLParser {
062: private static final String SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
063: private static final String XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
064: private static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
065: private DocumentBuilderFactory documentBuilderFactory;
066: private EntityResolver entityResolver;
067: private ErrorHandler errorHandler;
068:
069: public JAXPParser() {
070: super ();
071: documentBuilderFactory = DocumentBuilderFactory.newInstance();
072: setNamespaceAware(true);
073: setWhitespacePreserving(false);
074: }
075:
076: public void setNamespaceAware(boolean isNamespaceAware) {
077: documentBuilderFactory.setNamespaceAware(isNamespaceAware);
078: }
079:
080: public void setWhitespacePreserving(boolean isWhitespacePreserving) {
081: documentBuilderFactory
082: .setIgnoringElementContentWhitespace(!isWhitespacePreserving);
083: }
084:
085: public int getValidationMode() {
086: if (!documentBuilderFactory.isValidating()) {
087: return XMLParser.NONVALIDATING;
088: }
089:
090: try {
091: if (null == documentBuilderFactory
092: .getAttribute(SCHEMA_LANGUAGE)) {
093: return XMLParser.DTD_VALIDATION;
094: }
095: } catch (IllegalArgumentException e) {
096: return XMLParser.DTD_VALIDATION;
097: }
098:
099: return XMLParser.SCHEMA_VALIDATION;
100: }
101:
102: public void setValidationMode(int validationMode) {
103: switch (validationMode) {
104: case XMLParser.NONVALIDATING: {
105: documentBuilderFactory.setValidating(false);
106: // documentBuilderFactory.setAttribute(SCHEMA_LANGUAGE, null);
107: return;
108: }
109: case XMLParser.DTD_VALIDATION: {
110: documentBuilderFactory.setValidating(true);
111: // documentBuilderFactory.setAttribute(SCHEMA_LANGUAGE, null);
112: return;
113: }
114: case XMLParser.SCHEMA_VALIDATION: {
115: try {
116: documentBuilderFactory.setAttribute(SCHEMA_LANGUAGE,
117: XML_SCHEMA);
118: documentBuilderFactory.setValidating(true);
119: } catch (IllegalArgumentException e) {
120: // This parser does not support XML Schema validation so leave it as
121: // a non-validating parser.
122: }
123: return;
124: }
125: }
126: }
127:
128: public EntityResolver getEntityResolver() {
129: return entityResolver;
130: }
131:
132: public void setEntityResolver(EntityResolver entityResolver) {
133: this .entityResolver = entityResolver;
134: }
135:
136: public ErrorHandler getErrorHandler() {
137: return errorHandler;
138: }
139:
140: public void setErrorHandler(ErrorHandler errorHandler) {
141: this .errorHandler = errorHandler;
142: }
143:
144: public void setXMLSchema(URL url) throws XMLPlatformException {
145: if (null == url) {
146: return;
147: }
148: try {
149: documentBuilderFactory.setAttribute(SCHEMA_LANGUAGE,
150: XML_SCHEMA);
151: documentBuilderFactory.setAttribute(JAXP_SCHEMA_SOURCE, url
152: .toString());
153: } catch (IllegalArgumentException e) {
154: // The attribute isn't supported so do nothing
155: } catch (Exception e) {
156: XMLPlatformException.xmlPlatformErrorResolvingXMLSchema(
157: url, e);
158: }
159: }
160:
161: public void setXMLSchemas(Object[] schemas)
162: throws XMLPlatformException {
163: if ((null == schemas) || (schemas.length == 0)) {
164: return;
165: }
166: try {
167: documentBuilderFactory.setAttribute(SCHEMA_LANGUAGE,
168: XML_SCHEMA);
169: documentBuilderFactory.setAttribute(JAXP_SCHEMA_SOURCE,
170: schemas);
171: } catch (IllegalArgumentException e) {
172: // The attribute isn't supported so do nothing
173: } catch (Exception e) {
174: XMLPlatformException.xmlPlatformErrorResolvingXMLSchemas(
175: schemas, e);
176: }
177: }
178:
179: public Document parse(InputSource inputSource)
180: throws XMLPlatformException {
181: try {
182: return getDocumentBuilder().parse(inputSource);
183: } catch (SAXException e) {
184: throw XMLPlatformException.xmlPlatformParseException(e);
185: } catch (IOException e) {
186: throw XMLPlatformException.xmlPlatformParseException(e);
187: }
188: }
189:
190: public Document parse(File file) throws XMLPlatformException {
191: try {
192: return getDocumentBuilder().parse(file);
193: } catch (SAXParseException e) {
194: throw XMLPlatformException.xmlPlatformSAXParseException(e);
195: } catch (SAXException e) {
196: throw XMLPlatformException.xmlPlatformParseException(e);
197: } catch (IOException e) {
198: throw XMLPlatformException
199: .xmlPlatformFileNotFoundException(file, e);
200: }
201: }
202:
203: public Document parse(InputStream inputStream)
204: throws XMLPlatformException {
205: try {
206: return getDocumentBuilder().parse(inputStream);
207: } catch (SAXParseException e) {
208: throw XMLPlatformException.xmlPlatformSAXParseException(e);
209: } catch (SAXException e) {
210: throw XMLPlatformException.xmlPlatformParseException(e);
211: } catch (IOException e) {
212: throw XMLPlatformException.xmlPlatformParseException(e);
213: }
214: }
215:
216: public Document parse(Reader reader) throws XMLPlatformException {
217: InputSource inputSource = new InputSource(reader);
218: return parse(inputSource);
219: }
220:
221: public Document parse(Source source) throws XMLPlatformException {
222: try {
223: TransformerFactory transformerFactory = TransformerFactory
224: .newInstance();
225: Transformer transformer = transformerFactory
226: .newTransformer();
227: DOMResult domResult = new DOMResult();
228: transformer.transform(source, domResult);
229: return (Document) domResult.getNode();
230: } catch (TransformerException e) {
231: throw XMLPlatformException.xmlPlatformParseException(e);
232: }
233: }
234:
235: public Document parse(URL url) throws XMLPlatformException {
236: try {
237: InputStream inputStream = url.openStream();
238: return parse(inputStream);
239: } catch (IOException e) {
240: throw XMLPlatformException.xmlPlatformParseException(e);
241: }
242: }
243:
244: private DocumentBuilder getDocumentBuilder() {
245: try {
246: DocumentBuilder documentBuilder = documentBuilderFactory
247: .newDocumentBuilder();
248: documentBuilder.setEntityResolver(entityResolver);
249: documentBuilder.setErrorHandler(errorHandler);
250: return documentBuilder;
251: } catch (ParserConfigurationException e) {
252: throw XMLPlatformException.xmlPlatformParseException(e);
253: }
254: }
255: }
|