1 19 20 package org.netbeans.modules.web.debug.util; 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 private static final String JSP_PACKAGE_NAME = "org.apache.jsp"; 44 45 52 private static final String makeJavaPackage(String path) { 53 String classNameComponents[] = split(path,"/"); 54 StringBuffer legalClassNames = new StringBuffer (); 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 70 private static final String [] split(String path, String pat) { 71 Vector comps = new Vector (); 72 int pos = path.indexOf(pat); 73 int start = 0; 74 while( pos >= 0 ) { 75 if(pos > start ) { 76 String 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 [] result = new String [comps.size()]; 86 for(int i=0; i < comps.size(); i++) { 87 result[i] = (String )comps.elementAt(i); 88 } 89 return result; 90 } 91 92 99 private static final String makeJavaIdentifier(String identifier) { 100 StringBuffer modifiedIdentifier = 101 new StringBuffer (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 124 private static final String 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 (result); 132 } 133 134 137 private static boolean isJavaKeyword(String 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 getServletResourcePath(String moduleContextPath, String jspResourcePath) { 156 return getServletPackageName(jspResourcePath).replace('.', '/') + '/' + 157 getServletClassName(jspResourcePath) + ".java"; 158 } 159 160 private static String getServletPackageName(String jspUri) { 161 String 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 getDerivedPackageName(String jspUri) { 169 int iSep = jspUri.lastIndexOf('/'); 170 return (iSep > 0) ? makeJavaPackage(jspUri.substring(0,iSep)) : ""; 171 } 172 173 private static String getServletClassName(String jspUri) { 174 int iSep = jspUri.lastIndexOf('/') + 1; 175 return makeJavaIdentifier(jspUri.substring(iSep)); 176 } 177 178 } 179 | Popular Tags |