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 import org.apache.velocity.exception.ResourceNotFoundException;
26 import org.apache.velocity.exception.VelocityException;
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 687177 2008-08-19 22:00:32Z nbubna $
38 */
39 public class ContentResource extends Resource
40 {
41 /** Default empty constructor */
42 public ContentResource()
43 {
44 super();
45
46 setType(ResourceManager.RESOURCE_CONTENT);
47 }
48
49 /**
50 * Pull in static content and store it.
51 * @return True if everything went ok.
52 *
53 * @exception ResourceNotFoundException Resource could not be
54 * found.
55 */
56 public boolean process()
57 throws ResourceNotFoundException
58 {
59 BufferedReader reader = null;
60
61 try
62 {
63 StringWriter sw = new StringWriter();
64
65 reader = new BufferedReader(
66 new InputStreamReader(resourceLoader.getResourceStream(name),
67 encoding));
68
69 char buf[] = new char[1024];
70 int len = 0;
71
72 while ( ( len = reader.read( buf, 0, 1024 )) != -1)
73 sw.write( buf, 0, len );
74
75 setData(sw.toString());
76
77 return true;
78 }
79 catch ( ResourceNotFoundException e )
80 {
81 // Tell the ContentManager to continue to look through any
82 // remaining configured ResourceLoaders.
83 throw e;
84 }
85 catch ( Exception e )
86 {
87 String msg = "Cannot process content resource";
88 rsvc.getLog().error(msg, e);
89 throw new VelocityException(msg, e);
90 }
91 finally
92 {
93 if (reader != null)
94 {
95 try
96 {
97 reader.close();
98 }
99 catch (Exception ignored)
100 {
101 }
102 }
103 }
104 }
105 }