1414 * limitations under the License.
1515 */
1616
17- package org .springframework .web .util .patterns ;
17+ package org .springframework .web .util .pattern ;
1818
1919import java .util .regex .Matcher ;
20-
21- import org .springframework .web .util .patterns .PathPattern .MatchingContext ;
20+ import java .util .regex .Pattern ;
2221
2322/**
2423 * A path element representing capturing a piece of the path as a variable. In the pattern
2524 * '/foo/{bar}/goo' the {bar} is represented as a {@link CaptureVariablePathElement}. There
2625 * must be at least one character to bind to the variable.
2726 *
2827 * @author Andy Clement
28+ * @since 5.0
2929 */
3030class CaptureVariablePathElement extends PathElement {
3131
32- private String variableName ;
32+ private final String variableName ;
33+
34+ private Pattern constraintPattern ;
3335
34- private java .util .regex .Pattern constraintPattern ;
3536
3637 /**
3738 * @param pos the position in the pattern of this capture element
@@ -48,43 +49,47 @@ class CaptureVariablePathElement extends PathElement {
4849 }
4950 if (colon == -1 ) {
5051 // no constraint
51- variableName = new String (captureDescriptor , 1 , captureDescriptor .length - 2 );
52+ this . variableName = new String (captureDescriptor , 1 , captureDescriptor .length - 2 );
5253 }
5354 else {
54- variableName = new String (captureDescriptor , 1 , colon - 1 );
55+ this . variableName = new String (captureDescriptor , 1 , colon - 1 );
5556 if (caseSensitive ) {
56- constraintPattern = java . util . regex . Pattern
57- . compile ( new String (captureDescriptor , colon + 1 , captureDescriptor .length - colon - 2 ));
57+ this . constraintPattern = Pattern . compile (
58+ new String (captureDescriptor , colon + 1 , captureDescriptor .length - colon - 2 ));
5859 }
5960 else {
60- constraintPattern = java . util . regex . Pattern .compile (
61+ this . constraintPattern = Pattern .compile (
6162 new String (captureDescriptor , colon + 1 , captureDescriptor .length - colon - 2 ),
62- java . util . regex . Pattern .CASE_INSENSITIVE );
63+ Pattern .CASE_INSENSITIVE );
6364 }
6465 }
6566 }
6667
68+
6769 @ Override
68- public boolean matches (int candidateIndex , MatchingContext matchingContext ) {
70+ public boolean matches (int candidateIndex , PathPattern . MatchingContext matchingContext ) {
6971 int nextPos = matchingContext .scanAhead (candidateIndex );
7072 // There must be at least one character to capture:
7173 if (nextPos == candidateIndex ) {
7274 return false ;
7375 }
76+
7477 CharSequence candidateCapture = null ;
75- if (constraintPattern != null ) {
78+ if (this . constraintPattern != null ) {
7679 // TODO possible optimization - only regex match if rest of pattern matches? Benefit likely to vary pattern to pattern
7780 candidateCapture = new SubSequence (matchingContext .candidate , candidateIndex , nextPos );
78- Matcher m = constraintPattern .matcher (candidateCapture );
79- if (m .groupCount () != 0 ) {
80- throw new IllegalArgumentException ("No capture groups allowed in the constraint regex: " + constraintPattern .pattern ());
81+ Matcher matcher = constraintPattern .matcher (candidateCapture );
82+ if (matcher .groupCount () != 0 ) {
83+ throw new IllegalArgumentException (
84+ "No capture groups allowed in the constraint regex: " + this .constraintPattern .pattern ());
8185 }
82- if (!m .matches ()) {
86+ if (!matcher .matches ()) {
8387 return false ;
8488 }
8589 }
90+
8691 boolean match = false ;
87- if (next == null ) {
92+ if (this . next == null ) {
8893 if (matchingContext .determineRemainingPath && nextPos > candidateIndex ) {
8994 matchingContext .remainingPathIndex = nextPos ;
9095 match = true ;
@@ -101,14 +106,16 @@ public boolean matches(int candidateIndex, MatchingContext matchingContext) {
101106 }
102107 else {
103108 if (matchingContext .isMatchStartMatching && nextPos == matchingContext .candidateLength ) {
104- match = true ; // no more data but matches up to this point
109+ match = true ; // no more data but matches up to this point
105110 }
106111 else {
107- match = next .matches (nextPos , matchingContext );
112+ match = this . next .matches (nextPos , matchingContext );
108113 }
109114 }
115+
110116 if (match && matchingContext .extractingVariables ) {
111- matchingContext .set (variableName , new String (matchingContext .candidate , candidateIndex , nextPos - candidateIndex ));
117+ matchingContext .set (this .variableName ,
118+ new String (matchingContext .candidate , candidateIndex , nextPos - candidateIndex ));
112119 }
113120 return match ;
114121 }
@@ -117,10 +124,6 @@ public String getVariableName() {
117124 return this .variableName ;
118125 }
119126
120- public String toString () {
121- return "CaptureVariable({" + variableName + (constraintPattern == null ? "" : ":" + constraintPattern .pattern ()) + "})" ;
122- }
123-
124127 @ Override
125128 public int getNormalizedLength () {
126129 return 1 ;
@@ -140,4 +143,11 @@ public int getCaptureCount() {
140143 public int getScore () {
141144 return CAPTURE_VARIABLE_WEIGHT ;
142145 }
143- }
146+
147+
148+ public String toString () {
149+ return "CaptureVariable({" + this .variableName +
150+ (this .constraintPattern != null ? ":" + this .constraintPattern .pattern () : "" ) + "})" ;
151+ }
152+
153+ }
0 commit comments