1 /*
2 *
3 * ====================================================================
4 *
5 * The Apache Software License, Version 1.1
6 *
7 * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
8 * reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 *
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in
19 * the documentation and/or other materials provided with the
20 * distribution.
21 *
22 * 3. The end-user documentation included with the redistribution, if
23 * any, must include the following acknowlegement:
24 * "This product includes software developed by the
25 * Apache Software Foundation (http://www.apache.org/)."
26 * Alternately, this acknowlegement may appear in the software itself,
27 * if and wherever such third-party acknowlegements normally appear.
28 *
29 * 4. The names "The Jakarta Project", "Commons", and "Apache Software
30 * Foundation" must not be used to endorse or promote products derived
31 * from this software without prior written permission. For written
32 * permission, please contact apache@apache.org.
33 *
34 * 5. Products derived from this software may not be called "Apache"
35 * nor may "Apache" appear in their names without prior written
36 * permission of the Apache Group.
37 *
38 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
39 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
40 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
45 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
46 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
48 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49 * SUCH DAMAGE.
50 * ====================================================================
51 *
52 * This software consists of voluntary contributions made by many
53 * individuals on behalf of the Apache Software Foundation. For more
54 * information on the Apache Software Foundation, please see
55 * <http://www.apache.org/>.
56 */
57 package org.apache.commons.jelly.tags.core;
58
59 import java.io.FileOutputStream;
60 import java.io.IOException;
61 import java.io.OutputStream;
62
63 import org.apache.commons.jelly.MissingAttributeException;
64 import org.apache.commons.jelly.TagSupport;
65 import org.apache.commons.jelly.XMLOutput;
66
67 import org.dom4j.io.HTMLWriter;
68 import org.dom4j.io.OutputFormat;
69 import org.dom4j.io.XMLWriter;
70
71 /***
72 * A tag that pipes its body to a file.
73 *
74 * @author <a href="mailto:vinayc@apache.org">Vinay Chandran</a>
75 */
76 public class FileTag extends TagSupport
77 {
78 private String name;
79 private boolean omitXmlDeclaration = false;
80 private String outputMode = "xml";
81 private boolean prettyPrint;
82 private String encoding;
83
84 public FileTag(){
85 }
86
87 // Tag interface
88 //-------------------------------------------------------------------------
89 public void doTag(final XMLOutput output) throws Exception {
90 if ( name == null ) {
91 throw new MissingAttributeException( "name" );
92 }
93 XMLOutput newOutput = createXMLOutput();
94 try {
95 newOutput.startDocument();
96 invokeBody(newOutput);
97 newOutput.endDocument();
98 }
99 finally {
100 newOutput.close();
101 }
102 }
103
104 /***
105 * Sets the file name for the output
106 */
107 public void setName(String name) {
108 this.name = name;
109 }
110
111 /***
112 * Sets whether the XML declaration should be output or not
113 */
114 public void setOmitXmlDeclaration(boolean omitXmlDeclaration) {
115 this.omitXmlDeclaration = omitXmlDeclaration;
116 }
117
118
119 /***
120 * Sets the output mode, whether XML or HTML
121 */
122 public void setOutputMode(String outputMode) {
123 this.outputMode = outputMode;
124 }
125
126 /***
127 * Sets whether pretty printing mode is turned on. The default is off so that whitespace is preserved
128 */
129 public void setPrettyPrint(boolean prettyPrint) {
130 this.prettyPrint = prettyPrint;
131 }
132
133 /***
134 * Sets the XML encoding mode, which defaults to UTF-8
135 */
136 public void setEncoding(String encoding) {
137 this.encoding = encoding;
138 }
139
140 // Implementation methods
141 //-------------------------------------------------------------------------
142 protected XMLOutput createXMLOutput() throws Exception {
143
144 OutputFormat format = null;
145 if (prettyPrint) {
146 format = OutputFormat.createPrettyPrint();
147 }
148 else {
149 format = new OutputFormat();
150 }
151 if ( encoding != null ) {
152 format.setEncoding( encoding );
153 }
154 if ( omitXmlDeclaration ) {
155 format.setSuppressDeclaration(true);
156 }
157
158 OutputStream out = new FileOutputStream(name);
159
160 boolean isHtml = outputMode != null && outputMode.equalsIgnoreCase( "html" );
161 final XMLWriter xmlWriter = (isHtml)
162 ? new HTMLWriter(out, format)
163 : new XMLWriter(out, format);
164
165 XMLOutput answer = new XMLOutput() {
166 public void close() throws IOException {
167 xmlWriter.close();
168 }
169 };
170 answer.setContentHandler(xmlWriter);
171 answer.setLexicalHandler(xmlWriter);
172 return answer;
173 }
174 }
This page was automatically generated by Maven