Polishing

This commit is contained in:
Juergen Hoeller
2021-10-13 12:48:47 +02:00
parent 0f36569d75
commit b1c7f7d127
10 changed files with 83 additions and 77 deletions

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -83,17 +83,17 @@ class CaptureTheRestPathElement extends PathElement {
} }
private String pathToString(int fromSegment, List<Element> pathElements) { private String pathToString(int fromSegment, List<Element> pathElements) {
StringBuilder buf = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (int i = fromSegment, max = pathElements.size(); i < max; i++) { for (int i = fromSegment, max = pathElements.size(); i < max; i++) {
Element element = pathElements.get(i); Element element = pathElements.get(i);
if (element instanceof PathSegment) { if (element instanceof PathSegment) {
buf.append(((PathSegment)element).valueToMatch()); sb.append(((PathSegment)element).valueToMatch());
} }
else { else {
buf.append(element.value()); sb.append(element.value());
} }
} }
return buf.toString(); return sb.toString();
} }
@Override @Override
@@ -101,6 +101,11 @@ class CaptureTheRestPathElement extends PathElement {
return 1; return 1;
} }
@Override
public char[] getChars() {
return ("/{*" + this.variableName + "}").toCharArray();
}
@Override @Override
public int getWildcardCount() { public int getWildcardCount() {
return 0; return 0;
@@ -117,8 +122,4 @@ class CaptureTheRestPathElement extends PathElement {
return "CaptureTheRest(/{*" + this.variableName + "})"; return "CaptureTheRest(/{*" + this.variableName + "})";
} }
@Override
public char[] getChars() {
return ("/{*"+this.variableName+"}").toCharArray();
}
} }

View File

