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/06/12 17:35:13 UTC

cvs commit: jakarta-tomcat-jasper/jasper34/liaison/org/apache/jasper34/liaison ClassName.java ManglerOld.java

costin      01/06/12 08:35:13

  Added:       jasper34/liaison/org/apache/jasper34/liaison ClassName.java
                        ManglerOld.java
  Log:
  A third mangler to choose from - the "original" mangler using class hack.
  
  It's a cool idea, I'll try to preserve it as a general util, but very likely
  will not be used any more ( the JspLoader is too complex ).
  
  Revision  Changes    Path
  1.1                  jakarta-tomcat-jasper/jasper34/liaison/org/apache/jasper34/liaison/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.liaison;
  
  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.
   * 
   * Used by the old mangler
   *
   * @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(ContainerLiaison.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(ContainerLiaison.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(ContainerLiaison.getString("jsp.warning.bad.type"));
  	}
  	return (true);
      }
  }
  
  
  
  
  1.1                  jakarta-tomcat-jasper/jasper34/liaison/org/apache/jasper34/liaison/ManglerOld.java
  
  Index: ManglerOld.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.liaison;
  
  import java.io.File;
  import java.io.FileNotFoundException;
  
  import org.apache.jasper34.core.*;
  import org.apache.jasper34.generator.*;
  import org.apache.jasper34.jsptree.*;
  import org.apache.jasper34.core.Compiler;
  import org.apache.jasper34.runtime.JasperException;
  
  import org.apache.tomcat.util.log.*;
  
  /**
   * This is the original "funky code mangling and code generation scheme".
   *
   * @author Anil K. Vijendran
   */
  public class ManglerOld extends 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 ManglerOld()
      {
      }
  
      public void init( String jspFile, String outputDir ) {
  	this.jsp=new File( jspFile );
  	this.outputDir = outputDir;
  	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);
      }
  
  }