1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.builder;
17
18 import java.util.HashMap;
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 public class ParameterExpression extends HashMap<String, String> {
35
36 private static final long serialVersionUID = -2417552199605158680L;
37
38 public ParameterExpression(String expression) {
39 parse(expression);
40 }
41
42 private void parse(String expression) {
43 int p = skipWS(expression, 0);
44 if (expression.charAt(p) == '(') {
45 expression(expression, p + 1);
46 } else {
47 property(expression, p);
48 }
49 }
50
51 private void expression(String expression, int left) {
52 int match = 1;
53 int right = left + 1;
54 while (match > 0) {
55 if (expression.charAt(right) == ')') {
56 match--;
57 } else if (expression.charAt(right) == '(') {
58 match++;
59 }
60 right++;
61 }
62 put("expression", expression.substring(left, right - 1));
63 jdbcTypeOpt(expression, right);
64 }
65
66 private void property(String expression, int left) {
67 if (left < expression.length()) {
68 int right = skipUntil(expression, left, ",:");
69 put("property", trimmedStr(expression, left, right));
70 jdbcTypeOpt(expression, right);
71 }
72 }
73
74 private int skipWS(String expression, int p) {
75 for (int i = p; i < expression.length(); i++) {
76 if (expression.charAt(i) > 0x20) {
77 return i;
78 }
79 }
80 return expression.length();
81 }
82
83 private int skipUntil(String expression, int p, final String endChars) {
84 for (int i = p; i < expression.length(); i++) {
85 char c = expression.charAt(i);
86 if (endChars.indexOf(c) > -1) {
87 return i;
88 }
89 }
90 return expression.length();
91 }
92
93 private void jdbcTypeOpt(String expression, int p) {
94 p = skipWS(expression, p);
95 if (p < expression.length()) {
96 if (expression.charAt(p) == ':') {
97 jdbcType(expression, p + 1);
98 } else if (expression.charAt(p) == ',') {
99 option(expression, p + 1);
100 } else {
101 throw new BuilderException("Parsing error in {" + expression + "} in position " + p);
102 }
103 }
104 }
105
106 private void jdbcType(String expression, int p) {
107 int left = skipWS(expression, p);
108 int right = skipUntil(expression, left, ",");
109 if (right > left) {
110 put("jdbcType", trimmedStr(expression, left, right));
111 } else {
112 throw new BuilderException("Parsing error in {" + expression + "} in position " + p);
113 }
114 option(expression, right + 1);
115 }
116
117 private void option(String expression, int p) {
118 int left = skipWS(expression, p);
119 if (left < expression.length()) {
120 int right = skipUntil(expression, left, "=");
121 String name = trimmedStr(expression, left, right);
122 left = right + 1;
123 right = skipUntil(expression, left, ",");
124 String value = trimmedStr(expression, left, right);
125 put(name, value);
126 option(expression, right + 1);
127 }
128 }
129
130 private String trimmedStr(String str, int start, int end) {
131 while (str.charAt(start) <= 0x20) {
132 start++;
133 }
134 while (str.charAt(end - 1) <= 0x20) {
135 end--;
136 }
137 return start >= end ? "" : str.substring(start, end);
138 }
139
140 }