8e21dd65dca319ca81a0cd80393d4c13

I want to validate some XML with Schematron, and need help in Optimizing JAXP Code. Code compiles and executes without any Errors. Use Saxon if u want to test code. XSL-Files are official ISO Schematron.

How to do it more elegant, maybe with SAX instead of DOM? Unfortunately, I am not that good in JAXP. Thanks a lot in advance.

private void schematronTransformation()
      throws TransformerException {
      TransformerFactory tf = TransformerFactory.newInstance();

      Transformer stage1 = tf.newTransformer(new StreamSource(
         "Schematron/iso_dsdl_include.xsl"));
      DOMResult dres1 = new DOMResult();
      stage1.transform(new StreamSource("input.sch"), dres1);

      Transformer stage2 = tf.newTransformer(new StreamSource(
         "Schematron/iso_abstract_expand.xsl"));
      DOMResult dres2 = new DOMResult();
      stage2.transform(new DOMSource(dres1.getNode()), dres2);

      Transformer stage3 = tf.newTransformer(new StreamSource(
         "Schematron/iso_svrl_for_xslt2.xsl"));
      DOMResult dres3 = new DOMResult();
      stage3.transform(new DOMSource(dres2.getNode()), dres3);

      Transformer stage4 = tf.newTransformer(new DOMSource(dres3
         .getNode()));
      stage4.transform(new StreamSource("input.xml"),
         new StreamResult("output.xml"));

   }

Refactorings

No refactoring yet !

220a1ce7a99b513ece2aca0f6d4688c7

zetafish

October 9, 2009, October 09, 2009 14:15, permalink

No rating. Login to rate!

Compiling the schematron file only has to happen once. Also generalized the compile stages.

// NOTE: Compiling the 'input.sch' is needed only once.
    private final Transformer transformer = compileTransformer();
    
    
    // NOTE: sycnhronized because transformers are not thread safe.
    private synchronized void schematronTransformation()
            throws TransformerException
    {
        Source source = new StreamSource("input.xml");
        Result result = new StreamResult("output.xml");
        transformer.transform(source, result);
    }

    private static Transformer compileTransformer()
            throws TransformerException
    {
        TransformerFactory tf = TransformerFactory.newInstance();
        String[] stages = {
                "Schematron/iso_dsdl_include.xsl",
                "Schematron/iso_abstract_expand.xsl",
                "Schematron/iso_svrl_for_xslt2.xsl" };

        Node node = null;
        Source source = new StreamSource("input.sch");
        for (String stage : stages)
        {
            Source xsl = new StreamSource(stage);
            Transformer t = tf.newTransformer(xsl);
            DOMResult result = new DOMResult();
            t.transform(source, result);
            
            source = new DOMSource(node = result.getNode());
        }
        
        return tf.newTransformer(new DOMSource(node));
    }

    private static Node transform(Transformer t, Source source)
            throws TransformerException
    {
        DOMResult y = new DOMResult();
        t.transform(source, y);
        return y.getNode();
    }

Your refactoring





Format Copy from initial code

or Cancel