Extract common code

This commit is contained in:
Lukasz Lenart
2017-07-07 10:58:26 +02:00
parent b708d844c2
commit f522fbc454
4 changed files with 60 additions and 66 deletions
@@ -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;
}
}
@@ -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";
}
@@ -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;
}
}
@@ -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;
}
}