KickJava   Java API By Example, From Geeks To Geeks.

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


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.ParserConfigurationException JavaDoc;
26 import javax.xml.parsers.SAXParser JavaDoc;
27 import javax.xml.parsers.SAXParserFactory JavaDoc;
28 import org.xml.sax.SAXException JavaDoc;
29 import org.xml.sax.SAXNotRecognizedException JavaDoc;
30 import org.xml.sax.SAXNotSupportedException JavaDoc;
31
32 /**
33  * A special SAXParserFactory that delegates to other factories till it finds
34  * one that can satisfy configured requirements.
35  *
36  * @author Petr Nejedly
37  */

38 public class SAXFactoryImpl extends SAXParserFactory JavaDoc {
39     private static Class JavaDoc first;
40     
41     private Map JavaDoc<String JavaDoc,Boolean JavaDoc> features = new HashMap JavaDoc<String JavaDoc,Boolean JavaDoc>();
42     
43     /** The default property name according to the JAXP spec */
44     private static final String JavaDoc SAXParserFactory_PROP =
45         "javax.xml.parsers.SAXParserFactory"; // NOI18N
46

47     public static void install() {
48             System.getProperties().put(SAXParserFactory_PROP,
49                                    SAXFactoryImpl.class.getName());
50     }
51
52     static {
53         ClassLoader JavaDoc orig = Thread.currentThread().getContextClassLoader();
54         // Not app class loader. only ext and bootstrap
55
try {
56            Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader().getParent());
57            first = SAXParserFactory.newInstance().getClass();
58         } finally {
59            Thread.currentThread().setContextClassLoader(orig);
60         }
61         DOMFactoryImpl.install();
62         SAXFactoryImpl.install();
63     }
64     
65     public boolean getFeature(String JavaDoc name) throws ParserConfigurationException JavaDoc, SAXNotRecognizedException JavaDoc, SAXNotSupportedException JavaDoc {
66         return features.get(name);
67     }
68
69     public javax.xml.parsers.SAXParser JavaDoc newSAXParser() throws ParserConfigurationException JavaDoc, SAXException JavaDoc {
70         SAXParser JavaDoc parser = tryCreate();
71         return parser;
72     }
73
74     public void setFeature(java.lang.String JavaDoc name, boolean value) throws ParserConfigurationException JavaDoc, SAXNotRecognizedException JavaDoc, SAXNotSupportedException JavaDoc {
75         features.put(name, Boolean.valueOf(value));
76         tryCreate();
77     }
78
79     private SAXParser JavaDoc tryCreate() throws ParserConfigurationException JavaDoc, SAXNotRecognizedException JavaDoc, SAXNotSupportedException JavaDoc {
80         for (Iterator JavaDoc it = new LazyIterator(first, SAXParserFactory JavaDoc.class, SAXFactoryImpl.class); it.hasNext(); ) {
81             try {
82                 SAXParser JavaDoc parser = tryCreate((Class JavaDoc)it.next());
83                 return parser;
84             } catch (ParserConfigurationException JavaDoc e) {
85                 if (!it.hasNext()) throw e;
86             } catch (SAXNotRecognizedException JavaDoc e) {
87                 if (!it.hasNext()) throw e;
88             } catch (SAXNotSupportedException JavaDoc e) {
89                 if (!it.hasNext()) throw e;
90             } catch (SAXException JavaDoc e) {
91                 if (!it.hasNext()) throw new ParserConfigurationException JavaDoc();
92             }
93         }
94         throw new IllegalStateException JavaDoc("Can't get here!"); // NOI18N
95
}
96
97     private SAXParser JavaDoc tryCreate(Class JavaDoc delClass) throws ParserConfigurationException JavaDoc, SAXException JavaDoc {
98         Exception JavaDoc ex = null;
99         try {
100             SAXParserFactory JavaDoc delegate = (SAXParserFactory JavaDoc)delClass.newInstance();
101             delegate.setValidating(isValidating());
102             delegate.setNamespaceAware(isNamespaceAware());
103             for (Iterator JavaDoc it = features.entrySet().iterator(); it.hasNext(); ) {
104                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
105                 delegate.setFeature((String JavaDoc)entry.getKey(), ((Boolean JavaDoc)entry.getValue()).booleanValue());
106             }
107             return delegate.newSAXParser();
108         } catch (InstantiationException JavaDoc e) {
109             ex = e;
110         } catch (IllegalAccessException JavaDoc e) {
111             ex = e;
112         }
113         throw (ParserConfigurationException JavaDoc) new ParserConfigurationException JavaDoc("Broken factory").initCause(ex); // NOI18N
114
}
115 }
116
Popular Tags