001: package net.sf.saxon.dom;
002:
003: import javax.xml.parsers.DocumentBuilder;
004: import javax.xml.parsers.DocumentBuilderFactory;
005: import javax.xml.parsers.ParserConfigurationException;
006:
007: /**
008: * Implementation of JAXP 1.1 DocumentBuilderFactory. To build a Document using
009: * Saxon, set the system property javax.xml.parsers.DocumentBuilderFactory to
010: * "net.sf.saxon.om.DocumentBuilderFactoryImpl" and then call
011: * DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(InputSource);
012: */
013:
014: public class DocumentBuilderFactoryImpl extends DocumentBuilderFactory {
015:
016: public DocumentBuilderFactoryImpl() {
017: setCoalescing(true);
018: setExpandEntityReferences(true);
019: setIgnoringComments(false);
020: setIgnoringElementContentWhitespace(false);
021: setNamespaceAware(true);
022: setValidating(false);
023: }
024:
025: public void setAttribute(String name, Object value) {
026: throw new IllegalArgumentException(
027: "Unrecognized attribute name: " + name);
028: }
029:
030: public Object getAttribute(String name) {
031: throw new IllegalArgumentException(
032: "Unrecognized attribute name: " + name);
033: }
034:
035: public DocumentBuilder newDocumentBuilder()
036: throws ParserConfigurationException {
037:
038: // Check that configuration options are all available
039:
040: if (!isExpandEntityReferences()) {
041: throw new ParserConfigurationException(
042: "Saxon parser always expands entity references");
043: }
044: if (isIgnoringComments()) {
045: throw new ParserConfigurationException(
046: "Saxon parser does not allow comments to be ignored");
047: }
048: if (isIgnoringElementContentWhitespace()) {
049: throw new ParserConfigurationException(
050: "Saxon parser does not allow whitespace in element content to be ignored");
051: }
052: if (!isNamespaceAware()) {
053: throw new ParserConfigurationException(
054: "Saxon parser is always namespace aware");
055: }
056: if (isValidating()) {
057: throw new ParserConfigurationException(
058: "Saxon parser is non-validating");
059: }
060:
061: return new DocumentBuilderImpl();
062: }
063:
064: /**
065: * <p>Set a feature for this <code>DocumentBuilderFactory</code> and <code>DocumentBuilder</code>s created by this factory.</p>
066: * <p/>
067: * <p/>
068: * Feature names are fully qualified {@link java.net.URI}s.
069: * Implementations may define their own features.
070: * An {@link javax.xml.parsers.ParserConfigurationException} is thrown if this <code>DocumentBuilderFactory</code> or the
071: * <code>DocumentBuilder</code>s it creates cannot support the feature.
072: * It is possible for an <code>DocumentBuilderFactory</code> to expose a feature value but be unable to change its state.
073: * </p>
074: * <p/>
075: * <p/>
076: * All implementations are required to support the {@link javax.xml.XMLConstants#FEATURE_SECURE_PROCESSING} feature.
077: * When the feature is:</p>
078: * <ul>
079: * <li>
080: * <code>true</code>: the implementation will limit XML processing to conform to implementation limits.
081: * Examples include entity expansion limits and XML Schema constructs that would consume large amounts of resources.
082: * If XML processing is limited for security reasons, it will be reported via a call to the registered
083: * {@link org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException exception)}.
084: * See {@link javax.xml.parsers.DocumentBuilder#setErrorHandler(org.xml.sax.ErrorHandler errorHandler)}.
085: * </li>
086: * <li>
087: * <code>false</code>: the implementation will processing XML according to the XML specifications without
088: * regard to possible implementation limits.
089: * </li>
090: * </ul>
091: *
092: * @param name Feature name.
093: * @param value Is feature state <code>true</code> or <code>false</code>.
094: * @throws javax.xml.parsers.ParserConfigurationException
095: * if this <code>DocumentBuilderFactory</code> or the <code>DocumentBuilder</code>s
096: * it creates cannot support this feature.
097: * @throws NullPointerException If the <code>name</code> parameter is null.
098: */
099: public void setFeature(String name, boolean value)
100: throws ParserConfigurationException {
101: if (name
102: .equals(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING)
103: && !value) {
104: // no action
105: } else {
106: throw new ParserConfigurationException(
107: "Unsupported feature or value: " + name);
108: }
109: }
110:
111: /**
112: * <p>Get the state of the named feature.</p>
113: * <p/>
114: * <p/>
115: * Feature names are fully qualified {@link java.net.URI}s.
116: * Implementations may define their own features.
117: * An {@link javax.xml.parsers.ParserConfigurationException} is thrown if this <code>DocumentBuilderFactory</code> or the
118: * <code>DocumentBuilder</code>s it creates cannot support the feature.
119: * It is possible for an <code>DocumentBuilderFactory</code> to expose a feature value but be unable to change its state.
120: * </p>
121: *
122: * @param name Feature name.
123: * @return State of the named feature.
124: * @throws javax.xml.parsers.ParserConfigurationException
125: * if this <code>DocumentBuilderFactory</code>
126: * or the <code>DocumentBuilder</code>s it creates cannot support this feature.
127: */
128: public boolean getFeature(String name)
129: throws ParserConfigurationException {
130: if (name
131: .equals(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING)) {
132: return false;
133: } else {
134: throw new ParserConfigurationException(
135: "Unsupported feature: " + name);
136: }
137: }
138:
139: }
140:
141: //
142: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
143: // you may not use this file except in compliance with the License. You may obtain a copy of the
144: // License at http://www.mozilla.org/MPL/
145: //
146: // Software distributed under the License is distributed on an "AS IS" basis,
147: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
148: // See the License for the specific language governing rights and limitations under the License.
149: //
150: // The Original Code is: all this file.
151: //
152: // The Initial Developer of the Original Code is Michael H. Kay
153: //
154: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
155: //
156: // Contributor(s): none
157: //
|