Hello again Elena,
thanks for the pointer. Attached is the patch. It seems to work in one
schema per document case. I also had to patch DOMBuilderImpl slightly.
I also added setProperty() method in DOMBuilderImpl so that I could set
the document class. This should be removed if a more standard way to do
this is created.
The next problem I'm facing is the following:
normalizeDocument does not seem to update the PSVI information, at least
if new nodes are added to the document. Any pointers on how should I fix
this? Maybe I should create a new documentHandler for
XMLSchemaValidator, which then calls setPSVI method in endElement?
Regards,
Mikko
Elena Litani wrote:
> Hi Mikko,
>
> Mikko Honkala wrote:
>
>>Just one more quick question: if the document was loaded
>>& validated using a schema, is there currently any way to prevent schema reloading
>>in normalizeDocument()?
>
>
> Well, not yet. But you can try to prototype it and submit a patch.
>
> Look at
> org.apache.xerces.dom.CoreDocumentImpl.copyConfigurationProperties
> method.
> In this method you have access to the parser grammar pool and you can
> retrieve the grammar used to validate this document.
> Then create a DOMGrammarPool with this grammar and the grammar pool to
> the DOMValidationConfiguration (see
> org.apache.xerces.dom.CoreDocumentImpl.normalizeDocument()).
>
> That should stop re-loading of the grammar each time.
>
> Good luck,
Index: src/org/apache/xerces/dom/CoreDocumentImpl.java
===================================================================
RCS file:
/home/cvspublic/xml-xerces/java/src/org/apache/xerces/dom/CoreDocumentImpl.java,v
retrieving revision 1.28
diff -u -r1.28 CoreDocumentImpl.java
--- src/org/apache/xerces/dom/CoreDocumentImpl.java 23 Aug 2002 20:14:38 -0000
1.28
+++ src/org/apache/xerces/dom/CoreDocumentImpl.java 10 Sep 2002 11:45:32 -0000
@@ -97,6 +97,8 @@
import org.apache.xerces.util.SymbolTable;
import org.apache.xerces.util.DOMErrorHandlerWrapper;
import org.apache.xerces.util.ShadowedSymbolTable;
+import org.apache.xerces.util.XMLGrammarPoolImpl;
+
import org.apache.xerces.xni.parser.XMLErrorHandler;
import org.apache.xerces.xni.parser.XMLEntityResolver;
import org.apache.xerces.xni.parser.XMLParserConfiguration;
@@ -104,6 +106,8 @@
import org.apache.xerces.xni.grammars.Grammar;
import org.apache.xerces.xni.grammars.XMLGrammarDescription;
+import org.apache.xerces.impl.xs.XSDDescription;
+
/**
* The Document interface represents the entire HTML or XML document.
* Conceptually, it is the root of the document tree, and provides the
@@ -1051,10 +1055,16 @@
// set xml-schema validator handler
domNormalizer.setValidationHandler(
CoreDOMImplementationImpl.singleton.getValidator(XMLGrammarDescription.XML_SCHEMA));
-
+ XMLGrammarPool grammarPool = new XMLGrammarPoolImpl();
+ // set our own grammar pool
+ fConfiguration.setProperty(DOMValidationConfiguration.GRAMMAR_POOL,
+grammarPool);
+ Grammar[] grammars = new Grammar[]{
+ fGrammar
+ };
+
+grammarPool.cacheGrammars(XMLGrammarDescription.XML_SCHEMA,grammars);
if (fGrammar != null) {
- fConfiguration.setProperty(DOMValidationConfiguration.GRAMMAR_POOL,
domNormalizer);
+ fConfiguration.setProperty(DOMValidationConfiguration.GRAMMAR_POOL,
+grammarPool);
}
}
else { // remove validation handler
@@ -2329,7 +2339,11 @@
// retrieve XML Schema grammar based on teh namespace
// of the root element
String targetNamespace = this.docElement.getNamespaceURI();
- // pool.retrieveGrammar();
+ XSDDescription grammarDesc = new XSDDescription();
+ grammarDesc.setContextType(XSDDescription.CONTEXT_PREPARSE);
+ grammarDesc.setTargetNamespace(targetNamespace);
+ //Grammar grammar = pool.retrieveGrammar(grammarDesc);
+ fGrammar = pool.retrieveGrammar(grammarDesc);
}
}
}
Index: src/org/apache/xerces/parsers/DOMBuilderImpl.java
===================================================================
RCS file:
/home/cvspublic/xml-xerces/java/src/org/apache/xerces/parsers/DOMBuilderImpl.java,v
retrieving revision 1.21
diff -u -r1.21 DOMBuilderImpl.java
--- src/org/apache/xerces/parsers/DOMBuilderImpl.java 26 Aug 2002 21:53:30 -0000
1.21
+++ src/org/apache/xerces/parsers/DOMBuilderImpl.java 10 Sep 2002 11:45:33 -0000
@@ -77,6 +77,7 @@
import org.w3c.dom.ls.DOMInputSource;
import org.apache.xerces.dom.DOMErrorImpl;
+import org.apache.xerces.dom.DOMValidationConfiguration;
import org.apache.xerces.impl.Constants;
import org.apache.xerces.util.DOMEntityResolverWrapper;
import org.apache.xerces.util.SymbolTable;
@@ -90,6 +91,11 @@
import org.apache.xerces.xni.parser.XMLInputSource;
import org.apache.xerces.xni.parser.XMLParserConfiguration;
+// For setProperty
+import org.xml.sax.SAXParseException;
+import org.xml.sax.SAXNotRecognizedException;
+import org.xml.sax.SAXNotSupportedException;
+
/**
* This is Xerces DOM Builder class. It uses the abstract DOM
@@ -126,6 +132,11 @@
protected static final String DYNAMIC_VALIDATION =
Constants.XERCES_FEATURE_PREFIX + Constants.DYNAMIC_VALIDATION_FEATURE;
+ /** Property id: Grammar pool*/
+ protected static final String GRAMMAR_POOL =
+ Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY;
+
+
// DOM L3 Schema validation types:
protected static final String XML_SCHEMA_VALIDATION =
"http://www.w3.org/2001/XMLSchema";
@@ -143,6 +154,8 @@
protected final static boolean DEBUG = false;
protected DOMErrorHandlerWrapper fErrorHandler = null;
+
+ protected XMLGrammarPool fGrammarPool;
//
// Constructors
//
@@ -557,7 +570,6 @@
// to retrieve a grammar used in validation of the document
// for dom revalidation?
- /*
fGrammarPool = (XMLGrammarPool)fConfiguration.getProperty(GRAMMAR_POOL);
if (fGrammarPool == null) {
fGrammarPool = new XMLGrammarPoolImpl();
@@ -567,8 +579,6 @@
else {
//REVISIT: if user sets its own grammar pool do we do anything?
}
- */
-
}
/**
@@ -654,6 +664,40 @@
}
} // endDocument()
+
+
+ /**
+ * Set the value of any property in a SAX2 parser. The parser
+ * might not recognize the property, and if it does recognize
+ * it, it might not support the requested value.
+ *
+ * @param propertyId The unique identifier (URI) of the property
+ * being set.
+ * @param Object The value to which the property is being set.
+ *
+ * @exception SAXNotRecognizedException If the
+ * requested property is not known.
+ * @exception SAXNotSupportedException If the
+ * requested property is known, but the requested
+ * value is not supported.
+ */
+ public void setProperty(String propertyId, Object value)
+ throws SAXNotRecognizedException, SAXNotSupportedException {
+
+ try {
+ fConfiguration.setProperty(propertyId, value);
+ }
+ catch (XMLConfigurationException e) {
+ String message = e.getMessage();
+ if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) {
+ throw new SAXNotRecognizedException(message);
+ }
+ else {
+ throw new SAXNotSupportedException(message);
+ }
+ }
+
+ } // setProperty(String,Object)
} // class DOMBuilderImpl
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]