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