1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.launcher.types;
19
20 import java.util.ArrayList;
21 import java.util.Stack;
22 import org.apache.commons.launcher.Launcher;
23 import org.apache.tools.ant.BuildException;
24 import org.apache.tools.ant.types.DataType;
25 import org.apache.tools.ant.types.Reference;
26
27 /**
28 * A class that represents a set of nested elements of
29 * {@link ConditionalArgument} objects.
30 *
31 * @author Patrick Luby
32 */
33 public class ConditionalArgumentSet extends DataType {
34
35 //------------------------------------------------------------------ Fields
36
37 /**
38 * Cached arguments and nested ConditionalArgumentSet objects
39 */
40 private ArrayList list = new ArrayList();
41
42 //----------------------------------------------------------------- Methods
43
44 /**
45 * Add a {@link ConditionalArgument}.
46 *
47 * @param argument the {@link ConditionalArgument} to be
48 * added
49 */
50 protected void addConditionalargument(ConditionalArgument argument) {
51
52 if (isReference())
53 throw noChildrenAllowed();
54 list.add(argument);
55
56 }
57
58 /**
59 * Add a {@link ConditionalArgumentSet}.
60 *
61 * @param set the {@link ConditionalArgumentSet} to be added
62 */
63 protected void addConditionalargumentset(ConditionalArgumentSet set) {
64
65 if (isReference())
66 throw noChildrenAllowed();
67 list.add(set);
68
69 }
70
71 /**
72 * Get {@link ConditionalArgument} instances.
73 *
74 * @return the {@link ConditionalArgument} instances
75 */
76 public ArrayList getList() {
77
78 // Make sure we don't have a circular reference to this instance
79 if (!checked) {
80 Stack stk = new Stack();
81 stk.push(this);
82 dieOnCircularReference(stk, project);
83 }
84
85 // Recursively work through the tree of ConditionalArgumentSet objects
86 // and accumulate the list of ConditionalArgument objects.
87 ArrayList mergedList = new ArrayList(list.size());
88 for (int i = 0; i < list.size(); i++) {
89 Object o = list.get(i);
90 ConditionalArgumentSet nestedSet = null;
91 if (o instanceof Reference) {
92 o = ((Reference)o).getReferencedObject(project);
93 // Only references to this class are allowed
94 if (!o.getClass().isInstance(this))
95 throw new BuildException(Launcher.getLocalizedString("cannot.reference", this.getClass().getName()));
96 nestedSet = (ConditionalArgumentSet)o;
97 } else if (o.getClass().isInstance(this)) {
98 nestedSet = (ConditionalArgumentSet)o;
99 } else if (o instanceof ConditionalArgument) {
100 mergedList.add(o);
101 } else {
102 throw new BuildException(Launcher.getLocalizedString("cannot.nest", this.getClass().getName()));
103 }
104 if (nestedSet != null)
105 mergedList.addAll(nestedSet.getList());
106 }
107
108 return mergedList;
109
110 }
111
112 /**
113 * Makes this instance a reference to another instance. You must not
114 * set another attribute or nest elements inside this element if you
115 * make it a reference.
116 *
117 * @param r the reference to another {@link ConditionalArgumentSet}
118 * instance
119 */
120 public void setRefid(Reference r) throws BuildException {
121
122 if (!list.isEmpty())
123 throw tooManyAttributes();
124 list.add(r);
125 super.setRefid(r);
126
127 }
128
129 }