001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.xerces.jaxp;
019:
020: import java.util.Hashtable;
021:
022: import javax.xml.XMLConstants;
023: import javax.xml.parsers.ParserConfigurationException;
024: import javax.xml.parsers.SAXParser;
025: import javax.xml.parsers.SAXParserFactory;
026: import javax.xml.validation.Schema;
027:
028: import org.apache.xerces.impl.Constants;
029: import org.xml.sax.SAXException;
030: import org.xml.sax.SAXNotRecognizedException;
031: import org.xml.sax.SAXNotSupportedException;
032:
033: /**
034: * This is the implementation specific class for the
035: * <code>javax.xml.parsers.SAXParserFactory</code>. This is the platform
036: * default implementation for the platform.
037: *
038: * @author Rajiv Mordani
039: * @author Edwin Goei
040: *
041: * @version $Id: SAXParserFactoryImpl.java 447237 2006-09-18 05:03:10Z mrglavas $
042: */
043: public class SAXParserFactoryImpl extends SAXParserFactory {
044:
045: /** Feature identifier: namespaces. */
046: private static final String NAMESPACES_FEATURE = Constants.SAX_FEATURE_PREFIX
047: + Constants.NAMESPACES_FEATURE;
048:
049: /** Feature identifier: validation. */
050: private static final String VALIDATION_FEATURE = Constants.SAX_FEATURE_PREFIX
051: + Constants.VALIDATION_FEATURE;
052:
053: /** Feature identifier: XInclude processing */
054: private static final String XINCLUDE_FEATURE = Constants.XERCES_FEATURE_PREFIX
055: + Constants.XINCLUDE_FEATURE;
056:
057: private Hashtable features;
058: private Schema grammar;
059: private boolean isXIncludeAware;
060:
061: /**
062: * State of the secure processing feature, initially <code>false</code>
063: */
064: private boolean fSecureProcess = false;
065:
066: /**
067: * Creates a new instance of <code>SAXParser</code> using the currently
068: * configured factory parameters.
069: * @return javax.xml.parsers.SAXParser
070: */
071: public SAXParser newSAXParser() throws ParserConfigurationException {
072:
073: SAXParser saxParserImpl;
074: try {
075: saxParserImpl = new SAXParserImpl(this , features,
076: fSecureProcess);
077: } catch (SAXException se) {
078: // Translate to ParserConfigurationException
079: throw new ParserConfigurationException(se.getMessage());
080: }
081: return saxParserImpl;
082: }
083:
084: /**
085: * Common code for translating exceptions
086: */
087: private SAXParserImpl newSAXParserImpl()
088: throws ParserConfigurationException,
089: SAXNotRecognizedException, SAXNotSupportedException {
090:
091: SAXParserImpl saxParserImpl;
092: try {
093: saxParserImpl = new SAXParserImpl(this , features);
094: } catch (SAXNotSupportedException e) {
095: throw e;
096: } catch (SAXNotRecognizedException e) {
097: throw e;
098: } catch (SAXException se) {
099: throw new ParserConfigurationException(se.getMessage());
100: }
101: return saxParserImpl;
102: }
103:
104: /**
105: * Sets the particular feature in the underlying implementation of
106: * org.xml.sax.XMLReader.
107: */
108: public void setFeature(String name, boolean value)
109: throws ParserConfigurationException,
110: SAXNotRecognizedException, SAXNotSupportedException {
111: if (name == null) {
112: throw new NullPointerException();
113: }
114: // If this is the secure processing feature, save it then return.
115: if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
116: fSecureProcess = value;
117: return;
118: }
119: // Keep built-in settings in synch with the feature values.
120: else if (name.equals(NAMESPACES_FEATURE)) {
121: setNamespaceAware(value);
122: return;
123: } else if (name.equals(VALIDATION_FEATURE)) {
124: setValidating(value);
125: return;
126: } else if (name.equals(XINCLUDE_FEATURE)) {
127: setXIncludeAware(value);
128: return;
129: }
130:
131: // XXX This is ugly. We have to collect the features and then
132: // later create an XMLReader to verify the features.
133: if (features == null) {
134: features = new Hashtable();
135: }
136: features.put(name, value ? Boolean.TRUE : Boolean.FALSE);
137:
138: // Test the feature by possibly throwing SAX exceptions
139: try {
140: newSAXParserImpl();
141: } catch (SAXNotSupportedException e) {
142: features.remove(name);
143: throw e;
144: } catch (SAXNotRecognizedException e) {
145: features.remove(name);
146: throw e;
147: }
148: }
149:
150: /**
151: * returns the particular property requested for in the underlying
152: * implementation of org.xml.sax.XMLReader.
153: */
154: public boolean getFeature(String name)
155: throws ParserConfigurationException,
156: SAXNotRecognizedException, SAXNotSupportedException {
157: if (name == null) {
158: throw new NullPointerException();
159: }
160: if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
161: return fSecureProcess;
162: } else if (name.equals(NAMESPACES_FEATURE)) {
163: return isNamespaceAware();
164: } else if (name.equals(VALIDATION_FEATURE)) {
165: return isValidating();
166: } else if (name.equals(XINCLUDE_FEATURE)) {
167: return isXIncludeAware();
168: }
169: // Check for valid name by creating a dummy XMLReader to get
170: // feature value
171: return newSAXParserImpl().getXMLReader().getFeature(name);
172: }
173:
174: public Schema getSchema() {
175: return grammar;
176: }
177:
178: public void setSchema(Schema grammar) {
179: this .grammar = grammar;
180: }
181:
182: public boolean isXIncludeAware() {
183: return this .isXIncludeAware;
184: }
185:
186: public void setXIncludeAware(boolean state) {
187: this.isXIncludeAware = state;
188: }
189: }
|