001 package org.apache.myfaces.tobago.example.demo.jsp;
002
003 /*
004 * Licensed to the Apache Software Foundation (ASF) under one or more
005 * contributor license agreements. See the NOTICE file distributed with
006 * this work for additional information regarding copyright ownership.
007 * The ASF licenses this file to You under the Apache License, Version 2.0
008 * (the "License"); you may not use this file except in compliance with
009 * the License. You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020 /*
021 * Created on: 02.09.2002, 22:11:52
022 * $Id: JspFormatter.java 578592 2007-09-23 18:51:32Z bommel $
023 */
024
025 import javax.servlet.jsp.JspWriter;
026 import java.io.FileNotFoundException;
027 import java.io.FileReader;
028 import java.io.IOException;
029 import java.io.InputStream;
030 import java.io.InputStreamReader;
031 import java.io.LineNumberReader;
032 import java.io.PrintWriter;
033 import java.io.Reader;
034
035 public class JspFormatter {
036
037 public static void main(String[] args) throws Exception {
038 writeJsp(new FileReader(args[0]), new PrintWriter(System.out));
039 }
040
041 public static void writeJsp(String filename, JspWriter out)
042 throws IOException {
043 InputStream in
044 = JspFormatter.class.getClassLoader().getResourceAsStream(filename);
045 if (in == null) {
046 throw new FileNotFoundException(
047 "Resource not found in classpath: filename = '" + filename + "'");
048 }
049 writeJsp(new InputStreamReader(in), new PrintWriter(out));
050 }
051
052 public static void writeJsp(Reader reader, PrintWriter out)
053 throws IOException {
054 JspTagConverter formatter = new JspTagConverter();
055 String source = readJsp(reader);
056 out.println("<html><head>");
057 // out.println("<link rel=\"stylesheet\" href=\"jsp.css\" type=\"text/css\">");
058 out.println("<style>");
059 out.println("body {background-color: white;}");
060 out.println(".jsp-comment {background-color: rgb(227,227,227); color: rgb(128,128,128);}");
061 out.println(".jsp-directive {background-color: rgb(237,255,237);}");
062 out.println(".jsp-declaration {background-color: rgb(255,252,228);}");
063 out.println(".jsp-scriptlet {background-color: rgb(255,252,228); color: black; font-weight: normal;}");
064 out.println(".jsp-tag {background-color: rgb(255,252,228);}");
065 out.println(".html-tag {background-color: rgb(239,239,239);}");
066 out.println(".string {color: rgb(0,128,0); font-weight: bold;}");
067 out.println(".keyword {color: rgb(0,0,128); font-weight: bold;}");
068 out.println("</style>");
069 out.println("<title>formatted jsp code</title></head><body><pre>");
070 out.println(formatter.convert(source));
071 out.println("</pre></body></html>");
072 out.flush();
073 }
074
075 private static String readJsp(Reader reader) throws IOException {
076 LineNumberReader in = new LineNumberReader(reader);
077 StringBuilder buffer = new StringBuilder();
078 String line = null;
079 while (null != (line = in.readLine())) {
080 buffer.append(line);
081 buffer.append("\n");
082 }
083 in.close();
084 return buffer.toString();
085 }
086
087 }