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. import org.apache.tools.ant.util.regexp.Regexp;
  22. import org.apache.tools.ant.util.regexp.RegexpFactory;
  23. /***
  24. * A regular expression datatype. Keeps an instance of the
  25. * compiled expression for speed purposes. This compiled
  26. * expression is lazily evaluated (it is compiled the first
  27. * time it is needed). The syntax is the dependent on which
  28. * regular expression type you are using. The system property
  29. * "ant.regexp.regexpimpl" will be the classname of the implementation
  30. * that will be used.
  31. *
  32. * <pre>
  33. * For jdk <= 1.3, there are two available implementations:
  34. * org.apache.tools.ant.util.regexp.JakartaOroRegexp (the default)
  35. * Based on the jakarta-oro package
  36. *
  37. * org.apache.tools.ant.util.regexp.JakartaRegexpRegexp
  38. * Based on the jakarta-regexp package
  39. *
  40. * For jdk >= 1.4 an additional implementation is available:
  41. * org.apache.tools.ant.util.regexp.Jdk14RegexpRegexp
  42. * Based on the jdk 1.4 built in regular expression package.
  43. * </pre>
  44. *
  45. * <pre>
  46. * <regexp [ [id="id"] pattern="expression" | refid="id" ]
  47. * />
  48. * </pre>
  49. *
  50. * @see org.apache.oro.text.regex.Perl5Compiler
  51. * @see org.apache.regexp.RE
  52. * @see java.util.regex.Pattern
  53. *
  54. * @see org.apache.tools.ant.util.regexp.Regexp
  55. *
  56. * @ant.datatype name="regexp"
  57. */
  58. public class RegularExpression extends DataType {
  59. /** Name of this data type */
  60. public static final String DATA_TYPE_NAME = "regexp";
  61. // The regular expression factory
  62. private static final RegexpFactory factory = new RegexpFactory();
  63. private Regexp regexp;
  64. public RegularExpression() {
  65. this.regexp = factory.newRegexp();
  66. }
  67. public void setPattern(String pattern) {
  68. this.regexp.setPattern(pattern);
  69. }
  70. /***
  71. * Gets the pattern string for this RegularExpression in the
  72. * given project.
  73. */
  74. public String getPattern(Project p) {
  75. if (isReference()) {
  76. return getRef(p).getPattern(p);
  77. }
  78. return regexp.getPattern();
  79. }
  80. public Regexp getRegexp(Project p) {
  81. if (isReference()) {
  82. return getRef(p).getRegexp(p);
  83. }
  84. return this.regexp;
  85. }
  86. /***
  87. * Get the RegularExpression this reference refers to in
  88. * the given project. Check for circular references too
  89. */
  90. public RegularExpression getRef(Project p) {
  91. if (!isChecked()) {
  92. Stack stk = new Stack();
  93. stk.push(this);
  94. dieOnCircularReference(stk, p);
  95. }
  96. Object o = getRefid().getReferencedObject(p);
  97. if (!(o instanceof RegularExpression)) {
  98. String msg = getRefid().getRefId() + " doesn\'t denote a "
  99. + DATA_TYPE_NAME;
  100. throw new BuildException(msg);
  101. } else {
  102. return (RegularExpression) o;
  103. }
  104. }
  105. }