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