Skip to content

Commit aca6951

Browse files
committed
Reflecting feedback on pull request - simplifications, imports fix, concrete exception used
1 parent fd15446 commit aca6951

File tree

1 file changed

+15
-26
lines changed

1 file changed

+15
-26
lines changed

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

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,19 @@
1515
*/
1616
package org.apache.ibatis.builder.xml;
1717

18+
import org.apache.ibatis.builder.BuilderException;
1819
import org.apache.ibatis.builder.IncompleteElementException;
1920
import org.apache.ibatis.builder.MapperBuilderAssistant;
20-
import org.apache.ibatis.parsing.GenericTokenParser;
2121
import org.apache.ibatis.parsing.PropertyParser;
22-
import org.apache.ibatis.parsing.TokenHandler;
2322
import org.apache.ibatis.parsing.XNode;
2423
import org.apache.ibatis.session.Configuration;
2524
import org.w3c.dom.Node;
2625
import org.w3c.dom.NodeList;
2726

28-
import java.util.*;
27+
import java.util.ArrayList;
28+
import java.util.Collections;
29+
import java.util.List;
30+
import java.util.Properties;
2931

3032
/**
3133
* @author Frank D. Martinez [mnesarco]
@@ -63,15 +65,12 @@ private void applyIncludes(Node source, final Properties variablesContext) {
6365
// replace variables in include refid value
6466
refid = PropertyParser.parse(refid, variablesContext);
6567
Node toInclude = findSqlFragment(refid);
66-
Properties newVariablesContext = getVariablesContext(source);
68+
Properties newVariablesContext = getVariablesContext(source, variablesContext);
6769
if (!newVariablesContext.isEmpty()) {
68-
// replace variables in variable values too
69-
for (Object name : newVariablesContext.keySet()) {
70-
newVariablesContext.put(name, PropertyParser.parse(newVariablesContext.get(name).toString(), variablesContext));
71-
}
72-
// merge new and inherited into new full one
73-
applyInheritedContext(newVariablesContext, variablesContext);
74-
fullContext = newVariablesContext;
70+
// merge contexts
71+
fullContext = new Properties();
72+
fullContext.putAll(variablesContext);
73+
fullContext.putAll(newVariablesContext);
7574
} else {
7675
// no new context - use inherited fully
7776
fullContext = variablesContext;
@@ -113,25 +112,13 @@ private String getStringAttribute(Node node, String name) {
113112
return node.getAttributes().getNamedItem(name).getNodeValue();
114113
}
115114

116-
/**
117-
* Add inherited context into newly created one.
118-
* @param newContext variables defined current include clause where inherited values will be placed
119-
* @param inheritedContext all inherited variables values
120-
*/
121-
private void applyInheritedContext(Properties newContext, Properties inheritedContext) {
122-
for (Map.Entry<Object, Object> e : inheritedContext.entrySet()) {
123-
if (!newContext.containsKey(e.getKey())) {
124-
newContext.put(e.getKey(), e.getValue());
125-
}
126-
}
127-
}
128-
129115
/**
130116
* Read placholders and their values from include node definition.
131117
* @param node Include node instance
118+
* @param inheritedVariablesContext Current context used for replace variables in new variables values
132119
* @return variables context from include instance (no inherited values)
133120
*/
134-
private Properties getVariablesContext(Node node) {
121+
private Properties getVariablesContext(Node node, Properties inheritedVariablesContext) {
135122
List<Node> subElements = getSubElements(node);
136123
if (subElements.isEmpty()) {
137124
return new Properties();
@@ -140,10 +127,12 @@ private Properties getVariablesContext(Node node) {
140127
for (Node variableValue : subElements) {
141128
String name = getStringAttribute(variableValue, "name");
142129
String value = getStringAttribute(variableValue, "value");
130+
// Replace variables inside
131+
value = PropertyParser.parse(value, inheritedVariablesContext);
143132
// Push new value
144133
Object originalValue = variablesContext.put(name, value);
145134
if (originalValue != null) {
146-
throw new IllegalArgumentException("Variable " + name + " defined twice in the same include definition");
135+
throw new BuilderException("Variable " + name + " defined twice in the same include definition");
147136
}
148137
}
149138
return variablesContext;

0 commit comments

Comments
 (0)