1 14 15 package com.sun.facelets.util; 16 17 21 public final class Path { 22 23 public static final String normalize(String path) { 24 if (path.length() == 0) 25 return path; 26 String n = path; 27 boolean abs = false; 28 while (n.indexOf('\\') >= 0) { 29 n = n.replace('\\', '/'); 30 } 31 if (n.charAt(0) != '/') { 32 n = '/' + n; 33 abs = true; 34 } 35 int idx = 0; 36 while (true) { 37 idx = n.indexOf("%20"); 38 if (idx == -1) { 39 break; 40 } 41 n = n.substring(0, idx) + " " + n.substring(idx + 3); 42 } 43 while (true) { 44 idx = n.indexOf("/./"); 45 if (idx == -1) { 46 break; 47 } 48 n = n.substring(0, idx) + n.substring(idx + 2); 49 } 50 if (abs) { 51 n = n.substring(1); 52 } 53 return n; 54 } 55 56 public static final String relative(String ctx, String path) { 57 if (path.length() == 0) { 58 return context(ctx); 59 } 60 String c = context(normalize(ctx)); 61 String p = normalize(path); 62 p = c + p; 63 64 int idx = 0; 65 while (true) { 66 idx = p.indexOf("/../"); 67 if (idx == -1) { 68 break; 69 } 70 int s = p.lastIndexOf('/', idx - 3); 71 if (s == -1) { 72 break; 73 } 74 p = p.substring(0, s) + p.substring(idx + 3); 75 } 76 return p; 77 } 78 79 public static final String context(String path) { 80 int idx = path.lastIndexOf('/'); 81 if (idx == -1) { 82 return "/"; 83 } 84 return path.substring(0, idx + 1); 85 } 86 87 } 88 | Popular Tags |