1 /* 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3 */ 4 /* 5 * Licensed to the Apache Software Foundation (ASF) under one or more 6 * contributor license agreements. See the NOTICE file distributed with 7 * this work for additional information regarding copyright ownership. 8 * The ASF licenses this file to You under the Apache License, Version 2.0 9 * (the "License"); you may not use this file except in compliance with 10 * the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 21 package com.sun.org.apache.xerces.internal.parsers; 22 23 import java.io.IOException; 24 25 import com.sun.org.apache.xerces.internal.impl.Constants; 26 import com.sun.org.apache.xerces.internal.utils.XMLSecurityManager; 27 import com.sun.org.apache.xerces.internal.utils.XMLSecurityPropertyManager; 28 import com.sun.org.apache.xerces.internal.xni.XNIException; 29 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource; 30 import com.sun.org.apache.xerces.internal.xni.parser.XMLParserConfiguration; 31 32 import org.xml.sax.SAXNotSupportedException; 33 import org.xml.sax.SAXNotRecognizedException; 34 35 /** 36 * Base class of all XML-related parsers. 37 * <p> 38 * In addition to the features and properties recognized by the parser 39 * configuration, this parser recognizes these additional features and 40 * properties: 41 * <ul> 42 * <li>Properties 43 * <ul> 44 * <li>http://apache.org/xml/properties/internal/error-handler</li> 45 * <li>http://apache.org/xml/properties/internal/entity-resolver</li> 46 * </ul> 47 * </ul> 48 * 49 * @author Arnaud Le Hors, IBM 50 * @author Andy Clark, IBM 51 * 52 */ 53 public abstract class XMLParser { 54 55 // 56 // Constants 57 // 58 59 // properties 60 61 /** Property identifier: entity resolver. */ 62 protected static final String ENTITY_RESOLVER = 63 Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_RESOLVER_PROPERTY; 64 65 /** Property identifier: error handler. */ 66 protected static final String ERROR_HANDLER = 67 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_HANDLER_PROPERTY; 68 69 /** Recognized properties. */ 70 private static final String[] RECOGNIZED_PROPERTIES = { 71 ENTITY_RESOLVER, 72 ERROR_HANDLER, 73 }; 74 75 // 76 // Data 77 // 78 79 /** The parser configuration. */ 80 protected XMLParserConfiguration fConfiguration; 81 82 /** The XML Security Manager. */ 83 XMLSecurityManager securityManager; 84 85 /** The XML Security Property Manager. */ 86 XMLSecurityPropertyManager securityPropertyManager; 87 88 89 // 90 // Constructors 91 // 92 93 /** 94 * Query the state of a feature. 95 */ 96 public boolean getFeature(String featureId) 97 throws SAXNotSupportedException, SAXNotRecognizedException { 98 return fConfiguration.getFeature(featureId); 99 100 } 101 102 /** 103 * Default Constructor. 104 */ 105 protected XMLParser(XMLParserConfiguration config) { 106 107 // save configuration 108 fConfiguration = config; 109 110 // add default recognized properties 111 fConfiguration.addRecognizedProperties(RECOGNIZED_PROPERTIES); 112 113 } // <init>(XMLParserConfiguration) 114 115 // 116 // Public methods 117 // 118 119 /** 120 * parse 121 * 122 * @param inputSource 123 * 124 * @exception XNIException 125 * @exception java.io.IOException 126 */ 127 public void parse(XMLInputSource inputSource) 128 throws XNIException, IOException { 129 // null indicates that the parser is called directly, initialize them 130 if (securityManager == null) { 131 securityManager = new XMLSecurityManager(true); 132 fConfiguration.setProperty(Constants.SECURITY_MANAGER, securityManager); 133 } 134 if (securityPropertyManager == null) { 135 securityPropertyManager = new XMLSecurityPropertyManager(); 136 fConfiguration.setProperty(Constants.XML_SECURITY_PROPERTY_MANAGER, securityPropertyManager); 137 } 138 139 reset(); 140 fConfiguration.parse(inputSource); 141 142 } // parse(XMLInputSource) 143 144 // 145 // Protected methods 146 // 147 148 /** 149 * reset all components before parsing 150 */ 151 protected void reset() throws XNIException { 152 } // reset() 153 154 /** 155 * Stops the parsing process. 156 * @return true if the parsing process can be stopped, false otherwise 157 */ 158 public boolean stop() { 159 return fConfiguration.stop(); 160 } 161 162 /** 163 * Resumes the parsing process that has been stopped by the {@link #stop()} 164 * method. The method will simply return if parsing is not stopped. 165 * @return true if the parsing process is resumed successfully, false 166 * otherwise 167 */ 168 public boolean resume() { 169 return fConfiguration.resume(); 170 } 171 } // class XMLParser