Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) OSGi Alliance (2004, 2012). All Rights Reserved.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.osgi.service.condpermadmin;
 
 import java.util.List;

Condition representation used by the Conditional Permission Admin service.

This class encapsulates two pieces of information: a Condition type (class name), which must implement Condition, and the arguments passed to its constructor.

In order for a Condition represented by a ConditionInfo to be instantiated and considered during a permission check, its Condition class must be available from the system classpath.

The Condition class must either:

  • Declare a public static getCondition method that takes a Bundle object and a ConditionInfo object as arguments. That method must return an object that implements the Condition interface.
  • Implement the Condition interface and define a public constructor that takes a Bundle object and a ConditionInfo object as arguments.

Version:
$Id: c2497fefba5a0011a141f9548c3d383b34d3dc8d $
Immutable:
 
 public class ConditionInfo {
 	private final String	type;
 	private final String[]	args;

Constructs a ConditionInfo from the specified type and args.

Parameters:
type The fully qualified class name of the Condition represented by this ConditionInfo.
args The arguments for the Condition. These arguments are available to the newly created Condition by calling the getArgs() method.
Throws:
java.lang.NullPointerException If type is null.
 
 	public ConditionInfo(String typeString[] args) {
 		this. = type;
 		this. = (args != null) ? args.clone() : new String[0];
 		if (type == null) {
 			throw new NullPointerException("type is null");
 		}
 	}

Constructs a ConditionInfo object from the specified encoded ConditionInfo string. White space in the encoded ConditionInfo string is ignored.

Parameters:
encodedCondition The encoded ConditionInfo.
Throws:
java.lang.IllegalArgumentException If the specified encodedCondition is not properly formatted.
See also:
getEncoded()
 
 	public ConditionInfo(String encodedCondition) {
 		if (encodedCondition == null) {
 			throw new NullPointerException("missing encoded condition");
 		}
 		if (encodedCondition.length() == 0) {
 			throw new IllegalArgumentException("empty encoded condition");
 		}
 		try {
 			char[] encoded = encodedCondition.toCharArray();
 			int length = encoded.length;
 			int pos = 0;
 
 			/* skip whitespace */
 			while (Character.isWhitespace(encoded[pos])) {
 				pos++;
 			}
 
 			/* the first character must be '[' */
			if (encoded[pos] != '[') {
				throw new IllegalArgumentException("expecting open bracket");
			}
			pos++;
			/* skip whitespace */
			while (Character.isWhitespace(encoded[pos])) {
				pos++;
			}
			/* type is not quoted or encoded */
			int begin = pos;
			while (!Character.isWhitespace(encoded[pos]) && (encoded[pos] != ']')) {
				pos++;
			}
			if (pos == begin || encoded[begin] == '"') {
				throw new IllegalArgumentException("expecting type");
			}
			this. = new String(encodedbeginpos - begin);
			/* skip whitespace */
			while (Character.isWhitespace(encoded[pos])) {
				pos++;
			}
			/* type may be followed by args which are quoted and encoded */
			List<StringargsList = new ArrayList<String>();
			while (encoded[pos] == '"') {
				pos++;
				begin = pos;
				while (encoded[pos] != '"') {
					if (encoded[pos] == '\\') {
						pos++;
					}
					pos++;
				}
				argsList.add(unescapeString(encodedbeginpos));
				pos++;
				if (Character.isWhitespace(encoded[pos])) {
					/* skip whitespace */
					while (Character.isWhitespace(encoded[pos])) {
						pos++;
					}
				}
			}
			this. = argsList.toArray(new String[argsList.size()]);
			/* the final character must be ']' */
			char c = encoded[pos];
			pos++;
			while ((pos < length) && Character.isWhitespace(encoded[pos])) {
				pos++;
			}
			if ((c != ']') || (pos != length)) {
				throw new IllegalArgumentException("expecting close bracket");
			}
			throw new IllegalArgumentException("parsing terminated abruptly");
		}
	}

Returns the string encoding of this ConditionInfo in a form suitable for restoring this ConditionInfo.

The encoded format is:

   [type "arg0" "arg1" ...]
 
where argN are strings that must be encoded for proper parsing. Specifically, the &quot;, \, carriage return, and line feed characters must be escaped using \&quot;, \\, \r, and \n, respectively.

The encoded string contains no leading or trailing whitespace characters. A single space character is used between type and "arg0" and between the arguments.

Returns:
The string encoding of this ConditionInfo.
	public final String getEncoded() {
		StringBuffer output = new StringBuffer();
		output.append('[');
		output.append();
		for (int i = 0; i < .i++) {
			output.append(" \"");
			escapeString([i], output);
			output.append('\"');
		}
		output.append(']');
		return output.toString();
	}

Returns the string representation of this ConditionInfo. The string is created by calling the getEncoded method on this ConditionInfo.

Returns:
The string representation of this ConditionInfo.
	public String toString() {
		return getEncoded();
	}

Returns the fully qualified class name of the condition represented by this ConditionInfo.

Returns:
The fully qualified class name of the condition represented by this ConditionInfo.
	public final String getType() {
		return ;
	}

Returns arguments of this ConditionInfo.

Returns:
The arguments of this ConditionInfo. An empty array is returned if the ConditionInfo has no arguments.
	public final String[] getArgs() {
		return .clone();
	}

Determines the equality of two ConditionInfo objects. This method checks that specified object has the same type and args as this ConditionInfo object.

Parameters:
obj The object to test for equality with this ConditionInfo object.
Returns:
true if obj is a ConditionInfo, and has the same type and args as this ConditionInfo object; false otherwise.
	public boolean equals(Object obj) {
		if (obj == this) {
			return true;
		}
		if (!(obj instanceof ConditionInfo)) {
			return false;
		}
		ConditionInfo other = (ConditionInfoobj;
		if (!.equals(other.type) || . != other.args.length)
			return false;
		for (int i = 0; i < .i++) {
			if (![i].equals(other.args[i]))
				return false;
		}
		return true;
	}

Returns the hash code value for this object.

Returns:
A hash code value for this object.
	public int hashCode() {
		int h = 31 * 17 + .hashCode();
		for (int i = 0; i < .i++) {
			h = 31 * h + [i].hashCode();
		}
		return h;
	}

This escapes the quotes, backslashes, \n, and \r in the string using a backslash and appends the newly escaped string to a StringBuffer.
	private static void escapeString(String strStringBuffer output) {
		int len = str.length();
		for (int i = 0; i < leni++) {
			char c = str.charAt(i);
			switch (c) {
				case '"' :
				case '\\' :
					output.append('\\');
					output.append(c);
					break;
				case '\r' :
					output.append("\\r");
					break;
				case '\n' :
					output.append("\\n");
					break;
				default :
					output.append(c);
					break;
			}
		}
	}

Takes an encoded character array and decodes it into a new String.
	private static String unescapeString(char[] strint beginint end) {
		StringBuffer output = new StringBuffer(end - begin);
		for (int i = begini < endi++) {
			char c = str[i];
			if (c == '\\') {
				i++;
				if (i < end) {
					c = str[i];
					switch (c) {
						case '"' :
						case '\\' :
							break;
						case 'r' :
							c = '\r';
							break;
						case 'n' :
							c = '\n';
							break;
						default :
							c = '\\';
							i--;
							break;
					}
				}
			}
			output.append(c);
		}
		return output.toString();
	}
New to GrepCode? Check out our FAQ X