001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.taskdefs.optional.jsp;
019:
020: import java.io.File;
021:
022: /**
023: * This is a class derived from the Jasper code
024: * (org.apache.jasper.compiler.CommandLineCompiler) to map from a JSP filename
025: * to a valid Java classname.
026: *
027: */
028: public class JspNameMangler implements JspMangler {
029:
030: // CheckStyle:ConstantNameCheck OFF - bc
031:
032: /**
033: * this is the list of keywords which can not be used as classnames
034: */
035: public static final String[] keywords = { "assert", "abstract",
036: "boolean", "break", "byte", "case", "catch", "char",
037: "class", "const", "continue", "default", "do", "double",
038: "else", "extends", "final", "finally", "float", "for",
039: "goto", "if", "implements", "import", "instanceof", "int",
040: "interface", "long", "native", "new", "package", "private",
041: "protected", "public", "return", "short", "static",
042: "super", "switch", "synchronized", "this", "throw",
043: "throws", "transient", "try", "void", "volatile", "while" };
044:
045: // CheckStyle:ConstantNameCheck ON
046:
047: /**
048: * map from a jsp file to a java filename; does not do packages
049: *
050: * @param jspFile file
051: * @return java filename
052: */
053: public String mapJspToJavaName(File jspFile) {
054: return mapJspToBaseName(jspFile) + ".java";
055: }
056:
057: /**
058: * map from a jsp file to a base name; does not deal with extensions
059: *
060: * @param jspFile jspFile file
061: * @return exensionless potentially remapped name
062: */
063: private String mapJspToBaseName(File jspFile) {
064: String className;
065: className = stripExtension(jspFile);
066:
067: // since we don't mangle extensions like the servlet does,
068: // we need to check for keywords as class names
069: for (int i = 0; i < keywords.length; ++i) {
070: if (className.equals(keywords[i])) {
071: className += "%";
072: break;
073: }
074: }
075:
076: // Fix for invalid characters. If you think of more add to the list.
077: StringBuffer modifiedClassName = new StringBuffer(className
078: .length());
079: // first char is more restrictive than the rest
080: char firstChar = className.charAt(0);
081: if (Character.isJavaIdentifierStart(firstChar)) {
082: modifiedClassName.append(firstChar);
083: } else {
084: modifiedClassName.append(mangleChar(firstChar));
085: }
086: // this is the rest
087: for (int i = 1; i < className.length(); i++) {
088: char subChar = className.charAt(i);
089: if (Character.isJavaIdentifierPart(subChar)) {
090: modifiedClassName.append(subChar);
091: } else {
092: modifiedClassName.append(mangleChar(subChar));
093: }
094: }
095: return modifiedClassName.toString();
096: }
097:
098: /**
099: * get short filename from file
100: *
101: * @param jspFile file in
102: * @return file without any jsp extension
103: */
104: private String stripExtension(File jspFile) {
105: String className;
106: String filename = jspFile.getName();
107: if (filename.endsWith(".jsp")) {
108: className = filename.substring(0, filename.length() - 4);
109: } else {
110: className = filename;
111: }
112: return className;
113: }
114:
115: /**
116: * definition of the char escaping algorithm
117: *
118: * @param ch char to mangle
119: * @return mangled string; 5 digit hex value
120: */
121: private static String mangleChar(char ch) {
122:
123: if (ch == File.separatorChar) {
124: ch = '/';
125: }
126: String s = Integer.toHexString(ch);
127: int nzeros = 5 - s.length();
128: char[] result = new char[6];
129: result[0] = '_';
130: for (int i = 1; i <= nzeros; ++i) {
131: result[i] = '0';
132: }
133: int resultIndex = 0;
134: for (int i = nzeros + 1; i < 6; ++i) {
135: result[i] = s.charAt(resultIndex++);
136: }
137: return new String(result);
138: }
139:
140: /**
141: * taking in the substring representing the path relative to the source dir
142: * return a new string representing the destination path
143: * not supported, as jasper in tomcat4.0 doesnt either
144: * @param path not used
145: * @return null always.
146: */
147: public String mapPath(String path) {
148: return null;
149: }
150: }
|