kinman      02/04/02 15:38:13

  Modified:    jasper/src/share/org/apache/jasper CommandLineContext.java
               jasper/src/share/org/apache/jasper/compiler
                        CommandLineCompiler.java
  Log:
  - Fixed 5471 and 7488, where jspc generates package names that are not
    legal Java names.
  
    I decided not to use the patch submitted by Alain Coetmeur, but to reuse
    codes in CommandLineCompiler that mangles class names.  But thanks anyway.
  
  Revision  Changes    Path
  1.10      +16 -17    
jakarta-tomcat-4.0/jasper/src/share/org/apache/jasper/CommandLineContext.java
  
  Index: CommandLineContext.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-4.0/jasper/src/share/org/apache/jasper/CommandLineContext.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- CommandLineContext.java   2 Apr 2002 16:10:39 -0000       1.9
  +++ CommandLineContext.java   2 Apr 2002 23:38:13 -0000       1.10
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-tomcat-4.0/jasper/src/share/org/apache/jasper/CommandLineContext.java,v
 1.9 2002/04/02 16:10:39 glenn Exp $
  - * $Revision: 1.9 $
  - * $Date: 2002/04/02 16:10:39 $
  + * $Header: 
/home/cvs/jakarta-tomcat-4.0/jasper/src/share/org/apache/jasper/CommandLineContext.java,v
 1.10 2002/04/02 23:38:13 kinman Exp $
  + * $Revision: 1.10 $
  + * $Date: 2002/04/02 23:38:13 $
    *
    * ====================================================================
    * 
  @@ -216,33 +216,32 @@
       
       /**
        * The package name for the generated class.
  +     * The final package is assembled from the one specified in -p, and
  +     * the one derived from the path to jsp file.
        */
       public String getServletPackageName() {
  -        //get the path to the jsp file
  -        int indexOfSlash = getJspFile().lastIndexOf('/');
  +        //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 speced on
  +
  +        //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 && !servletPackageName.equals("")) {
  +        if (servletPackageName != null) {
               packageName = servletPackageName;
           }
  -        if (packageName.equals("")) {
  -            packageName = pathName.replace('/', '.');
  -        } else {
  -            packageName += pathName.replace('/', '.');
  -        }
  -        //strip off any leading '.' in the package name
  -        if (!packageName.equals("") && packageName.charAt(0) == '.') {
  -            packageName = packageName.substring(1);
  -        }
  -        return packageName;
  +        packageName += pathName.replace('/', '.');
  +
  +        return CommandLineCompiler.manglePackage(packageName);
       }
   
       /**
  
  
  
  1.5       +43 -16    
jakarta-tomcat-4.0/jasper/src/share/org/apache/jasper/compiler/CommandLineCompiler.java
  
  Index: CommandLineCompiler.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-4.0/jasper/src/share/org/apache/jasper/compiler/CommandLineCompiler.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- CommandLineCompiler.java  2 Apr 2002 16:10:39 -0000       1.4
  +++ CommandLineCompiler.java  2 Apr 2002 23:38:13 -0000       1.5
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-tomcat-4.0/jasper/src/share/org/apache/jasper/compiler/CommandLineCompiler.java,v
 1.4 2002/04/02 16:10:39 glenn Exp $
  - * $Revision: 1.4 $
  - * $Date: 2002/04/02 16:10:39 $
  + * $Header: 
/home/cvs/jakarta-tomcat-4.0/jasper/src/share/org/apache/jasper/compiler/CommandLineCompiler.java,v
 1.5 2002/04/02 23:38:13 kinman Exp $
  + * $Revision: 1.5 $
  + * $Date: 2002/04/02 23:38:13 $
    *
    * The Apache Software License, Version 1.1
    *
  @@ -61,6 +61,7 @@
   
   import java.io.File;
   import java.io.IOException;
  +import java.util.StringTokenizer;
   
   import org.apache.jasper.Constants;
   import org.apache.jasper.JspCompilationContext;
  @@ -86,8 +87,6 @@
           jsp = new File(ctxt.getJspFile());
           outputDir =  ctxt.getOptions().getScratchDir().getAbsolutePath();
        packageName = ctxt.getServletPackageName();
  -     if( packageName == null )
  -         packageName = "";
        pkgName = packageName;
           setMangler(this);
   
  @@ -163,30 +162,36 @@
                   className = jsp.getName().substring(0, jsp.getName().length() - 4);
               else
                   className = jsp.getName();
  +
           }
  +     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 (className.equals(keywords[i])) {
  -             className += "%";
  +         if (name.equals(keywords[i])) {
  +             name += "%";
  +             break;
            };
        };
        
        // Fix for invalid characters. If you think of more add to the list.
  -     StringBuffer modifiedClassName = new StringBuffer();
  -     if (Character.isJavaIdentifierStart(className.charAt(0)))
  -         modifiedClassName.append(className.charAt(0));
  +     StringBuffer modifiedName = new StringBuffer();
  +     if (Character.isJavaIdentifierStart(name.charAt(0)))
  +         modifiedName.append(name.charAt(0));
        else
  -         modifiedClassName.append(mangleChar(className.charAt(0)));
  -     for (int i = 1; i < className.length(); i++) {
  -         if (Character.isJavaIdentifierPart(className.charAt(i)))
  -             modifiedClassName.append(className.charAt(i));
  +         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
  -             modifiedClassName.append(mangleChar(className.charAt(i)));
  +             modifiedName.append(mangleChar(name.charAt(i)));
        }
        
  -     return modifiedClassName.toString();
  +     return modifiedName.toString();
       }
   
       private static final String mangleChar(char ch) {
  @@ -205,6 +210,28 @@
        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();
  +    }
   
       public final String getClassName() {
           return className;
  
  
  

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

Reply via email to