AW: AW: Invoking a modifing method for backreferences in Perl5Util.substitute

"Sann, Stephan" <[email protected]> Thu, 10 Mar 2005 18:07:55 +0100
Newsgroups gmane.comp.jakarta.oro.user
Message-ID <[email protected]>
Hallo Daniel,

thanks again for your post!

> I said subclass, not modify :)  You can subclass Perl5Substitution,
> override append, and wrap the MatchResult with your own
> MatchResultAdapter (maybe a dummy convenience adapter should be
> added to the API) before passing it to super.appendSubstitution().
> For example:
> [...]
> MyMatchResult would override the delegate's method return values,
> applying whatever modifications you wanted to the real result, which
> would be used transparently by Perl5Substitution.

Know I wrote a Perl5Subsitution-Extension as recommend by you. Also
I overwrote the "group(int group)"-Method of the passed MatchResult-
object:


<snip>

  /* (non-Javadoc)
   * @see org.apache.oro.text.regex.MatchResult#group(int)
   */
  public synchronized String group(int group) {

    return(this.orgMatchResult.group(group));
    //return(this.orgMatchResult.group(group).toUpperCase());
    //return(System.getProperty(this.orgMatchResult.group(group)));
    //return("foobar");
  }

</snip>

this.orgMatchResult is the MatchResult-object passed by

      super.appendSubstitution(appendBuffer, new MyMatchResult(match),
              substitutionCount, originalInput, matcher, pattern);


The hole thing realy works out - sometimes :-(


This is my Test-regexp:

    String ein =3D "Version prop:java.version  Home prop:java.home ";
    String regExp =3D "s#prop:(.*?)\\s#-$1-#gi";


Case a):
     return(this.orgMatchResult.group(group));

leads to:
Version -java.version- Home -java.home-

(As with normal "Perl5Util.substitute")


Case b):
    return(this.orgMatchResult.group(group).toUpperCase());

leads to:
Version -JAVA.VERSION- Home -JAVA.HOME-

(Great! My Modifikation works!)


Case c):
    return(System.getProperty(this.orgMatchResult.group(group)));

leads to:
java.lang.NullPointerException
	at =
org.apache.oro.text.regex.Perl5Substitution._calcSub(Perl5Substitution.ja=
va:341)
	at =
org.apache.oro.text.regex.Perl5Substitution.appendSubstitution(Perl5Subst=
itution.java:535)
	at =
de.lotk.yaoro.text.regex.YaPerl5Substitution.appendSubstitution(YaPerl5Su=
bstitution.java:95)
	at org.apache.oro.text.regex.Util.substitute(Util.java:457)
	at org.apache.oro.text.regex.Util.substitute(Util.java:412)
	at de.lotk.yaoro.text.perl.YaPerl5Util.substitute(YaPerl5Util.java:178)
	at de.lotk.yaoro.text.perl.YaPerl5Util.substitute(YaPerl5Util.java:37)
	at de.lotk.yaoro.YaOroTest.main(YaOroTest.java:38)
java.lang.RuntimeException
	at de.lotk.yaoro.YaOroTest.main(YaOroTest.java:44)
Exception in thread "main"=20

(What the hell...?)


The funny case d):
    return("foobar");

leads to:
java.lang.ArrayIndexOutOfBoundsException
	at java.lang.System.arraycopy(Native Method)
	at java.lang.StringBuffer.append(Unknown Source)
	at =
org.apache.oro.text.regex.Perl5Substitution._calcSub(Perl5Substitution.ja=
va:408)
	at =
org.apache.oro.text.regex.Perl5Substitution.appendSubstitution(Perl5Subst=
itution.java:535)
	at =
de.lotk.yaoro.text.regex.YaPerl5Substitution.appendSubstitution(YaPerl5Su=
bstitution.java:95)
	at org.apache.oro.text.regex.Util.substitute(Util.java:457)
	at org.apache.oro.text.regex.Util.substitute(Util.java:412)
	at de.lotk.yaoro.text.perl.YaPerl5Util.substitute(YaPerl5Util.java:178)
	at de.lotk.yaoro.text.perl.YaPerl5Util.substitute(YaPerl5Util.java:37)
	at de.lotk.yaoro.YaOroTest.main(YaOroTest.java:38)
java.lang.RuntimeException
	at de.lotk.yaoro.YaOroTest.main(YaOroTest.java:44)
Exception in thread "main"=20

(?????)


The pattern-engine seems to keep a copy of the backreference or =
whatever. Tried to
debug this thing but ended in cryptical code without comments - gave up =
:-(


Any more suggestions?

Thanks a lot!
Stephan