001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.util;
020:
021: import java.util.List;
022: import java.util.Iterator;
023: import java.util.ArrayList;
024: import java.util.Collections;
025: import org.apache.tools.ant.types.Mapper;
026:
027: /**
028: * A <code>FileNameMapper</code> that contains
029: * other <code>FileNameMapper</code>s.
030: * @see FileNameMapper
031: */
032: public abstract class ContainerMapper implements FileNameMapper {
033:
034: private List mappers = new ArrayList();
035:
036: /**
037: * Add a <code>Mapper</code>.
038: * @param mapper the <code>Mapper</code> to add.
039: */
040: public void addConfiguredMapper(Mapper mapper) {
041: add(mapper.getImplementation());
042: }
043:
044: /**
045: * An add configured version of the add method.
046: * This class used to contain an add method and an
047: * addConfiguredMapper method. Dur to ordering,
048: * the add method was always called first. This
049: * addConfigued method has been added to allow
050: * chaining to work correctly.
051: * @param fileNameMapper a <code>FileNameMapper</code>.
052: */
053: public void addConfigured(FileNameMapper fileNameMapper) {
054: add(fileNameMapper);
055: }
056:
057: /**
058: * Add a <code>FileNameMapper</code>.
059: * @param fileNameMapper a <code>FileNameMapper</code>.
060: * @throws IllegalArgumentException if attempting to add this
061: * <code>ContainerMapper</code> to itself, or if the specified
062: * <code>FileNameMapper</code> is itself a <code>ContainerMapper</code>
063: * that contains this <code>ContainerMapper</code>.
064: */
065: public synchronized void add(FileNameMapper fileNameMapper) {
066: if (this == fileNameMapper
067: || (fileNameMapper instanceof ContainerMapper && ((ContainerMapper) fileNameMapper)
068: .contains(this ))) {
069: throw new IllegalArgumentException(
070: "Circular mapper containment condition detected");
071: } else {
072: mappers.add(fileNameMapper);
073: }
074: }
075:
076: /**
077: * Return <code>true</code> if this <code>ContainerMapper</code> or any of
078: * its sub-elements contains the specified <code>FileNameMapper</code>.
079: * @param fileNameMapper the <code>FileNameMapper</code> to search for.
080: * @return <code>boolean</code>.
081: */
082: protected synchronized boolean contains(
083: FileNameMapper fileNameMapper) {
084: boolean foundit = false;
085: for (Iterator iter = mappers.iterator(); iter.hasNext()
086: && !foundit;) {
087: FileNameMapper next = (FileNameMapper) (iter.next());
088: foundit |= (next == fileNameMapper || (next instanceof ContainerMapper && ((ContainerMapper) next)
089: .contains(fileNameMapper)));
090: }
091: return foundit;
092: }
093:
094: /**
095: * Get the <code>List</code> of <code>FileNameMapper</code>s.
096: * @return <code>List</code>.
097: */
098: public synchronized List getMappers() {
099: return Collections.unmodifiableList(mappers);
100: }
101:
102: /**
103: * Empty implementation.
104: * @param ignore ignored.
105: */
106: public void setFrom(String ignore) {
107: //Empty
108: }
109:
110: /**
111: * Empty implementation.
112: * @param ignore ignored.
113: */
114: public void setTo(String ignore) {
115: //Empty
116: }
117:
118: }
|