From b708d844c292e064f5ef90bf65bfcd53b32b6659 Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Fri, 7 Jul 2017 10:24:02 +0200 Subject: [PATCH] Uses multiple suffixes --- .../convention/DefaultActionNameBuilder.java | 22 +++++++++--- .../convention/SEOActionNameBuilder.java | 36 ++++++++++++++----- 2 files changed, 45 insertions(+), 13 deletions(-) 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 46b21693d..97199703a 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; + /** *

* This class strips the word Action from the end of the class name @@ -33,7 +37,7 @@ import org.apache.commons.lang3.StringUtils; *

*/ public class DefaultActionNameBuilder implements ActionNameBuilder { - private String actionSuffix = "Action"; + private Set 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 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 f2920c018..490476d2c 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; + /** *

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