Uses multiple suffixes
Project: http://git-wip-us.apache.org/repos/asf/struts/repo Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/b708d844 Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/b708d844 Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/b708d844 Branch: refs/heads/master Commit: b708d844c292e064f5ef90bf65bfcd53b32b6659 Parents: cf10f68 Author: Lukasz Lenart <lukaszlen...@apache.org> Authored: Fri Jul 7 10:24:02 2017 +0200 Committer: Lukasz Lenart <lukaszlen...@apache.org> Committed: Fri Jul 7 10:24:02 2017 +0200 ---------------------------------------------------------------------- .../convention/DefaultActionNameBuilder.java | 22 +++++++++--- .../convention/SEOActionNameBuilder.java | 36 +++++++++++++++----- 2 files changed, 45 insertions(+), 13 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/struts/blob/b708d844/plugins/convention/src/main/java/org/apache/struts2/convention/DefaultActionNameBuilder.java ---------------------------------------------------------------------- diff --git a/plugins/convention/src/main/java/org/apache/struts2/convention/DefaultActionNameBuilder.java b/plugins/convention/src/main/java/org/apache/struts2/convention/DefaultActionNameBuilder.java index 46b2169..9719970 100644 --- a/plugins/convention/src/main/java/org/apache/struts2/convention/DefaultActionNameBuilder.java +++ b/plugins/convention/src/main/java/org/apache/struts2/convention/DefaultActionNameBuilder.java @@ -21,8 +21,12 @@ package org.apache.struts2.convention; import com.opensymphony.xwork2.inject.Inject; +import com.opensymphony.xwork2.util.TextParseUtil; import org.apache.commons.lang3.StringUtils; +import java.util.Collections; +import java.util.Set; + /** * <p> * This class strips the word <b>Action</b> from the end of the class name @@ -33,7 +37,7 @@ import org.apache.commons.lang3.StringUtils; * </p> */ public class DefaultActionNameBuilder implements ActionNameBuilder { - private String actionSuffix = "Action"; + private Set<String> actionSuffix = Collections.singleton("Action"); private boolean lowerCase; @Inject @@ -48,7 +52,7 @@ public class DefaultActionNameBuilder implements ActionNameBuilder { @Inject(value = "struts.convention.action.suffix", required = false) public void setActionSuffix(String actionSuffix) { if (StringUtils.isNotBlank(actionSuffix)) { - this.actionSuffix = actionSuffix; + this.actionSuffix = TextParseUtil.commaDelimitedStringToSet(actionSuffix); } } @@ -56,9 +60,7 @@ public class DefaultActionNameBuilder implements ActionNameBuilder { String actionName = className; // Truncate Action suffix if found - if (actionName.endsWith(actionSuffix)) { - actionName = actionName.substring(0, actionName.length() - actionSuffix.length()); - } + actionName = truncateSuffixIfMatches(actionName); // Force initial letter of action to lowercase, if desired if ((lowerCase) && (actionName.length() > 1)) { @@ -72,4 +74,14 @@ public class DefaultActionNameBuilder implements ActionNameBuilder { return actionName; } + + private String truncateSuffixIfMatches(String name) { + String actionName = name; + for (String suffix : actionSuffix) { + if (actionName.endsWith(suffix)) { + actionName = actionName.substring(0, actionName.length() - suffix.length()); + } + } + return actionName; + } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/struts/blob/b708d844/plugins/convention/src/main/java/org/apache/struts2/convention/SEOActionNameBuilder.java ---------------------------------------------------------------------- diff --git a/plugins/convention/src/main/java/org/apache/struts2/convention/SEOActionNameBuilder.java b/plugins/convention/src/main/java/org/apache/struts2/convention/SEOActionNameBuilder.java index f2920c0..490476d 100644 --- a/plugins/convention/src/main/java/org/apache/struts2/convention/SEOActionNameBuilder.java +++ b/plugins/convention/src/main/java/org/apache/struts2/convention/SEOActionNameBuilder.java @@ -21,10 +21,14 @@ package org.apache.struts2.convention; import com.opensymphony.xwork2.inject.Inject; +import com.opensymphony.xwork2.util.TextParseUtil; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; import org.apache.commons.lang3.StringUtils; +import java.util.Collections; +import java.util.Set; + /** * <p> * This class converts the class name into a SEO friendly name by recognizing @@ -35,7 +39,7 @@ import org.apache.commons.lang3.StringUtils; */ public class SEOActionNameBuilder implements ActionNameBuilder { private static final Logger LOG = LogManager.getLogger(SEOActionNameBuilder.class); - private String actionSuffix = "Action"; + private Set<String> actionSuffix = Collections.singleton("Action"); private boolean lowerCase; private String separator; @@ -53,20 +57,17 @@ public class SEOActionNameBuilder implements ActionNameBuilder { @Inject(value = "struts.convention.action.suffix", required = false) public void setActionSuffix(String actionSuffix) { if (StringUtils.isNotBlank(actionSuffix)) { - this.actionSuffix = actionSuffix; + this.actionSuffix = TextParseUtil.commaDelimitedStringToSet(actionSuffix); } } public String build(String className) { String actionName = className; - - if (actionName.equals(actionSuffix)) - throw new IllegalStateException("The action name cannot be the same as the action suffix [" + actionSuffix + "]"); + + checkActionName(actionName); // Truncate Action suffix if found - if (actionName.endsWith(actionSuffix)) { - actionName = actionName.substring(0, actionName.length() - actionSuffix.length()); - } + actionName = truncateSuffixIfMatches(actionName); // Convert to underscores char[] ca = actionName.toCharArray(); @@ -93,4 +94,23 @@ public class SEOActionNameBuilder implements ActionNameBuilder { return actionName; } + + void checkActionName(String actionName) { + for (String suffix : actionSuffix) { + if (actionName.equals(suffix)) { + throw new IllegalStateException("The action name cannot be the same as the action suffix [" + suffix + "]"); + } + } + } + + private String truncateSuffixIfMatches(String name) { + String actionName = name; + for (String suffix : actionSuffix) { + if (actionName.endsWith(suffix)) { + actionName = actionName.substring(0, actionName.length() - suffix.length()); + } + } + return actionName; + } + } \ No newline at end of file