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: package org.apache.tools.ant.types.resources.selectors;
019:
020: import java.util.Stack;
021: import java.util.Vector;
022: import java.util.Iterator;
023: import java.util.Collections;
024:
025: import org.apache.tools.ant.Project;
026: import org.apache.tools.ant.BuildException;
027: import org.apache.tools.ant.types.DataType;
028:
029: /**
030: * ResourceSelector container.
031: * @since Ant 1.7
032: */
033: public class ResourceSelectorContainer extends DataType {
034:
035: private Vector v = new Vector();
036:
037: /**
038: * Default constructor.
039: */
040: public ResourceSelectorContainer() {
041: }
042:
043: /**
044: * Construct a new ResourceSelectorContainer with the specified array of selectors.
045: * @param r the ResourceSelector[] to add.
046: */
047: public ResourceSelectorContainer(ResourceSelector[] r) {
048: for (int i = 0; i < r.length; i++) {
049: add(r[i]);
050: }
051: }
052:
053: /**
054: * Add a ResourceSelector to the container.
055: * @param s the ResourceSelector to add.
056: */
057: public void add(ResourceSelector s) {
058: if (isReference()) {
059: throw noChildrenAllowed();
060: }
061: if (s == null) {
062: return;
063: }
064: v.add(s);
065: setChecked(false);
066: }
067:
068: /**
069: * Learn whether this ResourceSelectorContainer has selectors.
070: * @return boolean indicating whether selectors have been added to the container.
071: */
072: public boolean hasSelectors() {
073: if (isReference()) {
074: return ((ResourceSelectorContainer) getCheckedRef())
075: .hasSelectors();
076: }
077: dieOnCircularReference();
078: return !v.isEmpty();
079: }
080:
081: /**
082: * Get the count of nested selectors.
083: * @return the selector count as int.
084: */
085: public int selectorCount() {
086: if (isReference()) {
087: return ((ResourceSelectorContainer) getCheckedRef())
088: .selectorCount();
089: }
090: dieOnCircularReference();
091: return v.size();
092: }
093:
094: /**
095: * Return an Iterator over the nested selectors.
096: * @return Iterator of ResourceSelectors.
097: */
098: public Iterator getSelectors() {
099: if (isReference()) {
100: return ((ResourceSelectorContainer) getCheckedRef())
101: .getSelectors();
102: }
103: dieOnCircularReference();
104: return Collections.unmodifiableList(v).iterator();
105: }
106:
107: /**
108: * Overrides the version from DataType to recurse on nested ResourceSelectors.
109: * @param stk the Stack of references.
110: * @param p the Project to resolve against.
111: * @throws BuildException on error.
112: */
113: protected void dieOnCircularReference(Stack stk, Project p)
114: throws BuildException {
115: if (isChecked()) {
116: return;
117: }
118: if (isReference()) {
119: super .dieOnCircularReference(stk, p);
120: } else {
121: for (Iterator i = v.iterator(); i.hasNext();) {
122: Object o = i.next();
123: if (o instanceof DataType) {
124: stk.push(o);
125: invokeCircularReferenceCheck((DataType) o, stk, p);
126: }
127: }
128: setChecked(true);
129: }
130: }
131:
132: }
|