15
15
*/
16
16
package org .culturegraph .mf .morph ;
17
17
18
- import java .io .FileNotFoundException ;
19
- import java .io .InputStream ;
20
- import java .io .Reader ;
21
18
import java .util .HashMap ;
22
19
import java .util .Map ;
23
20
26
23
import org .culturegraph .mf .morph .functions .FunctionFactory ;
27
24
import org .culturegraph .mf .morph .maps .MapFactory ;
28
25
import org .culturegraph .mf .types .ScopedHashMap ;
29
- import org .culturegraph .mf .util .ResourceUtil ;
30
26
import org .culturegraph .mf .util .StringUtil ;
27
+ import org .culturegraph .mf .util .xml .DomLoader ;
31
28
import org .w3c .dom .Document ;
32
29
import org .w3c .dom .Element ;
33
30
import org .w3c .dom .NamedNodeMap ;
@@ -103,42 +100,13 @@ protected final MapFactory getMapFactory() {
103
100
return mapFactory ;
104
101
}
105
102
106
- public final void walk (final String morphDef ) {
107
- try {
108
- walk (ResourceUtil .getStream (morphDef ));
109
- } catch (final FileNotFoundException e ) {
110
- throw new MorphDefException (e );
111
- }
112
- }
113
-
114
- public final void walk (final InputStream inputStream ) {
115
- if (inputStream == null ) {
116
- throw new IllegalArgumentException ("'inputStream' must not be null" );
117
- }
118
- walk (DomLoader .parse (SCHEMA_FILE , new InputSource (inputStream )));
119
- }
120
-
121
- public final void walk (final Reader reader ) {
122
- if (reader == null ) {
123
- throw new IllegalArgumentException ("'reader' must not be null" );
124
- }
125
- walk (DomLoader .parse (SCHEMA_FILE , new InputSource (reader )));
126
- }
127
-
128
- public final void walk (final Reader morphDefReader , final Map <String , String > vars ) {
103
+ public final void walk (final InputSource morphScript , final Map <String , String > vars ) {
129
104
this .vars .putAll (vars );
130
- walk (morphDefReader );
105
+ walk (morphScript );
131
106
}
132
107
133
-
134
- public final void walk (final String morphDef , final Map <String , String > vars ) {
135
- this .vars .putAll (vars );
136
- walk (morphDef );
137
- }
138
-
139
- public final void walk (final InputStream inputStream , final Map <String , String > vars ) {
140
- this .vars .putAll (vars );
141
- walk (inputStream );
108
+ public final void walk (final InputSource morphScript ) {
109
+ walk (DomLoader .parse (SCHEMA_FILE , morphScript ));
142
110
}
143
111
144
112
private static Tags tagOf (final Node child ) {
@@ -172,7 +140,7 @@ protected final void setIgnoreMissingVars(final boolean ignoreMissingVars) {
172
140
this .ignoreMissingVars = ignoreMissingVars ;
173
141
}
174
142
175
- protected final String resolvedAttribute (final Node node , final AttributeName attr ){
143
+ protected final String resolvedAttribute (final Node node , final AttributeName attr ) {
176
144
final String value = attribute (node , attr );
177
145
if (null ==value ){
178
146
return null ;
@@ -181,7 +149,7 @@ protected final String resolvedAttribute(final Node node, final AttributeName at
181
149
182
150
}
183
151
184
- protected final Map <String , String > resolvedAttributeMap (final Node node ){
152
+ protected final Map <String , String > resolvedAttributeMap (final Node node ) {
185
153
final Map <String , String > attributes = new HashMap <String , String >();
186
154
final NamedNodeMap attrNode = node .getAttributes ();
187
155
@@ -192,15 +160,6 @@ protected final Map<String, String> resolvedAttributeMap(final Node node){
192
160
return attributes ;
193
161
}
194
162
195
- private void handleVars (final Node varsNode ) {
196
- for (Node varNode = varsNode .getFirstChild (); varNode != null ; varNode = varNode .getNextSibling ()) {
197
- final String varName = attribute (varNode , AttributeName .NAME );
198
- final String varValue = attribute (varNode , AttributeName .VALUE );
199
- vars .put (varName , varValue );
200
- }
201
- vars = new ScopedHashMap <String , String >(vars );
202
- }
203
-
204
163
protected final void walk (final Document doc ) {
205
164
functionFactory = new FunctionFactory ();
206
165
collectFactory = new CollectFactory ();
@@ -242,44 +201,26 @@ protected final void walk(final Document doc) {
242
201
finish ();
243
202
}
244
203
245
- private void handleMacros (final Node node ) {
246
- for (Node macroNode = node .getFirstChild (); macroNode != null ; macroNode = macroNode .getNextSibling ()) {
247
- final String name = attribute (macroNode , AttributeName .NAME );
248
- macros .put (name , macroNode );
204
+ private void handleMeta (final Node node ) {
205
+ for (Node metaEntryNode = node .getFirstChild (); metaEntryNode != null ; metaEntryNode = metaEntryNode
206
+ .getNextSibling ()) {
207
+
208
+ handleMetaEntry (metaEntryNode .getLocalName (), metaEntryNode .getTextContent ());
249
209
}
250
210
}
251
211
252
- protected abstract void init ();
253
-
254
- protected abstract void finish ();
255
-
256
- protected abstract void setEntityMarker (final String entityMarker );
257
-
258
- protected abstract void handleInternalMap (final Node mapNode );
259
-
260
- protected abstract void handleMapClass (final Node mapNode );
261
-
262
- protected abstract void handleMetaEntry (final String name , final String value );
263
-
264
- protected abstract void handleFunctionDefinition (final Node functionDefNode );
265
-
266
- protected abstract void enterData (Node node );
267
-
268
- protected abstract void exitData (Node node );
269
-
270
- protected abstract void enterCollect (Node node );
271
-
272
- protected abstract void exitCollect (Node node );
273
-
274
- protected abstract void enterName (Node node );
275
-
276
- protected abstract void exitName (Node node );
277
-
278
- protected abstract void enterIf (Node node );
279
-
280
- protected abstract void exitIf (Node node );
212
+ private void handleFunctionDefinitions (final Node node ) {
213
+ for (Node functionDefNode = node .getFirstChild (); functionDefNode != null ; functionDefNode = functionDefNode
214
+ .getNextSibling ()) {
215
+ handleFunctionDefinition (functionDefNode );
216
+ }
217
+ }
281
218
282
- protected abstract void handleFunction (Node functionNode );
219
+ private void handleRules (final Node node ) {
220
+ for (Node ruleNode = node .getFirstChild (); ruleNode != null ; ruleNode = ruleNode .getNextSibling ()) {
221
+ handleRule (ruleNode );
222
+ }
223
+ }
283
224
284
225
private void handleRule (final Node node ) {
285
226
final String nodeName = node .getLocalName ();
@@ -337,9 +278,19 @@ private void handleMaps(final Node node) {
337
278
}
338
279
}
339
280
340
- private void handleRules (final Node node ) {
341
- for (Node ruleNode = node .getFirstChild (); ruleNode != null ; ruleNode = ruleNode .getNextSibling ()) {
342
- handleRule (ruleNode );
281
+ private void handleVars (final Node varsNode ) {
282
+ for (Node varNode = varsNode .getFirstChild (); varNode != null ; varNode = varNode .getNextSibling ()) {
283
+ final String varName = attribute (varNode , AttributeName .NAME );
284
+ final String varValue = attribute (varNode , AttributeName .VALUE );
285
+ vars .put (varName , varValue );
286
+ }
287
+ vars = new ScopedHashMap <String , String >(vars );
288
+ }
289
+
290
+ private void handleMacros (final Node node ) {
291
+ for (Node macroNode = node .getFirstChild (); macroNode != null ; macroNode = macroNode .getNextSibling ()) {
292
+ final String name = attribute (macroNode , AttributeName .NAME );
293
+ macros .put (name , macroNode );
343
294
}
344
295
}
345
296
@@ -355,18 +306,36 @@ protected final void illegalChild(final Node child) {
355
306
+ child .getParentNode ().getLocalName ());
356
307
}
357
308
358
- private void handleFunctionDefinitions (final Node node ) {
359
- for (Node functionDefNode = node .getFirstChild (); functionDefNode != null ; functionDefNode = functionDefNode
360
- .getNextSibling ()) {
361
- handleFunctionDefinition (functionDefNode );
362
- }
363
- }
309
+ protected abstract void init ();
364
310
365
- private void handleMeta (final Node node ) {
366
- for (Node metaEntryNode = node .getFirstChild (); metaEntryNode != null ; metaEntryNode = metaEntryNode
367
- .getNextSibling ()) {
311
+ protected abstract void finish ();
312
+
313
+ protected abstract void setEntityMarker (final String entityMarker );
314
+
315
+ protected abstract void handleInternalMap (final Node mapNode );
316
+
317
+ protected abstract void handleMapClass (final Node mapNode );
318
+
319
+ protected abstract void handleMetaEntry (final String name , final String value );
320
+
321
+ protected abstract void handleFunctionDefinition (final Node functionDefNode );
322
+
323
+ protected abstract void enterData (Node node );
324
+
325
+ protected abstract void exitData (Node node );
326
+
327
+ protected abstract void enterCollect (Node node );
328
+
329
+ protected abstract void exitCollect (Node node );
330
+
331
+ protected abstract void enterName (Node node );
332
+
333
+ protected abstract void exitName (Node node );
334
+
335
+ protected abstract void enterIf (Node node );
336
+
337
+ protected abstract void exitIf (Node node );
338
+
339
+ protected abstract void handleFunction (Node functionNode );
368
340
369
- handleMetaEntry (metaEntryNode .getLocalName (), metaEntryNode .getTextContent ());
370
- }
371
- }
372
341
}
0 commit comments