AW: Invoking a modifing method for backreferences in Perl5Util.substitute
"Sann, Stephan" <[email protected]> Thu, 17 Mar 2005 14:59:30 +0100
| Newsgroups | gmane.comp.jakarta.oro.user |
|---|---|
| Message-ID | <[email protected]> |
Hello Daniel,
sorry for the delay - I had to do a business travel.
> You need to recalculate the begin/end offset info as well (the
> toUppercase test works because the offset information was the same).
> That's why I said if you can implement it in a generic way, it's
> something I think we should definitely add to the API. It'll make
> life easier for anyone who wants to do this in the future.
Good to hear. When will it be available?
> If you see a way of improving the
> API to make it easire to apply arbitrary modifications to match
> results before substitution, don't hesitate to make a suggestion.
My idea was the following:
I wrote another substitute-method with an additional Param, a
java.lang.reflect.Method-Array. This Array contains a modifing Method
for each back-reference ([0] for $1, [1] for $2, etc.).
The assumption is that these methods are static, take a String-Param
and returns a String, too (like System.getProperty(String)).
My new group-method in MyMatchResult would look like this:
try {
Method actMethod =3D this.backRefModifyMethods[group];
Object[] modifingMethodArgs =3D new Object[1];
modifingMethodArgs[0] =3D this.orgMatchResult.group(group);
String modifiedBackRef =3D
(String)actMethod.invoke(null, modifingMethodArgs);
return(modifiedBackRef);
}
catch(Exception e) {
e.printStackTrace();
throw(new RuntimeException());
}
Maybe you should add a Construktor to Perl5Util, where I can force
a different Substitution-Object.
I had to modify Perl5Util like this:
substitution =3D
new MyPerl5Substitution(replacement.toString(), numInterpolations,
backRefModifyMethods);
to use my own Perl5Subsitution-implemetation. A Constructor with this
option would make life easier, too.
Best regards
Stephan