KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > parser > JspNameUtil


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.web.project.parser;
21
22 import java.util.Vector JavaDoc;
23 import org.openide.filesystems.FileObject;
24 import org.openide.filesystems.FileUtil;
25
26 /** Various utilities copied over from org.apache.jasper.JspUtil.
27  */

28 public class JspNameUtil {
29
30     private static final String JavaDoc javaKeywords[] = {
31         "abstract", "boolean", "break", "byte", "case",
32         "catch", "char", "class", "const", "continue",
33         "default", "do", "double", "else", "extends",
34         "final", "finally", "float", "for", "goto",
35         "if", "implements", "import", "instanceof", "int",
36         "interface", "long", "native", "new", "package",
37         "private", "protected", "public", "return", "short",
38         "static", "strictfp", "super", "switch", "synchronized",
39         "this", "throws", "transient", "try", "void",
40         "volatile", "while" };
41         
42     /**
43      * The default package name for compiled jsp pages.
44      */

45     private static final String JavaDoc JSP_PACKAGE_NAME = "org.apache.jsp";
46     
47     /**
48      * Converts the given path to a Java package or fully-qualified class name
49      *
50      * @param path Path to convert
51      *
52      * @return Java package corresponding to the given path
53      */

54     private static final String JavaDoc makeJavaPackage(String JavaDoc path) {
55         String JavaDoc classNameComponents[] = split(path,"/");
56         StringBuffer JavaDoc legalClassNames = new StringBuffer JavaDoc();
57         for (int i = 0; i < classNameComponents.length; i++) {
58             legalClassNames.append(makeJavaIdentifier(classNameComponents[i]));
59             if (i < classNameComponents.length - 1) {
60                 legalClassNames.append('.');
61             }
62         }
63         return legalClassNames.toString();
64     }
65     
66     /**
67      * Splits a string into it's components.
68      * @param path String to split
69      * @param pat Pattern to split at
70      * @return the components of the path
71      */

72     private static final String JavaDoc [] split(String JavaDoc path, String JavaDoc pat) {
73         Vector JavaDoc comps = new Vector JavaDoc();
74         int pos = path.indexOf(pat);
75         int start = 0;
76         while( pos >= 0 ) {
77             if(pos > start ) {
78                 String JavaDoc comp = path.substring(start,pos);
79                 comps.add(comp);
80             }
81             start = pos + pat.length();
82             pos = path.indexOf(pat,start);
83         }
84         if( start < path.length()) {
85             comps.add(path.substring(start));
86         }
87         String JavaDoc [] result = new String JavaDoc[comps.size()];
88         for(int i=0; i < comps.size(); i++) {
89             result[i] = (String JavaDoc)comps.elementAt(i);
90         }
91         return result;
92     }
93     
94     /**
95      * Converts the given identifier to a legal Java identifier
96      *
97      * @param identifier Identifier to convert
98      *
99      * @return Legal Java identifier corresponding to the given identifier
100      */

101     private static final String JavaDoc makeJavaIdentifier(String JavaDoc identifier) {
102         StringBuffer JavaDoc modifiedIdentifier =
103         new StringBuffer JavaDoc(identifier.length());
104         if (!Character.isJavaIdentifierStart(identifier.charAt(0))) {
105             modifiedIdentifier.append('_');
106         }
107         for (int i = 0; i < identifier.length(); i++) {
108             char ch = identifier.charAt(i);
109             if (Character.isJavaIdentifierPart(ch) && ch != '_') {
110                 modifiedIdentifier.append(ch);
111             } else if (ch == '.') {
112                 modifiedIdentifier.append('_');
113             } else {
114                 modifiedIdentifier.append(mangleChar(ch));
115             }
116         }
117         if (isJavaKeyword(modifiedIdentifier.toString())) {
118             modifiedIdentifier.append('_');
119         }
120         return modifiedIdentifier.toString();
121     }
122     
123     /**
124      * Mangle the specified character to create a legal Java class name.
125      */

126     private static final String JavaDoc mangleChar(char ch) {
127         char[] result = new char[5];
128         result[0] = '_';
129         result[1] = Character.forDigit((ch >> 12) & 0xf, 16);
130         result[2] = Character.forDigit((ch >> 8) & 0xf, 16);
131         result[3] = Character.forDigit((ch >> 4) & 0xf, 16);
132         result[4] = Character.forDigit(ch & 0xf, 16);
133         return new String JavaDoc(result);
134     }
135     
136     /**
137      * Test whether the argument is a Java keyword
138      */

139     private static boolean isJavaKeyword(String JavaDoc key) {
140         int i = 0;
141         int j = javaKeywords.length;
142         while (i < j) {
143             int k = (i+j)/2;
144             int result = javaKeywords[k].compareTo(key);
145             if (result == 0) {
146                 return true;
147             }
148             if (result < 0) {
149                 i = k+1;
150             } else {
151                 j = k;
152             }
153         }
154         return false;
155     }
156
157     public static String JavaDoc getServletName(FileObject docBase, FileObject jsp) {
158         String JavaDoc jspRelativePath = FileUtil.getRelativePath(docBase, jsp);
159         return getServletResourcePath(null, jspRelativePath);
160     }
161     
162     public static String JavaDoc getServletResourcePath(String JavaDoc moduleContextPath, String JavaDoc jspResourcePath) {
163         return getServletPackageName(jspResourcePath).replace('.', '/') + '/' +
164             getServletClassName(jspResourcePath) + ".java";
165     }
166
167     private static String JavaDoc getServletPackageName(String JavaDoc jspUri) {
168         String JavaDoc dPackageName = getDerivedPackageName(jspUri);
169         if (dPackageName.length() == 0) {
170             return JSP_PACKAGE_NAME;
171         }
172         return JSP_PACKAGE_NAME + '.' + getDerivedPackageName(jspUri);
173     }
174     
175     private static String JavaDoc getDerivedPackageName(String JavaDoc jspUri) {
176         int iSep = jspUri.lastIndexOf('/');
177         return (iSep > 0) ? makeJavaPackage(jspUri.substring(0,iSep)) : "";
178     }
179     
180     private static String JavaDoc getServletClassName(String JavaDoc jspUri) {
181         int iSep = jspUri.lastIndexOf('/') + 1;
182         return makeJavaIdentifier(jspUri.substring(iSep));
183     }
184     
185 }
186
Popular Tags