1 18 package org.apache.tools.ant.taskdefs.optional.jsp; 19 import java.io.File ; 20 21 27 public class JspNameMangler implements JspMangler { 28 29 31 34 public static final String [] keywords = { 35 "assert", 36 "abstract", "boolean", "break", "byte", 37 "case", "catch", "char", "class", 38 "const", "continue", "default", "do", 39 "double", "else", "extends", "final", 40 "finally", "float", "for", "goto", 41 "if", "implements", "import", 42 "instanceof", "int", "interface", 43 "long", "native", "new", "package", 44 "private", "protected", "public", 45 "return", "short", "static", "super", 46 "switch", "synchronized", "this", 47 "throw", "throws", "transient", 48 "try", "void", "volatile", "while" 49 }; 50 51 53 59 public String mapJspToJavaName(File jspFile) { 60 return mapJspToBaseName(jspFile) + ".java"; 61 } 62 63 64 70 private String mapJspToBaseName(File jspFile) { 71 String className; 72 className = stripExtension(jspFile); 73 74 for (int i = 0; i < keywords.length; ++i) { 77 if (className.equals(keywords[i])) { 78 className += "%"; 79 break; 80 } 81 } 82 83 StringBuffer modifiedClassName = new StringBuffer (className.length()); 85 char firstChar = className.charAt(0); 87 if (Character.isJavaIdentifierStart(firstChar)) { 88 modifiedClassName.append(firstChar); 89 } else { 90 modifiedClassName.append(mangleChar(firstChar)); 91 } 92 for (int i = 1; i < className.length(); i++) { 94 char subChar = className.charAt(i); 95 if (Character.isJavaIdentifierPart(subChar)) { 96 modifiedClassName.append(subChar); 97 } else { 98 modifiedClassName.append(mangleChar(subChar)); 99 } 100 } 101 return modifiedClassName.toString(); 102 } 103 104 105 111 private String stripExtension(File jspFile) { 112 String className; 113 String filename = jspFile.getName(); 114 if (filename.endsWith(".jsp")) { 115 className = filename.substring(0, filename.length() - 4); 116 } else { 117 className = filename; 118 } 119 return className; 120 } 121 122 123 129 private static String mangleChar(char ch) { 130 131 if (ch == File.separatorChar) { 132 ch = '/'; 133 } 134 String s = Integer.toHexString(ch); 135 int nzeros = 5 - s.length(); 136 char[] result = new char[6]; 137 result[0] = '_'; 138 for (int i = 1; i <= nzeros; ++i) { 139 result[i] = '0'; 140 } 141 int resultIndex = 0; 142 for (int i = nzeros + 1; i < 6; ++i) { 143 result[i] = s.charAt(resultIndex++); 144 } 145 return new String (result); 146 } 147 148 155 public String mapPath(String path) { 156 return null; 157 } 158 } 159 160 | Popular Tags |