1 8 package org.xml.sax.helpers; 9 import java.io.BufferedReader ; 10 import java.io.InputStream ; 11 import java.io.InputStreamReader ; 12 import org.xml.sax.XMLReader ; 13 import org.xml.sax.SAXException ; 14 15 16 49 final public class XMLReaderFactory 50 { 51 56 private XMLReaderFactory () 57 { 58 } 59 60 private static final String property = "org.xml.sax.driver"; 61 62 104 public static XMLReader createXMLReader () 105 throws SAXException 106 { 107 String className = null; 108 ClassLoader loader = NewInstance.getClassLoader (); 109 110 try { className = System.getProperty (property); } 112 catch (RuntimeException e) { } 113 114 if (className == null) { 116 try { 117 String service = "META-INF/services/" + property; 118 InputStream in; 119 BufferedReader reader; 120 121 if (loader == null) 122 in = ClassLoader.getSystemResourceAsStream (service); 123 else 124 in = loader.getResourceAsStream (service); 125 126 if (in != null) { 127 reader = new BufferedReader ( 128 new InputStreamReader (in, "UTF8")); 129 className = reader.readLine (); 130 in.close (); 131 } 132 } catch (Exception e) { 133 } 134 } 135 136 if (className == null) { 138 140 className = "com.sun.org.apache.xerces.internal.parsers.SAXParser"; 144 145 } 147 148 if (className != null) 150 return loadClass (loader, className); 151 152 try { 154 return new ParserAdapter (ParserFactory.makeParser ()); 155 } catch (Exception e) { 156 throw new SAXException ("Can't create default XMLReader; " 157 + "is system property org.xml.sax.driver set?"); 158 } 159 } 160 161 162 177 public static XMLReader createXMLReader (String className) 178 throws SAXException 179 { 180 return loadClass (NewInstance.getClassLoader (), className); 181 } 182 183 private static XMLReader loadClass (ClassLoader loader, String className) 184 throws SAXException 185 { 186 try { 187 return (XMLReader ) NewInstance.newInstance (loader, className); 188 } catch (ClassNotFoundException e1) { 189 throw new SAXException ("SAX2 driver class " + className + 190 " not found", e1); 191 } catch (IllegalAccessException e2) { 192 throw new SAXException ("SAX2 driver class " + className + 193 " found but cannot be loaded", e2); 194 } catch (InstantiationException e3) { 195 throw new SAXException ("SAX2 driver class " + className + 196 " loaded but cannot be instantiated (no empty public constructor?)", 197 e3); 198 } catch (ClassCastException e4) { 199 throw new SAXException ("SAX2 driver class " + className + 200 " does not implement XMLReader", e4); 201 } 202 } 203 } 204 | Popular Tags |