KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > jsp > JspNameMangler


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * 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 package org.apache.tools.ant.taskdefs.optional.jsp;
19 import java.io.File JavaDoc;
20
21 /**
22  * This is a class derived from the Jasper code
23  * (org.apache.jasper.compiler.CommandLineCompiler) to map from a JSP filename
24  * to a valid Java classname.
25  *
26  */

27 public class JspNameMangler implements JspMangler {
28
29     // CheckStyle:ConstantNameCheck OFF - bc
30

31     /**
32      * this is the list of keywords which can not be used as classnames
33      */

34     public static final String JavaDoc[] keywords = {
35             "assert",
36             "abstract", "boolean", "break", "byte",
37             "case", "catch", "char", "class",
38             "const", "continue", "default", "do",
39             "double", "else", "extends", "final",
40             "finally", "float", "for", "goto",
41             "if", "implements", "import",
42             "instanceof", "int", "interface",
43             "long", "native", "new", "package",
44             "private", "protected", "public",
45             "return", "short", "static", "super",
46             "switch", "synchronized", "this",
47             "throw", "throws", "transient",
48             "try", "void", "volatile", "while"
49             };
50
51     // CheckStyle:ConstantNameCheck ON
52

53     /**
54      * map from a jsp file to a java filename; does not do packages
55      *
56      * @param jspFile file
57      * @return java filename
58      */

59     public String JavaDoc mapJspToJavaName(File JavaDoc jspFile) {
60         return mapJspToBaseName(jspFile) + ".java";
61     }
62
63
64     /**
65      * map from a jsp file to a base name; does not deal with extensions
66      *
67      * @param jspFile jspFile file
68      * @return exensionless potentially remapped name
69      */

70     private String JavaDoc mapJspToBaseName(File JavaDoc jspFile) {
71         String JavaDoc className;
72         className = stripExtension(jspFile);
73
74         // since we don't mangle extensions like the servlet does,
75
// we need to check for keywords as class names
76
for (int i = 0; i < keywords.length; ++i) {
77             if (className.equals(keywords[i])) {
78                 className += "%";
79                 break;
80             }
81         }
82
83         // Fix for invalid characters. If you think of more add to the list.
84
StringBuffer JavaDoc modifiedClassName = new StringBuffer JavaDoc(className.length());
85         // first char is more restrictive than the rest
86
char firstChar = className.charAt(0);
87         if (Character.isJavaIdentifierStart(firstChar)) {
88             modifiedClassName.append(firstChar);
89         } else {
90             modifiedClassName.append(mangleChar(firstChar));
91         }
92         // this is the rest
93
for (int i = 1; i < className.length(); i++) {
94             char subChar = className.charAt(i);
95             if (Character.isJavaIdentifierPart(subChar)) {
96                 modifiedClassName.append(subChar);
97             } else {
98                 modifiedClassName.append(mangleChar(subChar));
99             }
100         }
101         return modifiedClassName.toString();
102     }
103
104
105     /**
106      * get short filename from file
107      *
108      * @param jspFile file in
109      * @return file without any jsp extension
110      */

111     private String JavaDoc stripExtension(File JavaDoc jspFile) {
112         String JavaDoc className;
113         String JavaDoc filename = jspFile.getName();
114         if (filename.endsWith(".jsp")) {
115             className = filename.substring(0, filename.length() - 4);
116         } else {
117             className = filename;
118         }
119         return className;
120     }
121
122
123     /**
124      * definition of the char escaping algorithm
125      *
126      * @param ch char to mangle
127      * @return mangled string; 5 digit hex value
128      */

129     private static String JavaDoc mangleChar(char ch) {
130
131         if (ch == File.separatorChar) {
132             ch = '/';
133         }
134         String JavaDoc s = Integer.toHexString(ch);
135         int nzeros = 5 - s.length();
136         char[] result = new char[6];
137         result[0] = '_';
138         for (int i = 1; i <= nzeros; ++i) {
139             result[i] = '0';
140         }
141         int resultIndex = 0;
142         for (int i = nzeros + 1; i < 6; ++i) {
143             result[i] = s.charAt(resultIndex++);
144         }
145         return new String JavaDoc(result);
146     }
147
148     /**
149      * taking in the substring representing the path relative to the source dir
150      * return a new string representing the destination path
151      * not supported, as jasper in tomcat4.0 doesnt either
152      * @param path not used
153      * @return null always.
154      */

155     public String JavaDoc mapPath(String JavaDoc path) {
156         return null;
157     }
158 }
159
160
Popular Tags