View Javadoc

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  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      // Tag interface
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          try {
87              reader = new InputStreamReader(in, encoding);
88          } catch (UnsupportedEncodingException e) {
89              throw new JellyTagException("unsupported encoding",e);
90          }
91  
92          String text = null;
93  
94          try {
95              text = loadText(reader);
96          }
97          catch (IOException e) {
98              throw new JellyTagException(e);
99          }
100 
101         context.setVariable(var, text);
102     }
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         this.var = var;
113     }
114     /***
115      * Returns the file.
116      * @return File
117      */
118     public File getFile() {
119         return file;
120     }
121 
122     /***
123      * Returns the uri.
124      * @return String
125      */
126     public String getUri() {
127         return uri;
128     }
129 
130     /***
131      * Returns the var.
132      * @return String
133      */
134     public String getVar() {
135         return var;
136     }
137 
138     /***
139      * Sets the file to be parsed as text
140      */
141     public void setFile(File file) {
142         this.file = file;
143     }
144 
145     /***
146      * Sets the encoding to use to read the file
147      */
148     public void setEncoding(String encoding) {
149         this.encoding = encoding;
150     }
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         this.uri = uri;
159     }
160 
161     /*** Returns the encoding set.
162     * @return the encoding set with {@link #setEncoding(String)}
163       */
164     public String getEncoding() {
165         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         StringBuffer buffer = new StringBuffer();
177 
178         // @todo its probably more efficient to use a fixed char[] buffer instead
179         try {
180             BufferedReader bufferedReader = new BufferedReader(reader);
181             while (true) {
182                 String line = bufferedReader.readLine();
183                 if (line == null) {
184                     break;
185                 }
186                 else {
187                     buffer.append(line);
188                     buffer.append('\n');
189                 }
190             }
191             return buffer.toString();
192         }
193         finally {
194             try {
195                 reader.close();
196             }
197             catch (Exception e) {
198                 log.error( "Caught exception closing Reader: " + e, e);
199             }
200         }
201     }
202 }