%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.commons.jelly.tags.util.LoadTextTag |
|
|
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.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: 1.7 $ |
|
42 | */ |
|
43 | 1 | public class LoadTextTag extends TagSupport { |
44 | ||
45 | /** The Log to which logging calls will be made. */ |
|
46 | 1 | 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 | 0 | public LoadTextTag() { |
54 | 0 | } |
55 | ||
56 | // Tag interface |
|
57 | //------------------------------------------------------------------------- |
|
58 | public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException { |
|
59 | 0 | if (var == null) { |
60 | 0 | throw new MissingAttributeException("var"); |
61 | } |
|
62 | 0 | if (file == null && uri == class="keyword">null) { |
63 | 0 | throw new JellyTagException( "This tag must have a 'file' or 'uri' specified" ); |
64 | } |
|
65 | ||
66 | 0 | InputStream in = null; |
67 | 0 | if (file != null) { |
68 | 0 | if (! file.exists()) { |
69 | 0 | throw new JellyTagException( "The file: " + file + " does not exist" ); |
70 | } |
|
71 | ||
72 | try { |
|
73 | 0 | in = new FileInputStream(file); |
74 | 0 | } catch (FileNotFoundException e) { |
75 | 0 | throw new JellyTagException("could not find the file",e); |
76 | 0 | } |
77 | } |
|
78 | else { |
|
79 | 0 | in = context.getResourceAsStream(uri); |
80 | 0 | if (in == null) { |
81 | 0 | throw new JellyTagException( "Could not find uri: " + uri ); |
82 | } |
|
83 | } |
|
84 | ||
85 | 0 | Reader reader = null; |
86 | try { |
|
87 | 0 | reader = new InputStreamReader(in, encoding); |
88 | 0 | } catch (UnsupportedEncodingException e) { |
89 | 0 | throw new JellyTagException("unsupported encoding",e); |
90 | } |
|
91 | ||
92 | 0 | String text = null; |
93 | ||
94 | try { |
|
95 | 0 | text = loadText(reader); |
96 | 0 | } |
97 | catch (IOException e) { |
|
98 | 0 | throw new JellyTagException(e); |
99 | } |
|
100 | ||
101 | 0 | context.setVariable(var, text); |
102 | 0 | } |
103 | ||
104 | // Properties |
|
105 | //------------------------------------------------------------------------- |
|
106 | ||
107 | /** |
|
108 | * Sets the name of the variable which will be exported with the text value of the |
|
109 | * given file. |
|
110 | */ |
|
111 | public void setVar(String var) { |
|
112 | 0 | this.var = class="keyword">var; |
113 | 0 | } |
114 | /** |
|
115 | * Returns the file. |
|
116 | * @return File |
|
117 | */ |
|
118 | public File getFile() { |
|
119 | 0 | return file; |
120 | } |
|
121 | ||
122 | /** |
|
123 | * Returns the uri. |
|
124 | * @return String |
|
125 | */ |
|
126 | public String getUri() { |
|
127 | 0 | return uri; |
128 | } |
|
129 | ||
130 | /** |
|
131 | * Returns the var. |
|
132 | * @return String |
|
133 | */ |
|
134 | public String getVar() { |
|
135 | 0 | return var; |
136 | } |
|
137 | ||
138 | /** |
|
139 | * Sets the file to be parsed as text |
|
140 | */ |
|
141 | public void setFile(File file) { |
|
142 | 0 | this.file = file; |
143 | 0 | } |
144 | ||
145 | /** |
|
146 | * Sets the encoding to use to read the file |
|
147 | */ |
|
148 | public void setEncoding(String encoding) { |
|
149 | 0 | this.encoding = encoding; |
150 | 0 | } |
151 | ||
152 | /** |
|
153 | * Sets the uri to be parsed as text. |
|
154 | * This can be an absolute URL or a relative or absolute URI |
|
155 | * from this Jelly script or the root context. |
|
156 | */ |
|
157 | public void setUri(String uri) { |
|
158 | 0 | this.uri = uri; |
159 | 0 | } |
160 | ||
161 | /** Returns the encoding set. |
|
162 | * @return the encoding set with {@link #setEncoding(String)} |
|
163 | */ |
|
164 | public String getEncoding() { |
|
165 | 0 | return encoding; |
166 | } |
|
167 | ||
168 | ||
169 | // Implementation methods |
|
170 | //------------------------------------------------------------------------- |
|
171 | ||
172 | /** |
|
173 | * Loads all the text from the given Reader |
|
174 | */ |
|
175 | protected String loadText(Reader reader) throws IOException { |
|
176 | 0 | StringBuffer buffer = new StringBuffer(); |
177 | ||
178 | // @todo its probably more efficient to use a fixed char[] buffer instead |
|
179 | try { |
|
180 | 0 | BufferedReader bufferedReader = new BufferedReader(reader); |
181 | 0 | while (true) { |
182 | 0 | String line = bufferedReader.readLine(); |
183 | 0 | if (line == null) { |
184 | 0 | break; |
185 | } |
|
186 | else { |
|
187 | 0 | buffer.append(line); |
188 | 0 | buffer.append('\n'); |
189 | } |
|
190 | } |
|
191 | 0 | return buffer.toString(); |
192 | } |
|
193 | finally { |
|
194 | 0 | try { |
195 | 0 | reader.close(); |
196 | 0 | } |
197 | catch (Exception e) { |
|
198 | 0 | log.error( "Caught exception closing Reader: " + e, e); |
199 | 0 | } |
200 | } |
|
201 | } |
|
202 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |