1 64 package com.jcorporate.expresso.kernel.digester; 65 66 import com.jcorporate.expresso.kernel.util.ClassLocator; 67 import org.apache.log4j.Logger; 68 import org.xml.sax.SAXException ; 69 70 77 78 public class SaxParserConfigurer { 79 static boolean configured = false; 80 81 private static final Logger log = Logger.getLogger(SaxParserConfigurer.class); 82 83 public SaxParserConfigurer() { 84 setSAXParser(); 85 } 86 87 91 synchronized void setSAXParser() { 92 synchronized (SaxParserConfigurer.class) { 93 if (configured) { 94 return; 95 } 96 97 String curParser = System.getProperty("javax.xml.parsers.SAXParserFactory", null); 102 if (curParser == null) { 103 curParser = System.getProperty("org.xml.sax.parser", null); 104 } 105 106 if (curParser != null && curParser.length() > 0) { 107 System.out.println("SAX Parser already explicitly defined. It is: " + 108 curParser); 109 configured = true; 110 return; 111 } 112 113 114 115 MySAXParser[] parserList = 117 { 118 new MySAXParser("org.apache.xerces.jaxp.SAXParserFactoryImpl", 119 "org.apache.xerces.parsers.SAXParser", 120 "Apache Xerces"), 121 122 new MySAXParser("org.apache.crimson.jaxp.SAXParserFactoryImpl", 123 "org.apache.crimson.parser.XMLReaderImpl", 124 "Apache Crimson"), 125 126 new MySAXParser("com.jclark.xml.jaxp.SAXParserFactoryImpl", 131 "com.jclark.xml.sax.Driver", 132 "James Clark's XP with SAX Additions"), 133 134 new MySAXParser("gnu.xml.aelfred2.JAXPFactory", 138 "gnu.xml.dom.JAXPFactory", 139 "GNU JAXP"), 140 141 }; 142 143 int idx = 0; 146 while ((idx < parserList.length) && 147 (!parserList[idx].existsInClasspath())) { 148 idx++; 149 } 150 151 if (idx < parserList.length) { 154 log.info("Setting default XML parser factory to " + parserList[idx].getDesc()); 155 System.setProperty("javax.xml.parsers.SAXParserFactory", parserList[idx].getName()); 156 System.setProperty("org.xml.sax.driver", parserList[idx].getSAXDriverName()); 157 } else { 158 log.info("Couldn't find a suitable SAX/JAXP-compliant XML parser"); 159 log.info("Please put Apache Crimson or Xerces in classpath"); 160 } 161 162 configured = true; 163 } 164 } 165 166 171 class MySAXParser { 172 private String className = null; 174 175 private String saxDriverClassName = null; 177 178 private String classDesc = null; 180 181 public MySAXParser(String cName, String sdcName, String cDesc) { 183 className = cName; 184 saxDriverClassName = sdcName; 185 classDesc = cDesc; 186 } 187 188 private MySAXParser() { 190 } 191 192 public String getName() { 193 return className; 194 } 195 196 public String getSAXDriverName() { 197 return saxDriverClassName; 198 } 199 200 public String getDesc() { 201 return classDesc; 202 } 203 204 public boolean existsInClasspath() { 206 boolean returnValue = true; 207 String oldProperty = null; 208 try { 209 Class clazz = ClassLocator.loadClass(getName()); 210 211 if (oldProperty == null) { 220 oldProperty = System.getProperty("javax.xml.parsers.SAXParserFactory"); 221 } 222 try { 223 System.setProperty("javax.xml.parsers.SAXParserFactory", getName()); 224 javax.xml.parsers.SAXParserFactory spf = javax.xml.parsers.SAXParserFactory.newInstance(); 225 javax.xml.parsers.SAXParser sp = spf.newSAXParser(); 226 } catch (java.security.AccessControlException ex) { 227 log.warn("App Server would not let us set the SAX Parser to another parser."); 228 } 229 } catch (ClassNotFoundException cnfe) { 230 returnValue = false; 231 } catch (javax.xml.parsers.ParserConfigurationException ex) { 232 System.err.println( 233 "Found class " + getName() + " but unable to instantiate SAX Parser " + ex.getMessage()); 234 ex.printStackTrace(System.err); 235 returnValue = false; 236 } catch (SAXException ex) { 237 System.err.println( 238 "Found class " + getName() + " but unable to instantiate SAX Parser " + ex.getMessage()); 239 ex.printStackTrace(System.err); 240 returnValue = false; 241 } catch (javax.xml.parsers.FactoryConfigurationError ex) { 242 System.err.println( 243 "Found class " + getName() + " but unable to instantiate ClassFactory " + ex.getMessage()); 244 ex.printStackTrace(System.err); 245 returnValue = false; 246 } 247 248 if (oldProperty != null) { 251 System.setProperty("javax.xml.parsers.SAXParserFactory", oldProperty); 252 } 253 254 return returnValue; 255 } 256 } 257 } | Popular Tags |