Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ConditionalArgumentSet |
|
| 4.75;4.75 |
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 | 0 | public class ConditionalArgumentSet extends DataType { |
34 | ||
35 | //------------------------------------------------------------------ Fields | |
36 | ||
37 | /** | |
38 | * Cached arguments and nested ConditionalArgumentSet objects | |
39 | */ | |
40 | 0 | 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 | 0 | if (isReference()) |
53 | 0 | throw noChildrenAllowed(); |
54 | 0 | list.add(argument); |
55 | ||
56 | 0 | } |
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 | 0 | if (isReference()) |
66 | 0 | throw noChildrenAllowed(); |
67 | 0 | list.add(set); |
68 | ||
69 | 0 | } |
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 | 0 | if (!checked) { |
80 | 0 | Stack stk = new Stack(); |
81 | 0 | stk.push(this); |
82 | 0 | dieOnCircularReference(stk, project); |
83 | } | |
84 | ||
85 | // Recursively work through the tree of ConditionalArgumentSet objects | |
86 | // and accumulate the list of ConditionalArgument objects. | |
87 | 0 | ArrayList mergedList = new ArrayList(list.size()); |
88 | 0 | for (int i = 0; i < list.size(); i++) { |
89 | 0 | Object o = list.get(i); |
90 | 0 | ConditionalArgumentSet nestedSet = null; |
91 | 0 | if (o instanceof Reference) { |
92 | 0 | o = ((Reference)o).getReferencedObject(project); |
93 | // Only references to this class are allowed | |
94 | 0 | if (!o.getClass().isInstance(this)) |
95 | 0 | throw new BuildException(Launcher.getLocalizedString("cannot.reference", this.getClass().getName())); |
96 | 0 | nestedSet = (ConditionalArgumentSet)o; |
97 | 0 | } else if (o.getClass().isInstance(this)) { |
98 | 0 | nestedSet = (ConditionalArgumentSet)o; |
99 | 0 | } else if (o instanceof ConditionalArgument) { |
100 | 0 | mergedList.add(o); |
101 | } else { | |
102 | 0 | throw new BuildException(Launcher.getLocalizedString("cannot.nest", this.getClass().getName())); |
103 | } | |
104 | 0 | if (nestedSet != null) |
105 | 0 | mergedList.addAll(nestedSet.getList()); |
106 | } | |
107 | ||
108 | 0 | 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 | 0 | if (!list.isEmpty()) |
123 | 0 | throw tooManyAttributes(); |
124 | 0 | list.add(r); |
125 | 0 | super.setRefid(r); |
126 | ||
127 | 0 | } |
128 | ||
129 | } |