Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   /*
    * Copyright (c) OSGi Alliance (2005, 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.framework;
  
  import java.util.List;
  import java.util.Map;
  import java.util.Set;
Framework Utility class.

This class contains utility methods which access Framework functions that may be useful to bundles.

Version:
$Id: 1f46ea2bbbe2a1242fdaf0877709fb9c02eefae1 $
Since:
1.3
ThreadSafe:
  
  public class FrameworkUtil {
FrameworkUtil objects may not be constructed.
  
  	private FrameworkUtil() {
  		// private empty constructor to prevent construction
  	}

Creates a Filter object. This Filter object may be used to match a ServiceReference object or a Dictionary object.

If the filter cannot be parsed, an InvalidSyntaxException will be thrown with a human readable message where the filter became unparsable.

This method returns a Filter implementation which may not perform as well as the framework implementation-specific Filter implementation returned by BundleContext.createFilter(java.lang.String).

Parameters:
filter The filter string.
Returns:
A Filter object encapsulating the filter string.
Throws:
InvalidSyntaxException If filter contains an invalid filter string that cannot be parsed.
java.lang.NullPointerException If filter is null.
See also:
Filter
  
  	public static Filter createFilter(String filterthrows InvalidSyntaxException {
  		return FilterImpl.newInstance(filter);
  	}

Match a Distinguished Name (DN) chain against a pattern. DNs can be matched using wildcards. A wildcard ('*' \u002A) replaces all possible values. Due to the structure of the DN, the comparison is more complicated than string-based wildcard matching.

A wildcard can stand for zero or more DNs in a chain, a number of relative distinguished names (RDNs) within a DN, or the value of a single RDN. The DNs in the chain and the matching pattern are canonicalized before processing. This means, among other things, that spaces must be ignored, except in values.

The format of a wildcard match pattern is:

 matchPattern ::= dn-match ( ';' dn-match ) *
 dn-match     ::= ( '*' | rdn-match ) ( ',' rdn-match ) * | '-'
 rdn-match    ::= name '=' value-match
 value-match  ::= '*' | value-star
 value-star   ::= < value, requires escaped '*' and '-' >
 

The most simple case is a single wildcard; it must match any DN. A wildcard can also replace the first list of RDNs of a DN. The first RDNs are the least significant. Such lists of matched RDNs can be empty.

For example, a match pattern with a wildcard that matches all DNs that end with RDNs of o=ACME and c=US would look like this:

 *, o=ACME, c=US
 
This match pattern would match the following DNs:
 cn = Bugs Bunny, o = ACME, c = US
 ou = Carrots, cn=Daffy Duck, o=ACME, c=US
 street = 9C\, Avenue St. Drézéry, o=ACME, c=US
 dc=www, dc=acme, dc=com, o=ACME, c=US
 o=ACME, c=US
 
The following DNs would not match:
 street = 9C\, Avenue St. Drézéry, o=ACME, c=FR
 dc=www, dc=acme, dc=com, c=US
 
If a wildcard is used for a value of an RDN, the value must be exactly *. The wildcard must match any value, and no substring matching must be done. For example:
 cn=*,o=ACME,c=*
 
This match pattern with wildcard must match the following DNs:
 cn=Bugs Bunny,o=ACME,c=US
 cn = Daffy Duck , o = ACME , c = US
 cn=Road Runner, o=ACME, c=NL
 
But not:
 o=ACME, c=NL
 dc=acme.com, cn=Bugs Bunny, o=ACME, c=US
 

A match pattern may contain a chain of DN match patterns. The semicolon( ';' \u003B) must be used to separate DN match patterns in a chain. Wildcards can also be used to match against a complete DN within a chain.

The following example matches a certificate signed by Tweety Inc. in the US.

 * ; ou=S & V, o=Tweety Inc., c=US
 

The wildcard ('*') matches zero or one DN in the chain, however, sometimes it is necessary to match a longer chain. The minus sign ( '-' \u002D) represents zero or more DNs, whereas the asterisk only represents a single DN. For example, to match a DN where the Tweety Inc. is in the DN chain, use the following expression:

 - ; *, o=Tweety Inc., c=US
 

Parameters:
matchPattern The pattern against which to match the DN chain.
dnChain The DN chain to match against the specified pattern. Each element of the chain must be of type String and use the format defined in RFC 2253.
Returns:
true If the pattern matches the DN chain; otherwise false is returned.
Throws:
java.lang.IllegalArgumentException If the specified match pattern or DN chain is invalid.
Since:
1.5
 
 	public static boolean matchDistinguishedNameChain(String matchPatternList<StringdnChain) {
 		return DNChainMatching.match(matchPatterndnChain);
 	}

Return a Bundle for the specified bundle class. The returned Bundle is the bundle associated with the bundle class loader which defined the specified class.

Parameters:
classFromBundle A class defined by a bundle class loader.
Returns:
A Bundle for the specified bundle class or null if the specified class was not defined by a bundle class loader.
Since:
1.5
 
 	public static Bundle getBundle(final Class<?> classFromBundle) {
 		// We use doPriv since the caller may not have permission
 		// to call getClassLoader.
 		Object cl = AccessController.doPrivileged(new PrivilegedAction<Object>() {
 			public Object run() {
 				return classFromBundle.getClassLoader();
 			}
 		});
 
 		if (cl instanceof BundleReference) {
 			return ((BundleReferencecl).getBundle();
 		}
 		return null;
 	}

RFC 1960-based Filter. Filter objects can be created by calling the constructor with the desired filter string. A Filter object can be called numerous times to determine if the match argument matches the filter string that was used to create the Filter object.

The syntax of a filter string is the string representation of LDAP search filters as defined in RFC 1960: A String Representation of LDAP Search Filters (available at http://www.ietf.org/rfc/rfc1960.txt). It should be noted that RFC 2254: A String Representation of LDAP Search Filters (available at http://www.ietf.org/rfc/rfc2254.txt) supersedes RFC 1960 but only adds extensible matching and is not applicable for this API.

The string representation of an LDAP search filter is defined by the following grammar. It uses a prefix format.

   <filter> ::= '(' <filtercomp> ')'
   <filtercomp> ::= <and> | <or> | <not> | <item>
   <and> ::= '&' <filterlist>
   <or> ::= '|' <filterlist>
   <not> ::= '!' <filter>
   <filterlist> ::= <filter> | <filter> <filterlist>
   <item> ::= <simple> | <present> | <substring>
   <simple> ::= <attr> <filtertype> <value>
   <filtertype> ::= <equal> | <approx> | <greater> | <less>
   <equal> ::= '='
   <approx> ::= '˜='
   <greater> ::= '>='
   <less> ::= '<='
   <present> ::= <attr> '=*'
   <substring> ::= <attr> '=' <initial> <any> <final>
   <initial> ::= NULL | <value>
   <any> ::= '*' <starval>
   <starval> ::= NULL | <value> '*' <starval>
   <final> ::= NULL | <value>
 
&lt;attr&gt; is a string representing an attribute, or key, in the properties objects of the registered services. Attribute names are not case sensitive; that is cn and CN both refer to the same attribute. &lt;value&gt; is a string representing the value, or part of one, of a key in the properties objects of the registered services. If a &lt;value&gt; must contain one of the characters ' *' or '(' or ')', these characters should be escaped by preceding them with the backslash '\' character. Note that although both the &lt;substring&gt; and &lt;present&gt; productions can produce the 'attr=*' construct, this construct is used only to denote a presence filter.

Examples of LDAP filters are:

   "(cn=Babs Jensen)"
   "(!(cn=Tim Howes))"
   "(&(" + Constants.OBJECTCLASS + "=Person)(|(sn=Jensen)(cn=Babs J*)))"
   "(o=univ*of*mich*)"
 

The approximate match (~=) is implementation specific but should at least ignore case and white space differences. Optional are codes like soundex or other smart "closeness" comparisons.

Comparison of values is not straightforward. Strings are compared differently than numbers and it is possible for a key to have multiple values. Note that that keys in the match argument must always be strings. The comparison is defined by the object type of the key's value. The following rules apply for comparison:

Property Value Type Comparison Type
StringString comparison
Integer, Long, Float, Double, Byte, Short, BigInteger, BigDecimalnumerical comparison
Charactercharacter comparison
Booleanequality comparisons only
[] (array)recursively applied to values
Collectionrecursively applied to values
Note: arrays of primitives are also supported.
A filter matches a key that has multiple values if it matches at least one of those values. For example,
 Dictionary d = new Hashtable();
 d.put("cn", new String[] {"a", "b", "c"});
 
d will match (cn=a) and also (cn=b)

A filter component that references a key having an unrecognizable data type will evaluate to false .

 
 	static private final class FilterImpl implements Filter {
 		/* filter operators */
 		private static final int	EQUAL		= 1;
 		private static final int	APPROX		= 2;
 		private static final int	GREATER		= 3;
 		private static final int	LESS		= 4;
 		private static final int	PRESENT		= 5;
 		private static final int	SUBSTRING	= 6;
 		private static final int	AND			= 7;
 		private static final int	OR			= 8;
 		private static final int	NOT			= 9;

