You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Martin Cooper <ma...@tumbleweed.com> on 2002/10/01 20:18:39 UTC

RE: cvs commit: jakarta-commons-sandbox/jelly/src/java/org/apache /commons/jelly/tags/core FileTag.java


> -----Original Message-----
> From: jstrachan@apache.org [mailto:jstrachan@apache.org]
> Sent: Tuesday, October 01, 2002 9:45 AM
> To: jakarta-commons-sandbox-cvs@apache.org
> Subject: cvs commit:
> jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jell
y/tags/cor
> e FileTag.java
> 
> 
> jstrachan    2002/10/01 09:45:23
> 
>   Modified:    jelly/src/test/org/apache/commons/jelly suite.jelly
>                jelly/src/java/org/apache/commons/jelly/tags/core
>                         FileTag.java
>   Log:
>   Added patch to <j:file> so that a variable can be specified 
> instead of a file name and the body will be turned into a 
> variable string.
>   
>   Its not a brilliantly named tag but the following could 
> generate some XML as a String
>   
>   <j:file var="foo">
>   	<some>
>   		<x m="l"/>
>   	</some>
>   <j:file>
>   I now have a value ${foo}

I'm most likely misunderstanding what <j:file> is all about, but isn't the
above the same thing as this?

  <j:set var="foo">
  	<some>
  		<x m="l"/>
  	</some>
  </j:set>

--
Martin Cooper


>   
>   Revision  Changes    Path
>   1.4       +10 -0     
> jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jell
y/suite.jelly
>   
>   Index: suite.jelly
>   ===================================================================
>   RCS file: 
> /home/cvs/jakarta-commons-sandbox/jelly/src/test/org/apache/co
mmons/jelly/suite.jelly,v
>   retrieving revision 1.3
>   retrieving revision 1.4
>   diff -u -r1.3 -r1.4
>   --- suite.jelly	24 Sep 2002 11:39:50 -0000	1.3
>   +++ suite.jelly	1 Oct 2002 16:45:23 -0000	1.4
>   @@ -106,4 +106,14 @@
>    			
> expected="org.apache.commons.jelly.define.Customer" 
>    			actual="${customer.class.name}"/>
>      </test:case>
>   +
>   +	<test:case name="testFileToVar">
>   +		<j:file var="foo" omitXmlDeclaration="true">
>   +			<foo x="1">hello</foo>
>   +		</j:file>
>   +
>   +		<test:assertEquals 
>   +			expected='&lt;foo x="1"&gt;hello&lt;/foo&gt;' 
>   +			actual="${foo}"/>
>   +  </test:case>
>    </test:suite>
>   
>   
>   
>   1.3       +60 -21    
> jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jell
y/tags/core/FileTag.java
>   
>   Index: FileTag.java
>   ===================================================================
>   RCS file: 
> /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/co
mmons/jelly/tags/core/FileTag.java,v
>   retrieving revision 1.2
>   retrieving revision 1.3
>   diff -u -r1.2 -r1.3
>   --- FileTag.java	29 Jul 2002 18:14:21 -0000	1.2
>   +++ FileTag.java	1 Oct 2002 16:45:23 -0000	1.3
>   @@ -56,11 +56,12 @@
>     */
>    package org.apache.commons.jelly.tags.core;
>    
>   -import java.io.FileOutputStream;
>   +import java.io.FileWriter;
>    import java.io.IOException;
>   -import java.io.OutputStream;
>   +import java.io.StringWriter;
>   +import java.io.Writer;
>    
>   -import org.apache.commons.jelly.MissingAttributeException;
>   +import org.apache.commons.jelly.JellyException;
>    import org.apache.commons.jelly.TagSupport;
>    import org.apache.commons.jelly.XMLOutput;
>    
>   @@ -69,12 +70,13 @@
>    import org.dom4j.io.XMLWriter;
>    
>    /** 
>   - * A tag that pipes its body to a file.
>   + * A tag that pipes its body to a file denoted by the name 
> attribute or to an in memory String
>   + * which is then output to a variable denoted by the var variable.
>     *
>     * @author <a href="mailto:vinayc@apache.org">Vinay Chandran</a>
>     */
>   -public class FileTag extends TagSupport 
>   -{
>   +public class FileTag extends TagSupport {
>   +    private String var;
>        private String name;
>        private boolean omitXmlDeclaration = false;
>        private String outputMode = "xml";
>   @@ -87,19 +89,22 @@
>        // Tag interface
>        
> //------------------------------------------------------------
> ------------- 
>        public void doTag(final XMLOutput output) throws Exception {
>   -        if ( name == null ) {
>   -            throw new MissingAttributeException( "name" );
>   +        if ( name != null ) {
>   +            Writer writer = new FileWriter(name);
>   +            writeBody(writer);
>   +        }
>   +        else if (var != null) {
>   +            StringWriter writer = new StringWriter();
>   +            writeBody(writer);
>   +            context.setVariable(var, writer.toString());
>            }
>   -        XMLOutput newOutput = createXMLOutput();
>   -        try {
>   -            newOutput.startDocument();
>   -            invokeBody(newOutput);
>   -            newOutput.endDocument();
>   -        }
>   -        finally {
>   -            newOutput.close();
>   +        else {
>   +            throw new JellyException( "This tag must have 
> either the 'name' or the 'var' variables defined" );
>            }
>        }
>   +        
>   +    // Properties
>   +    
> //------------------------------------------------------------
> ------------- 
>        
>        /**
>         * Sets the file name for the output
>   @@ -137,9 +142,45 @@
>            this.encoding = encoding;
>        }
>            
>   +    /**
>   +     * Returns the var.
>   +     * @return String
>   +     */
>   +    public String getVar() {
>   +        return var;
>   +    }
>   +
>   +    /**
>   +     * Sets the var.
>   +     * @param var The var to set
>   +     */
>   +    public void setVar(String var) {
>   +        this.var = var;
>   +    }
>   +
>        // Implementation methods
>        
> //------------------------------------------------------------
> ------------- 
>   -    protected XMLOutput createXMLOutput() throws Exception {
>   +
>   +    /**
>   +     * Writes the body fo this tag to the given Writer
>   +     */
>   +    protected void writeBody(Writer writer) throws Exception {
>   +
>   +        XMLOutput newOutput = createXMLOutput(writer);
>   +        try {
>   +            newOutput.startDocument();
>   +            invokeBody(newOutput);
>   +            newOutput.endDocument();
>   +        }
>   +        finally {
>   +            newOutput.close();
>   +        }
>   +    }
>   +    
>   +    /**
>   +     * A Factory method to create a new XMLOutput from the 
> given Writer.
>   +     */
>   +    protected XMLOutput createXMLOutput(Writer writer) 
> throws Exception {
>            
>            OutputFormat format = null;
>            if (prettyPrint) {
>   @@ -155,12 +196,10 @@
>                format.setSuppressDeclaration(true);
>            }
>                        
>   -        OutputStream out = new FileOutputStream(name);
>   -        
>            boolean isHtml = outputMode != null && 
> outputMode.equalsIgnoreCase( "html" );
>            final XMLWriter xmlWriter = (isHtml) 
>   -            ? new HTMLWriter(out, format)
>   -            : new XMLWriter(out, format);
>   +            ? new HTMLWriter(writer, format)
>   +            : new XMLWriter(writer, format);
>    
>            XMLOutput answer = new XMLOutput() {
>                public void close() throws IOException {
>   
>   
>   
> 
> --
> To unsubscribe, e-mail:   
> <ma...@jakarta.apache.org>
> For additional commands, e-mail: 
> <ma...@jakarta.apache.org>
> 
> 


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>