View Javadoc

1   package org.apache.velocity.runtime.resource;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.    
20   */
21  
22  import java.io.StringWriter;
23  import java.io.BufferedReader;
24  import java.io.InputStreamReader;
25  
26  import org.apache.velocity.exception.ResourceNotFoundException;
27  
28  /**
29   * This class represent a general text resource that may have been
30   * retrieved from any number of possible sources.
31   *
32   * Also of interest is Velocity's {@link org.apache.velocity.Template}
33   * <code>Resource</code>.
34   *
35   * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
36   * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
37   * @version $Id: ContentResource.java 463298 2006-10-12 16:10:32Z henning $
38   */
39  public class ContentResource extends Resource
40  {
41      /** Default empty constructor */
42      public ContentResource()
43      {
44      }
45  
46      /**
47       * Pull in static content and store it.
48       * @return True if everything went ok.
49       *
50       * @exception ResourceNotFoundException Resource could not be
51       * found.
52       */
53      public boolean process()
54          throws ResourceNotFoundException
55      {
56          BufferedReader reader = null;
57  
58          try
59          {
60              StringWriter sw = new StringWriter();
61  
62              reader = new BufferedReader(
63                  new InputStreamReader(resourceLoader.getResourceStream(name),
64                                        encoding));
65  
66              char buf[] = new char[1024];
67              int len = 0;
68  
69              while ( ( len = reader.read( buf, 0, 1024 )) != -1)
70                  sw.write( buf, 0, len );
71  
72              setData(sw.toString());
73  
74              return true;
75          }
76          catch ( ResourceNotFoundException e )
77          {
78              // Tell the ContentManager to continue to look through any
79              // remaining configured ResourceLoaders.
80              throw e;
81          }
82          catch ( Exception e )
83          {
84              rsvc.getLog().error("Cannot process content resource", e);
85              return false;
86          }
87          finally
88          {
89              if (reader != null)
90              {
91                  try
92                  {
93                      reader.close();
94                  }
95                  catch (Exception ignored)
96                  {
97                  }
98              }
99          }
100     }
101 }