32 |
32 |
33 package org.xml.sax.helpers; |
33 package org.xml.sax.helpers; |
34 import java.io.BufferedReader; |
34 import java.io.BufferedReader; |
35 import java.io.InputStream; |
35 import java.io.InputStream; |
36 import java.io.InputStreamReader; |
36 import java.io.InputStreamReader; |
37 import java.security.AccessController; |
|
38 import java.security.PrivilegedAction; |
|
39 import org.xml.sax.XMLReader; |
37 import org.xml.sax.XMLReader; |
40 import org.xml.sax.SAXException; |
38 import org.xml.sax.SAXException; |
41 |
39 |
42 |
40 |
43 /** |
41 /** |
83 private XMLReaderFactory () |
81 private XMLReaderFactory () |
84 { |
82 { |
85 } |
83 } |
86 |
84 |
87 private static final String property = "org.xml.sax.driver"; |
85 private static final String property = "org.xml.sax.driver"; |
88 |
86 private static SecuritySupport ss = new SecuritySupport(); |
89 private static String _clsFromJar = null; |
87 |
90 private static boolean _jarread = false; |
88 private static boolean _jarread = false; |
91 /** |
89 /** |
92 * Attempt to create an XMLReader from system defaults. |
90 * Attempt to create an XMLReader from system defaults. |
93 * In environments which can support it, the name of the XMLReader |
91 * In environments which can support it, the name of the XMLReader |
94 * class is determined by trying each these options in order, and |
92 * class is determined by trying each these options in order, and |
132 */ |
130 */ |
133 public static XMLReader createXMLReader () |
131 public static XMLReader createXMLReader () |
134 throws SAXException |
132 throws SAXException |
135 { |
133 { |
136 String className = null; |
134 String className = null; |
137 ClassLoader loader = NewInstance.getClassLoader (); |
135 ClassLoader cl = ss.getContextClassLoader(); |
138 |
136 |
139 // 1. try the JVM-instance-wide system property |
137 // 1. try the JVM-instance-wide system property |
140 try { className = System.getProperty (property); } |
138 try { |
141 catch (RuntimeException e) { /* normally fails for applets */ } |
139 className = ss.getSystemProperty(property); |
|
140 } |
|
141 catch (RuntimeException e) { /* continue searching */ } |
142 |
142 |
143 // 2. if that fails, try META-INF/services/ |
143 // 2. if that fails, try META-INF/services/ |
144 if (className == null) { |
144 if (className == null) { |
145 if (!_jarread) { |
145 if (!_jarread) { |
146 final ClassLoader loader1 = loader; |
|
147 _jarread = true; |
146 _jarread = true; |
148 _clsFromJar = (String) |
147 String service = "META-INF/services/" + property; |
149 AccessController.doPrivileged(new PrivilegedAction() { |
148 InputStream in; |
150 public Object run() { |
149 BufferedReader reader; |
151 String clsName = null; |
150 |
152 try { |
151 try { |
153 String service = "META-INF/services/" + property; |
152 if (cl != null) { |
154 InputStream in; |
153 in = ss.getResourceAsStream(cl, service); |
155 BufferedReader reader; |
154 |
156 if (loader1 == null) |
155 // If no provider found then try the current ClassLoader |
157 in = ClassLoader.getSystemResourceAsStream (service); |
156 if (in == null) { |
158 else |
157 cl = null; |
159 in = loader1.getResourceAsStream (service); |
158 in = ss.getResourceAsStream(cl, service); |
160 |
|
161 if (in != null) { |
|
162 reader = new BufferedReader ( |
|
163 new InputStreamReader (in, "UTF8")); |
|
164 clsName = reader.readLine (); |
|
165 in.close (); |
|
166 } |
|
167 } catch (Exception e) { |
|
168 } |
159 } |
169 return clsName; |
160 } else { |
|
161 // No Context ClassLoader, try the current ClassLoader |
|
162 in = ss.getResourceAsStream(cl, service); |
170 } |
163 } |
171 }); |
164 |
|
165 if (in != null) { |
|
166 reader = new BufferedReader ( |
|
167 new InputStreamReader (in, "UTF8")); |
|
168 className = reader.readLine (); |
|
169 in.close (); |
|
170 } |
|
171 } catch (Exception e) { |
|
172 } |
172 } |
173 } |
173 className = _clsFromJar; |
|
174 } |
174 } |
175 |
175 |
176 // 3. Distro-specific fallback |
176 // 3. Distro-specific fallback |
177 if (className == null) { |
177 if (className == null) { |
178 // BEGIN DISTRIBUTION-SPECIFIC |
178 // BEGIN DISTRIBUTION-SPECIFIC |
185 // END DISTRIBUTION-SPECIFIC |
185 // END DISTRIBUTION-SPECIFIC |
186 } |
186 } |
187 |
187 |
188 // do we know the XMLReader implementation class yet? |
188 // do we know the XMLReader implementation class yet? |
189 if (className != null) |
189 if (className != null) |
190 return loadClass (loader, className); |
190 return loadClass (cl, className); |
191 |
191 |
192 // 4. panic -- adapt any SAX1 parser |
192 // 4. panic -- adapt any SAX1 parser |
193 try { |
193 try { |
194 return new ParserAdapter (ParserFactory.makeParser ()); |
194 return new ParserAdapter (ParserFactory.makeParser ()); |
195 } catch (Exception e) { |
195 } catch (Exception e) { |
215 * @see #createXMLReader() |
215 * @see #createXMLReader() |
216 */ |
216 */ |
217 public static XMLReader createXMLReader (String className) |
217 public static XMLReader createXMLReader (String className) |
218 throws SAXException |
218 throws SAXException |
219 { |
219 { |
220 return loadClass (NewInstance.getClassLoader (), className); |
220 return loadClass (ss.getContextClassLoader(), className); |
221 } |
221 } |
222 |
222 |
223 private static XMLReader loadClass (ClassLoader loader, String className) |
223 private static XMLReader loadClass (ClassLoader loader, String className) |
224 throws SAXException |
224 throws SAXException |
225 { |
225 { |