
// $Id: ShowActionGroupParamRule.java,v 1.3 2003/12/22 19:16:09 larsga Exp $

package net.ontopia.topicmaps.webed.webapps.omnieditor;

import net.ontopia.topicmaps.webed.impl.basic.Constants;
import net.ontopia.topicmaps.webed.impl.basic.ParamRuleIF;
import net.ontopia.topicmaps.webed.impl.basic.ActionContextIF;

import org.apache.oro.text.perl.Perl5Util;

/**
 * INTERNAL: Implementation of ParamRuleIF to substitute the show
 * action group request parameter name with the action group and to
 * remove the action group request parameter.
 *
 * This is for example useful for the simple submit action in the tab
 * of the topic edit page.
 */
public class ShowActionGroupParamRule implements ParamRuleIF {

  // share this instance with all objects
  static Perl5Util putil = new Perl5Util();
  
  public String generate(ActionContextIF context,
                         String actionName, String nextActionTemplate,
                         String urlWithParams) {
    String url = urlWithParams;
    // ag=bar -> REMOVE
    url = putil.substitute("s/(\\?|&)" + Constants.RP_ACTIONGROUP +
                           "=.*?(&|$)/$1/", url);
    // show_ag=foo -> ag=foo
    url = putil.substitute("s/(\\?|&)" + Constants.RP_SHOW_AG +
                           "=(.*?)(&|$)/$1" + Constants.RP_ACTIONGROUP +
                           "=$2$3/", url);
    return url;
  }
  
}

@@ -129,16 +129,81 @@
 ditor;%0A%0A
+import java.util.regex.Matcher;%0Aimport java.util.regex.Pattern;%0A%0A
 import n
@@ -378,52 +378,8 @@
 F;%0A%0A
-import org.apache.oro.text.perl.Perl5Util;%0A%0A
 /**%0A
@@ -732,89 +732,231 @@
 %0A%0A  
-// share this instance with all objects%0A  static Perl5Util putil = new Perl5Util(
+private static final Pattern PATTERN_AG = Pattern.compile(%22(%5C%5C?%7C&)(%22 + Constants.RP_ACTIONGROUP + %22=.*?(&%7C$))%22);%0A  private static final Pattern PATTERN_SHOW = Pattern.compile(%22(%5C%5C?%7C&)%22 + Constants.RP_SHOW_AG + %22=(.*?)(&%7C$)%22
 );%0A 
@@ -1191,330 +1191,263 @@
     
-url = putil.substitute(%22s/(%5C%5C?%7C&)%22 + Constants.RP_ACTIONGROUP +%0A                           %22=.*?(&%7C$)/$1/%22, url);%0A    // show_ag=foo -%3E ag=foo%0A    url = putil.substitute(%22s/(%5C%5C?%7C&)%22 + Constants.RP_SHOW_AG +%0A                           %22=(.*?)(&%7C$)/$1%22 + Constants.RP_ACTIONGROUP +%0A                           %22=$2$3/%22, url);
+Matcher m = PATTERN_AG.matcher(url);%0A    if (m != null) %7B%0A      url = m.replaceAll(%22$1%22);%0A    %7D%0A    // show_ag=foo -%3E ag=foo%0A    m = PATTERN_SHOW.matcher(url);%0A    if (m != null) %7B%0A      url = m.replaceAll(%22$1%22 + Constants.RP_ACTIONGROUP + %22=$2$3%22);%0A    %7D
 %0A   

Update issue 81 Converted ShowActionGroupParamRule to java.util.regex