Skip to content

Commit 89e2082

Browse files
committed
ParameterExpressionParser refactored
1 parent 66f2a2f commit 89e2082

File tree

4 files changed

+51
-54
lines changed

4 files changed

+51
-54
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012 MyBatis.org.
2+
* Copyright 2012-2013 MyBatis.org.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,11 +16,10 @@
1616
package org.apache.ibatis.builder;
1717

1818
import java.util.HashMap;
19-
import java.util.Map;
2019

2120
/**
22-
* Inline parameter expression parser.
23-
* Supported grammar (simplified):
21+
* Inline parameter expression parser. Supported grammar (simplified):
22+
*
2423
* <pre>
2524
* inline-parameter = (propertyName | expression) oldJdbcType attributes
2625
* propertyName = /expression language's property navigation path/
@@ -30,24 +29,24 @@
3029
* attribute = name '=' value
3130
* </pre>
3231
*/
33-
public class ParameterExpressionParser {
32+
public class ParameterExpression extends HashMap<String, String> {
3433

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);
3938
}
4039

41-
private static void parse(String expression, Map<String, String> map) {
40+
private void parse(String expression) {
4241
int p = skipWS(expression, 0);
4342
if (expression.charAt(p) == '(') {
44-
expression(expression, p + 1, map);
43+
expression(expression, p + 1);
4544
} else {
46-
property(expression, p, map);
45+
property(expression, p);
4746
}
4847
}
4948

50-
private static void expression(String expression, int left, Map<String, String> map) {
49+
private void expression(String expression, int left) {
5150
int match = 1;
5251
int right = left + 1;
5352
while (match > 0) {
@@ -58,19 +57,19 @@ private static void expression(String expression, int left, Map<String, String>
5857
}
5958
right++;
6059
}
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);
6362
}
6463

65-
private static void property(String expression, int left, Map<String, String> map) {
64+
private void property(String expression, int left) {
6665
if (left < expression.length()) {
6766
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);
7069
}
7170
}
7271

73-
private static int skipWS(String expression, int p) {
72+
private int skipWS(String expression, int p) {
7473
for (int i = p; i < expression.length(); i++) {
7574
if (expression.charAt(i) > 0x20) {
7675
return i;
@@ -79,7 +78,7 @@ private static int skipWS(String expression, int p) {
7978
return expression.length();
8079
}
8180

82-
private static int skipUntil(String expression, int p, final String endChars) {
81+
private int skipUntil(String expression, int p, final String endChars) {
8382
for (int i = p; i < expression.length(); i++) {
8483
char c = expression.charAt(i);
8584
if (endChars.indexOf(c) > -1) {
@@ -89,53 +88,51 @@ private static int skipUntil(String expression, int p, final String endChars) {
8988
return expression.length();
9089
}
9190

92-
private static void jdbcTypeOpt(String expression, int p, Map<String, String> map) {
91+
private void jdbcTypeOpt(String expression, int p) {
9392
p = skipWS(expression, p);
9493
if (p < expression.length()) {
9594
if (expression.charAt(p) == ':') {
96-
jdbcType(expression, p + 1, map);
95+
jdbcType(expression, p + 1);
9796
} else if (expression.charAt(p) == ',') {
98-
option(expression, p + 1, map);
97+
option(expression, p + 1);
9998
} else {
10099
throw new BuilderException("Parsing error in {" + new String(expression) + "} in position " + p);
101100
}
102101
}
103102
}
104103

105-
private static void jdbcType(String expression, int p, Map<String, String> map) {
104+
private void jdbcType(String expression, int p) {
106105
int left = skipWS(expression, p);
107106
int right = skipUntil(expression, left, ",");
108107
if (right > left) {
109-
map.put("jdbcType", trimmedStr(expression, left, right));
108+
put("jdbcType", trimmedStr(expression, left, right));
110109
} else {
111110
throw new BuilderException("Parsing error in {" + new String(expression) + "} in position " + p);
112111
}
113-
option(expression, right + 1, map);
112+
option(expression, right + 1);
114113
}
115114

116-
private static void option(String expression, int p, Map<String, String> map) {
115+
private void option(String expression, int p) {
117116
int left = skipWS(expression, p);
118117
if (left < expression.length()) {
119118
int right = skipUntil(expression, left, "=");
120119
String name = trimmedStr(expression, left, right);
121120
left = right + 1;
122121
right = skipUntil(expression, left, ",");
123122
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);
126125
}
127126
}
128127

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) {
133130
start++;
134131
}
135-
while (str.charAt(end - 1) <= 0x20)
136-
{
132+
while (str.charAt(end - 1) <= 0x20) {
137133
end--;
138134
}
139135
return start >= end ? "" : str.substring(start, end);
140136
}
137+
141138
}

src/main/java/org/apache/ibatis/builder/SqlSourceBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ private ParameterMapping buildParameterMapping(String content) {
121121

122122
private Map<String, String> parseParameterMapping(String content) {
123123
try {
124-
return ParameterExpressionParser.parse(content);
124+
return new ParameterExpression(content);
125125
} catch (BuilderException ex) {
126126
throw ex;
127127
} catch (Exception ex) {

src/test/java/org/apache/ibatis/builder/ParameterExpressionParserTest.java renamed to src/test/java/org/apache/ibatis/builder/ParameterExpressionTest.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012 MyBatis.org.
2+
* Copyright 2012-2013 MyBatis.org.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,61 +16,61 @@
1616
package org.apache.ibatis.builder;
1717

1818
import java.util.Map;
19-
import junit.framework.Assert;
19+
import org.junit.Assert;
2020
import org.junit.Test;
2121

22-
public class ParameterExpressionParserTest {
22+
public class ParameterExpressionTest {
2323

2424
@Test
2525
public void simpleProperty() {
26-
Map<String, String> result = ParameterExpressionParser.parse("id");
26+
Map<String, String> result = new ParameterExpression("id");
2727
Assert.assertEquals(1, result.size());
2828
Assert.assertEquals("id", result.get("property"));
2929
}
3030

3131
@Test
3232
public void simplePropertyWithOldStyleJdbcType() {
33-
Map<String, String> result = ParameterExpressionParser.parse("id:VARCHAR");
33+
Map<String, String> result = new ParameterExpression("id:VARCHAR");
3434
Assert.assertEquals(2, result.size());
3535
Assert.assertEquals("id", result.get("property"));
3636
Assert.assertEquals("VARCHAR", result.get("jdbcType"));
3737
}
3838

3939
@Test
4040
public void oldStyleJdbcTypeWithExtraWhitespaces() {
41-
Map<String, String> result = ParameterExpressionParser.parse(" id : VARCHAR ");
41+
Map<String, String> result = new ParameterExpression(" id : VARCHAR ");
4242
Assert.assertEquals(2, result.size());
4343
Assert.assertEquals("id", result.get("property"));
4444
Assert.assertEquals("VARCHAR", result.get("jdbcType"));
4545
}
4646

4747
@Test
4848
public void expressionWithOldStyleJdbcType() {
49-
Map<String, String> result = ParameterExpressionParser.parse("(id.toString()):VARCHAR");
49+
Map<String, String> result = new ParameterExpression("(id.toString()):VARCHAR");
5050
Assert.assertEquals(2, result.size());
5151
Assert.assertEquals("id.toString()", result.get("expression"));
5252
Assert.assertEquals("VARCHAR", result.get("jdbcType"));
5353
}
5454

5555
@Test
5656
public void simplePropertyWithOneAttribute() {
57-
Map<String, String> result = ParameterExpressionParser.parse("id,name=value");
57+
Map<String, String> result = new ParameterExpression("id,name=value");
5858
Assert.assertEquals(2, result.size());
5959
Assert.assertEquals("id", result.get("property"));
6060
Assert.assertEquals("value", result.get("name"));
6161
}
6262

6363
@Test
6464
public void expressionWithOneAttribute() {
65-
Map<String, String> result = ParameterExpressionParser.parse("(id.toString()),name=value");
65+
Map<String, String> result = new ParameterExpression("(id.toString()),name=value");
6666
Assert.assertEquals(2, result.size());
6767
Assert.assertEquals("id.toString()", result.get("expression"));
6868
Assert.assertEquals("value", result.get("name"));
6969
}
7070

7171
@Test
7272
public void simplePropertyWithManyAttributes() {
73-
Map<String, String> result = ParameterExpressionParser.parse("id, attr1=val1, attr2=val2, attr3=val3");
73+
Map<String, String> result = new ParameterExpression("id, attr1=val1, attr2=val2, attr3=val3");
7474
Assert.assertEquals(4, result.size());
7575
Assert.assertEquals("id", result.get("property"));
7676
Assert.assertEquals("val1", result.get("attr1"));
@@ -80,7 +80,7 @@ public void simplePropertyWithManyAttributes() {
8080

8181
@Test
8282
public void expressionWithManyAttributes() {
83-
Map<String, String> result = ParameterExpressionParser.parse("(id.toString()), attr1=val1, attr2=val2, attr3=val3");
83+
Map<String, String> result = new ParameterExpression("(id.toString()), attr1=val1, attr2=val2, attr3=val3");
8484
Assert.assertEquals(4, result.size());
8585
Assert.assertEquals("id.toString()", result.get("expression"));
8686
Assert.assertEquals("val1", result.get("attr1"));
@@ -90,7 +90,7 @@ public void expressionWithManyAttributes() {
9090

9191
@Test
9292
public void simplePropertyWithOldStyleJdbcTypeAndAttributes() {
93-
Map<String, String> result = ParameterExpressionParser.parse("id:VARCHAR, attr1=val1, attr2=val2");
93+
Map<String, String> result = new ParameterExpression("id:VARCHAR, attr1=val1, attr2=val2");
9494
Assert.assertEquals(4, result.size());
9595
Assert.assertEquals("id", result.get("property"));
9696
Assert.assertEquals("VARCHAR", result.get("jdbcType"));
@@ -100,7 +100,7 @@ public void simplePropertyWithOldStyleJdbcTypeAndAttributes() {
100100

101101
@Test
102102
public void simplePropertyWithSpaceAndManyAttributes() {
103-
Map<String, String> result = ParameterExpressionParser.parse("user name, attr1=val1, attr2=val2, attr3=val3");
103+
Map<String, String> result = new ParameterExpression("user name, attr1=val1, attr2=val2, attr3=val3");
104104
Assert.assertEquals(4, result.size());
105105
Assert.assertEquals("user name", result.get("property"));
106106
Assert.assertEquals("val1", result.get("attr1"));
@@ -110,7 +110,7 @@ public void simplePropertyWithSpaceAndManyAttributes() {
110110

111111
@Test
112112
public void shouldIgnoreLeadingAndTrailingSpaces() {
113-
Map<String, String> result = ParameterExpressionParser.parse(" id , jdbcType = VARCHAR, attr1 = val1 , attr2 = val2 ");
113+
Map<String, String> result = new ParameterExpression(" id , jdbcType = VARCHAR, attr1 = val1 , attr2 = val2 ");
114114
Assert.assertEquals(4, result.size());
115115
Assert.assertEquals("id", result.get("property"));
116116
Assert.assertEquals("VARCHAR", result.get("jdbcType"));

src/test/java/org/apache/ibatis/submitted/language/VelocitySqlSourceBuilder.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012 MyBatis.org.
2+
* Copyright 2012-2013 MyBatis.org.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,7 +21,7 @@
2121

2222
import org.apache.ibatis.builder.BaseBuilder;
2323
import org.apache.ibatis.builder.BuilderException;
24-
import org.apache.ibatis.builder.ParameterExpressionParser;
24+
import org.apache.ibatis.builder.ParameterExpression;
2525
import org.apache.ibatis.builder.StaticSqlSource;
2626
import org.apache.ibatis.mapping.ParameterMapping;
2727
import org.apache.ibatis.mapping.SqlSource;
@@ -127,13 +127,13 @@ private ParameterMapping buildParameterMapping(String content) {
127127

128128
private Map<String, String> parseParameterMapping(String content) {
129129
try {
130-
return ParameterExpressionParser.parse(content);
130+
return new ParameterExpression(content);
131131
} catch (BuilderException ex) {
132132
throw ex;
133133
} catch (Exception ex) {
134134
throw new BuilderException("Parsing error was found in mapping @{" + content + "}. Check syntax #{property|(expression), var1=value1, var2=value2, ...} ", ex);
135135
}
136136
}
137-
138137
}
138+
139139
}

0 commit comments

Comments
 (0)