From f073a88c12a21c82cd4af0fb65464fd6b2d2ad94 Mon Sep 17 00:00:00 2001 From: Antoine Levy-Lambert Date: Mon, 1 Dec 2003 22:03:25 +0000 Subject: [PATCH] Make the choice of regular expression implementation possible with ant.regexp.regexpimpl as a normal project property PR: 15390 git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@275722 13f79535-47bb-0310-9956-ffa450edef68 --- docs/manual/CoreTypes/filterchain.html | 33 +++---- docs/manual/CoreTypes/regexp.html | 98 +++++++++++++++++++ docs/manual/OptionalTasks/replaceregexp.html | 47 +-------- docs/manual/conceptstypeslist.html | 1 + .../tools/ant/types/RegularExpression.java | 53 ++++++++-- 5 files changed, 161 insertions(+), 71 deletions(-) create mode 100644 docs/manual/CoreTypes/regexp.html diff --git a/docs/manual/CoreTypes/filterchain.html b/docs/manual/CoreTypes/filterchain.html index 332076611..070a73a7b 100644 --- a/docs/manual/CoreTypes/filterchain.html +++ b/docs/manual/CoreTypes/filterchain.html @@ -333,19 +333,8 @@ Convenience method: Filter which includes only those lines that contain the user-specified regular expression matching strings. - - - - - - - - - - - -
Parameter TypeParameter ValueRequired
regexpPattern of the substring to be searched for.Yes
-

+See Regexp Type for the description of the nested element regexp and of +the choice of regular expression implementation.

Example:

This will fetch all those lines that contain the pattern foo @@ -1112,10 +1101,12 @@ Include only lines that contain "foo";

ReplaceRegex

-This string filter replaces regular expressions. See -ReplaceRegexp -for an explanation on regular expressions. +This string filter replaces regular expressions. This filter may be used directly within a filterchain. +

+ See Regexp Type +concerning the choice of the implementation. +

@@ -1143,7 +1134,6 @@ for an explanation of regex flags.
No
-

Examples:

Replace all occurances of "hello" with "world", ignoring case. @@ -1158,11 +1148,12 @@ Replace all occurances of "hello" with "world", ignoring case.

ContainsRegex

This filters strings that match regular expressions. The filter may optionally replace the matched regular expression. -See -ReplaceRegexp -for an explanation on regular expressions. This filter may be used directly within a filterchain. - +

+See +Regexp Type +concerning the choice of regular expression implementation. +

diff --git a/docs/manual/CoreTypes/regexp.html b/docs/manual/CoreTypes/regexp.html new file mode 100644 index 000000000..b8edda158 --- /dev/null +++ b/docs/manual/CoreTypes/regexp.html @@ -0,0 +1,98 @@ + + + + +Regexp Type + + + + +

Regexp

+

+Regexp represents a regular expression. +

Parameters

+
Attribute
+ + + + + + + + + + +
AttributeDescriptionRequired
patternregular expression patternYes
+ +

Examples

+
+     <regexp id="myregexp" pattern="alpha(.+)beta"/>
+
+

+Defines a regular expression for later use with id myregexp. +

+
+     <regexp refid="myregexp"/>
+
+

+Use the regular expression with id myregexp. +

+ +

Choice of regular expression implementation

+

+Ant comes with +wrappers for +the java.util.regex package of JDK 1.4, +jakarta-regexp +and jakarta-ORO, +See installation dependencies + concerning the supporting libraries.

+

+The property ant.regexp.regexpimpl governs which regular expression implementation will be chosen. +Possible values for this property are : +

+It can also be another implementation of the interface org.apache.tools.ant.util.regexp.Regexp. +If ant.regexp.regexpimpl is not defined, ant checks in the order Jdk14Regexp, JakartaOroRegexp, + JakartaRegexp for the availability of the corresponding library. The first of these 3 which is found will be used.

+

+There are cross-platform issues for matches related to line terminator. +For example if you use $ to anchor your regular expression on the end of a line +the results might be very different depending on both your platform and the regular +expression library you use. It is 'highly recommended' that you test your pattern on +both Unix and Windows platforms before you rely on it. +

+We strongly recommend that you use Jakarta Oro. +

+

Usage

+The following tasks and types use the Regexp type : + +

+These string filters also use the mechanism of regexp to choose a regular expression implementation : +

+ +
+

Copyright © 2003 Apache Software Foundation. +All rights Reserved.

+ + diff --git a/docs/manual/OptionalTasks/replaceregexp.html b/docs/manual/OptionalTasks/replaceregexp.html index dec669b85..543205889 100644 --- a/docs/manual/OptionalTasks/replaceregexp.html +++ b/docs/manual/OptionalTasks/replaceregexp.html @@ -19,7 +19,8 @@ have been regenerated by this task.

