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. package org.apache.commons.launcher.types;
  17. import java.util.ArrayList;
  18. import java.util.Stack;
  19. import org.apache.commons.launcher.Launcher;
  20. import org.apache.tools.ant.BuildException;
  21. import org.apache.tools.ant.types.DataType;
  22. import org.apache.tools.ant.types.Reference;
  23. /**
  24. * A class that represents a set of nested elements of
  25. * {@link ConditionalVariable} objects.
  26. *
  27. * @author Patrick Luby
  28. */
  29. public class ConditionalVariableSet extends DataType {
  30. //------------------------------------------------------------------ Fields
  31. /**
  32. * Cached variables and nested ConditionalVariableSet objects
  33. */
  34. private ArrayList list = new ArrayList();
  35. //----------------------------------------------------------------- Methods
  36. /**
  37. * Add a {@link ConditionalVariable}.
  38. *
  39. * @param variable the {@link ConditionalVariable} to be
  40. * added
  41. */
  42. protected void addConditionalvariable(ConditionalVariable variable) {
  43. if (isReference())
  44. throw noChildrenAllowed();
  45. list.add(variable);
  46. }
  47. /**
  48. * Add a {@link ConditionalVariableSet}.
  49. *
  50. * @param set the {@link ConditionalVariableSet} to be added
  51. */
  52. protected void addConditionalvariableset(ConditionalVariableSet set) {
  53. if (isReference())
  54. throw noChildrenAllowed();
  55. list.add(set);
  56. }
  57. /**
  58. * Get {@link ConditionalVariable} instances.
  59. *
  60. * @return the {@link ConditionalVariable} instances
  61. */
  62. public ArrayList getList() {
  63. // Make sure we don't have a circular reference to this instance
  64. if (!checked) {
  65. Stack stk = new Stack();
  66. stk.push(this);
  67. dieOnCircularReference(stk, project);
  68. }
  69. // Recursively work through the tree of ConditionalVariableSet objects
  70. // and accumulate the list of ConditionalVariable objects.
  71. ArrayList mergedList = new ArrayList(list.size());
  72. for (int i = 0; i < list.size(); i++) {
  73. Object o = list.get(i);
  74. ConditionalVariableSet nestedSet = null;
  75. if (o instanceof Reference) {
  76. o = ((Reference)o).getReferencedObject(project);
  77. // Only references to this class are allowed
  78. if (!o.getClass().isInstance(this))
  79. throw new BuildException(Launcher.getLocalizedString("cannot.reference", this.getClass().getName()));
  80. nestedSet = (ConditionalVariableSet)o;
  81. } else if (o.getClass().isInstance(this)) {
  82. nestedSet = (ConditionalVariableSet)o;
  83. } else if (o instanceof ConditionalVariable) {
  84. mergedList.add(o);
  85. } else {
  86. throw new BuildException(Launcher.getLocalizedString("cannot.nest", this.getClass().getName()));
  87. }
  88. if (nestedSet != null)
  89. mergedList.addAll(nestedSet.getList());
  90. }
  91. return mergedList;
  92. }
  93. /**
  94. * Makes this instance a reference to another instance. You must not
  95. * set another attribute or nest elements inside this element if you
  96. * make it a reference.
  97. *
  98. * @param r the reference to another {@link ConditionalVariableSet}
  99. * instance
  100. */
  101. public void setRefid(Reference r) throws BuildException {
  102. if (!list.isEmpty())
  103. throw tooManyAttributes();
  104. list.add(r);
  105. super.setRefid(r);
  106. }
  107. }