1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
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 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
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 |
|
|
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 |
|
|
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 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
public void setName(String name) { |
93 |
26 |
this.name = name; |
94 |
26 |
} |
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
public void setOmitXmlDeclaration(boolean omitXmlDeclaration) { |
100 |
52 |
this.omitXmlDeclaration = omitXmlDeclaration; |
101 |
52 |
} |
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
public void setOutputMode(String outputMode) { |
108 |
13 |
this.outputMode = outputMode; |
109 |
13 |
} |
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
public void setPrettyPrint(boolean prettyPrint) { |
115 |
0 |
this.prettyPrint = prettyPrint; |
116 |
0 |
} |
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
public void setEncoding(String encoding) { |
122 |
26 |
this.encoding = encoding; |
123 |
26 |
} |
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
|
128 |
|
|
129 |
|
public void setAppend(boolean doAppend) { |
130 |
26 |
this.doAppend = doAppend; |
131 |
26 |
} |
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
|
138 |
|
public String getVar() { |
139 |
0 |
return var; |
140 |
|
} |
141 |
|
|
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
public void setVar(String var) { |
147 |
52 |
this.var = class="keyword">var; |
148 |
52 |
} |
149 |
|
|
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
protected void writeBody(Writer writer) throws SAXException, JellyTagException { |
154 |
|
|
155 |
78 |
XMLOutput newOutput = createXMLOutput(writer); |
156 |
|
try { |
157 |
|
|
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 |
|
|
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 |
|
} |