01: package org.apache.velocity.runtime.resource;
02:
03: /*
04: * Licensed to the Apache Software Foundation (ASF) under one
05: * or more contributor license agreements. See the NOTICE file
06: * distributed with this work for additional information
07: * regarding copyright ownership. The ASF licenses this file
08: * to you under the Apache License, Version 2.0 (the
09: * "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: /** Default empty constructor */
41: public ContentResource() {
42: }
43:
44: /**
45: * Pull in static content and store it.
46: * @return True if everything went ok.
47: *
48: * @exception ResourceNotFoundException Resource could not be
49: * found.
50: */
51: public boolean process() throws ResourceNotFoundException {
52: BufferedReader reader = null;
53:
54: try {
55: StringWriter sw = new StringWriter();
56:
57: reader = new BufferedReader(new InputStreamReader(
58: resourceLoader.getResourceStream(name), encoding));
59:
60: char buf[] = new char[1024];
61: int len = 0;
62:
63: while ((len = reader.read(buf, 0, 1024)) != -1)
64: sw.write(buf, 0, len);
65:
66: setData(sw.toString());
67:
68: return true;
69: } catch (ResourceNotFoundException e) {
70: // Tell the ContentManager to continue to look through any
71: // remaining configured ResourceLoaders.
72: throw e;
73: } catch (Exception e) {
74: rsvc.getLog().error("Cannot process content resource", e);
75: return false;
76: } finally {
77: if (reader != null) {
78: try {
79: reader.close();
80: } catch (Exception ignored) {
81: }
82: }
83: }
84: }
85: }
|