00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 package org.osgi.service.permissionadmin;
00020
00040 public class PermissionInfo {
00041 private String type;
00042 private String name;
00043 private String actions;
00044
00068 public PermissionInfo(String type, String name, String actions) {
00069 this.type = type;
00070 this.name = name;
00071 this.actions = actions;
00072 if (type == null) {
00073 throw new NullPointerException("type is null");
00074 }
00075 if ((name == null) && (actions != null)) {
00076 throw new IllegalArgumentException("name missing");
00077 }
00078 }
00079
00091 public PermissionInfo(String encodedPermission) {
00092 if (encodedPermission == null) {
00093 throw new NullPointerException("missing encoded permission");
00094 }
00095 if (encodedPermission.length() == 0) {
00096 throw new IllegalArgumentException("empty encoded permission");
00097 }
00098 try {
00099 char[] encoded = encodedPermission.toCharArray();
00100 int length = encoded.length;
00101 int pos = 0;
00102
00103
00104 while (Character.isWhitespace(encoded[pos])) {
00105 pos++;
00106 }
00107
00108
00109 if (encoded[pos] != '(') {
00110 throw new IllegalArgumentException(
00111 "expecting open parenthesis");
00112 }
00113 pos++;
00114
00115
00116 while (Character.isWhitespace(encoded[pos])) {
00117 pos++;
00118 }
00119
00120
00121 int begin = pos;
00122 while (!Character.isWhitespace(encoded[pos]) && (encoded[pos] != ')')) {
00123 pos++;
00124 }
00125 if (pos == begin || encoded[begin] == '"') {
00126 throw new IllegalArgumentException("expecting type");
00127 }
00128 this.type = new String(encoded, begin, pos - begin);
00129
00130
00131 while (Character.isWhitespace(encoded[pos])) {
00132 pos++;
00133 }
00134
00135
00136 if (encoded[pos] == '"') {
00137 pos++;
00138 begin = pos;
00139 while (encoded[pos] != '"') {
00140 if (encoded[pos] == '\\') {
00141 pos++;
00142 }
00143 pos++;
00144 }
00145 this.name = unescapeString(encoded, begin, pos);
00146 pos++;
00147
00148 if (Character.isWhitespace(encoded[pos])) {
00149
00150 while (Character.isWhitespace(encoded[pos])) {
00151 pos++;
00152 }
00153
00154
00155 if (encoded[pos] == '"') {
00156 pos++;
00157 begin = pos;
00158 while (encoded[pos] != '"') {
00159 if (encoded[pos] == '\\') {
00160 pos++;
00161 }
00162 pos++;
00163 }
00164 this.actions = unescapeString(encoded, begin, pos);
00165 pos++;
00166
00167
00168 while (Character.isWhitespace(encoded[pos])) {
00169 pos++;
00170 }
00171 }
00172 }
00173 }
00174
00175
00176 char c = encoded[pos];
00177 pos++;
00178 while ((pos < length) && Character.isWhitespace(encoded[pos])) {
00179 pos++;
00180 }
00181 if ((c != ')') || (pos != length)) {
00182 throw new IllegalArgumentException("expecting close parenthesis");
00183 }
00184 }
00185 catch (ArrayIndexOutOfBoundsException e) {
00186 throw new IllegalArgumentException("parsing terminated abruptly");
00187 }
00188 }
00189
00225 public final String getEncoded() {
00226 StringBuffer output = new StringBuffer(
00227 8
00228 + type.length()
00229 + ((((name == null) ? 0 : name.length()) + ((actions == null) ? 0
00230 : actions.length())) << 1));
00231 output.append('(');
00232 output.append(type);
00233 if (name != null) {
00234 output.append(" \"");
00235 escapeString(name, output);
00236 if (actions != null) {
00237 output.append("\" \"");
00238 escapeString(actions, output);
00239 }
00240 output.append('\"');
00241 }
00242 output.append(')');
00243 return output.toString();
00244 }
00245
00253 public String toString() {
00254 return getEncoded();
00255 }
00256
00264 public final String getType() {
00265 return type;
00266 }
00267
00276 public final String getName() {
00277 return name;
00278 }
00279
00288 public final String getActions() {
00289 return actions;
00290 }
00291
00304 public boolean equals(Object obj) {
00305 if (obj == this) {
00306 return true;
00307 }
00308 if (!(obj instanceof PermissionInfo)) {
00309 return false;
00310 }
00311 PermissionInfo other = (PermissionInfo) obj;
00312 if (!type.equals(other.type) || ((name == null) ^ (other.name == null))
00313 || ((actions == null) ^ (other.actions == null))) {
00314 return false;
00315 }
00316 if (name != null) {
00317 if (actions != null) {
00318 return name.equals(other.name) && actions
00319 .equals(other.actions);
00320 }
00321 else {
00322 return name.equals(other.name);
00323 }
00324 }
00325 else {
00326 return true;
00327 }
00328 }
00329
00335 public int hashCode() {
00336 int hash = type.hashCode();
00337 if (name != null) {
00338 hash ^= name.hashCode();
00339 if (actions != null) {
00340 hash ^= actions.hashCode();
00341 }
00342 }
00343 return hash;
00344 }
00345
00350 private static void escapeString(String str, StringBuffer output) {
00351 int len = str.length();
00352 for (int i = 0; i < len; i++) {
00353 char c = str.charAt(i);
00354 switch (c) {
00355 case '"' :
00356 case '\\' :
00357 output.append('\\');
00358 output.append(c);
00359 break;
00360 case '\r' :
00361 output.append("\\r");
00362 break;
00363 case '\n' :
00364 output.append("\\n");
00365 break;
00366 default :
00367 output.append(c);
00368 break;
00369 }
00370 }
00371 }
00372
00376 private static String unescapeString(char[] str, int begin, int end) {
00377 StringBuffer output = new StringBuffer(end - begin);
00378 for (int i = begin; i < end; i++) {
00379 char c = str[i];
00380 if (c == '\\') {
00381 i++;
00382 if (i < end) {
00383 c = str[i];
00384 switch (c) {
00385 case '"' :
00386 case '\\' :
00387 break;
00388 case 'r' :
00389 c = '\r';
00390 break;
00391 case 'n' :
00392 c = '\n';
00393 break;
00394 default :
00395 c = '\\';
00396 i--;
00397 break;
00398 }
00399 }
00400 }
00401 output.append(c);
00402 }
00403
00404 return output.toString();
00405 }
00406 }