You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by co...@apache.org on 2001/05/28 05:03:29 UTC

cvs commit: jakarta-tomcat-jasper/jasper34/liaison/org/apache/jasper34/tomcat33 JasperMangler.java JspInterceptor.java

costin      01/05/27 20:03:29

  Modified:    jasper34/liaison/org/apache/jasper34/servlet
                        JspEngineContext.java
               jasper34/liaison/org/apache/jasper34/tomcat33
                        JspInterceptor.java
  Added:       jasper34/liaison/org/apache/jasper34/servlet ClassName.java
                        JspCompiler.java
               jasper34/liaison/org/apache/jasper34/tomcat33
                        JasperMangler.java
  Log:
  Moved the liaison-specific classes out of generator.
  
  We need to make the manglers and options replaceable ( i.e. the user
  should be able to plug any scheme in any liaison )
  
  Revision  Changes    Path
  1.2       +1 -0      jakarta-tomcat-jasper/jasper34/liaison/org/apache/jasper34/servlet/JspEngineContext.java
  
  Index: JspEngineContext.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-jasper/jasper34/liaison/org/apache/jasper34/servlet/JspEngineContext.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- JspEngineContext.java	2001/05/28 02:17:58	1.1
  +++ JspEngineContext.java	2001/05/28 03:03:28	1.2
  @@ -67,6 +67,7 @@
   import org.apache.jasper34.generator.Compiler;
   import org.apache.jasper34.core.*;
   import org.apache.jasper34.runtime.*;
  +import org.apache.jasper34.javacompiler.*;
   
   import org.apache.tomcat.util.log.*;
   /**
  
  
  
  1.1                  jakarta-tomcat-jasper/jasper34/liaison/org/apache/jasper34/servlet/ClassName.java
  
  Index: ClassName.java
  ===================================================================
  /*
   * ====================================================================
   * 
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights 
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:  
   *       "This product includes software developed by the 
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written 
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */ 
  
  package org.apache.jasper34.servlet;
  
  import org.apache.jasper34.core.*;
  import org.apache.jasper34.generator.*;
  import org.apache.jasper34.runtime.JasperException;
  
  import java.io.DataInputStream;
  import java.io.FileInputStream;
  import java.io.InputStream;
  import java.io.IOException;
  
  
  
  /**
   * Parse a .class file to figure out the name of the class from which
   * it was generated. 
   *
   * @author Anil Vijendran
   */
  public class ClassName {
  
      static String processClassData(InputStream in) throws JasperException, IOException {
  	DataInputStream din = new DataInputStream(in);
  	din.readInt(); // magic
  	din.readUnsignedShort(); // majorVersion
  	din.readUnsignedShort(); // minorVersion
  	int count = din.readUnsignedShort(); // #constant pool entries
  	ConstantPool[] constantPool = new ConstantPool[count];
  	constantPool[0] = new ConstantPool();
  	for (int i = 1; i < constantPool.length; i++) {
  	    constantPool[i] = new ConstantPool();
  	    if (!constantPool[i].read(din))
  		throw new JasperException(Constants.getString("jsp.error.classname"));
  	    // These two types take up "two" spots in the table
  	    if ((constantPool[i].type == ConstantPool.LONG) ||
  		(constantPool[i].type == ConstantPool.DOUBLE))
  		i++;
  	}
  
  	for (int i = 1; i < constantPool.length; i++) {
  	    if (constantPool[i] == null)
  		continue;
  	    if (constantPool[i].index1 > 0)
  		constantPool[i].arg1 = constantPool[constantPool[i].index1];
  	    if (constantPool[i].index2 > 0)
  		constantPool[i].arg2 = constantPool[constantPool[i].index2];
  	}
  	int accessFlags = din.readUnsignedShort();
  	ConstantPool thisClass = constantPool[din.readUnsignedShort()];
          din.close();
  	return printClassName(thisClass.arg1.strValue);
      }
  
      private static String printClassName(String s) {
  	StringBuffer x;
  
  	if (s.charAt(0) == '[') {
  	    return(typeString(s, ""));
  	}
  
  	x = new StringBuffer();
  	for (int j = 0; j < s.length(); j++) {
  	    if (s.charAt(j) == '/')
  		x.append('.');
  	    else
  		x.append(s.charAt(j));
  	}
  	return (x.toString());
      }
  
      private static String typeString(String typeString, String varName) {
  	    int isArray = 0;
  	    int	ndx = 0;
  	    StringBuffer x = new StringBuffer();
  
  	    while (typeString.charAt(ndx) == '[') {
  	        isArray++;
  	        ndx++;
  	    }
  
  	    switch (typeString.charAt(ndx)) {
  	        case 'B' :
  		        x.append("byte ");
  		        break;
  	        case 'C' :
  		        x.append("char ");
  		        break;
  	        case 'D' :
  		        x.append("double ");
  		        break;
  	        case 'F' :
  		        x.append("float ");
  		        break;
  	        case 'I' :
  		        x.append("int ");
  		        break;
  	        case 'J' :
  		        x.append("long ");
  		        break;
  	        case 'L' :
  		        for (int i = ndx+1; i < typeString.indexOf(';'); i++) {
  		            if (typeString.charAt(i) != '/')
  			            x.append(typeString.charAt(i));
  		            else
  			        x.append('.');
  		        }
  		        x.append(" ");
  		        break;
  	        case 'V':
  		        x.append("void ");
  		        break;
  	        case 'S' :
  		        x.append("short ");
  		        break;
  	        case 'Z' :
  		        x.append("boolean ");
  		        break;
  	    }
  	    x.append(varName);
  	    while (isArray > 0) {
  	        x.append("[]");
  	        isArray--;
  	    }
  	    return (x.toString());
      }
  
      public static String getClassName(String classFile) throws JasperException {
  	try {
  	    //	    System.out.println("Getting class name from class data");
  	    FileInputStream fin = new FileInputStream(classFile);
  	    return processClassData(fin);
  	} catch (IOException ex) {
  	    throw new JasperException(Constants.getString("jsp.error.classname"), 
  	    					ex);
  	}
      }
      
      public static void main(String[] args) {
  	try {
  	    for(int i = 0; i < args.length; i++)
  		System.out.println("Filename: "+ args[i]+" Classname: "+getClassName(args[i]));
  	} catch (Exception ex) {
  	    ex.printStackTrace();	// OK
  	}
      }
  }
  
  class ConstantPool {
      int	type;			// type of this item
      String name; 		// String for the type
      ConstantPool  arg1;	// index to first argument
      ConstantPool  arg2;	// index to second argument
      int index1, index2;
      String 	strValue; 		// ASCIZ String value
      int		intValue;
      long	longValue;
      float	floatValue;
      double	doubleValue;
  
      public static final int CLASS = 7;
      public static final int FIELDREF = 9;
      public static final int METHODREF = 10;
      public static final int STRING = 8;
      public static final int INTEGER = 3;
      public static final int FLOAT = 4;
      public static final int LONG = 5;
      public static final int DOUBLE = 6;
      public static final int INTERFACE = 11;
      public static final int NAMEANDTYPE = 12;
      public static final int ASCIZ = 1;
      public static final int UNICODE = 2;
  
  
      /**
       * Generic constructor
       */
      public ConstantPool() {
  	index1 = -1;
  	index2 = -1;
  	arg1 = null;
  	arg2 = null;
  	type = -1;
      }
  
      public boolean read(DataInputStream din)
  	throws IOException {
  	int	len;
  	char	c;
  
  	type = din.readByte();
  	switch (type) {
  	    case CLASS:
  		name = "Class";
  		index1 = din.readUnsignedShort();
  		index2 = -1;
  		break;
  	    case FIELDREF:
  		name = "Field Reference";
  		index1 = din.readUnsignedShort();
  		index2 = din.readUnsignedShort();
  		break;
  	    case METHODREF:
  		name = "Method Reference";
  		index1 = din.readUnsignedShort();
  		index2 = din.readUnsignedShort();
  		break;
  	    case INTERFACE:
  		name = "Interface Method Reference";
  		index1 = din.readUnsignedShort();
  		index2 = din.readUnsignedShort();
  		break;
  	    case NAMEANDTYPE:
  		name = "Name and Type";
  		index1 = din.readUnsignedShort();
  		index2 = din.readUnsignedShort();
  		break;
  	    case STRING:
  		name = "String";
  		index1 = din.readUnsignedShort();
  		index2 = -1;
  		break;
  	    case INTEGER:
  		name = "Integer";
  		intValue = din.readInt();
  		break;
  	    case FLOAT:
  		name = "Float";
  		floatValue = din.readFloat();
  		break;
  	    case LONG:
  		name = "Long";
  		longValue = din.readLong();
  		break;
  	    case DOUBLE:
  		name = "Double";
  		doubleValue = din.readDouble();
  		break;
  	    case ASCIZ:
  	    case UNICODE:
  		if (type == ASCIZ)
  		    name = "ASCIZ";
  		else
  		    name = "UNICODE";
  
  		StringBuffer xxBuf = new StringBuffer();
  
  		len = din.readUnsignedShort();
  		while (len > 0) {
  		    c = (char) (din.readByte());
  		    xxBuf.append(c);
  		    len--;
  		}
  		strValue = xxBuf.toString();
  		break;
  	    default:
  		System.err.println(Constants.getString("jsp.warning.bad.type"));
  	}
  	return (true);
      }
  }
  
  
  
  
  1.1                  jakarta-tomcat-jasper/jasper34/liaison/org/apache/jasper34/servlet/JspCompiler.java
  
  Index: JspCompiler.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights 
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:  
   *       "This product includes software developed by the 
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written 
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */ 
  
  package org.apache.jasper34.servlet;
  
  import java.io.File;
  import java.io.FileNotFoundException;
  
  import org.apache.jasper34.core.*;
  import org.apache.jasper34.generator.*;
  import org.apache.jasper34.generator.Compiler;
  import org.apache.jasper34.runtime.JasperException;
  
  import org.apache.tomcat.util.log.*;
  
  /**
   * JspCompiler is an implementation of Compiler with a funky code
   * mangling and code generation scheme!
   *
   * The reason that it is both a sub-class of compiler and an implementation
   * of mangler is because the isOutDated method that is overridden and the
   * name mangulation both depend on the actual existance of other class and
   * java files.  I.e. the value of a mangled name is a function of both the
   * name to be mangled and also of the state of the scratchdir.
   *
   * @author Anil K. Vijendran
   */
  public class JspCompiler extends Compiler implements Mangler {
      
      String pkgName, javaFileName, classFileName;
      String realClassName;
  
      File jsp;
      String outputDir;
  
      //    ClassFileData cfd;
      boolean outDated;
      static final int JSP_TOKEN_LEN= Constants.JSP_TOKEN.length();
  
      Log loghelper = Log.getLog("JASPER_LOG", "JspCompiler");
      
      public JspCompiler(JspCompilationContext ctxt) throws JasperException {
          super(ctxt);
          
          this.jsp = new File(ctxt.getJspFile());
          this.outputDir = ctxt.getOutputDir();
          this.outDated = false;
          setMangler(this);
  
  	// If the .class file exists and is outdated, compute a new
  	// class name
  	if( isOutDated() ) {
  	    generateNewClassName();
  	}
      }
  
      private void generateNewClassName() {
  	File classFile = new File(getClassFileName());
  	if (! classFile.exists()) {
  	     String prefix = getPrefix(jsp.getPath());
  	     realClassName= prefix + getBaseClassName() +
  		 Constants.JSP_TOKEN + "0";
  	    return;
  	} 
  
  	String cn=getRealClassName();
  	String baseClassName = cn.
  	    substring(0, cn.lastIndexOf(Constants.JSP_TOKEN));
  	int jspTokenIdx=cn.lastIndexOf(Constants.JSP_TOKEN);
  	String versionS=cn.substring(jspTokenIdx + JSP_TOKEN_LEN,
  				     cn.length());
  	int number= Integer.valueOf(versionS).intValue();
  	number++;
  	realClassName = baseClassName + Constants.JSP_TOKEN + number;
      }
      
      /** Return the real class name for the JSP, including package and
       *   version.
       *
       *  This method is called when the server is started and a .class file
       *  is found from a previous compile or when the .class file is older,
       *  to find next version.
       */
      public final String getRealClassName() {
  	if( realClassName!=null ) return realClassName;
  
          try {
              realClassName = ClassName.getClassName( getClassFileName() );
          } catch( JasperException ex) {
              // ops, getClassName should throw something
  	    loghelper.log("Exception in getRealClassName", ex);
  	    return null;
          }
  	return realClassName;
  
      }
      
      public final String getClassName() {
          // CFD gives you the whole class name
          // This method returns just the class name sans the package
  
  	String cn=getRealClassName();
          int lastDot = cn.lastIndexOf('.');
  	String className=null;
          if (lastDot != -1) 
              className = cn.substring(lastDot+1,
                                       cn.length());
          else // no package name case
              className = cn;
  
          return className;
      }
  
      public final String getJavaFileName() {
          if( javaFileName!=null ) return javaFileName;
  	javaFileName = getClassName() + ".java";
   	if (outputDir != null && !outputDir.equals(""))
   	    javaFileName = outputDir + File.separatorChar + javaFileName;
  	return javaFileName;
      }
      
      public final String getClassFileName() {
          if( classFileName!=null) return classFileName;
  
  	//        computeClassFileName();
          String prefix = getPrefix(jsp.getPath());
          classFileName = prefix + 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" 
      };
  
      public final String getPackageName() {
          if( pkgName!=null) return pkgName;
  
  	// compute package name 
  	String pathName = jsp.getPath();
  	StringBuffer modifiedpkgName = new StringBuffer ();
          int indexOfSepChar = pathName.lastIndexOf(File.separatorChar);
          
  	if (indexOfSepChar == -1 || indexOfSepChar == 0)
  	    pkgName = null;
  	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;
  		while (index != -1) {
  		    String tmpathName = pathName.substring (0,index+1) + '%';
  		    pathName = tmpathName + pathName.substring (index+2);
  		    index = pathName.indexOf(fs + keywords[i]);
  		}
  	    }
  	    
  	    // XXX fix for paths containing '.'.
  	    // Need to be more elegant here.
              pathName = pathName.replace('.','_');
  	    
  	    pkgName = pathName.substring(0, pathName.lastIndexOf(
  	    		File.separatorChar)).replace(File.separatorChar, '.');
  	    for (int i=0; i<pkgName.length(); i++) 
  		if (Character.isLetter(pkgName.charAt(i)) == true ||
  		    pkgName.charAt(i) == '.') {
  		    modifiedpkgName.append(pkgName.substring(i,i+1));
  		}
  		else
  		    modifiedpkgName.append(mangleChar(pkgName.charAt(i)));
  
  	    if (modifiedpkgName.charAt(0) == '.') {
                  String modifiedpkgNameString = modifiedpkgName.toString();
                  pkgName = modifiedpkgNameString.
  		    substring(1, 
  			      modifiedpkgName.length ());
              }
  	    else 
  	        pkgName = modifiedpkgName.toString();
  	}
  	return pkgName;
      }
  
      private final String getBaseClassName() {
  	String className;
          
          if (jsp.getName().endsWith(".jsp"))
              className = jsp.getName().substring(0, jsp.getName().length() - 4);
          else
              className = jsp.getName();
              
  	
  	// Fix for invalid characters. If you think of more add to the list.
  	StringBuffer modifiedClassName = new StringBuffer();
  	for (int i = 0; i < className.length(); i++) {
  	    if (Character.isLetterOrDigit(className.charAt(i)) == true)
  		modifiedClassName.append(className.substring(i,i+1));
  	    else
  		modifiedClassName.append(mangleChar(className.charAt(i)));
  	}
  	
  	return modifiedClassName.toString();
      }
  
      private final String getPrefix(String pathName) {
  	if (pathName != null) {
  	    StringBuffer modifiedName = new StringBuffer();
  	    for (int i = 0; i < pathName.length(); i++) {
  		if (Character.isLetter(pathName.charAt(i)) == true)
  		    modifiedName.append(pathName.substring(i,i+1));
  		else
  		    modifiedName.append(mangleChar(pathName.charAt(i)));
   	    }
  	    return modifiedName.toString();
  	}
  	else 
              return "";
      }
  
      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);
      }
  
      /**
       * Determines whether the current JSP class is older than the JSP file
       * from whence it came
       */
      public boolean isOutDated() {
          File jspReal = null;
  
          String realPath = ctxt.getRealPath(jsp.getPath());
          if (realPath == null)
              return true;
  
          jspReal = new File(realPath);
  
  		  if(!jspReal.exists()){
  			  return true;
  		  }
  
  		  File classFile = new File(getClassFileName());
          if (classFile.exists()) {
              outDated = classFile.lastModified() < jspReal.lastModified();
          } else {
              outDated = true;
          }
  
          return outDated;
      }
  }
  
  
  
  
  1.2       +1 -0      jakarta-tomcat-jasper/jasper34/liaison/org/apache/jasper34/tomcat33/JspInterceptor.java
  
  Index: JspInterceptor.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-jasper/jasper34/liaison/org/apache/jasper34/tomcat33/JspInterceptor.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- JspInterceptor.java	2001/05/28 02:18:58	1.1
  +++ JspInterceptor.java	2001/05/28 03:03:29	1.2
  @@ -72,6 +72,7 @@
   import org.apache.jasper34.core.*;
   import org.apache.jasper34.runtime.*;
   import org.apache.jasper34.generator.*;
  +import org.apache.jasper34.javacompiler.*;
   import org.apache.jasper34.generator.Compiler;
   
   import org.apache.tomcat.core.*;
  
  
  
  1.1                  jakarta-tomcat-jasper/jasper34/liaison/org/apache/jasper34/tomcat33/JasperMangler.java
  
  Index: JasperMangler.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights 
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:  
   *       "This product includes software developed by the 
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written 
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */ 
  package org.apache.jasper34.tomcat33;
  
  import java.util.*;
  import java.io.*;
  import java.net.*;
  
  import org.apache.jasper34.core.*;
  import org.apache.jasper34.generator.*;
  
  // utils - can be moved here if needed
  import org.apache.tomcat.util.JavaGeneratorTool;
  import org.apache.tomcat.util.io.FileUtil;
  
  
  /** Mangler implementation - use the directory of the jsp file as a package
      name, minimize "special" encoding - in general, simpler and predictible
      names for the common case.
  
      This file is also using a special mechanism for the "versioned" classes
      ( based on Anil's idea of generating new class each time the jsp file
      changes - without a context restart that looses data ).
  
      We use an additional file per jsp saving the current version - at
      startup the file will be read to avoid recompilation. That removes the
      need for a "special" class loader and the hacks in reading internal
      class info.
  */
  public class JasperMangler implements Mangler{
  
      public JasperMangler(String workDir, String docBase, String jspFile)
      {
  	this.jspFile=jspFile;
  	this.workDir=workDir;
  	this.docBase=docBase;
  	init();
      }
  
      /** Versioned class name ( without package ).
       */
      public String getClassName() {
  	return JavaGeneratorTool.getVersionedName( baseClassN, version );
      }
      
      /**
       *   Full path to the generated java file ( including version )
       */
      public String getJavaFileName() {
  	return javaFileName;
      }
  
      /** The package name ( "." separated ) of the generated
       *  java file
       */
      public String getPackageName() {
  	if( pkgDir!=null ) {
  	    return pkgDir.replace('/', '.');
  	} else {
  	    return null;
  	}
      }
  
      /** Full path to the compiled class file ( including version )
       */
      public String getClassFileName() {
  	return classFileName;
      }
  
      // -------------------- JspInterceptor fields --------------------
      
      /** Returns the jsp file, as declared by <jsp-file> in server.xml
       *  or the context-relative path that was extension mapped to jsp
       */
      public String getJspFile() {
  	return jspFile;
      }
  
      /** Returns the directory where the class is located, using
       *  the normal class loader rules.
       */
      public String getClassDir() {
  	return classDir;
      }
      
      /** The class name ( package + class + versioning ) of the
       *  compilation result
       */
      public String getServletClassName() {
  	if( pkgDir!=null ) {
  	    return getPackageName()  + "." + getClassName();
  	} else {
  	    return getClassName();
  	}
      }
  
      public int getVersion() {
  	return version;
      }
  
      // In Jasper = not used - it's specific to the class scheme
      // used by JspServlet
      // Full path to the class file - without version.
      
  
      public String getBaseClassName() {
  	return baseClassN;
      }
  
      public String getPackageDir() {
  	return pkgDir;
      }
      
      public String getJspFilePath() {
  	return FileUtil.safePath( docBase, jspFile);
      }
  
      private String fixInvalidChars(String className) {
  	// Fix for invalid characters. From CommandLineCompiler
  	StringBuffer modifiedClassName = new StringBuffer();
  	char c='/';
  	for (int i = 0; i < className.length(); i++) {
  	    char prev=c;
  	    c=className.charAt(i);
  	    // workaround for common "//" problem. Alternative
  	    // would be to encode the dot.
  	    if( prev=='/' && c=='/' ) {
  		continue;
  	    }
  	    
  	    if (Character.isLetterOrDigit(c) == true ||
  		c=='_' ||
  		c=='/' )
  		modifiedClassName.append(className.substring(i,i+1));
  	    else
  		modifiedClassName.append(mangleChar(className.charAt(i)));
  	}
  	return modifiedClassName.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);
      }
  
  
      
      /** compute basic names - pkgDir and baseClassN
       */
      private void init() {
  	int lastComp=jspFile.lastIndexOf(  "/" );
  
  	if( lastComp > 0 ) {
  	    // has package 
  	    // ignore the first "/" of jspFile
  	    pkgDir=jspFile.substring( 1, lastComp );
  	}
  	
  	// remove "special" words, replace "."
  	if( pkgDir!=null ) {
  	    pkgDir=JavaGeneratorTool.manglePackage(pkgDir);
  	    pkgDir=pkgDir.replace('.', '_');
  	    pkgDir=fixInvalidChars( pkgDir );
  	    if ( "/".equals(File.separator) )
  		classDir=workDir + File.separator + pkgDir;
              else
  		classDir=workDir + File.separator +
                  	pkgDir.replace('/',File.separatorChar);
  	} else {
  	    classDir=workDir;
  	}
  	
  	int extIdx=jspFile.lastIndexOf( "." );
  
  	if( extIdx<0 ) {
  	    // no "." 
  	    if( lastComp > 0 )
  		baseClassN=jspFile.substring( lastComp+1 );
  	    else
  		baseClassN=jspFile.substring( 1 );
  	} else {
  	    if( lastComp > 0 )
  		baseClassN=jspFile.substring( lastComp+1, extIdx );
  	    else
  		baseClassN=jspFile.substring( 1, extIdx );
  	}
  
  	baseClassN=fixInvalidChars( baseClassN );
  	
  	//	System.out.println("XXXMangler: " + jspFile + " " +
  	// pkgDir + " " + baseClassN);
  
  	// extract version from the .class dir, using the base name
  	version=JavaGeneratorTool.readVersion(classDir,
  					      baseClassN);
  	if( version==-1 ) {
  	    version=0;
  	} 
  	updateVersionPaths();
      }
  
      private void updateVersionPaths() {
  	// version dependent stuff
  	String baseName=classDir + File.separator + JavaGeneratorTool.
  	    getVersionedName( baseClassN, version);
  	
  	javaFileName= baseName + ".java";
  
  	classFileName=baseName +  ".class";
      }
      
      /** Move to a new class name, if a changes has been detected.
       */
      public void nextVersion() {
  	version++;
  	JavaGeneratorTool.writeVersion( getClassDir(), baseClassN, version);
  	updateVersionPaths();
      }
  
      // context-relative jsp path 
      // extracted from the <jsp-file> or the result of a *.jsp mapping
      private String jspFile; 
      // version of the compiled java file
      private int version;
      private String workDir;
      private String docBase;
      // the "/" separted version
      private String pkgDir;
      // class name without package and version
      private String baseClassN;
      private String classDir;
      private String javaFileName;
      private String classFileName;
  }