source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/taskdefs/optional/jsp/JspNameMangler.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 4.9 KB
Line 
1/*
2 * Copyright 2001-2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17package org.apache.tools.ant.taskdefs.optional.jsp;
18import java.io.File;
19
20/**
21 * This is a class derived from the Jasper code
22 * (org.apache.jasper.compiler.CommandLineCompiler) to map from a JSP filename
23 * to a valid Java classname.
24 *
25 */
26public class JspNameMangler implements JspMangler {
27
28 /**
29 * this is the list of keywords which can not be used as classnames
30 */
31 public static final String[] keywords = {
32 "assert",
33 "abstract", "boolean", "break", "byte",
34 "case", "catch", "char", "class",
35 "const", "continue", "default", "do",
36 "double", "else", "extends", "final",
37 "finally", "float", "for", "goto",
38 "if", "implements", "import",
39 "instanceof", "int", "interface",
40 "long", "native", "new", "package",
41 "private", "protected", "public",
42 "return", "short", "static", "super",
43 "switch", "synchronized", "this",
44 "throw", "throws", "transient",
45 "try", "void", "volatile", "while"
46 };
47
48
49 /**
50 * map from a jsp file to a java filename; does not do packages
51 *
52 * @param jspFile file
53 * @return java filename
54 */
55 public String mapJspToJavaName(File jspFile) {
56 return mapJspToBaseName(jspFile) + ".java";
57 }
58
59
60 /**
61 * map from a jsp file to a base name; does not deal with extensions
62 *
63 * @param jspFile jspFile file
64 * @return exensionless potentially remapped name
65 */
66 private String mapJspToBaseName(File jspFile) {
67 String className;
68 className = stripExtension(jspFile);
69
70 // since we don't mangle extensions like the servlet does,
71 // we need to check for keywords as class names
72 for (int i = 0; i < keywords.length; ++i) {
73 if (className.equals(keywords[i])) {
74 className += "%";
75 break;
76 }
77 }
78
79 // Fix for invalid characters. If you think of more add to the list.
80 StringBuffer modifiedClassName = new StringBuffer(className.length());
81 // first char is more restrictive than the rest
82 char firstChar = className.charAt(0);
83 if (Character.isJavaIdentifierStart(firstChar)) {
84 modifiedClassName.append(firstChar);
85 } else {
86 modifiedClassName.append(mangleChar(firstChar));
87 }
88 // this is the rest
89 for (int i = 1; i < className.length(); i++) {
90 char subChar = className.charAt(i);
91 if (Character.isJavaIdentifierPart(subChar)) {
92 modifiedClassName.append(subChar);
93 } else {
94 modifiedClassName.append(mangleChar(subChar));
95 }
96 }
97 return modifiedClassName.toString();
98 }
99
100
101 /**
102 * get short filename from file
103 *
104 * @param jspFile file in
105 * @return file without any jsp extension
106 */
107 private String stripExtension(File jspFile) {
108 String className;
109 String filename = jspFile.getName();
110 if (filename.endsWith(".jsp")) {
111 className = filename.substring(0, filename.length() - 4);
112 } else {
113 className = filename;
114 }
115 return className;
116 }
117
118
119 /**
120 * definition of the char escaping algorithm
121 *
122 * @param ch char to mangle
123 * @return mangled string; 5 digit hex value
124 */
125 private static final String mangleChar(char ch) {
126
127 if (ch == File.separatorChar) {
128 ch = '/';
129 }
130 String s = Integer.toHexString(ch);
131 int nzeros = 5 - s.length();
132 char[] result = new char[6];
133 result[0] = '_';
134 for (int i = 1; i <= nzeros; ++i) {
135 result[i] = '0';
136 }
137 int resultIndex = 0;
138 for (int i = nzeros + 1; i < 6; ++i) {
139 result[i] = s.charAt(resultIndex++);
140 }
141 return new String(result);
142 }
143
144 /**
145 * taking in the substring representing the path relative to the source dir
146 * return a new string representing the destination path
147 * not supported, as jasper in tomcat4.0 doesnt either
148 */
149 public String mapPath(String path) {
150 return null;
151 }
152}
153
Note: See TracBrowser for help on using the repository browser.