/
PathImpl.java
359 lines (301 loc) · 10.4 KB
/
PathImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
* JBoss, Home of Professional Open Source
* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hibernate.validator.engine;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.validation.Path;
import org.hibernate.validator.util.Contracts;
/**
* @author Hardy Ferentschik
* @author Gunnar Morling
* @author Kevin Pollet - SERLI - (kevin.pollet@serli.com)
*/
public final class PathImpl implements Path, Serializable {
private static final long serialVersionUID = 7564511574909882392L;
public static final String PROPERTY_PATH_SEPARATOR = ".";
/**
* Regular expression used to split a string path into its elements.
*
* @see <a href="http://www.regexplanet.com/simple/index.jsp">Regular expression tester</a>
*/
private static final String LEADING_PROPERTY_GROUP = "[^\\[\\.]+"; // everything up to a [ or .
private static final String OPTIONAL_INDEX_GROUP = "\\[(\\w*)\\]";
private static final String REMAINING_PROPERTY_STRING = "\\.(.*)"; // processed recursively
private static final Pattern PATH_PATTERN = Pattern.compile( "(" + LEADING_PROPERTY_GROUP + ")(" + OPTIONAL_INDEX_GROUP + ")?(" + REMAINING_PROPERTY_STRING + ")*" );
private static final int PROPERTY_NAME_GROUP = 1;
private static final int INDEXED_GROUP = 2;
private static final int INDEX_GROUP = 3;
private static final int REMAINING_STRING_GROUP = 5;
private final List<Node> nodeList;
private NodeImpl currentLeafNode;
private int hashCode;
/**
* Returns a {@code Path} instance representing the path described by the
* given string. To create a root node the empty string should be passed.
*
* @param propertyPath the path as string representation.
*
* @return a {@code Path} instance representing the path described by the
* given string.
*
* @throws IllegalArgumentException in case {@code property == null} or
* {@code property} cannot be parsed.
*/
public static PathImpl createPathFromString(String propertyPath) {
if ( propertyPath == null ) {
throw new IllegalArgumentException( "null is not allowed as property path." );
}
if ( propertyPath.length() == 0 ) {
return createNewPath( null );
}
return parseProperty( propertyPath );
}
/**
* Creates a path representing the specified method parameter.
*
* @param method The method hosting the parameter to represent.
* @param parameterName The parameter's name, e.g. "arg0" or "param1".
*
* @return A path representing the specified method parameter.
*/
public static PathImpl createPathForMethodParameter(Method method, String parameterName) {
Contracts.assertNotNull( method, "A method is required to create a method parameter path." );
Contracts.assertNotNull( parameterName, "A parameter name is required to create a method parameter path." );
PathImpl path = createRootPath();
path.addMethodParameterNode( method, parameterName );
return path;
}
public static PathImpl createPathForMethodReturnValue(Method method) {
Contracts.assertNotNull( method, "A method is required to create a method return value path." );
PathImpl path = createRootPath();
path.addMethodReturnValueNode( method );
return path;
}
public static PathImpl createNewPath(String name) {
PathImpl path = new PathImpl();
path.addNode( name );
return path;
}
public static PathImpl createRootPath() {
return createNewPath( null );
}
public static PathImpl createCopy(PathImpl path) {
return new PathImpl( path );
}
public final boolean isRootPath() {
return nodeList.size() == 1 && nodeList.get( 0 ).getName() == null;
}
public final PathImpl getPathWithoutLeafNode() {
return new PathImpl( nodeList.subList( 0, nodeList.size() - 1 ) );
}
public final NodeImpl addNode(String nodeName) {
NodeImpl parent = nodeList.size() == 0 ? null : (NodeImpl) nodeList.get( nodeList.size() - 1 );
currentLeafNode = new NodeImpl( nodeName, parent, false, null, null );
nodeList.add( currentLeafNode );
hashCode = -1;
return currentLeafNode;
}
private NodeImpl addMethodParameterNode(Method method, String parameterName) {
NodeImpl parent = nodeList.size() == 0 ? null : (NodeImpl) nodeList.get( nodeList.size() - 1 );
currentLeafNode = new MethodParameterNodeImpl( method, parameterName, parent );
nodeList.add( currentLeafNode );
hashCode = -1;
return currentLeafNode;
}
private NodeImpl addMethodReturnValueNode(Method method) {
NodeImpl parent = nodeList.size() == 0 ? null : (NodeImpl) nodeList.get( nodeList.size() - 1 );
currentLeafNode = new MethodReturnValueNodeImpl( method, parent );
nodeList.add( currentLeafNode );
hashCode = -1;
return currentLeafNode;
}
public final NodeImpl makeLeafNodeIterable() {
NodeImpl leafNode = getLeafNode();
currentLeafNode = new NodeImpl( leafNode.getName(), leafNode.getParent(), true, null, null );
nodeList.remove( leafNode );
nodeList.add( currentLeafNode );
hashCode = -1;
return currentLeafNode;
}
public final NodeImpl setLeafNodeIndex(Integer index) {
NodeImpl leafNode = getLeafNode();
currentLeafNode = new NodeImpl( leafNode.getName(), leafNode.getParent(), true, index, null );
nodeList.remove( leafNode );
nodeList.add( currentLeafNode );
hashCode = -1;
return currentLeafNode;
}
public final NodeImpl setLeafNodeMapKey(Object key) {
NodeImpl leafNode = getLeafNode();
currentLeafNode = new NodeImpl( leafNode.getName(), leafNode.getParent(), true, null, key );
nodeList.remove( leafNode );
nodeList.add( currentLeafNode );
hashCode = -1;
return currentLeafNode;
}
public final NodeImpl getLeafNode() {
return currentLeafNode;
}
public final Iterator<Path.Node> iterator() {
if ( nodeList.size() == 0 ) {
return Collections.<Path.Node>emptyList().iterator();
}
if ( nodeList.size() == 1 ) {
return nodeList.iterator();
}
return nodeList.subList( 1, nodeList.size() ).iterator();
}
public final String asString() {
StringBuilder builder = new StringBuilder();
boolean first = true;
for ( int i = 1; i < nodeList.size(); i++ ) {
NodeImpl nodeImpl = (NodeImpl) nodeList.get( i );
if ( nodeImpl.getName() != null ) {
if ( !first ) {
builder.append( PROPERTY_PATH_SEPARATOR );
}
builder.append( nodeImpl.asString() );
}
first = false;
}
return builder.toString();
}
@Override
public String toString() {
return asString();
}
@Override
public boolean equals(Object o) {
if ( this == o ) {
return true;
}
if ( o == null || getClass() != o.getClass() ) {
return false;
}
PathImpl path = (PathImpl) o;
if ( nodeList != null && !nodeList.equals( path.nodeList ) ) {
return false;
}
if ( nodeList == null && path.nodeList != null ) {
return false;
}
return true;
}
@Override
public int hashCode() {
if ( hashCode == -1 ) {
buildHashCode();
}
return hashCode;
}
public void buildHashCode() {
hashCode = nodeList != null ? nodeList.hashCode() : 0;
}
/**
* Copy constructor.
*
* @param path the path to make a copy of.
*/
private PathImpl(PathImpl path) {
this.nodeList = new ArrayList<Node>();
NodeImpl parent = null;
NodeImpl node = null;
for ( int i = 0; i < path.nodeList.size(); i++ ) {
node = (NodeImpl) path.nodeList.get( i );
NodeImpl newNode = new NodeImpl( node, parent );
this.nodeList.add( newNode );
parent = newNode;
}
currentLeafNode = node;
}
private PathImpl() {
nodeList = new ArrayList<Node>();
}
private PathImpl(List<Node> nodeList) {
this.nodeList = new ArrayList<Node>();
for ( Node node : nodeList ) {
this.nodeList.add( node );
}
}
private static PathImpl parseProperty(String property) {
PathImpl path = createNewPath( null );
String tmp = property;
do {
Matcher matcher = PATH_PATTERN.matcher( tmp );
if ( matcher.matches() ) {
String value = matcher.group( PROPERTY_NAME_GROUP );
if ( !isValidJavaIdentifier( value ) ) {
throw new IllegalArgumentException( value + " is not a valid Java Identifier" );
}
// create the node
path.addNode( value );
// is the node indexable
if ( matcher.group( INDEXED_GROUP ) != null ) {
path.makeLeafNodeIterable();
}
// take care of the index/key if one exists
String indexOrKey = matcher.group( INDEX_GROUP );
if ( indexOrKey != null && indexOrKey.length() > 0 ) {
try {
Integer i = Integer.parseInt( indexOrKey );
path.setLeafNodeIndex( i );
}
catch ( NumberFormatException e ) {
path.setLeafNodeMapKey( indexOrKey );
}
}
// match the remaining string
tmp = matcher.group( REMAINING_STRING_GROUP );
}
else {
throw new IllegalArgumentException( "Unable to parse property path " + property );
}
} while ( tmp != null );
if ( path.getLeafNode().isIterable() ) {
path.addNode( null );
}
return path;
}
/**
* Validate that the given identifier is a valid Java identifier according to the Java Language Specification,
* <a href="http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.8">chapter 3.8</a>
*
* @param identifier the identifier string
* @return true if the given identifier is a valid Java Identifier
*
* @throws IllegalArgumentException if the identifier is null
*/
private static boolean isValidJavaIdentifier(String identifier) {
Contracts.assertNotNull( identifier, "identifier param cannot be null" );
final char[] identifierChars = identifier.toCharArray();
if ( identifierChars.length == 0 || !Character.isJavaIdentifierStart( (int) identifierChars[0] ) ) {
return false;
}
for ( int i = 1; i < identifierChars.length; i++ ) {
if ( !Character.isJavaIdentifierPart( (int) identifierChars[i] ) ) {
return false;
}
}
return true;
}
}