You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by pi...@apache.org on 2005/09/07 23:18:53 UTC
svn commit: r279423 - in
/cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon:
components/validation/ components/validation/impl/ transformation/
Author: pier
Date: Wed Sep 7 14:18:43 2005
New Revision: 279423
URL: http://svn.apache.org/viewcvs?rev=279423&view=rev
Log:
Starting to split up Validation and XML Schema validation in Xerces
Added:
cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesGrammarParser.java
Removed:
cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesErrorWrapper.java
Modified:
cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/Schema.java
cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/AbstractSchema.java
cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/JingSchema.java
cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesComponentManager.java
cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesSchema.java
cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesSchemaParser.java
cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesValidationHandler.java
cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/transformation/ValidatingTransformer.java
Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/Schema.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/Schema.java?rev=279423&r1=279422&r2=279423&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/Schema.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/Schema.java Wed Sep 7 14:18:43 2005
@@ -18,6 +18,7 @@
import org.apache.excalibur.source.SourceValidity;
import org.xml.sax.ContentHandler;
import org.xml.sax.ErrorHandler;
+import org.xml.sax.SAXException;
/**
* <p>An interface defining a schema used for validation of XML documents.</p>
@@ -53,8 +54,10 @@
* <p>Once used, the returned {@link ContentHandler} <b>can't</b> be reused.</p>
*
* @return a <b>non-null</b> {@link ContentHandler} instance.
+ * @throws SAXException if an error occurred creating the validation handler.
*/
- public ContentHandler newValidator();
+ public ContentHandler newValidator()
+ throws SAXException;
/**
* <p>Return a new {@link ContentHandler} instance that can be used to send SAX
@@ -68,7 +71,9 @@
*
* @param handler an {@link ErrorHandler} to notify of validation errors.
* @return a <b>non-null</b> {@link ContentHandler} instance.
+ * @throws SAXException if an error occurred creating the validation handler.
*/
- public ContentHandler newValidator(ErrorHandler handler);
+ public ContentHandler newValidator(ErrorHandler handler)
+ throws SAXException;
}
Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/AbstractSchema.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/AbstractSchema.java?rev=279423&r1=279422&r2=279423&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/AbstractSchema.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/AbstractSchema.java Wed Sep 7 14:18:43 2005
@@ -60,8 +60,10 @@
* <p>Once used, the returned {@link ContentHandler} <b>can't</b> be reused.</p>
*
* @return a <b>non-null</b> {@link ContentHandler} instance.
+ * @throws SAXException if an error occurred creating the validation handler.
*/
- public ContentHandler newValidator() {
+ public ContentHandler newValidator()
+ throws SAXException {
ErrorHandler handler = new ErrorHandler() {
public void warning(SAXParseException e) throws SAXException {
throw e;
Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/JingSchema.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/JingSchema.java?rev=279423&r1=279422&r2=279423&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/JingSchema.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/JingSchema.java Wed Sep 7 14:18:43 2005
@@ -19,6 +19,7 @@
import org.apache.excalibur.source.SourceValidity;
import org.xml.sax.ContentHandler;
import org.xml.sax.ErrorHandler;
+import org.xml.sax.SAXException;
import com.thaiopensource.util.PropertyMap;
import com.thaiopensource.util.PropertyMapBuilder;
@@ -59,8 +60,10 @@
*
* @param errorHandler an {@link ErrorHandler} to notify of validation errors.
* @return a <b>non-null</b> {@link ContentHandler} instance.
+ * @throws SAXException if an error occurred creating the validation handler.
*/
- public ContentHandler newValidator(ErrorHandler errorHandler) {
+ public ContentHandler newValidator(ErrorHandler errorHandler)
+ throws SAXException {
if (errorHandler == null) errorHandler = new DraconianErrorHandler();
final PropertyMapBuilder builder = new PropertyMapBuilder();
ValidateProperty.ERROR_HANDLER.put(builder, errorHandler);
Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesComponentManager.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesComponentManager.java?rev=279423&r1=279422&r2=279423&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesComponentManager.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesComponentManager.java Wed Sep 7 14:18:43 2005
@@ -15,22 +15,31 @@
*/
package org.apache.cocoon.components.validation.impl;
+import java.util.Iterator;
+
import org.apache.xerces.impl.Constants;
import org.apache.xerces.impl.XMLEntityManager;
import org.apache.xerces.impl.XMLErrorReporter;
+import org.apache.xerces.impl.msg.XMLMessageFormatter;
import org.apache.xerces.impl.validation.ValidationManager;
import org.apache.xerces.impl.xs.XSMessageFormatter;
import org.apache.xerces.util.NamespaceSupport;
import org.apache.xerces.util.ParserConfigurationSettings;
import org.apache.xerces.util.SymbolTable;
+import org.apache.xerces.xni.XNIException;
import org.apache.xerces.xni.grammars.XMLGrammarPool;
+import org.apache.xerces.xni.parser.XMLComponent;
import org.apache.xerces.xni.parser.XMLEntityResolver;
import org.apache.xerces.xni.parser.XMLErrorHandler;
+import org.apache.xerces.xni.parser.XMLParseException;
import org.xml.sax.ErrorHandler;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
/**
- * <p>TODO: ...</p>
+ * <p>An implementation of Xerces' {@link XMLComponentManager} interface
+ * allowing interoperation of components while parsing or validatin.</p>
*
* @author <a href="mailto:pier@betaversion.org">Pier Fumagalli</a>
*/
@@ -81,6 +90,11 @@
};
public XercesComponentManager(XMLGrammarPool grammarPool,
+ XMLEntityResolver entityResolver) {
+ this(grammarPool, entityResolver, null);
+ }
+
+ public XercesComponentManager(XMLGrammarPool grammarPool,
XMLEntityResolver entityResolver,
final ErrorHandler errorHandler) {
@@ -90,7 +104,11 @@
XMLErrorReporter errorReporter = new XMLErrorReporter();
errorReporter.putMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN,
new XSMessageFormatter());
- XMLErrorHandler xercesHandler = new XercesErrorWrapper(errorHandler);
+ errorReporter.putMessageFormatter(XMLMessageFormatter.XML_DOMAIN,
+ new XMLMessageFormatter());
+ errorReporter.putMessageFormatter(XMLMessageFormatter.XMLNS_DOMAIN,
+ new XMLMessageFormatter());
+ XMLErrorHandler xercesHandler = new ErrorHandlerWrapper(errorHandler);
super.setProperty(P_XMLGRAMMAR_POOL, grammarPool);
super.setProperty(P_ENTITY_RESOLVER, entityResolver);
@@ -108,6 +126,69 @@
super.setFeature(F_SCHEMA_VALIDATION, true);
/* Initialize the configured Error Reporter */
- errorReporter.reset(this);
+ Iterator iterator = super.fProperties.values().iterator();
+ while (iterator.hasNext()) {
+ Object object = iterator.next();
+ if (object instanceof XMLComponent) {
+ ((XMLComponent) object).reset(this);
+ }
+ }
+ }
+
+ /**
+ * <p>A simple wrapper around a SAX {@link ErrorHandler} exposing the
+ * handler as a Xerces {@link XMLErrorHandler}.</p>
+ */
+ private static final class ErrorHandlerWrapper implements XMLErrorHandler {
+
+ private final ErrorHandler errorHandler;
+
+ private ErrorHandlerWrapper(ErrorHandler errorHandler) {
+ this.errorHandler = errorHandler;
+ }
+
+ public void warning(String domain, String key, XMLParseException e)
+ throws XNIException {
+ if (this.errorHandler != null) try {
+ this.errorHandler.warning(this.makeException(e));
+ } catch (SAXException saxException) {
+ throw new XNIException(saxException);
+ } else {
+ throw e;
+ }
+ }
+
+ public void error(String domain, String key, XMLParseException e)
+ throws XNIException {
+ if (this.errorHandler != null) try {
+ this.errorHandler.warning(this.makeException(e));
+ } catch (SAXException saxException) {
+ throw new XNIException(saxException);
+ } else {
+ throw e;
+ }
+ }
+
+ public void fatalError(String domain, String key, XMLParseException e)
+ throws XNIException {
+ if (this.errorHandler != null) try {
+ this.errorHandler.warning(this.makeException(e));
+ } catch (SAXException saxException) {
+ throw new XNIException(saxException);
+ } else {
+ throw e;
+ }
+ }
+
+ private SAXParseException makeException(XMLParseException exception) {
+ final SAXParseException saxParseException;
+ saxParseException = new SAXParseException(exception.getMessage(),
+ exception.getPublicId(),
+ exception.getLiteralSystemId(),
+ exception.getLineNumber(),
+ exception.getColumnNumber(),
+ exception);
+ return (SAXParseException) saxParseException.initCause(exception);
+ }
}
}
Added: cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesGrammarParser.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesGrammarParser.java?rev=279423&view=auto
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesGrammarParser.java (added)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesGrammarParser.java Wed Sep 7 14:18:43 2005
@@ -0,0 +1,90 @@
+/*
+ * Copyright 1999-2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.components.validation.impl;
+
+import java.io.IOException;
+
+import org.apache.cocoon.components.validation.Schema;
+import org.apache.cocoon.components.validation.SchemaParser;
+import org.apache.excalibur.source.SourceValidity;
+import org.apache.xerces.impl.Constants;
+import org.apache.xerces.util.XMLGrammarPoolImpl;
+import org.apache.xerces.xni.grammars.XMLGrammarLoader;
+import org.apache.xerces.xni.grammars.XMLGrammarPool;
+import org.apache.xerces.xni.parser.XMLComponent;
+import org.xml.sax.SAXException;
+
+/**
+ * <p>The implementation of the {@link SchemaParser} interface using the internals
+ * of <a href="http://xml.apache.org/xerces2-j/">Xerces</a>.</p>
+ *
+ * @author <a href="mailto:pier@betaversion.org">Pier Fumagalli</a>
+ */
+public abstract class XercesGrammarParser extends CachingSchemaParser
+implements SchemaParser {
+
+ private static final String P_XML_GRAMMAR_POOL =
+ Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY;
+
+ /**
+ * <p>Create a new {@link XercesGrammarParser} instance.</p>
+ */
+ public XercesGrammarParser() {
+ super();
+ }
+
+ /**
+ * <p>Parse the specified URI and return a {@link Schema}.</p>
+ *
+ * @param uri the URI of the {@link Schema} to return.
+ * @return a <b>non-null</b> {@link Schema} instance.
+ * @throws SAXException if an error occurred parsing the schema.
+ * @throws IOException if an I/O error occurred parsing the schema.
+ */
+ protected final Schema parseSchema(String uri)
+ throws IOException, SAXException {
+ /* Create a Xerces Grammar Pool and Entity Resolver */
+ XMLGrammarPool pool = new XMLGrammarPoolImpl();
+ XercesEntityResolver resolver = new XercesEntityResolver();
+
+ /* Create a Xerces component manager contextualizing the loader */
+ XercesComponentManager manager = new XercesComponentManager(pool, resolver);
+
+ /* Create a new XML Schema Loader and set the pool into it */
+ XMLGrammarLoader loader = this.newGrammarLoader();
+ ((XMLComponent) loader).reset(manager);
+
+ /* Load (parse and interpret) the grammar */
+ loader.loadGrammar(resolver.resolveEntity(uri));
+
+ /* Return a new Schema instance */
+ SourceValidity validity = resolver.getSourceValidity();
+ Class validator = this.getValidationHandler();
+ return new XercesSchema(pool, validity, resolver, validator);
+ }
+
+ /**
+ * <p>Return a {@link XMLGrammarLoader} instance able to read the grammar
+ * handled by this {@link SchemaParser}.</p>
+ */
+ protected abstract XMLGrammarLoader newGrammarLoader();
+
+ /**
+ * <p>Return the {@link Class} that will implement the validator handler.</p>
+ */
+ protected abstract Class getValidationHandler();
+
+}
Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesSchema.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesSchema.java?rev=279423&r1=279422&r2=279423&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesSchema.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesSchema.java Wed Sep 7 14:18:43 2005
@@ -16,9 +16,12 @@
package org.apache.cocoon.components.validation.impl;
import org.apache.excalibur.source.SourceValidity;
+import org.apache.xerces.xni.XMLDocumentHandler;
import org.apache.xerces.xni.grammars.XMLGrammarPool;
+import org.apache.xerces.xni.parser.XMLComponent;
import org.xml.sax.ContentHandler;
import org.xml.sax.ErrorHandler;
+import org.xml.sax.SAXException;
/**
* <p>TODO: ...</p>
@@ -27,15 +30,28 @@
*/
public class XercesSchema extends AbstractSchema {
+ private final XercesEntityResolver entityResolver;
private final XMLGrammarPool grammarPool;
+ private final Class validatorClass;
- public XercesSchema(XMLGrammarPool grammarPool, SourceValidity validity) {
- super(validity);
- this.grammarPool = grammarPool;
+ public XercesSchema(XMLGrammarPool grammarPool, SourceValidity sourceValidity,
+ XercesEntityResolver entityResolver, Class validatorClass) {
+ super(sourceValidity);
grammarPool.lockPool();
+ this.entityResolver = entityResolver;
+ this.validatorClass = validatorClass;
+ this.grammarPool = grammarPool;
}
- public ContentHandler newValidator(ErrorHandler handler) {
- return new XercesValidationHandler(this.grammarPool);
+ public ContentHandler newValidator(ErrorHandler errorHandler)
+ throws SAXException {
+ XercesComponentManager manager = new XercesComponentManager(this.grammarPool, entityResolver, errorHandler);
+ try {
+ Object instance = validatorClass.newInstance();
+ ((XMLComponent) instance).reset(manager);
+ return new XercesValidationHandler((XMLDocumentHandler) instance);
+ } catch (Exception exception) {
+ throw new SAXException("Unable to access validator", exception);
+ }
}
}
Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesSchemaParser.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesSchemaParser.java?rev=279423&r1=279422&r2=279423&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesSchemaParser.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesSchemaParser.java Wed Sep 7 14:18:43 2005
@@ -15,19 +15,12 @@
*/
package org.apache.cocoon.components.validation.impl;
-import java.io.IOException;
-
-import org.apache.cocoon.components.validation.Schema;
import org.apache.cocoon.components.validation.SchemaParser;
import org.apache.cocoon.components.validation.Validator;
import org.apache.xerces.impl.Constants;
import org.apache.xerces.impl.xs.XMLSchemaLoader;
-import org.apache.xerces.util.XMLGrammarPoolImpl;
-import org.apache.xerces.xni.XNIException;
-import org.apache.xerces.xni.grammars.XMLGrammarPool;
-import org.apache.xerces.xni.parser.XMLErrorHandler;
-import org.apache.xerces.xni.parser.XMLParseException;
-import org.xml.sax.SAXException;
+import org.apache.xerces.impl.xs.XMLSchemaValidator;
+import org.apache.xerces.xni.grammars.XMLGrammarLoader;
/**
* <p>The implementation of the {@link SchemaParser} interface for the XML Schema
@@ -35,14 +28,13 @@
*
* @author <a href="mailto:pier@betaversion.org">Pier Fumagalli</a>
*/
-public class XercesSchemaParser extends CachingSchemaParser implements SchemaParser {
+public class XercesSchemaParser extends XercesGrammarParser implements SchemaParser {
private static final String F_SCHEMA_FULL_CHECK =
Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_FULL_CHECKING;
- private static final String P_XML_GRAMMAR_POOL =
- Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY;
- private static final String[] GRAMMARS =
+ private static final String[] GRAMMARS =
new String[] { Validator.GRAMMAR_XML_SCHEMA };
+ private static final Class VALIDATOR = XMLSchemaValidator.class;
/**
* <p>Create a new {@link XercesSchemaParser} instance.</p>
@@ -52,51 +44,6 @@
}
/**
- * <p>Parse the specified URI and return a {@link Schema}.</p>
- *
- * @param uri the URI of the {@link Schema} to return.
- * @return a <b>non-null</b> {@link Schema} instance.
- * @throws SAXException if an error occurred parsing the schema.
- * @throws IOException if an I/O error occurred parsing the schema.
- */
- protected Schema parseSchema(String uri)
- throws IOException, SAXException {
- /* Create a Xerces Grammar Pool */
- XMLGrammarPool pool = new XMLGrammarPoolImpl();
-
- /* Create a new XML Schema Loader and set the pool into it */
- XMLSchemaLoader loader = new XMLSchemaLoader();
- loader.setFeature(F_SCHEMA_FULL_CHECK, true);
- loader.setProperty(P_XML_GRAMMAR_POOL, pool);
-
- /* Set up the entity resolver (from Cocoon) used to resolve URIs */
- XercesEntityResolver resolver = new XercesEntityResolver();
- loader.setEntityResolver(resolver);
-
- /* Default Error Handler: fail always! */
- loader.setErrorHandler(new XMLErrorHandler() {
- public void warning(String domain, String key, XMLParseException e)
- throws XNIException {
- throw e;
- }
- public void error(String domain, String key, XMLParseException e)
- throws XNIException {
- throw e;
- }
- public void fatalError(String domain, String key, XMLParseException e)
- throws XNIException {
- throw e;
- }
- });
-
- /* Load (parse and interpret) the grammar */
- loader.loadGrammar(resolver.resolveEntity(uri));
-
- /* Return a new Schema instance */
- return new XercesSchema(pool, resolver.getSourceValidity());
- }
-
- /**
* <p>Return an array of {@link String}s containing all schema grammars
* supported by this {@link SchemaParser}.</p>
*
@@ -105,5 +52,15 @@
*/
public String[] getSupportedGrammars() {
return GRAMMARS;
+ }
+
+ protected XMLGrammarLoader newGrammarLoader() {
+ XMLSchemaLoader loader = new XMLSchemaLoader();
+ loader.setFeature(F_SCHEMA_FULL_CHECK, true);
+ return loader;
+ }
+
+ protected Class getValidationHandler() {
+ return VALIDATOR;
}
}
Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesValidationHandler.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesValidationHandler.java?rev=279423&r1=279422&r2=279423&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesValidationHandler.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/components/validation/impl/XercesValidationHandler.java Wed Sep 7 14:18:43 2005
@@ -15,7 +15,6 @@
*/
package org.apache.cocoon.components.validation.impl;
-import org.apache.xerces.impl.xs.XMLSchemaValidator;
import org.apache.xerces.util.NamespaceSupport;
import org.apache.xerces.util.SAXLocatorWrapper;
import org.apache.xerces.util.XMLAttributesImpl;
@@ -23,8 +22,8 @@
import org.apache.xerces.xni.NamespaceContext;
import org.apache.xerces.xni.QName;
import org.apache.xerces.xni.XMLAttributes;
+import org.apache.xerces.xni.XMLDocumentHandler;
import org.apache.xerces.xni.XMLString;
-import org.apache.xerces.xni.grammars.XMLGrammarPool;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
@@ -37,13 +36,12 @@
*/
public class XercesValidationHandler implements ContentHandler {
- private final XMLSchemaValidator validator = new XMLSchemaValidator();
+ private final XMLDocumentHandler validationHandler;
private final NamespaceContext namespaceContext = new NamespaceSupport();
-
- private SAXLocatorWrapper locator = new SAXLocatorWrapper();
-
- public XercesValidationHandler(XMLGrammarPool grammarPool) {
- this.validator.reset(new XercesComponentManager(grammarPool, new XercesEntityResolver(), null));
+ private final SAXLocatorWrapper locator = new SAXLocatorWrapper();
+
+ public XercesValidationHandler(XMLDocumentHandler validationHandler) {
+ this.validationHandler = validationHandler;
}
public void setDocumentLocator(Locator locator) {
@@ -54,7 +52,7 @@
public void startDocument()
throws SAXException {
System.err.println("START DOCUMENT");
- this.validator.startDocument(this.locator,
+ this.validationHandler.startDocument(this.locator,
this.locator.getEncoding(),
this.namespaceContext,
null);
@@ -62,7 +60,7 @@
public void endDocument()
throws SAXException {
- this.validator.endDocument(null);
+ this.validationHandler.endDocument(null);
}
public void startPrefixMapping(String pfx, String uri)
@@ -93,32 +91,32 @@
xmlatts.addAttribute(aQname, aType, aValue);
}
this.namespaceContext.pushContext();
- this.validator.startElement(qname, xmlatts, null);
+ this.validationHandler.startElement(qname, xmlatts, null);
}
public void endElement(String namespace, String local, String qualified)
throws SAXException {
QName qname = this.qname(namespace, local, qualified);
- this.validator.endElement(qname, null);
+ this.validationHandler.endElement(qname, null);
this.namespaceContext.popContext();
}
public void characters(char buffer[], int offset, int length)
throws SAXException {
XMLString data = new XMLString(buffer, offset, length);
- this.validator.characters(data, null);
+ this.validationHandler.characters(data, null);
}
public void ignorableWhitespace(char buffer[], int offset, int length)
throws SAXException {
XMLString data = new XMLString(buffer, offset, length);
- this.validator.ignorableWhitespace(data, null);
+ this.validationHandler.ignorableWhitespace(data, null);
}
public void processingInstruction(String target, String extra)
throws SAXException {
XMLString data = new XMLString(extra.toCharArray(), 0, extra.length());
- this.validator.processingInstruction(target, data, null);
+ this.validationHandler.processingInstruction(target, data, null);
}
public void skippedEntity(String arg0)
Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/transformation/ValidatingTransformer.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/transformation/ValidatingTransformer.java?rev=279423&r1=279422&r2=279423&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/transformation/ValidatingTransformer.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/validation/java/org/apache/cocoon/transformation/ValidatingTransformer.java Wed Sep 7 14:18:43 2005
@@ -37,6 +37,7 @@
import org.apache.cocoon.xml.XMLConsumer;
import org.apache.cocoon.xml.XMLMulticaster;
import org.apache.excalibur.source.SourceValidity;
+import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
/**
@@ -56,6 +57,7 @@
private ServiceManager serviceManager = null;
private Validator validator = null;
private SchemaParser schemaParser = null;
+ private ContentHandler handler = null;
private Schema schema = null;
private String key = null;
@@ -118,6 +120,7 @@
this.schema = this.schemaParser.getSchema(source);
this.key = this.getClass().getName() + ":" +
this.schemaParser.getClass().getName() + ":" + source;
+ this.handler = this.schema.newValidator();
}
/**
@@ -127,7 +130,7 @@
* @param consumer the {@link XMLConsumer} to send SAX events to.
*/
public void setConsumer(XMLConsumer consumer) {
- XMLConsumer handler = new ContentHandlerWrapper(this.schema.newValidator());
+ XMLConsumer handler = new ContentHandlerWrapper(this.handler);
super.setConsumer(new XMLMulticaster(handler, consumer));
}
@@ -155,6 +158,7 @@
* <p>Recycle this component instance at the end of request processing.</p>
*/
public void recycle() {
+ this.handler = null;
this.schema = null;
this.key = null;
super.recycle();