pier 2004/03/30 08:17:58
Added: src/kernel/org/apache/cocoon/kernel/resolution
CompoundResolver.java
Log:
The "CompoundResolver" will be used to recursively resolve a resource in
an instance dependancy tree.
Revision Changes Path
1.1
cocoon-2.2/src/kernel/org/apache/cocoon/kernel/resolution/CompoundResolver.java
Index: CompoundResolver.java
===================================================================
/* ==========================================================================
*
*
*
* Copyright 2004 The Apache Software Foundation.
*
*
*
* 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.apache.cocoon.kernel.resolution;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
/**
* <p>A [EMAIL PROTECTED] CompoundResolver} is an implementation of the
[EMAIL PROTECTED] Resolver}
* interface delegating resource resolution to other [EMAIL PROTECTED]
Resolver}s.</p>
*
* <p>This instance is backed up by an [EMAIL PROTECTED] ArrayList}.</p>
*
* @author <a href="mailto:[EMAIL PROTECTED]">Pier Fumagalli</a>
* @author <a href="http://www.vnunet.com/">VNU Business Publications</a>
* @version 1.0 (CVS $Revision: 1.1 $)
*/
public class CompoundResolver extends AbstractSet implements Resolver {
/** <p>Our [EMAIL PROTECTED] List}.</p> */
private List list = new ArrayList();
/**
* <p>Create a new [EMAIL PROTECTED] CompoundResolver} instance backed up
by another
* [EMAIL PROTECTED] Resolver} instance.</p>
*/
public CompoundResolver() {
super();
}
/**
* <p>Resolve a specified name into a [EMAIL PROTECTED] Resource}.</p>
*
* @param name a non null [EMAIL PROTECTED] String} identifying the
resource name.
* @return a [EMAIL PROTECTED] Resource} instance or <b>null</b> if not
found.
*/
public Resource resolve(String name) {
Resolver r[] = (Resolver[])list.toArray(new Resolver[list.size()]);
for (int x = 0; x < r.length; x ++) {
Resource s = r[x].resolve(name);
if (s != null) return(s);
}
return(null);
}
/**
* <p>Returns an [EMAIL PROTECTED] Iterator} over all [EMAIL PROTECTED]
Resolver} elements
* contained in this [EMAIL PROTECTED] CompoundResolver}.</p>
*
* @return a <b>non null</b> [EMAIL PROTECTED] Iterator} instance.
*/
public Iterator iterator() {
return(this.list.iterator());
}
/**
* <p>Returns the number of all [EMAIL PROTECTED] Resolver} elements
contained in this
* [EMAIL PROTECTED] CompoundResolver}.</p>
*
* @return a non negative number.
*/
public int size() {
return(this.list.size());
}
/**
* <p>Checks whether this [EMAIL PROTECTED] CompoundResolver} contains
the specified
* object instance.</p>
*
* <p>Note that this implementation <b>does not</b> check on sub-elements
* implementing the [EMAIL PROTECTED] Collection} iterface, it is
therefore non
* recursive.</p>
*
* @see #add(Object)
* @return <b>true</b> if this [EMAIL PROTECTED] CompoundResolver}
contains the
* specified [EMAIL PROTECTED] Object} directly, <b>false</b>
otherwise.
*/
public boolean contains(Object object) {
return(this.list.contains(object));
}
/**
* <p>Return a [EMAIL PROTECTED] Resolver} array of all elements
contained in this
* [EMAIL PROTECTED] CompoundResolver}.</p>
*
* @return an <b>non null</b> array castable to a [EMAIL PROTECTED]
Resolver} array.
*/
public Object[] toArray() {
return(this.list.toArray(new Resolver[this.list.size()]));
}
/**
* <p>Return an array of all elements contained in this
* [EMAIL PROTECTED] CompoundResolver}.</p>
*
* @param array the array into which the elements of the collection are to
* be stored, if it is big enough; otherwise, a new array of
* the same runtime type is allocated for this purpose.
* @return an <b>non null</b> array castable to a [EMAIL PROTECTED]
Resolver} array.
* @throws ArrayStoreException if the runtime type of the specified array
* is not a supertype of the runtime type of every element in this
* collection.
*/
public Object[] toArray(Object array[]) {
return(this.list.toArray(array));
}
/**
* <p>Ensures that this [EMAIL PROTECTED] CompoundResolver} contains the
specified
* element.</p>
*
* <p>Note that this method will check all [EMAIL PROTECTED] Resolver}s
contained in
* this [EMAIL PROTECTED] CompoundResolver}, and recursively the contents
of all those
* elements also implementing the [EMAIL PROTECTED] Collection} interface.
*
* @param object a [EMAIL PROTECTED] Object} castable to [EMAIL
PROTECTED] Resolver}.
* @return <b>true</b> if this instance changed in result of the call.
* @throws ClassCastException if the specified [EMAIL PROTECTED] Object}
is not an
* instance of [EMAIL PROTECTED] Resolver}.
*/
public boolean add(Object object) {
if (object == null) return(false);
Resolver resolver = (Resolver) object;
Iterator iterator = this.list.iterator();
while (iterator.hasNext()) {
Resolver current = (Resolver) iterator.next();
if (current == resolver) return(false);
if (!(current instanceof Collection)) continue;
if (((Collection)current).contains(resolver)) return(false);
}
return(this.list.add(resolver));
}
/**
* <p>Ensures that this [EMAIL PROTECTED] CompoundResolver} does not
contain the
* specified element.</p>
*
* <p>Note that this implementation <b>does not</b> check on sub-elements
* implementing the [EMAIL PROTECTED] Collection} iterface, it is
therefore non
* recursive.</p>
*
* @param object a [EMAIL PROTECTED] Object} to check.
* @return <b>true</b> if this instance changed in result of the call.
*/
public boolean remove(Object object) {
if (object == null) return(false);
return(this.list.remove(object));
}
/**
* <p>Removes all of the elements from this [EMAIL PROTECTED]
CompoundResolver}.</p>
*/
public void clear() {
this.list.clear();
}
}