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 StringBuffer sb = new StringBuffer();
039: final char[] input = s.toCharArray();
040:
041: for (int i = 0; i < input.length; i++) {
042: final char c = input[i];
043: if (c == '"') {
044: sb.append('\\');
045: }
046: if (c == '\\') {
047: sb.append('\\');
048: }
049:
050: if (c == '\n') {
051: sb.append("\");\n").append(" out.write(\"");
052: continue;
053: }
054: sb.append(c);
055: }
056:
057: return sb.toString();
058: }
059:
060: private void wrap(final StringBuffer pInput, final Writer pOutput)
061: throws IOException {
062:
063: pOutput.append(" out.write(\"");
064:
065: pOutput.append(quote(pInput.toString()));
066:
067: pOutput.append("\");").append('\n');
068: }
069:
070: public byte[] generateJavaSource(final String pResourceName,
071: final File pFile) {
072:
073: try {
074: final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
075: final Reader input = new InputStreamReader(
076: new FileInputStream(pFile));
077: final Writer output = new OutputStreamWriter(outputStream);
078:
079: final int p = pResourceName.lastIndexOf('/');
080:
081: final String className;
082: final String packageName;
083:
084: if (p < 0) {
085: className = ConversionUtils
086: .stripExtension(pResourceName);
087: packageName = "";
088: } else {
089: className = ConversionUtils
090: .stripExtension(pResourceName.substring(p + 1));
091: packageName = pResourceName.substring(0, p).replace(
092: '/', '.');
093: output.append("package ").append(packageName).append(
094: ";").append('\n');
095: }
096:
097: output.append("import java.io.PrintWriter;").append('\n');
098: output.append("import java.io.IOException;").append('\n');
099: output.append("import javax.servlet.http.HttpServlet;")
100: .append('\n');
101: output.append(
102: "import javax.servlet.http.HttpServletRequest;")
103: .append('\n');
104: output.append(
105: "import javax.servlet.http.HttpServletResponse;")
106: .append('\n');
107: output.append("import javax.servlet.ServletException;")
108: .append('\n');
109: output.append("public class ").append(className).append(
110: " extends HttpServlet {").append('\n');
111: output
112: .append(
113: " protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {")
114: .append('\n');
115: output
116: .append(
117: " final PrintWriter out = response.getWriter();")
118: .append('\n');
119:
120: final char[] open = "<?".toCharArray();
121: final char[] close = "?>".toCharArray();
122:
123: StringBuffer sb = new StringBuffer();
124: char[] watch = open;
125: int w = 0;
126: while (true) {
127: int c = input.read();
128:
129: if (c < 0) {
130: break;
131: }
132:
133: if (c == watch[w]) {
134: w++;
135: if (watch.length == w) {
136: if (watch == open) {
137: // found open
138:
139: wrap(sb, output);
140:
141: sb = new StringBuffer();
142: watch = close;
143: } else if (watch == close) {
144: // found close
145:
146: // <? ... ?> is java
147: output.append(sb.toString());
148:
149: sb = new StringBuffer();
150: watch = open;
151: }
152: w = 0;
153: }
154: } else {
155: if (w > 0) {
156: sb.append(watch, 0, w);
157: }
158:
159: sb.append((char) c);
160:
161: w = 0;
162: }
163: }
164:
165: if (watch == open) {
166: wrap(sb, output);
167: }
168:
169: output.append(" out.close();").append('\n');
170: output.append(" out.flush();").append('\n');
171: output.append(" }").append('\n');
172: output.append("}").append('\n');
173:
174: output.close();
175:
176: return outputStream.toByteArray();
177: } catch (IOException e) {
178: return null;
179: }
180: }
181:
182: }
|