Uses multiple suffixes

This commit is contained in:
Lukasz Lenart
2017-07-07 10:24:02 +02:00
parent cf10f68e76
commit b708d844c2
2 changed files with 45 additions and 13 deletions
@@ -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;
}
}
@@ -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;
}
}