Coverage report

  %line %branch
org.apache.commons.jelly.tags.core.FileTag
81% 
96% 

 1  
 /*
 2  
  * Copyright 2002,2004 The Apache Software Foundation.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.apache.commons.jelly.tags.core;
 17  
 
 18  
 import java.io.FileNotFoundException;
 19  
 import java.io.FileOutputStream;
 20  
 import java.io.IOException;
 21  
 import java.io.OutputStreamWriter;
 22  
 import java.io.StringWriter;
 23  
 import java.io.UnsupportedEncodingException;
 24  
 import java.io.Writer;
 25  
 
 26  
 import org.apache.commons.jelly.JellyTagException;
 27  
 import org.apache.commons.jelly.TagSupport;
 28  
 import org.apache.commons.jelly.XMLOutput;
 29  
 import org.apache.commons.jelly.util.SafeContentHandler;
 30  
 import org.dom4j.io.HTMLWriter;
 31  
 import org.dom4j.io.OutputFormat;
 32  
 import org.dom4j.io.XMLWriter;
 33  
 import org.xml.sax.SAXException;
 34  
 
 35  
 /**
 36  
  * A tag that pipes its body to a file denoted by the name attribute or to an in memory String
 37  
  * which is then output to a variable denoted by the var variable.
 38  
  *
 39  
  * @author <a href="mailto:vinayc@apache.org">Vinay Chandran</a>
 40  
  */
 41  
 public class FileTag extends TagSupport {
 42  78
     private boolean doAppend = false;
 43  
     private String var;
 44  
     private String name;
 45  78
     private boolean omitXmlDeclaration = false;
 46  78
     private String outputMode = "xml";
 47  
     private boolean prettyPrint;
 48  
     private String encoding;
 49  
 
 50  78
     public FileTag(){
 51  78
     }
 52  
 
 53  
     // Tag interface
 54  
     //-------------------------------------------------------------------------
 55  
     public void doTag(final XMLOutput output) throws JellyTagException {
 56  
         try {
 57  78
             if ( name != null ) {
 58  26
                 String encoding = (this.encoding != null) ? class="keyword">this.encoding : "UTF-8";
 59  26
                 Writer writer = new OutputStreamWriter( class="keyword">new FileOutputStream( name, doAppend ), encoding );
 60  26
                 writeBody(writer);
 61  26
             }
 62  52
             else if (var != null) {
 63  52
                 StringWriter writer = new StringWriter();
 64  52
                 writeBody(writer);
 65  52
                 String result = writer.toString();
 66  52
                 Object varValue = context.getVariable(var);
 67  
                 // if we're appending, and var is an instance of string, append it.
 68  52
                 if (doAppend && varValue instanceof String) {
 69  13
                     context.setVariable(var, class="keyword">varValue + result);
 70  13
                 } else {
 71  39
                     context.setVariable(var, result);
 72  
                 }
 73  52
             }
 74  
             else {
 75  0
                 throw new JellyTagException( "This tag must have either the 'name' or the 'var' variables defined" );
 76  
             }
 77  0
         } catch (FileNotFoundException e) {
 78  0
             throw new JellyTagException(e);
 79  0
         } catch (UnsupportedEncodingException e) {
 80  0
             throw new JellyTagException(e);
 81  0
         } catch (SAXException e) {
 82  0
             throw new JellyTagException("could not write file",e);
 83  78
         }
 84  78
     }
 85  
 
 86  
     // Properties
 87  
     //-------------------------------------------------------------------------
 88  
 
 89  
     /**
 90  
      * Sets the file name for the output
 91  
      */
 92  
     public void setName(String name) {
 93  26
         this.name = name;
 94  26
     }
 95  
 
 96  
     /**
 97  
      * Sets whether the XML declaration should be output or not
 98  
      */
 99  
     public void setOmitXmlDeclaration(boolean omitXmlDeclaration) {
 100  52
         this.omitXmlDeclaration = omitXmlDeclaration;
 101  52
     }
 102  
 
 103  
 
 104  
     /**
 105  
      * Sets the output mode, whether XML or HTML
 106  
      */
 107  
     public void setOutputMode(String outputMode) {
 108  13
         this.outputMode = outputMode;
 109  13
     }
 110  
 
 111  
     /**
 112  
      * Sets whether pretty printing mode is turned on. The default is off so that whitespace is preserved
 113  
      */
 114  
     public void setPrettyPrint(boolean prettyPrint) {
 115  0
         this.prettyPrint = prettyPrint;
 116  0
     }
 117  
 
 118  
     /**
 119  
      * Sets the XML encoding mode, which defaults to UTF-8
 120  
      */
 121  
     public void setEncoding(String encoding) {
 122  26
         this.encoding = encoding;
 123  26
     }
 124  
     
 125  
     /**
 126  
      * Sets wether to append at the end of the file
 127  
      * (not really something you normally do with an XML file).
 128  
      */
 129  
     public void setAppend(boolean doAppend) {
 130  26
         this.doAppend = doAppend;
 131  26
     }
 132  
     
 133  
 
 134  
     /**
 135  
      * Returns the var.
 136  
      * @return String
 137  
      */
 138  
     public String getVar() {
 139  0
         return var;
 140  
     }
 141  
 
 142  
     /**
 143  
      * Sets the var.
 144  
      * @param var The var to set
 145  
      */
 146  
     public void setVar(String var) {
 147  52
         this.var = class="keyword">var;
 148  52
     }
 149  
 
 150  
     /**
 151  
      * Writes the body fo this tag to the given Writer
 152  
      */
 153  
     protected void writeBody(Writer writer) throws SAXException, JellyTagException {
 154  
 
 155  78
         XMLOutput newOutput = createXMLOutput(writer);
 156  
         try {
 157  
             // we need to avoid multiple start/end document events
 158  78
             newOutput.setContentHandler(
 159  
                 new SafeContentHandler(class="keyword">newOutput.getContentHandler())
 160  
             );
 161  78
             newOutput.startDocument();
 162  78
             invokeBody(newOutput);
 163  78
             newOutput.endDocument();
 164  
         }
 165  
         finally {
 166  78
             try { newOutput.close(); } catch (IOException e) {}
 167  0
         }
 168  78
     }
 169  
 
 170  
     /**
 171  
      * A Factory method to create a new XMLOutput from the given Writer.
 172  
      */
 173  
     protected XMLOutput createXMLOutput(Writer writer) {
 174  
 
 175  78
         OutputFormat format = null;
 176  78
         if (prettyPrint) {
 177  0
             format = OutputFormat.createPrettyPrint();
 178  0
         }
 179  
         else {
 180  78
             format = new OutputFormat();
 181  
         }
 182  78
         if ( encoding != null ) {
 183  26
             format.setEncoding( encoding );
 184  
         }
 185  78
         if ( omitXmlDeclaration ) {
 186  52
             format.setSuppressDeclaration(true);
 187  
         }
 188  
 
 189  78
         boolean isHtml = outputMode != null && outputMode.equalsIgnoreCase( "html" );
 190  78
         final XMLWriter xmlWriter = (isHtml)
 191  
             ? new HTMLWriter(writer, format)
 192  
             : new XMLWriter(writer, format);
 193  
 
 194  78
         xmlWriter.setEscapeText(isEscapeText());
 195  
 
 196  78
         XMLOutput answer = new XMLOutput() {
 197  
             public void close() throws IOException {
 198  
                 xmlWriter.close();
 199  
             }
 200  
         };
 201  78
         answer.setContentHandler(xmlWriter);
 202  78
         answer.setLexicalHandler(xmlWriter);
 203  78
         return answer;
 204  
     }
 205  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.