KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > startup > DOMFactoryImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.startup;
21
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25 import javax.xml.parsers.DocumentBuilder JavaDoc;
26 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
27 import javax.xml.parsers.ParserConfigurationException JavaDoc;
28
29 /**
30  * A special DocumentBuilderFactory that delegates to other factories till
31  * it finds one that can satisfy configured requirements.
32  *
33  * @author Petr Nejedly
34  */

35 public class DOMFactoryImpl extends DocumentBuilderFactory JavaDoc {
36     private static Class JavaDoc first;
37
38     private Map JavaDoc<String JavaDoc, Object JavaDoc> attributes = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
39     
40     /** The default property name according to the JAXP spec */
41     private static final String JavaDoc Factory_PROP =
42         "javax.xml.parsers.DocumentBuilderFactory"; // NOI18N
43

44     public static void install() {
45         System.getProperties().put(Factory_PROP,
46                                    DOMFactoryImpl.class.getName());
47     }
48
49     static {
50         ClassLoader JavaDoc orig = Thread.currentThread().getContextClassLoader();
51         // Not app class loader. only ext and bootstrap
52
try {
53            Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader().getParent());
54            first = DocumentBuilderFactory.newInstance().getClass();
55         } finally {
56            Thread.currentThread().setContextClassLoader(orig);
57         }
58         
59         DOMFactoryImpl.install();
60         SAXFactoryImpl.install();
61     }
62     
63     public java.lang.Object JavaDoc getAttribute(java.lang.String JavaDoc name) throws java.lang.IllegalArgumentException JavaDoc {
64         return attributes.get(name);
65     }
66     
67     public boolean getFeature (String JavaDoc name) {
68         return Boolean.TRUE.equals (getAttribute (name));
69     }
70     
71     public void setFeature(String JavaDoc name, boolean value) throws ParserConfigurationException JavaDoc {
72         try {
73             setAttribute (name, Boolean.valueOf (value));
74         } catch (IllegalArgumentException JavaDoc ex) {
75             ParserConfigurationException JavaDoc p = new ParserConfigurationException JavaDoc ();
76             p.initCause (ex);
77             throw p;
78         }
79     }
80     
81     
82
83     public DocumentBuilder JavaDoc newDocumentBuilder() throws javax.xml.parsers.ParserConfigurationException JavaDoc {
84         try {
85             return tryCreate();
86         } catch (IllegalArgumentException JavaDoc e) {
87             throw (ParserConfigurationException JavaDoc) new ParserConfigurationException JavaDoc(e.toString()).initCause(e);
88         }
89     }
90
91
92     public void setAttribute(java.lang.String JavaDoc name, java.lang.Object JavaDoc value) throws java.lang.IllegalArgumentException JavaDoc {
93         attributes.put(name, value);
94         try {
95             tryCreate();
96         } catch (ParserConfigurationException JavaDoc e) {
97             throw (IllegalArgumentException JavaDoc) new IllegalArgumentException JavaDoc(e.toString()).initCause(e);
98         }
99     }
100     
101     private DocumentBuilder JavaDoc tryCreate() throws ParserConfigurationException JavaDoc, IllegalArgumentException JavaDoc {
102         for (Iterator JavaDoc it = new LazyIterator(first, DocumentBuilderFactory JavaDoc.class, DOMFactoryImpl.class); it.hasNext(); ) {
103             try {
104                 DocumentBuilder JavaDoc builder = tryCreate((Class JavaDoc)it.next());
105                 return builder;
106             } catch (ParserConfigurationException JavaDoc e) {
107                 if (!it.hasNext()) throw e;
108             } catch (IllegalArgumentException JavaDoc e) {
109                 if (!it.hasNext()) throw e;
110             }
111         }
112         throw new IllegalStateException JavaDoc("Can't get here!"); // NOI18N
113
}
114
115     private DocumentBuilder JavaDoc tryCreate(Class JavaDoc delClass) throws ParserConfigurationException JavaDoc, IllegalArgumentException JavaDoc {
116         Exception JavaDoc ex = null;
117         try {
118             DocumentBuilderFactory JavaDoc delegate = (DocumentBuilderFactory JavaDoc)delClass.newInstance();
119             delegate.setNamespaceAware(isNamespaceAware());
120             delegate.setValidating(isValidating());
121             delegate.setIgnoringElementContentWhitespace(isIgnoringElementContentWhitespace());
122             delegate.setExpandEntityReferences(isExpandEntityReferences());
123             delegate.setIgnoringComments(isIgnoringComments());
124             delegate.setCoalescing(isCoalescing());
125
126             for (Iterator JavaDoc it = attributes.entrySet().iterator(); it.hasNext(); ) {
127                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
128                 delegate.setAttribute((String JavaDoc)entry.getKey(), entry.getValue());
129             }
130             return delegate.newDocumentBuilder();
131         } catch (InstantiationException JavaDoc e) {
132             ex = e;
133         } catch (IllegalAccessException JavaDoc e) {
134             ex = e;
135         }
136         throw (ParserConfigurationException JavaDoc) new ParserConfigurationException JavaDoc("Broken factory").initCause(ex); // NOI18N
137
}
138 }
139
Popular Tags