001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020: package com.sun.xml.xsom.parser;
021:
022: import java.io.IOException;
023: import java.net.URL;
024:
025: import javax.xml.parsers.ParserConfigurationException;
026: import javax.xml.parsers.SAXParserFactory;
027:
028: import org.xml.sax.ContentHandler;
029: import org.xml.sax.EntityResolver;
030: import org.xml.sax.ErrorHandler;
031: import org.xml.sax.InputSource;
032: import org.xml.sax.Locator;
033: import org.xml.sax.SAXException;
034: import org.xml.sax.SAXParseException;
035: import org.xml.sax.XMLReader;
036: import org.xml.sax.helpers.XMLFilterImpl;
037:
038: import com.sun.xml.xsom.impl.parser.Messages;
039:
040: /**
041: * Standard XMLParser implemented by using JAXP.
042: *
043: * @author
044: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
045: */
046: public class JAXPParser implements XMLParser {
047:
048: private final SAXParserFactory factory;
049:
050: public JAXPParser(SAXParserFactory factory) {
051: factory.setNamespaceAware(true); // just in case
052: this .factory = factory;
053: }
054:
055: public JAXPParser() {
056: this (SAXParserFactory.newInstance());
057: }
058:
059: public void parse(InputSource source, ContentHandler handler,
060: ErrorHandler errorHandler, EntityResolver entityResolver)
061:
062: throws SAXException, IOException {
063:
064: try {
065: XMLReader reader = factory.newSAXParser().getXMLReader();
066: reader = new XMLReaderEx(reader);
067:
068: reader.setContentHandler(handler);
069: if (errorHandler != null)
070: reader.setErrorHandler(errorHandler);
071: if (entityResolver != null)
072: reader.setEntityResolver(entityResolver);
073: reader.parse(source);
074: } catch (ParserConfigurationException e) {
075: // in practice this won't happen
076: SAXParseException spe = new SAXParseException(e
077: .getMessage(), null, e);
078: errorHandler.fatalError(spe);
079: throw spe;
080: }
081: }
082:
083: /**
084: * XMLReader with improved error message for entity resolution failure.
085: *
086: * TODO: this class is completely stand-alone, so it shouldn't be
087: * an inner class.
088: */
089: private static class XMLReaderEx extends XMLFilterImpl {
090:
091: private Locator locator;
092:
093: XMLReaderEx(XMLReader parent) {
094: this .setParent(parent);
095: }
096:
097: /**
098: * Resolves entities and reports user-friendly error messages.
099: *
100: * <p>
101: * Some XML parser (at least Xerces) does not report much information
102: * when it fails to resolve an entity, which is often quite
103: * frustrating. For example, if you are behind a firewall and the
104: * schema contains a reference to www.w3.org, and there is no
105: * entity resolver, the parser will just throw an IOException
106: * that doesn't contain any information about where that reference
107: * occurs nor what it is accessing.
108: *
109: * <p>
110: * By implementing an EntityResolver and resolving the reference
111: * by ourselves, we can report an error message with all the
112: * necessary information to fix the problem.
113: *
114: * <p>
115: * Note that we still need to the client-specified entity resolver
116: * to let the application handle entity resolution. Here we just catch
117: * an IOException and add more information.
118: */
119: public InputSource resolveEntity(String publicId,
120: String systemId) throws SAXException {
121: try {
122: InputSource is = null;
123:
124: // ask the client-specified entity resolver first
125: if (this .getEntityResolver() != null)
126: is = this .getEntityResolver().resolveEntity(
127: publicId, systemId);
128: if (is != null)
129: return is; // if that succeeds, fine.
130:
131: // rather than returning null, resolve it now
132: // so that we can detect errors.
133: is = new InputSource(new URL(systemId).openStream());
134: is.setSystemId(systemId);
135: is.setPublicId(publicId);
136: return is;
137: } catch (IOException e) {
138: // catch this error and provide a nice error message, rather than
139: // just throwing this IOException.
140: SAXParseException spe = new SAXParseException(Messages
141: .format(Messages.ERR_ENTITY_RESOLUTION_FAILURE,
142: systemId, e.toString()), // use the toString method to get the class name
143: locator, e);
144: if (this .getErrorHandler() != null)
145: this .getErrorHandler().fatalError(spe);
146: throw spe;
147: }
148: }
149:
150: public void setDocumentLocator(Locator locator) {
151: super.setDocumentLocator(locator);
152: this.locator = locator;
153: }
154: }
155: }
|