1 34 import java.io.*; 36 import java.util.*; 37 38 public class ConvertURL { 39 40 static String convertURL(String href) { 41 StringBuffer output = new StringBuffer (); 42 43 if (href.startsWith("http") == false && 44 href.startsWith("javascript:") == false && 45 href.startsWith("mailto:") == false && 46 href.startsWith("aim:") == false && 47 href.startsWith("<%=response.encodeURL") == false) { 48 href = href.replaceAll("<%=", "\" + "); 49 href = href.replaceAll("%>", " + \""); 50 51 boolean redundantEnd = false; 52 if (href.endsWith(" + \"")) { 53 redundantEnd = true; 54 href = href.substring(0, href.length()-4); 56 } 57 58 boolean redundantBegin = false; 59 if (href.startsWith("\" + ")) { 60 redundantBegin = true; 61 href = href.substring(4); 63 } 64 65 if (redundantBegin) { 66 output.append("<%=response.encodeURL("); 67 } 68 else { 69 output.append("<%=response.encodeURL(\""); 70 } 71 72 output.append(href); 73 74 if (redundantEnd) { 75 output.append(")%>"); 76 } else { 77 output.append("\")%>"); 78 } 79 } else { 80 return href; 81 } 82 return output.toString(); 83 } 84 85 static String convertTagA(String tagA) throws IllegalArgumentException { 86 String output = ""; 87 if (tagA.startsWith("<a ") == false) { 88 throw new IllegalArgumentException (); 89 } 90 if (tagA.endsWith(">") == false) { 91 throw new IllegalArgumentException (); 92 } 93 94 if (tagA.indexOf('>') != tagA.lastIndexOf('>')) { 95 } 97 int firstIndex = tagA.indexOf("href=\""); 98 if (firstIndex == -1) { 99 System.out.println("WARNING: TagA does not have href: " + tagA); 100 return tagA; 101 } 102 int lastIndex = tagA.indexOf("\"", firstIndex + 6); 103 104 String begin = tagA.substring(0, firstIndex + 6); 105 String href = tagA.substring(firstIndex + 6, lastIndex); 106 String end = tagA.substring(lastIndex); 107 108 112 output = begin + convertURL(href) + end; 113 114 return output; 115 } 116 117 static String convertTagForm(String tagForm) throws IllegalArgumentException { 118 String output = ""; 119 if (tagForm.startsWith("<form ") == false) { 120 throw new IllegalArgumentException (); 121 } 122 if (tagForm.endsWith(">") == false) { 123 throw new IllegalArgumentException (); 124 } 125 126 if (tagForm.indexOf('>') != tagForm.lastIndexOf('>')) { 127 } 129 int firstIndex = tagForm.indexOf("action=\""); 130 if (firstIndex == -1) { 131 System.out.println("WARNING: TagForm does not have action: " + tagForm); 132 return tagForm; 133 } 134 int lastIndex = tagForm.indexOf("\"", firstIndex + 8); 135 136 String begin = tagForm.substring(0, firstIndex + 8); 137 String href = tagForm.substring(firstIndex + 8, lastIndex); 138 String end = tagForm.substring(lastIndex); 139 140 144 output = begin + convertURL(href) + end; 145 146 return output; 147 } 148 149 static String convertTagMeta(String tagMeta) throws IllegalArgumentException { 150 String output = ""; 151 if (tagMeta.startsWith("<meta ") == false) { 152 throw new IllegalArgumentException (); 153 } 154 if (tagMeta.endsWith(">") == false) { 155 throw new IllegalArgumentException (); 156 } 157 158 if (tagMeta.indexOf('>') != tagMeta.lastIndexOf('>')) { 159 } 161 int firstIndex = tagMeta.indexOf("url="); 162 if (firstIndex == -1) { 163 System.out.println("WARNING: TagMeta does not have url=: " + tagMeta); 164 return tagMeta; 165 } 166 int lastIndex = tagMeta.indexOf("'", firstIndex + 4); 167 168 String begin = tagMeta.substring(0, firstIndex + 4); 169 String href = tagMeta.substring(firstIndex + 4, lastIndex); 170 String end = tagMeta.substring(lastIndex); 171 172 176 output = begin + convertURL(href) + end; 177 178 return output; 179 } 180 181 static String convertLine(String line, String startToken, String endToken) throws IllegalArgumentException { 182 int startIndex = -1; 183 int endIndex = -1; 184 outer: 185 while (true) { 186 startIndex = line.indexOf(startToken, endIndex + 1); 187 if (startIndex < 0 || startIndex + 1 >= line.length()) { 188 break; 189 } 190 int beginSearchEndIndex = startIndex + 1; 191 while (true) { 192 endIndex = line.indexOf(endToken, beginSearchEndIndex); 193 if (endIndex < 0) break; 194 char charBefore = line.charAt(endIndex - 1); 195 if (charBefore == '%') { 196 beginSearchEndIndex = endIndex + 1; 197 } else { 198 break; 199 } 200 } 201 if (endIndex < 0) { 202 break; 203 } 204 String matches = line.substring(startIndex, endIndex+1); 205 for (int k = 0; k < matches.length(); k++) { 209 218 } 219 String replace = null; 220 if (startToken.equals("<a ")) { 221 replace = (String ) convertTagA(matches); 222 } else if (startToken.equals("<form ")) { 223 replace = (String ) convertTagForm(matches); 224 } else if (startToken.equals("<meta ")) { 225 replace = (String ) convertTagMeta(matches); 226 } else { 227 throw new IllegalArgumentException (); 228 } 229 230 if (replace == null) { 233 replace = matches; 235 } 236 line = line.substring(0, startIndex) 237 + replace 238 + line.substring(endIndex + 1); 239 endIndex = startIndex + replace.length() - 1; 242 if (endIndex + 1 >= line.length()) { 243 break; 244 } 245 } 246 return line; 247 } 248 249 static String convertLine(String line) throws Exception { 250 try { 251 String linewithA = convertLine(line, "<a ", ">"); 252 String linewithAandForm = convertLine(linewithA, "<form ", ">"); 253 String linewithAandFormAndMeta = convertLine(linewithAandForm, "<meta ", ">"); 254 return linewithAandFormAndMeta; 255 } catch (Exception ex) { 256 System.out.println("line = " + line); 257 throw ex; 258 } 259 } 260 261 264 public static ArrayList getContent(String fileName) { 265 ArrayList arrLine= new ArrayList(); 266 File file = new File(fileName); 267 if (!file.exists()) { 268 return null; 269 } else { 270 try { 271 BufferedReader reader = new BufferedReader(new FileReader(file)); 272 String inLine = reader.readLine(); 273 while (inLine != null) { 274 arrLine.add(inLine + "\n"); 275 inLine = reader.readLine(); 277 } 278 reader.close(); 279 return arrLine; 280 } catch (IOException e) { 281 e.printStackTrace(); 282 return null; 283 } 284 } 285 } 286 287 public static void convertFile(String srcFilename, String descFilename) 288 throws Exception { 289 ArrayList descArr = new ArrayList(); 290 Collection tempArr = getContent(srcFilename); 291 for (Iterator iterator = tempArr.iterator(); iterator.hasNext(); ) { 292 descArr.add(convertLine((String ) iterator.next())); 293 } 294 ExportContentToFile(descArr, descFilename); 295 } 296 297 public static void ExportContentToFile(ArrayList lines, String fileName) { 298 String curDir = System.getProperty("user.dir"); try { 300 BufferedWriter out = new BufferedWriter(new FileWriter(curDir + "/out/" + fileName)); 301 for (Iterator iterator = lines.iterator(); iterator.hasNext(); ) { 302 out.write((String ) iterator.next()); 304 } 305 out.close(); 306 } catch (IOException e) { 307 System.out.print(e); 308 } 309 } 310 311 public static void main(String [] args) throws Exception { 312 316 324 328 332 333 339 341 try { 342 if (args[0].equals("all")) { 343 String curDir = System.getProperty("user.dir"); File dir = new File(curDir); 345 if (dir.isFile()) throw new IOException("IOException -> BadInputException: not a directory."); 346 File[] files = dir.listFiles(); 347 if (files != null) { 348 for (int i = 0; i < files.length; i++) { 349 File file = files[i]; 350 if (file.isFile()) { 351 String absPath = file.getAbsolutePath(); 352 if (absPath.endsWith(".jsp")) { 353 int lastIndex = absPath.lastIndexOf('\\'); 354 String name = absPath.substring(lastIndex + 1); 355 System.out.println("name = " + name); 356 convertFile(name, name); 357 } 358 } else { 359 System.out.println("get folder = " + file); 360 } 361 } 362 } dir.delete(); 364 } else { 365 convertFile(args[0], args[0]); 366 } 367 } catch (Exception ex) { 368 ex.printStackTrace(); 369 } 370 } 371 } 372 | Popular Tags |