KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > dom > DocumentBuilderFactoryImpl


1 package net.sf.saxon.dom;
2
3 import javax.xml.parsers.DocumentBuilder JavaDoc;
4 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
5 import javax.xml.parsers.ParserConfigurationException JavaDoc;
6
7 /**
8 * Implementation of JAXP 1.1 DocumentBuilderFactory. To build a Document using
9 * Saxon, set the system property javax.xml.parsers.DocumentBuilderFactory to
10 * "net.sf.saxon.om.DocumentBuilderFactoryImpl" and then call
11 * DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(InputSource);
12 */

13
14 public class DocumentBuilderFactoryImpl extends DocumentBuilderFactory JavaDoc {
15     
16     public DocumentBuilderFactoryImpl() {
17         setCoalescing(true);
18         setExpandEntityReferences(true);
19         setIgnoringComments(false);
20         setIgnoringElementContentWhitespace(false);
21         setNamespaceAware(true);
22         setValidating(false);
23     }
24
25     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
26         throw new IllegalArgumentException JavaDoc("Unrecognized attribute name: " + name);
27     }
28
29     public Object JavaDoc getAttribute(String JavaDoc name) {
30         throw new IllegalArgumentException JavaDoc("Unrecognized attribute name: " + name);
31     }
32     
33     public DocumentBuilder JavaDoc newDocumentBuilder() throws ParserConfigurationException JavaDoc {
34
35         // Check that configuration options are all available
36

37         if (!isExpandEntityReferences()) {
38             throw new ParserConfigurationException JavaDoc(
39                 "Saxon parser always expands entity references");
40         }
41         if (isIgnoringComments()) {
42             throw new ParserConfigurationException JavaDoc(
43                 "Saxon parser does not allow comments to be ignored");
44         }
45         if (isIgnoringElementContentWhitespace()) {
46             throw new ParserConfigurationException JavaDoc(
47                 "Saxon parser does not allow whitespace in element content to be ignored");
48         }
49         if (!isNamespaceAware()) {
50             throw new ParserConfigurationException JavaDoc(
51                 "Saxon parser is always namespace aware");
52         }
53         if (isValidating()) {
54             throw new ParserConfigurationException JavaDoc(
55                 "Saxon parser is non-validating");
56         }
57
58         return new DocumentBuilderImpl();
59     }
60
61     /**
62      * <p>Set a feature for this <code>DocumentBuilderFactory</code> and <code>DocumentBuilder</code>s created by this factory.</p>
63      * <p/>
64      * <p/>
65      * Feature names are fully qualified {@link java.net.URI}s.
66      * Implementations may define their own features.
67      * An {@link javax.xml.parsers.ParserConfigurationException} is thrown if this <code>DocumentBuilderFactory</code> or the
68      * <code>DocumentBuilder</code>s it creates cannot support the feature.
69      * It is possible for an <code>DocumentBuilderFactory</code> to expose a feature value but be unable to change its state.
70      * </p>
71      * <p/>
72      * <p/>
73      * All implementations are required to support the {@link javax.xml.XMLConstants#FEATURE_SECURE_PROCESSING} feature.
74      * When the feature is:</p>
75      * <ul>
76      * <li>
77      * <code>true</code>: the implementation will limit XML processing to conform to implementation limits.
78      * Examples include entity expansion limits and XML Schema constructs that would consume large amounts of resources.
79      * If XML processing is limited for security reasons, it will be reported via a call to the registered
80      * {@link org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException exception)}.
81      * See {@link javax.xml.parsers.DocumentBuilder#setErrorHandler(org.xml.sax.ErrorHandler errorHandler)}.
82      * </li>
83      * <li>
84      * <code>false</code>: the implementation will processing XML according to the XML specifications without
85      * regard to possible implementation limits.
86      * </li>
87      * </ul>
88      *
89      * @param name Feature name.
90      * @param value Is feature state <code>true</code> or <code>false</code>.
91      * @throws javax.xml.parsers.ParserConfigurationException
92      * if this <code>DocumentBuilderFactory</code> or the <code>DocumentBuilder</code>s
93      * it creates cannot support this feature.
94      * @throws NullPointerException If the <code>name</code> parameter is null.
95      */

96     public void setFeature(String JavaDoc name, boolean value) throws ParserConfigurationException JavaDoc {
97         if (name.equals(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING) && !value) {
98             // no action
99
} else {
100             throw new ParserConfigurationException JavaDoc("Unsupported feature or value: " + name);
101         }
102     }
103
104     /**
105      * <p>Get the state of the named feature.</p>
106      * <p/>
107      * <p/>
108      * Feature names are fully qualified {@link java.net.URI}s.
109      * Implementations may define their own features.
110      * An {@link javax.xml.parsers.ParserConfigurationException} is thrown if this <code>DocumentBuilderFactory</code> or the
111      * <code>DocumentBuilder</code>s it creates cannot support the feature.
112      * It is possible for an <code>DocumentBuilderFactory</code> to expose a feature value but be unable to change its state.
113      * </p>
114      *
115      * @param name Feature name.
116      * @return State of the named feature.
117      * @throws javax.xml.parsers.ParserConfigurationException
118      * if this <code>DocumentBuilderFactory</code>
119      * or the <code>DocumentBuilder</code>s it creates cannot support this feature.
120      */

121     public boolean getFeature(String JavaDoc name) throws ParserConfigurationException JavaDoc {
122         if (name.equals(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING)) {
123             return false;
124         } else {
125             throw new ParserConfigurationException JavaDoc("Unsupported feature: " + name);
126         }
127     }
128
129 }
130
131
132 //
133
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
134
// you may not use this file except in compliance with the License. You may obtain a copy of the
135
// License at http://www.mozilla.org/MPL/
136
//
137
// Software distributed under the License is distributed on an "AS IS" basis,
138
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
139
// See the License for the specific language governing rights and limitations under the License.
140
//
141
// The Original Code is: all this file.
142
//
143
// The Initial Developer of the Original Code is Michael H. Kay
144
//
145
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
146
//
147
// Contributor(s): none
148
//
Popular Tags