001: /*
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 1999 The Apache Software Foundation. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution, if
020: * any, must include the following acknowlegement:
021: * "This product includes software developed by the
022: * Apache Software Foundation (http://www.apache.org/)."
023: * Alternately, this acknowlegement may appear in the software itself,
024: * if and wherever such third-party acknowlegements normally appear.
025: *
026: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
027: * Foundation" must not be used to endorse or promote products derived
028: * from this software without prior written permission. For written
029: * permission, please contact apache@apache.org.
030: *
031: * 5. Products derived from this software may not be called "Apache"
032: * nor may "Apache" appear in their names without prior written
033: * permission of the Apache Group.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: *
049: * This software consists of voluntary contributions made by many
050: * individuals on behalf of the Apache Software Foundation. For more
051: * information on the Apache Software Foundation, please see
052: * <http://www.apache.org/>.
053: *
054: */
055:
056: package com.sun.portal.providers.jsp.jasper3.jasper.compiler;
057:
058: import java.io.File;
059:
060: import com.sun.portal.providers.jsp.jasper3.jasper.Constants;
061: import com.sun.portal.providers.jsp.jasper3.jasper.JasperException;
062:
063: /**
064: * JspMangler is an implementation of Mangler with a funky code
065: * mangling and code generation scheme!
066: */
067: public class JspMangler implements Mangler {
068:
069: String pkgName, className, javaFileName, classFileName;
070: String prefix;
071:
072: File jsp;
073: String outputDir;
074:
075: public JspMangler(String jspFile, String outputDir) {
076: this .jsp = new File(jspFile);
077: this .outputDir = outputDir;
078: }
079:
080: public final String getClassName() {
081: if (className != null)
082: return className;
083:
084: if (jsp.getName().endsWith(".jsp"))
085: className = jsp.getName().substring(0,
086: jsp.getName().length() - 4);
087: else
088: className = jsp.getName();
089:
090: // Fix for invalid characters. If you think of more add to the list.
091: StringBuffer modifiedClassName = new StringBuffer();
092:
093: className = "_"
094: + mangleString(className, (className.length() - 1),
095: modifiedClassName) + Constants.JSP_TOKEN;
096:
097: return className;
098: }
099:
100: // generated .java file name will be in the same directory as the
101: // the class file.
102: public final String getJavaFileName() {
103: if (javaFileName != null)
104: return javaFileName;
105:
106: String prefix = getPrefix(jsp.getPath());
107: javaFileName = prefix + File.separatorChar + getClassName()
108: + ".java";
109: if (outputDir != null && !outputDir.equals(""))
110: javaFileName = outputDir + File.separatorChar
111: + javaFileName;
112: return javaFileName;
113: }
114:
115: public final String getClassFileName() {
116: if (classFileName != null)
117: return classFileName;
118:
119: String prefix = getPrefix(jsp.getPath());
120: classFileName = prefix + File.separatorChar + getClassName()
121: + ".class";
122: if (outputDir != null && !outputDir.equals(""))
123: classFileName = outputDir + File.separatorChar
124: + classFileName;
125: return classFileName;
126: }
127:
128: // Package namges will have: "package _jsps._foo._bar;" where
129: // the given JSP is /foo/bar/my.jsp
130: public final String getPackageName() {
131: if (pkgName != null)
132: return pkgName;
133:
134: // compute package name
135: String pathName = jsp.getPath();
136: String pkgName = getPrefix(pathName);
137: pkgName = pkgName.replace(File.separatorChar, '.');
138: return pkgName;
139: }
140:
141: public final void setPrefix(String pfx) {
142: if (pfx != null)
143: prefix = pfx;
144: }
145:
146: private final String getPrefix(String pathName) {
147: if (prefix != null)
148: return prefix;
149:
150: if (pathName == null)
151: return Constants.JSP_DIR;
152:
153: int maxIndex = pathName.lastIndexOf(File.separatorChar);
154: if (maxIndex == -1 || maxIndex == 0)
155: return Constants.JSP_DIR;
156:
157: StringBuffer modifiedName = new StringBuffer();
158: modifiedName.append(Constants.JSP_DIR);
159:
160: // pathName is not null and atleast there is one segment in the path
161: // Get the prefix upto the last segment of the path string,
162: // i.e. skip the file name portion. Also, exclude trailing file separator
163: return mangleString(pathName, (maxIndex - 1), modifiedName);
164: }
165:
166: // Common path mangling code
167: private final String mangleString(String pathName, int maxIndex,
168: StringBuffer sb) {
169: for (int i = 0; i <= maxIndex; i++) {
170: if (Character.isLetterOrDigit(pathName.charAt(i)) == true
171: || pathName.charAt(i) == '_')
172: sb.append(pathName.substring(i, i + 1));
173: else if (pathName.charAt(i) == File.separatorChar) {
174: sb.append(File.separatorChar);
175: if (i != maxIndex)
176: sb.append("_");
177: } else if (pathName.charAt(i) == '-'
178: || pathName.charAt(i) == '.')
179: sb.append("_");
180: else
181: sb.append(mangleChar(pathName.charAt(i)));
182: }
183: return sb.toString();
184: }
185:
186: private static final String mangleChar(char ch) {
187: if (ch == File.separatorChar)
188: ch = '/';
189: String s = Integer.toHexString(ch);
190: int nzeros = 5 - s.length();
191: char[] result = new char[6];
192: result[0] = '_';
193: for (int i = 1; i <= nzeros; i++)
194: result[i] = '0';
195: for (int i = nzeros + 1, j = 0; i < 6; i++, j++)
196: result[i] = s.charAt(j);
197: return new String(result);
198: }
199: }
|