1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.jelly.tags.util;
17
18 import java.io.BufferedReader;
19 import java.io.File;
20 import java.io.FileNotFoundException;
21 import java.io.FileReader;
22 import java.io.InputStream;
23 import java.io.InputStreamReader;
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.io.UnsupportedEncodingException;
27 import java.io.Reader;
28
29 import org.apache.commons.jelly.JellyTagException;
30 import org.apache.commons.jelly.MissingAttributeException;
31 import org.apache.commons.jelly.TagSupport;
32 import org.apache.commons.jelly.XMLOutput;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36
37 /***
38 * A tag which loads text from a file or URI into a Jelly variable.
39 *
40 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
41 * @version $Revision: 155420 $
42 */
43 public class LoadTextTag extends TagSupport {
44
45 /*** The Log to which logging calls will be made. */
46 private static final Log log = LogFactory.getLog(LoadTextTag.class);
47
48 private String var;
49 private File file;
50 private String uri;
51 private String encoding;
52
53 public LoadTextTag() {
54 }
55
56
57
58 public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException {
59 if (var == null) {
60 throw new MissingAttributeException("var");
61 }
62 if (file == null && uri == null) {
63 throw new JellyTagException( "This tag must have a 'file' or 'uri' specified" );
64 }
65
66 InputStream in = null;
67 if (file != null) {
68 if (! file.exists()) {
69 throw new JellyTagException( "The file: " + file + " does not exist" );
70 }
71
72 try {
73 in = new FileInputStream(file);
74 } catch (FileNotFoundException e) {
75 throw new JellyTagException("could not find the file",e);
76 }
77 }
78 else {
79 in = context.getResourceAsStream(uri);
80 if (in == null) {
81 throw new JellyTagException( "Could not find uri: " + uri );
82 }
83 }
84
85 Reader reader = null;
86 if (encoding != null) {
87 try {
88 reader = new InputStreamReader(in, encoding);
89 } catch (UnsupportedEncodingException e) {
90 throw new JellyTagException("unsupported encoding",e);
91 }
92 } else {
93 reader = new InputStreamReader(in);
94 }
95
96 String text = null;
97
98 try {
99 text = loadText(reader);
100 }
101 catch (IOException e) {
102 throw new JellyTagException(e);
103 }
104
105 context.setVariable(var, text);
106 }
107
108
109
110
111 /***
112 * Sets the name of the variable which will be exported with the text value of the
113 * given file.
114 */
115 public void setVar(String var) {
116 this.var = var;
117 }
118 /***
119 * Returns the file.
120 * @return File
121 */
122 public File getFile() {
123 return file;
124 }
125
126 /***
127 * Returns the uri.
128 * @return String
129 */
130 public String getUri() {
131 return uri;
132 }
133
134 /***
135 * Returns the var.
136 * @return String
137 */
138 public String getVar() {
139 return var;
140 }
141
142 /***
143 * Sets the file to be parsed as text
144 */
145 public void setFile(File file) {
146 this.file = file;
147 }
148
149 /***
150 * Sets the encoding to use to read the file
151 */
152 public void setEncoding(String encoding) {
153 this.encoding = encoding;
154 }
155
156 /***
157 * Sets the uri to be parsed as text.
158 * This can be an absolute URL or a relative or absolute URI
159 * from this Jelly script or the root context.
160 */
161 public void setUri(String uri) {
162 this.uri = uri;
163 }
164
165 /*** Returns the encoding set.
166 * @return the encoding set with {@link #setEncoding(String)}
167 */
168 public String getEncoding() {
169 return encoding;
170 }
171
172
173
174
175
176 /***
177 * Loads all the text from the given Reader
178 */
179 protected String loadText(Reader reader) throws IOException {
180 StringBuffer buffer = new StringBuffer();
181
182
183 try {
184 BufferedReader bufferedReader = new BufferedReader(reader);
185 while (true) {
186 String line = bufferedReader.readLine();
187 if (line == null) {
188 break;
189 }
190 else {
191 buffer.append(line);
192 buffer.append('\n');
193 }
194 }
195 return buffer.toString();
196 }
197 finally {
198 try {
199 reader.close();
200 }
201 catch (Exception e) {
202 log.error( "Caught exception closing Reader: " + e, e);
203 }
204 }
205 }
206 }