1
1
/*
2
- * Copyright 2012 MyBatis.org.
2
+ * Copyright 2012-2013 MyBatis.org.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
package org .apache .ibatis .builder ;
17
17
18
18
import java .util .HashMap ;
19
- import java .util .Map ;
20
19
21
20
/**
22
- * Inline parameter expression parser.
23
- * Supported grammar (simplified):
21
+ * Inline parameter expression parser. Supported grammar (simplified):
22
+ *
24
23
* <pre>
25
24
* inline-parameter = (propertyName | expression) oldJdbcType attributes
26
25
* propertyName = /expression language's property navigation path/
30
29
* attribute = name '=' value
31
30
* </pre>
32
31
*/
33
- public class ParameterExpressionParser {
32
+ public class ParameterExpression extends HashMap < String , String > {
34
33
35
- public static Map < String , String > parse ( String expression ) {
36
- Map < String , String > map = new HashMap < String , String >();
37
- parse ( expression , map );
38
- return map ;
34
+ private static final long serialVersionUID = - 2417552199605158680L ;
35
+
36
+ public ParameterExpression ( String expression ) {
37
+ parse ( expression ) ;
39
38
}
40
39
41
- private static void parse (String expression , Map < String , String > map ) {
40
+ private void parse (String expression ) {
42
41
int p = skipWS (expression , 0 );
43
42
if (expression .charAt (p ) == '(' ) {
44
- expression (expression , p + 1 , map );
43
+ expression (expression , p + 1 );
45
44
} else {
46
- property (expression , p , map );
45
+ property (expression , p );
47
46
}
48
47
}
49
48
50
- private static void expression (String expression , int left , Map < String , String > map ) {
49
+ private void expression (String expression , int left ) {
51
50
int match = 1 ;
52
51
int right = left + 1 ;
53
52
while (match > 0 ) {
@@ -58,19 +57,19 @@ private static void expression(String expression, int left, Map<String, String>
58
57
}
59
58
right ++;
60
59
}
61
- map . put ("expression" , expression .substring (left , right - 1 ));
62
- jdbcTypeOpt (expression , right , map );
60
+ put ("expression" , expression .substring (left , right - 1 ));
61
+ jdbcTypeOpt (expression , right );
63
62
}
64
63
65
- private static void property (String expression , int left , Map < String , String > map ) {
64
+ private void property (String expression , int left ) {
66
65
if (left < expression .length ()) {
67
66
int right = skipUntil (expression , left , ",:" );
68
- map . put ("property" , trimmedStr (expression , left , right ));
69
- jdbcTypeOpt (expression , right , map );
67
+ put ("property" , trimmedStr (expression , left , right ));
68
+ jdbcTypeOpt (expression , right );
70
69
}
71
70
}
72
71
73
- private static int skipWS (String expression , int p ) {
72
+ private int skipWS (String expression , int p ) {
74
73
for (int i = p ; i < expression .length (); i ++) {
75
74
if (expression .charAt (i ) > 0x20 ) {
76
75
return i ;
@@ -79,7 +78,7 @@ private static int skipWS(String expression, int p) {
79
78
return expression .length ();
80
79
}
81
80
82
- private static int skipUntil (String expression , int p , final String endChars ) {
81
+ private int skipUntil (String expression , int p , final String endChars ) {
83
82
for (int i = p ; i < expression .length (); i ++) {
84
83
char c = expression .charAt (i );
85
84
if (endChars .indexOf (c ) > -1 ) {
@@ -89,53 +88,51 @@ private static int skipUntil(String expression, int p, final String endChars) {
89
88
return expression .length ();
90
89
}
91
90
92
- private static void jdbcTypeOpt (String expression , int p , Map < String , String > map ) {
91
+ private void jdbcTypeOpt (String expression , int p ) {
93
92
p = skipWS (expression , p );
94
93
if (p < expression .length ()) {
95
94
if (expression .charAt (p ) == ':' ) {
96
- jdbcType (expression , p + 1 , map );
95
+ jdbcType (expression , p + 1 );
97
96
} else if (expression .charAt (p ) == ',' ) {
98
- option (expression , p + 1 , map );
97
+ option (expression , p + 1 );
99
98
} else {
100
99
throw new BuilderException ("Parsing error in {" + new String (expression ) + "} in position " + p );
101
100
}
102
101
}
103
102
}
104
103
105
- private static void jdbcType (String expression , int p , Map < String , String > map ) {
104
+ private void jdbcType (String expression , int p ) {
106
105
int left = skipWS (expression , p );
107
106
int right = skipUntil (expression , left , "," );
108
107
if (right > left ) {
109
- map . put ("jdbcType" , trimmedStr (expression , left , right ));
108
+ put ("jdbcType" , trimmedStr (expression , left , right ));
110
109
} else {
111
110
throw new BuilderException ("Parsing error in {" + new String (expression ) + "} in position " + p );
112
111
}
113
- option (expression , right + 1 , map );
112
+ option (expression , right + 1 );
114
113
}
115
114
116
- private static void option (String expression , int p , Map < String , String > map ) {
115
+ private void option (String expression , int p ) {
117
116
int left = skipWS (expression , p );
118
117
if (left < expression .length ()) {
119
118
int right = skipUntil (expression , left , "=" );
120
119
String name = trimmedStr (expression , left , right );
121
120
left = right + 1 ;
122
121
right = skipUntil (expression , left , "," );
123
122
String value = trimmedStr (expression , left , right );
124
- map . put (name , value );
125
- option (expression , right + 1 , map );
123
+ put (name , value );
124
+ option (expression , right + 1 );
126
125
}
127
126
}
128
127
129
- private static String trimmedStr (String str , int start , int end )
130
- {
131
- while (str .charAt (start ) <= 0x20 )
132
- {
128
+ private String trimmedStr (String str , int start , int end ) {
129
+ while (str .charAt (start ) <= 0x20 ) {
133
130
start ++;
134
131
}
135
- while (str .charAt (end - 1 ) <= 0x20 )
136
- {
132
+ while (str .charAt (end - 1 ) <= 0x20 ) {
137
133
end --;
138
134
}
139
135
return start >= end ? "" : str .substring (start , end );
140
136
}
137
+
141
138
}
0 commit comments