Browse Source

Polishing

pull/27555/head
Juergen Hoeller 3 years ago
parent
commit
b1c7f7d127
  1. 19
      spring-web/src/main/java/org/springframework/web/util/pattern/CaptureTheRestPathElement.java
  2. 27
      spring-web/src/main/java/org/springframework/web/util/pattern/CaptureVariablePathElement.java
  3. 9
      spring-web/src/main/java/org/springframework/web/util/pattern/LiteralPathElement.java
  4. 12
      spring-web/src/main/java/org/springframework/web/util/pattern/PathPattern.java
  5. 14
      spring-web/src/main/java/org/springframework/web/util/pattern/PatternParseException.java
  6. 32
      spring-web/src/main/java/org/springframework/web/util/pattern/RegexPathElement.java
  7. 11
      spring-web/src/main/java/org/springframework/web/util/pattern/SeparatorPathElement.java
  8. 12
      spring-web/src/main/java/org/springframework/web/util/pattern/SingleCharWildcardedPathElement.java
  9. 11
      spring-web/src/main/java/org/springframework/web/util/pattern/WildcardPathElement.java
  10. 11
      spring-web/src/main/java/org/springframework/web/util/pattern/WildcardTheRestPathElement.java

19
spring-web/src/main/java/org/springframework/web/util/pattern/CaptureTheRestPathElement.java

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -83,17 +83,17 @@ class CaptureTheRestPathElement extends PathElement { @@ -83,17 +83,17 @@ class CaptureTheRestPathElement extends PathElement {
}
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++) {
Element element = pathElements.get(i);
if (element instanceof PathSegment) {
buf.append(((PathSegment)element).valueToMatch());
sb.append(((PathSegment)element).valueToMatch());
}
else {
buf.append(element.value());
sb.append(element.value());
}
}
return buf.toString();
return sb.toString();
}
@Override
@ -101,6 +101,11 @@ class CaptureTheRestPathElement extends PathElement { @@ -101,6 +101,11 @@ class CaptureTheRestPathElement extends PathElement {
return 1;
}
@Override
public char[] getChars() {
return ("/{*" + this.variableName + "}").toCharArray();
}
@Override
public int getWildcardCount() {
return 0;
@ -117,8 +122,4 @@ class CaptureTheRestPathElement extends PathElement { @@ -117,8 +122,4 @@ class CaptureTheRestPathElement extends PathElement {
return "CaptureTheRest(/{*" + this.variableName + "})";
}
@Override
public char[] getChars() {
return ("/{*"+this.variableName+"}").toCharArray();
}
}

27
spring-web/src/main/java/org/springframework/web/util/pattern/CaptureVariablePathElement.java

@ -35,7 +35,7 @@ class CaptureVariablePathElement extends PathElement { @@ -35,7 +35,7 @@ class CaptureVariablePathElement extends PathElement {
private final String variableName;
@Nullable
private Pattern constraintPattern;
private final Pattern constraintPattern;
/**
@ -55,6 +55,7 @@ class CaptureVariablePathElement extends PathElement { @@ -55,6 +55,7 @@ class CaptureVariablePathElement extends PathElement {
if (colon == -1) {
// no constraint
this.variableName = new String(captureDescriptor, 1, captureDescriptor.length - 2);
this.constraintPattern = null;
}
else {
this.variableName = new String(captureDescriptor, 1, colon - 1);
@ -134,6 +135,18 @@ class CaptureVariablePathElement extends PathElement { @@ -134,6 +135,18 @@ class CaptureVariablePathElement extends PathElement {
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
public int getWildcardCount() {
return 0;
@ -156,16 +169,4 @@ class CaptureVariablePathElement extends PathElement { @@ -156,16 +169,4 @@ class CaptureVariablePathElement extends PathElement {
(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();
}
}

9
spring-web/src/main/java/org/springframework/web/util/pattern/LiteralPathElement.java

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -26,14 +26,15 @@ import org.springframework.web.util.pattern.PathPattern.MatchingContext; @@ -26,14 +26,15 @@ import org.springframework.web.util.pattern.PathPattern.MatchingContext;
* literal path elements 'foo', 'bar' and 'goo'.
*
* @author Andy Clement
* @since 5.0
*/
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) {

12
spring-web/src/main/java/org/springframework/web/util/pattern/PathPattern.java

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

14
spring-web/src/main/java/org/springframework/web/util/pattern/PatternParseException.java

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -66,14 +66,14 @@ public class PatternParseException extends IllegalArgumentException { @@ -66,14 +66,14 @@ public class PatternParseException extends IllegalArgumentException {
* with a pointer to the error position, as well as the error message.
*/
public String toDetailedString() {
StringBuilder buf = new StringBuilder();
buf.append(this.pattern).append('\n');
StringBuilder sb = new StringBuilder();
sb.append(this.pattern).append('\n');
for (int i = 0; i < this.position; i++) {
buf.append(' ');
sb.append(' ');
}
buf.append("^\n");
buf.append(getMessage());
return buf.toString();
sb.append("^\n");
sb.append(getMessage());
return sb.toString();
}
public int getPosition() {

32
spring-web/src/main/java/org/springframework/web/util/pattern/RegexPathElement.java

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

11
spring-web/src/main/java/org/springframework/web/util/pattern/SeparatorPathElement.java

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -63,13 +63,14 @@ class SeparatorPathElement extends PathElement { @@ -63,13 +63,14 @@ class SeparatorPathElement extends PathElement {
}
@Override
public String toString() {
return "Separator(" + this.separator + ")";
public char[] getChars() {
return new char[] {this.separator};
}
@Override
public char[] getChars() {
return new char[] {this.separator};
public String toString() {
return "Separator(" + this.separator + ")";
}
}

12
spring-web/src/main/java/org/springframework/web/util/pattern/SingleCharWildcardedPathElement.java

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -124,15 +124,15 @@ class SingleCharWildcardedPathElement extends PathElement { @@ -124,15 +124,15 @@ class SingleCharWildcardedPathElement extends PathElement {
return this.len;
}
@Override
public String toString() {
return "SingleCharWildcarded(" + String.valueOf(this.text) + ")";
public char[] getChars() {
return this.text;
}
@Override
public char[] getChars() {
return this.text;
public String toString() {
return "SingleCharWildcarded(" + String.valueOf(this.text) + ")";
}
}

11
spring-web/src/main/java/org/springframework/web/util/pattern/WildcardPathElement.java

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -86,6 +86,11 @@ class WildcardPathElement extends PathElement { @@ -86,6 +86,11 @@ class WildcardPathElement extends PathElement {
return 1;
}
@Override
public char[] getChars() {
return new char[] {'*'};
}
@Override
public int getWildcardCount() {
return 1;
@ -102,8 +107,4 @@ class WildcardPathElement extends PathElement { @@ -102,8 +107,4 @@ class WildcardPathElement extends PathElement {
return "Wildcard(*)";
}
@Override
public char[] getChars() {
return new char[] {'*'};
}
}

11
spring-web/src/main/java/org/springframework/web/util/pattern/WildcardTheRestPathElement.java

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -47,6 +47,11 @@ class WildcardTheRestPathElement extends PathElement { @@ -47,6 +47,11 @@ class WildcardTheRestPathElement extends PathElement {
return 1;
}
@Override
public char[] getChars() {
return (this.separator + "**").toCharArray();
}
@Override
public int getWildcardCount() {
return 1;
@ -58,8 +63,4 @@ class WildcardTheRestPathElement extends PathElement { @@ -58,8 +63,4 @@ class WildcardTheRestPathElement extends PathElement {
return "WildcardTheRest(" + this.separator + "**)";
}
@Override
public char[] getChars() {
return (this.separator+"**").toCharArray();
}
}

Loading…
Cancel
Save