1. /*
  2. * Copyright 2001-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.Stack;
  19. import org.apache.tools.ant.BuildException;
  20. import org.apache.tools.ant.Project;
  21. /***
  22. * A regular expression substitution datatype. It is an expression
  23. * that is meant to replace a regular expression.
  24. *
  25. * <pre>
  26. * <substitition [ [id="id"] expression="expression" | refid="id" ]
  27. * />
  28. * </pre>
  29. *
  30. * @see org.apache.oro.text.regex.Perl5Substitution
  31. */
  32. public class Substitution extends DataType {
  33. /** The name of this data type */
  34. public static final String DATA_TYPE_NAME = "substitition";
  35. private String expression;
  36. public Substitution() {
  37. this.expression = null;
  38. }
  39. public void setExpression(String expression) {
  40. this.expression = expression;
  41. }
  42. /***
  43. * Gets the pattern string for this RegularExpression in the
  44. * given project.
  45. */
  46. public String getExpression(Project p) {
  47. if (isReference()) {
  48. return getRef(p).getExpression(p);
  49. }
  50. return expression;
  51. }
  52. /***
  53. * Get the RegularExpression this reference refers to in
  54. * the given project. Check for circular references too
  55. */
  56. public Substitution getRef(Project p) {
  57. if (!isChecked()) {
  58. Stack stk = new Stack();
  59. stk.push(this);
  60. dieOnCircularReference(stk, p);
  61. }
  62. Object o = getRefid().getReferencedObject(p);
  63. if (!(o instanceof Substitution)) {
  64. String msg = getRefid().getRefId() + " doesn\'t denote a substitution";
  65. throw new BuildException(msg);
  66. } else {
  67. return (Substitution) o;
  68. }
  69. }
  70. }