1 23 24 package org.jivesoftware.stringprep; 25 26 public class Stringprep { 27 28 32 private static final char [] NODEPREP_PROHIBIT = new char [] { '\u0022', '\u0026', '\'', 33 '\u002F', '\u003A', '\u003C', '\u003E', '\u003E', '\u0040'}; 34 35 public static String nameprep(String input, boolean allowUnassigned) 36 throws StringprepException 37 { 38 if (input == null) { 39 return null; 40 } 41 StringBuilder s = new StringBuilder (input); 42 43 if (allowUnassigned) { 44 if (contains(s, RFC3454.A1)) { 45 throw new StringprepException(StringprepException.CONTAINS_UNASSIGNED); 46 } 47 } 48 49 filter(s, RFC3454.B1); 50 map(s, RFC3454.B2search, RFC3454.B2replace); 51 52 s = new StringBuilder (NFKC.normalizeNFKC(s.toString())); 53 56 if (contains(s, RFC3454.C1_2) || 57 contains(s, RFC3454.C2_2) || 58 contains(s, RFC3454.C3) || 59 contains(s, RFC3454.C4) || 60 contains(s, RFC3454.C5) || 61 contains(s, RFC3454.C6) || 62 contains(s, RFC3454.C7) || 63 contains(s, RFC3454.C8)) { 64 throw new StringprepException(StringprepException.CONTAINS_PROHIBITED); 67 } 68 69 boolean r = contains(s, RFC3454.D1); 71 boolean l = contains(s, RFC3454.D2); 72 73 75 if (r && l) { 77 throw new StringprepException(StringprepException.BIDI_BOTHRAL); 78 } 79 80 if (r) { 82 if (!contains(s.charAt(0), RFC3454.D1) || 83 !contains(s.charAt(s.length() - 1), RFC3454.D1)) { 84 throw new StringprepException(StringprepException.BIDI_LTRAL); 85 } 86 } 87 88 return s.toString(); 89 } 90 91 public static String nodeprep(String input) throws StringprepException { 92 if (input == null) { 93 return null; 94 } 95 StringBuilder s = new StringBuilder (input); 96 97 if (contains(s, RFC3454.A1)) { 98 throw new StringprepException(StringprepException.CONTAINS_UNASSIGNED); 99 } 100 101 filter(s, RFC3454.B1); 102 map(s, RFC3454.B2search, RFC3454.B2replace); 103 104 s = new StringBuilder (NFKC.normalizeNFKC(s.toString())); 105 106 if (contains(s, RFC3454.C1_1) || 107 contains(s, RFC3454.C1_2) || 108 contains(s, RFC3454.C2_1) || 109 contains(s, RFC3454.C2_2) || 110 contains(s, RFC3454.C3) || 111 contains(s, RFC3454.C4) || 112 contains(s, RFC3454.C5) || 113 contains(s, RFC3454.C6) || 114 contains(s, RFC3454.C7) || 115 contains(s, RFC3454.C8) || 116 contains(s, NODEPREP_PROHIBIT)) 117 { 118 throw new StringprepException(StringprepException.CONTAINS_PROHIBITED); 121 } 122 123 boolean r = contains(s, RFC3454.D1); 125 boolean l = contains(s, RFC3454.D2); 126 127 129 if (r && l) { 131 throw new StringprepException(StringprepException.BIDI_BOTHRAL); 132 } 133 134 if (r) { 136 if (!contains(s.charAt(0), RFC3454.D1) || 137 !contains(s.charAt(s.length() - 1), RFC3454.D1)) { 138 throw new StringprepException(StringprepException.BIDI_LTRAL); 139 } 140 } 141 142 return s.toString(); 143 } 144 145 public static String resourceprep(String input) throws StringprepException { 146 if (input == null) { 147 return null; 148 } 149 StringBuilder s = new StringBuilder (input); 150 151 if (contains(s, RFC3454.A1)) { 152 throw new StringprepException(StringprepException.CONTAINS_UNASSIGNED); 153 } 154 155 filter(s, RFC3454.B1); 156 157 s = new StringBuilder (NFKC.normalizeNFKC(s.toString())); 158 159 if (contains(s, RFC3454.C1_2) && 160 contains(s, RFC3454.C2_1) && 161 contains(s, RFC3454.C2_2) && 162 contains(s, RFC3454.C3) && 163 contains(s, RFC3454.C4) && 164 contains(s, RFC3454.C5) && 165 contains(s, RFC3454.C6) && 166 contains(s, RFC3454.C7) && 167 contains(s, RFC3454.C8)) 168 { 169 throw new StringprepException(StringprepException.CONTAINS_PROHIBITED); 172 } 173 174 boolean r = contains(s, RFC3454.D1); 176 boolean l = contains(s, RFC3454.D2); 177 178 180 if (r && l) { 182 throw new StringprepException(StringprepException.BIDI_BOTHRAL); 183 } 184 185 if (r) { 187 if (!contains(s.charAt(0), RFC3454.D1) || 188 !contains(s.charAt(s.length() - 1), RFC3454.D1)) { 189 throw new StringprepException(StringprepException.BIDI_LTRAL); 190 } 191 } 192 193 return s.toString(); 194 } 195 196 private static boolean contains(StringBuilder s, char[] p) { 197 for (int i = 0; i < p.length; i++) { 198 char c = p[i]; 199 for (int j = 0; j < s.length(); j++) { 200 if (c == s.charAt(j)) { 201 return true; 202 } 203 } 204 } 205 return false; 206 } 207 208 private static boolean contains(StringBuilder s, char[][] p) { 209 for (int i = 0; i < p.length; i++) { 210 char[] r = p[i]; 211 if (1 == r.length) { 212 char c = r[0]; 213 for (int j = 0; j < s.length(); j++) { 214 if (c == s.charAt(j)) { 215 return true; 216 } 217 } 218 } else if (2 == r.length) { 219 char f = r[0]; 220 char t = r[1]; 221 for (int j = 0; j < s.length(); j++) { 222 if (f <= s.charAt(j) && t >= s.charAt(j)) { 223 return true; 224 } 225 } 226 } 227 } 228 return false; 229 } 230 231 private static boolean contains(char c, char[][] p) { 232 for (int i = 0; i < p.length; i++) { 233 char[] r = p[i]; 234 if (1 == r.length) { 235 if (c == r[0]) { 236 return true; 237 } 238 } else if (2 == r.length) { 239 char f = r[0]; 240 char t = r[1]; 241 if (f <= c && t >= c) { 242 return true; 243 } 244 } 245 } 246 return false; 247 } 248 249 private static void filter(StringBuilder s, char[] f) { 250 for (int i = 0; i < f.length; i++) { 251 char c = f[i]; 252 253 int j = 0; 254 while (j < s.length()) { 255 if (c == s.charAt(j)) { 256 s.deleteCharAt(j); 257 } else { 258 j++; 259 } 260 } 261 } 262 } 263 264 private static void filter(StringBuilder s, char[][] f) { 265 for (int i = 0; i < f.length; i++) { 266 char[] r = f[i]; 267 268 if (1 == r.length) { 269 char c = r[0]; 270 271 int j = 0; 272 while (j < s.length()) { 273 if (c == s.charAt(j)) { 274 s.deleteCharAt(j); 275 } else { 276 j++; 277 } 278 } 279 } else if (2 == r.length) { 280 char from = r[0]; 281 char to = r[1]; 282 283 int j = 0; 284 while (j < s.length()) { 285 if (from <= s.charAt(j) && to >= s.charAt(j)) { 286 s.deleteCharAt(j); 287 } else { 288 j++; 289 } 290 } 291 } 292 } 293 } 294 295 private static void map(StringBuilder s, char[] search, String [] replace) { 296 for (int i = 0; i < search.length; i++) { 297 char c = search[i]; 298 299 int j = 0; 300 while (j < s.length()) { 301 if (c == s.charAt(j)) { 302 s.deleteCharAt(j); 303 if (null != replace[i]) { 304 s.insert(j, replace[i]); 305 j += replace[i].length() - 1; 306 } 307 } else { 308 j++; 309 } 310 } 311 } 312 } 313 } | Popular Tags |