FileDocCategorySizeDatePackage
XercesParser.javaAPI DocApache Tomcat 6.0.146540Fri Jul 20 04:20:34 BST 2007org.apache.tomcat.util.digester

XercesParser

public class XercesParser extends Object
Create a SAXParser based on the underlying Xerces version. Currently, Xerces 2.3 and up doesn't implement schema validation the same way 2.1 was. In other to support schema validation in a portable way between parser, some features/properties need to be set.
since
1.6

Fields Summary
protected static org.apache.juli.logging.Log
log
The Log to which all SAX event related logging calls will be made.
private static final String
JAXP_SCHEMA_SOURCE
The JAXP 1.2 property required to set up the schema location.
protected static String
JAXP_SCHEMA_LANGUAGE
The JAXP 1.2 property to set up the schemaLanguage used.
protected static String
XERCES_DYNAMIC
Xerces dynamic validation property
protected static String
XERCES_SCHEMA
Xerces schema validation property
protected static float
version
A float representing the underlying Xerces version
protected static String
versionNumber
The current Xerces version.
Constructors Summary
Methods Summary
private static voidconfigureOldXerces(javax.xml.parsers.SAXParser parser, java.util.Properties properties)
Configure schema validation as recommended by the JAXP 1.2 spec. The properties object may contains information about the schema local and language.

param
properties parser optional info


        String schemaLocation = (String)properties.get("schemaLocation");
        String schemaLanguage = (String)properties.get("schemaLanguage");

        try{
            if (schemaLocation != null) {
                parser.setProperty(JAXP_SCHEMA_LANGUAGE, schemaLanguage);
                parser.setProperty(JAXP_SCHEMA_SOURCE, schemaLocation);
            }
        } catch (SAXNotRecognizedException e){
            log.info(parser.getClass().getName() + ": " 
                                        + e.getMessage() + " not supported."); 
        }

    
private static voidconfigureXerces(javax.xml.parsers.SAXParserFactory factory)
Configure schema validation as recommended by the Xerces spec. Both DTD and Schema validation will be enabled simultaneously.

param
factory SAXParserFactory to be configured


        factory.setFeature(XERCES_DYNAMIC, true);
        factory.setFeature(XERCES_SCHEMA, true);

    
private static java.lang.StringgetXercesVersion()
Return the current Xerces version.

return
the current Xerces version.



                   
        
        // If for some reason we can't get the version, set it to 1.0.
        String versionNumber = "1.0";
        try{
            // Use reflection to avoid a build dependency with Xerces.
            Class versionClass = 
                            Class.forName("org.apache.xerces.impl.Version");
            // Will return Xerces-J 2.x.0
            Method method = 
                versionClass.getMethod("getVersion", (Class[]) null); 
            String version = (String)method.invoke(null, (Object[]) null);
            versionNumber = version.substring( "Xerces-J".length() , 
                                               version.lastIndexOf(".") ); 
        } catch (Exception ex){
            // Do nothing.
        }
        return versionNumber;
    
public static javax.xml.parsers.SAXParsernewSAXParser(java.util.Properties properties)
Create a SAXParser based on the underlying Xerces version.

param
properties parser specific properties/features
return
an XML Schema/DTD enabled SAXParser


        SAXParserFactory factory =  
                        (SAXParserFactory)properties.get("SAXParserFactory");

        if (versionNumber == null){
            versionNumber = getXercesVersion();
            version = new Float( versionNumber ).floatValue();
        }

        // Note: 2.2 is completely broken (with XML Schema). 
        if (version > 2.1) {

            configureXerces(factory);
            return factory.newSAXParser();
        } else {
            SAXParser parser = factory.newSAXParser();
            configureOldXerces(parser,properties);
            return parser;
        }