001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.jci.examples.serverpages;
019
020 import java.io.ByteArrayOutputStream;
021 import java.io.File;
022 import java.io.FileInputStream;
023 import java.io.IOException;
024 import java.io.InputStreamReader;
025 import java.io.OutputStreamWriter;
026 import java.io.Reader;
027 import java.io.Writer;
028
029 import org.apache.commons.jci.utils.ConversionUtils;
030
031 /**
032 * @author tcurdt
033 */
034 public final class JspGenerator {
035
036 private String quote( final String s ) {
037
038 final StringBuilder sb = new StringBuilder();
039 final char[] input = s.toCharArray();
040
041 for (char c : input) {
042 if (c == '"') {
043 sb.append('\\');
044 }
045 if (c == '\\') {
046 sb.append('\\');
047 }
048
049 if (c == '\n') {
050 sb.append("\");\n").append(" out.write(\"");
051 continue;
052 }
053 sb.append(c);
054 }
055
056 return sb.toString();
057 }
058
059 private void wrap( final StringBuilder pInput, final Writer pOutput ) throws IOException {
060
061 pOutput.append(" out.write(\"");
062
063 pOutput.append(quote(pInput.toString()));
064
065 pOutput.append("\");").append('\n');
066 }
067
068 public byte[] generateJavaSource( final String pResourceName, final File pFile ) {
069
070 final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
071 final Writer output = new OutputStreamWriter(outputStream);
072
073 try {
074 final Reader input = new InputStreamReader(new FileInputStream(pFile));
075
076 final int p = pResourceName.lastIndexOf('/');
077
078 final String className;
079 final String packageName;
080
081 if (p < 0) {
082 className = ConversionUtils.stripExtension(pResourceName);
083 packageName = "";
084 } else {
085 className = ConversionUtils.stripExtension(pResourceName.substring(p+1));
086 packageName = pResourceName.substring(0, p).replace('/', '.');
087 output.append("package ").append(packageName).append(";").append('\n');
088 }
089
090
091 output.append("import java.io.PrintWriter;").append('\n');
092 output.append("import java.io.IOException;").append('\n');
093 output.append("import javax.servlet.http.HttpServlet;").append('\n');
094 output.append("import javax.servlet.http.HttpServletRequest;").append('\n');
095 output.append("import javax.servlet.http.HttpServletResponse;").append('\n');
096 output.append("import javax.servlet.ServletException;").append('\n');
097 output.append("public class ").append(className).append(" extends HttpServlet {").append('\n');
098 output.append(" protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {").append('\n');
099 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 }