commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject cvs commit: jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/xml TransformTag.java
Date Thu, 26 Sep 2002 12:28:21 GMT
jstrachan    2002/09/26 05:28:21

  Modified:    jelly/src/test/org/apache/commons/jelly/xml TestXMLTags.java
               jelly/src/java/org/apache/commons/jelly/tags/xml
                        TransformTag.java
  Added:       jelly/src/test/org/apache/commons/jelly/xml
                        transformExampleSAXOutput.jelly
  Log:
  Added support in the <x:transform> tag so that output can be SAX events. This allows
more efficient SAX based XSLT pipelines.
  
  So if users don't want to parse the output of the XSLT, they can just pipe it out as SAX
events.
  
  Also added a test case of this.
  
  Revision  Changes    Path
  1.6       +7 -0      jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/xml/TestXMLTags.java
  
  Index: TestXMLTags.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/xml/TestXMLTags.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- TestXMLTags.java	25 Sep 2002 08:37:50 -0000	1.5
  +++ TestXMLTags.java	26 Sep 2002 12:28:21 -0000	1.6
  @@ -147,6 +147,13 @@
           assertEquals("Produces the correct output", "It works!", text);
       }
   
  +    public void testTransformSAXOutput() throws Exception {
  +        String text = evaluteScriptAsText(
  +            "src/test/org/apache/commons/jelly/xml/transformExampleSAXOutput.jelly"
  +        );
  +        assertEquals("Produces the correct output", "It works!", text);
  +    }
  +
       public void testTransformSchematron() throws Exception {
           String text = evaluteScriptAsText(
               "src/test/org/apache/commons/jelly/xml/schematron/transformSchematronExample.jelly"
  
  
  
  1.1                  jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/xml/transformExampleSAXOutput.jelly
  
  Index: transformExampleSAXOutput.jelly
  ===================================================================
  <?xml version="1.0"?>
  <j:jelly xmlns:j="jelly:core" xmlns:x="jelly:xml">
  
    <x:parse var="xsl_doc">
  		<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  
        <xsl:template match="document">
          <html>
            <xsl:apply-templates/>
            <body></body>
          </html>
        </xsl:template>
  
        <xsl:template match="chapter">
          <title><xsl:apply-templates/></title>
        </xsl:template>
  
      </xsl:stylesheet>
  	</x:parse>
  
  	<!-- here we're testing if the output of the XSLT comes out as SAX events
  		|| that the <x:parse> tag can then parse.
  		|| We're using the <x:parse> around the <x:transform> purely to test
  		|| that the <x:transform> is capable of outputting SAX events -->
  	<x:parse var="doc">		
  	  <x:transform xsl="${xsl_doc}">
  	    <document>
  	      <chapter>It works!</chapter>
  	    </document>
  	  </x:transform>
  	</x:parse>	
  	
    <x:expr select="$doc/html/title"/>
  
  </j:jelly>
  
  
  
  
  
  1.2       +33 -12    jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/xml/TransformTag.java
  
  Index: TransformTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/xml/TransformTag.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TransformTag.java	25 Sep 2002 08:37:50 -0000	1.1
  +++ TransformTag.java	26 Sep 2002 12:28:21 -0000	1.2
  @@ -61,11 +61,14 @@
    */
   package org.apache.commons.jelly.tags.xml;
   
  +import javax.xml.transform.Result;
   import javax.xml.transform.Source;
   import javax.xml.transform.Transformer;
   import javax.xml.transform.TransformerException;
   import javax.xml.transform.TransformerFactory;
   import javax.xml.transform.URIResolver;
  +import javax.xml.transform.sax.SAXResult;
  +import javax.xml.transform.sax.SAXSource;
   import javax.xml.transform.stream.StreamSource;
   
   import org.apache.commons.jelly.XMLOutput;
  @@ -117,9 +120,6 @@
        * @throws Exception - when required attributes are missing
        */
       public void doTag(XMLOutput output) throws Exception {
  -        if (this.getVar() == null) {
  -            throw new IllegalArgumentException("The var attribute cannot be null");
  -        }
           Document xmlDocument = this.getXmlDocument(output);
           Document xslDocument = this.parse(this.xsl);
   
  @@ -127,13 +127,23 @@
           Transformer transformer = tf.newTransformer(new DocumentSource(xslDocument));
   
           DocumentSource xmlDocSource = new DocumentSource(xmlDocument);
  -        DocumentResult result = new DocumentResult();
  -        
  -        transformer.transform(xmlDocSource, result);
   
  -        Document transformedDoc = result.getDocument();
  +        String var = getVar();        
  +        if (var == null) {
  +            // pass the result of the transform out as SAX events
  +            Result result = createSAXResult(output);
  +            transformer.transform(xmlDocSource, result);
  +        }
  +        else {
  +            DocumentResult result = new DocumentResult();
  +            transformer.transform(xmlDocSource, result);
  +
  +            // output the result as a variable
  +            Document transformedDoc = result.getDocument();
  +            context.setVariable(var, transformedDoc);
  +        }
  +        
   
  -        context.setVariable(getVar(), transformedDoc);
       }
   
       // Properties
  @@ -188,5 +198,16 @@
                   return new StreamSource(context.getResourceAsStream(href));
               }
           };
  +    }
  +    
  +    /**
  +     * Factory method to create a new SAXResult for the given
  +     * XMLOutput so that the output of an XSLT transform will go
  +     * directly into the XMLOutput that we are given.
  +     */
  +    protected Result createSAXResult(XMLOutput output) {
  +        SAXResult result = new SAXResult(output);
  +        result.setLexicalHandler(output);
  +        return result;
       }
   }
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message