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 |
|
---|
19 |
|
---|
20 | import java.util.Stack;
|
---|
21 | import org.apache.tools.ant.BuildException;
|
---|
22 | import org.apache.tools.ant.Project;
|
---|
23 | import org.apache.tools.ant.util.regexp.Regexp;
|
---|
24 | import org.apache.tools.ant.util.regexp.RegexpFactory;
|
---|
25 |
|
---|
26 | /***
|
---|
27 | * A regular expression datatype. Keeps an instance of the
|
---|
28 | * compiled expression for speed purposes. This compiled
|
---|
29 | * expression is lazily evaluated (it is compiled the first
|
---|
30 | * time it is needed). The syntax is the dependent on which
|
---|
31 | * regular expression type you are using. The system property
|
---|
32 | * "ant.regexp.regexpimpl" will be the classname of the implementation
|
---|
33 | * that will be used.
|
---|
34 | *
|
---|
35 | * <pre>
|
---|
36 | * For jdk <= 1.3, there are two available implementations:
|
---|
37 | * org.apache.tools.ant.util.regexp.JakartaOroRegexp (the default)
|
---|
38 | * Based on the jakarta-oro package
|
---|
39 | *
|
---|
40 | * org.apache.tools.ant.util.regexp.JakartaRegexpRegexp
|
---|
41 | * Based on the jakarta-regexp package
|
---|
42 | *
|
---|
43 | * For jdk >= 1.4 an additional implementation is available:
|
---|
44 | * org.apache.tools.ant.util.regexp.Jdk14RegexpRegexp
|
---|
45 | * Based on the jdk 1.4 built in regular expression package.
|
---|
46 | * </pre>
|
---|
47 | *
|
---|
48 | * <pre>
|
---|
49 | * <regexp [ [id="id"] pattern="expression" | refid="id" ]
|
---|
50 | * />
|
---|
51 | * </pre>
|
---|
52 | *
|
---|
53 | * @see org.apache.oro.text.regex.Perl5Compiler
|
---|
54 | * @see org.apache.regexp.RE
|
---|
55 | * @see java.util.regex.Pattern
|
---|
56 | *
|
---|
57 | * @see org.apache.tools.ant.util.regexp.Regexp
|
---|
58 | *
|
---|
59 | * @ant.datatype name="regexp"
|
---|
60 | */
|
---|
61 | public class RegularExpression extends DataType {
|
---|
62 | /** Name of this data type */
|
---|
63 | public static final String DATA_TYPE_NAME = "regexp";
|
---|
64 | private boolean alreadyInit = false;
|
---|
65 |
|
---|
66 | // The regular expression factory
|
---|
67 | private static final RegexpFactory FACTORY = new RegexpFactory();
|
---|
68 |
|
---|
69 | private Regexp regexp = null;
|
---|
70 | // temporary variable
|
---|
71 | private String myPattern;
|
---|
72 | private boolean setPatternPending = false;
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * default constructor
|
---|
76 | */
|
---|
77 | public RegularExpression() {
|
---|
78 | }
|
---|
79 |
|
---|
80 | private void init(Project p) {
|
---|
81 | if (!alreadyInit) {
|
---|
82 | this.regexp = FACTORY.newRegexp(p);
|
---|
83 | alreadyInit = true;
|
---|
84 | }
|
---|
85 | }
|
---|
86 | private void setPattern() {
|
---|
87 | if (setPatternPending) {
|
---|
88 | regexp.setPattern(myPattern);
|
---|
89 | setPatternPending = false;
|
---|
90 | }
|
---|
91 | }
|
---|
92 | /**
|
---|
93 | * sets the regular expression pattern
|
---|
94 | * @param pattern regular expression pattern
|
---|
95 | */
|
---|
96 | public void setPattern(String pattern) {
|
---|
97 | if (regexp == null) {
|
---|
98 | myPattern = pattern;
|
---|
99 | setPatternPending = true;
|
---|
100 | } else {
|
---|
101 | regexp.setPattern(pattern);
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | /***
|
---|
106 | * Gets the pattern string for this RegularExpression in the
|
---|
107 | * given project.
|
---|
108 | * @param p project
|
---|
109 | * @return pattern
|
---|
110 | */
|
---|
111 | public String getPattern(Project p) {
|
---|
112 | init(p);
|
---|
113 | if (isReference()) {
|
---|
114 | return getRef(p).getPattern(p);
|
---|
115 | }
|
---|
116 | setPattern();
|
---|
117 | return regexp.getPattern();
|
---|
118 | }
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * provides a reference to the Regexp contained in this
|
---|
122 | * @param p project
|
---|
123 | * @return Regexp instance associated with this RegularExpression instance
|
---|
124 | */
|
---|
125 | public Regexp getRegexp(Project p) {
|
---|
126 | init(p);
|
---|
127 | if (isReference()) {
|
---|
128 | return getRef(p).getRegexp(p);
|
---|
129 | }
|
---|
130 | setPattern();
|
---|
131 | return this.regexp;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /***
|
---|
135 | * Get the RegularExpression this reference refers to in
|
---|
136 | * the given project. Check for circular references too
|
---|
137 | * @param p project
|
---|
138 | * @return resolved RegularExpression instance
|
---|
139 | */
|
---|
140 | public RegularExpression getRef(Project p) {
|
---|
141 | if (!isChecked()) {
|
---|
142 | Stack stk = new Stack();
|
---|
143 | stk.push(this);
|
---|
144 | dieOnCircularReference(stk, p);
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 | Object o = getRefid().getReferencedObject(p);
|
---|
149 | if (!(o instanceof RegularExpression)) {
|
---|
150 | String msg = getRefid().getRefId() + " doesn\'t denote a "
|
---|
151 | + DATA_TYPE_NAME;
|
---|
152 | throw new BuildException(msg);
|
---|
153 | } else {
|
---|
154 | return (RegularExpression) o;
|
---|
155 | }
|
---|
156 | }
|
---|
157 | }
|
---|