Extract common code
Project: http://git-wip-us.apache.org/repos/asf/struts/repo Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/f522fbc4 Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/f522fbc4 Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/f522fbc4 Branch: refs/heads/master Commit: f522fbc45417b836463553e3d9110ee58fcf2673 Parents: b708d84 Author: Lukasz Lenart <lukaszlen...@apache.org> Authored: Fri Jul 7 10:58:26 2017 +0200 Committer: Lukasz Lenart <lukaszlen...@apache.org> Committed: Fri Jul 7 10:58:26 2017 +0200 ---------------------------------------------------------------------- .../convention/AbstractActionNameBuilder.java | 44 +++++++++++++++++++ .../struts2/convention/ConventionConstants.java | 2 + .../convention/DefaultActionNameBuilder.java | 35 +++------------ .../convention/SEOActionNameBuilder.java | 45 +++----------------- 4 files changed, 60 insertions(+), 66 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/struts/blob/f522fbc4/plugins/convention/src/main/java/org/apache/struts2/convention/AbstractActionNameBuilder.java ---------------------------------------------------------------------- diff --git a/plugins/convention/src/main/java/org/apache/struts2/convention/AbstractActionNameBuilder.java b/plugins/convention/src/main/java/org/apache/struts2/convention/AbstractActionNameBuilder.java new file mode 100644 index 0000000..a546e62 --- /dev/null +++ b/plugins/convention/src/main/java/org/apache/struts2/convention/AbstractActionNameBuilder.java @@ -0,0 +1,44 @@ +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; + +public abstract class AbstractActionNameBuilder implements ActionNameBuilder { + + private Set<String> actionSuffix = Collections.singleton("Action"); + + /** + * @param actionSuffix (Optional) Classes that end with these value will be mapped as actions + * (defaults to "Action") + */ + @Inject(value = "struts.convention.action.suffix", required = false) + public void setActionSuffix(String actionSuffix) { + if (StringUtils.isNotBlank(actionSuffix)) { + this.actionSuffix = TextParseUtil.commaDelimitedStringToSet(actionSuffix); + } + } + + + protected 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 + "]"); + } + } + } + + protected 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; + } + +} http://git-wip-us.apache.org/repos/asf/struts/blob/f522fbc4/plugins/convention/src/main/java/org/apache/struts2/convention/ConventionConstants.java ---------------------------------------------------------------------- diff --git a/plugins/convention/src/main/java/org/apache/struts2/convention/ConventionConstants.java b/plugins/convention/src/main/java/org/apache/struts2/convention/ConventionConstants.java index 2842b8c..b360d94 100644 --- a/plugins/convention/src/main/java/org/apache/struts2/convention/ConventionConstants.java +++ b/plugins/convention/src/main/java/org/apache/struts2/convention/ConventionConstants.java @@ -29,4 +29,6 @@ public class ConventionConstants { public static final String CONVENTION_RESULT_MAP_BUILDER = "struts.convention.resultMapBuilder"; public static final String CONVENTION_INTERCEPTOR_MAP_BUILDER = "struts.convention.interceptorMapBuilder"; public static final String CONVENTION_CONVENTIONS_SERVICE = "struts.convention.conventionsService"; + public static final String CONVENTION_ACTION_NAME_LOWERCASE = "struts.convention.action.name.lowercase"; + public static final String CONVENTION_ACTION_NAME_SEPARATOR = "struts.convention.action.name.separator"; } http://git-wip-us.apache.org/repos/asf/struts/blob/f522fbc4/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 9719970..797a519 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,11 +21,6 @@ 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> @@ -36,29 +31,22 @@ import java.util.Set; * action names. * </p> */ -public class DefaultActionNameBuilder implements ActionNameBuilder { - private Set<String> actionSuffix = Collections.singleton("Action"); +public class DefaultActionNameBuilder extends AbstractActionNameBuilder { + private boolean lowerCase; @Inject - public DefaultActionNameBuilder(@Inject(value="struts.convention.action.name.lowercase") String lowerCase) { + public DefaultActionNameBuilder( + @Inject(ConventionConstants.CONVENTION_ACTION_NAME_LOWERCASE) String lowerCase + ) { this.lowerCase = Boolean.parseBoolean(lowerCase); } - /** - * @param actionSuffix (Optional) Classes that end with these value will be mapped as actions - * (defaults to "Action") - */ - @Inject(value = "struts.convention.action.suffix", required = false) - public void setActionSuffix(String actionSuffix) { - if (StringUtils.isNotBlank(actionSuffix)) { - this.actionSuffix = TextParseUtil.commaDelimitedStringToSet(actionSuffix); - } - } - public String build(String className) { String actionName = className; + checkActionName(actionName); + // Truncate Action suffix if found actionName = truncateSuffixIfMatches(actionName); @@ -75,13 +63,4 @@ 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/f522fbc4/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 490476d..3fea36a 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,13 +21,8 @@ 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> @@ -37,30 +32,22 @@ import java.util.Set; * from the class name. * </p> */ -public class SEOActionNameBuilder implements ActionNameBuilder { +public class SEOActionNameBuilder extends AbstractActionNameBuilder { + private static final Logger LOG = LogManager.getLogger(SEOActionNameBuilder.class); - private Set<String> actionSuffix = Collections.singleton("Action"); + private boolean lowerCase; private String separator; @Inject - public SEOActionNameBuilder(@Inject(value="struts.convention.action.name.lowercase") String lowerCase, - @Inject(value="struts.convention.action.name.separator") String separator) { + public SEOActionNameBuilder( + @Inject(ConventionConstants.CONVENTION_ACTION_NAME_LOWERCASE) String lowerCase, + @Inject(ConventionConstants.CONVENTION_ACTION_NAME_SEPARATOR) String separator + ) { this.lowerCase = Boolean.parseBoolean(lowerCase); this.separator = separator; } - /** - * @param actionSuffix (Optional) Classes that end with these value will be mapped as actions - * (defaults to "Action") - */ - @Inject(value = "struts.convention.action.suffix", required = false) - public void setActionSuffix(String actionSuffix) { - if (StringUtils.isNotBlank(actionSuffix)) { - this.actionSuffix = TextParseUtil.commaDelimitedStringToSet(actionSuffix); - } - } - public String build(String className) { String actionName = className; @@ -95,22 +82,4 @@ 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