001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.types;
019:
020: import org.apache.tools.ant.Project;
021: import org.apache.tools.ant.util.regexp.Regexp;
022: import org.apache.tools.ant.util.regexp.RegexpFactory;
023:
024: /***
025: * A regular expression datatype. Keeps an instance of the
026: * compiled expression for speed purposes. This compiled
027: * expression is lazily evaluated (it is compiled the first
028: * time it is needed). The syntax is the dependent on which
029: * regular expression type you are using. The system property
030: * "ant.regexp.regexpimpl" will be the classname of the implementation
031: * that will be used.
032: *
033: * <pre>
034: * For jdk <= 1.3, there are two available implementations:
035: * org.apache.tools.ant.util.regexp.JakartaOroRegexp (the default)
036: * Based on the jakarta-oro package
037: *
038: * org.apache.tools.ant.util.regexp.JakartaRegexpRegexp
039: * Based on the jakarta-regexp package
040: *
041: * For jdk >= 1.4 an additional implementation is available:
042: * org.apache.tools.ant.util.regexp.Jdk14RegexpRegexp
043: * Based on the jdk 1.4 built in regular expression package.
044: * </pre>
045: *
046: * <pre>
047: * <regexp [ [id="id"] pattern="expression" | refid="id" ]
048: * />
049: * </pre>
050: *
051: * @see org.apache.oro.text.regex.Perl5Compiler
052: * @see org.apache.regexp.RE
053: * @see java.util.regex.Pattern
054: *
055: * @see org.apache.tools.ant.util.regexp.Regexp
056: *
057: * @ant.datatype name="regexp"
058: */
059: public class RegularExpression extends DataType {
060: /** Name of this data type */
061: public static final String DATA_TYPE_NAME = "regexp";
062: private boolean alreadyInit = false;
063:
064: // The regular expression factory
065: private static final RegexpFactory FACTORY = new RegexpFactory();
066:
067: private Regexp regexp = null;
068: // temporary variable
069: private String myPattern;
070: private boolean setPatternPending = false;
071:
072: /**
073: * default constructor
074: */
075: public RegularExpression() {
076: }
077:
078: private void init(Project p) {
079: if (!alreadyInit) {
080: this .regexp = FACTORY.newRegexp(p);
081: alreadyInit = true;
082: }
083: }
084:
085: private void setPattern() {
086: if (setPatternPending) {
087: regexp.setPattern(myPattern);
088: setPatternPending = false;
089: }
090: }
091:
092: /**
093: * sets the regular expression pattern
094: * @param pattern regular expression pattern
095: */
096: public void setPattern(String pattern) {
097: if (regexp == null) {
098: myPattern = pattern;
099: 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: return (RegularExpression) getCheckedRef(p);
142: }
143: }
|