KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > jaxp > DocumentBuilderFactoryImpl


1 /*
2  *
3  * The Apache Software License, Version 1.1
4  *
5  *
6  * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Xerces" and "Apache Software Foundation" must
29  * not be used to endorse or promote products derived from this
30  * software without prior written permission. For written
31  * permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * nor may "Apache" appear in their name, without prior written
35  * permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation and was
53  * originally based on software copyright (c) 1999, Sun Microsystems, Inc.,
54  * http://www.sun.com. For more information on the Apache Software
55  * Foundation, please see <http://www.apache.org/>.
56  */

57
58
59 package com.sun.org.apache.xerces.internal.jaxp;
60
61 import java.util.Hashtable JavaDoc;
62
63 import javax.xml.parsers.DocumentBuilder JavaDoc;
64 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
65 import javax.xml.parsers.ParserConfigurationException JavaDoc;
66 import javax.xml.validation.Schema JavaDoc;
67 import com.sun.org.apache.xerces.internal.dom.DOMMessageFormatter;
68
69 import com.sun.org.apache.xerces.internal.impl.Constants;
70 import com.sun.org.apache.xerces.internal.parsers.DOMParser;
71 import org.xml.sax.SAXException JavaDoc;
72
73 /**
74  * @author Rajiv Mordani
75  * @author Edwin Goei
76  * @version $Id: DocumentBuilderFactoryImpl.java,v 1.14 2004/02/24 23:15:58 mrglavas Exp $
77  */

78 public class DocumentBuilderFactoryImpl extends DocumentBuilderFactory JavaDoc {
79     /** These are DocumentBuilderFactory attributes not DOM attributes */
80     private Hashtable JavaDoc attributes;
81     private Schema JavaDoc grammar;
82     private boolean isXIncludeAware;
83     
84     /**
85      * Creates a new instance of a {@link javax.xml.parsers.DocumentBuilder}
86      * using the currently configured parameters.
87      */

88     public DocumentBuilder JavaDoc newDocumentBuilder()
89     throws ParserConfigurationException JavaDoc {
90         // check the consistency between the specified schema and
91
// the schema property. I thought about putting this into
92
// DocumentBuilderImpl, but because of the hack in the getAttribute method,
93
// we can't really do that. -KK
94
if( attributes!= null && attributes.containsKey("http://java.sun.com/xml/jaxp/properties/schemaLanguage") && grammar!=null )
95             throw new ParserConfigurationException JavaDoc(
96             DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN,
97             "jaxp-schema-support",null));
98         
99         
100         try {
101             return new DocumentBuilderImpl(this, attributes);
102         } catch (SAXException JavaDoc se) {
103             // Handles both SAXNotSupportedException, SAXNotRecognizedException
104
throw new ParserConfigurationException JavaDoc(se.getMessage());
105         }
106     }
107     
108     /**
109      * Allows the user to set specific attributes on the underlying
110      * implementation.
111      * @param name name of attribute
112      * @param value null means to remove attribute
113      */

114     public void setAttribute(String JavaDoc name, Object JavaDoc value)
115     throws IllegalArgumentException JavaDoc {
116         // This handles removal of attributes
117
if (value == null) {
118             if (attributes != null) {
119                 attributes.remove(name);
120             }
121             // Unrecognized attributes do not cause an exception
122
return;
123         }
124         
125         // This is ugly. We have to collect the attributes and then
126
// later create a DocumentBuilderImpl to verify the attributes.
127

128         // Create Hashtable if none existed before
129
if (attributes == null) {
130             attributes = new Hashtable JavaDoc();
131         }
132         
133         attributes.put(name, value);
134         
135         // Test the attribute name by possibly throwing an exception
136
try {
137             new DocumentBuilderImpl(this, attributes);
138         } catch (Exception JavaDoc e) {
139             attributes.remove(name);
140             throw new IllegalArgumentException JavaDoc(e.getMessage());
141         }
142     }
143     
144     /**
145      * Allows the user to retrieve specific attributes on the underlying
146      * implementation.
147      */

148     public Object JavaDoc getAttribute(String JavaDoc name) throws IllegalArgumentException JavaDoc {
149         // See if it's in the attributes Hashtable
150
if (attributes != null) {
151             Object JavaDoc val = attributes.get(name);
152             if (val != null) {
153                 return val;
154             }
155         }
156         
157         DOMParser domParser = null;
158         try {
159             // We create a dummy DocumentBuilderImpl in case the attribute
160
// name is not one that is in the attributes hashtable.
161
domParser =
162             new DocumentBuilderImpl(this, attributes).getDOMParser();
163             return domParser.getProperty(name);
164         } catch (SAXException JavaDoc se1) {
165             // assert(name is not recognized or not supported), try feature
166
try {
167                 boolean result = domParser.getFeature(name);
168                 // Must have been a feature
169
return result ? Boolean.TRUE : Boolean.FALSE;
170             } catch (SAXException JavaDoc se2) {
171                 // Not a property or a feature
172
throw new IllegalArgumentException JavaDoc(se1.getMessage());
173             }
174         }
175     }
176     
177     public Schema JavaDoc getSchema() {
178         return grammar;
179     }
180     
181     public void setSchema(Schema JavaDoc grammar) {
182         this.grammar = grammar;
183     }
184     
185     public boolean isXIncludeAware() {
186         return this.isXIncludeAware;
187     }
188     
189     public void setXIncludeAware(boolean state) {
190         this.isXIncludeAware = state;
191     }
192     
193     public void setFeature(String JavaDoc name, boolean value)
194     throws ParserConfigurationException JavaDoc{
195         
196         //Revisit::
197
//for now use attributes itself. we just support on feature.
198
//If we need to use setFeature in full fledge we should
199
//document what is supported by setAttribute
200
//and what is by setFeature.
201
//user should not use setAttribute("xyz",Boolean.TRUE)
202
//instead of setFeature("xyz",true);
203
if(attributes == null)
204             attributes = new Hashtable JavaDoc();
205         if(name.equals(Constants.FEATURE_SECURE_PROCESSING)){
206             attributes.put(Constants.FEATURE_SECURE_PROCESSING,Boolean.valueOf(value));
207         } else throw new ParserConfigurationException JavaDoc(
208         DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN,
209         "jaxp_feature_not_supported",
210         new Object JavaDoc[] {name}));
211         
212     }
213     
214     public boolean getFeature(String JavaDoc name)
215     throws ParserConfigurationException JavaDoc {
216         
217         if (name.equals(Constants.FEATURE_SECURE_PROCESSING)){
218             Object JavaDoc ob = attributes.get(Constants.FEATURE_SECURE_PROCESSING);
219             if(ob == null) return false;
220             return ((Boolean JavaDoc)ob).booleanValue();
221         }
222         else
223             throw new ParserConfigurationException JavaDoc(DOMMessageFormatter.formatMessage(
224             DOMMessageFormatter.DOM_DOMAIN,"jaxp_feature_not_supported",
225             new Object JavaDoc[] {name}));
226     }
227 }
228
Popular Tags