View Javadoc

1   package org.apache.velocity.site.doxia.velocity;
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.BufferedReader;
23  import java.io.ByteArrayInputStream;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.io.Reader;
27  import java.io.UnsupportedEncodingException;
28  
29  import org.apache.commons.lang.StringUtils;
30  import org.apache.velocity.exception.ResourceNotFoundException;
31  import org.codehaus.plexus.logging.LogEnabled;
32  import org.codehaus.plexus.logging.Logger;
33  
34  /**
35   * This loader reads the current template from a passed in {@link Reader} object
36   * and returns the template as an {@link InputStream} for Velocity to use.
37   *
38   * @plexus.component role="org.apache.velocity.site.doxia.velocity.DoxiaResourceLoader"
39   *
40   * @author <a href="mailto:henning@apache.org">Henning P. Schmiedehausen</a>
41   * @version $Revision: 526751 $
42   */
43  public class DefaultDoxiaResourceLoader extends AbstractDoxiaResourceLoader implements DoxiaResourceLoader, LogEnabled
44  {
45  
46      /** Plexus logging */
47      private Logger logger = null;
48  
49      /** The current reader to load the template from. */
50      private Reader templateReader = null;
51  
52      /**
53       * @see LogEnabled#enableLogging(Logger)
54       */
55      public void enableLogging(final Logger logger)
56      {
57          this.logger = logger;
58      }
59  
60      /**
61       * Set the {@link Reader} object for the loader to use.
62       *
63       * @param templateReader
64       *            A {@link Reader} object. Must not be null.
65       */
66      public void setTemplateReader(final Reader templateReader)
67      {
68          this.templateReader = templateReader;
69      }
70  
71      /**
72       * Returns the template available from the {@link Reader} that has been set
73       * using {@link #setTemplateReader(Reader)} when the requested template name
74       * is the empty string. This allows Velocity to pull the current template
75       * out of the reader and process as a template.
76       *
77       * TODO: Teach Velocity to know about InputReaders. This method is a kludge
78       * at best.
79       *
80       * @param templateName
81       *            The template to retrieve. This loader only reacts on the empty
82       *            (&quot;&quot;) template name.
83       *
84       * @return An {@link InputStream} representing the Template available from
85       *         the Reader or null.
86       *
87       *
88       * @throws ResourceNotFoundException
89       *             When this
90       *             {@link org.apache.velocity.runtime.resource.loader.ResourceLoader}
91       *             should have been able to supply the template but encountered
92       *             an error.
93       */
94      public InputStream getResourceStream(final String templateName) throws ResourceNotFoundException
95      {
96          if ((templateReader == null) || StringUtils.isNotEmpty(templateName))
97          {
98              return null;
99          }
100 
101         BufferedReader bufReader = new BufferedReader(templateReader);
102 
103         StringBuffer template = new StringBuffer();
104 
105         String buf = null;
106 
107         try
108         {
109             while ((buf = bufReader.readLine()) != null)
110             {
111                 template.append(buf).append("\n");
112             }
113         }
114         catch (IOException ioe)
115         {
116             throw new ResourceNotFoundException("While reading Template: ", ioe);
117         }
118         finally
119         {
120             if (bufReader != null)
121             {
122                 try
123                 {
124                     bufReader.close();
125                 }
126                 catch (IOException ioe2)
127                 {
128                     logger.error("While closing Input Stream: ", ioe2);
129                 }
130             }
131         }
132 
133         try
134         {
135             // must match the input.encoding setting in the properties file!
136             byte[] templateBytes = template.toString().getBytes("UTF-8");
137 
138             return new ByteArrayInputStream(templateBytes);
139         }
140         catch (UnsupportedEncodingException uee)
141         {
142             throw new ResourceNotFoundException("While encoding Template: ", uee);
143         }
144     }
145 }