001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Sam
028: */
029:
030: package com.caucho.netbeans.util;
031:
032: import java.util.Vector;
033:
034: public final class JspNameUtil {
035:
036: private static final String javaKeywords[] = { "abstract",
037: "boolean", "break", "byte", "case", "catch", "char",
038: "class", "const", "continue", "default", "do", "double",
039: "else", "extends", "final", "finally", "float", "for",
040: "goto", "if", "implements", "import", "instanceof", "int",
041: "interface", "long", "native", "new", "package", "private",
042: "protected", "public", "return", "short", "static",
043: "strictfp", "super", "switch", "synchronized", "this",
044: "throws", "transient", "try", "void", "volatile", "while" };
045:
046: // XXX: change
047:
048: /**
049: * The default package name for compiled jsp pages.
050: */
051: public static final String JSP_PACKAGE_NAME = "org.apache.jsp";
052:
053: /**
054: * Converts the given path to a Java package or fully-qualified class name
055: *
056: * @param path Path to convert
057: *
058: * @return Java package corresponding to the given path
059: */
060: public static final String makeJavaPackage(String path) {
061: String classNameComponents[] = split(path, "/");
062: StringBuffer legalClassNames = new StringBuffer();
063: for (int i = 0; i < classNameComponents.length; i++) {
064: legalClassNames
065: .append(makeJavaIdentifier(classNameComponents[i]));
066: if (i < classNameComponents.length - 1) {
067: legalClassNames.append('.');
068: }
069: }
070: return legalClassNames.toString();
071: }
072:
073: /**
074: * Splits a string into it's components.
075: *
076: * @param path String to split
077: * @param pat Pattern to split at
078: *
079: * @return the components of the path
080: */
081: private static final String[] split(String path, String pat) {
082: Vector comps = new Vector();
083: int pos = path.indexOf(pat);
084: int start = 0;
085: while (pos >= 0) {
086: if (pos > start) {
087: String comp = path.substring(start, pos);
088: comps.add(comp);
089: }
090: start = pos + pat.length();
091: pos = path.indexOf(pat, start);
092: }
093: if (start < path.length()) {
094: comps.add(path.substring(start));
095: }
096: String[] result = new String[comps.size()];
097: for (int i = 0; i < comps.size(); i++) {
098: result[i] = (String) comps.elementAt(i);
099: }
100: return result;
101: }
102:
103: /**
104: * Converts the given identifier to a legal Java identifier
105: *
106: * @param identifier Identifier to convert
107: *
108: * @return Legal Java identifier corresponding to the given identifier
109: */
110: public static final String makeJavaIdentifier(String identifier) {
111: StringBuffer modifiedIdentifier = new StringBuffer(identifier
112: .length());
113: if (!Character.isJavaIdentifierStart(identifier.charAt(0))) {
114: modifiedIdentifier.append('_');
115: }
116: for (int i = 0; i < identifier.length(); i++) {
117: char ch = identifier.charAt(i);
118: if (Character.isJavaIdentifierPart(ch) && ch != '_') {
119: modifiedIdentifier.append(ch);
120: } else if (ch == '.') {
121: modifiedIdentifier.append('_');
122: } else {
123: modifiedIdentifier.append(mangleChar(ch));
124: }
125: }
126: if (isJavaKeyword(modifiedIdentifier.toString())) {
127: modifiedIdentifier.append('_');
128: }
129: return modifiedIdentifier.toString();
130: }
131:
132: /**
133: * Mangle the specified character to create a legal Java class name.
134: */
135: public static final String mangleChar(char ch) {
136: char[] result = new char[5];
137: result[0] = '_';
138: result[1] = Character.forDigit((ch >> 12) & 0xf, 16);
139: result[2] = Character.forDigit((ch >> 8) & 0xf, 16);
140: result[3] = Character.forDigit((ch >> 4) & 0xf, 16);
141: result[4] = Character.forDigit(ch & 0xf, 16);
142: return new String(result);
143: }
144:
145: /**
146: * Test whether the argument is a Java keyword
147: */
148: public static boolean isJavaKeyword(String key) {
149: int i = 0;
150: int j = javaKeywords.length;
151: while (i < j) {
152: int k = (i + j) / 2;
153: int result = javaKeywords[k].compareTo(key);
154: if (result == 0) {
155: return true;
156: }
157: if (result < 0) {
158: i = k + 1;
159: } else {
160: j = k;
161: }
162: }
163: return false;
164: }
165:
166: }
|