Menu

#142 PropertyRegex throws StringIndexOutOfBoundsException

open
process (9)
5
2006-08-16
2005-08-02
Anonymous
No

I'm using the following declaration to convert package
name to path:

<propertyregex
property="pkg.path"
input="${pkg}"
regexp="[\.]"
replace="${file.separator}"
global="true"
casesensitive="false"
/>

When running on MS Windows (XP, Ant v 1.6.5) it throws
a StringIndexOutOfBoundsException with following stack
trace.

java.lang.StringIndexOutOfBoundsException: String index
out of range
: 1
at org.apache.tools.ant.Task.perform(Task.java:373)
at
org.apache.tools.ant.Target.execute(Target.java:341)
at
org.apache.tools.ant.Target.performTasks(Target.java:369)
at
org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
at
org.apache.tools.ant.Project.executeTarget(Project.java:1185)
at
org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:40)
at
org.apache.tools.ant.Project.executeTargets(Project.java:1068)
at
org.apache.tools.ant.Main.runBuild(Main.java:668)
at
org.apache.tools.ant.Main.startAnt(Main.java:187)
at
org.apache.tools.ant.launch.Launcher.run(Launcher.java:246)
at
org.apache.tools.ant.launch.Launcher.main(Launcher.java:67)
Caused by: java.lang.StringIndexOutOfBoundsException:
String index out of range: 1
at java.lang.String.charAt(String.java:444)
at
java.util.regex.Matcher.appendReplacement(Matcher.java:551)
at
java.util.regex.Matcher.replaceAll(Matcher.java:661)
at
org.apache.tools.ant.util.regexp.Jdk14RegexpRegexp.substitute(Jdk14RegexpRegexp.java:91)
at
net.sf.antcontrib.property.RegexTask.doReplace(RegexTask.java:136)
at
net.sf.antcontrib.property.RegexTask.execute(RegexTask.java:190)
at
org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
at org.apache.tools.ant.Task.perform(Task.java:364)
... 10 more
--- Nested Exception ---
java.lang.StringIndexOutOfBoundsException: String index
out of range: 1
at java.lang.String.charAt(String.java:444)
at
java.util.regex.Matcher.appendReplacement(Matcher.java:551)
at
java.util.regex.Matcher.replaceAll(Matcher.java:661)
at
org.apache.tools.ant.util.regexp.Jdk14RegexpRegexp.substitute(Jdk14RegexpRegexp.java:91)
at
net.sf.antcontrib.property.RegexTask.doReplace(RegexTask.java:136)
at
net.sf.antcontrib.property.RegexTask.execute(RegexTask.java:190)
at
org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
at org.apache.tools.ant.Task.perform(Task.java:364)
at
org.apache.tools.ant.Target.execute(Target.java:341)
at
org.apache.tools.ant.Target.performTasks(Target.java:369)
at
org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
at
org.apache.tools.ant.Project.executeTarget(Project.java:1185)
at
org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:40)
at
org.apache.tools.ant.Project.executeTargets(Project.java:1068)
at
org.apache.tools.ant.Main.runBuild(Main.java:668)
at
org.apache.tools.ant.Main.startAnt(Main.java:187)
at
org.apache.tools.ant.launch.Launcher.run(Launcher.java:246)
at
org.apache.tools.ant.launch.Launcher.main(Launcher.java:67)

Discussion

  • Matt Inger

    Matt Inger - 2005-08-03

    Logged In: YES
    user_id=83032

    This is an issue with the regular expression package in ANT.
    It does not provide a way to escape the "\" character in an
    independent way. Different implementations of regular
    expression subsititution use either \ or $ as the group
    indicator, and not all implementations allow you to esacpe
    the backslash.

    I suggest in the mean time that you use / and normalize the
    path with the <pathconvert> task.

    I'll look into this more, but at this point, i think we just have to
    live with it.

     
  • Matt Inger

    Matt Inger - 2005-08-03

    Logged In: YES
    user_id=83032

    OK. I looked at the ANT code, and this is bug in the JDK14
    regular expression wrapper for the ANT datatype for regular
    expression. The solution is that we modify the substitution
    string before performing the substitution so that any \
    characters are replaced with \\, since a single \ is an escape
    character for substitutions.

     
  • Matt Inger

    Matt Inger - 2005-08-03

    Logged In: YES
    user_id=83032

    On second thought, that would preclude us from having real
    escape characters in substitution strings. Just use the
    workaround.

     
  • Matt Inger

    Matt Inger - 2006-08-16
    • assigned_to: nobody --> mattinger
     

Log in to post a comment.