costin 01/06/12 08:33:21 Added: jasper34/liaison/org/apache/jasper34/liaison Mangler33.java ManglerCli.java Log: 2 Manglers to choose from. One was used in JspC, one in JspInterceptor. Revision Changes Path 1.1 jakarta-tomcat-jasper/jasper34/liaison/org/apache/jasper34/liaison/Mangler33.java Index: Mangler33.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 [EMAIL PROTECTED] * * 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.util.*; import java.io.*; import java.net.*; import org.apache.jasper34.core.*; import org.apache.jasper34.core.Mangler; 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 Mangler33 extends Mangler { public Mangler33(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; } 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; } 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; } 1.1 jakarta-tomcat-jasper/jasper34/liaison/org/apache/jasper34/liaison/ManglerCli.java Index: ManglerCli.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 [EMAIL PROTECTED] * * 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.*; import org.apache.jasper34.core.*; import org.apache.jasper34.runtime.*; import org.apache.jasper34.generator.*; import org.apache.jasper34.jsptree.*; import org.apache.jasper34.core.Compiler; /** * Overrides some methods so that we get the desired effects. * @author Danno Ferrin */ public class ManglerCli extends Mangler { String javaFileName; String classFileName; String pkgName; String className; File jsp; String outputDir; public ManglerCli() { } // pageInfo.getJspFile() // outputDir = pageInfo.getOptions().getScratchDir().getAbsolutePath(); // ctxt.isOutputInDirs() public void init(String jspFile, String outputDir, boolean isOutputInDirs) { jsp = new File(jspFile); this.outputDir=outputDir; computePackageName(); className = getBaseClassName(); // yes this is kind of messed up ... but it works if (isOutputInDirs) { // String pkgName = pageInfo.getServletPackageName(); if (pkgName == null) { pkgName = ""; } String tmpDir = outputDir + File.separatorChar + pkgName.replace('.', File.separatorChar); File f = new File(tmpDir); if (!f.exists()) { if (f.mkdirs()) { outputDir = tmpDir; } } else { outputDir = tmpDir; } } computeClassFileName(); computeJavaFileName(); } public final void computeJavaFileName() { // javaFileName = pageInfo.getServletClassName() + ".java"; // if ("null.java".equals(javaFileName)) { javaFileName = getBaseClassName() + ".java"; // }; if (outputDir != null && !outputDir.equals("")) javaFileName = outputDir + File.separatorChar + javaFileName; } void computeClassFileName() { String prefix = getPrefix(jsp.getPath()); classFileName = prefix + getBaseClassName() + ".class"; if (outputDir != null && !outputDir.equals("")) classFileName = outputDir + File.separatorChar + 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" }; void computePackageName() { String pathName = jsp.getPath(); StringBuffer modifiedpkgName = new StringBuffer (); int indexOfSepChar = pathName.lastIndexOf(File.separatorChar); if (("".equals(pkgName)) || (indexOfSepChar == -1) || (indexOfSepChar == 0)) { pkgName = null; } else { for (int i = 0; i < keywords.length; i++) { char fs = File.separatorChar; 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] + fs); } } // 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, '.'); // if (pkgName != null) { // pkgName = pageInfo.getServletPackageName(); // } 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(); } } private final String getInitialClassName() { String prefix = getPrefix(jsp.getPath()); return prefix + getBaseClassName() + Constants.JSP_TOKEN + "0"; } private final String getBaseClassName() { String className = null; //pageInfo.getServletClassName(); if (className == null) { if (jsp.getName().endsWith(".jsp")) className = jsp.getName().substring(0, jsp.getName().length() - 4); 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(); 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); } public final String getPackageName() { return pkgName; } public final String getClassName() { return className; } public final String getJavaFileName() { return javaFileName; } public final String getClassFileName() { return classFileName; } }