13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
16
- package org .metafacture .metamorph ;
17
16
18
- import java .util .HashMap ;
19
- import java .util .Map ;
17
+ package org .metafacture .metamorph ;
20
18
21
19
import org .metafacture .commons .StringUtil ;
22
20
import org .metafacture .commons .types .ScopedHashMap ;
23
21
import org .metafacture .metamorph .api .MorphBuildException ;
24
22
import org .metafacture .metamorph .xml .DomLoader ;
23
+
25
24
import org .w3c .dom .Document ;
26
25
import org .w3c .dom .Element ;
27
26
import org .w3c .dom .NamedNodeMap ;
28
27
import org .w3c .dom .Node ;
29
28
import org .xml .sax .InputSource ;
30
29
30
+ import java .util .HashMap ;
31
+ import java .util .Map ;
31
32
32
33
/**
33
34
* Builds a {@link Metamorph} from an xml description
@@ -99,15 +100,56 @@ protected final MapFactory getMapFactory() {
99
100
return mapFactory ;
100
101
}
101
102
102
- public final void walk (final InputSource morphScript , final Map <String , String > vars ) {
103
- this . vars .putAll (vars );
103
+ public final void walk (final InputSource morphScript , final Map <String , String > newVars ) {
104
+ vars .putAll (newVars );
104
105
walk (morphScript );
105
106
}
106
107
107
108
public final void walk (final InputSource morphScript ) {
108
109
walk (DomLoader .parse (SCHEMA_FILE , morphScript ));
109
110
}
110
111
112
+ protected final void walk (final Document doc ) {
113
+ functionFactory = new FunctionFactory ();
114
+ collectFactory = new CollectFactory ();
115
+ collectFactory .registerClass (ENTITY , Entity .class );
116
+ mapFactory = new MapFactory ();
117
+
118
+ init ();
119
+
120
+ final Element root = doc .getDocumentElement ();
121
+ final int version = Integer .parseInt (attribute (root , AttributeName .VERSION ));
122
+ checkVersionCompatibility (version );
123
+
124
+ setEntityMarker (attribute (root , AttributeName .ENTITY_MARKER ));
125
+
126
+ for (Node node = root .getFirstChild (); node != null ; node = node .getNextSibling ()) {
127
+ switch (tagOf (node )) {
128
+ case META :
129
+ handleMeta (node );
130
+ break ;
131
+ case FUNCTIONS :
132
+ handleFunctionDefinitions (node );
133
+ break ;
134
+ case RULES :
135
+ handleRules (node );
136
+ break ;
137
+ case MAPS :
138
+ handleMaps (node );
139
+ break ;
140
+ case VARS :
141
+ handleVars (node );
142
+ break ;
143
+ case MACROS :
144
+ handleMacros (node );
145
+ break ;
146
+ default :
147
+ illegalChild (node );
148
+ }
149
+ }
150
+ finish ();
151
+ }
152
+
111
153
private static Tags tagOf (final Node child ) {
112
154
return Tags .valueOf (child .getLocalName ().toUpperCase ());
113
155
}
@@ -131,7 +173,7 @@ protected static Map<String, String> attributeMap(final Node elementNode) {
131
173
return attributes ;
132
174
}
133
175
134
- protected final String resolveVars (final String string ){
176
+ protected final String resolveVars (final String string ) {
135
177
return StringUtil .format (string , Metamorph .VAR_START , Metamorph .VAR_END , ignoreMissingVars , vars );
136
178
}
137
179
@@ -141,7 +183,7 @@ protected final void setIgnoreMissingVars(final boolean ignoreMissingVars) {
141
183
142
184
protected final String resolvedAttribute (final Node node , final AttributeName attr ) {
143
185
final String value = attribute (node , attr );
144
- if (null == value ){
186
+ if (null == value ) {
145
187
return null ;
146
188
}
147
189
return resolveVars (value );
@@ -159,48 +201,6 @@ protected final Map<String, String> resolvedAttributeMap(final Node node) {
159
201
return attributes ;
160
202
}
161
203
162
- protected final void walk (final Document doc ) {
163
- functionFactory = new FunctionFactory ();
164
- collectFactory = new CollectFactory ();
165
- collectFactory .registerClass (ENTITY , Entity .class );
166
- mapFactory = new MapFactory ();
167
-
168
- init ();
169
-
170
- final Element root = doc .getDocumentElement ();
171
- final int version = Integer .parseInt (attribute (root , AttributeName .VERSION ));
172
- checkVersionCompatibility (version );
173
-
174
- setEntityMarker (attribute (root , AttributeName .ENTITY_MARKER ));
175
-
176
- for (Node node = root .getFirstChild (); node != null ; node = node .getNextSibling ()) {
177
-
178
- switch (tagOf (node )) {
179
- case META :
180
- handleMeta (node );
181
- break ;
182
- case FUNCTIONS :
183
- handleFunctionDefinitions (node );
184
- break ;
185
- case RULES :
186
- handleRules (node );
187
- break ;
188
- case MAPS :
189
- handleMaps (node );
190
- break ;
191
- case VARS :
192
- handleVars (node );
193
- break ;
194
- case MACROS :
195
- handleMacros (node );
196
- break ;
197
- default :
198
- illegalChild (node );
199
- }
200
- }
201
- finish ();
202
- }
203
-
204
204
private void handleMeta (final Node node ) {
205
205
for (Node metaEntryNode = node .getFirstChild (); metaEntryNode != null ; metaEntryNode = metaEntryNode
206
206
.getNextSibling ()) {
@@ -231,39 +231,44 @@ private void handleRule(final Node node) {
231
231
enterIf (child );
232
232
handleRule (child .getFirstChild ());
233
233
exitIf (child );
234
- } else if (POSTPROCESS .equals (child .getLocalName ())) {
234
+ }
235
+ else if (POSTPROCESS .equals (child .getLocalName ())) {
235
236
handlePostprocess (child );
236
- } else if (ENTITY_NAME .equals (child .getLocalName ())) {
237
+ }
238
+ else if (ENTITY_NAME .equals (child .getLocalName ())) {
237
239
enterName (child );
238
240
handleRule (child .getFirstChild ());
239
241
exitName (child );
240
- } else {
242
+ }
243
+ else {
241
244
handleRule (child );
242
245
}
243
246
}
244
247
exitCollect (node );
245
- } else if (DATA .equals (nodeName )) {
248
+ }
249
+ else if (DATA .equals (nodeName )) {
246
250
enterData (node );
247
251
handlePostprocess (node );
248
252
exitData (node );
249
- } else if (CALL_MACRO .equals (nodeName )){
253
+ }
254
+ else if (CALL_MACRO .equals (nodeName )) {
250
255
final String macroName = attribute (node , AttributeName .NAME );
251
256
final Node macroNode = macros .get (macroName );
252
- if (macroNode == null ){
257
+ if (macroNode == null ) {
253
258
throw new MorphBuildException ("Macro '" + macroName + "' undefined!" );
254
259
}
255
260
vars = new ScopedHashMap <String , String >(vars );
256
261
vars .putAll (resolvedAttributeMap (node ));
257
262
handleRules (macroNode );
258
263
vars = vars .getOuterScope ();
259
- }else {
264
+ }
265
+ else {
260
266
illegalChild (node );
261
267
}
262
268
}
263
269
264
270
private void handlePostprocess (final Node node ) {
265
- for (Node functionNode = node .getFirstChild (); functionNode != null ; functionNode = functionNode
266
- .getNextSibling ()) {
271
+ for (Node functionNode = node .getFirstChild (); functionNode != null ; functionNode = functionNode .getNextSibling ()) {
267
272
handleFunction (functionNode );
268
273
}
269
274
}
@@ -272,7 +277,8 @@ private void handleMaps(final Node node) {
272
277
for (Node mapNode = node .getFirstChild (); mapNode != null ; mapNode = mapNode .getNextSibling ()) {
273
278
if (MAP .equals (mapNode .getLocalName ())) {
274
279
handleInternalMap (mapNode );
275
- } else {
280
+ }
281
+ else {
276
282
handleMapClass (mapNode );
277
283
}
278
284
}
@@ -296,29 +302,27 @@ private void handleMacros(final Node node) {
296
302
297
303
private void checkVersionCompatibility (final int version ) {
298
304
if (version < LOWEST_COMPATIBLE_VERSION || version > CURRENT_VERSION ) {
299
- throw new MorphBuildException ("Version " + version
300
- + " of definition file not supported by metamorph version " + CURRENT_VERSION );
305
+ throw new MorphBuildException ("Version " + version + " of definition file not supported by metamorph version " + CURRENT_VERSION );
301
306
}
302
307
}
303
308
304
309
protected final void illegalChild (final Node child ) {
305
- throw new MorphBuildException ("Schema mismatch: illegal tag " + child .getLocalName () + " in node "
306
- + child .getParentNode ().getLocalName ());
310
+ throw new MorphBuildException ("Schema mismatch: illegal tag " + child .getLocalName () + " in node " + child .getParentNode ().getLocalName ());
307
311
}
308
312
309
313
protected abstract void init ();
310
314
311
315
protected abstract void finish ();
312
316
313
- protected abstract void setEntityMarker (final String entityMarker );
317
+ protected abstract void setEntityMarker (String entityMarker );
314
318
315
- protected abstract void handleInternalMap (final Node mapNode );
319
+ protected abstract void handleInternalMap (Node mapNode );
316
320
317
- protected abstract void handleMapClass (final Node mapNode );
321
+ protected abstract void handleMapClass (Node mapNode );
318
322
319
- protected abstract void handleMetaEntry (final String name , final String value );
323
+ protected abstract void handleMetaEntry (String name , String value );
320
324
321
- protected abstract void handleFunctionDefinition (final Node functionDefNode );
325
+ protected abstract void handleFunctionDefinition (Node functionDefNode );
322
326
323
327
protected abstract void enterData (Node node );
324
328
0 commit comments