Hello all,

This is the first time I've submitted a patch to an open source project, so
please be gentle. As a new comer I chose something silly to repair.
Basically in 3.2 the bean that shows the example jsp source is broken.  I
made it possible to display jsp and html files that sit in a readable
directory of a Tomcat web application context.    I also added color to the
code for easy reading.  Dumb, I know.  But I thought it was a good slow
start.  

I've attached the .java file to replace the old one.  Below is the diff.

Thanks

Eric

--- ShowSourceNew.java  Sun Jun  3 21:16:11 2001
+++ ShowSource.java     Wed May 16 19:59:42 2001
@@ -1,15 +1,18 @@
 package examples;
 
+
 import javax.servlet.*;
 import javax.servlet.jsp.*;
 import javax.servlet.jsp.tagext.*;
+
 import java.io.*;
-import java.util.*;
 
 /**
  * Display the sources of the JSP file.
  */
-public class ShowSource extends TagSupport {
+public class ShowSource
+    extends TagSupport
+{
     String jspFile;
     
     public void setJspFile(String jspFile) {
@@ -17,54 +20,32 @@
     }
 
     public int doEndTag() throws JspException {
-       if ((jspFile.indexOf( ".." ) >= 0) ||
(jspFile.toUpperCase().indexOf("/WEB-INF/") != -1) ||
(jspFile.toUpperCase().indexOf("/META-INF/") != -1)) throw new
JspTagException("Invalid JSP file " + jspFile);
+       if ((jspFile.indexOf( ".." ) >= 0) ||
+            (jspFile.toUpperCase().indexOf("/WEB-INF/") != 0) ||
+            (jspFile.toUpperCase().indexOf("/META-INF/") != 0))
+           throw new JspTagException("Invalid JSP file " + jspFile);
+
+        InputStream in
+            = pageContext.getServletContext().getResourceAsStream(jspFile);
+
+        if (in == null)
+            throw new JspTagException("Unable to find JSP file: "+jspFile);
+
+        InputStreamReader reader = new InputStreamReader(in);
+       JspWriter out = pageContext.getOut();
 
-               JspWriter out = pageContext.getOut();
 
-               try {
-                       
-                       File f = new
File(pageContext.getServletContext().getRealPath(jspFile));
-               FileReader in = new FileReader(f);
-                       if (in == null) throw new JspTagException("Unable to
find JSP file: "+jspFile);
-                       int size = (int)f.length();
-                       char[] data = new char[size];
-                       int chars = 0;
-                       while (chars < size){
-                               chars += in.read(data, chars, size - chars);
-                       }
-                       out.println("<html>");
-                       for (int i=0; i < chars ; i++) {
-                               if (data[i] == '<' && data [i+1] == '!' &&
data [i+2] == '-'){
-                                       out.print("<font
color=green>&lt!-");
-                                       i = i + 3;
-                               }
-                               else if (data[i] == '-' && data [i+1] == '-'
&& data [i+2] == '>'){
-                                       out.print("--&gt</font>");
-                                       i = i + 3;
-                               }
-                               if (data[i] == '<' && data[i+1] == '%'){
-                                       out.print("<font color=red>&lt%");
-                                       i++;
-                               }
-                               else if (data[i] == '%' && data[i+1] ==
'>'){
-                                       out.print("%&gt</font>");
-                                       i++;
-                               }
-                               else if (data[i] == '<'){
-                                       out.print("<font color=blue>&lt");
-                               }
-                               else if (data[i] == '>'){
-                                       out.print("&gt</font>");
-                               }
-                               else if (data[i] == '\n'){
-                                       out.println("<br>");
-                               }
-                               else
-                                       out.print(data[i]);
-                               }
+        try {
+            out.println("<body>");
+            out.println("<pre>");
+            for(int ch = in.read(); ch != -1; ch = in.read())
+                if (ch == '<')
+                    out.print("&lt;");
+                else
+                    out.print((char) ch);
+            out.println("</pre>");
             out.println("</body>");
-        } 
-               catch (IOException ex) {
+        } catch (IOException ex) {
             throw new JspTagException("IOException: "+ex.toString());
         }
         return super.doEndTag();

ShowSource.java

Reply via email to