You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cocoon.apache.org by Oskar Casquero <jt...@bi.ehu.es> on 2003/01/27 14:36:14 UTC

ValidationTransformer

Hi,

I am trying to do a ValidationTransformer that will validate an xml document
with a grammar (W3C Schema, RELAX NG, DTD). To do this I am using JARV, that
gives a validation interface which can control a validation engine (for
example, Xerces-2) through the suitable driver.

I have done the first version of the ValidationTransformer via DOM. To do
it, I modified the WriteDOMSessionTransformer, implementing ErrorHandler
interface and adding to it the code necessary to validate with W3C schemas
(another grammars can be used setting the grammar type as a parameter in the
sitemap).

setup method:

public void setup(SourceResolver resolver, Map objectModel, String source,
Parameters parameters)
    throws ProcessingException, SAXException, IOException, Exception {
    ...
    schema = parameters.getParameter(MyValidationTransformer.SCHEMA, null);
    if (schema!=null) {
        getLogger().debug("MyValidationTransformer: "+
MyValidationTransformer.SCHEMA + "=" + schema);
    } else {
        getLogger().error("MyValidationTransformer: need " +
MyValidationTransformer.SCHEMA + " parameters");
    }

    File schemaFile = new
File("/usr/local/jakarta-tomcat-4.0.4/webapps/cocoon/RTF/schemas/" +
schema);
    VerifierFactory factory =
VerifierFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    getLogger().debug("MyValidationTransformer: JARV implementation
obtained");
    Schema schemaObject = factory.compileSchema(schemaFile);
    getLogger().debug("MyValidationTransformer: schema compiled");
    verifier = schemaObject.newVerifier();
    verifier.setErrorHandler(this);
    getLogger().debug("MyValidationTransformer: errorHandler configured");
    } catch (Exception e) {
        getLogger().debug("MyValidationTransformer: JARV error: " +
e.getMessage() + ", " + e.getCause());
    }
}

endElement method:

public void endElement(String uri, String name, String raw)
    throws SAXException {
    if (name.equalsIgnoreCase(rootElement) && sessionAvailable) {
    ...
    getLogger().debug("MyValidationTransformer: DOM tree is in session
object");
    verifier.verify(builder.getDocument());
    getLogger().debug("MyValidationTransformer: DOM tree verified");
    }
    ...
}

error method for ErrorHandler implementation:

public void error( final SAXParseException spe )
throws SAXException
{
    String message = "Error parsing " + spe.getSystemId() + " (line " +
            spe.getLineNumber() + " col. " + spe.getColumnNumber() +
            "): " + spe.getMessage();
    getLogger().error( message, spe );
    throw new SAXException( message, spe );
}

I check the transformer with the following pipeline: if I send a valid
document it must return the document without doing any transformation; if
the document is not valid, an error message must be returned by cocoon
containing ErrorHandler's exception message.

<map:match pattern="UML_MAST_XMI2MAST_XMLresponse">
    <map:generate type="myStream">
        <map:parameter name="file-name" value="mast"/>
        <map:parameter name="validation" value="false"/>
    </map:generate>
    <!--<map:transform type="xalan" src="stylesheets/xmi2xml.xsl"/>-->
    <map:transform type="myValidation">
        <map:parameter name="dom-name" value="DBresult"/>
        <map:parameter name="dom-root-element" value="MAST_RT_View"/>
        <map:parameter name="schema" value="mast.xsd"/>
    </map:transform>
    <map:transform type="xalan" src="stylesheets/simple-xml2html.xsl"/>
    <map:serialize/>
</map:match>

This works well. But if I try to add <map:transform type="xalan"
src="stylesheets/xmi2xml.xsl"/> after the generator, the document is
returned to the browser, despite it is not valid. So I think that the
ValidationTransformer is not working well, but I am not totally sure,
because the error is logged in sitemap.log.

So, why is the error logged into sitemap.log, but not returned as an
exception error message to the client?

Could be because of the way in how cocoon works, because when the validation
of the DOM tree is starting or being done, the pipeline process is already
complete?

The alternative is to perform the validation via SAX, but I don't know how
to mix the verifierHandler and the contentHandler of the transformer in
order to make available incoming SAX events for the verifierHandler. I try
to do:

public void setup(SourceResolver resolver, Map objectModel, String source,
Parameters parameters)
    throws ProcessingException, SAXException, IOException, Exception {
    ...
    VerifierHandler verifierHandler = verifier.getVerifierHandler();
    super.setContentHandler(verifierHandler);
    ...
}

so that as the transformer receives SAX events from a generator or another
transfomer, the verifierHandler will also receive them, and if the documents
is not valid and exception will be thrown by the ErrorHandler. But I don't
know why, the super.setContentHandler(verifierHandler) is not working (???)

Oskar


---------------------------------------------------------------------
To unsubscribe, e-mail: cocoon-dev-unsubscribe@xml.apache.org
For additional commands, email: cocoon-dev-help@xml.apache.org