1 18 19 package org.osgi.service.permissionadmin; 20 21 40 public class PermissionInfo { 41 private String type; 42 private String name; 43 private String actions; 44 45 68 public PermissionInfo(String type, String name, String actions) { 69 this.type = type; 70 this.name = name; 71 this.actions = actions; 72 if (type == null) { 73 throw new NullPointerException ("type is null"); 74 } 75 if ((name == null) && (actions != null)) { 76 throw new IllegalArgumentException ("name missing"); 77 } 78 } 79 80 91 public PermissionInfo(String encodedPermission) { 92 if (encodedPermission == null) { 93 throw new NullPointerException ("missing encoded permission"); 94 } 95 if (encodedPermission.length() == 0) { 96 throw new IllegalArgumentException ("empty encoded permission"); 97 } 98 try { 99 char[] encoded = encodedPermission.toCharArray(); 100 int length = encoded.length; 101 int pos = 0; 102 103 104 while (Character.isWhitespace(encoded[pos])) { 105 pos++; 106 } 107 108 109 if (encoded[pos] != '(') { 110 throw new IllegalArgumentException ( 111 "expecting open parenthesis"); 112 } 113 pos++; 114 115 116 while (Character.isWhitespace(encoded[pos])) { 117 pos++; 118 } 119 120 121 int begin = pos; 122 while (!Character.isWhitespace(encoded[pos]) && (encoded[pos] != ')')) { 123 pos++; 124 } 125 if (pos == begin || encoded[begin] == '"') { 126 throw new IllegalArgumentException ("expecting type"); 127 } 128 this.type = new String (encoded, begin, pos - begin); 129 130 131 while (Character.isWhitespace(encoded[pos])) { 132 pos++; 133 } 134 135 136 if (encoded[pos] == '"') { 137 pos++; 138 begin = pos; 139 while (encoded[pos] != '"') { 140 if (encoded[pos] == '\\') { 141 pos++; 142 } 143 pos++; 144 } 145 this.name = unescapeString(encoded, begin, pos); 146 pos++; 147 148 if (Character.isWhitespace(encoded[pos])) { 149 150 while (Character.isWhitespace(encoded[pos])) { 151 pos++; 152 } 153 154 155 if (encoded[pos] == '"') { 156 pos++; 157 begin = pos; 158 while (encoded[pos] != '"') { 159 if (encoded[pos] == '\\') { 160 pos++; 161 } 162 pos++; 163 } 164 this.actions = unescapeString(encoded, begin, pos); 165 pos++; 166 167 168 while (Character.isWhitespace(encoded[pos])) { 169 pos++; 170 } 171 } 172 } 173 } 174 175 176 char c = encoded[pos]; 177 pos++; 178 while ((pos < length) && Character.isWhitespace(encoded[pos])) { 179 pos++; 180 } 181 if ((c != ')') || (pos != length)) { 182 throw new IllegalArgumentException ("expecting close parenthesis"); 183 } 184 } 185 catch (ArrayIndexOutOfBoundsException e) { 186 throw new IllegalArgumentException ("parsing terminated abruptly"); 187 } 188 } 189 190 225 public final String getEncoded() { 226 StringBuffer output = new StringBuffer ( 227 8 228 + type.length() 229 + ((((name == null) ? 0 : name.length()) + ((actions == null) ? 0 230 : actions.length())) << 1)); 231 output.append('('); 232 output.append(type); 233 if (name != null) { 234 output.append(" \""); 235 escapeString(name, output); 236 if (actions != null) { 237 output.append("\" \""); 238 escapeString(actions, output); 239 } 240 output.append('\"'); 241 } 242 output.append(')'); 243 return output.toString(); 244 } 245 246 253 public String toString() { 254 return getEncoded(); 255 } 256 257 264 public final String getType() { 265 return type; 266 } 267 268 276 public final String getName() { 277 return name; 278 } 279 280 288 public final String getActions() { 289 return actions; 290 } 291 292 304 public boolean equals(Object obj) { 305 if (obj == this) { 306 return true; 307 } 308 if (!(obj instanceof PermissionInfo)) { 309 return false; 310 } 311 PermissionInfo other = (PermissionInfo) obj; 312 if (!type.equals(other.type) || ((name == null) ^ (other.name == null)) 313 || ((actions == null) ^ (other.actions == null))) { 314 return false; 315 } 316 if (name != null) { 317 if (actions != null) { 318 return name.equals(other.name) && actions 319 .equals(other.actions); 320 } 321 else { 322 return name.equals(other.name); 323 } 324 } 325 else { 326 return true; 327 } 328 } 329 330 335 public int hashCode() { 336 int hash = type.hashCode(); 337 if (name != null) { 338 hash ^= name.hashCode(); 339 if (actions != null) { 340 hash ^= actions.hashCode(); 341 } 342 } 343 return hash; 344 } 345 346 350 private static void escapeString(String str, StringBuffer output) { 351 int len = str.length(); 352 for (int i = 0; i < len; i++) { 353 char c = str.charAt(i); 354 switch (c) { 355 case '"' : 356 case '\\' : 357 output.append('\\'); 358 output.append(c); 359 break; 360 case '\r' : 361 output.append("\\r"); 362 break; 363 case '\n' : 364 output.append("\\n"); 365 break; 366 default : 367 output.append(c); 368 break; 369 } 370 } 371 } 372 373 376 private static String unescapeString(char[] str, int begin, int end) { 377 StringBuffer output = new StringBuffer (end - begin); 378 for (int i = begin; i < end; i++) { 379 char c = str[i]; 380 if (c == '\\') { 381 i++; 382 if (i < end) { 383 c = str[i]; 384 switch (c) { 385 case '"' : 386 case '\\' : 387 break; 388 case 'r' : 389 c = '\r'; 390 break; 391 case 'n' : 392 c = '\n'; 393 break; 394 default : 395 c = '\\'; 396 i--; 397 break; 398 } 399 } 400 } 401 output.append(c); 402 } 403 404 return output.toString(); 405 } 406 } 407 | Popular Tags |