1 /* 2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.xml.internal.xsom.parser; 27 28 import java.io.IOException; 29 import java.net.URL; 30 31 import javax.xml.parsers.ParserConfigurationException; 32 import javax.xml.parsers.SAXParserFactory; 33 34 import org.xml.sax.ContentHandler; 35 import org.xml.sax.EntityResolver; 36 import org.xml.sax.ErrorHandler; 37 import org.xml.sax.InputSource; 38 import org.xml.sax.Locator; 39 import org.xml.sax.SAXException; 40 import org.xml.sax.SAXParseException; 41 import org.xml.sax.XMLReader; 42 import org.xml.sax.helpers.XMLFilterImpl; 43 44 import com.sun.xml.internal.xsom.impl.parser.Messages; 45 46 /** 47 * Standard XMLParser implemented by using JAXP. 48 * 49 * @author 50 * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) 51 */ 52 public class JAXPParser implements XMLParser { 53 54 private final SAXParserFactory factory; 55 56 public JAXPParser( SAXParserFactory factory ) { 57 factory.setNamespaceAware(true); // just in case 58 this.factory = factory; 59 } 60 61 public JAXPParser() { 62 this( SAXParserFactory.newInstance()); 63 } 64 65 public void parse( InputSource source, ContentHandler handler, 66 ErrorHandler errorHandler, EntityResolver entityResolver ) 67 68 throws SAXException, IOException { 69 70 try { 71 XMLReader reader = factory.newSAXParser().getXMLReader(); 72 reader = new XMLReaderEx(reader); 73 74 reader.setContentHandler(handler); 75 if(errorHandler!=null) 76 reader.setErrorHandler(errorHandler); 77 if(entityResolver!=null) 78 reader.setEntityResolver(entityResolver); 79 reader.parse(source); 80 } catch( ParserConfigurationException e ) { 81 // in practice this won't happen 82 SAXParseException spe = new SAXParseException(e.getMessage(),null,e); 83 errorHandler.fatalError(spe); 84 throw spe; 85 } 86 } 87 88 /** 89 * XMLReader with improved error message for entity resolution failure. 90 * 91 * TODO: this class is completely stand-alone, so it shouldn't be 92 * an inner class. 93 */ 94 private static class XMLReaderEx extends XMLFilterImpl { 95 96 private Locator locator; 97 98 XMLReaderEx( XMLReader parent ) { 99 this.setParent(parent); 100 } 101 102 /** 103 * Resolves entities and reports user-friendly error messages. 104 * 105 * <p> 106 * Some XML parser (at least Xerces) does not report much information 107 * when it fails to resolve an entity, which is often quite 108 * frustrating. For example, if you are behind a firewall and the 109 * schema contains a reference to www.w3.org, and there is no 110 * entity resolver, the parser will just throw an IOException 111 * that doesn't contain any information about where that reference 112 * occurs nor what it is accessing. 113 * 114 * <p> 115 * By implementing an EntityResolver and resolving the reference 116 * by ourselves, we can report an error message with all the 117 * necessary information to fix the problem. 118 * 119 * <p> 120 * Note that we still need to the client-specified entity resolver 121 * to let the application handle entity resolution. Here we just catch 122 * an IOException and add more information. 123 */ 124 @Override 125 public InputSource resolveEntity(String publicId, String systemId) throws SAXException { 126 try { 127 InputSource is=null; 128 129 // ask the client-specified entity resolver first 130 if( this.getEntityResolver()!=null) 131 is = this.getEntityResolver().resolveEntity(publicId,systemId); 132 if( is!=null ) return is; // if that succeeds, fine. 133 134 // rather than returning null, resolve it now 135 // so that we can detect errors. 136 is = new InputSource( new URL(systemId).openStream() ); 137 is.setSystemId(systemId); 138 is.setPublicId(publicId); 139 return is; 140 } catch( IOException e ) { 141 // catch this error and provide a nice error message, rather than 142 // just throwing this IOException. 143 SAXParseException spe = new SAXParseException( 144 Messages.format(Messages.ERR_ENTITY_RESOLUTION_FAILURE, 145 systemId, e.toString()), // use the toString method to get the class name 146 locator, e ); 147 if(this.getErrorHandler()!=null) 148 this.getErrorHandler().fatalError(spe); 149 throw spe; 150 } 151 } 152 153 @Override 154 public void setDocumentLocator(Locator locator) { 155 super.setDocumentLocator(locator); 156 this.locator = locator; 157 } 158 } 159 }