View Javadoc

1   package org.apache.torque.generator.template.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.ByteArrayOutputStream;
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.io.InputStreamReader;
28  import java.io.OutputStreamWriter;
29  import java.io.Writer;
30  
31  import org.apache.torque.generator.template.TemplateFilter;
32  
33  /**
34   * A Filter for velocity templates. It has the effect of beautifying the output
35   * of the velocity outlet.
36   */
37  public class VelocityTemplateFilter implements TemplateFilter
38  {
39      /**
40       * With which String tabs should be replaced
41       */
42      private static final String TAB_REPLACEMENT = "    ";
43  
44      /**
45       * This method filters the template and replaces some
46       * unwanted characters. For example it removes leading
47       * spaces in front of velocity commands and replaces
48       * tabs with spaces to prevent bounces in different
49       * code editors with different tab-width-setting.
50       *
51       * @param resource the input stream to filter.
52       * @param encoding the encoding to use, or null for the system encoding.
53       *
54       * @return the filtered input stream.
55       *
56       * @throws IOException if creating, reading or writing to a stream fails.
57       */
58      public InputStream filter(InputStream resource, String encoding)
59              throws IOException
60      {
61          InputStreamReader streamReader;
62          if (encoding == null)
63          {
64              streamReader = new InputStreamReader(resource);
65          }
66          else
67          {
68              streamReader = new InputStreamReader(resource, encoding);
69          }
70          BufferedReader bufferedReader = new BufferedReader(streamReader);
71          ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
72          Writer outputStreamWriter = null;
73          if (encoding == null)
74          {
75              outputStreamWriter = new OutputStreamWriter(outputStream);
76          }
77          else
78          {
79              outputStreamWriter = new OutputStreamWriter(outputStream, encoding);
80          }
81  
82          boolean onlySpacesInLineSoFar = true;
83          StringBuilder startLineBuffer = new StringBuilder();
84          while (true)
85          {
86              int readChar = bufferedReader.read();
87  
88              if (readChar == -1)
89              {
90                  break;
91              }
92              if (!onlySpacesInLineSoFar)
93              {
94                  outputStreamWriter.write(readChar);
95                  if ('\r' == readChar || '\n' == readChar)
96                  {
97                      onlySpacesInLineSoFar = true;
98                  }
99              }
100             else
101             {
102                 if (' ' == readChar)
103                 {
104                     startLineBuffer.append((char) readChar);
105                 }
106                 else if ('\t' == readChar)
107                 {
108                     startLineBuffer.append(TAB_REPLACEMENT);
109                 }
110                 else if ('#' == readChar)
111                 {
112                     if (startLineBuffer.length() != 0)
113                     {
114                         startLineBuffer = new StringBuilder();
115                     }
116                     outputStreamWriter.write(readChar);
117                     onlySpacesInLineSoFar = false;
118                 }
119                 else if ('\r' == readChar || '\n' == readChar)
120                 {
121                     if (startLineBuffer.length() != 0)
122                     {
123                         outputStreamWriter.write(startLineBuffer.toString());
124                         startLineBuffer = new StringBuilder();
125                     }
126                     outputStreamWriter.write(readChar);
127                 }
128                 else
129                 {
130                     if (startLineBuffer.length() != 0)
131                     {
132                         outputStreamWriter.write(startLineBuffer.toString());
133                         startLineBuffer = new StringBuilder();
134                     }
135                     outputStreamWriter.write(readChar);
136                     onlySpacesInLineSoFar = false;
137 
138                  }
139             }
140         }
141         outputStreamWriter.write(startLineBuffer.toString());
142 
143         outputStreamWriter.flush();
144         outputStreamWriter.close();
145 
146         return new ByteArrayInputStream(outputStream.toByteArray());
147     }
148 }