KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > jaxp > DocumentBuilderFactoryImpl


1 /*
2  * Copyright 2000-2002,2004,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.xerces.jaxp;
18
19 import java.util.Hashtable JavaDoc;
20
21 import javax.xml.XMLConstants JavaDoc;
22 import javax.xml.parsers.DocumentBuilder JavaDoc;
23 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
24 import javax.xml.parsers.ParserConfigurationException JavaDoc;
25 import javax.xml.validation.Schema JavaDoc;
26
27 import org.apache.xerces.parsers.DOMParser;
28 import org.apache.xerces.util.SAXMessageFormatter;
29 import org.xml.sax.SAXException JavaDoc;
30 import org.xml.sax.SAXNotRecognizedException JavaDoc;
31 import org.xml.sax.SAXNotSupportedException JavaDoc;
32
33 /**
34  * @author Rajiv Mordani
35  * @author Edwin Goei
36  * @version $Id: DocumentBuilderFactoryImpl.java,v 1.20 2005/06/21 15:13:07 mrglavas Exp $
37  */

38 public class DocumentBuilderFactoryImpl extends DocumentBuilderFactory JavaDoc {
39     /** These are DocumentBuilderFactory attributes not DOM attributes */
40     private Hashtable JavaDoc attributes;
41     private Hashtable JavaDoc features;
42     private Schema JavaDoc grammar;
43     private boolean isXIncludeAware;
44     
45     /**
46      * State of the secure processing feature, initially <code>false</code>
47      */

48     private boolean fSecureProcess = false;
49
50     /**
51      * Creates a new instance of a {@link javax.xml.parsers.DocumentBuilder}
52      * using the currently configured parameters.
53      */

54     public DocumentBuilder JavaDoc newDocumentBuilder()
55         throws ParserConfigurationException JavaDoc
56     {
57         /** Check that if a Schema has been specified that neither of the schema properties have been set. */
58         if (grammar != null && attributes != null) {
59             if (attributes.containsKey(JAXPConstants.JAXP_SCHEMA_LANGUAGE)) {
60                 throw new ParserConfigurationException JavaDoc(
61                         SAXMessageFormatter.formatMessage(null,
62                         "schema-already-specified", new Object JavaDoc[] {JAXPConstants.JAXP_SCHEMA_LANGUAGE}));
63             }
64             else if (attributes.containsKey(JAXPConstants.JAXP_SCHEMA_SOURCE)) {
65                 throw new ParserConfigurationException JavaDoc(
66                         SAXMessageFormatter.formatMessage(null,
67                         "schema-already-specified", new Object JavaDoc[] {JAXPConstants.JAXP_SCHEMA_SOURCE}));
68             }
69         }
70         
71         try {
72             return new DocumentBuilderImpl(this, attributes, features, fSecureProcess);
73         } catch (SAXException JavaDoc se) {
74             // Handles both SAXNotSupportedException, SAXNotRecognizedException
75
throw new ParserConfigurationException JavaDoc(se.getMessage());
76         }
77     }
78
79     /**
80      * Allows the user to set specific attributes on the underlying
81      * implementation.
82      * @param name name of attribute
83      * @param value null means to remove attribute
84      */

85     public void setAttribute(String JavaDoc name, Object JavaDoc value)
86         throws IllegalArgumentException JavaDoc
87     {
88         // This handles removal of attributes
89
if (value == null) {
90             if (attributes != null) {
91                 attributes.remove(name);
92             }
93             // Unrecognized attributes do not cause an exception
94
return;
95         }
96         
97         // This is ugly. We have to collect the attributes and then
98
// later create a DocumentBuilderImpl to verify the attributes.
99

100         // Create Hashtable if none existed before
101
if (attributes == null) {
102             attributes = new Hashtable JavaDoc();
103         }
104
105         attributes.put(name, value);
106
107         // Test the attribute name by possibly throwing an exception
108
try {
109             new DocumentBuilderImpl(this, attributes, features);
110         } catch (Exception JavaDoc e) {
111             attributes.remove(name);
112             throw new IllegalArgumentException JavaDoc(e.getMessage());
113         }
114     }
115
116     /**
117      * Allows the user to retrieve specific attributes on the underlying
118      * implementation.
119      */

120     public Object JavaDoc getAttribute(String JavaDoc name)
121         throws IllegalArgumentException JavaDoc
122     {
123         // See if it's in the attributes Hashtable
124
if (attributes != null) {
125             Object JavaDoc val = attributes.get(name);
126             if (val != null) {
127                 return val;
128             }
129         }
130
131         DOMParser domParser = null;
132         try {
133             // We create a dummy DocumentBuilderImpl in case the attribute
134
// name is not one that is in the attributes hashtable.
135
domParser =
136                 new DocumentBuilderImpl(this, attributes, features).getDOMParser();
137             return domParser.getProperty(name);
138         } catch (SAXException JavaDoc se1) {
139             // assert(name is not recognized or not supported), try feature
140
try {
141                 boolean result = domParser.getFeature(name);
142                 // Must have been a feature
143
return result ? Boolean.TRUE : Boolean.FALSE;
144             } catch (SAXException JavaDoc se2) {
145                 // Not a property or a feature
146
throw new IllegalArgumentException JavaDoc(se1.getMessage());
147             }
148         }
149     }
150     
151     public Schema JavaDoc getSchema() {
152         return grammar;
153     }
154     
155     public void setSchema(Schema JavaDoc grammar) {
156         this.grammar = grammar;
157     }
158     
159     public boolean isXIncludeAware() {
160         return this.isXIncludeAware;
161     }
162     
163     public void setXIncludeAware(boolean state) {
164         this.isXIncludeAware = state;
165     }
166     
167     public boolean getFeature(String JavaDoc name)
168         throws ParserConfigurationException JavaDoc {
169         if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
170             return fSecureProcess;
171         }
172         // See if it's in the features Hashtable
173
if (features != null) {
174             Object JavaDoc val = features.get(name);
175             if (val != null) {
176                 return ((Boolean JavaDoc) val).booleanValue();
177             }
178         }
179         try {
180             DOMParser domParser = new DocumentBuilderImpl(this, attributes, features).getDOMParser();
181             return domParser.getFeature(name);
182         }
183         catch (SAXException JavaDoc e) {
184             throw new ParserConfigurationException JavaDoc(e.getMessage());
185         }
186     }
187     
188     public void setFeature(String JavaDoc name, boolean value)
189         throws ParserConfigurationException JavaDoc {
190         // If this is the secure processing feature, save it then return.
191
if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
192             fSecureProcess = value;
193             return;
194         }
195         if (features == null) {
196             features = new Hashtable JavaDoc();
197         }
198         features.put(name, value ? Boolean.TRUE : Boolean.FALSE);
199         // Test the feature by possibly throwing SAX exceptions
200
try {
201             new DocumentBuilderImpl(this, attributes, features);
202         }
203         catch (SAXNotSupportedException JavaDoc e) {
204             features.remove(name);
205             throw new ParserConfigurationException JavaDoc(e.getMessage());
206         }
207         catch (SAXNotRecognizedException JavaDoc e) {
208             features.remove(name);
209             throw new ParserConfigurationException JavaDoc(e.getMessage());
210         }
211     }
212 }
213
Popular Tags