1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16- package org .metafacture .metamorph ;
1716
18- import java .util .HashMap ;
19- import java .util .Map ;
17+ package org .metafacture .metamorph ;
2018
2119import org .metafacture .commons .StringUtil ;
2220import org .metafacture .commons .types .ScopedHashMap ;
2321import org .metafacture .metamorph .api .MorphBuildException ;
2422import org .metafacture .metamorph .xml .DomLoader ;
23+
2524import org .w3c .dom .Document ;
2625import org .w3c .dom .Element ;
2726import org .w3c .dom .NamedNodeMap ;
2827import org .w3c .dom .Node ;
2928import org .xml .sax .InputSource ;
3029
30+ import java .util .HashMap ;
31+ import java .util .Map ;
3132
3233/**
3334 * Builds a {@link Metamorph} from an xml description
@@ -99,15 +100,56 @@ protected final MapFactory getMapFactory() {
99100 return mapFactory ;
100101 }
101102
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 );
104105 walk (morphScript );
105106 }
106107
107108 public final void walk (final InputSource morphScript ) {
108109 walk (DomLoader .parse (SCHEMA_FILE , morphScript ));
109110 }
110111
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+
111153 private static Tags tagOf (final Node child ) {
112154 return Tags .valueOf (child .getLocalName ().toUpperCase ());
113155 }
@@ -131,7 +173,7 @@ protected static Map<String, String> attributeMap(final Node elementNode) {
131173 return attributes ;
132174 }
133175
134- protected final String resolveVars (final String string ){
176+ protected final String resolveVars (final String string ) {
135177 return StringUtil .format (string , Metamorph .VAR_START , Metamorph .VAR_END , ignoreMissingVars , vars );
136178 }
137179
@@ -141,7 +183,7 @@ protected final void setIgnoreMissingVars(final boolean ignoreMissingVars) {
141183
142184 protected final String resolvedAttribute (final Node node , final AttributeName attr ) {
143185 final String value = attribute (node , attr );
144- if (null == value ){
186+ if (null == value ) {
145187 return null ;
146188 }
147189 return resolveVars (value );
@@ -159,48 +201,6 @@ protected final Map<String, String> resolvedAttributeMap(final Node node) {
159201 return attributes ;
160202 }
161203
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-
204204 private void handleMeta (final Node node ) {
205205 for (Node metaEntryNode = node .getFirstChild (); metaEntryNode != null ; metaEntryNode = metaEntryNode
206206 .getNextSibling ()) {
@@ -231,39 +231,44 @@ private void handleRule(final Node node) {
231231 enterIf (child );
232232 handleRule (child .getFirstChild ());
233233 exitIf (child );
234- } else if (POSTPROCESS .equals (child .getLocalName ())) {
234+ }
235+ else if (POSTPROCESS .equals (child .getLocalName ())) {
235236 handlePostprocess (child );
236- } else if (ENTITY_NAME .equals (child .getLocalName ())) {
237+ }
238+ else if (ENTITY_NAME .equals (child .getLocalName ())) {
237239 enterName (child );
238240 handleRule (child .getFirstChild ());
239241 exitName (child );
240- } else {
242+ }
243+ else {
241244 handleRule (child );
242245 }
243246 }
244247 exitCollect (node );
245- } else if (DATA .equals (nodeName )) {
248+ }
249+ else if (DATA .equals (nodeName )) {
246250 enterData (node );
247251 handlePostprocess (node );
248252 exitData (node );
249- } else if (CALL_MACRO .equals (nodeName )){
253+ }
254+ else if (CALL_MACRO .equals (nodeName )) {
250255 final String macroName = attribute (node , AttributeName .NAME );
251256 final Node macroNode = macros .get (macroName );
252- if (macroNode == null ){
257+ if (macroNode == null ) {
253258 throw new MorphBuildException ("Macro '" + macroName + "' undefined!" );
254259 }
255260 vars = new ScopedHashMap <String , String >(vars );
256261 vars .putAll (resolvedAttributeMap (node ));
257262 handleRules (macroNode );
258263 vars = vars .getOuterScope ();
259- }else {
264+ }
265+ else {
260266 illegalChild (node );
261267 }
262268 }
263269
264270 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 ()) {
267272 handleFunction (functionNode );
268273 }
269274 }
@@ -272,7 +277,8 @@ private void handleMaps(final Node node) {
272277 for (Node mapNode = node .getFirstChild (); mapNode != null ; mapNode = mapNode .getNextSibling ()) {
273278 if (MAP .equals (mapNode .getLocalName ())) {
274279 handleInternalMap (mapNode );
275- } else {
280+ }
281+ else {
276282 handleMapClass (mapNode );
277283 }
278284 }
@@ -296,29 +302,27 @@ private void handleMacros(final Node node) {
296302
297303 private void checkVersionCompatibility (final int version ) {
298304 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 );
301306 }
302307 }
303308
304309 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 ());
307311 }
308312
309313 protected abstract void init ();
310314
311315 protected abstract void finish ();
312316
313- protected abstract void setEntityMarker (final String entityMarker );
317+ protected abstract void setEntityMarker (String entityMarker );
314318
315- protected abstract void handleInternalMap (final Node mapNode );
319+ protected abstract void handleInternalMap (Node mapNode );
316320
317- protected abstract void handleMapClass (final Node mapNode );
321+ protected abstract void handleMapClass (Node mapNode );
318322
319- protected abstract void handleMetaEntry (final String name , final String value );
323+ protected abstract void handleMetaEntry (String name , String value );
320324
321- protected abstract void handleFunctionDefinition (final Node functionDefNode );
325+ protected abstract void handleFunctionDefinition (Node functionDefNode );
322326
323327 protected abstract void enterData (Node node );
324328
0 commit comments