<commit_before>
// $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;
  }
  
}<commit_msg>Update issue 81 Converted ShowActionGroupParamRule to java.util.regex<commit_after>
// $Id: ShowActionGroupParamRule.java,v 1.3 2003/12/22 19:16:09 larsga Exp $

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

import java.util.regex.Matcher;
import java.util.regex.Pattern;

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

/**
 * 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 {

  private static final Pattern PATTERN_AG = Pattern.compile("(\\?|&)(" + Constants.RP_ACTIONGROUP + "=.*?(&|$))");
  private static final Pattern PATTERN_SHOW = Pattern.compile("(\\?|&)" + Constants.RP_SHOW_AG + "=(.*?)(&|$)");
  
  public String generate(ActionContextIF context,
                         String actionName, String nextActionTemplate,
                         String urlWithParams) {
    String url = urlWithParams;
    // ag=bar -> REMOVE
    Matcher m = PATTERN_AG.matcher(url);
    if (m != null) {
      url = m.replaceAll("$1");
    }
    // show_ag=foo -> ag=foo
    m = PATTERN_SHOW.matcher(url);
    if (m != null) {
      url = m.replaceAll("$1" + Constants.RP_ACTIONGROUP + "=$2$3");
    }
    return url;
  }
  
}
{'fraction_non_alphanumeric': 0.12221471978392978, 'fraction_numerical': 0.015192437542201216, 'mean_word_length': 3.0422919508867667, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4632281', 'n_tokens_mistral': 966, 'n_tokens_neox': 915, 'n_words': 232}