remm        2002/06/02 14:40:22

  Modified:    jasper2/src/share/org/apache/jasper CommandLineContext.java
                        JspC.java
  Removed:     jasper2/src/share/org/apache/jasper/compiler
                        CommandLineCompiler.java
  Log:
  - Last part of the compiler refactoring.
  - Make JspC work with the Ant compiler.
  - Remove the CommandLineCompiler class (partially refactored into the
    CommandLineContext class).
  - Only tested with the JspC -webapp option. I don't really know what all the other
    options are supposed to do. Please test it.
  
  Revision  Changes    Path
  1.3       +239 -49   
jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/CommandLineContext.java
  
  Index: CommandLineContext.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/CommandLineContext.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- CommandLineContext.java   24 Apr 2002 02:21:04 -0000      1.2
  +++ CommandLineContext.java   2 Jun 2002 21:40:22 -0000       1.3
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/CommandLineContext.java,v
 1.2 2002/04/24 02:21:04 kinman Exp $
  - * $Revision: 1.2 $
  - * $Date: 2002/04/24 02:21:04 $
  + * $Header: 
/home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/CommandLineContext.java,v
 1.3 2002/06/02 21:40:22 remm Exp $
  + * $Revision: 1.3 $
  + * $Date: 2002/06/02 21:40:22 $
    *
    * ====================================================================
    * 
  @@ -66,10 +66,10 @@
   package org.apache.jasper;
   
   import java.io.*;
  +import java.util.StringTokenizer;
   
   import org.apache.jasper.compiler.JspReader;
   import org.apache.jasper.compiler.ServletWriter;
  -import org.apache.jasper.compiler.CommandLineCompiler;
   import org.apache.jasper.compiler.Compiler;
   
   import java.net.URL;
  @@ -83,9 +83,14 @@
    *
    * @author Danno Ferrin
    * @author Pierre Delisle
  + * @author Remy Maucherat
    */
   public class CommandLineContext implements JspCompilationContext {
   
  +
  +    // ----------------------------------------------------- Instance Variables
  +
  +
       String classPath;
       JspReader reader;
       ServletWriter writer;
  @@ -93,23 +98,24 @@
       boolean errPage;
       String jspFile;
       String servletClassName;
  -    String servletPackageName;
  +    String servletPackageName = Constants.JSP_PACKAGE_NAME;;
       String servletJavaFileName;
       String contentType;
       Options options;
  +    private String classFileName;
  +    private String jspPath;
  +    private String outputDir;
   
       String uriBase;
       File uriRoot;
   
       boolean outputInDirs;
   
  -    public CommandLineContext(String newClassPath,
  -                              String newJspFile, String newUriBase,
  +    public CommandLineContext(String newJspFile, String newUriBase,
                                 String newUriRoot, boolean newErrPage,
                                 Options newOptions)
  -    throws JasperException
  -    {
  -        classPath = newClassPath;
  +        throws JasperException {
  +
           uriBase = newUriBase;
           String tUriRoot = newUriRoot;
           jspFile = newJspFile;
  @@ -138,8 +144,33 @@
                           Constants.getString("jsp.error.jspc.uriroot_not_dir"));
               }
           }
  +
       }
   
  +
  +    /**
  +     * Resolve relative path, and create output directories.
  +     */
  +    public void setupContext() {
  +
  +        outputDir = options.getScratchDir().toString();
  +
  +        if (isOutputInDirs()) {
  +            int indexOfSlash = getJspFile().lastIndexOf('/');
  +            String pathName = "";
  +            if (indexOfSlash != -1) {
  +                pathName = getJspFile().substring(0, indexOfSlash);
  +            }
  +            String tmpDir = outputDir + File.separatorChar + pathName;
  +            File f = new File(tmpDir);
  +            if (!f.exists()) {
  +                f.mkdirs();
  +            }
  +        }
  +
  +    }
  +
  +
       /**
        * The classpath that is passed off to the Java compiler. 
        */
  @@ -148,6 +179,13 @@
       }
       
       /**
  +     * The classpath that is passed off to the Java compiler. 
  +     */
  +    public void setClassPath(String classPath) {
  +        this.classPath = classPath;
  +    }
  +
  +    /**
        * Get the input reader for the JSP text. 
        */
       public JspReader getReader() {
  @@ -179,25 +217,13 @@
       
       /**
        * The scratch directory to generate code into.
  -     *
  -     * FIXME: In some places this is called scratchDir and in some
  -     * other places it is called outputDir.
        */
       public String getOutputDir() {
  -        return options.getScratchDir().toString();
  +        return outputDir;
  +        //return options.getScratchDir().toString();
       }
       
       /**
  -     * The scratch directory to generate code into for javac.
  -     *
  -     * FIXME: In some places this is called scratchDir and in some
  -     * other places it is called outputDir.
  -     */
  -    public String getJavacOutputDir() {
  -        return options.getScratchDir().toString();
  -    }
  -
  -    /**
        * Path of the JSP URI. Note that this is not a file name. This is
        * the context rooted URI of the JSP file. 
        */
  @@ -210,6 +236,29 @@
        * generated class. 
        */
       public String getServletClassName() {
  +        if (servletClassName != null) {
  +            return servletClassName;
  +        }
  +        int iSep = jspFile.lastIndexOf('/') + 1;
  +        int iEnd = jspFile.length();
  +        StringBuffer modifiedClassName = 
  +            new StringBuffer(jspFile.length() - iSep);
  +     if (!Character.isJavaIdentifierStart(jspFile.charAt(iSep))) {
  +         // If the first char is not a legal Java letter or digit,
  +         // prepend a '$'.
  +         modifiedClassName.append('$');
  +     }
  +        for (int i = iSep; i < iEnd; i++) {
  +            char ch = jspFile.charAt(i);
  +            if (Character.isLetterOrDigit(ch)) {
  +                modifiedClassName.append(ch);
  +            } else if (ch == '.') {
  +                modifiedClassName.append('$');
  +            } else {
  +                modifiedClassName.append(mangleChar(ch));
  +            }
  +        }
  +        servletClassName = modifiedClassName.toString();
           return servletClassName;
       }
       
  @@ -219,36 +268,59 @@
        * the one derived from the path to jsp file.
        */
       public String getServletPackageName() {
  -        //Get the path to the jsp file.  Note that the jspFile, by the
  -     //time it gets here, would have been normalized to use '/'
  -     //as file separator.
  -
  -     int indexOfSlash = getJspFile().lastIndexOf('/');
  -        String pathName;
  -        if (indexOfSlash != -1) {
  -            pathName = getJspFile().substring(0, indexOfSlash);
  -        } else {
  -            pathName = "/";
  -        }
  -
  -        //Assemble the package name from the base package name specified on
  -        //the command line and the package name derived from the path to
  -        //the jsp file
  -        String packageName = "";
  -        if (servletPackageName != null) {
  -            packageName = servletPackageName;
  -        }
  -        packageName += pathName.replace('/', '.');
  -
  -        return CommandLineCompiler.manglePackage(packageName);
  +        return servletPackageName;
       }
   
  +
       /**
        * Full path name of the Java file into which the servlet is being
        * generated. 
        */
       public String getServletJavaFileName() {
  -        return servletJavaFileName;
  +
  +        if (servletJavaFileName != null) {
  +            return servletJavaFileName;
  +        }
  +
  +        String outputDir = getOutputDir();
  +        //servletJavaFileName = getServletClassName() + ".java";
  +        servletJavaFileName = "/" + getJspPath();
  +     if (outputDir != null && !outputDir.equals("")) {
  +         servletJavaFileName = outputDir + servletJavaFileName;
  +        }
  +     return servletJavaFileName;
  +
  +    }
  +
  +    public String getJspPath() {
  +        if (jspPath != null) {
  +            return jspPath;
  +        }
  +        String dirName = getJspFile();
  +        int pos = dirName.lastIndexOf('/');
  +        if (pos > 0) {
  +            dirName = dirName.substring(0, pos + 1);
  +        } else {
  +            dirName = "";
  +        }
  +        jspPath = dirName + getServletClassName() + ".java";
  +        if (jspPath.startsWith("/")) {
  +            jspPath = jspPath.substring(1);
  +        }
  +        return jspPath;
  +    }
  +
  +    public String getClassFileName() {
  +        if (classFileName != null) {
  +            return classFileName;
  +        }
  +
  +        String outputDir = getOutputDir();
  +        classFileName = getServletClassName() + ".class";
  +     if (outputDir != null && !outputDir.equals("")) {
  +         classFileName = outputDir + File.separatorChar + classFileName;
  +        }
  +     return classFileName;
       }
   
       /**
  @@ -319,8 +391,11 @@
        * is not done yet. Right now we're just hardcoding the actual
        * compilers that are created. 
        */
  -    public Compiler createCompiler() throws JasperException {
  -        return new CommandLineCompiler(this);
  +    public Compiler createCompiler()
  +        throws JasperException {
  +
  +        return new Compiler(this);
  +
       }
   
   
  @@ -390,5 +465,120 @@
            options.getTldLocationsCache().getLocation(uri);
        return location;
       }
  +
  +    public String getClassName() {
  +        String outputDir = getOutputDir();
  +        String classFileName = getBaseClassName() + ".class";
  +     if (outputDir != null && !outputDir.equals(""))
  +         classFileName = outputDir + File.separatorChar + classFileName;
  +        return classFileName;
  +    }
  +
  +    public static String [] keywords = {
  +        "abstract", "boolean", "break", "byte",
  +        "case", "catch", "char", "class",
  +        "const", "continue", "default", "do",
  +        "double", "else", "extends", "final",
  +        "finally", "float", "for", "goto",
  +        "if", "implements", "import",
  +        "instanceof", "int", "interface",
  +        "long", "native", "new", "package",
  +        "private", "protected", "public",
  +        "return", "short", "static", "super",
  +        "switch", "synchronized", "this",
  +        "throw", "throws", "transient",
  +        "try", "void", "volatile", "while"
  +    };
  +
  +    private final String getBaseClassName() {
  +        String className = getServletClassName();
  +        String prefix = null;
  +        if (className == null) {
  +            String jsp = getJspFile();
  +            int indexOfSlash = jsp.lastIndexOf('/');
  +            if (indexOfSlash != -1) {
  +                prefix = jsp.substring(0, indexOfSlash + 1);
  +                jsp = jsp.substring(indexOfSlash + 1);
  +            }
  +            if (jsp.endsWith(".jsp")) {
  +                className = jsp.substring(0, jsp.length() - 4);
  +            } else {
  +                className = jsp;
  +            }
  +        }
  +        if (prefix != null) {
  +            return prefix + mangleName(className);
  +        } else {
  +            return mangleName(className);
  +        }
  +    }
  +     
  +    private static final String mangleName(String name) {
  +
  +     // 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 (name.equals(keywords[i])) {
  +             name += "%";
  +             break;
  +         };
  +     };
  +     
  +     // Fix for invalid characters. If you think of more add to the list.
  +     StringBuffer modifiedName = new StringBuffer();
  +     if (Character.isJavaIdentifierStart(name.charAt(0)))
  +         modifiedName.append(name.charAt(0));
  +     else
  +         modifiedName.append(mangleChar(name.charAt(0)));
  +     for (int i = 1; i < name.length(); i++) {
  +         if (Character.isJavaIdentifierPart(name.charAt(i)))
  +             modifiedName.append(name.charAt(i));
  +         else
  +             modifiedName.append(mangleChar(name.charAt(i)));
  +     }
  +     
  +     return modifiedName.toString();
  +    }
  +
  +    private static final String mangleChar(char ch) {
  +     
  +        if(ch == File.separatorChar) {
  +         ch = '/';
  +     }
  +     String s = Integer.toHexString(ch);
  +     int nzeros = 5 - s.length();
  +     char[] result = new char[6];
  +     result[0] = '_';
  +     for (int i = 1; i <= nzeros; i++)
  +         result[i] = '0';
  +     for (int i = nzeros+1, j = 0; i < 6; i++, j++)
  +         result[i] = s.charAt(j);
  +     return new String(result);
  +    }
  +
  +    /**
  +     * Make sure that the package name is a legal Java name
  +     *
  +     * @param name The input string, containing arbitary chars separated by
  +     *             '.'s, with possible leading, trailing, or double '.'s
  +     * @return legal Java package name.
  +     */
  +    public static String manglePackage(String name) {
  +        boolean first = true;
  +
  +        StringBuffer b = new StringBuffer();
  +        StringTokenizer t = new StringTokenizer(name, ".");
  +        while (t.hasMoreTokens()) {
  +            String nt = t.nextToken();
  +            if (nt.length() > 0) {
  +                if (b.length() > 0)
  +                    b.append('.');
  +                b.append(mangleName(nt));
  +            }
  +        }
  +        return b.toString();
  +    }
  +
  +
   }
   
  
  
  
  1.6       +27 -16    
jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/JspC.java
  
  Index: JspC.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/JspC.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- JspC.java 31 May 2002 05:13:13 -0000      1.5
  +++ JspC.java 2 Jun 2002 21:40:22 -0000       1.6
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/JspC.java,v 1.5 
2002/05/31 05:13:13 remm Exp $
  - * $Revision: 1.5 $
  - * $Date: 2002/05/31 05:13:13 $
  + * $Header: 
/home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/JspC.java,v 1.6 
2002/06/02 21:40:22 remm Exp $
  + * $Revision: 1.6 $
  + * $Date: 2002/06/02 21:40:22 $
    *
    * ====================================================================
    * 
  @@ -68,7 +68,6 @@
   import org.apache.jasper.compiler.JspReader;
   import org.apache.jasper.compiler.ServletWriter;
   import org.apache.jasper.compiler.Compiler;
  -import org.apache.jasper.compiler.CommandLineCompiler;
   import org.apache.jasper.compiler.TldLocationsCache;
   
   import org.apache.jasper.servlet.JasperLoader;
  @@ -85,7 +84,7 @@
    * @author Danno Ferrin
    * @author Pierre Delisle
    */
  -public class JspC implements Options { //, JspCompilationContext {
  +public class JspC implements Options {
   
       public static final String DEFAULT_IE_CLASS_ID = 
               "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93";
  @@ -367,9 +366,9 @@
       public boolean parseFile(PrintStream log, String file, Writer servletout, 
Writer mappingout)
       {
           try {
  -            CommandLineContext clctxt = new CommandLineContext(
  -                    getClassPath(), file.replace('\\','/'), uriBase, uriRoot,
  -                 false, this);
  +
  +            CommandLineContext clctxt = new CommandLineContext
  +                (file.replace('\\','/'), uriBase, uriRoot, false, this);
               if ((targetClassName != null) && (targetClassName.length() > 0)) {
                   clctxt.setServletClassName(targetClassName);
               }
  @@ -379,12 +378,18 @@
               if (dirset) {
                   clctxt.setOutputInDirs(true);
               }
  +
  +            clctxt.setupContext();
  +
  +            String classPath = getClassPath();
            ArrayList urls = new ArrayList();
   
               if (new File(clctxt.getRealPath("/")).exists()) {
                   File classes = new File(clctxt.getRealPath("/WEB-INF/classes"));
                   try {
                        if (classes.exists()) {
  +                         classPath = classPath + File.pathSeparator 
  +                             + classes.getCanonicalPath();
                            urls.add(classes.getCanonicalFile().toURL());
                       }
                   } catch (IOException ioe) {
  @@ -401,6 +406,8 @@
                            File libFile = new File(lib.toString()
                                       + File.separator
                                       + libs[i]);
  +                            classPath = classPath + File.pathSeparator 
  +                                + libFile.getCanonicalPath();
                            urls.add(libFile.getCanonicalFile().toURL());
                           } catch (IOException ioe) {
                               // failing a toCanonicalPath on a file that
  @@ -412,7 +419,7 @@
                   }
               }
               StringTokenizer tokenizer = new StringTokenizer
  -                (clctxt.getClassPath(), File.pathSeparator);
  +                (getClassPath(), File.pathSeparator);
               while (tokenizer.hasMoreTokens()) {
                   String path = tokenizer.nextToken();
                   try {
  @@ -430,17 +437,21 @@
               URLClassLoader loader = new URLClassLoader
                   ((URL[])(urls.toArray(new URL[urls.size()])));
               clctxt.setClassLoader(loader);
  -            CommandLineCompiler clc = new CommandLineCompiler(clctxt);
  +            clctxt.setClassPath(classPath);
  +            Compiler clc = clctxt.createCompiler();
   
               clc.compile();
   
               targetClassName = null;
  +
  +            String className = clctxt.getClassName();
  +            String packageName = clctxt.getServletPackageName();
  +
               String thisServletName;
  -            if  ("".equals(clc.getPackageName())) {
  -                thisServletName = clc.getClassName();
  -             } else {
  -                thisServletName = clc.getPackageName()
  -                    + '.' + clc.getClassName();
  +            if  ("".equals(packageName)) {
  +                thisServletName = className;
  +            } else {
  +                thisServletName = packageName + '.' + className;
               }
               if (servletout != null) {
                   servletout.write("\n\t<servlet>\n\t\t<servlet-name>");
  @@ -466,6 +477,7 @@
                   dieOnExit = true;
               }
           } catch (FileNotFoundException fne) {
  +            fne.printStackTrace();
                   Constants.message("jspc.error.fileDoesNotExist", 
                           new Object[] {fne.getMessage()}, Logger.WARNING);
           } catch (Exception e) {
  @@ -478,7 +490,6 @@
           }
           return false;
       }
  -
   
       public void parseFiles(PrintStream log)  throws JasperException {
   
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to