package test.at.gv.egovernment.moa.util; import java.io.FileInputStream; import java.io.InputStream; import org.apache.xerces.parsers.DOMParser; import org.apache.xerces.parsers.XMLGrammarPreparser; import org.apache.xerces.util.SymbolTable; import org.apache.xerces.util.XMLGrammarPoolImpl; import org.apache.xerces.xni.grammars.Grammar; import org.apache.xerces.xni.grammars.XMLGrammarDescription; import org.apache.xerces.xni.parser.XMLInputSource; import org.xml.sax.InputSource; import test.at.gv.egovernment.moa.MOATestCase; import at.gv.egovernment.moa.util.Constants; /** * Experimentation with Xerces grammar caching. * * Used the Xerces sample 'XMLGrammarBuilder' as a starting point. * * @author Patrick Peck * @version $Id$ */ public class XMLGrammarBuilderTest extends MOATestCase { private static final String GRAMMAR_POOL = org.apache.xerces.impl.Constants.XERCES_PROPERTY_PREFIX + org.apache.xerces.impl.Constants.XMLGRAMMAR_POOL_PROPERTY; protected static final String NAMESPACES_FEATURE_ID = "http://xml.org/sax/features/namespaces"; protected static final String VALIDATION_FEATURE_ID = "http://xml.org/sax/features/validation"; protected static final String SCHEMA_VALIDATION_FEATURE_ID = "http://apache.org/xml/features/validation/schema"; protected static final String SCHEMA_FULL_CHECKING_FEATURE_ID = "http://apache.org/xml/features/validation/schema-full-checking"; private static final int BIG_PRIME = 2039; private SymbolTable symbolTable; private XMLGrammarPoolImpl grammarPool; /** * Constructor for XMLGrammarBuilderTest. * @param name */ public XMLGrammarBuilderTest(String name) { super(name); } protected void setUp() throws Exception { XMLGrammarPreparser preparser; // set up symbol table and grammar pool symbolTable = new SymbolTable(BIG_PRIME); grammarPool = new XMLGrammarPoolImpl(); preparser = new XMLGrammarPreparser(symbolTable); preparser.registerPreparser(XMLGrammarDescription.XML_SCHEMA, null); preparser.setProperty(GRAMMAR_POOL, grammarPool); preparser.setFeature(NAMESPACES_FEATURE_ID, true); preparser.setFeature(VALIDATION_FEATURE_ID, true); // now we can still do schema features just in case, // so long as it's our configuraiton...... preparser.setFeature(SCHEMA_VALIDATION_FEATURE_ID, true); preparseSchemaResource( preparser, Constants.DSIG_SCHEMA_LOCATION, "/resources/schemas/xmldsig-core-schema.xsd"); } private static Grammar preparseSchemaResource( XMLGrammarPreparser preparser, String systemId, String resource) throws Exception { InputStream is = XMLGrammarBuilderTest.class.getResourceAsStream(resource); return preparser.preparseGrammar( XMLGrammarDescription.XML_SCHEMA, new XMLInputSource(null, systemId, null, is, null)); } public void testParseValidating() throws Exception { DOMParser parser = new DOMParser(symbolTable, grammarPool); parser.setFeature(NAMESPACES_FEATURE_ID, true); parser.setFeature(VALIDATION_FEATURE_ID, true); parser.setFeature(SCHEMA_VALIDATION_FEATURE_ID, true); parser.parse( new InputSource( new FileInputStream(TESTDATA_ROOT + "xml/dsigTransform/base64.xml"))); parser.getDocument(); } }