1 3 19 20 package com.sun.org.apache.xml.internal.resolver.helpers; 21 22 33 public abstract class PublicId { 34 protected PublicId() { } 35 36 57 public static String normalize(String publicId) { 58 String normal = publicId.replace('\t', ' '); 59 normal = normal.replace('\r', ' '); 60 normal = normal.replace('\n', ' '); 61 normal = normal.trim(); 62 63 int pos; 64 65 while ((pos = normal.indexOf(" ")) >= 0) { 66 normal = normal.substring(0, pos) + normal.substring(pos+1); 67 } 68 69 return normal; 70 } 71 72 82 public static String encodeURN(String publicId) { 83 String urn = PublicId.normalize(publicId); 84 85 urn = PublicId.stringReplace(urn, "%", "%25"); 86 urn = PublicId.stringReplace(urn, ";", "%3B"); 87 urn = PublicId.stringReplace(urn, "'", "%27"); 88 urn = PublicId.stringReplace(urn, "?", "%3F"); 89 urn = PublicId.stringReplace(urn, "#", "%23"); 90 urn = PublicId.stringReplace(urn, "+", "%2B"); 91 urn = PublicId.stringReplace(urn, " ", "+"); 92 urn = PublicId.stringReplace(urn, "::", ";"); 93 urn = PublicId.stringReplace(urn, ":", "%3A"); 94 urn = PublicId.stringReplace(urn, "//", ":"); 95 urn = PublicId.stringReplace(urn, "/", "%2F"); 96 97 return "urn:publicid:" + urn; 98 } 99 100 110 public static String decodeURN(String urn) { 111 String publicId = ""; 112 113 if (urn.startsWith("urn:publicid:")) { 114 publicId = urn.substring(13); 115 } else { 116 return urn; 117 } 118 119 publicId = PublicId.stringReplace(publicId, "%2F", "/"); 120 publicId = PublicId.stringReplace(publicId, ":", "//"); 121 publicId = PublicId.stringReplace(publicId, "%3A", ":"); 122 publicId = PublicId.stringReplace(publicId, ";", "::"); 123 publicId = PublicId.stringReplace(publicId, "+", " "); 124 publicId = PublicId.stringReplace(publicId, "%2B", "+"); 125 publicId = PublicId.stringReplace(publicId, "%23", "#"); 126 publicId = PublicId.stringReplace(publicId, "%3F", "?"); 127 publicId = PublicId.stringReplace(publicId, "%27", "'"); 128 publicId = PublicId.stringReplace(publicId, "%3B", ";"); 129 publicId = PublicId.stringReplace(publicId, "%25", "%"); 130 131 return publicId; 132 } 133 134 138 private static String stringReplace(String str, 139 String oldStr, 140 String newStr) { 141 142 String result = ""; 143 int pos = str.indexOf(oldStr); 144 145 147 while (pos >= 0) { 148 result += str.substring(0, pos); 150 result += newStr; 151 str = str.substring(pos+1); 152 153 pos = str.indexOf(oldStr); 154 } 155 156 return result + str; 157 } 158 } 159 | Popular Tags |