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 StringBuffer sb = new StringBuffer();
39          final char[] input = s.toCharArray();
40  
41          for (int i = 0; i < input.length; i++) {
42              final char c = input[i];
43              if (c == '"') {
44                  sb.append('\\');
45              }
46              if (c == '\\') {
47                  sb.append('\\');
48              }
49  
50              if (c == '\n') {
51                  sb.append("\");\n").append("    out.write(\"");
52                  continue;
53              }
54              sb.append(c);
55          }
56  
57          return sb.toString();
58      }
59  
60      private void wrap( final StringBuffer pInput, final Writer pOutput ) throws IOException {
61  
62          pOutput.append("    out.write(\"");
63  
64          pOutput.append(quote(pInput.toString()));
65  
66          pOutput.append("\");").append('\n');
67      }
68  
69      public byte[] generateJavaSource( final  String pResourceName, final File pFile ) {
70  
71      	final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
72          final Writer output = new OutputStreamWriter(outputStream);
73  
74          try {
75              final Reader input = new InputStreamReader(new FileInputStream(pFile));
76  
77              final int p = pResourceName.lastIndexOf('/');
78  
79              final String className;
80              final String packageName;
81  
82              if (p < 0) {
83                  className = ConversionUtils.stripExtension(pResourceName);
84                  packageName = "";
85              } else {
86                  className = ConversionUtils.stripExtension(pResourceName.substring(p+1));
87                  packageName = pResourceName.substring(0, p).replace('/', '.');
88                  output.append("package ").append(packageName).append(";").append('\n');
89              }
90  
91  
92              output.append("import java.io.PrintWriter;").append('\n');
93              output.append("import java.io.IOException;").append('\n');
94              output.append("import javax.servlet.http.HttpServlet;").append('\n');
95              output.append("import javax.servlet.http.HttpServletRequest;").append('\n');
96              output.append("import javax.servlet.http.HttpServletResponse;").append('\n');
97              output.append("import javax.servlet.ServletException;").append('\n');
98              output.append("public class ").append(className).append(" extends HttpServlet {").append('\n');
99              output.append("  protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {").append('\n');
100             output.append("    final PrintWriter out = response.getWriter();").append('\n');
101 
102 
103             final char[] open = "<?".toCharArray();
104             final char[] close = "?>".toCharArray();
105 
106             StringBuffer sb = new StringBuffer();
107             char[] watch = open;
108             int w = 0;
109             while(true) {
110                 int c = input.read();
111 
112                 if (c < 0) {
113                     break;
114                 }
115 
116                 if (c == watch[w]) {
117                     w++;
118                     if (watch.length == w) {
119                         if (watch == open) {
120                             // found open
121 
122                             wrap(sb, output);
123 
124                             sb = new StringBuffer();
125                             watch = close;
126                         } else if (watch == close) {
127                             // found close
128 
129                             // <? ... ?> is java
130                             output.append(sb.toString());
131 
132                             sb = new StringBuffer();
133                             watch = open;
134                         }
135                         w = 0;
136                     }
137                 } else {
138                     if (w > 0) {
139                         sb.append(watch, 0, w);
140                     }
141 
142                     sb.append((char)c);
143 
144                     w = 0;
145                 }
146             }
147 
148             if (watch == open) {
149                 wrap(sb, output);
150             }
151 
152 
153             output.append("    out.close();").append('\n');
154             output.append("    out.flush();").append('\n');
155             output.append("  }").append('\n');
156             output.append("}").append('\n');
157 
158             return outputStream.toByteArray();
159 
160         } catch (IOException e) {
161             return null;
162         } finally {
163             try {
164 				output.close();
165 			} catch (IOException e) {
166 			}        	
167         }
168     }
169 
170 }