Uses StringBuffer instead of StringBuilder

This commit is contained in:
Lukasz Lenart
2021-09-24 15:55:10 +02:00
parent 173338a444
commit 991170716a
@@ -19,7 +19,6 @@
package com.opensymphony.xwork2.config.impl;
import com.opensymphony.xwork2.util.PatternMatcher;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -36,16 +35,16 @@ import java.util.regex.Pattern;
/**
* <p> Matches patterns against pre-compiled wildcard expressions pulled from
* target objects. It uses the wildcard matcher from the Apache Cocoon
* project. Patterns will be matched in the order they were added. The first
* match wins, so more specific patterns should be defined before less specific
* project. Patterns will be matched in the order they were added. The first
* match wins, so more specific patterns should be defined before less specific
* patterns.
*
*
* @since 2.1
*/
public abstract class AbstractMatcher<E> implements Serializable {
private static final Logger LOG = LogManager.getLogger(AbstractMatcher.class);
private static final Pattern WILDCARD_PATTERN = Pattern.compile("\\{(.)\\}");
private static final Pattern WILDCARD_PATTERN = Pattern.compile("\\{(.)}");
/**
* <p> Handles all wildcard pattern matching. </p>
@@ -62,14 +61,14 @@ public abstract class AbstractMatcher<E> implements Serializable {
* to the map in {@link #replaceParameters(Map, Map)}
* and will be accessible in {@link com.opensymphony.xwork2.config.entities.ResultConfig}.
* If set to false, the named parameters won't be appended.
*
* <p>
* This behaviour is controlled by {@link org.apache.struts2.StrutsConstants#STRUTS_MATCHER_APPEND_NAMED_PARAMETERS}
*
* @since 2.5.23
* See WW-5065
*/
private final boolean appendNamedParameters;
public AbstractMatcher(PatternMatcher<?> helper, boolean appendNamedParameters) {
this.wildcard = (PatternMatcher<Object>) helper;
this.appendNamedParameters = appendNamedParameters;
@@ -92,18 +91,17 @@ public abstract class AbstractMatcher<E> implements Serializable {
* in the order they were added. Only patterns that actually contain a
* wildcard will be compiled.
* </p>
*
*
* <p>
* Patterns can optionally be matched "loosely". When the end of the pattern
* matches \*[^*]\*$ (wildcard, no wildcard, wildcard), if the pattern
* fails, it is also matched as if the last two characters didn't exist. The
* goal is to support the legacy "*!*" syntax, where the "!*" is optional.
* </p>
*
* @param name The pattern
* @param target The object to associate with the pattern
* @param looseMatch
* To loosely match wildcards or not
*
* @param name The pattern
* @param target The object to associate with the pattern
* @param looseMatch To loosely match wildcards or not
*/
public void addPattern(String name, E target, boolean looseMatch) {
@@ -130,9 +128,9 @@ public abstract class AbstractMatcher<E> implements Serializable {
}
}
}
public void freeze() {
compiledPatterns = Collections.unmodifiableList(new ArrayList<Mapping<E>>());
compiledPatterns = Collections.unmodifiableList(new ArrayList<>());
}
/**
@@ -147,7 +145,7 @@ public abstract class AbstractMatcher<E> implements Serializable {
if (compiledPatterns.size() > 0) {
LOG.debug("Attempting to match '{}' to a wildcard pattern, {} available", potentialMatch, compiledPatterns.size());
Map<String,String> vars = new LinkedHashMap<>();
Map<String, String> vars = new LinkedHashMap<>();
for (Mapping<E> m : compiledPatterns) {
if (wildcard.match(vars, potentialMatch, m.getPattern())) {
LOG.debug("Value matches pattern '{}'", m.getOriginalPattern());
@@ -168,23 +166,22 @@ public abstract class AbstractMatcher<E> implements Serializable {
* @param orig The original object
* @param vars A Map of wildcard-matched strings
* @return A cloned object with appropriate properties replaced with
* wildcard-matched values
* wildcard-matched values
*/
protected abstract E convert(String path, E orig, Map<String, String> vars);
/**
* <p>Replaces parameter values</p>
*
* @param orig The original parameters with placeholder values
* @param vars A Map of wildcard-matched strings
*
* @param orig The original parameters with placeholder values
* @param vars A Map of wildcard-matched strings
* @return map with replaced parameters
*/
protected Map<String,String> replaceParameters(Map<String, String> orig, Map<String,String> vars) {
protected Map<String, String> replaceParameters(Map<String, String> orig, Map<String, String> vars) {
Map<String, String> map = new LinkedHashMap<>();
//this will set the group index references, like {1}
for (Map.Entry<String,String> entry : orig.entrySet()) {
for (Map.Entry<String, String> entry : orig.entrySet()) {
map.put(entry.getKey(), convertParam(entry.getValue(), vars));
}
@@ -192,7 +189,7 @@ public abstract class AbstractMatcher<E> implements Serializable {
LOG.debug("Appending named parameters to the result map");
//the values map will contain entries like name->"Lex Luthor" and 1->"Lex Luthor"
//now add the non-numeric values
for (Map.Entry<String,String> entry: vars.entrySet()) {
for (Map.Entry<String, String> entry : vars.entrySet()) {
if (!NumberUtils.isCreatable(entry.getKey())) {
map.put(entry.getKey(), entry.getValue());
}
@@ -205,28 +202,28 @@ public abstract class AbstractMatcher<E> implements Serializable {
/**
* <p> Inserts into a value wildcard-matched strings where specified
* with the {x} syntax. If a wildcard-matched value isn't found, the
* replacement token is turned into an empty string.
* replacement token is turned into an empty string.
* </p>
*
* @param val The value to convert
* @param vars A Map of wildcard-matched strings
* @return The new value
*/
protected String convertParam(String val, Map<String, String> vars) {
if (val == null) {
return null;
}
protected String convertParam(String val, Map<String, String> vars) {
if (val == null) {
return null;
}
Matcher wildcardMatcher = WILDCARD_PATTERN.matcher(val);
Matcher wildcardMatcher = WILDCARD_PATTERN.matcher(val);
StringBuilder result = new StringBuilder();
while (wildcardMatcher.find()) {
wildcardMatcher.appendReplacement(result, vars.getOrDefault(wildcardMatcher.group(1), ""));
}
wildcardMatcher.appendTail(result);
StringBuffer result = new StringBuffer();
while (wildcardMatcher.find()) {
wildcardMatcher.appendReplacement(result, vars.getOrDefault(wildcardMatcher.group(1), ""));
}
wildcardMatcher.appendTail(result);
return result.toString();
}
return result.toString();
}
/**
* <p> Stores a compiled wildcard pattern and the object it came
@@ -238,7 +235,7 @@ public abstract class AbstractMatcher<E> implements Serializable {
*/
private final String original;
/**
* <p> The compiled pattern. </p>
*/
@@ -253,8 +250,8 @@ public abstract class AbstractMatcher<E> implements Serializable {
* <p> Contructs a read-only Mapping instance. </p>
*
* @param original The original pattern
* @param pattern The compiled pattern
* @param config The original object
* @param pattern The compiled pattern
* @param config The original object
*/
public Mapping(String original, Object pattern, E config) {
this.original = original;
@@ -279,7 +276,7 @@ public abstract class AbstractMatcher<E> implements Serializable {
public E getTarget() {
return this.config;
}
/**
* <p> Gets the original wildcard pattern. </p>
*