1 19 20 package org.netbeans.modules.web.project.parser; 21 22 import java.util.Vector ; 23 import org.openide.filesystems.FileObject; 24 import org.openide.filesystems.FileUtil; 25 26 28 public class JspNameUtil { 29 30 private static final String javaKeywords[] = { 31 "abstract", "boolean", "break", "byte", "case", 32 "catch", "char", "class", "const", "continue", 33 "default", "do", "double", "else", "extends", 34 "final", "finally", "float", "for", "goto", 35 "if", "implements", "import", "instanceof", "int", 36 "interface", "long", "native", "new", "package", 37 "private", "protected", "public", "return", "short", 38 "static", "strictfp", "super", "switch", "synchronized", 39 "this", "throws", "transient", "try", "void", 40 "volatile", "while" }; 41 42 45 private static final String JSP_PACKAGE_NAME = "org.apache.jsp"; 46 47 54 private static final String makeJavaPackage(String path) { 55 String classNameComponents[] = split(path,"/"); 56 StringBuffer legalClassNames = new StringBuffer (); 57 for (int i = 0; i < classNameComponents.length; i++) { 58 legalClassNames.append(makeJavaIdentifier(classNameComponents[i])); 59 if (i < classNameComponents.length - 1) { 60 legalClassNames.append('.'); 61 } 62 } 63 return legalClassNames.toString(); 64 } 65 66 72 private static final String [] split(String path, String pat) { 73 Vector comps = new Vector (); 74 int pos = path.indexOf(pat); 75 int start = 0; 76 while( pos >= 0 ) { 77 if(pos > start ) { 78 String comp = path.substring(start,pos); 79 comps.add(comp); 80 } 81 start = pos + pat.length(); 82 pos = path.indexOf(pat,start); 83 } 84 if( start < path.length()) { 85 comps.add(path.substring(start)); 86 } 87 String [] result = new String [comps.size()]; 88 for(int i=0; i < comps.size(); i++) { 89 result[i] = (String )comps.elementAt(i); 90 } 91 return result; 92 } 93 94 101 private static final String makeJavaIdentifier(String identifier) { 102 StringBuffer modifiedIdentifier = 103 new StringBuffer (identifier.length()); 104 if (!Character.isJavaIdentifierStart(identifier.charAt(0))) { 105 modifiedIdentifier.append('_'); 106 } 107 for (int i = 0; i < identifier.length(); i++) { 108 char ch = identifier.charAt(i); 109 if (Character.isJavaIdentifierPart(ch) && ch != '_') { 110 modifiedIdentifier.append(ch); 111 } else if (ch == '.') { 112 modifiedIdentifier.append('_'); 113 } else { 114 modifiedIdentifier.append(mangleChar(ch)); 115 } 116 } 117 if (isJavaKeyword(modifiedIdentifier.toString())) { 118 modifiedIdentifier.append('_'); 119 } 120 return modifiedIdentifier.toString(); 121 } 122 123 126 private static final String mangleChar(char ch) { 127 char[] result = new char[5]; 128 result[0] = '_'; 129 result[1] = Character.forDigit((ch >> 12) & 0xf, 16); 130 result[2] = Character.forDigit((ch >> 8) & 0xf, 16); 131 result[3] = Character.forDigit((ch >> 4) & 0xf, 16); 132 result[4] = Character.forDigit(ch & 0xf, 16); 133 return new String (result); 134 } 135 136 139 private static boolean isJavaKeyword(String key) { 140 int i = 0; 141 int j = javaKeywords.length; 142 while (i < j) { 143 int k = (i+j)/2; 144 int result = javaKeywords[k].compareTo(key); 145 if (result == 0) { 146 return true; 147 } 148 if (result < 0) { 149 i = k+1; 150 } else { 151 j = k; 152 } 153 } 154 return false; 155 } 156 157 public static String getServletName(FileObject docBase, FileObject jsp) { 158 String jspRelativePath = FileUtil.getRelativePath(docBase, jsp); 159 return getServletResourcePath(null, jspRelativePath); 160 } 161 162 public static String getServletResourcePath(String moduleContextPath, String jspResourcePath) { 163 return getServletPackageName(jspResourcePath).replace('.', '/') + '/' + 164 getServletClassName(jspResourcePath) + ".java"; 165 } 166 167 private static String getServletPackageName(String jspUri) { 168 String dPackageName = getDerivedPackageName(jspUri); 169 if (dPackageName.length() == 0) { 170 return JSP_PACKAGE_NAME; 171 } 172 return JSP_PACKAGE_NAME + '.' + getDerivedPackageName(jspUri); 173 } 174 175 private static String getDerivedPackageName(String jspUri) { 176 int iSep = jspUri.lastIndexOf('/'); 177 return (iSep > 0) ? makeJavaPackage(jspUri.substring(0,iSep)) : ""; 178 } 179 180 private static String getServletClassName(String jspUri) { 181 int iSep = jspUri.lastIndexOf('/') + 1; 182 return makeJavaIdentifier(jspUri.substring(iSep)); 183 } 184 185 } 186 | Popular Tags |