You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by ds...@apache.org on 2009/07/21 20:51:41 UTC

svn commit: r796467 [6/25] - in /felix/trunk/sigil: common/core.tests/src/org/apache/felix/sigil/core/ common/core/src/org/apache/felix/sigil/bnd/ common/core/src/org/apache/felix/sigil/config/ common/core/src/org/apache/felix/sigil/core/ common/core/s...

Modified: felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/ModelElementSupport.java
URL: http://svn.apache.org/viewvc/felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/ModelElementSupport.java?rev=796467&r1=796466&r2=796467&view=diff
==============================================================================
--- felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/ModelElementSupport.java (original)
+++ felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/ModelElementSupport.java Tue Jul 21 18:51:33 2009
@@ -19,6 +19,7 @@
 
 package org.apache.felix.sigil.model;
 
+
 import java.io.Serializable;
 import java.lang.ref.SoftReference;
 import java.lang.reflect.Array;
@@ -35,694 +36,954 @@
 import java.util.logging.Logger;
 
 
-public class ModelElementSupport implements Serializable {
+public class ModelElementSupport implements Serializable
+{
+
+    private static final Logger log = Logger.getLogger( ModelElementSupport.class.getName() );
+
+    private static final long serialVersionUID = 1L;
 
-	private static final Logger log = Logger.getLogger( ModelElementSupport.class.getName() );
-	
-	private static final long serialVersionUID = 1L;
-	
-	private static final PropertyAdapter[] EMPTY_PROPS = new PropertyAdapter[] {};
-	private static final IModelElement[] EMPTY_ELEMENTS = new IModelElement[] {};
-	private static final Object[] ZERO_ARGS = new Object[] {};
-	private static final Class<?>[] ZERO_PARAMS = new Class[] {};
-	
-	private static final WeakHashMap<Class<?>, SoftReference<ChildAdapter[]>> adapterCache = new WeakHashMap<Class<?>, SoftReference<ChildAdapter[]>>();;	
-	private static final WeakHashMap<Class<?>, SoftReference<PropertyAdapter[]>> propertyCache = new WeakHashMap<Class<?>, SoftReference<PropertyAdapter[]>>();;	
-	
-	private IModelElement target;
-	
-	private transient SoftReference<PropertyAdapter[]> propertyReference;
-	private transient SoftReference<ChildAdapter[]> childrenReference;
-	private transient SoftReference<Set<String>> propertyNameReference;
-	
-	public ModelElementSupport(IModelElement target) {
-		this.target = target;
-	}
-
-	public void setProperty(String name, Object value) throws NoSuchMethodException {
-		PropertyAdapter p = findProperty( name, value );
-		if ( p == null ) {
-			throw new NoSuchMethodException( "No such property " + name + " for type " + target.getClass() );
-		}
-		invoke( target, p.getWriteMethod(), value );
-	}
-	
-	public void addProperty(String name, Object value) throws NoSuchMethodException {
-		PropertyAdapter p = findProperty( name, value );
-		if ( p == null ) {
-			throw new NoSuchMethodException( "No such property " + name + " for type " + target.getClass() );
-		}
-		invoke( target, p.getAddMethod(), value );
-	}
-
-	public void removeProperty(String name, Object value) throws NoSuchMethodException {
-		PropertyAdapter p = findProperty( name, value );
-		if ( p == null ) {
-			throw new NoSuchMethodException( "No such property " + name + " for type " + target.getClass() );
-		}
-		invoke( target, p.getRemoveMethod(), value );
-	}
-	
-	public Object getProperty( String name ) throws NoSuchMethodException {
-		PropertyAdapter p = findProperty( name, null );
-		if ( p == null ) {
-			throw new NoSuchMethodException( "No such property " + name + " for type " + target.getClass() );
-		}
-		return invoke( target, p.getReadMethod(), ZERO_ARGS );
-	}
-	
-	public Set<String> getPropertyNames() {
-		Set<String> names = propertyNameReference == null ? null : propertyNameReference.get();
-		
-		if ( names == null ) {
-			names = new HashSet<String>();
-			
-			PropertyAdapter[] props = cachedProps( target.getClass() );
-			for ( PropertyAdapter prop : props ) {
-				names.add( prop.getName() );
-			}
-			
-			propertyNameReference = new SoftReference<Set<String>>(names);
-		}
-		
-		return names;
-	}
-	
-	public Object getDefaultPropertyValue(String name) {
-		try {
-			Method m = target.getClass().getMethod( makeDefaultPropertyValue( name ), ZERO_PARAMS );
-			return invoke( target, m, ZERO_ARGS );
-		} catch (SecurityException e) {
-			throw new UndeclaredThrowableException(e);
-		} catch (NoSuchMethodException e) {
-			// fine no default
-			return null;
-		}
-	}	
-	
-	public Class<?> getPropertyType(String name) throws NoSuchMethodException {
-		PropertyAdapter p = findProperty( name, null );
-		if ( p == null ) {
-			throw new NoSuchMethodException( "No such property " + name + " for type " + target.getClass() );
-		}
-		return p.getPropertyType();
-	}	
-	
-	@SuppressWarnings("unchecked")
-	public <T extends IModelElement> T[] childrenOfType( Class<T> type ) {
-		ChildAdapter[] adapters = cachedAdapters();
-		
-		if ( adapters.length == 0 ) {
-			// return (T[]) EMPTY_ELEMENTS;
-    		return ((T[])Array.newInstance(type, 0));
-		}
-		
-		ArrayList<T> elements = new ArrayList<T>();
-		
-		for ( ChildAdapter adapter : adapters ) {
-			Collection<? extends IModelElement> val = adapter.members(target);
-			
-			for ( IModelElement e : val ) {
-				if ( type.isInstance(e) ) {
-					elements.add( (T) e );
-				}
-			}
-		}
-		
-		//return elements.toArray( (T[]) EMPTY_ELEMENTS );
-		return elements.toArray((T[])Array.newInstance(type, elements.size()));
-	}
-	
-	public IModelElement[] children() {
-		ChildAdapter[] adapters = cachedAdapters();
-		
-		if ( adapters.length == 0 ) {
-			return EMPTY_ELEMENTS;
-		}
-		
-		ArrayList<IModelElement> elements = new ArrayList<IModelElement>();
-		
-		for ( ChildAdapter adapter : adapters ) {
-			elements.addAll( adapter.members(target) );
-		}
-		
-		return elements.toArray( EMPTY_ELEMENTS );
-	}
-	
-	public boolean addChild(IModelElement element) throws InvalidModelException {
-		if ( element.getParent() == null ) {
-			ChildAdapter[] adapters = cachedAdapters();
-			
-			if ( adapters.length > 0 ) {
-				for ( ChildAdapter adapter : adapters ) {
-					if ( adapter.add( target, element ) ) {
-						element.setParent(target);
-						return true;
-					}
-				}
-			}
-		}
-		
-		return false;
-	}
-	
-	public boolean removeChild(IModelElement element) {
-		if ( element.getParent() == target ) {
-			ChildAdapter[] adapters = cachedAdapters();
-			
-			if ( adapters.length > 0 ) {
-				for ( ChildAdapter adapter : adapters ) {
-					if ( adapter.remove( target, element ) ) {
-						element.setParent( null );
-						return true;
-					}
-				}
-			}
-		}
-		
-		return false;
-	}
-
-	public Set<Class<? extends IModelElement>> getChildrenTypes(boolean required) {
-		ChildAdapter[] adapters = cachedAdapters();
-		
-		if ( adapters.length == 0 ) {
-			return Collections.emptySet();
-		}
-		
-		HashSet<Class<? extends IModelElement>> types = new HashSet<Class<? extends IModelElement>>();
-		
-		for ( ChildAdapter adapter : adapters ) {
-			if ( adapter.isRequired() == required ) {
-				Class<? extends IModelElement> type = adapter.getType();
-				
-				if ( type != null ) {
-					types.add( type );
-				}
-			}
-		}
-		
-		return types;
-	}
-	
-	private PropertyAdapter findProperty(String name, Object value) {
-		PropertyAdapter[] props = propertyReference == null ? null : propertyReference.get();
-		
-		if ( props == null ) {
-			props = cachedProps( target.getClass() );
-			propertyReference = new SoftReference<PropertyAdapter[]>( props );
-		}
-		
-		for ( PropertyAdapter prop : props ) {
-			if ( prop.getName().equals( name ) && (value == null || prop.getRawType().isAssignableFrom(value.getClass() ) ) ) {
-				return prop;
-			}
-		}
-		
-		return null;
-	}
-
-	private static PropertyAdapter[] cachedProps(
-			Class<? extends IModelElement> type) {
-		SoftReference<PropertyAdapter[]> ref = propertyCache.get( type );
-		
-		PropertyAdapter[] props = ref == null ? null : ref.get();
-		
-		if ( props == null ) {
-			props = loadProps( type );
-			propertyCache.put( type, new SoftReference<PropertyAdapter[]>( props ) );
-		}
-		
-		return props;
-	}
-
-	private static PropertyAdapter[] loadProps(Class<? extends IModelElement> type) {
-		ArrayList<PropertyAdapter> props = new ArrayList<PropertyAdapter>();
-		for ( Method m : type.getMethods() ) {
-			if ( isValidProperty( m )) {
-				try {
-					PropertyAdapter p = new PropertyAdapter( m, type );
-					props.add( p );
-				} catch (NoSuchMethodException e) {
-					// fine not a bean method
-					log.finer( "Invalid bean property method " + m + ": " + e.getMessage() );
-				}
-			}
-		}
-		
-		return props.toArray( EMPTY_PROPS );
-	}
-
-	private static boolean isValidProperty(Method m) {
-		return m.getName().startsWith( "get" ) && m.getParameterTypes().length == 0 && !m.getDeclaringClass().equals( Object.class ) && !IModelElement.class.isAssignableFrom(m.getReturnType());
-	}
-
-	private static String makeDefaultPropertyValue(String name) {
-		return "getDefault" + capitalise( name );
-	}
-
-	private static String capitalise(String name) {
-		return Character.toUpperCase(name.charAt(0))  + name.substring(1);
-	}
-
-	private static String decapitalise(String substring) {
-		return Character.toLowerCase(substring.charAt(0)) + substring.substring(1);
-	}
-
-	private ChildAdapter[] cachedAdapters() {
-		ChildAdapter[] adapters = childrenReference == null ? null : childrenReference.get();
-		
-		if ( adapters == null ) {
-			adapters = loadAdapters( target );
-			childrenReference = new SoftReference<ChildAdapter[]>( adapters );
-		}		
-		
-		return adapters;
-	}
-	
-	private static ChildAdapter[] loadAdapters(IModelElement target) {
-		Class<? extends IModelElement> type = target.getClass();
-		SoftReference<ChildAdapter[]> ref = adapterCache.get( type );
-		
-		ChildAdapter[] adapters = ref == null ? null : ref.get();
-		
-		if ( adapters == null ) {
-			adapters = buildAdapters( type );
-			adapterCache.put( type, new SoftReference<ChildAdapter[]>( adapters ) );
-		}
-		
-		return adapters;
-	}
-	
-	private static ChildAdapter[] buildAdapters(Class<? extends IModelElement> type) {
-		ArrayList<ChildAdapter> adapters = new ArrayList<ChildAdapter>();
-		
-		for ( Method m : type.getMethods() ) {
-			ChildAdapter adapter = null;
-			
-			if ( isValidGetProperty( m ) ) {
-				adapter = buildGetAdapter( m );
-			}
-			else if ( isValidSetProperty( m ) ) {
-				adapter = buildSetAdapter( m );
-			}
-			else if ( isValidAddProperty( m ) ) {
-				adapter = buildAddAdapter( m );
-			}
-			else if ( isValidRemoveProperty( m ) ) {
-				adapter = buildRemoveAdapter( m );
-			}
-			
-			if ( adapter != null ) {
-				adapters.add( adapter );
-			}
-		}
-		
-		return adapters.toArray( new ChildAdapter[adapters.size()] );
-	}
-
-	private static ChildAdapter buildGetAdapter(Method m) {
-		if ( IModelElement.class.isAssignableFrom( m.getReturnType() ) ) {
-			return new GetPropertyAdapter( m );
-		}
-		else if ( Collection.class.isAssignableFrom( m.getReturnType() ) ) {
-			return new GetCollectionAdapter( m );
-		}
-		else if ( isModelArray( m.getReturnType() ) ) {
-			return new GetArrayAdapter( m );
-		}
-		else {
-			return null;
-		}
-	}
-	
-	private static ChildAdapter buildSetAdapter(Method m) {
-		if ( IModelElement.class.isAssignableFrom( m.getParameterTypes()[0] ) ) {
-			return new SetPropertyAdapter( m );
-		}
-		else {
-			return null;
-		}
-	}
-
-	private static ChildAdapter buildAddAdapter(Method m) {
-		if ( IModelElement.class.isAssignableFrom( m.getParameterTypes()[0] ) ) {
-			return new AddPropertyAdapter( m );
-		}
-		else {
-			return null;
-		}
-	}
-
-	private static ChildAdapter buildRemoveAdapter(Method m) {
-		if ( IModelElement.class.isAssignableFrom( m.getParameterTypes()[0] ) ) {
-			return new RemovePropertyAdapter( m );
-		}
-		else {
-			return null;
-		}
-	}
-
-	private static boolean isValidRemoveProperty(Method m) {
-		return m.getParameterTypes().length == 1 && m.getName().startsWith( "remove" ) && !isDeclared( ICompoundModelElement.class, m );
-	}
-
-	private static boolean isValidAddProperty(Method m) {
-		return m.getParameterTypes().length == 1 && m.getName().startsWith( "add" ) && !isDeclared( ICompoundModelElement.class, m );
-	}
-
-	private static boolean isDeclared(Class<? extends IModelElement> element, Method m) {
-		try {
-			element.getMethod( m.getName(), m.getParameterTypes() );
-			return true;
-		} catch (SecurityException e) {
-			throw new UndeclaredThrowableException( e );
-		} catch (NoSuchMethodException e) {
-			return false;
-		}
-	}
-
-	private static boolean isValidSetProperty(Method m) {
-		return m.getParameterTypes().length == 1 && m.getName().startsWith( "set" ) && !isDeclared( IModelElement.class, m );
-	}
-
-	private static boolean isValidGetProperty(Method m) {
-		return m.getParameterTypes().length == 0 && m.getName().startsWith( "get" ) && !isDeclared( IModelElement.class, m ) && !isDeclared(ICompoundModelElement.class, m);
-	}
-
-	private static Object invoke( Object target, Method m, Object... args ) {
-		try {
-			return m.invoke(target, args);
-		} catch (IllegalArgumentException e) {
-			// this should already have been tested
-			throw new IllegalStateException(e);
-		} catch (IllegalAccessException e) {
-			throw new UndeclaredThrowableException( e );
-		} catch (InvocationTargetException e) {
-			throw new UndeclaredThrowableException( e.getCause() );
-		}
-	}		
-
-	private static class PropertyAdapter {
-
-		String prop;
-		String name;
-		Method g;
-		Method s;
-		Method a;
-		Method r;
-		Class<?> propertyType;
-		
-		public PropertyAdapter(Method g, Class<?> type) throws SecurityException, NoSuchMethodException {
-			if ( g.getReturnType().isArray() || Iterable.class.isAssignableFrom(g.getReturnType() ) ) {
-				prop = g.getName().substring(3);
-				// remove trailing s - as in addWibble, removeWibble, getWibbles
-				prop = prop.substring(0, prop.length() - 1);
-				name = decapitalise( prop );
-				a = find( "add", prop, g.getReturnType(), type );
-				propertyType = a.getParameterTypes()[0];
-				r = find( "remove", prop, g.getReturnType(), type );
-				if ( r.getParameterTypes()[0] != propertyType ) {
-					throw new NoSuchMethodException( "Add remove property method types do not match" );
-				}
-				propertyType = Array.newInstance(propertyType, 0).getClass();
-			}
-			else {
-				prop = g.getName().substring(3);
-				name = decapitalise( prop );
-				propertyType = g.getReturnType();
-				s = find( "set", prop, propertyType, type );
-			}
-			
-			this.g = g;
-		}
-		
-		public Class<?> getRawType() {
-			return propertyType.isArray() ? propertyType.getComponentType() : propertyType;
-		}
-
-		public Class<?> getPropertyType() {
-			return propertyType;
-		}
-
-		public Method getReadMethod() {
-			return g;
-		}
-
-		public Method getAddMethod() throws NoSuchMethodException {
-			if ( a == null ) {
-				throw new NoSuchMethodException( "No such method add" + prop ); 
-			}
-			
-			return a;
-		}
-
-		public Method getRemoveMethod() throws NoSuchMethodException {
-			if ( r == null ) {
-				throw new NoSuchMethodException( "No such method remove" + prop ); 
-			}
-			
-			return r;
-		}
-
-		public Method getWriteMethod() throws NoSuchMethodException {
-			if ( s == null ) {
-				throw new NoSuchMethodException( "No such method set" + prop ); 
-			}
-			
-			return s;
-		}
-		
-		@Override
-		public String toString() {
-			return "PropertyAdapter[" + name + "]";
-		}
-
-		private Method find(String prefix, String prop, Class<?> returnType, Class<?> type) throws SecurityException, NoSuchMethodException {
-			String methodName = prefix + prop;
-			
-			if ( returnType.isArray() ) {
-				Class<?> t = returnType.getComponentType();
-				return type.getMethod( methodName, new Class[] { t } );
-			}
-			else if ( Iterable.class.isAssignableFrom( returnType ) ) {
-				Method f = null;
-				for ( Method m : type.getMethods() ) {
-					if ( m.getParameterTypes().length == 1 && m.getName().equals( methodName ) && !IModelElement.class.isAssignableFrom(m.getParameterTypes()[0]) ) {
-						if ( f == null ) {
-							f = m;
-						}
-						else {
-							throw new NoSuchMethodException( "Found duplicate " + methodName );
-						}
-					}
-				}
-				if ( f == null ) {
-					throw new NoSuchMethodException( "No such method " + methodName );
-				}
-				
-				return f;
-			}
-			else {
-				return type.getMethod( methodName, new Class[] { returnType } ); 
-			}
-		}
-		public String getName() {
-			return name;
-		}
-		
-	}
-	
-	private static abstract class ChildAdapter {
-		Method m;
-		
-		ChildAdapter( Method m ) {
-			this.m = m;
-		}
-		
-		public boolean isRequired() {
-			return m.isAnnotationPresent(Required.class);
-		}
-
-		boolean add(Object target, IModelElement element) {
-			return false;
-		}
-
-		abstract Class<? extends IModelElement> getType();
-
-		boolean remove(Object target, IModelElement element) {
-			return false;
-		}
-
-		Collection<? extends IModelElement> members(Object target) {
-			return Collections.emptyList();
-		}
-
-		@Override
-		public String toString() {
-			return "ChildAdapter[ " + m.getName() + "]";
-		}	
-	}
-	
-	private static class GetPropertyAdapter extends ChildAdapter {
-		GetPropertyAdapter(Method m) {
-			super(m);
-		}
-
-		@Override
-		Collection<? extends IModelElement> members(Object target) {
-			IModelElement member = (IModelElement) invoke( target, m, ZERO_ARGS );
-			if ( member == null ) {
-				return Collections.emptyList();
-			}
-			else {
-				return Collections.<IModelElement>singleton( member );
-			}
-		}
-
-		@SuppressWarnings("unchecked")
-		@Override
-		Class<? extends IModelElement> getType() {
-			return (Class<? extends IModelElement>) m.getReturnType();
-		}
-	}	
-	
-	private static class GetCollectionAdapter extends ChildAdapter {
-		public GetCollectionAdapter(Method m) {
-			super(m);
-		}
-
-		@SuppressWarnings("unchecked")
-		@Override
-		Collection<? extends IModelElement> members(Object target) {
-			Collection members = (Collection) invoke( target, m, ZERO_ARGS );
-			if ( members == null ) {
-				return Collections.emptyList();
-			}
-			else {
-				ArrayList<IModelElement> safe = new ArrayList<IModelElement>(members.size());
-				for ( Object o : members ) {
-					if ( o instanceof IModelElement ) {
-						safe.add( (IModelElement) o );
-					}
-				}
-				return safe;
-			}
-		}
-		
-		@Override
-		Class<? extends IModelElement> getType() {
-			// impossible to get type of a collection as erasure removes generics info
-			return null;
-		}		
-		
-	}
-	
-	private static class GetArrayAdapter extends ChildAdapter {
-		public GetArrayAdapter(Method m) {
-			super(m);
-		}
-
-		@Override
-		Collection<? extends IModelElement> members(Object target) {
-			IModelElement[] array = (IModelElement[]) invoke( target, m, ZERO_ARGS );
-			if ( array == null || array.length == 0) {
-				return Collections.emptyList();
-			}
-			else {
-				return (Collection<? extends IModelElement>) Arrays.asList( array );
-			}
-		}
-		
-		@SuppressWarnings("unchecked")
-		@Override
-		Class<? extends IModelElement> getType() {
-			return (Class<? extends IModelElement>) m.getReturnType().getComponentType();
-		}
-	}
-	
-	private static class SetPropertyAdapter extends ChildAdapter {
-		public SetPropertyAdapter(Method m) {
-			super(m);
-		}
-
-		@Override
-		boolean add(Object target, IModelElement element) {
-			if ( m.getParameterTypes()[0].isAssignableFrom( element.getClass() ) ) {
-				invoke(target, m, new Object[] { element } );
-				return true;
-			}
-			else {
-				return false;
-			}
-		}
-
-		@Override
-		boolean remove(Object target, IModelElement element) {
-			if ( m.getParameterTypes()[0].isAssignableFrom( element.getClass() ) ) {
-				invoke(target, m, new Object[] { null } );
-				return true;
-			}
-			else {
-				return false;
-			}
-		}		
-		
-		@SuppressWarnings("unchecked")
-		@Override
-		Class<? extends IModelElement> getType() {
-			return (Class<? extends IModelElement>) m.getParameterTypes()[0];
-		}		
-	}	
-	
-	private static class AddPropertyAdapter extends ChildAdapter {
-		public AddPropertyAdapter(Method m) {
-			super(m);
-		}
-
-		@Override
-		boolean add(Object target, IModelElement element) {
-			if ( m.getParameterTypes()[0].isAssignableFrom( element.getClass() ) ) {
-				invoke(target, m, new Object[] { element } );
-				return true;
-			}
-			else {
-				return false;
-			}
-		}
-		
-		@SuppressWarnings("unchecked")
-		@Override
-		Class<? extends IModelElement> getType() {
-			return (Class<? extends IModelElement>) m.getParameterTypes()[0];
-		}		
-	}
-	
-	private static class RemovePropertyAdapter extends ChildAdapter {
-
-		public RemovePropertyAdapter(Method m) {
-			super(m);
-		}
-
-		@Override
-		boolean remove(Object target, IModelElement element) {
-			if ( m.getParameterTypes()[0].isAssignableFrom( element.getClass() ) ) {
-				invoke(target, m, new Object[] { element } );
-				return true;
-			}
-			else {
-				return false;
-			}
-		}
-		
-		@SuppressWarnings("unchecked")
-		@Override
-		Class<? extends IModelElement> getType() {
-			return (Class<? extends IModelElement>) m.getParameterTypes()[0];
-		}		
-	}
-	
-	private static boolean isModelArray(Class<?> returnType) {
-		return returnType.isArray() && IModelElement.class.isAssignableFrom(returnType.getComponentType() );
-	}
+    private static final PropertyAdapter[] EMPTY_PROPS = new PropertyAdapter[]
+        {};
+    private static final IModelElement[] EMPTY_ELEMENTS = new IModelElement[]
+        {};
+    private static final Object[] ZERO_ARGS = new Object[]
+        {};
+    private static final Class<?>[] ZERO_PARAMS = new Class[]
+        {};
+
+    private static final WeakHashMap<Class<?>, SoftReference<ChildAdapter[]>> adapterCache = new WeakHashMap<Class<?>, SoftReference<ChildAdapter[]>>();;
+    private static final WeakHashMap<Class<?>, SoftReference<PropertyAdapter[]>> propertyCache = new WeakHashMap<Class<?>, SoftReference<PropertyAdapter[]>>();;
+
+    private IModelElement target;
+
+    private transient SoftReference<PropertyAdapter[]> propertyReference;
+    private transient SoftReference<ChildAdapter[]> childrenReference;
+    private transient SoftReference<Set<String>> propertyNameReference;
+
+
+    public ModelElementSupport( IModelElement target )
+    {
+        this.target = target;
+    }
+
+
+    public void setProperty( String name, Object value ) throws NoSuchMethodException
+    {
+        PropertyAdapter p = findProperty( name, value );
+        if ( p == null )
+        {
+            throw new NoSuchMethodException( "No such property " + name + " for type " + target.getClass() );
+        }
+        invoke( target, p.getWriteMethod(), value );
+    }
+
+
+    public void addProperty( String name, Object value ) throws NoSuchMethodException
+    {
+        PropertyAdapter p = findProperty( name, value );
+        if ( p == null )
+        {
+            throw new NoSuchMethodException( "No such property " + name + " for type " + target.getClass() );
+        }
+        invoke( target, p.getAddMethod(), value );
+    }
+
+
+    public void removeProperty( String name, Object value ) throws NoSuchMethodException
+    {
+        PropertyAdapter p = findProperty( name, value );
+        if ( p == null )
+        {
+            throw new NoSuchMethodException( "No such property " + name + " for type " + target.getClass() );
+        }
+        invoke( target, p.getRemoveMethod(), value );
+    }
+
+
+    public Object getProperty( String name ) throws NoSuchMethodException
+    {
+        PropertyAdapter p = findProperty( name, null );
+        if ( p == null )
+        {
+            throw new NoSuchMethodException( "No such property " + name + " for type " + target.getClass() );
+        }
+        return invoke( target, p.getReadMethod(), ZERO_ARGS );
+    }
+
+
+    public Set<String> getPropertyNames()
+    {
+        Set<String> names = propertyNameReference == null ? null : propertyNameReference.get();
+
+        if ( names == null )
+        {
+            names = new HashSet<String>();
+
+            PropertyAdapter[] props = cachedProps( target.getClass() );
+            for ( PropertyAdapter prop : props )
+            {
+                names.add( prop.getName() );
+            }
+
+            propertyNameReference = new SoftReference<Set<String>>( names );
+        }
+
+        return names;
+    }
+
+
+    public Object getDefaultPropertyValue( String name )
+    {
+        try
+        {
+            Method m = target.getClass().getMethod( makeDefaultPropertyValue( name ), ZERO_PARAMS );
+            return invoke( target, m, ZERO_ARGS );
+        }
+        catch ( SecurityException e )
+        {
+            throw new UndeclaredThrowableException( e );
+        }
+        catch ( NoSuchMethodException e )
+        {
+            // fine no default
+            return null;
+        }
+    }
+
+
+    public Class<?> getPropertyType( String name ) throws NoSuchMethodException
+    {
+        PropertyAdapter p = findProperty( name, null );
+        if ( p == null )
+        {
+            throw new NoSuchMethodException( "No such property " + name + " for type " + target.getClass() );
+        }
+        return p.getPropertyType();
+    }
+
+
+    @SuppressWarnings("unchecked")
+    public <T extends IModelElement> T[] childrenOfType( Class<T> type )
+    {
+        ChildAdapter[] adapters = cachedAdapters();
+
+        if ( adapters.length == 0 )
+        {
+            // return (T[]) EMPTY_ELEMENTS;
+            return ( ( T[] ) Array.newInstance( type, 0 ) );
+        }
+
+        ArrayList<T> elements = new ArrayList<T>();
+
+        for ( ChildAdapter adapter : adapters )
+        {
+            Collection<? extends IModelElement> val = adapter.members( target );
+
+            for ( IModelElement e : val )
+            {
+                if ( type.isInstance( e ) )
+                {
+                    elements.add( ( T ) e );
+                }
+            }
+        }
+
+        //return elements.toArray( (T[]) EMPTY_ELEMENTS );
+        return elements.toArray( ( T[] ) Array.newInstance( type, elements.size() ) );
+    }
+
+
+    public IModelElement[] children()
+    {
+        ChildAdapter[] adapters = cachedAdapters();
+
+        if ( adapters.length == 0 )
+        {
+            return EMPTY_ELEMENTS;
+        }
+
+        ArrayList<IModelElement> elements = new ArrayList<IModelElement>();
+
+        for ( ChildAdapter adapter : adapters )
+        {
+            elements.addAll( adapter.members( target ) );
+        }
+
+        return elements.toArray( EMPTY_ELEMENTS );
+    }
+
+
+    public boolean addChild( IModelElement element ) throws InvalidModelException
+    {
+        if ( element.getParent() == null )
+        {
+            ChildAdapter[] adapters = cachedAdapters();
+
+            if ( adapters.length > 0 )
+            {
+                for ( ChildAdapter adapter : adapters )
+                {
+                    if ( adapter.add( target, element ) )
+                    {
+                        element.setParent( target );
+                        return true;
+                    }
+                }
+            }
+        }
+
+        return false;
+    }
+
+
+    public boolean removeChild( IModelElement element )
+    {
+        if ( element.getParent() == target )
+        {
+            ChildAdapter[] adapters = cachedAdapters();
+
+            if ( adapters.length > 0 )
+            {
+                for ( ChildAdapter adapter : adapters )
+                {
+                    if ( adapter.remove( target, element ) )
+                    {
+                        element.setParent( null );
+                        return true;
+                    }
+                }
+            }
+        }
+
+        return false;
+    }
+
+
+    public Set<Class<? extends IModelElement>> getChildrenTypes( boolean required )
+    {
+        ChildAdapter[] adapters = cachedAdapters();
+
+        if ( adapters.length == 0 )
+        {
+            return Collections.emptySet();
+        }
+
+        HashSet<Class<? extends IModelElement>> types = new HashSet<Class<? extends IModelElement>>();
+
+        for ( ChildAdapter adapter : adapters )
+        {
+            if ( adapter.isRequired() == required )
+            {
+                Class<? extends IModelElement> type = adapter.getType();
+
+                if ( type != null )
+                {
+                    types.add( type );
+                }
+            }
+        }
+
+        return types;
+    }
+
+
+    private PropertyAdapter findProperty( String name, Object value )
+    {
+        PropertyAdapter[] props = propertyReference == null ? null : propertyReference.get();
+
+        if ( props == null )
+        {
+            props = cachedProps( target.getClass() );
+            propertyReference = new SoftReference<PropertyAdapter[]>( props );
+        }
+
+        for ( PropertyAdapter prop : props )
+        {
+            if ( prop.getName().equals( name )
+                && ( value == null || prop.getRawType().isAssignableFrom( value.getClass() ) ) )
+            {
+                return prop;
+            }
+        }
+
+        return null;
+    }
+
+
+    private static PropertyAdapter[] cachedProps( Class<? extends IModelElement> type )
+    {
+        SoftReference<PropertyAdapter[]> ref = propertyCache.get( type );
+
+        PropertyAdapter[] props = ref == null ? null : ref.get();
+
+        if ( props == null )
+        {
+            props = loadProps( type );
+            propertyCache.put( type, new SoftReference<PropertyAdapter[]>( props ) );
+        }
+
+        return props;
+    }
+
+
+    private static PropertyAdapter[] loadProps( Class<? extends IModelElement> type )
+    {
+        ArrayList<PropertyAdapter> props = new ArrayList<PropertyAdapter>();
+        for ( Method m : type.getMethods() )
+        {
+            if ( isValidProperty( m ) )
+            {
+                try
+                {
+                    PropertyAdapter p = new PropertyAdapter( m, type );
+                    props.add( p );
+                }
+                catch ( NoSuchMethodException e )
+                {
+                    // fine not a bean method
+                    log.finer( "Invalid bean property method " + m + ": " + e.getMessage() );
+                }
+            }
+        }
+
+        return props.toArray( EMPTY_PROPS );
+    }
+
+
+    private static boolean isValidProperty( Method m )
+    {
+        return m.getName().startsWith( "get" ) && m.getParameterTypes().length == 0
+            && !m.getDeclaringClass().equals( Object.class )
+            && !IModelElement.class.isAssignableFrom( m.getReturnType() );
+    }
+
+
+    private static String makeDefaultPropertyValue( String name )
+    {
+        return "getDefault" + capitalise( name );
+    }
+
+
+    private static String capitalise( String name )
+    {
+        return Character.toUpperCase( name.charAt( 0 ) ) + name.substring( 1 );
+    }
+
+
+    private static String decapitalise( String substring )
+    {
+        return Character.toLowerCase( substring.charAt( 0 ) ) + substring.substring( 1 );
+    }
+
+
+    private ChildAdapter[] cachedAdapters()
+    {
+        ChildAdapter[] adapters = childrenReference == null ? null : childrenReference.get();
+
+        if ( adapters == null )
+        {
+            adapters = loadAdapters( target );
+            childrenReference = new SoftReference<ChildAdapter[]>( adapters );
+        }
+
+        return adapters;
+    }
+
+
+    private static ChildAdapter[] loadAdapters( IModelElement target )
+    {
+        Class<? extends IModelElement> type = target.getClass();
+        SoftReference<ChildAdapter[]> ref = adapterCache.get( type );
+
+        ChildAdapter[] adapters = ref == null ? null : ref.get();
+
+        if ( adapters == null )
+        {
+            adapters = buildAdapters( type );
+            adapterCache.put( type, new SoftReference<ChildAdapter[]>( adapters ) );
+        }
+
+        return adapters;
+    }
+
+
+    private static ChildAdapter[] buildAdapters( Class<? extends IModelElement> type )
+    {
+        ArrayList<ChildAdapter> adapters = new ArrayList<ChildAdapter>();
+
+        for ( Method m : type.getMethods() )
+        {
+            ChildAdapter adapter = null;
+
+            if ( isValidGetProperty( m ) )
+            {
+                adapter = buildGetAdapter( m );
+            }
+            else if ( isValidSetProperty( m ) )
+            {
+                adapter = buildSetAdapter( m );
+            }
+            else if ( isValidAddProperty( m ) )
+            {
+                adapter = buildAddAdapter( m );
+            }
+            else if ( isValidRemoveProperty( m ) )
+            {
+                adapter = buildRemoveAdapter( m );
+            }
+
+            if ( adapter != null )
+            {
+                adapters.add( adapter );
+            }
+        }
+
+        return adapters.toArray( new ChildAdapter[adapters.size()] );
+    }
+
+
+    private static ChildAdapter buildGetAdapter( Method m )
+    {
+        if ( IModelElement.class.isAssignableFrom( m.getReturnType() ) )
+        {
+            return new GetPropertyAdapter( m );
+        }
+        else if ( Collection.class.isAssignableFrom( m.getReturnType() ) )
+        {
+            return new GetCollectionAdapter( m );
+        }
+        else if ( isModelArray( m.getReturnType() ) )
+        {
+            return new GetArrayAdapter( m );
+        }
+        else
+        {
+            return null;
+        }
+    }
+
+
+    private static ChildAdapter buildSetAdapter( Method m )
+    {
+        if ( IModelElement.class.isAssignableFrom( m.getParameterTypes()[0] ) )
+        {
+            return new SetPropertyAdapter( m );
+        }
+        else
+        {
+            return null;
+        }
+    }
+
+
+    private static ChildAdapter buildAddAdapter( Method m )
+    {
+        if ( IModelElement.class.isAssignableFrom( m.getParameterTypes()[0] ) )
+        {
+            return new AddPropertyAdapter( m );
+        }
+        else
+        {
+            return null;
+        }
+    }
+
+
+    private static ChildAdapter buildRemoveAdapter( Method m )
+    {
+        if ( IModelElement.class.isAssignableFrom( m.getParameterTypes()[0] ) )
+        {
+            return new RemovePropertyAdapter( m );
+        }
+        else
+        {
+            return null;
+        }
+    }
+
+
+    private static boolean isValidRemoveProperty( Method m )
+    {
+        return m.getParameterTypes().length == 1 && m.getName().startsWith( "remove" )
+            && !isDeclared( ICompoundModelElement.class, m );
+    }
+
+
+    private static boolean isValidAddProperty( Method m )
+    {
+        return m.getParameterTypes().length == 1 && m.getName().startsWith( "add" )
+            && !isDeclared( ICompoundModelElement.class, m );
+    }
+
+
+    private static boolean isDeclared( Class<? extends IModelElement> element, Method m )
+    {
+        try
+        {
+            element.getMethod( m.getName(), m.getParameterTypes() );
+            return true;
+        }
+        catch ( SecurityException e )
+        {
+            throw new UndeclaredThrowableException( e );
+        }
+        catch ( NoSuchMethodException e )
+        {
+            return false;
+        }
+    }
+
+
+    private static boolean isValidSetProperty( Method m )
+    {
+        return m.getParameterTypes().length == 1 && m.getName().startsWith( "set" )
+            && !isDeclared( IModelElement.class, m );
+    }
+
+
+    private static boolean isValidGetProperty( Method m )
+    {
+        return m.getParameterTypes().length == 0 && m.getName().startsWith( "get" )
+            && !isDeclared( IModelElement.class, m ) && !isDeclared( ICompoundModelElement.class, m );
+    }
+
+
+    private static Object invoke( Object target, Method m, Object... args )
+    {
+        try
+        {
+            return m.invoke( target, args );
+        }
+        catch ( IllegalArgumentException e )
+        {
+            // this should already have been tested
+            throw new IllegalStateException( e );
+        }
+        catch ( IllegalAccessException e )
+        {
+            throw new UndeclaredThrowableException( e );
+        }
+        catch ( InvocationTargetException e )
+        {
+            throw new UndeclaredThrowableException( e.getCause() );
+        }
+    }
+
+    private static class PropertyAdapter
+    {
+
+        String prop;
+        String name;
+        Method g;
+        Method s;
+        Method a;
+        Method r;
+        Class<?> propertyType;
+
+
+        public PropertyAdapter( Method g, Class<?> type ) throws SecurityException, NoSuchMethodException
+        {
+            if ( g.getReturnType().isArray() || Iterable.class.isAssignableFrom( g.getReturnType() ) )
+            {
+                prop = g.getName().substring( 3 );
+                // remove trailing s - as in addWibble, removeWibble, getWibbles
+                prop = prop.substring( 0, prop.length() - 1 );
+                name = decapitalise( prop );
+                a = find( "add", prop, g.getReturnType(), type );
+                propertyType = a.getParameterTypes()[0];
+                r = find( "remove", prop, g.getReturnType(), type );
+                if ( r.getParameterTypes()[0] != propertyType )
+                {
+                    throw new NoSuchMethodException( "Add remove property method types do not match" );
+                }
+                propertyType = Array.newInstance( propertyType, 0 ).getClass();
+            }
+            else
+            {
+                prop = g.getName().substring( 3 );
+                name = decapitalise( prop );
+                propertyType = g.getReturnType();
+                s = find( "set", prop, propertyType, type );
+            }
+
+            this.g = g;
+        }
+
+
+        public Class<?> getRawType()
+        {
+            return propertyType.isArray() ? propertyType.getComponentType() : propertyType;
+        }
+
+
+        public Class<?> getPropertyType()
+        {
+            return propertyType;
+        }
+
+
+        public Method getReadMethod()
+        {
+            return g;
+        }
+
+
+        public Method getAddMethod() throws NoSuchMethodException
+        {
+            if ( a == null )
+            {
+                throw new NoSuchMethodException( "No such method add" + prop );
+            }
+
+            return a;
+        }
+
+
+        public Method getRemoveMethod() throws NoSuchMethodException
+        {
+            if ( r == null )
+            {
+                throw new NoSuchMethodException( "No such method remove" + prop );
+            }
+
+            return r;
+        }
+
+
+        public Method getWriteMethod() throws NoSuchMethodException
+        {
+            if ( s == null )
+            {
+                throw new NoSuchMethodException( "No such method set" + prop );
+            }
+
+            return s;
+        }
+
+
+        @Override
+        public String toString()
+        {
+            return "PropertyAdapter[" + name + "]";
+        }
+
+
+        private Method find( String prefix, String prop, Class<?> returnType, Class<?> type ) throws SecurityException,
+            NoSuchMethodException
+        {
+            String methodName = prefix + prop;
+
+            if ( returnType.isArray() )
+            {
+                Class<?> t = returnType.getComponentType();
+                return type.getMethod( methodName, new Class[]
+                    { t } );
+            }
+            else if ( Iterable.class.isAssignableFrom( returnType ) )
+            {
+                Method f = null;
+                for ( Method m : type.getMethods() )
+                {
+                    if ( m.getParameterTypes().length == 1 && m.getName().equals( methodName )
+                        && !IModelElement.class.isAssignableFrom( m.getParameterTypes()[0] ) )
+                    {
+                        if ( f == null )
+                        {
+                            f = m;
+                        }
+                        else
+                        {
+                            throw new NoSuchMethodException( "Found duplicate " + methodName );
+                        }
+                    }
+                }
+                if ( f == null )
+                {
+                    throw new NoSuchMethodException( "No such method " + methodName );
+                }
+
+                return f;
+            }
+            else
+            {
+                return type.getMethod( methodName, new Class[]
+                    { returnType } );
+            }
+        }
+
+
+        public String getName()
+        {
+            return name;
+        }
+
+    }
+
+    private static abstract class ChildAdapter
+    {
+        Method m;
+
+
+        ChildAdapter( Method m )
+        {
+            this.m = m;
+        }
+
+
+        public boolean isRequired()
+        {
+            return m.isAnnotationPresent( Required.class );
+        }
+
+
+        boolean add( Object target, IModelElement element )
+        {
+            return false;
+        }
+
+
+        abstract Class<? extends IModelElement> getType();
+
+
+        boolean remove( Object target, IModelElement element )
+        {
+            return false;
+        }
+
+
+        Collection<? extends IModelElement> members( Object target )
+        {
+            return Collections.emptyList();
+        }
+
+
+        @Override
+        public String toString()
+        {
+            return "ChildAdapter[ " + m.getName() + "]";
+        }
+    }
+
+    private static class GetPropertyAdapter extends ChildAdapter
+    {
+        GetPropertyAdapter( Method m )
+        {
+            super( m );
+        }
+
+
+        @Override
+        Collection<? extends IModelElement> members( Object target )
+        {
+            IModelElement member = ( IModelElement ) invoke( target, m, ZERO_ARGS );
+            if ( member == null )
+            {
+                return Collections.emptyList();
+            }
+            else
+            {
+                return Collections.<IModelElement> singleton( member );
+            }
+        }
+
+
+        @SuppressWarnings("unchecked")
+        @Override
+        Class<? extends IModelElement> getType()
+        {
+            return ( Class<? extends IModelElement> ) m.getReturnType();
+        }
+    }
+
+    private static class GetCollectionAdapter extends ChildAdapter
+    {
+        public GetCollectionAdapter( Method m )
+        {
+            super( m );
+        }
+
+
+        @SuppressWarnings("unchecked")
+        @Override
+        Collection<? extends IModelElement> members( Object target )
+        {
+            Collection members = ( Collection ) invoke( target, m, ZERO_ARGS );
+            if ( members == null )
+            {
+                return Collections.emptyList();
+            }
+            else
+            {
+                ArrayList<IModelElement> safe = new ArrayList<IModelElement>( members.size() );
+                for ( Object o : members )
+                {
+                    if ( o instanceof IModelElement )
+                    {
+                        safe.add( ( IModelElement ) o );
+                    }
+                }
+                return safe;
+            }
+        }
+
+
+        @Override
+        Class<? extends IModelElement> getType()
+        {
+            // impossible to get type of a collection as erasure removes generics info
+            return null;
+        }
+
+    }
+
+    private static class GetArrayAdapter extends ChildAdapter
+    {
+        public GetArrayAdapter( Method m )
+        {
+            super( m );
+        }
+
+
+        @Override
+        Collection<? extends IModelElement> members( Object target )
+        {
+            IModelElement[] array = ( IModelElement[] ) invoke( target, m, ZERO_ARGS );
+            if ( array == null || array.length == 0 )
+            {
+                return Collections.emptyList();
+            }
+            else
+            {
+                return ( Collection<? extends IModelElement> ) Arrays.asList( array );
+            }
+        }
+
+
+        @SuppressWarnings("unchecked")
+        @Override
+        Class<? extends IModelElement> getType()
+        {
+            return ( Class<? extends IModelElement> ) m.getReturnType().getComponentType();
+        }
+    }
+
+    private static class SetPropertyAdapter extends ChildAdapter
+    {
+        public SetPropertyAdapter( Method m )
+        {
+            super( m );
+        }
+
+
+        @Override
+        boolean add( Object target, IModelElement element )
+        {
+            if ( m.getParameterTypes()[0].isAssignableFrom( element.getClass() ) )
+            {
+                invoke( target, m, new Object[]
+                    { element } );
+                return true;
+            }
+            else
+            {
+                return false;
+            }
+        }
+
+
+        @Override
+        boolean remove( Object target, IModelElement element )
+        {
+            if ( m.getParameterTypes()[0].isAssignableFrom( element.getClass() ) )
+            {
+                invoke( target, m, new Object[]
+                    { null } );
+                return true;
+            }
+            else
+            {
+                return false;
+            }
+        }
+
+
+        @SuppressWarnings("unchecked")
+        @Override
+        Class<? extends IModelElement> getType()
+        {
+            return ( Class<? extends IModelElement> ) m.getParameterTypes()[0];
+        }
+    }
+
+    private static class AddPropertyAdapter extends ChildAdapter
+    {
+        public AddPropertyAdapter( Method m )
+        {
+            super( m );
+        }
+
+
+        @Override
+        boolean add( Object target, IModelElement element )
+        {
+            if ( m.getParameterTypes()[0].isAssignableFrom( element.getClass() ) )
+            {
+                invoke( target, m, new Object[]
+                    { element } );
+                return true;
+            }
+            else
+            {
+                return false;
+            }
+        }
+
+
+        @SuppressWarnings("unchecked")
+        @Override
+        Class<? extends IModelElement> getType()
+        {
+            return ( Class<? extends IModelElement> ) m.getParameterTypes()[0];
+        }
+    }
+
+    private static class RemovePropertyAdapter extends ChildAdapter
+    {
+
+        public RemovePropertyAdapter( Method m )
+        {
+            super( m );
+        }
+
+
+        @Override
+        boolean remove( Object target, IModelElement element )
+        {
+            if ( m.getParameterTypes()[0].isAssignableFrom( element.getClass() ) )
+            {
+                invoke( target, m, new Object[]
+                    { element } );
+                return true;
+            }
+            else
+            {
+                return false;
+            }
+        }
+
+
+        @SuppressWarnings("unchecked")
+        @Override
+        Class<? extends IModelElement> getType()
+        {
+            return ( Class<? extends IModelElement> ) m.getParameterTypes()[0];
+        }
+    }
+
+
+    private static boolean isModelArray( Class<?> returnType )
+    {
+        return returnType.isArray() && IModelElement.class.isAssignableFrom( returnType.getComponentType() );
+    }
 }

Modified: felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/OverrideOptions.java
URL: http://svn.apache.org/viewvc/felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/OverrideOptions.java?rev=796467&r1=796466&r2=796467&view=diff
==============================================================================
--- felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/OverrideOptions.java (original)
+++ felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/OverrideOptions.java Tue Jul 21 18:51:33 2009
@@ -19,42 +19,54 @@
 
 package org.apache.felix.sigil.model;
 
+
 import java.util.HashMap;
 import java.util.Map;
 
+
 /**
  * @author dave
  * 
  */
-public enum OverrideOptions {
+public enum OverrideOptions
+{
     NO("no"), MAY("may"), MUST("must");
 
     private String str;
 
     private static Map<String, OverrideOptions> map = new HashMap<String, OverrideOptions>();
 
-    static {
-        for (OverrideOptions option : OverrideOptions.values()) {
-            map.put(option.str.toLowerCase(), option);
+    static
+    {
+        for ( OverrideOptions option : OverrideOptions.values() )
+        {
+            map.put( option.str.toLowerCase(), option );
         }
     }
 
-    private OverrideOptions(String str) {
+
+    private OverrideOptions( String str )
+    {
         this.str = str;
     }
 
-    public static OverrideOptions parse(String val) {
-        OverrideOptions option = map.get(val.toLowerCase());
 
-        if (option == null) {
-            throw new IllegalArgumentException("Invalid override value " + val);
+    public static OverrideOptions parse( String val )
+    {
+        OverrideOptions option = map.get( val.toLowerCase() );
+
+        if ( option == null )
+        {
+            throw new IllegalArgumentException( "Invalid override value " + val );
         }
 
         return option;
     }
-    
+
+
     @Override
-    public String toString() {
+    public String toString()
+    {
         return str;
     }
 

Modified: felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/Required.java
URL: http://svn.apache.org/viewvc/felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/Required.java?rev=796467&r1=796466&r2=796467&view=diff
==============================================================================
--- felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/Required.java (original)
+++ felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/Required.java Tue Jul 21 18:51:33 2009
@@ -19,6 +19,7 @@
 
 package org.apache.felix.sigil.model;
 
+
 import java.lang.annotation.Documented;
 import java.lang.annotation.ElementType;
 import java.lang.annotation.Inherited;
@@ -26,10 +27,12 @@
 import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;
 
+
 @Inherited
 @Documented
 @Retention(RetentionPolicy.RUNTIME)
 @Target(ElementType.METHOD)
-public @interface Required {
+public @interface Required
+{
 
 }

Modified: felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/common/And.java
URL: http://svn.apache.org/viewvc/felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/common/And.java?rev=796467&r1=796466&r2=796467&view=diff
==============================================================================
--- felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/common/And.java (original)
+++ felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/common/And.java Tue Jul 21 18:51:33 2009
@@ -19,81 +19,110 @@
 
 package org.apache.felix.sigil.model.common;
 
+
 import java.util.Map;
 
-public class And implements LDAPExpr {
+
+public class And implements LDAPExpr
+{
 
     /**
      */
     private static final long serialVersionUID = 1L;
     private LDAPExpr[] children;
 
-    public static LDAPExpr apply(LDAPExpr... terms) {
-        if (terms == null) {
-            throw new NullPointerException("terms cannot be null");
+
+    public static LDAPExpr apply( LDAPExpr... terms )
+    {
+        if ( terms == null )
+        {
+            throw new NullPointerException( "terms cannot be null" );
         }
-        else if (terms.length == 0) {
+        else if ( terms.length == 0 )
+        {
             return Expressions.T;
         }
-        else if (terms.length == 1) {
+        else if ( terms.length == 1 )
+        {
             return terms[0];
         }
         LDAPExpr[] filtered = new LDAPExpr[terms.length];
         int ctr = 0;
-        for (int i = 0; i < terms.length; i++) {
-            if (terms[i].equals(Expressions.F))
+        for ( int i = 0; i < terms.length; i++ )
+        {
+            if ( terms[i].equals( Expressions.F ) )
                 return Expressions.F;
-            if (terms[i].equals(Expressions.T))
+            if ( terms[i].equals( Expressions.T ) )
                 continue;
             filtered[ctr] = terms[i];
             ctr++;
         }
-        if (ctr == 0) {
+        if ( ctr == 0 )
+        {
             return Expressions.T;
         }
-        else if (ctr == 1) {
+        else if ( ctr == 1 )
+        {
             return filtered[0];
         }
         LDAPExpr[] andTerms = new LDAPExpr[ctr];
-        System.arraycopy(filtered, 0, andTerms, 0, ctr);
+        System.arraycopy( filtered, 0, andTerms, 0, ctr );
 
-        return new And(andTerms);
+        return new And( andTerms );
     }
 
-    private And(LDAPExpr... children) {
+
+    private And( LDAPExpr... children )
+    {
         this.children = children;
     }
 
-    public boolean eval(Map<String, ?> map) {
-        for (int i = 0; i < children.length; i++) {
-            if (!children[i].eval(map)) {
+
+    public boolean eval( Map<String, ?> map )
+    {
+        for ( int i = 0; i < children.length; i++ )
+        {
+            if ( !children[i].eval( map ) )
+            {
                 return false;
             }
         }
         return true;
     }
 
-    public void visit(ExprVisitor v) {
-        v.visitAnd(this);
+
+    public void visit( ExprVisitor v )
+    {
+        v.visitAnd( this );
     }
 
-    public LDAPExpr[] getChildren() {
+
+    public LDAPExpr[] getChildren()
+    {
         return children;
     }
 
-    public void setChildren(LDAPExpr[] children) {
+
+    public void setChildren( LDAPExpr[] children )
+    {
         this.children = children;
     }
 
+
     @Override
-    public boolean equals(Object other) {
-        if (other instanceof And) {
-            And that = (And) other;
-            if (children.length != that.children.length) {
+    public boolean equals( Object other )
+    {
+        if ( other instanceof And )
+        {
+            And that = ( And ) other;
+            if ( children.length != that.children.length )
+            {
                 return false;
             }
-            for (int i = 0; i < children.length; i++) {
-                if (!children[i].equals(that.children[i])) {
+            for ( int i = 0; i < children.length; i++ )
+            {
+                if ( !children[i].equals( that.children[i] ) )
+                {
                     return false;
                 }
             }
@@ -102,14 +131,17 @@
         return false;
     }
 
+
     @Override
-    public String toString() {
-        StringBuffer buf = new StringBuffer(256);
-        buf.append("(&");
-        for (int i = 0; i < children.length; i++) {
-            buf.append(" ").append(children[i]).append(" ");
+    public String toString()
+    {
+        StringBuffer buf = new StringBuffer( 256 );
+        buf.append( "(&" );
+        for ( int i = 0; i < children.length; i++ )
+        {
+            buf.append( " " ).append( children[i] ).append( " " );
         }
-        buf.append(")");
+        buf.append( ")" );
         return buf.toString();
     }
 

Modified: felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/common/Cardinality.java
URL: http://svn.apache.org/viewvc/felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/common/Cardinality.java?rev=796467&r1=796466&r2=796467&view=diff
==============================================================================
--- felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/common/Cardinality.java (original)
+++ felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/common/Cardinality.java Tue Jul 21 18:51:33 2009
@@ -19,135 +19,173 @@
 
 package org.apache.felix.sigil.model.common;
 
+
 import java.io.Serializable;
 
+
 /**
  * Immutable class representing cardinality constraints between two entities.
  * 
  */
-public class Cardinality implements Serializable {
+public class Cardinality implements Serializable
+{
 
     /**
      * 
      */
     private static final long serialVersionUID = 1L;
-    public static final Cardinality ZERO_TO_MANY = new Cardinality(0, -1);
-    public static final Cardinality ONE_TO_MANY = new Cardinality(1, -1);
-    public static final Cardinality ZERO_TO_ONE = new Cardinality(0, 1);
-    public static final Cardinality ONE_TO_ONE = new Cardinality(1, 1);
+    public static final Cardinality ZERO_TO_MANY = new Cardinality( 0, -1 );
+    public static final Cardinality ONE_TO_MANY = new Cardinality( 1, -1 );
+    public static final Cardinality ZERO_TO_ONE = new Cardinality( 0, 1 );
+    public static final Cardinality ONE_TO_ONE = new Cardinality( 1, 1 );
 
     private int min;
     private int max;
 
+
     /**
      * @param min
      *            >=0 (usually 0 or 1)
      * @param max
      *            >=min or -1 to indicate an unbounded maximum
      */
-    public Cardinality(int min, int max) {
-        if (min < 0) {
-            throw new IllegalArgumentException("Min cannot be less than 0");
+    public Cardinality( int min, int max )
+    {
+        if ( min < 0 )
+        {
+            throw new IllegalArgumentException( "Min cannot be less than 0" );
         }
 
-        if ((max < min) && (max != -1)) {
-            throw new IllegalArgumentException("Max cannot be less than min");
+        if ( ( max < min ) && ( max != -1 ) )
+        {
+            throw new IllegalArgumentException( "Max cannot be less than min" );
         }
 
         this.min = min;
         this.max = max;
     }
 
-    public int getMin() {
+
+    public int getMin()
+    {
         return min;
     }
 
-    public int getMax() {
+
+    public int getMax()
+    {
         return max;
     }
 
-    public String toString() {
-        return min + ".." + ((max == -1) ? ("n") : (Integer.toString(max)));
+
+    public String toString()
+    {
+        return min + ".." + ( ( max == -1 ) ? ( "n" ) : ( Integer.toString( max ) ) );
     }
 
-    public boolean isDefined(Cardinality cardinality) {
-        return (min <= cardinality.min) && ((max == -1) || (max >= cardinality.max));
+
+    public boolean isDefined( Cardinality cardinality )
+    {
+        return ( min <= cardinality.min ) && ( ( max == -1 ) || ( max >= cardinality.max ) );
     }
 
-    public boolean isSingleton() {
-        return (min == 1) && (max == 1);
+
+    public boolean isSingleton()
+    {
+        return ( min == 1 ) && ( max == 1 );
     }
 
-    public static Cardinality parse(String stringRep) throws IllegalArgumentException {
+
+    public static Cardinality parse( String stringRep ) throws IllegalArgumentException
+    {
         stringRep = stringRep.trim();
 
-        int dotdot = stringRep.indexOf("..");
+        int dotdot = stringRep.indexOf( ".." );
 
-        if (dotdot == -1) {
-            throw new IllegalArgumentException("Invalid cardinality string representation, expected ..");
+        if ( dotdot == -1 )
+        {
+            throw new IllegalArgumentException( "Invalid cardinality string representation, expected .." );
         }
 
-        String minStr = stringRep.substring(0, dotdot);
-        String maxStr = stringRep.substring(dotdot + 2);
+        String minStr = stringRep.substring( 0, dotdot );
+        String maxStr = stringRep.substring( dotdot + 2 );
 
-        int min = Integer.parseInt(minStr);
+        int min = Integer.parseInt( minStr );
         int max = min;
 
-        if ("n".equals(maxStr)) {
+        if ( "n".equals( maxStr ) )
+        {
             max = -1;
         }
-        else {
-            max = Integer.parseInt(maxStr);
+        else
+        {
+            max = Integer.parseInt( maxStr );
         }
 
-        return cardinality(min, max);
+        return cardinality( min, max );
     }
 
-    public static Cardinality cardinality(int min, int max) {
+
+    public static Cardinality cardinality( int min, int max )
+    {
         Cardinality c = null;
 
-        if (min == 0) {
-            if (max == 1) {
+        if ( min == 0 )
+        {
+            if ( max == 1 )
+            {
                 c = ZERO_TO_ONE;
             }
-            else if (max == -1) {
+            else if ( max == -1 )
+            {
                 c = ZERO_TO_MANY;
             }
         }
-        else if (min == 1) {
-            if (max == 1) {
+        else if ( min == 1 )
+        {
+            if ( max == 1 )
+            {
                 c = ONE_TO_ONE;
             }
-            else if (max == -1) {
+            else if ( max == -1 )
+            {
                 c = ONE_TO_MANY;
             }
         }
 
-        if (c == null)
-            c = new Cardinality(min, max);
+        if ( c == null )
+            c = new Cardinality( min, max );
 
         return c;
     }
 
-    public int hashCode() {
+
+    public int hashCode()
+    {
         return max ^ min;
     }
 
-    public boolean equals(Object o) {
-        if (o == this) {
+
+    public boolean equals( Object o )
+    {
+        if ( o == this )
+        {
             return true;
         }
 
-        if (o == null) {
+        if ( o == null )
+        {
             return false;
         }
 
-        try {
-            Cardinality c = (Cardinality) o;
+        try
+        {
+            Cardinality c = ( Cardinality ) o;
 
-            return (min == c.min) && (max == c.max);
+            return ( min == c.min ) && ( max == c.max );
         }
-        catch (ClassCastException cce) {
+        catch ( ClassCastException cce )
+        {
             return false;
         }
     }

Modified: felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/common/ExprVisitor.java
URL: http://svn.apache.org/viewvc/felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/common/ExprVisitor.java?rev=796467&r1=796466&r2=796467&view=diff
==============================================================================
--- felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/common/ExprVisitor.java (original)
+++ felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/common/ExprVisitor.java Tue Jul 21 18:51:33 2009
@@ -19,12 +19,22 @@
 
 package org.apache.felix.sigil.model.common;
 
-public interface ExprVisitor {
 
-    void visitAnd(And a);
-    void visitOr(Or o);
-    void visitNot(Not n);
-    void visitSimple(SimpleTerm st);
+public interface ExprVisitor
+{
+
+    void visitAnd( And a );
+
+
+    void visitOr( Or o );
+
+
+    void visitNot( Not n );
+
+
+    void visitSimple( SimpleTerm st );
+
+
     // if none of the above matches use this
-    void visitAny(LDAPExpr ex);
+    void visitAny( LDAPExpr ex );
 }

Modified: felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/common/Expressions.java
URL: http://svn.apache.org/viewvc/felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/common/Expressions.java?rev=796467&r1=796466&r2=796467&view=diff
==============================================================================
--- felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/common/Expressions.java (original)
+++ felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/common/Expressions.java Tue Jul 21 18:51:33 2009
@@ -19,61 +19,82 @@
 
 package org.apache.felix.sigil.model.common;
 
+
 import java.util.Map;
 
-public class Expressions {
 
-    public static LDAPExpr and(LDAPExpr... terms) {
-        return And.apply(terms);
+public class Expressions
+{
+
+    public static LDAPExpr and( LDAPExpr... terms )
+    {
+        return And.apply( terms );
     }
 
-    public static LDAPExpr or(LDAPExpr... terms) {
-        return Or.apply(terms);
+
+    public static LDAPExpr or( LDAPExpr... terms )
+    {
+        return Or.apply( terms );
     }
 
-    public static LDAPExpr not(LDAPExpr e) {
-        return Not.apply(e);
+
+    public static LDAPExpr not( LDAPExpr e )
+    {
+        return Not.apply( e );
     }
 
     public static LDAPExpr T = Bool.TRUE;
     public static LDAPExpr F = Bool.FALSE;
 
+
     // supports direct use of wildcards for ease of testing, but not literal *s
-    public static SimpleTerm ex(String name, Ops op, String rhs) {
+    public static SimpleTerm ex( String name, Ops op, String rhs )
+    {
 
-        rhs = rhs.replace('*', SimpleTerm.WILDCARD);
-        return new SimpleTerm(name, op, rhs);
+        rhs = rhs.replace( '*', SimpleTerm.WILDCARD );
+        return new SimpleTerm( name, op, rhs );
     }
 
 }
 
-class Bool implements LDAPExpr {
+class Bool implements LDAPExpr
+{
 
     /**
      * 
      */
     private static final long serialVersionUID = 1L;
-    public static final Bool TRUE = new Bool(true);
-    public static final Bool FALSE = new Bool(false);
+    public static final Bool TRUE = new Bool( true );
+    public static final Bool FALSE = new Bool( false );
 
     private boolean bool;
 
-    public Bool(boolean bool) {
+
+    public Bool( boolean bool )
+    {
         this.bool = bool;
     }
 
-    public boolean eval(Map<String, ?> map) {
+
+    public boolean eval( Map<String, ?> map )
+    {
         return bool;
     }
 
-    public void visit(ExprVisitor v) {
+
+    public void visit( ExprVisitor v )
+    {
     }
 
-    public LDAPExpr[] getChildren() {
+
+    public LDAPExpr[] getChildren()
+    {
         return CHILDLESS;
     }
 
-    public String toString() {
+
+    public String toString()
+    {
         return "(" + bool + ")";
     }
 }

Modified: felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/common/FilterValidator.java
URL: http://svn.apache.org/viewvc/felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/common/FilterValidator.java?rev=796467&r1=796466&r2=796467&view=diff
==============================================================================
--- felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/common/FilterValidator.java (original)
+++ felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/common/FilterValidator.java Tue Jul 21 18:51:33 2009
@@ -19,15 +19,20 @@
 
 package org.apache.felix.sigil.model.common;
 
-public interface FilterValidator {
+
+public interface FilterValidator
+{
 
     public static FilterValidator ACCEPT_ALL = new AcceptEverythingValidator();
 
-    boolean validate(LDAPExpr filter);
 
-    static class AcceptEverythingValidator implements FilterValidator {
+    boolean validate( LDAPExpr filter );
+
+    static class AcceptEverythingValidator implements FilterValidator
+    {
 
-        public boolean validate(LDAPExpr filter) {
+        public boolean validate( LDAPExpr filter )
+        {
             return true;
         }
 

Modified: felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/common/LDAPExpr.java
URL: http://svn.apache.org/viewvc/felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/common/LDAPExpr.java?rev=796467&r1=796466&r2=796467&view=diff
==============================================================================
--- felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/common/LDAPExpr.java (original)
+++ felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/common/LDAPExpr.java Tue Jul 21 18:51:33 2009
@@ -19,21 +19,30 @@
 
 package org.apache.felix.sigil.model.common;
 
+
 import java.io.Serializable;
 import java.util.Map;
 
-public interface LDAPExpr extends Serializable {
 
-    public static final LDAPExpr[] CHILDLESS = new LDAPExpr[] {};
+public interface LDAPExpr extends Serializable
+{
+
+    public static final LDAPExpr[] CHILDLESS = new LDAPExpr[]
+        {};
     public static LDAPExpr ACCEPT_ALL = Expressions.T;
 
+
     LDAPExpr[] getChildren();
 
-    void visit(ExprVisitor v);
 
-    boolean equals(Object other);
+    void visit( ExprVisitor v );
+
+
+    boolean equals( Object other );
+
 
     int hashCode();
 
-    boolean eval(Map<String, ?> map);
+
+    boolean eval( Map<String, ?> map );
 }

Modified: felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/common/LDAPParseException.java
URL: http://svn.apache.org/viewvc/felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/common/LDAPParseException.java?rev=796467&r1=796466&r2=796467&view=diff
==============================================================================
--- felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/common/LDAPParseException.java (original)
+++ felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/common/LDAPParseException.java Tue Jul 21 18:51:33 2009
@@ -19,7 +19,9 @@
 
 package org.apache.felix.sigil.model.common;
 
-public class LDAPParseException extends Exception {
+
+public class LDAPParseException extends Exception
+{
 
     /**
      * 
@@ -27,31 +29,39 @@
     private static final long serialVersionUID = 1L;
 
     private ParseState ps;
-    private static final String LINE_SEPARATOR = System.getProperty("line.separator", "\\r\\n");
+    private static final String LINE_SEPARATOR = System.getProperty( "line.separator", "\\r\\n" );
+
 
-    public LDAPParseException(String message, ParseState ps) {
-        super(message);
+    public LDAPParseException( String message, ParseState ps )
+    {
+        super( message );
         this.ps = ps;
     }
 
-    public LDAPParseException(String message) {
-        super(message);
+
+    public LDAPParseException( String message )
+    {
+        super( message );
     }
 
+
     @Override
-    public String getMessage() {
-        if (ps == null) {
+    public String getMessage()
+    {
+        if ( ps == null )
+        {
             return super.getMessage();
         }
 
         String basicMessage = super.getMessage();
-        StringBuffer buf = new StringBuffer(basicMessage.length() + ps.str.length() * 2);
-        buf.append(basicMessage).append(LINE_SEPARATOR);
-        buf.append(ps.str).append(LINE_SEPARATOR);
-        for (int i = 0; i < ps.pos; i++) {
-            buf.append(" ");
+        StringBuffer buf = new StringBuffer( basicMessage.length() + ps.str.length() * 2 );
+        buf.append( basicMessage ).append( LINE_SEPARATOR );
+        buf.append( ps.str ).append( LINE_SEPARATOR );
+        for ( int i = 0; i < ps.pos; i++ )
+        {
+            buf.append( " " );
         }
-        buf.append("^");
+        buf.append( "^" );
         return buf.toString();
     }
 

Modified: felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/common/LDAPParser.java
URL: http://svn.apache.org/viewvc/felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/common/LDAPParser.java?rev=796467&r1=796466&r2=796467&view=diff
==============================================================================
--- felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/common/LDAPParser.java (original)
+++ felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/common/LDAPParser.java Tue Jul 21 18:51:33 2009
@@ -19,6 +19,7 @@
 
 package org.apache.felix.sigil.model.common;
 
+
 import static org.apache.felix.sigil.model.common.Expressions.and;
 import static org.apache.felix.sigil.model.common.Expressions.not;
 import static org.apache.felix.sigil.model.common.Expressions.or;
@@ -32,193 +33,244 @@
 import java.util.ArrayList;
 import java.util.List;
 
-public class LDAPParser {
+
+public class LDAPParser
+{
 
     private static final LDAPParser parser = new LDAPParser();
 
-    public static LDAPExpr parseExpression(String strExpr) throws LDAPParseException {
-        return parser.parse(strExpr);
+
+    public static LDAPExpr parseExpression( String strExpr ) throws LDAPParseException
+    {
+        return parser.parse( strExpr );
     }
 
-    public static void main(String[] args) {
-        for (String arg : args) {
-            try {
-                System.out.println(parseExpression(arg));
-            }
-            catch (LDAPParseException e) {
-                System.out.println("Failed to parse " + arg);
+
+    public static void main( String[] args )
+    {
+        for ( String arg : args )
+        {
+            try
+            {
+                System.out.println( parseExpression( arg ) );
+            }
+            catch ( LDAPParseException e )
+            {
+                System.out.println( "Failed to parse " + arg );
                 e.printStackTrace();
             }
         }
     }
 
-    public LDAPExpr parse(String strExpr) throws LDAPParseException {
 
-        if (strExpr == null || strExpr.trim().length() == 0) {
+    public LDAPExpr parse( String strExpr ) throws LDAPParseException
+    {
+
+        if ( strExpr == null || strExpr.trim().length() == 0 )
+        {
             return LDAPExpr.ACCEPT_ALL;
         }
 
-        ParseState ps = new ParseState(strExpr);
-        LDAPExpr expr = parseExpr(ps);
+        ParseState ps = new ParseState( strExpr );
+        LDAPExpr expr = parseExpr( ps );
         ps.skipWhitespace();
-        if (!ps.isEndOfString()) {
-            error("expected end of expression ", ps);
+        if ( !ps.isEndOfString() )
+        {
+            error( "expected end of expression ", ps );
         }
         return expr;
     }
 
-    public LDAPExpr parseExpr(ParseState ps) throws LDAPParseException {
+
+    public LDAPExpr parseExpr( ParseState ps ) throws LDAPParseException
+    {
         ps.skipWhitespace();
-        if (!(ps.peek() == '(')) {
-            error("expected (", ps);
+        if ( !( ps.peek() == '(' ) )
+        {
+            error( "expected (", ps );
         }
         ps.read();
         LDAPExpr expr = null;
         ps.skipWhitespace();
         char ch = ps.peek();
-        switch (ch) {
-        case '&':
-            ps.readAndSkipWhiteSpace();
-            List<LDAPExpr> andList = new ArrayList<LDAPExpr>();
-            while (ps.peek() == '(') {
-                andList.add(parseExpr(ps));
-                ps.skipWhitespace();
-            }
-            LDAPExpr[] andArr = andList.toArray(new LDAPExpr[andList.size()]);
-            expr = and(andArr);
-            break;
-        case '|':
-            ps.readAndSkipWhiteSpace();
-            List<LDAPExpr> orList = new ArrayList<LDAPExpr>();
-            while (ps.peek() == '(') {
-                orList.add(parseExpr(ps));
-                ps.skipWhitespace();
-            }
-            LDAPExpr[] orArray = orList.toArray(new LDAPExpr[orList.size()]);
-            expr = or(orArray);
-            break;
-        case '!':
-            ps.readAndSkipWhiteSpace();
-            expr = not(parseExpr(ps));
-            break;
-        default:
-            if (isNameChar(ch)) {
-                expr = parseSimple(ps);
-            }
-            else {
-                error("unexpected character: '" + ch + "'", ps);
-            }
+        switch ( ch )
+        {
+            case '&':
+                ps.readAndSkipWhiteSpace();
+                List<LDAPExpr> andList = new ArrayList<LDAPExpr>();
+                while ( ps.peek() == '(' )
+                {
+                    andList.add( parseExpr( ps ) );
+                    ps.skipWhitespace();
+                }
+                LDAPExpr[] andArr = andList.toArray( new LDAPExpr[andList.size()] );
+                expr = and( andArr );
+                break;
+            case '|':
+                ps.readAndSkipWhiteSpace();
+                List<LDAPExpr> orList = new ArrayList<LDAPExpr>();
+                while ( ps.peek() == '(' )
+                {
+                    orList.add( parseExpr( ps ) );
+                    ps.skipWhitespace();
+                }
+                LDAPExpr[] orArray = orList.toArray( new LDAPExpr[orList.size()] );
+                expr = or( orArray );
+                break;
+            case '!':
+                ps.readAndSkipWhiteSpace();
+                expr = not( parseExpr( ps ) );
+                break;
+            default:
+                if ( isNameChar( ch ) )
+                {
+                    expr = parseSimple( ps );
+                }
+                else
+                {
+                    error( "unexpected character: '" + ch + "'", ps );
+                }
         }
         ps.skipWhitespace();
-        if (ps.peek() != ')') {
-            error("expected )", ps);
+        if ( ps.peek() != ')' )
+        {
+            error( "expected )", ps );
         }
         ps.read();
         return expr;
 
     }
 
-    void error(String message, ParseState ps) throws LDAPParseException {
-        throw new LDAPParseException(message, ps);
+
+    void error( String message, ParseState ps ) throws LDAPParseException
+    {
+        throw new LDAPParseException( message, ps );
     }
 
-    private SimpleTerm parseSimple(ParseState ps) throws LDAPParseException {
+
+    private SimpleTerm parseSimple( ParseState ps ) throws LDAPParseException
+    {
         // read name
-        StringBuffer name = new StringBuffer(16);
-        for (char c = ps.peek(); !ps.isEndOfString() && isNameChar(c); c = ps.peek()) {
+        StringBuffer name = new StringBuffer( 16 );
+        for ( char c = ps.peek(); !ps.isEndOfString() && isNameChar( c ); c = ps.peek() )
+        {
             ps.read();
-            name.append(c);
+            name.append( c );
         }
         ps.skipWhitespace();
         Ops op = null;
         // read op
-        if (ps.lookingAt("=")) {
+        if ( ps.lookingAt( "=" ) )
+        {
             op = EQ;
-            ps.skip(1);
+            ps.skip( 1 );
         }
-        else if (ps.lookingAt(">=")) {
+        else if ( ps.lookingAt( ">=" ) )
+        {
             op = GE;
-            ps.skip(2);
+            ps.skip( 2 );
         }
-        else if (ps.lookingAt("<=")) {
+        else if ( ps.lookingAt( "<=" ) )
+        {
             op = LE;
-            ps.skip(2);
+            ps.skip( 2 );
         }
-        else if (ps.lookingAt(">")) {
+        else if ( ps.lookingAt( ">" ) )
+        {
             op = GT;
-            ps.skip(1);
+            ps.skip( 1 );
         }
-        else if (ps.lookingAt("<")) {
+        else if ( ps.lookingAt( "<" ) )
+        {
             op = LT;
-            ps.skip(1);
+            ps.skip( 1 );
         }
-        else if (ps.lookingAt("-=")) {
+        else if ( ps.lookingAt( "-=" ) )
+        {
             op = APPROX;
-            ps.skip(2);
+            ps.skip( 2 );
         }
-        else if (ps.isEndOfString()) {
-            error("unexpected end of expression", ps);
-        }
-        else {
-            error("unexpected character: '" + ps.peek() + "'", ps);
+        else if ( ps.isEndOfString() )
+        {
+            error( "unexpected end of expression", ps );
+        }
+        else
+        {
+            error( "unexpected character: '" + ps.peek() + "'", ps );
         }
         ps.skipWhitespace();
 
         boolean escaped = false;
-        StringBuffer value = new StringBuffer(16);
+        StringBuffer value = new StringBuffer( 16 );
 
-        while (!ps.isEndOfString() && !Character.isWhitespace(ps.peek()) && !(ps.peek() == ')' && !escaped)) {
+        while ( !ps.isEndOfString() && !Character.isWhitespace( ps.peek() ) && !( ps.peek() == ')' && !escaped ) )
+        {
 
             char ch = ps.peek();
 
-            if (ch == '\\') {
+            if ( ch == '\\' )
+            {
                 escaped = true;
                 ps.read();
             }
-            else if (ch == '*') {
-                if (escaped) {
-                    value.append(ch);
+            else if ( ch == '*' )
+            {
+                if ( escaped )
+                {
+                    value.append( ch );
                     escaped = false;
                 }
-                else {
-                    value.append(SimpleTerm.WILDCARD);
+                else
+                {
+                    value.append( SimpleTerm.WILDCARD );
                 }
                 ps.read();
             }
-            else if (isLiteralValue(ch)) {
-                if (escaped) {
-                    error("incorrectly applied escape of '" + ch + "'", ps);
+            else if ( isLiteralValue( ch ) )
+            {
+                if ( escaped )
+                {
+                    error( "incorrectly applied escape of '" + ch + "'", ps );
                 }
-                value.append(ps.read());
+                value.append( ps.read() );
             }
-            else if (isEscapedValue(ch)) {
-                if (!escaped) {
-                    error("missing escape for '" + ch + "'", ps);
+            else if ( isEscapedValue( ch ) )
+            {
+                if ( !escaped )
+                {
+                    error( "missing escape for '" + ch + "'", ps );
                 }
-                value.append(ps.read());
+                value.append( ps.read() );
                 escaped = false;
             }
-            else {
-                error("unexpected character: '" + ps.peek() + "'", ps);
+            else
+            {
+                error( "unexpected character: '" + ps.peek() + "'", ps );
             }
         }
         ps.skipWhitespace();
 
-        SimpleTerm expr = new SimpleTerm(name.toString(), op, value.toString());
+        SimpleTerm expr = new SimpleTerm( name.toString(), op, value.toString() );
 
         return expr;
     }
 
-    private boolean isNameChar(int ch) {
-        return !(Character.isWhitespace(ch) || (ch == '(') || (ch == ')') || (ch == '<') || (ch == '>') || (ch == '=')
-                || (ch == '~') || (ch == '*') || (ch == '\\'));
+
+    private boolean isNameChar( int ch )
+    {
+        return !( Character.isWhitespace( ch ) || ( ch == '(' ) || ( ch == ')' ) || ( ch == '<' ) || ( ch == '>' )
+            || ( ch == '=' ) || ( ch == '~' ) || ( ch == '*' ) || ( ch == '\\' ) );
     }
 
-    private boolean isLiteralValue(int ch) {
-        return !(Character.isWhitespace(ch) || (ch == '(') || (ch == ')') || (ch == '*'));
+
+    private boolean isLiteralValue( int ch )
+    {
+        return !( Character.isWhitespace( ch ) || ( ch == '(' ) || ( ch == ')' ) || ( ch == '*' ) );
     }
 
-    private boolean isEscapedValue(int ch) {
-        return (ch == '(') || (ch == ')') || (ch == '*');
+
+    private boolean isEscapedValue( int ch )
+    {
+        return ( ch == '(' ) || ( ch == ')' ) || ( ch == '*' );
     }
 }

Modified: felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/common/Not.java
URL: http://svn.apache.org/viewvc/felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/common/Not.java?rev=796467&r1=796466&r2=796467&view=diff
==============================================================================
--- felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/common/Not.java (original)
+++ felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/common/Not.java Tue Jul 21 18:51:33 2009
@@ -19,9 +19,12 @@
 
 package org.apache.felix.sigil.model.common;
 
+
 import java.util.Map;
 
-public class Not implements LDAPExpr {
+
+public class Not implements LDAPExpr
+{
 
     /**
      * 
@@ -29,58 +32,82 @@
     private static final long serialVersionUID = 1L;
     private LDAPExpr[] children;
 
-    public static LDAPExpr apply(LDAPExpr e) {
-        if (e == null) {
-            throw new NullPointerException("cannot apply Not to a null expression");
+
+    public static LDAPExpr apply( LDAPExpr e )
+    {
+        if ( e == null )
+        {
+            throw new NullPointerException( "cannot apply Not to a null expression" );
         }
-        if (e.equals(Expressions.T)) {
+        if ( e.equals( Expressions.T ) )
+        {
             return Expressions.F;
         }
-        if (e.equals(Expressions.F)) {
+        if ( e.equals( Expressions.F ) )
+        {
             return Expressions.T;
         }
-        return new Not(e);
+        return new Not( e );
     }
 
-    private Not(LDAPExpr child) {
-        this.children = new LDAPExpr[] { child };
+
+    private Not( LDAPExpr child )
+    {
+        this.children = new LDAPExpr[]
+            { child };
     }
 
-    public boolean eval(Map<String, ?> map) {
-        return !children[0].eval(map);
+
+    public boolean eval( Map<String, ?> map )
+    {
+        return !children[0].eval( map );
     }
 
-    public LDAPExpr getEx() {
+
+    public LDAPExpr getEx()
+    {
         return children[0];
     }
 
-    public void visit(ExprVisitor v) {
-        v.visitNot(this);
+
+    public void visit( ExprVisitor v )
+    {
+        v.visitNot( this );
     }
 
-    public LDAPExpr[] getChildren() {
+
+    public LDAPExpr[] getChildren()
+    {
         return children;
     }
 
-    public void setChild(LDAPExpr child) {
-        this.children = new LDAPExpr[] { child };
+
+    public void setChild( LDAPExpr child )
+    {
+        this.children = new LDAPExpr[]
+            { child };
     }
 
+
     @Override
-    public boolean equals(Object other) {
-        if (other instanceof Not) {
-            Not that = (Not) other;
-            return children[0].equals(that.children[0]);
+    public boolean equals( Object other )
+    {
+        if ( other instanceof Not )
+        {
+            Not that = ( Not ) other;
+            return children[0].equals( that.children[0] );
         }
         return false;
     }
 
+
     @Override
-    public String toString() {
-        StringBuffer buf = new StringBuffer(256);
-        buf.append("(!");
-        buf.append(" ").append(children[0]).append(" ");
-        buf.append(")");
+    public String toString()
+    {
+        StringBuffer buf = new StringBuffer( 256 );
+        buf.append( "(!" );
+        buf.append( " " ).append( children[0] ).append( " " );
+        buf.append( ")" );
         return buf.toString();
     }
 

Modified: felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/common/Ops.java
URL: http://svn.apache.org/viewvc/felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/common/Ops.java?rev=796467&r1=796466&r2=796467&view=diff
==============================================================================
--- felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/common/Ops.java (original)
+++ felix/trunk/sigil/common/core/src/org/apache/felix/sigil/model/common/Ops.java Tue Jul 21 18:51:33 2009
@@ -19,26 +19,30 @@
 
 package org.apache.felix.sigil.model.common;
 
-public enum Ops {
+
+public enum Ops
+{
     EQ, GE, LE, GT, LT, APPROX;
 
     @Override
-    public String toString() {
-        switch (this) {
-        case EQ:
-            return "=";
-        case GE:
-            return ">=";
-        case LE:
-            return "<=";
-        case GT:
-            return ">";
-        case LT:
-            return "<";
-        case APPROX:
-            return "~=";
-        default:
-            return super.toString();
+    public String toString()
+    {
+        switch ( this )
+        {
+            case EQ:
+                return "=";
+            case GE:
+                return ">=";
+            case LE:
+                return "<=";
+            case GT:
+                return ">";
+            case LT:
+                return "<";
+            case APPROX:
+                return "~=";
+            default:
+                return super.toString();
         }
     }