1 18 19 package org.osgi.service.condpermadmin; 20 21 import java.util.ArrayList ; 22 23 50 public class ConditionInfo { 51 private String type; 52 private String [] args; 53 54 66 public ConditionInfo(String type, String [] args) { 67 this.type = type; 68 this.args = args != null ? args : new String [0]; 69 if (type == null) { 70 throw new NullPointerException ("type is null"); 71 } 72 } 73 74 84 public ConditionInfo(String encodedCondition) { 85 if (encodedCondition == null) { 86 throw new NullPointerException ("missing encoded condition"); 87 } 88 if (encodedCondition.length() == 0) { 89 throw new IllegalArgumentException ("empty encoded condition"); 90 } 91 try { 92 char[] encoded = encodedCondition.toCharArray(); 93 int length = encoded.length; 94 int pos = 0; 95 96 97 while (Character.isWhitespace(encoded[pos])) { 98 pos++; 99 } 100 101 102 if (encoded[pos] != '[') { 103 throw new IllegalArgumentException ("expecting open bracket"); 104 } 105 pos++; 106 107 108 while (Character.isWhitespace(encoded[pos])) { 109 pos++; 110 } 111 112 113 int begin = pos; 114 while (!Character.isWhitespace(encoded[pos]) 115 && (encoded[pos] != ']')) { 116 pos++; 117 } 118 if (pos == begin || encoded[begin] == '"') { 119 throw new IllegalArgumentException ("expecting type"); 120 } 121 this.type = new String (encoded, begin, pos - begin); 122 123 124 while (Character.isWhitespace(encoded[pos])) { 125 pos++; 126 } 127 128 129 ArrayList argsList = new ArrayList (); 130 while (encoded[pos] == '"') { 131 pos++; 132 begin = pos; 133 while (encoded[pos] != '"') { 134 if (encoded[pos] == '\\') { 135 pos++; 136 } 137 pos++; 138 } 139 argsList.add(unescapeString(encoded, begin, pos)); 140 pos++; 141 142 if (Character.isWhitespace(encoded[pos])) { 143 144 while (Character.isWhitespace(encoded[pos])) { 145 pos++; 146 } 147 } 148 } 149 this.args = (String []) argsList 150 .toArray(new String [argsList.size()]); 151 152 153 char c = encoded[pos]; 154 pos++; 155 while ((pos < length) && Character.isWhitespace(encoded[pos])) { 156 pos++; 157 } 158 if ((c != ']') || (pos != length)) { 159 throw new IllegalArgumentException ("expecting close bracket"); 160 } 161 } 162 catch (ArrayIndexOutOfBoundsException e) { 163 throw new IllegalArgumentException ("parsing terminated abruptly"); 164 } 165 } 166 167 191 public final String getEncoded() { 192 StringBuffer output = new StringBuffer (); 193 output.append('['); 194 output.append(type); 195 196 for (int i = 0; i < args.length; i++) { 197 output.append(" \""); 198 escapeString(args[i], output); 199 output.append('\"'); 200 } 201 202 output.append(']'); 203 204 return output.toString(); 205 } 206 207 214 public String toString() { 215 return getEncoded(); 216 } 217 218 225 public final String getType() { 226 return type; 227 } 228 229 236 public final String [] getArgs() { 237 return args; 238 } 239 240 253 public boolean equals(Object obj) { 254 if (obj == this) { 255 return true; 256 } 257 258 if (!(obj instanceof ConditionInfo)) { 259 return false; 260 } 261 262 ConditionInfo other = (ConditionInfo) obj; 263 264 if (!type.equals(other.type) || args.length != other.args.length) 265 return false; 266 267 for (int i = 0; i < args.length; i++) { 268 if (!args[i].equals(other.args[i])) 269 return false; 270 } 271 return true; 272 } 273 274 279 280 public int hashCode() { 281 int hash = type.hashCode(); 282 283 for (int i = 0; i < args.length; i++) { 284 hash ^= args[i].hashCode(); 285 } 286 return hash; 287 } 288 289 293 private static void escapeString(String str, StringBuffer output) { 294 int len = str.length(); 295 for (int i = 0; i < len; i++) { 296 char c = str.charAt(i); 297 switch (c) { 298 case '"' : 299 case '\\' : 300 output.append('\\'); 301 output.append(c); 302 break; 303 case '\r' : 304 output.append("\\r"); 305 break; 306 case '\n' : 307 output.append("\\n"); 308 break; 309 default : 310 output.append(c); 311 break; 312 } 313 } 314 } 315 316 319 private static String unescapeString(char[] str, int begin, int end) { 320 StringBuffer output = new StringBuffer (end - begin); 321 for (int i = begin; i < end; i++) { 322 char c = str[i]; 323 if (c == '\\') { 324 i++; 325 if (i < end) { 326 c = str[i]; 327 switch (c) { 328 case '"' : 329 case '\\' : 330 break; 331 case 'r' : 332 c = '\r'; 333 break; 334 case 'n' : 335 c = '\n'; 336 break; 337 default : 338 c = '\\'; 339 i--; 340 break; 341 } 342 } 343 } 344 output.append(c); 345 } 346 347 return output.toString(); 348 } 349 } 350 | Popular Tags |