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.types;
  18. import java.util.Vector;
  19. import org.apache.tools.ant.BuildException;
  20. /**
  21. * An AntFileReader is a wrapper class that encloses the classname
  22. * and configuration of a Configurable FilterReader.
  23. *
  24. */
  25. public final class AntFilterReader
  26. extends DataType
  27. implements Cloneable {
  28. private String className;
  29. private final Vector parameters = new Vector();
  30. private Path classpath;
  31. public final void setClassName(final String className) {
  32. this.className = className;
  33. }
  34. public final String getClassName() {
  35. return className;
  36. }
  37. public final void addParam(final Parameter param) {
  38. parameters.addElement(param);
  39. }
  40. /**
  41. * Set the classpath to load the FilterReader through (attribute).
  42. */
  43. public final void setClasspath(Path classpath) {
  44. if (isReference()) {
  45. throw tooManyAttributes();
  46. }
  47. if (this.classpath == null) {
  48. this.classpath = classpath;
  49. } else {
  50. this.classpath.append(classpath);
  51. }
  52. }
  53. /**
  54. * Set the classpath to load the FilterReader through (nested element).
  55. */
  56. public final Path createClasspath() {
  57. if (isReference()) {
  58. throw noChildrenAllowed();
  59. }
  60. if (this.classpath == null) {
  61. this.classpath = new Path(getProject());
  62. }
  63. return this.classpath.createPath();
  64. }
  65. /**
  66. * Get the classpath
  67. */
  68. public final Path getClasspath() {
  69. return classpath;
  70. }
  71. /**
  72. * Set the classpath to load the FilterReader through via
  73. * reference (attribute).
  74. */
  75. public void setClasspathRef(Reference r) {
  76. if (isReference()) {
  77. throw tooManyAttributes();
  78. }
  79. createClasspath().setRefid(r);
  80. }
  81. public final Parameter[] getParams() {
  82. Parameter[] params = new Parameter[parameters.size()];
  83. parameters.copyInto(params);
  84. return params;
  85. }
  86. /**
  87. * Makes this instance in effect a reference to another AntFilterReader
  88. * instance.
  89. *
  90. * <p>You must not set another attribute or nest elements inside
  91. * this element if you make it a reference.</p>
  92. *
  93. * @param r the reference to which this instance is associated
  94. * @exception BuildException if this instance already has been configured.
  95. */
  96. public void setRefid(Reference r) throws BuildException {
  97. if (!parameters.isEmpty() || className != null
  98. || classpath != null) {
  99. throw tooManyAttributes();
  100. }
  101. // change this to get the objects from the other reference
  102. Object o = r.getReferencedObject(getProject());
  103. if (o instanceof AntFilterReader) {
  104. AntFilterReader afr = (AntFilterReader) o;
  105. setClassName(afr.getClassName());
  106. setClasspath(afr.getClasspath());
  107. Parameter[] p = afr.getParams();
  108. if (p != null) {
  109. for (int i = 0; i < p.length; i++) {
  110. addParam(p[i]);
  111. }
  112. }
  113. } else {
  114. String msg = r.getRefId() + " doesn\'t refer to a FilterReader";
  115. throw new BuildException(msg);
  116. }
  117. super.setRefid(r);
  118. }
  119. }