KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > launcher > types > ConditionalVariableSet


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.launcher.types;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Stack JavaDoc;
21 import org.apache.commons.launcher.Launcher;
22 import org.apache.tools.ant.BuildException;
23 import org.apache.tools.ant.types.DataType;
24 import org.apache.tools.ant.types.Reference;
25
26 /**
27  * A class that represents a set of nested elements of
28  * {@link ConditionalVariable} objects.
29  *
30  * @author Patrick Luby
31  */

32 public class ConditionalVariableSet extends DataType {
33
34     //------------------------------------------------------------------ Fields
35

36     /**
37      * Cached variables and nested ConditionalVariableSet objects
38      */

39     private ArrayList JavaDoc list = new ArrayList JavaDoc();
40
41     //----------------------------------------------------------------- Methods
42

43     /**
44      * Add a {@link ConditionalVariable}.
45      *
46      * @param variable the {@link ConditionalVariable} to be
47      * added
48      */

49     protected void addConditionalvariable(ConditionalVariable variable) {
50
51         if (isReference())
52             throw noChildrenAllowed();
53         list.add(variable);
54
55     }
56
57     /**
58      * Add a {@link ConditionalVariableSet}.
59      *
60      * @param set the {@link ConditionalVariableSet} to be added
61      */

62     protected void addConditionalvariableset(ConditionalVariableSet set) {
63
64         if (isReference())
65             throw noChildrenAllowed();
66         list.add(set);
67
68     }
69
70     /**
71      * Get {@link ConditionalVariable} instances.
72      *
73      * @return the {@link ConditionalVariable} instances
74      */

75     public ArrayList JavaDoc getList() {
76
77         // Make sure we don't have a circular reference to this instance
78
if (!checked) {
79             Stack JavaDoc stk = new Stack JavaDoc();
80             stk.push(this);
81             dieOnCircularReference(stk, project);
82         }
83
84         // Recursively work through the tree of ConditionalVariableSet objects
85
// and accumulate the list of ConditionalVariable objects.
86
ArrayList JavaDoc mergedList = new ArrayList JavaDoc(list.size());
87         for (int i = 0; i < list.size(); i++) {
88             Object JavaDoc o = list.get(i);
89             ConditionalVariableSet nestedSet = null;
90             if (o instanceof Reference) {
91                 o = ((Reference)o).getReferencedObject(project);
92                 // Only references to this class are allowed
93
if (!o.getClass().isInstance(this))
94                     throw new BuildException(Launcher.getLocalizedString("cannot.reference", this.getClass().getName()));
95                 nestedSet = (ConditionalVariableSet)o;
96             } else if (o.getClass().isInstance(this)) {
97                 nestedSet = (ConditionalVariableSet)o;
98             } else if (o instanceof ConditionalVariable) {
99                 mergedList.add(o);
100             } else {
101                 throw new BuildException(Launcher.getLocalizedString("cannot.nest", this.getClass().getName()));
102             }
103             if (nestedSet != null)
104                 mergedList.addAll(nestedSet.getList());
105         }
106
107         return mergedList;
108
109     }
110
111     /**
112      * Makes this instance a reference to another instance. You must not
113      * set another attribute or nest elements inside this element if you
114      * make it a reference.
115      *
116      * @param r the reference to another {@link ConditionalVariableSet}
117      * instance
118      */

119     public void setRefid(Reference r) throws BuildException {
120
121         if (!list.isEmpty())
122             throw tooManyAttributes();
123         list.add(r);
124         super.setRefid(r);
125
126     }
127
128 }
129
Popular Tags