From 991170716ad32bc65505d562472afda95092ddf0 Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Fri, 24 Sep 2021 15:55:10 +0200 Subject: [PATCH] Uses StringBuffer instead of StringBuilder --- .../xwork2/config/impl/AbstractMatcher.java | 77 +++++++++---------- 1 file changed, 37 insertions(+), 40 deletions(-) diff --git a/core/src/main/java/com/opensymphony/xwork2/config/impl/AbstractMatcher.java b/core/src/main/java/com/opensymphony/xwork2/config/impl/AbstractMatcher.java index 509f851c4..0b95367e3 100644 --- a/core/src/main/java/com/opensymphony/xwork2/config/impl/AbstractMatcher.java +++ b/core/src/main/java/com/opensymphony/xwork2/config/impl/AbstractMatcher.java @@ -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; /** *

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 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("\\{(.)}"); /** *

Handles all wildcard pattern matching.

@@ -62,14 +61,14 @@ public abstract class AbstractMatcher 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. - * + *

* 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) helper; this.appendNamedParameters = appendNamedParameters; @@ -92,18 +91,17 @@ public abstract class AbstractMatcher implements Serializable { * in the order they were added. Only patterns that actually contain a * wildcard will be compiled. *

- * + * *

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

- * - * @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 implements Serializable { } } } - + public void freeze() { - compiledPatterns = Collections.unmodifiableList(new ArrayList>()); + compiledPatterns = Collections.unmodifiableList(new ArrayList<>()); } /** @@ -147,7 +145,7 @@ public abstract class AbstractMatcher implements Serializable { if (compiledPatterns.size() > 0) { LOG.debug("Attempting to match '{}' to a wildcard pattern, {} available", potentialMatch, compiledPatterns.size()); - Map vars = new LinkedHashMap<>(); + Map vars = new LinkedHashMap<>(); for (Mapping m : compiledPatterns) { if (wildcard.match(vars, potentialMatch, m.getPattern())) { LOG.debug("Value matches pattern '{}'", m.getOriginalPattern()); @@ -168,23 +166,22 @@ public abstract class AbstractMatcher 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 vars); /** *

Replaces parameter values

* - * @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 replaceParameters(Map orig, Map vars) { + protected Map replaceParameters(Map orig, Map vars) { Map map = new LinkedHashMap<>(); //this will set the group index references, like {1} - for (Map.Entry entry : orig.entrySet()) { + for (Map.Entry entry : orig.entrySet()) { map.put(entry.getKey(), convertParam(entry.getValue(), vars)); } @@ -192,7 +189,7 @@ public abstract class AbstractMatcher 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 entry: vars.entrySet()) { + for (Map.Entry entry : vars.entrySet()) { if (!NumberUtils.isCreatable(entry.getKey())) { map.put(entry.getKey(), entry.getValue()); } @@ -205,28 +202,28 @@ public abstract class AbstractMatcher implements Serializable { /** *

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

* * @param val The value to convert * @param vars A Map of wildcard-matched strings * @return The new value */ - protected String convertParam(String val, Map vars) { - if (val == null) { - return null; - } + protected String convertParam(String val, Map 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(); + } /** *

Stores a compiled wildcard pattern and the object it came @@ -238,7 +235,7 @@ public abstract class AbstractMatcher implements Serializable { */ private final String original; - + /** *

The compiled pattern.

*/ @@ -253,8 +250,8 @@ public abstract class AbstractMatcher implements Serializable { *

Contructs a read-only Mapping instance.

* * @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 implements Serializable { public E getTarget() { return this.config; } - + /** *

Gets the original wildcard pattern.

*