001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.j2ee.jboss4.ide;
043:
044: import java.util.Arrays;
045: import java.util.Vector;
046:
047: /** Various utilities copied over from org.apache.jasper.JspUtil.
048: */
049: public class JspNameUtil {
050:
051: private static final String JAVA_KEYWORDS[] = { "abstract",
052: "boolean", "break", "byte", "case", "catch", "char",
053: "class", "const", "continue", "default", "do", "double",
054: "else", "extends", "final", "finally", "float", "for",
055: "goto", "if", "implements", "import", "instanceof", "int",
056: "interface", "long", "native", "new", "package", "private",
057: "protected", "public", "return", "short", "static",
058: "strictfp", "super", "switch", "synchronized", "this",
059: "throws", "transient", "try", "void", "volatile", "while" };
060:
061: /**
062: * The default package name for compiled jsp pages.
063: */
064: public static final String JSP_PACKAGE_NAME = "org.apache.jsp";
065:
066: /**
067: * Converts the given path to a Java package or fully-qualified class name
068: *
069: * @param path Path to convert
070: *
071: * @return Java package corresponding to the given path
072: */
073: public static final String makeJavaPackage(String path) {
074: String classNameComponents[] = split(path, "/");
075: StringBuffer legalClassNames = new StringBuffer();
076: for (int i = 0; i < classNameComponents.length; i++) {
077: legalClassNames
078: .append(makeJavaIdentifier(classNameComponents[i]));
079: if (i < classNameComponents.length - 1) {
080: legalClassNames.append('.');
081: }
082: }
083: return legalClassNames.toString();
084: }
085:
086: /**
087: * Splits a string into it's components.
088: * @param path String to split
089: * @param pat Pattern to split at
090: * @return the components of the path
091: */
092: private static final String[] split(String path, String pat) {
093: Vector comps = new Vector();
094: int pos = path.indexOf(pat);
095: int start = 0;
096: while (pos >= 0) {
097: if (pos > start) {
098: String comp = path.substring(start, pos);
099: comps.add(comp);
100: }
101: start = pos + pat.length();
102: pos = path.indexOf(pat, start);
103: }
104: if (start < path.length()) {
105: comps.add(path.substring(start));
106: }
107: String[] result = new String[comps.size()];
108: for (int i = 0; i < comps.size(); i++) {
109: result[i] = (String) comps.elementAt(i);
110: }
111: return result;
112: }
113:
114: /**
115: * Converts the given identifier to a legal Java identifier
116: *
117: * @param identifier Identifier to convert
118: *
119: * @return Legal Java identifier corresponding to the given identifier
120: */
121: public static final String makeJavaIdentifier(String identifier) {
122: StringBuffer modifiedIdentifier = new StringBuffer(identifier
123: .length());
124: if (!Character.isJavaIdentifierStart(identifier.charAt(0))) {
125: modifiedIdentifier.append('_');
126: }
127: for (int i = 0; i < identifier.length(); i++) {
128: char ch = identifier.charAt(i);
129: if (Character.isJavaIdentifierPart(ch) && ch != '_') {
130: modifiedIdentifier.append(ch);
131: } else if (ch == '.') {
132: modifiedIdentifier.append('_');
133: } else {
134: modifiedIdentifier.append(mangleChar(ch));
135: }
136: }
137: if (isJavaKeyword(modifiedIdentifier.toString())) {
138: modifiedIdentifier.append('_');
139: }
140: return modifiedIdentifier.toString();
141: }
142:
143: /**
144: * Mangle the specified character to create a legal Java class name.
145: */
146: public static final String mangleChar(char ch) {
147: char[] result = new char[5];
148: result[0] = '_';
149: result[1] = Character.forDigit((ch >> 12) & 0xf, 16);
150: result[2] = Character.forDigit((ch >> 8) & 0xf, 16);
151: result[3] = Character.forDigit((ch >> 4) & 0xf, 16);
152: result[4] = Character.forDigit(ch & 0xf, 16);
153: return new String(result);
154: }
155:
156: /**
157: * Test whether the argument is a Java keyword
158: */
159: public static boolean isJavaKeyword(String key) {
160: return Arrays.binarySearch(JAVA_KEYWORDS, key) >= 0;
161: }
162:
163: }
|