KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > RegularExpression


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.types;
19
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 /***
25  * A regular expression datatype. Keeps an instance of the
26  * compiled expression for speed purposes. This compiled
27  * expression is lazily evaluated (it is compiled the first
28  * time it is needed). The syntax is the dependent on which
29  * regular expression type you are using. The system property
30  * "ant.regexp.regexpimpl" will be the classname of the implementation
31  * that will be used.
32  *
33  * <pre>
34  * For jdk &lt;= 1.3, there are two available implementations:
35  * org.apache.tools.ant.util.regexp.JakartaOroRegexp (the default)
36  * Based on the jakarta-oro package
37  *
38  * org.apache.tools.ant.util.regexp.JakartaRegexpRegexp
39  * Based on the jakarta-regexp package
40  *
41  * For jdk &gt;= 1.4 an additional implementation is available:
42  * org.apache.tools.ant.util.regexp.Jdk14RegexpRegexp
43  * Based on the jdk 1.4 built in regular expression package.
44  * </pre>
45  *
46  * <pre>
47  * &lt;regexp [ [id="id"] pattern="expression" | refid="id" ]
48  * /&gt;
49  * </pre>
50  *
51  * @see org.apache.oro.text.regex.Perl5Compiler
52  * @see org.apache.regexp.RE
53  * @see java.util.regex.Pattern
54  *
55  * @see org.apache.tools.ant.util.regexp.Regexp
56  *
57  * @ant.datatype name="regexp"
58  */

59 public class RegularExpression extends DataType {
60     /** Name of this data type */
61     public static final String JavaDoc DATA_TYPE_NAME = "regexp";
62     private boolean alreadyInit = false;
63
64     // The regular expression factory
65
private static final RegexpFactory FACTORY = new RegexpFactory();
66
67     private Regexp regexp = null;
68     // temporary variable
69
private String JavaDoc myPattern;
70     private boolean setPatternPending = false;
71
72     /**
73      * default constructor
74      */

75     public RegularExpression() {
76     }
77
78     private void init(Project p) {
79         if (!alreadyInit) {
80             this.regexp = FACTORY.newRegexp(p);
81             alreadyInit = true;
82         }
83     }
84     private void setPattern() {
85         if (setPatternPending) {
86             regexp.setPattern(myPattern);
87             setPatternPending = false;
88         }
89     }
90     /**
91      * sets the regular expression pattern
92      * @param pattern regular expression pattern
93      */

94     public void setPattern(String JavaDoc pattern) {
95         if (regexp == null) {
96             myPattern = pattern;
97             setPatternPending = true;
98         } else {
99             regexp.setPattern(pattern);
100         }
101     }
102
103     /***
104      * Gets the pattern string for this RegularExpression in the
105      * given project.
106      * @param p project
107      * @return pattern
108      */

109     public String JavaDoc getPattern(Project p) {
110         init(p);
111         if (isReference()) {
112             return getRef(p).getPattern(p);
113         }
114         setPattern();
115         return regexp.getPattern();
116     }
117
118     /**
119      * provides a reference to the Regexp contained in this
120      * @param p project
121      * @return Regexp instance associated with this RegularExpression instance
122      */

123     public Regexp getRegexp(Project p) {
124         init(p);
125         if (isReference()) {
126             return getRef(p).getRegexp(p);
127         }
128         setPattern();
129         return this.regexp;
130     }
131
132     /***
133      * Get the RegularExpression this reference refers to in
134      * the given project. Check for circular references too
135      * @param p project
136      * @return resolved RegularExpression instance
137      */

138     public RegularExpression getRef(Project p) {
139         return (RegularExpression) getCheckedRef(p);
140     }
141 }
142
Popular Tags