001    /*
002    * Copyright 2004 The Apache Software Foundation
003    *
004    * Licensed under the Apache License, Version 2.0 (the "License");
005    * you may not use this file except in compliance with the License.
006    * You may obtain a copy of the License at
007    *
008    *     http://www.apache.org/licenses/LICENSE-2.0
009    *
010    * Unless required by applicable law or agreed to in writing, software
011    * distributed under the License is distributed on an "AS IS" BASIS,
012    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013    * See the License for the specific language governing permissions and
014    * limitations under the License.
015    */
016    package examples;
017    
018    
019    import javax.servlet.*;
020    import javax.servlet.jsp.*;
021    import javax.servlet.jsp.tagext.*;
022    
023    import java.io.*;
024    
025    /**
026     * Display the sources of the JSP file.
027     */
028    public class ShowSource
029        extends TagSupport
030    {
031        String jspFile;
032        
033        public void setJspFile(String jspFile) {
034            this.jspFile = jspFile;
035        }
036    
037        public int doEndTag() throws JspException {
038            if ((jspFile.indexOf( ".." ) >= 0) ||
039                (jspFile.toUpperCase().indexOf("/WEB-INF/") != 0) ||
040                (jspFile.toUpperCase().indexOf("/META-INF/") != 0))
041                throw new JspTagException("Invalid JSP file " + jspFile);
042    
043            InputStream in
044                = pageContext.getServletContext().getResourceAsStream(jspFile);
045    
046            if (in == null)
047                throw new JspTagException("Unable to find JSP file: "+jspFile);
048    
049            InputStreamReader reader = new InputStreamReader(in);
050            JspWriter out = pageContext.getOut();
051    
052    
053            try {
054                out.println("<body>");
055                out.println("<pre>");
056                for(int ch = in.read(); ch != -1; ch = in.read())
057                    if (ch == '<')
058                        out.print("<");
059                    else
060                        out.print((char) ch);
061                out.println("</pre>");
062                out.println("</body>");
063            } catch (IOException ex) {
064                throw new JspTagException("IOException: "+ex.toString());
065            }
066            return super.doEndTag();
067        }
068    }
069    
070        
071            
072