1 19 20 package org.netbeans.modules.j2ee.jboss4.ide; 21 22 import java.util.Vector ; 23 24 26 public class JspNameUtil { 27 28 private static final String 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 43 public static final String JSP_PACKAGE_NAME = "org.apache.jsp"; 44 45 46 53 public static final String makeJavaPackage(String path) { 54 String classNameComponents[] = split(path,"/"); 55 StringBuffer legalClassNames = new StringBuffer (); 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 71 private static final String [] split(String path, String pat) { 72 Vector comps = new Vector (); 73 int pos = path.indexOf(pat); 74 int start = 0; 75 while( pos >= 0 ) { 76 if(pos > start ) { 77 String 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 [] result = new String [comps.size()]; 87 for(int i=0; i < comps.size(); i++) { 88 result[i] = (String )comps.elementAt(i); 89 } 90 return result; 91 } 92 93 100 public static final String makeJavaIdentifier(String identifier) { 101 StringBuffer modifiedIdentifier = 102 new StringBuffer (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 125 public static final String 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 (result); 133 } 134 135 138 public static boolean isJavaKeyword(String 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 |