filter operation
 
 		private final int			op;
filter attribute or null if operation AND, OR or NOT
 
 		private final String		attr;
filter operands
 
 		private final Object		value;
 
 		/* normalized filter string for Filter object */
 		private transient String	filterString;

Constructs a FrameworkUtil.FilterImpl object. This filter object may be used to match a ServiceReference or a Dictionary.

If the filter cannot be parsed, an InvalidSyntaxException will be thrown with a human readable message where the filter became unparsable.

Parameters:
filterString the filter string.
Throws:
InvalidSyntaxException If the filter parameter contains an invalid filter string that cannot be parsed.
 
 		static FilterImpl newInstance(String filterStringthrows InvalidSyntaxException {
 			return new Parser(filterString).parse();
 		}
 
 		FilterImpl(int operationString attrObject value) {
 			this. = operation;
 			this. = attr;
 			this. = value;
 			 = null;
 		}

Filter using a service's properties.

This Filter is executed using the keys and values of the referenced service's properties. The keys are looked up in a case insensitive manner.

Parameters:
reference The reference to the service whose properties are used in the match.
Returns:
true if the service's properties match this Filter; false otherwise.
 
 		public boolean match(ServiceReference<?> reference) {
 			return matches(new ServiceReferenceMap(reference));
 		}

Filter using a Dictionary with case insensitive key lookup. This Filter is executed using the specified Dictionary's keys and values. The keys are looked up in a case insensitive manner.

Parameters:
dictionary The Dictionary whose key/value pairs are used in the match.
Returns:
true if the Dictionary's values match this filter; false otherwise.
Throws:
java.lang.IllegalArgumentException If dictionary contains case variants of the same key name.
 
 		public boolean match(Dictionary<String, ?> dictionary) {
 			return matches(new CaseInsensitiveMap(dictionary));
 		}

Filter using a Dictionary. This Filter is executed using the specified Dictionary's keys and values. The keys are looked up in a normal manner respecting case.

Parameters:
dictionary The Dictionary whose key/value pairs are used in the match.
Returns:
true if the Dictionary's values match this filter; false otherwise.
Since:
1.3
 
 		public boolean matchCase(Dictionary<String, ?> dictionary) {
 			switch () {
 				case  : {
 					FilterImpl[] filters = (FilterImpl[]) ;
 					for (FilterImpl f : filters) {
 						if (!f.matchCase(dictionary)) {
 							return false;
 						}
 					}
 					return true;
 				}
 
 				case  : {
 					FilterImpl[] filters = (FilterImpl[]) ;
 					for (FilterImpl f : filters) {
 						if (f.matchCase(dictionary)) {
 							return true;
 						}
 					}
 					return false;
 				}
 
 				case  : {
 					FilterImpl filter = (FilterImpl;
 					return !filter.matchCase(dictionary);
 				}
 
 				case  :
 				case  :
 				case  :
 				case  :
 				case  : {
 					Object prop = (dictionary == null) ? null : dictionary.get();
 					return compare(prop);
 				}
 
 				case  : {
 					Object prop = (dictionary == null) ? null : dictionary.get();
 					return prop != null;
 				}
 			}
 
 			return false;
 		}

Filter using a Map. This Filter is executed using the specified Map's keys and values. The keys are looked up in a normal manner respecting case.

Parameters:
map The Map whose key/value pairs are used in the match. Maps with null key or values are not supported. A null value is considered not present to the filter.
Returns:
true if the Map's values match this filter; false otherwise.
Since:
1.6
 
 		public boolean matches(Map<String, ?> map) {
 			switch () {
 				case  : {
 					FilterImpl[] filters = (FilterImpl[]) ;
 					for (FilterImpl f : filters) {
 						if (!f.matches(map)) {
 							return false;
 						}
 					}
 					return true;
 				}
 
 				case  : {
 					FilterImpl[] filters = (FilterImpl[]) ;
 					for (FilterImpl f : filters) {
 						if (f.matches(map)) {
 							return true;
 						}
 					}
 					return false;
 				}
 
 				case  : {
 					FilterImpl filter = (FilterImpl;
 					return !filter.matches(map);
 				}
 
 				case  :
 				case  :
 				case  :
 				case  :
 				case  : {
 					Object prop = (map == null) ? null : map.get();
 					return compare(prop);
 				}
 
 				case  : {
 					Object prop = (map == null) ? null : map.get();
 					return prop != null;
 				}
 			}
 
 			return false;
 		}

Returns this Filter's filter string.

The filter string is normalized by removing whitespace which does not affect the meaning of the filter.

Returns:
This Filter's filter string.
 
 		public String toString() {
 			String result = ;
 			if (result == null) {
 				 = result = normalize().toString();
 			}
 			return result;
 		}

Returns this Filter's normalized filter string.

The filter string is normalized by removing whitespace which does not affect the meaning of the filter.

Returns:
This Filter's filter string.
 
 		private StringBuffer normalize() {
 			StringBuffer sb = new StringBuffer();
 			sb.append('(');
 
 			switch () {
 				case  : {
 					sb.append('&');
 
 					FilterImpl[] filters = (FilterImpl[]) ;
 					for (FilterImpl f : filters) {
 						sb.append(f.normalize());
 					}
 
 					break;
 				}
 
 				case  : {
 					sb.append('|');
 
 					FilterImpl[] filters = (FilterImpl[]) ;
 					for (FilterImpl f : filters) {
 						sb.append(f.normalize());
 					}
 
 					break;
 				}
 
 				case  : {
 					sb.append('!');
 					FilterImpl filter = (FilterImpl;
 					sb.append(filter.normalize());
 
 					break;
 				}
 
 				case  : {
 					sb.append();
 					sb.append('=');
 
 					String[] substrings = (String[]) ;
 
 					for (String substr : substrings) {
 						if (substr == null/* * */{
 							sb.append('*');
 						} else /* xxx */{
 							sb.append(encodeValue(substr));
 						}
 					}
 
 					break;
 				}
 				case  : {
 					sb.append();
 					sb.append('=');
 
 					break;
 				}
 				case  : {
 					sb.append();
 					sb.append(">=");
 
 					break;
 				}
 				case  : {
 					sb.append();
 					sb.append("<=");
 
 					break;
 				}
 				case  : {
 					sb.append();
 					sb.append("~=");
 
 					break;
 				}
 
 				case  : {
 					sb.append();
 					sb.append("=*");
 
 					break;
 				}
 			}
 
 			sb.append(')');
 
 			return sb;
 		}

Compares this Filter to another Filter.

This implementation returns the result of calling this.toString().equals(obj.toString().

Parameters:
obj The object to compare against this Filter.
Returns:
If the other object is a Filter object, then returns the result of calling this.toString().equals(obj.toString(); false otherwise.
 
 		public boolean equals(Object obj) {
 			if (obj == this) {
 				return true;
 			}
 
 			if (!(obj instanceof Filter)) {
 				return false;
 			}
 
 			return this.toString().equals(obj.toString());
 		}

Returns the hashCode for this Filter.

This implementation returns the result of calling this.toString().hashCode().

Returns:
The hashCode of this Filter.
 
 		public int hashCode() {
 			return this.toString().hashCode();
 		}

Encode the value string such that '(', '*', ')' and '\' are escaped.

Parameters:
value unencoded value string.
Returns:
encoded value string.
 
 		private static String encodeValue(String value) {
 			boolean encoded = false;
 			int inlen = value.length();
 			int outlen = inlen << 1; /* inlen 2 */
 
 			char[] output = new char[outlen];
 			value.getChars(0, inlenoutputinlen);
 
 			int cursor = 0;
 			for (int i = inleni < outleni++) {
 				char c = output[i];
 
 				switch (c) {
 					case '(' :
 					case '*' :
 					case ')' :
 					case '\\' : {
 						output[cursor] = '\\';
 						cursor++;
 						encoded = true;
 
 						break;
 					}
 				}
 
 				output[cursor] = c;
 				cursor++;
 			}
 
 			return encoded ? new String(output, 0, cursor) : value;
 		}
 
 		private boolean compare(int operationObject value1Object value2) {
 			if (value1 == null) {
 				return false;
 			}
 			if (value1 instanceof String) {
 				return compare_String(operation, (Stringvalue1value2);
 			}
 
 			Class<?> clazz = value1.getClass();
 			if (clazz.isArray()) {
 				Class<?> type = clazz.getComponentType();
 				if (type.isPrimitive()) {
 					return compare_PrimitiveArray(operationtypevalue1value2);
 				}
 				return compare_ObjectArray(operation, (Object[]) value1value2);
 			}
 			if (value1 instanceof Collection<?>) {
 				return compare_Collection(operation, (Collection<?>) value1value2);
 			}
 			if (value1 instanceof Integer) {
 				return compare_Integer(operation, ((Integervalue1).intValue(), value2);
 			}
 			if (value1 instanceof Long) {
 				return compare_Long(operation, ((Longvalue1).longValue(), value2);
 			}
 			if (value1 instanceof Byte) {
 				return compare_Byte(operation, ((Bytevalue1).byteValue(), value2);
 			}
 			if (value1 instanceof Short) {
 				return compare_Short(operation, ((Shortvalue1).shortValue(), value2);
 			}
 			if (value1 instanceof Character) {
 				return compare_Character(operation, ((Charactervalue1).charValue(), value2);
 			}
 			if (value1 instanceof Float) {
 				return compare_Float(operation, ((Floatvalue1).floatValue(), value2);
 			}
 			if (value1 instanceof Double) {
 				return compare_Double(operation, ((Doublevalue1).doubleValue(), value2);
 			}
 			if (value1 instanceof Boolean) {
 				return compare_Boolean(operation, ((Booleanvalue1).booleanValue(), value2);
 			}
 			if (value1 instanceof Comparable<?>) {
 				Comparable<Objectcomparable = (Comparable<Object>) value1;
 				return compare_Comparable(operationcomparablevalue2);
 			}
 			return compare_Unknown(operationvalue1value2);
 		}
 
 		private boolean compare_Collection(int operationCollection<?> collectionObject value2) {
 			for (Object value1 : collection) {
 				if (compare(operationvalue1value2)) {
 					return true;
 				}
 			}
 			return false;
 		}
 
 		private boolean compare_ObjectArray(int operationObject[] arrayObject value2) {
 			for (Object value1 : array) {
 				if (compare(operationvalue1value2)) {
 					return true;
 				}
 			}
 			return false;
 		}
 
 		private boolean compare_PrimitiveArray(int operationClass<?> typeObject primarrayObject value2) {
 			if (..isAssignableFrom(type)) {
 				int[] array = (int[]) primarray;
 				for (int value1 : array) {
 					if (compare_Integer(operationvalue1value2)) {
 						return true;
 					}
 				}
 				return false;
 			}
 			if (..isAssignableFrom(type)) {
 				long[] array = (long[]) primarray;
 				for (long value1 : array) {
 					if (compare_Long(operationvalue1value2)) {
 						return true;
 					}
 				}
 				return false;
 			}
 			if (..isAssignableFrom(type)) {
 				byte[] array = (byte[]) primarray;
 				for (byte value1 : array) {
 					if (compare_Byte(operationvalue1value2)) {
 						return true;
 					}
 				}
 				return false;
 			}
 			if (..isAssignableFrom(type)) {
 				short[] array = (short[]) primarray;
 				for (short value1 : array) {
 					if (compare_Short(operationvalue1value2)) {
 						return true;
 					}
 				}
 				return false;
 			}
 				char[] array = (char[]) primarray;
 				for (char value1 : array) {
 					if (compare_Character(operationvalue1value2)) {
 						return true;
 					}
 				}
 				return false;
 			}
 			if (..isAssignableFrom(type)) {
 				float[] array = (float[]) primarray;
 				for (float value1 : array) {
 					if (compare_Float(operationvalue1value2)) {
 						return true;
 					}
 				}
 				return false;
 			}
 			if (..isAssignableFrom(type)) {
 				double[] array = (double[]) primarray;
 				for (double value1 : array) {
 					if (compare_Double(operationvalue1value2)) {
 						return true;
 					}
 				}
 				return false;
 			}
 			if (..isAssignableFrom(type)) {
 				boolean[] array = (boolean[]) primarray;
 				for (boolean value1 : array) {
 					if (compare_Boolean(operationvalue1value2)) {
 						return true;
 					}
 				}
 				return false;
 			}
 			return false;
 		}
 
 		private boolean compare_String(int operationString stringObject value2) {
 			switch (operation) {
 				case  : {
 					String[] substrings = (String[]) value2;
 					int pos = 0;
 					for (int i = 0, size = substrings.lengthi < sizei++) {
 						String substr = substrings[i];
 
 						if (i + 1 < size/* if this is not that last substr */{
 							if (substr == null/* * */{
 								String substr2 = substrings[i + 1];
 
 								if (substr2 == null/* ** */
 									continue/* ignore first star */
 								/* xxx */
 								int index = string.indexOf(substr2pos);
 								if (index == -1) {
 									return false;
 								}
 
 								pos = index + substr2.length();
 								if (i + 2 < size// if there are more
 									// substrings, increment
 									// over the string we just
 									// matched; otherwise need
 									// to do the last substr
 									// check
 									i++;
 							} else /* xxx */{
 								int len = substr.length();
 								if (string.regionMatches(possubstr, 0, len)) {
 									pos += len;
 								} else {
 									return false;
 								}
 							}
 						} else /* last substr */{
 							if (substr == null/* * */{
 								return true;
 							}
 							/* xxx */
 							return string.endsWith(substr);
 						}
 					}
 
 					return true;
 				}
 				case  : {
 					return string.equals(value2);
 				}
 				case  : {
 					string = approxString(string);
 					String string2 = approxString((Stringvalue2);
 
 					return string.equalsIgnoreCase(string2);
 				}
 				case  : {
 					return string.compareTo((Stringvalue2) >= 0;
 				}
 				case  : {
 					return string.compareTo((Stringvalue2) <= 0;
 				}
 			}
 			return false;
 		}
 
 		private boolean compare_Integer(int operationint intvalObject value2) {
 			if (operation == ) {
 				return false;
 			}
 			int intval2;
 			try {
 				intval2 = Integer.parseInt(((Stringvalue2).trim());
 			} catch (IllegalArgumentException e) {
 				return false;
 			}
 			switch (operation) {
 				case  :
 				case  : {
 					return intval == intval2;
 				}
 				case  : {
 					return intval >= intval2;
 				}
 				case  : {
 					return intval <= intval2;
 				}
 			}
 			return false;
 		}
 
 		private boolean compare_Long(int operationlong longvalObject value2) {
 			if (operation == ) {
 				return false;
 			}
 			long longval2;
 			try {
 				longval2 = Long.parseLong(((Stringvalue2).trim());
 			} catch (IllegalArgumentException e) {
 				return false;
 			}
 
 			switch (operation) {
 				case  :
 				case  : {
 					return longval == longval2;
 				}
 				case  : {
 					return longval >= longval2;
 				}
 				case  : {
 					return longval <= longval2;
 				}
 			}
 			return false;
 		}
 
 		private boolean compare_Byte(int operationbyte bytevalObject value2) {
 			if (operation == ) {
 				return false;
 			}
 			byte byteval2;
 			try {
 				byteval2 = Byte.parseByte(((Stringvalue2).trim());
 			} catch (IllegalArgumentException e) {
 				return false;
 			}
 
 			switch (operation) {
				case  :
				case  : {
					return byteval == byteval2;
				case  : {
					return byteval >= byteval2;
				case  : {
					return byteval <= byteval2;
			return false;
		private boolean compare_Short(int operationshort shortvalObject value2) {
			if (operation == ) {
				return false;
			short shortval2;
			try {
				shortval2 = Short.parseShort(((Stringvalue2).trim());
catch (IllegalArgumentException e) {
				return false;
			switch (operation) {
				case  :
				case  : {
					return shortval == shortval2;
				case  : {
					return shortval >= shortval2;
				case  : {
					return shortval <= shortval2;
			return false;
		private boolean compare_Character(int operationchar charvalObject value2) {
			if (operation == ) {
				return false;
			char charval2;
			try {
				charval2 = ((Stringvalue2).charAt(0);
				return false;
			switch (operation) {
				case  : {
					return charval == charval2;
				case  : {
					return (charval == charval2) || (Character.toUpperCase(charval) == Character.toUpperCase(charval2)) || (Character.toLowerCase(charval) == Character.toLowerCase(charval2));
				case  : {
					return charval >= charval2;
				case  : {
					return charval <= charval2;
			return false;
		private boolean compare_Boolean(int operationboolean boolvalObject value2) {
			if (operation == ) {
				return false;
			boolean boolval2 = Boolean.valueOf(((Stringvalue2).trim()).booleanValue();
			switch (operation) {
				case  :
				case  :
				case  :
				case  : {
					return boolval == boolval2;
			return false;
		private boolean compare_Float(int operationfloat floatvalObject value2) {
			if (operation == ) {
				return false;
			float floatval2;
			try {
				floatval2 = Float.parseFloat(((Stringvalue2).trim());
catch (IllegalArgumentException e) {
				return false;
			switch (operation) {
				case  :
				case  : {
					return Float.compare(floatvalfloatval2) == 0;
				case  : {
					return Float.compare(floatvalfloatval2) >= 0;
				case  : {
					return Float.compare(floatvalfloatval2) <= 0;
			return false;
		private boolean compare_Double(int operationdouble doublevalObject value2) {
			if (operation == ) {
				return false;
			double doubleval2;
			try {
				doubleval2 = Double.parseDouble(((Stringvalue2).trim());
catch (IllegalArgumentException e) {
				return false;
			switch (operation) {
				case  :
				case  : {
					return Double.compare(doublevaldoubleval2) == 0;
				case  : {
					return Double.compare(doublevaldoubleval2) >= 0;
				case  : {
					return Double.compare(doublevaldoubleval2) <= 0;
			return false;
		private static Object valueOf(Class<?> targetString value2) {
			do {
				Method method;
				try {
					method = target.getMethod("valueOf"String.class);
catch (NoSuchMethodException e) {
					break;
				if (Modifier.isStatic(method.getModifiers()) && target.isAssignableFrom(method.getReturnType())) {
					try {
						return method.invoke(nullvalue2.trim());
catch (IllegalAccessException e) {
						return null;
						return null;
while (false);
			do {
				Constructor<?> constructor;
				try {
					constructor = target.getConstructor(String.class);
catch (NoSuchMethodException e) {
					break;
				setAccessible(constructor);
				try {
					return constructor.newInstance(value2.trim());
catch (IllegalAccessException e) {
					return null;
					return null;
catch (InstantiationException e) {
					return null;
while (false);
			return null;
		private static void setAccessible(AccessibleObject accessible) {
			if (!accessible.isAccessible()) {
				AccessController.doPrivileged(new SetAccessibleAction(accessible));
		private boolean compare_Comparable(int operationComparable<Objectvalue1Object value2) {
			if (operation == ) {
				return false;
			value2 = valueOf(value1.getClass(), (Stringvalue2);
			if (value2 == null) {
				return false;
			try {
				switch (operation) {
					case  :
					case  : {
						return value1.compareTo(value2) == 0;
					case  : {
						return value1.compareTo(value2) >= 0;
					case  : {
						return value1.compareTo(value2) <= 0;
catch (Exception e) {
				// if the compareTo method throws an exception; return false
				return false;
			return false;
		private boolean compare_Unknown(int operationObject value1Object value2) {
			if (operation == ) {
				return false;
			value2 = valueOf(value1.getClass(), (Stringvalue2);
			if (value2 == null) {
				return false;
			try {
				switch (operation) {
					case  :
					case  :
					case  :
					case  : {
						return value1.equals(value2);
catch (Exception e) {
				// if the equals method throws an exception; return false
				return false;
			return false;
		}

Map a string for an APPROX (~=) comparison. This implementation removes white spaces. This is the minimum implementation allowed by the OSGi spec.

Parameters:
input Input string.
Returns:
String ready for APPROX comparison.
		private static String approxString(String input) {
			boolean changed = false;
			char[] output = input.toCharArray();
			int cursor = 0;
			for (char c : output) {
				if (Character.isWhitespace(c)) {
					changed = true;
					continue;
				output[cursor] = c;
				cursor++;
			return changed ? new String(output, 0, cursor) : input;
		}

Parser class for OSGi filter strings. This class parses the complete filter string and builds a tree of Filter objects rooted at the parent.
		static private final class Parser {
			private final String	filterstring;
			private final char[]	filterChars;
			private int				pos;
			Parser(String filterstring) {
				this. = filterstring;
				 = filterstring.toCharArray();
				 = 0;
				FilterImpl filter;
				try {
					filter = parse_filter();
					throw new InvalidSyntaxException("Filter ended abruptly"e);
				if ( != .) {
					throw new InvalidSyntaxException("Extraneous trailing characters: " + .substring(), );
				return filter;
				FilterImpl filter;
				if ([] != '(') {
					throw new InvalidSyntaxException("Missing '(': " + .substring(), );
				filter = parse_filtercomp();
				if ([] != ')') {
					throw new InvalidSyntaxException("Missing ')': " + .substring(), );
				return filter;
				char c = [];
				switch (c) {
					case '&' : {
						return parse_and();
					case '|' : {
						return parse_or();
					case '!' : {
						return parse_not();
				return parse_item();
				int lookahead = ;
				if ([] != '(') {
					 = lookahead - 1;
					return parse_item();
				List<FilterImploperands = new ArrayList<FilterImpl>(10);
				while ([] == '(') {
					operands.add(child);
				return new FilterImpl(.nulloperands.toArray(new FilterImpl[operands.size()]));
				int lookahead = ;
				if ([] != '(') {
					 = lookahead - 1;
					return parse_item();
				List<FilterImploperands = new ArrayList<FilterImpl>(10);
				while ([] == '(') {
					operands.add(child);
				return new FilterImpl(.nulloperands.toArray(new FilterImpl[operands.size()]));
				int lookahead = ;
				if ([] != '(') {
					 = lookahead - 1;
					return parse_item();
				return new FilterImpl(.nullchild);
				String attr = parse_attr();
				switch ([]) {
					case '~' : {
						if ([ + 1] == '=') {
							 += 2;
							return new FilterImpl(.attrparse_value());
						break;
					case '>' : {
						if ([ + 1] == '=') {
							 += 2;
							return new FilterImpl(.attrparse_value());
						break;
					case '<' : {
						if ([ + 1] == '=') {
							 += 2;
							return new FilterImpl(.attrparse_value());
						break;
					case '=' : {
						if ([ + 1] == '*') {
							int oldpos = ;
							 += 2;
							if ([] == ')') {
								return new FilterImpl(.attrnull);
							 = oldpos;
						Object string = parse_substring();
						if (string instanceof String) {
							return new FilterImpl(.attrstring);
						return new FilterImpl(.attrstring);
				throw new InvalidSyntaxException("Invalid operator: " + .substring(), );
				int begin = ;
				int end = ;
				char c = [];
				while (c != '~' && c != '<' && c != '>' && c != '=' && c != '(' && c != ')') {
					if (!Character.isWhitespace(c)) {
						end = ;
				int length = end - begin;
				if (length == 0) {
					throw new InvalidSyntaxException("Missing attr: " + .substring(), );
				return new String(beginlength);
				parseloop: while (true) {
					char c = [];
					switch (c) {
						case ')' : {
							break parseloop;
						case '(' : {
							throw new InvalidSyntaxException("Invalid value: " + .substring(), );
						case '\\' : {
							/* fall through into default */
						default : {
							sb.append(c);
							break;
				if (sb.length() == 0) {
					throw new InvalidSyntaxException("Missing value: " + .substring(), );
				return sb.toString();