@@ -35,7 +35,7 @@ class CaptureVariablePathElement extends PathElement {
private final String variableName; private final String variableName;
@Nullable @Nullable
private Pattern constraintPattern; private final Pattern constraintPattern;
/** /**
@@ -55,6 +55,7 @@ class CaptureVariablePathElement extends PathElement {
if (colon == -1) { if (colon == -1) {
// no constraint // no constraint
this.variableName = new String(captureDescriptor, 1, captureDescriptor.length - 2); this.variableName = new String(captureDescriptor, 1, captureDescriptor.length - 2);
this.constraintPattern = null;
} }
else { else {
this.variableName = new String(captureDescriptor, 1, colon - 1); this.variableName = new String(captureDescriptor, 1, colon - 1);
@@ -134,6 +135,18 @@ class CaptureVariablePathElement extends PathElement {
return 1; return 1;
} }
@Override
public char[] getChars() {
StringBuilder sb = new StringBuilder();
sb.append('{');
sb.append(this.variableName);
if (this.constraintPattern != null) {
sb.append(':').append(this.constraintPattern.pattern());
}
sb.append('}');
return sb.toString().toCharArray();
}
@Override @Override
public int getWildcardCount() { public int getWildcardCount() {
return 0; return 0;
@@ -156,16 +169,4 @@ class CaptureVariablePathElement extends PathElement {
(this.constraintPattern != null ? ":" + this.constraintPattern.pattern() : "") + "})"; (this.constraintPattern != null ? ":" + this.constraintPattern.pattern() : "") + "})";
} }
@Override
public char[] getChars() {
StringBuilder b = new StringBuilder();
b.append('{');
b.append(this.variableName);
if (this.constraintPattern != null) {
b.append(':').append(this.constraintPattern.pattern());
}
b.append('}');
return b.toString().toCharArray();
}
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2020 the original author or authors. * Copyright 2002-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -26,14 +26,15 @@ import org.springframework.web.util.pattern.PathPattern.MatchingContext;
* literal path elements 'foo', 'bar' and 'goo'. * literal path elements 'foo', 'bar' and 'goo'.
* *
* @author Andy Clement * @author Andy Clement
* @since 5.0
*/ */
class LiteralPathElement extends PathElement { class LiteralPathElement extends PathElement {
private char[] text; private final char[] text;
private int len; private final int len;
private boolean caseSensitive; private final boolean caseSensitive;
public LiteralPathElement(int pos, char[] literalText, boolean caseSensitive, char separator) { public LiteralPathElement(int pos, char[] literalText, boolean caseSensitive, char separator) {

View File

@@ -336,18 +336,18 @@ public class PathPattern implements Comparable<PathPattern> {
PathContainer resultPath = null; PathContainer resultPath = null;
if (multipleAdjacentSeparators) { if (multipleAdjacentSeparators) {
// Need to rebuild the path without the duplicate adjacent separators // Need to rebuild the path without the duplicate adjacent separators
StringBuilder buf = new StringBuilder(); StringBuilder sb = new StringBuilder();
int i = startIndex; int i = startIndex;
while (i < endIndex) { while (i < endIndex) {
Element e = pathElements.get(i++); Element e = pathElements.get(i++);
buf.append(e.value()); sb.append(e.value());
if (e instanceof Separator) { if (e instanceof Separator) {
while (i < endIndex && (pathElements.get(i) instanceof Separator)) { while (i < endIndex && (pathElements.get(i) instanceof Separator)) {
i++; i++;
} }
} }
} }
resultPath = PathContainer.parsePath(buf.toString(), this.pathOptions); resultPath = PathContainer.parsePath(sb.toString(), this.pathOptions);
} }
else if (startIndex >= endIndex) { else if (startIndex >= endIndex) {
resultPath = PathContainer.parsePath(""); resultPath = PathContainer.parsePath("");
@@ -491,13 +491,13 @@ public class PathPattern implements Comparable<PathPattern> {
* @return the string form of the pattern * @return the string form of the pattern
*/ */
String computePatternString() { String computePatternString() {
StringBuilder buf = new StringBuilder(); StringBuilder sb = new StringBuilder();
PathElement pe = this.head; PathElement pe = this.head;
while (pe != null) { while (pe != null) {
buf.append(pe.getChars()); sb.append(pe.getChars());
pe = pe.next; pe = pe.next;
} }
return buf.toString(); return sb.toString();
} }
@Nullable @Nullable

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -66,14 +66,14 @@ public class PatternParseException extends IllegalArgumentException {
* with a pointer to the error position, as well as the error message. * with a pointer to the error position, as well as the error message.
*/ */
public String toDetailedString() { public String toDetailedString() {
StringBuilder buf = new StringBuilder(); StringBuilder sb = new StringBuilder();
buf.append(this.pattern).append('\n'); sb.append(this.pattern).append('\n');
for (int i = 0; i < this.position; i++) { for (int i = 0; i < this.position; i++) {
buf.append(' '); sb.append(' ');
} }
buf.append("^\n"); sb.append("^\n");
buf.append(getMessage()); sb.append(getMessage());
return buf.toString(); return sb.toString();
} }
public int getPosition() { public int getPosition() {

View File

@@ -136,20 +136,19 @@ class RegexPathElement extends PathElement {
if (matches) { if (matches) {
if (isNoMorePattern()) { if (isNoMorePattern()) {
if (matchingContext.determineRemainingPath && if (matchingContext.determineRemainingPath &&
(this.variableNames.isEmpty() || textToMatch.length() > 0)) { (this.variableNames.isEmpty() || textToMatch.length() > 0)) {
matchingContext.remainingPathIndex = pathIndex + 1; matchingContext.remainingPathIndex = pathIndex + 1;
matches = true; matches = true;
} }
else { else {
// No more pattern, is there more data? // No more pattern, is there more data?
// If pattern is capturing variables there must be some actual data to bind to them // If pattern is capturing variables there must be some actual data to bind to them
matches = (pathIndex + 1) >= matchingContext.pathLength matches = (pathIndex + 1 >= matchingContext.pathLength) &&
&& (this.variableNames.isEmpty() || textToMatch.length() > 0); (this.variableNames.isEmpty() || textToMatch.length() > 0);
if (!matches && matchingContext.isMatchOptionalTrailingSeparator()) { if (!matches && matchingContext.isMatchOptionalTrailingSeparator()) {
matches = (this.variableNames.isEmpty() matches = (this.variableNames.isEmpty() || textToMatch.length() > 0) &&
|| textToMatch.length() > 0) (pathIndex + 2 >= matchingContext.pathLength) &&
&& (pathIndex + 2) >= matchingContext.pathLength matchingContext.isSeparator(pathIndex + 1);
&& matchingContext.isSeparator(pathIndex + 1);
} }
} }
} }
@@ -160,11 +159,11 @@ class RegexPathElement extends PathElement {
if (matches && matchingContext.extractingVariables) { if (matches && matchingContext.extractingVariables) {
// Process captures // Process captures
if (this.variableNames.size() != matcher.groupCount()) { // SPR-8455 if (this.variableNames.size() != matcher.groupCount()) { // SPR-8455
throw new IllegalArgumentException("The number of capturing groups in the pattern segment " throw new IllegalArgumentException("The number of capturing groups in the pattern segment " +
+ this.pattern + " does not match the number of URI template variables it defines, " this.pattern + " does not match the number of URI template variables it defines, " +
+ "which can occur if capturing groups are used in a URI template regex. " "which can occur if capturing groups are used in a URI template regex. " +
+ "Use non-capturing groups instead."); "Use non-capturing groups instead.");
} }
for (int i = 1; i <= matcher.groupCount(); i++) { for (int i = 1; i <= matcher.groupCount(); i++) {
String name = this.variableNames.get(i - 1); String name = this.variableNames.get(i - 1);
@@ -187,6 +186,11 @@ class RegexPathElement extends PathElement {
return (this.regex.length - varsLength - this.variableNames.size()); return (this.regex.length - varsLength - this.variableNames.size());
} }
@Override
public char[] getChars() {
return this.regex;
}
@Override @Override
public int getCaptureCount() { public int getCaptureCount() {
return this.variableNames.size(); return this.variableNames.size();
@@ -208,8 +212,4 @@ class RegexPathElement extends PathElement {
return "Regex(" + String.valueOf(this.regex) + ")"; return "Regex(" + String.valueOf(this.regex) + ")";
} }
@Override
public char[] getChars() {
return this.regex;
}
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -62,14 +62,15 @@ class SeparatorPathElement extends PathElement {
return 1; return 1;
} }
@Override
public String toString() {
return "Separator(" + this.separator + ")";
}
@Override @Override
public char[] getChars() { public char[] getChars() {
return new char[] {this.separator}; return new char[] {this.separator};
} }
@Override
public String toString() {
return "Separator(" + this.separator + ")";
}
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2020 the original author or authors. * Copyright 2002-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -124,15 +124,15 @@ class SingleCharWildcardedPathElement extends PathElement {
return this.len; return this.len;
} }
@Override
public char[] getChars() {
return this.text;
}
@Override @Override
public String toString() { public String toString() {
return "SingleCharWildcarded(" + String.valueOf(this.text) + ")"; return "SingleCharWildcarded(" + String.valueOf(this.text) + ")";
} }
@Override
public char[] getChars() {
return this.text;
}
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -86,6 +86,11 @@ class WildcardPathElement extends PathElement {
return 1; return 1;
} }
@Override
public char[] getChars() {
return new char[] {'*'};
}
@Override @Override
public int getWildcardCount() { public int getWildcardCount() {
return 1; return 1;
@@ -102,8 +107,4 @@ class WildcardPathElement extends PathElement {
return "Wildcard(*)"; return "Wildcard(*)";
} }
@Override
public char[] getChars() {
return new char[] {'*'};
}
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -47,6 +47,11 @@ class WildcardTheRestPathElement extends PathElement {
return 1; return 1;
} }
@Override
public char[] getChars() {
return (this.separator + "**").toCharArray();
}
@Override @Override
public int getWildcardCount() { public int getWildcardCount() {
return 1; return 1;
@@ -58,8 +63,4 @@ class WildcardTheRestPathElement extends PathElement {
return "WildcardTheRest(" + this.separator + "**)"; return "WildcardTheRest(" + this.separator + "**)";
} }
@Override
public char[] getChars() {
return (this.separator+"**").toCharArray();
}
} }