You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by sh...@locus.apache.org on 2000/02/14 07:15:12 UTC

cvs commit: jakarta-tomcat/src/share/org/apache/jasper/resources messages.properties

shemnon     00/02/13 22:15:11

  Modified:    src/share/org/apache/jasper JspC.java
               src/share/org/apache/jasper/compiler
                        CommandLineCompiler.java
               src/share/org/apache/jasper/resources messages.properties
  Log:
  Added support for the compialtion of an entire web app, with options to
  generate web.xml info.
  
  Also fixed a bug where a class could have the name of a reserved keyword,
  now the command like compiler mangles them.
  
  Revision  Changes    Path
  1.4       +221 -62   jakarta-tomcat/src/share/org/apache/jasper/JspC.java
  
  Index: JspC.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/jasper/JspC.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- JspC.java	2000/02/13 06:25:23	1.3
  +++ JspC.java	2000/02/14 06:15:11	1.4
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/jasper/JspC.java,v 1.3 2000/02/13 06:25:23 akv Exp $
  - * $Revision: 1.3 $
  - * $Date: 2000/02/13 06:25:23 $
  + * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/jasper/JspC.java,v 1.4 2000/02/14 06:15:11 shemnon Exp $
  + * $Revision: 1.4 $
  + * $Date: 2000/02/14 06:15:11 $
    *
    * ====================================================================
    * 
  @@ -62,6 +62,8 @@
   package org.apache.jasper;
   
   import java.io.*;
  +import java.util.*;
  +
   import org.apache.jasper.compiler.JspReader;
   import org.apache.jasper.compiler.ServletWriter;
   import org.apache.jasper.compiler.TagLibraries;
  @@ -77,7 +79,6 @@
    * Shell for the jspc compiler.  Handles all options associated with the 
    * command line and creates compilation contexts which it then compiles
    * according to the specified options.
  - *
    * @author Danno Ferrin
    */
   public class JspC implements Options { //, JspCompilationContext {
  @@ -95,6 +96,13 @@
       public static final String SWITCH_FULL_STOP = "--";
       public static final String SWITCH_URI_BASE = "-uribase";
       public static final String SWITCH_URI_ROOT = "-uriroot";
  +    public static final String SWITCH_FILE_WEBAPP = "-webapp";
  +    public static final String SWITCH_WEBAPP_INC = "-webinc";
  +    public static final String SWITCH_WEBAPP_XML = "-webxml";
  +
  +    public static final int NO_WEBXML = 0;
  +    public static final int INC_WEBXML = 10;
  +    public static final int ALL_WEBXML = 20;
   
       // future direction
       //public static final String SWITCH_XML_OUTPUT = "-xml";
  @@ -118,10 +126,15 @@
   
       String uriRoot;
   
  +    String webxmlFile;
  +    int webxmlLevel;
  +
       //JspLoader loader;
   
       boolean dirset;
   
  +    Vector extensions;
  +
       public boolean getKeepGenerated() {
           // isn't this why we are running jspc?
           return true;
  @@ -198,18 +211,18 @@
       public JspC(String[] arg, PrintStream log) {
           args = arg;
           String tok;
  +
  +        int verbosityLevel = Logger.WARNING;
   
  -	int verbosityLevel = Logger.WARNING;
  -	
           while ((tok = nextArg()) != null) {
               if (tok.equals(SWITCH_QUIET)) {
  -		verbosityLevel = Logger.WARNING;
  +                verbosityLevel = Logger.WARNING;
               } else if (tok.equals(SWITCH_VERBOSE)) {
                   verbosityLevel = Logger.INFORMATION;
               } else if (tok.startsWith(SWITCH_VERBOSE)) {
                   try {
  -                    verbosityLevel 
  -			= Integer.parseInt(tok.substring(SWITCH_VERBOSE.length()));
  +                    verbosityLevel
  +                     = Integer.parseInt(tok.substring(SWITCH_VERBOSE.length()));
                   } catch (NumberFormatException nfe) {
                       log.println(
                           "Verbosity level " 
  @@ -247,6 +260,16 @@
                   uriBase = nextArg();
               } else if (tok.equals(SWITCH_URI_ROOT)) {
                   uriRoot = nextArg();
  +            } else if (tok.equals(SWITCH_WEBAPP_INC)) {
  +                webxmlFile = nextArg();
  +                if (webxmlFile != null) {
  +                    webxmlLevel = INC_WEBXML;
  +                };
  +            } else if (tok.equals(SWITCH_WEBAPP_XML)) {
  +                webxmlFile = nextArg();
  +                if (webxmlFile != null) {
  +                    webxmlLevel = ALL_WEBXML;
  +                };
               } else {
                   pushBackArg();
                   // Not a recognized Option?  Start treting them as JSP Pages
  @@ -254,12 +277,89 @@
               }
           }
   
  -	Constants.jasperLog = new TomcatLogger();
  -	Constants.jasperLog.setVerbosityLevel(verbosityLevel);
  +        Constants.jasperLog = new TomcatLogger();
  +        Constants.jasperLog.setVerbosityLevel(verbosityLevel);
   
       };
       
  +  public boolean parseFile(PrintStream log, String file, Writer mapout)
  +    {
  +        try {
  +            JspLoader loader =
  +                    new JspLoader(getClass().getClassLoader(), this);
  +            CommandLineContext clctxt = new CommandLineContext(
  +                    loader, getClassPath(), file, uriBase, uriRoot, false,
  +                    this);
  +            if ((targetClassName != null) && (targetClassName.length() > 0)) {
  +                clctxt.setServletClassName(targetClassName);
  +                clctxt.lockClassName();
  +            }
  +            if (targetPackage != null) {
  +                clctxt.setServletPackageName(targetPackage);
  +                clctxt.lockPackageName();
  +            }
  +            if (dirset) {
  +                clctxt.setOutputInDirs(true);
  +            };
  +            File uriDir = new File(clctxt.getRealPath("/"));
  +            if (uriDir.exists()) {
  +                if ((new File(uriDir, "WEB-INF/classes")).exists()) {
  +                    loader.addJar(clctxt.getRealPath("/WEB-INF/classes"));
  +                }
  +                File lib = new File(clctxt.getRealPath("WEB-INF/lib"));
  +                if (lib.exists() && lib.isDirectory()) {
  +                    String[] libs = lib.list();
  +                    for (int i = 0; i < libs.length; i++) {
  +                        try {
  +                            loader.addJar(lib.getCanonicalPath()
  +                                    + File.separator
  +                                    + libs[i]);
  +                        } catch (IOException ioe) {
  +                            // failing a toCanonicalPath on a file that
  +                            // exists() should be a JVM regression test,
  +                            // therefore we have permission to freak out
  +                            throw new RuntimeException(ioe.toString());
  +                        }
  +                    }
  +                }
  +            };
  +            CommandLineCompiler clc = new CommandLineCompiler(clctxt);
  +
  +            clc.compile();
  +
  +            targetClassName = null;
  +            if (mapout != null) {
  +                String thisServletName = clc.getPackageName()
  +                        + '.' + clc.getClassName();
  +                mapout.write("\n\t<servlet>\n\t\t<servlet-name>");
  +                mapout.write(thisServletName);
  +                mapout.write("</servlet-name>\n\t\t<servlet-class>");
  +                mapout.write(thisServletName);
  +                mapout.write("</servlet-class>\n\t</servlet>\n\t<servlet-mapping>\n\t\t<url-pattern>");
  +                mapout.write(file);
  +                mapout.write("</url-pattern>\n\t\t<servlet-name>");
  +                mapout.write(thisServletName);
  +                mapout.write("</servlet-name>\n\t</servlet-mapping>\n");
  +            };
  +            return true;
  +        } catch (JasperException je) {
  +            je.printStackTrace(log);
  +            log.print("error:");
  +            log.println(je.getMessage());
  +        } catch (Exception e) {
  +            e.printStackTrace(log);
  +            log.print("ERROR:");
  +            log.println(e.toString());
  +        };
  +        return false;
  +    }
  +
  +
       public void parseFiles(PrintStream log)  throws JasperException {
  +
  +        boolean scratchDirSet = (scratchDir != null);
  +        boolean urirootSet = (uriRoot != null);
  +
           // set up a scratch/output dir if none is provided
           if (scratchDir == null) {
               String temp = System.getProperty("java.io.tempdir");
  @@ -269,9 +369,11 @@
               scratchDir = new File(temp);
           }
   
  -        // set up the uri root if none is explicitly set
  -        if (uriRoot == null) {
  -            File f = new File(args[argPos]);
  +        File f = new File(args[argPos]);
  +        if (!f.exists() && f.isDirectory() && (args.length - argPos == 1)) {
  +            // do web-app conversion
  +        } else if (uriRoot == null) {
  +            // set up the uri root if none is explicitly set
               try {
                   if (f.exists()) {
                       f = new File(f.getCanonicalPath());
  @@ -300,60 +402,117 @@
   
           String file = nextFile();
           while (file != null) {
  -            try {
  -                JspLoader loader =
  -                        new JspLoader(getClass().getClassLoader(), this);
  -                CommandLineContext clctxt = new CommandLineContext(
  -                        loader, getClassPath(), file, uriBase, uriRoot, false,
  -                        this);
  -                if ((targetClassName != null) && (targetClassName.length() > 0)) {
  -                    clctxt.setServletClassName(targetClassName);
  -                    clctxt.lockClassName();
  -                }
  -                if (targetPackage != null) {
  -                    clctxt.setServletPackageName(targetPackage);
  -                    clctxt.lockPackageName();
  -                }
  -                if (dirset) {
  -                    clctxt.setOutputInDirs(true);
  +            if (SWITCH_FILE_WEBAPP.equals(file)) {
  +                String base = nextFile();
  +                if (base == null) {
  +                    // friendly but quiet failure
  +                    break;
  +                }// else if (".".equals(base)) {
  +                //    base = "";
  +                //};
  +                String oldRoot = uriRoot;
  +                if (!urirootSet) {
  +                    uriRoot = base;
                   };
  -                File uriDir = new File(clctxt.getRealPath("/"));
  -                if (uriDir.exists()) {
  -                    if ((new File(uriDir, "WEB-INF/classes")).exists()) {
  -                        loader.addJar(clctxt.getRealPath("/WEB-INF/classes"));
  -                    }
  -                    File lib = new File(clctxt.getRealPath("WEB-INF/lib"));
  -                    if (lib.exists() && lib.isDirectory()) {
  -                        String[] libs = lib.list();
  -                        for (int i = 0; i < libs.length; i++) {
  -                            try {
  -                                loader.addJar(lib.getCanonicalPath()
  -                                        + File.separator
  -                                        + libs[i]);
  -                            } catch (IOException ioe) {
  -                                // failing a toCanonicalPath on a file that
  -                                // exists() should be a JVM regression test,
  -                                // therefore we have permission to freak out
  -                                throw new RuntimeException(ioe.toString());
  -                            }
  -                        }
  -                    }
  +                Vector pages = new Vector();
  +
  +                Stack dirs = new Stack();
  +                dirs.push(base);
  +                if (extensions == null) {
  +                    extensions = new Vector();
  +                    extensions.addElement("jsp");
  +                };
  +                while (!dirs.isEmpty()) {
  +                    String s = dirs.pop().toString();
  +                    //System.out.println("--" + s);
  +                    f = new File(s);
  +                    if (f.exists() && f.isDirectory()) {
  +                        String[] files = f.list();
  +                        String ext;
  +                        for (int i = 0; i < files.length; i++) {
  +                            File f2 = new File(s, files[i]);
  +                            //System.out.println(":" + f2.getPath());
  +                            if (f2.isDirectory()) {
  +                                dirs.push(f2.getPath());
  +                                //System.out.println("++" + f2.getPath());
  +                            } else {
  +                                ext = files[i].substring(
  +                                        files[i].lastIndexOf('.') + 1);
  +                                if (extensions.contains(ext)) {
  +                                    //System.out.println(s + "?" + files[i]);
  +                                    pages.addElement(
  +                                        s + File.separatorChar + files[i]);
  +                                } else {
  +                                    //System.out.println("not done:" + ext);
  +                                };
  +                            };
  +                        };
  +                    };
  +                };
  +
  +                File froot = new File(uriRoot);
  +                String ubase = null;
  +                try {
  +                    ubase = froot.getCanonicalPath();
  +                } catch (IOException ioe) {
  +                    // if we cannot get the base, leave it null
                   };
  -                CommandLineCompiler clc = new CommandLineCompiler(clctxt);
  -                
  -                clc.compile();
  -
  -                targetClassName = null;
  -            } catch (JasperException je) {
  -                je.printStackTrace(System.err);
  -                System.out.print("error:");
  -                System.out.println(je.getMessage());
  -            } catch (Exception e) {
  -                e.printStackTrace(System.out);
  -                System.out.print("ERROR:");
  -                System.out.println(e.toString());
  +
  +                //System.out.println("==" + ubase);
  +
  +
  +                Writer mapout;
  +                try {
  +                    if (webxmlLevel >= INC_WEBXML) {
  +                        File fmapings = new File(webxmlFile);
  +                        mapout = new FileWriter(fmapings);
  +                    } else {
  +                        mapout = null;
  +                    };
  +                    if (webxmlLevel >= ALL_WEBXML) {
  +                        mapout.write(Constants.getString("jspc.webxml.header"));
  +                    };
  +                } catch (IOException ioe) {
  +                    mapout = null;
  +                };
  +
  +                Enumeration e = pages.elements();
  +                while (e.hasMoreElements())
  +                {
  +                    String nextjsp = e.nextElement().toString();
  +                    try {
  +                        if (ubase != null) {
  +                            File fjsp = new File(nextjsp);
  +                            String s = fjsp.getCanonicalPath();
  +                            //System.out.println("**" + s);
  +                            if (s.startsWith(ubase)) {
  +                                nextjsp = s.substring(ubase.length());
  +                            };
  +                        };
  +                    } catch (IOException ioe) {
  +                        // if we got problems dont change the file name
  +                    };
  +
  +                    if (nextjsp.startsWith("." + File.separatorChar)) {
  +                        nextjsp = nextjsp.substring(2);
  +                    };
  +
  +                    parseFile(log, nextjsp, mapout);
  +                };
  +                uriRoot = oldRoot;
  +                if (mapout != null) {
  +                    try {
  +                        if (webxmlLevel >= ALL_WEBXML) {
  +                            mapout.write(Constants.getString("jspc.webxml.footer"));
  +                        };
  +                        mapout.close();
  +                    } catch (IOException ioe) {
  +                        // noting to do if it fails since we are done with it
  +                    }
  +                }
  +            } else {
  +                parseFile(log, file, null);
               };
  -            targetClassName = null;
               file = nextFile();
           }
       };
  
  
  
  1.3       +32 -16    jakarta-tomcat/src/share/org/apache/jasper/compiler/CommandLineCompiler.java
  
  Index: CommandLineCompiler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/jasper/compiler/CommandLineCompiler.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- CommandLineCompiler.java	2000/02/09 06:50:48	1.2
  +++ CommandLineCompiler.java	2000/02/14 06:15:11	1.3
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/jasper/compiler/CommandLineCompiler.java,v 1.2 2000/02/09 06:50:48 shemnon Exp $
  - * $Revision: 1.2 $
  - * $Date: 2000/02/09 06:50:48 $
  + * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/jasper/compiler/CommandLineCompiler.java,v 1.3 2000/02/14 06:15:11 shemnon Exp $
  + * $Revision: 1.3 $
  + * $Date: 2000/02/14 06:15:11 $
    *
    * The Apache Software License, Version 1.1
    *
  @@ -84,12 +84,21 @@
   
           jsp = new File(ctxt.getJspFile());
           outputDir =  ctxt.getOptions().getScratchDir().getAbsolutePath();
  +
  +        setMangler(this);
  +
  +        computePackageName();
  +        ctxt.setServletPackageName(pkgName);
  +        className = getBaseClassName();
           // yes this is kind of messed up ... but it works
           if (ctxt.isOutputInDirs()) {
  +            String pkgName = ctxt.getServletPackageName();
  +            if (pkgName == null) {
  +                pkgName = "";
  +            }
               String tmpDir = outputDir
                      + File.separatorChar
  -                   + ctxt.getServletPackageName()
  -                         .replace('.', File.separatorChar);
  +                   + pkgName.replace('.', File.separatorChar);
               File f = new File(tmpDir);
               if (!f.exists()) {
                   if (f.mkdirs()) {
  @@ -99,11 +108,6 @@
                   outputDir = tmpDir;
               }
           }
  -
  -        setMangler(this);
  -
  -        computePackageName();
  -        className = getBaseClassName();
           computeClassFileName();
           computeJavaFileName();
       };
  @@ -120,6 +124,9 @@
   
       public final void computeJavaFileName() {
   	javaFileName = ctxt.getServletClassName() + ".java";
  +	if ("null.java".equals(javaFileName)) {
  +    	    javaFileName = getBaseClassName() + ".java";
  +    	};
   	if (outputDir != null && !outputDir.equals(""))
   	    javaFileName = outputDir + File.separatorChar + javaFileName;
       }
  @@ -158,14 +165,16 @@
   	} else {
   	    for (int i = 0; i < keywords.length; i++) {
   		char fs = File.separatorChar;
  -		int index1 = pathName.indexOf(fs + keywords[i]);
  -		int index2 = pathName.indexOf(keywords[i]);
  -		if (index1 == -1 && index2 == -1) continue;
  -		int index = (index2 == -1) ? index1 : index2;
  +		int index;
  +		if (pathName.startsWith(keywords[i] + fs)) {
  +		    index = 0;
  +		} else {
  +		    index = pathName.indexOf(fs + keywords[i] + fs);
  +		}
   		while (index != -1) {
   		    String tmpathName = pathName.substring (0,index+1) + '%';
   		    pathName = tmpathName + pathName.substring (index+2);
  -		    index = pathName.indexOf(fs + keywords[i]);
  +		    index = pathName.indexOf(fs + keywords[i] + fs);
   		}
   	    }
   	
  @@ -213,7 +222,14 @@
               else
                   className = jsp.getName();
           }
  -
  +	
  +	// since we don't mangle extensions like the servlet does,
  +	// we need to check for keywords as class names
  +	for (int i = 0; i < keywords.length; i++) {
  +	    if (className.equals(keywords[i])) {
  +		className += "%";
  +	    };
  +	};
   	
   	// Fix for invalid characters. If you think of more add to the list.
   	StringBuffer modifiedClassName = new StringBuffer();
  
  
  
  1.12      +24 -5     jakarta-tomcat/src/share/org/apache/jasper/resources/messages.properties
  
  Index: messages.properties
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/jasper/resources/messages.properties,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- messages.properties	2000/02/09 06:50:50	1.11
  +++ messages.properties	2000/02/14 06:15:11	1.12
  @@ -144,16 +144,35 @@
   jsp.error.jspc.uriroot_not_dir=The -uriroot option must specify a pre-existing directory
   jspc.implicit.uriRoot=uriRoot implicitly set to "{0}"
   jspc.usage=Usage: jspc <options> [--] <jsp files>\n\
  +where jsp files is any number of:\n\
  +\    <file>         A file to be parsed as a jsp page\n\
  +\    -webapp <dir>  A directory containing a web-app, all jsp pages\n\
  +\                   will recursivly be parsed\n\
   where options include:\n\
   \    -q          Quite mode (same as -v0)\n\
   \    -v[#]       Verbose mode (optional number is level, default is 2)\n\
   \    -d <dir>    Output Directory\n\
   \    -dd <dir>   Literal Output Directory.  (package dirs will not be made)\n\
  -\    -ieplugin   Java Plugin classid for IE\n\
   \    -p <name>   Name of target package\n\
   \    -c <name>   Name of target class name\n\
   \                (only applies to first JSP page)\n\
  -\    -uribase    The uri directory compilations shoule be relative to\n\
  -\                (Default is "/")\n\
  -\    -uriroot    The root directory that uri files should be resolced against\n\
  -\                (Default is the directory jspc is invoked from)\n\
  +\    -uribase <dir>  The uri directory compilations shoule be relative to\n\
  +\                    (Default is "/")\n\
  +\    -uriroot <dir>  The root directory that uri files should be resolved\n\
  +\                    against, (Default is the directory jspc is invoked from)\n\
  +\    -webinc <file>  Creates partial servlet mapings for the -webapp option\n\
  +\    -webxml <file>  Creates a complete web.xml when using the -webapp option.\n\
  +\    -ieplugin <clsid>  Java Plugin classid for Internet Explorer\n\
  +
  +jspc.webxml.header=<?xml version="1.0" encoding="ISO-8859-1"?>i\n\
  +\n\
  +<!DOCTYPE web-app\n\
  +\    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"\n\
  +\    "http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">\n\
  +<!-- automatically created by tomcat jspc -->\n\
  +<web-app>\n\
  +\n
  +jspc.webxml.footer=\n\
  +</web-app>\n\
  +\n
  +