15
15
*/
16
16
package org .apache .ibatis .builder .xml ;
17
17
18
+ import org .apache .ibatis .builder .BuilderException ;
18
19
import org .apache .ibatis .builder .IncompleteElementException ;
19
20
import org .apache .ibatis .builder .MapperBuilderAssistant ;
20
- import org .apache .ibatis .parsing .GenericTokenParser ;
21
21
import org .apache .ibatis .parsing .PropertyParser ;
22
- import org .apache .ibatis .parsing .TokenHandler ;
23
22
import org .apache .ibatis .parsing .XNode ;
24
23
import org .apache .ibatis .session .Configuration ;
25
24
import org .w3c .dom .Node ;
26
25
import org .w3c .dom .NodeList ;
27
26
28
- import java .util .*;
27
+ import java .util .ArrayList ;
28
+ import java .util .Collections ;
29
+ import java .util .List ;
30
+ import java .util .Properties ;
29
31
30
32
/**
31
33
* @author Frank D. Martinez [mnesarco]
@@ -63,15 +65,12 @@ private void applyIncludes(Node source, final Properties variablesContext) {
63
65
// replace variables in include refid value
64
66
refid = PropertyParser .parse (refid , variablesContext );
65
67
Node toInclude = findSqlFragment (refid );
66
- Properties newVariablesContext = getVariablesContext (source );
68
+ Properties newVariablesContext = getVariablesContext (source , variablesContext );
67
69
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 );
75
74
} else {
76
75
// no new context - use inherited fully
77
76
fullContext = variablesContext ;
@@ -113,25 +112,13 @@ private String getStringAttribute(Node node, String name) {
113
112
return node .getAttributes ().getNamedItem (name ).getNodeValue ();
114
113
}
115
114
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
-
129
115
/**
130
116
* Read placholders and their values from include node definition.
131
117
* @param node Include node instance
118
+ * @param inheritedVariablesContext Current context used for replace variables in new variables values
132
119
* @return variables context from include instance (no inherited values)
133
120
*/
134
- private Properties getVariablesContext (Node node ) {
121
+ private Properties getVariablesContext (Node node , Properties inheritedVariablesContext ) {
135
122
List <Node > subElements = getSubElements (node );
136
123
if (subElements .isEmpty ()) {
137
124
return new Properties ();
@@ -140,10 +127,12 @@ private Properties getVariablesContext(Node node) {
140
127
for (Node variableValue : subElements ) {
141
128
String name = getStringAttribute (variableValue , "name" );
142
129
String value = getStringAttribute (variableValue , "value" );
130
+ // Replace variables inside
131
+ value = PropertyParser .parse (value , inheritedVariablesContext );
143
132
// Push new value
144
133
Object originalValue = variablesContext .put (name , value );
145
134
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" );
147
136
}
148
137
}
149
138
return variablesContext ;
0 commit comments