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> | 1 /* 2 * Copyright (c) 1997, 2013, 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 import java.util.logging.Level; 31 import java.util.logging.Logger; 32 33 import javax.xml.parsers.ParserConfigurationException; 34 import javax.xml.parsers.SAXParser; 35 import javax.xml.parsers.SAXParserFactory; 36 37 import org.xml.sax.*; 38 import org.xml.sax.helpers.XMLFilterImpl; 39 40 import com.sun.xml.internal.xsom.impl.parser.Messages; 41 42 /** 43 * Standard XMLParser implemented by using JAXP. 44 * 45 * @author 46 * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) 47 */ 48 public class JAXPParser implements XMLParser { 49 50 // not in older JDK, so must be duplicated here, otherwise javax.xml.XMLConstants should be used 51 private static final String ACCESS_EXTERNAL_SCHEMA = "http://javax.xml.XMLConstants/property/accessExternalSchema"; 52 53 private static final Logger LOGGER = Logger.getLogger(JAXPParser.class.getName()); 54 55 private final SAXParserFactory factory; 56 57 public JAXPParser( SAXParserFactory factory ) { 58 factory.setNamespaceAware(true); // just in case 59 this.factory = factory; 60 } 61 62 /** 63 * @deprecated Unsafe, use JAXPParser(factory) instead with 64 * security features initialized by setting 65 * XMLConstants.FEATURE_SECURE_PROCESSING feature. 66 */ 67 public JAXPParser() { 68 this( SAXParserFactory.newInstance()); 69 } 70 71 public void parse( InputSource source, ContentHandler handler, 72 ErrorHandler errorHandler, EntityResolver entityResolver ) 73 74 throws SAXException, IOException { 75 76 try { 77 SAXParser saxParser = allowFileAccess(factory.newSAXParser(), false); 78 XMLReader reader = new XMLReaderEx(saxParser.getXMLReader()); 79 80 reader.setContentHandler(handler); 81 if(errorHandler!=null) 82 reader.setErrorHandler(errorHandler); 83 if(entityResolver!=null) 84 reader.setEntityResolver(entityResolver); 85 reader.parse(source); 86 } catch( ParserConfigurationException e ) { 87 // in practice this won't happen 88 SAXParseException spe = new SAXParseException(e.getMessage(),null,e); 89 errorHandler.fatalError(spe); 90 throw spe; 91 } 92 } 93 94 private static SAXParser allowFileAccess(SAXParser saxParser, boolean disableSecureProcessing) throws SAXException { 95 96 // if feature secure processing enabled, nothing to do, file is allowed, 97 // or user is able to control access by standard JAXP mechanisms 98 if (disableSecureProcessing) { 99 return saxParser; 100 } 101 102 try { 103 saxParser.setProperty(ACCESS_EXTERNAL_SCHEMA, "file"); 104 LOGGER.log(Level.FINE, Messages.format(Messages.JAXP_SUPPORTED_PROPERTY, ACCESS_EXTERNAL_SCHEMA)); 105 } catch (SAXException ignored) { 106 // nothing to do; support depends on version JDK or SAX implementation 107 LOGGER.log(Level.CONFIG, Messages.format(Messages.JAXP_UNSUPPORTED_PROPERTY, ACCESS_EXTERNAL_SCHEMA), ignored); 108 } 109 return saxParser; 110 } 111 112 /** 113 * XMLReader with improved error message for entity resolution failure. 114 * 115 * TODO: this class is completely stand-alone, so it shouldn't be 116 * an inner class. 117 */ 118 private static class XMLReaderEx extends XMLFilterImpl { 119 120 private Locator locator; 121 122 XMLReaderEx( XMLReader parent ) { 123 this.setParent(parent); 124 } 125 126 /** 127 * Resolves entities and reports user-friendly error messages. 128 * 129 * <p> |