1 12 13 14 package org.w3c.dom.bootstrap; 15 16 import java.util.StringTokenizer ; 17 import java.util.Vector ; 18 import org.w3c.dom.DOMImplementationSource ; 19 import org.w3c.dom.DOMImplementationList ; 20 import org.w3c.dom.DOMImplementation ; 21 import java.io.InputStream ; 22 import java.io.BufferedReader ; 23 import java.io.InputStreamReader ; 24 import java.security.AccessController ; 25 import java.security.PrivilegedAction ; 26 27 55 public final class DOMImplementationRegistry { 56 60 public static final String PROPERTY = 61 "org.w3c.dom.DOMImplementationSourceList"; 62 63 66 private static final int DEFAULT_LINE_LENGTH = 80; 67 68 71 private Vector sources; 72 73 77 private DOMImplementationRegistry(final Vector srcs) { 78 sources = srcs; 79 } 80 81 108 public static DOMImplementationRegistry newInstance() 109 throws 110 ClassNotFoundException , 111 InstantiationException , 112 IllegalAccessException , 113 ClassCastException { 114 Vector sources = new Vector (); 115 116 ClassLoader classLoader = getClassLoader(); 117 String p = getSystemProperty(PROPERTY); 119 120 if (p == null) { 124 p = getServiceValue(classLoader); 125 } 126 if (p == null) { 127 131 } 132 if (p != null) { 133 StringTokenizer st = new StringTokenizer (p); 134 while (st.hasMoreTokens()) { 135 String sourceName = st.nextToken(); 136 Class sourceClass = null; 139 if (classLoader != null) { 140 sourceClass = classLoader.loadClass(sourceName); 141 } else { 142 sourceClass = Class.forName(sourceName); 143 } 144 DOMImplementationSource source = 145 (DOMImplementationSource ) sourceClass.newInstance(); 146 sources.addElement(source); 147 } 148 } 149 return new DOMImplementationRegistry (sources); 150 } 151 152 164 public DOMImplementation getDOMImplementation(final String features) { 165 int size = sources.size(); 166 String name = null; 167 for (int i = 0; i < size; i++) { 168 DOMImplementationSource source = 169 (DOMImplementationSource ) sources.elementAt(i); 170 DOMImplementation impl = source.getDOMImplementation(features); 171 if (impl != null) { 172 return impl; 173 } 174 } 175 return null; 176 } 177 178 189 public DOMImplementationList getDOMImplementationList(final String features) { 190 final Vector implementations = new Vector (); 191 int size = sources.size(); 192 for (int i = 0; i < size; i++) { 193 DOMImplementationSource source = 194 (DOMImplementationSource ) sources.elementAt(i); 195 DOMImplementationList impls = 196 source.getDOMImplementationList(features); 197 for (int j = 0; j < impls.getLength(); j++) { 198 DOMImplementation impl = impls.item(j); 199 implementations.addElement(impl); 200 } 201 } 202 return new DOMImplementationList () { 203 public DOMImplementation item(final int index) { 204 if (index >= 0 && index < implementations.size()) { 205 try { 206 return (DOMImplementation ) 207 implementations.elementAt(index); 208 } catch (ArrayIndexOutOfBoundsException e) { 209 return null; 210 } 211 } 212 return null; 213 } 214 215 public int getLength() { 216 return implementations.size(); 217 } 218 }; 219 } 220 221 226 public void addSource(final DOMImplementationSource s) { 227 if (s == null) { 228 throw new NullPointerException (); 229 } 230 if (!sources.contains(s)) { 231 sources.addElement(s); 232 } 233 } 234 235 241 private static ClassLoader getClassLoader() { 242 try { 243 ClassLoader contextClassLoader = getContextClassLoader(); 244 245 if (contextClassLoader != null) { 246 return contextClassLoader; 247 } 248 } catch (Exception e) { 249 return DOMImplementationRegistry .class.getClassLoader(); 252 } 253 return DOMImplementationRegistry .class.getClassLoader(); 254 } 255 256 264 private static String getServiceValue(final ClassLoader classLoader) { 265 String serviceId = "META-INF/services/" + PROPERTY; 266 try { 268 InputStream is = getResourceAsStream(classLoader, serviceId); 269 270 if (is != null) { 271 BufferedReader rd; 272 try { 273 rd = 274 new BufferedReader (new InputStreamReader (is, "UTF-8"), 275 DEFAULT_LINE_LENGTH); 276 } catch (java.io.UnsupportedEncodingException e) { 277 rd = 278 new BufferedReader (new InputStreamReader (is), 279 DEFAULT_LINE_LENGTH); 280 } 281 String serviceValue = rd.readLine(); 282 rd.close(); 283 if (serviceValue != null && serviceValue.length() > 0) { 284 return serviceValue; 285 } 286 } 287 } catch (Exception ex) { 288 return null; 289 } 290 return null; 291 } 292 293 298 private static boolean isJRE11() { 299 try { 300 Class c = Class.forName("java.security.AccessController"); 301 return false; 305 } catch (Exception ex) { 306 } 308 return true; 309 } 310 311 317 private static ClassLoader getContextClassLoader() { 318 return isJRE11() 319 ? null 320 : (ClassLoader ) 321 AccessController.doPrivileged(new PrivilegedAction () { 322 public Object run() { 323 ClassLoader classLoader = null; 324 try { 325 classLoader = 326 Thread.currentThread().getContextClassLoader(); 327 } catch (SecurityException ex) { 328 } 329 return classLoader; 330 } 331 }); 332 } 333 334 342 private static String getSystemProperty(final String name) { 343 return isJRE11() 344 ? (String ) System.getProperty(name) 345 : (String ) AccessController.doPrivileged(new PrivilegedAction () { 346 public Object run() { 347 return System.getProperty(name); 348 } 349 }); 350 } 351 352 361 private static InputStream getResourceAsStream(final ClassLoader classLoader, 362 final String name) { 363 if (isJRE11()) { 364 InputStream ris; 365 if (classLoader == null) { 366 ris = ClassLoader.getSystemResourceAsStream(name); 367 } else { 368 ris = classLoader.getResourceAsStream(name); 369 } 370 return ris; 371 } else { 372 return (InputStream ) 373 AccessController.doPrivileged(new PrivilegedAction () { 374 public Object run() { 375 InputStream ris; 376 if (classLoader == null) { 377 ris = 378 ClassLoader.getSystemResourceAsStream(name); 379 } else { 380 ris = classLoader.getResourceAsStream(name); 381 } 382 return ris; 383 } 384 }); 385 } 386 } 387 } 388 | Popular Tags |