Similar to regexp type mappers this task needs a supporting regular expression library and an implementation of -org.apache.tools.ant.util.regexp.Regexp. See details below.

+org.apache.tools.ant.util.regexp.Regexp. +See details in the documentation of the Regexp Type.

Parameters

@@ -79,51 +80,11 @@ library and an implementation of

replaces occurrences of the property name "OldProperty" with "NewProperty" in a properties file, preserving the existing value, in the file ${src}/build.properties

- -

Choice of regular expression implementation

-

-Ant comes with -wrappers for -the java.util.regex package of JDK 1.4, -jakarta-regexp -and jakarta-ORO, -See installation dependencies - concerning the supporting libraries.

-

-The system property ant.regexp.regexpimpl governs which regular expression implementation will be chosen. -Possible values for this property are : -

-It can also be another implementation of the interface org.apache.tools.ant.util.regexp.Regexp. -If ant.regexp.regexpimpl is not defined, ant checks in the order Jdk14Regexp, JakartaOroRegexp, - JakartaRegexp for the availability of the corresponding library. The first of these 3 which is found will be used.

-

-There are cross-platform issues for matches related to line terminator. -For example if you use $ to anchor your regular expression on the end of a line -the results might be very different depending on both your platform and the regular -expression library you use. It is 'highly recommended' that you test your pattern on -both Unix and Windows platforms before you rely on it. -

-We strongly recommend that you use Jakarta Oro. -

+

Parameters specified as nested elements

This task supports a nested FileSet element.

-

This task supports a nested Regexp element to specify +

This task supports a nested Regexp element to specify the regular expression. You can use this element to refer to a previously defined regular expression datatype instance.

diff --git a/docs/manual/conceptstypeslist.html b/docs/manual/conceptstypeslist.html index 3ec9ed6b2..a9cfa6021 100644 --- a/docs/manual/conceptstypeslist.html +++ b/docs/manual/conceptstypeslist.html @@ -27,6 +27,7 @@ Path-like Structures
Permissions
PropertySet
+Regexp
Selectors
XMLCatalog
ZipFileSet
diff --git a/src/main/org/apache/tools/ant/types/RegularExpression.java b/src/main/org/apache/tools/ant/types/RegularExpression.java index bd76aed9e..78508d447 100644 --- a/src/main/org/apache/tools/ant/types/RegularExpression.java +++ b/src/main/org/apache/tools/ant/types/RegularExpression.java @@ -1,7 +1,7 @@ /* * The Apache Software License, Version 1.1 * - * Copyright (c) 2001-2002 The Apache Software Foundation. All rights + * Copyright (c) 2001-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -99,42 +99,81 @@ import org.apache.tools.ant.util.regexp.RegexpFactory; public class RegularExpression extends DataType { /** Name of this data type */ public static final String DATA_TYPE_NAME = "regexp"; + private boolean alreadyInit = false; // The regular expression factory - private static final RegexpFactory factory = new RegexpFactory(); + private static final RegexpFactory FACTORY = new RegexpFactory(); - private Regexp regexp; + private Regexp regexp = null; + // temporary variable + private String myPattern; + private boolean setPatternPending = false; + /** + * default constructor + */ public RegularExpression() { - this.regexp = factory.newRegexp(); } + private void init(Project p) { + if (!alreadyInit) { + this.regexp = FACTORY.newRegexp(p); + alreadyInit = true; + } + } + private void setPattern() { + if (setPatternPending) { + regexp.setPattern(myPattern); + setPatternPending = false; + } + } + /** + * sets the regular expression pattern + * @param pattern regular expression pattern + */ public void setPattern(String pattern) { - this.regexp.setPattern(pattern); + if (regexp == null) { + myPattern = pattern; + setPatternPending = true; + } else { + regexp.setPattern(pattern); + } } /*** * Gets the pattern string for this RegularExpression in the * given project. + * @param p project + * @return pattern */ public String getPattern(Project p) { + init(p); if (isReference()) { return getRef(p).getPattern(p); } - + setPattern(); return regexp.getPattern(); } + /** + * provides a reference to the Regexp contained in this + * @param p project + * @return Regexp instance associated with this RegularExpression instance + */ public Regexp getRegexp(Project p) { + init(p); if (isReference()) { return getRef(p).getRegexp(p); } + setPattern(); return this.regexp; } /*** * Get the RegularExpression this reference refers to in * the given project. Check for circular references too + * @param p project + * @return resolved RegularExpression instance */ public RegularExpression getRef(Project p) { if (!isChecked()) { @@ -147,7 +186,7 @@ public class RegularExpression extends DataType { Object o = getRefid().getReferencedObject(p); if (!(o instanceof RegularExpression)) { String msg = getRefid().getRefId() + " doesn\'t denote a " - + DATA_TYPE_NAME; + + DATA_TYPE_NAME; throw new BuildException(msg); } else { return (RegularExpression) o;