View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.jci.examples.serverpages;
19  
20  import java.io.ByteArrayOutputStream;
21  import java.io.File;
22  import java.io.FileInputStream;
23  import java.io.IOException;
24  import java.io.InputStreamReader;
25  import java.io.OutputStreamWriter;
26  import java.io.Reader;
27  import java.io.Writer;
28  
29  import org.apache.commons.jci.utils.ConversionUtils;
30  
31  /**
32   * @author tcurdt
33   */
34  public final class JspGenerator {
35  
36      private String quote( final String s ) {
37  
38          final StringBuilder sb = new StringBuilder();
39          final char[] input = s.toCharArray();
40  
41          for (char c : input) {
42              if (c == '"') {
43                  sb.append('\\');
44              }
45              if (c == '\\') {
46                  sb.append('\\');
47              }
48  
49              if (c == '\n') {
50                  sb.append("\");\n").append("    out.write(\"");
51                  continue;
52              }
53              sb.append(c);
54          }
55  
56          return sb.toString();
57      }
58  
59      private void wrap( final StringBuilder pInput, final Writer pOutput ) throws IOException {
60  
61          pOutput.append("    out.write(\"");
62  
63          pOutput.append(quote(pInput.toString()));
64  
65          pOutput.append("\");").append('\n');
66      }
67  
68      public byte[] generateJavaSource( final  String pResourceName, final File pFile ) {
69  
70      	final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
71          final Writer output = new OutputStreamWriter(outputStream);
72  
73          try {
74              final Reader input = new InputStreamReader(new FileInputStream(pFile));
75  
76              final int p = pResourceName.lastIndexOf('/');
77  
78              final String className;
79              final String packageName;
80  
81              if (p < 0) {
82                  className = ConversionUtils.stripExtension(pResourceName);
83                  packageName = "";
84              } else {
85                  className = ConversionUtils.stripExtension(pResourceName.substring(p+1));
86                  packageName = pResourceName.substring(0, p).replace('/', '.');
87                  output.append("package ").append(packageName).append(";").append('\n');
88              }
89  
90  
91              output.append("import java.io.PrintWriter;").append('\n');
92              output.append("import java.io.IOException;").append('\n');
93              output.append("import javax.servlet.http.HttpServlet;").append('\n');
94              output.append("import javax.servlet.http.HttpServletRequest;").append('\n');
95              output.append("import javax.servlet.http.HttpServletResponse;").append('\n');
96              output.append("import javax.servlet.ServletException;").append('\n');
97              output.append("public class ").append(className).append(" extends HttpServlet {").append('\n');
98              output.append("  protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {").append('\n');
99              output.append("    final PrintWriter out = response.getWriter();").append('\n');
100 
101 
102             final char[] open = "<?".toCharArray();
103             final char[] close = "?>".toCharArray();
104 
105             StringBuilder sb = new StringBuilder();
106             char[] watch = open;
107             int w = 0;
108             while(true) {
109                 int c = input.read();
110 
111                 if (c < 0) {
112                     break;
113                 }
114 
115                 if (c == watch[w]) {
116                     w++;
117                     if (watch.length == w) {
118                         if (watch == open) {
119                             // found open
120 
121                             wrap(sb, output);
122 
123                             sb = new StringBuilder();
124                             watch = close;
125                         } else if (watch == close) {
126                             // found close
127 
128                             // <? ... ?> is java
129                             output.append(sb.toString());
130 
131                             sb = new StringBuilder();
132                             watch = open;
133                         }
134                         w = 0;
135                     }
136                 } else {
137                     if (w > 0) {
138                         sb.append(watch, 0, w);
139                     }
140 
141                     sb.append((char)c);
142 
143                     w = 0;
144                 }
145             }
146 
147             if (watch == open) {
148                 wrap(sb, output);
149             }
150 
151 
152             output.append("    out.close();").append('\n');
153             output.append("    out.flush();").append('\n');
154             output.append("  }").append('\n');
155             output.append("}").append('\n');
156 
157             return outputStream.toByteArray();
158 
159         } catch (IOException e) {
160             return null;
161         } finally {
162             try {
163 				output.close();
164 			} catch (IOException e) {
165 			}        	
166         }
167     }
168 
169 }