1. /*
  2. * Copyright 2002,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.tools.ant.taskdefs.optional.extension;
  18. import java.util.ArrayList;
  19. import java.util.Arrays;
  20. import org.apache.tools.ant.BuildException;
  21. import org.apache.tools.ant.Project;
  22. import org.apache.tools.ant.types.DataType;
  23. import org.apache.tools.ant.types.FileSet;
  24. import org.apache.tools.ant.types.Reference;
  25. /**
  26. * The Extension set lists a set of "Optional Packages" /
  27. * "Extensions".
  28. *
  29. * @version $Revision: 1.4.2.4 $ $Date: 2004/03/09 17:01:45 $
  30. * @ant.data-type name="extension-set"
  31. */
  32. public class ExtensionSet
  33. extends DataType {
  34. /**
  35. * ExtensionAdapter objects representing extensions.
  36. */
  37. private final ArrayList extensions = new ArrayList();
  38. /**
  39. * Filesets specifying all the extensions wanted.
  40. */
  41. private final ArrayList extensionsFilesets = new ArrayList();
  42. /**
  43. * Adds an extension that this library requires.
  44. *
  45. * @param extensionAdapter an extension that this library requires.
  46. */
  47. public void addExtension(final ExtensionAdapter extensionAdapter) {
  48. extensions.add(extensionAdapter);
  49. }
  50. /**
  51. * Adds a set of files about which extensions data will be extracted.
  52. *
  53. * @param fileSet a set of files about which extensions data will be extracted.
  54. */
  55. public void addLibfileset(final LibFileSet fileSet) {
  56. extensionsFilesets.add(fileSet);
  57. }
  58. /**
  59. * Adds a set of files about which extensions data will be extracted.
  60. *
  61. * @param fileSet a set of files about which extensions data will be extracted.
  62. */
  63. public void addFileset(final FileSet fileSet) {
  64. extensionsFilesets.add(fileSet);
  65. }
  66. /**
  67. * Extract a set of Extension objects from the ExtensionSet.
  68. *
  69. * @param project the project instance.
  70. * @return an array containing the Extensions from this set
  71. * @throws BuildException if an error occurs
  72. */
  73. public Extension[] toExtensions(final Project project)
  74. throws BuildException {
  75. final ArrayList extensionsList = ExtensionUtil.toExtensions(extensions);
  76. ExtensionUtil.extractExtensions(project, extensionsList, extensionsFilesets);
  77. return (Extension[]) extensionsList.toArray(new Extension[extensionsList.size()]);
  78. }
  79. /**
  80. * Makes this instance in effect a reference to another ExtensionSet
  81. * instance.
  82. *
  83. * <p>You must not set another attribute or nest elements inside
  84. * this element if you make it a reference.</p>
  85. *
  86. * @param reference the reference to which this instance is associated
  87. * @exception BuildException if this instance already has been configured.
  88. */
  89. public void setRefid(final Reference reference)
  90. throws BuildException {
  91. if (!extensions.isEmpty() || !extensionsFilesets.isEmpty()) {
  92. throw tooManyAttributes();
  93. }
  94. // change this to get the objects from the other reference
  95. final Object object =
  96. reference.getReferencedObject(getProject());
  97. if (object instanceof ExtensionSet) {
  98. final ExtensionSet other = (ExtensionSet) object;
  99. extensions.addAll(other.extensions);
  100. extensionsFilesets.addAll(other.extensionsFilesets);
  101. } else {
  102. final String message =
  103. reference.getRefId() + " doesn\'t refer to a ExtensionSet";
  104. throw new BuildException(message);
  105. }
  106. super.setRefid(reference);
  107. }
  108. /**
  109. * @see java.lang.Object#toString()
  110. */
  111. public String toString() {
  112. return "ExtensionSet" + Arrays.asList(toExtensions(getProject()));
  113. }
  114. }