Skip to content

Commit 3ac1bec

Browse files
committed
Enhanced XPathParser to include new constructor options and type specific evaluators.
1 parent 8d640f1 commit 3ac1bec

File tree

5 files changed

+94
-18
lines changed

5 files changed

+94
-18
lines changed

src/main/java/org/apache/ibatis/builder/xml/XMLConfigBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public XMLConfigBuilder(Reader reader, String environment, Properties props) {
4444
this.configuration.setVariables(props);
4545
this.parsed = false;
4646
this.environment = environment;
47-
this.parser = new XPathParser(reader, true, new XMLMapperEntityResolver(), props);
47+
this.parser = new XPathParser(reader, true, props, new XMLMapperEntityResolver());
4848
}
4949

5050
public Configuration parse() {

src/main/java/org/apache/ibatis/builder/xml/XMLMapperBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public XMLMapperBuilder(Reader reader, Configuration configuration, String resou
2828
public XMLMapperBuilder(Reader reader, Configuration configuration, String resource, Map<String, XNode> sqlFragments) {
2929
super(configuration);
3030
this.builderAssistant = new MapperBuilderAssistant(configuration, resource);
31-
this.parser = new XPathParser(reader, true, new XMLMapperEntityResolver(), configuration.getVariables());
31+
this.parser = new XPathParser(reader, true, configuration.getVariables(), new XMLMapperEntityResolver());
3232
this.sqlFragments = sqlFragments;
3333
this.resource = resource;
3434
}

src/main/java/org/apache/ibatis/parsing/XPathParser.java

Lines changed: 86 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import javax.xml.xpath.XPathConstants;
1414
import javax.xml.xpath.XPathFactory;
1515
import java.io.Reader;
16+
import java.io.StringReader;
1617
import java.util.ArrayList;
1718
import java.util.List;
1819
import java.util.Properties;
@@ -25,22 +26,52 @@ public class XPathParser {
2526
private Properties variables;
2627
private XPath xpath;
2728

28-
public XPathParser(Reader reader, boolean validation, EntityResolver entityResolver, Properties variables) {
29-
this.validation = validation;
30-
this.entityResolver = entityResolver;
31-
this.variables = variables;
32-
this.document = createDocument(reader);
33-
XPathFactory factory = XPathFactory.newInstance();
34-
this.xpath = factory.newXPath();
29+
public XPathParser(String xml) {
30+
commonConstructor(createDocument(new StringReader(xml)), false, null, null);
3531
}
3632

37-
public XPathParser(Document document, boolean validation, EntityResolver entityResolver, Properties variables) {
38-
this.validation = validation;
39-
this.entityResolver = entityResolver;
40-
this.variables = variables;
41-
this.document = document;
42-
XPathFactory factory = XPathFactory.newInstance();
43-
this.xpath = factory.newXPath();
33+
public XPathParser(Reader reader) {
34+
commonConstructor(createDocument(reader), false, null, null);
35+
}
36+
37+
public XPathParser(Document document) {
38+
commonConstructor(document, false, null, null);
39+
}
40+
41+
public XPathParser(String xml, boolean validation) {
42+
commonConstructor(createDocument(new StringReader(xml)), validation, null, null);
43+
}
44+
45+
public XPathParser(Reader reader, boolean validation) {
46+
commonConstructor(createDocument(reader), validation, null, null);
47+
}
48+
49+
public XPathParser(Document document, boolean validation) {
50+
commonConstructor(document, validation, null, null);
51+
}
52+
53+
public XPathParser(String xml, boolean validation, Properties variables) {
54+
commonConstructor(createDocument(new StringReader(xml)), validation, variables, null);
55+
}
56+
57+
public XPathParser(Reader reader, boolean validation, Properties variables) {
58+
commonConstructor(createDocument(reader), validation, variables, null);
59+
}
60+
61+
public XPathParser(Document document, boolean validation, Properties variables) {
62+
commonConstructor(document, validation, variables, null);
63+
}
64+
65+
public XPathParser(String xml, boolean validation, Properties variables, EntityResolver entityResolver) {
66+
commonConstructor(createDocument(new StringReader(xml)), validation, variables, entityResolver);
67+
}
68+
69+
public XPathParser(Reader reader, boolean validation, Properties variables, EntityResolver entityResolver) {
70+
commonConstructor(createDocument(reader), validation, variables, entityResolver);
71+
}
72+
73+
public XPathParser(Document document, boolean validation, Properties variables, EntityResolver entityResolver) {
74+
commonConstructor(document, validation, variables, entityResolver);
4475
}
4576

4677
public void setVariables(Properties variables) {
@@ -65,6 +96,38 @@ public Boolean evalBoolean(Object root, String expression) {
6596
return (Boolean) evaluate(expression, root, XPathConstants.BOOLEAN);
6697
}
6798

99+
public Short evalShort(String expression) {
100+
return evalShort(document, expression);
101+
}
102+
103+
public Short evalShort(Object root, String expression) {
104+
return Short.valueOf(evalString(root, expression));
105+
}
106+
107+
public Integer evalInteger(String expression) {
108+
return evalInteger(document, expression);
109+
}
110+
111+
public Integer evalInteger(Object root, String expression) {
112+
return Integer.valueOf(evalString(root, expression));
113+
}
114+
115+
public Long evalLong(String expression) {
116+
return evalLong(document, expression);
117+
}
118+
119+
public Long evalLong(Object root, String expression) {
120+
return Long.valueOf(evalString(root, expression));
121+
}
122+
123+
public Float evalFloat(String expression) {
124+
return evalFloat(document, expression);
125+
}
126+
127+
public Float evalFloat(Object root, String expression) {
128+
return Float.valueOf(evalString(root, expression));
129+
}
130+
68131
public Double evalDouble(String expression) {
69132
return evalDouble(document, expression);
70133
}
@@ -137,4 +200,13 @@ public void warning(SAXParseException exception) throws SAXException {
137200
}
138201
}
139202

203+
private void commonConstructor(Document document, boolean validation, Properties variables, EntityResolver entityResolver) {
204+
this.validation = validation;
205+
this.entityResolver = entityResolver;
206+
this.variables = variables;
207+
this.document = document;
208+
XPathFactory factory = XPathFactory.newInstance();
209+
this.xpath = factory.newXPath();
210+
}
211+
140212
}

src/test/java/com/ibatis/common/util/NodeEventParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void addNodeletHandler(Object handler) {
5151
public void parse(Reader reader) throws ParsingException {
5252
try {
5353
Document doc = createDocument(reader);
54-
xpathParser = new XPathParser(doc,validation,entityResolver,variables);
54+
xpathParser = new XPathParser(doc,validation, variables, entityResolver);
5555
parse(doc.getLastChild());
5656
} catch (Exception e) {
5757
throw new ParsingException("Error parsing XML. Cause: " + e, e);

src/test/java/org/apache/ibatis/parsing/XPathParserTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ public void shouldTestXPathParserMethods() throws Exception {
1313
String resource = "resources/nodelet_test.xml";
1414
Reader reader = Resources.getResourceAsReader(resource);
1515
XPathParser parser = new XPathParser(reader, false, null, null);
16-
assertEquals((Double) 5.8d, parser.evalDouble("/employee/height"));
16+
assertEquals((long)1970, parser.evalLong("/employee/birth_date/year"));
17+
assertEquals((short)6, parser.evalShort("/employee/birth_date/month"));
18+
assertEquals(15, parser.evalInteger("/employee/birth_date/day"));
19+
assertEquals(5.8f, parser.evalFloat("/employee/height"));
20+
assertEquals(5.8d, parser.evalDouble("/employee/height"));
1721
assertEquals("${id_var}", parser.evalString("/employee/@id"));
1822
assertEquals(Boolean.TRUE, parser.evalBoolean("/employee/active"));
1923
assertEquals("<id>${id_var}</id>", parser.evalNode("/employee/@id").toString().trim());

0 commit comments

Comments
 (0)