KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > debug > util > 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.debug.util;
21
22 import java.util.Vector JavaDoc;
23
24 /** Various utilities copied over from org.apache.jasper.JspUtil.
25  */

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

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

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

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

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

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

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