1 18 19 20 package org.apache.tomcat.util.digester; 21 22 import java.lang.reflect.Method ; 23 import java.util.Properties ; 24 25 import javax.xml.parsers.ParserConfigurationException ; 26 import javax.xml.parsers.SAXParser ; 27 import javax.xml.parsers.SAXParserFactory ; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 import org.xml.sax.SAXException ; 32 import org.xml.sax.SAXNotRecognizedException ; 33 import org.xml.sax.SAXNotSupportedException ; 34 35 43 44 public class XercesParser{ 45 46 49 protected static Log log = 50 LogFactory.getLog("org.apache.commons.digester.Digester.sax"); 51 52 53 56 private static final String JAXP_SCHEMA_SOURCE = 57 "http://java.sun.com/xml/jaxp/properties/schemaSource"; 58 59 60 63 protected static String JAXP_SCHEMA_LANGUAGE = 64 "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; 65 66 67 70 protected static String XERCES_DYNAMIC = 71 "http://apache.org/xml/features/validation/dynamic"; 72 73 74 77 protected static String XERCES_SCHEMA = 78 "http://apache.org/xml/features/validation/schema"; 79 80 81 84 protected static float version; 85 86 87 90 protected static String versionNumber = null; 91 92 93 97 private static String getXercesVersion() { 98 String versionNumber = "1.0"; 100 try{ 101 Class versionClass = 103 Class.forName("org.apache.xerces.impl.Version"); 104 Method method = 106 versionClass.getMethod("getVersion", (Class []) null); 107 String version = (String )method.invoke(null, (Object []) null); 108 versionNumber = version.substring( "Xerces-J".length() , 109 version.lastIndexOf(".") ); 110 } catch (Exception ex){ 111 } 113 return versionNumber; 114 } 115 116 117 123 public static SAXParser newSAXParser(Properties properties) 124 throws ParserConfigurationException , 125 SAXException , 126 SAXNotSupportedException { 127 128 SAXParserFactory factory = 129 (SAXParserFactory )properties.get("SAXParserFactory"); 130 131 if (versionNumber == null){ 132 versionNumber = getXercesVersion(); 133 version = new Float ( versionNumber ).floatValue(); 134 } 135 136 if (version > 2.1) { 138 139 configureXerces(factory); 140 return factory.newSAXParser(); 141 } else { 142 SAXParser parser = factory.newSAXParser(); 143 configureOldXerces(parser,properties); 144 return parser; 145 } 146 } 147 148 149 155 private static void configureOldXerces(SAXParser parser, 156 Properties properties) 157 throws ParserConfigurationException , 158 SAXNotSupportedException { 159 160 String schemaLocation = (String )properties.get("schemaLocation"); 161 String schemaLanguage = (String )properties.get("schemaLanguage"); 162 163 try{ 164 if (schemaLocation != null) { 165 parser.setProperty(JAXP_SCHEMA_LANGUAGE, schemaLanguage); 166 parser.setProperty(JAXP_SCHEMA_SOURCE, schemaLocation); 167 } 168 } catch (SAXNotRecognizedException e){ 169 log.info(parser.getClass().getName() + ": " 170 + e.getMessage() + " not supported."); 171 } 172 173 } 174 175 176 181 private static void configureXerces(SAXParserFactory factory) 182 throws ParserConfigurationException , 183 SAXNotRecognizedException , 184 SAXNotSupportedException { 185 186 factory.setFeature(XERCES_DYNAMIC, true); 187 factory.setFeature(XERCES_SCHEMA, true); 188 189 } 190 } 191 | Popular Tags |