1 3 56 57 package org.jboss.util.xml.catalog.helpers; 58 59 70 public abstract class PublicId { 71 protected PublicId() { } 72 73 94 public static String normalize(String publicId) { 95 String normal = publicId.replace('\t', ' '); 96 normal = normal.replace('\r', ' '); 97 normal = normal.replace('\n', ' '); 98 normal = normal.trim(); 99 100 int pos; 101 102 while ((pos = normal.indexOf(" ")) >= 0) { 103 normal = normal.substring(0, pos) + normal.substring(pos+1); 104 } 105 106 return normal; 107 } 108 109 119 public static String encodeURN(String publicId) { 120 String urn = PublicId.normalize(publicId); 121 122 urn = PublicId.stringReplace(urn, "%", "%25"); 123 urn = PublicId.stringReplace(urn, ";", "%3B"); 124 urn = PublicId.stringReplace(urn, "'", "%27"); 125 urn = PublicId.stringReplace(urn, "?", "%3F"); 126 urn = PublicId.stringReplace(urn, "#", "%23"); 127 urn = PublicId.stringReplace(urn, "+", "%2B"); 128 urn = PublicId.stringReplace(urn, " ", "+"); 129 urn = PublicId.stringReplace(urn, "::", ";"); 130 urn = PublicId.stringReplace(urn, ":", "%3A"); 131 urn = PublicId.stringReplace(urn, "//", ":"); 132 urn = PublicId.stringReplace(urn, "/", "%2F"); 133 134 return "urn:publicid:" + urn; 135 } 136 137 147 public static String decodeURN(String urn) { 148 String publicId = ""; 149 150 if (urn.startsWith("urn:publicid:")) { 151 publicId = urn.substring(13); 152 } else { 153 return urn; 154 } 155 156 publicId = PublicId.stringReplace(publicId, "%2F", "/"); 157 publicId = PublicId.stringReplace(publicId, ":", "//"); 158 publicId = PublicId.stringReplace(publicId, "%3A", ":"); 159 publicId = PublicId.stringReplace(publicId, ";", "::"); 160 publicId = PublicId.stringReplace(publicId, "+", " "); 161 publicId = PublicId.stringReplace(publicId, "%2B", "+"); 162 publicId = PublicId.stringReplace(publicId, "%23", "#"); 163 publicId = PublicId.stringReplace(publicId, "%3F", "?"); 164 publicId = PublicId.stringReplace(publicId, "%27", "'"); 165 publicId = PublicId.stringReplace(publicId, "%3B", ";"); 166 publicId = PublicId.stringReplace(publicId, "%25", "%"); 167 168 return publicId; 169 } 170 171 175 private static String stringReplace(String str, 176 String oldStr, 177 String newStr) { 178 179 String result = ""; 180 int pos = str.indexOf(oldStr); 181 182 184 while (pos >= 0) { 185 result += str.substring(0, pos); 187 result += newStr; 188 str = str.substring(pos+1); 189 190 pos = str.indexOf(oldStr); 191 } 192 193 return result + str; 194 } 195 } 196 | Popular Tags |