01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: */
18:
19: package org.apache.tools.ant.taskdefs.optional.jsp;
20:
21: import java.io.File;
22:
23: /**
24: * this class implements the name mangling rules of the jasper in tomcat4.1.x
25: * which is likely to remain for some time
26: * @see "org.apache.jasper.JspCompilationContext"
27: */
28: public class Jasper41Mangler implements JspMangler {
29:
30: /**
31: * map from a jsp file to a java filename; does not do packages
32: *
33: * @param jspFile file
34: * @return java filename
35: */
36: public String mapJspToJavaName(File jspFile) {
37: String jspUri = jspFile.getAbsolutePath();
38: int start = jspUri.lastIndexOf(File.separatorChar) + 1;
39: int end = jspUri.length();
40: StringBuffer modifiedClassName;
41: modifiedClassName = new StringBuffer(jspUri.length() - start);
42: if (!Character.isJavaIdentifierStart(jspUri.charAt(start))
43: || jspUri.charAt(start) == '_') {
44: // If the first char is not a start of Java identifier or is _
45: // prepend a '_'.
46: modifiedClassName.append('_');
47: }
48: for (int i = start; i < end; i++) {
49: char ch = jspUri.charAt(i);
50: if (Character.isJavaIdentifierPart(ch)) {
51: modifiedClassName.append(ch);
52: } else if (ch == '.') {
53: modifiedClassName.append('_');
54: } else {
55: modifiedClassName.append(mangleChar(ch));
56: }
57: }
58: return modifiedClassName.toString();
59: }
60:
61: /**
62: * Mangle the specified character to create a legal Java class name.
63: */
64: private static String mangleChar(char ch) {
65:
66: String s = Integer.toHexString(ch);
67: int nzeros = 5 - s.length();
68: char[] result = new char[6];
69: result[0] = '_';
70: for (int i = 1; i <= nzeros; i++) {
71: result[i] = '0';
72: }
73: for (int i = nzeros + 1, j = 0; i < 6; i++, j++) {
74: result[i] = s.charAt(j);
75: }
76: return new String(result);
77: }
78:
79: /**
80: * taking in the substring representing the path relative to the source dir
81: * return a new string representing the destination path
82: * @param path not used.
83: * @return null as this is not implemented.
84: * @todo
85: */
86: public String mapPath(String path) {
87: return null;
88: }
89:
90: }
|