14
14
* limitations under the License.
15
15
*/
16
16
17
- package org .springframework .web .util .patterns ;
17
+ package org .springframework .web .util .pattern ;
18
18
19
19
import java .util .regex .Matcher ;
20
-
21
- import org .springframework .web .util .patterns .PathPattern .MatchingContext ;
20
+ import java .util .regex .Pattern ;
22
21
23
22
/**
24
23
* A path element representing capturing a piece of the path as a variable. In the pattern
25
24
* '/foo/{bar}/goo' the {bar} is represented as a {@link CaptureVariablePathElement}. There
26
25
* must be at least one character to bind to the variable.
27
26
*
28
27
* @author Andy Clement
28
+ * @since 5.0
29
29
*/
30
30
class CaptureVariablePathElement extends PathElement {
31
31
32
- private String variableName ;
32
+ private final String variableName ;
33
+
34
+ private Pattern constraintPattern ;
33
35
34
- private java .util .regex .Pattern constraintPattern ;
35
36
36
37
/**
37
38
* @param pos the position in the pattern of this capture element
@@ -48,43 +49,47 @@ class CaptureVariablePathElement extends PathElement {
48
49
}
49
50
if (colon == -1 ) {
50
51
// no constraint
51
- variableName = new String (captureDescriptor , 1 , captureDescriptor .length - 2 );
52
+ this . variableName = new String (captureDescriptor , 1 , captureDescriptor .length - 2 );
52
53
}
53
54
else {
54
- variableName = new String (captureDescriptor , 1 , colon - 1 );
55
+ this . variableName = new String (captureDescriptor , 1 , colon - 1 );
55
56
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 ));
58
59
}
59
60
else {
60
- constraintPattern = java . util . regex . Pattern .compile (
61
+ this . constraintPattern = Pattern .compile (
61
62
new String (captureDescriptor , colon + 1 , captureDescriptor .length - colon - 2 ),
62
- java . util . regex . Pattern .CASE_INSENSITIVE );
63
+ Pattern .CASE_INSENSITIVE );
63
64
}
64
65
}
65
66
}
66
67
68
+
67
69
@ Override
68
- public boolean matches (int candidateIndex , MatchingContext matchingContext ) {
70
+ public boolean matches (int candidateIndex , PathPattern . MatchingContext matchingContext ) {
69
71
int nextPos = matchingContext .scanAhead (candidateIndex );
70
72
// There must be at least one character to capture:
71
73
if (nextPos == candidateIndex ) {
72
74
return false ;
73
75
}
76
+
74
77
CharSequence candidateCapture = null ;
75
- if (constraintPattern != null ) {
78
+ if (this . constraintPattern != null ) {
76
79
// TODO possible optimization - only regex match if rest of pattern matches? Benefit likely to vary pattern to pattern
77
80
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 ());
81
85
}
82
- if (!m .matches ()) {
86
+ if (!matcher .matches ()) {
83
87
return false ;
84
88
}
85
89
}
90
+
86
91
boolean match = false ;
87
- if (next == null ) {
92
+ if (this . next == null ) {
88
93
if (matchingContext .determineRemainingPath && nextPos > candidateIndex ) {
89
94
matchingContext .remainingPathIndex = nextPos ;
90
95
match = true ;
@@ -101,14 +106,16 @@ public boolean matches(int candidateIndex, MatchingContext matchingContext) {
101
106
}
102
107
else {
103
108
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
105
110
}
106
111
else {
107
- match = next .matches (nextPos , matchingContext );
112
+ match = this . next .matches (nextPos , matchingContext );
108
113
}
109
114
}
115
+
110
116
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 ));
112
119
}
113
120
return match ;
114
121
}
@@ -117,10 +124,6 @@ public String getVariableName() {
117
124
return this .variableName ;
118
125
}
119
126
120
- public String toString () {
121
- return "CaptureVariable({" + variableName + (constraintPattern == null ? "" : ":" + constraintPattern .pattern ()) + "})" ;
122
- }
123
-
124
127
@ Override
125
128
public int getNormalizedLength () {
126
129
return 1 ;
@@ -140,4 +143,11 @@ public int getCaptureCount() {
140
143
public int getScore () {
141
144
return CAPTURE_VARIABLE_WEIGHT ;
142
145
}
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