KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > jboss4 > ide > 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.j2ee.jboss4.ide;
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     public static final String JavaDoc JSP_PACKAGE_NAME = "org.apache.jsp";
44         
45     
46     /**
47      * Converts the given path to a Java package or fully-qualified class name
48      *
49      * @param path Path to convert
50      *
51      * @return Java package corresponding to the given path
52      */

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

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

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

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

138     public static boolean isJavaKeyword(String JavaDoc key) {
139         int i = 0;
140         int j = javaKeywords.length;
141         while (i < j) {
142             int k = (i+j)/2;
143             int result = javaKeywords[k].compareTo(key);
144             if (result == 0) {
145                 return true;
146             }
147             if (result < 0) {
148                 i = k+1;
149             } else {
150                 j = k;
151             }
152         }
153         return false;
154     }
155     
156 }
157
Popular Tags