00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 package org.osgi.service.condpermadmin;
00020
00021 import java.util.ArrayList;
00022
00050 public class ConditionInfo {
00051 private String type;
00052 private String[] args;
00053
00066 public ConditionInfo(String type, String[] args) {
00067 this.type = type;
00068 this.args = args != null ? args : new String[0];
00069 if (type == null) {
00070 throw new NullPointerException("type is null");
00071 }
00072 }
00073
00084 public ConditionInfo(String encodedCondition) {
00085 if (encodedCondition == null) {
00086 throw new NullPointerException("missing encoded condition");
00087 }
00088 if (encodedCondition.length() == 0) {
00089 throw new IllegalArgumentException("empty encoded condition");
00090 }
00091 try {
00092 char[] encoded = encodedCondition.toCharArray();
00093 int length = encoded.length;
00094 int pos = 0;
00095
00096
00097 while (Character.isWhitespace(encoded[pos])) {
00098 pos++;
00099 }
00100
00101
00102 if (encoded[pos] != '[') {
00103 throw new IllegalArgumentException("expecting open bracket");
00104 }
00105 pos++;
00106
00107
00108 while (Character.isWhitespace(encoded[pos])) {
00109 pos++;
00110 }
00111
00112
00113 int begin = pos;
00114 while (!Character.isWhitespace(encoded[pos])
00115 && (encoded[pos] != ']')) {
00116 pos++;
00117 }
00118 if (pos == begin || encoded[begin] == '"') {
00119 throw new IllegalArgumentException("expecting type");
00120 }
00121 this.type = new String(encoded, begin, pos - begin);
00122
00123
00124 while (Character.isWhitespace(encoded[pos])) {
00125 pos++;
00126 }
00127
00128
00129 ArrayList argsList = new ArrayList();
00130 while (encoded[pos] == '"') {
00131 pos++;
00132 begin = pos;
00133 while (encoded[pos] != '"') {
00134 if (encoded[pos] == '\\') {
00135 pos++;
00136 }
00137 pos++;
00138 }
00139 argsList.add(unescapeString(encoded, begin, pos));
00140 pos++;
00141
00142 if (Character.isWhitespace(encoded[pos])) {
00143
00144 while (Character.isWhitespace(encoded[pos])) {
00145 pos++;
00146 }
00147 }
00148 }
00149 this.args = (String[]) argsList
00150 .toArray(new String[argsList.size()]);
00151
00152
00153 char c = encoded[pos];
00154 pos++;
00155 while ((pos < length) && Character.isWhitespace(encoded[pos])) {
00156 pos++;
00157 }
00158 if ((c != ']') || (pos != length)) {
00159 throw new IllegalArgumentException("expecting close bracket");
00160 }
00161 }
00162 catch (ArrayIndexOutOfBoundsException e) {
00163 throw new IllegalArgumentException("parsing terminated abruptly");
00164 }
00165 }
00166
00191 public final String getEncoded() {
00192 StringBuffer output = new StringBuffer();
00193 output.append('[');
00194 output.append(type);
00195
00196 for (int i = 0; i < args.length; i++) {
00197 output.append(" \"");
00198 escapeString(args[i], output);
00199 output.append('\"');
00200 }
00201
00202 output.append(']');
00203
00204 return output.toString();
00205 }
00206
00214 public String toString() {
00215 return getEncoded();
00216 }
00217
00225 public final String getType() {
00226 return type;
00227 }
00228
00236 public final String[] getArgs() {
00237 return args;
00238 }
00239
00253 public boolean equals(Object obj) {
00254 if (obj == this) {
00255 return true;
00256 }
00257
00258 if (!(obj instanceof ConditionInfo)) {
00259 return false;
00260 }
00261
00262 ConditionInfo other = (ConditionInfo) obj;
00263
00264 if (!type.equals(other.type) || args.length != other.args.length)
00265 return false;
00266
00267 for (int i = 0; i < args.length; i++) {
00268 if (!args[i].equals(other.args[i]))
00269 return false;
00270 }
00271 return true;
00272 }
00273
00280 public int hashCode() {
00281 int hash = type.hashCode();
00282
00283 for (int i = 0; i < args.length; i++) {
00284 hash ^= args[i].hashCode();
00285 }
00286 return hash;
00287 }
00288
00293 private static void escapeString(String str, StringBuffer output) {
00294 int len = str.length();
00295 for (int i = 0; i < len; i++) {
00296 char c = str.charAt(i);
00297 switch (c) {
00298 case '"' :
00299 case '\\' :
00300 output.append('\\');
00301 output.append(c);
00302 break;
00303 case '\r' :
00304 output.append("\\r");
00305 break;
00306 case '\n' :
00307 output.append("\\n");
00308 break;
00309 default :
00310 output.append(c);
00311 break;
00312 }
00313 }
00314 }
00315
00319 private static String unescapeString(char[] str, int begin, int end) {
00320 StringBuffer output = new StringBuffer(end - begin);
00321 for (int i = begin; i < end; i++) {
00322 char c = str[i];
00323 if (c == '\\') {
00324 i++;
00325 if (i < end) {
00326 c = str[i];
00327 switch (c) {
00328 case '"' :
00329 case '\\' :
00330 break;
00331 case 'r' :
00332 c = '\r';
00333 break;
00334 case 'n' :
00335 c = '\n';
00336 break;
00337 default :
00338 c = '\\';
00339 i--;
00340 break;
00341 }
00342 }
00343 }
00344 output.append(c);
00345 }
00346
00347 return output.toString();
00348 }
00349 }