Skip to content

Commit b0d8e86

Browse files
author
mgeipel
committed
refactored Flux
moved ANTLR specifics to FluxCompiler
1 parent 70b59eb commit b0d8e86

File tree

2 files changed

+61
-20
lines changed

2 files changed

+61
-20
lines changed

src/main/java/org/culturegraph/mf/Flux.java

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.io.File;
1919
import java.io.FilenameFilter;
2020
import java.io.IOException;
21-
import java.io.InputStream;
2221
import java.net.URL;
2322
import java.net.URLClassLoader;
2423
import java.util.HashMap;
@@ -28,14 +27,9 @@
2827
import java.util.regex.Matcher;
2928
import java.util.regex.Pattern;
3029

31-
import org.antlr.runtime.ANTLRInputStream;
32-
import org.antlr.runtime.CommonTokenStream;
3330
import org.antlr.runtime.RecognitionException;
34-
import org.antlr.runtime.tree.CommonTreeNodeStream;
3531
import org.culturegraph.mf.flux.Flow;
36-
import org.culturegraph.mf.flux.parser.FlowBuilder;
37-
import org.culturegraph.mf.flux.parser.FluxLexer;
38-
import org.culturegraph.mf.flux.parser.FluxParser;
32+
import org.culturegraph.mf.flux.FluxCompiler;
3933
import org.culturegraph.mf.util.ResourceUtil;
4034

4135
/**
@@ -101,21 +95,10 @@ public boolean accept(final File dir, final String name) {
10195
}
10296

10397
// run parser and builder
104-
final Flow flow = compileFlow(compileAst(ResourceUtil.getStream(fluxFile)), vars);
105-
flow.start();
98+
FluxCompiler.compile(ResourceUtil.getStream(fluxFile), vars).start();
99+
//flow.start();
106100
}
107101
}
108102

109-
public static CommonTreeNodeStream compileAst(final InputStream flowDef) throws IOException, RecognitionException {
110-
final FluxParser parser = new FluxParser(new CommonTokenStream(new FluxLexer(new ANTLRInputStream(flowDef))));
111-
return new CommonTreeNodeStream(parser.flux().getTree());
112-
}
113103

114-
public static Flow compileFlow(final CommonTreeNodeStream treeNodes, final Map<String, String> vars)
115-
throws RecognitionException {
116-
final FlowBuilder flowBuilder = new FlowBuilder(treeNodes);
117-
flowBuilder.addVaribleAssignements(vars);
118-
final Flow flow = flowBuilder.flux();
119-
return flow;
120-
}
121104
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2013 Deutsche Nationalbibliothek
3+
*
4+
* Licensed under the Apache License, Version 2.0 the "License";
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.culturegraph.mf.flux;
18+
19+
import java.io.IOException;
20+
import java.io.InputStream;
21+
import java.util.Map;
22+
23+
import org.antlr.runtime.ANTLRInputStream;
24+
import org.antlr.runtime.CommonTokenStream;
25+
import org.antlr.runtime.RecognitionException;
26+
import org.antlr.runtime.tree.CommonTreeNodeStream;
27+
import org.culturegraph.mf.flux.parser.FlowBuilder;
28+
import org.culturegraph.mf.flux.parser.FluxLexer;
29+
import org.culturegraph.mf.flux.parser.FluxParser;
30+
31+
/**
32+
* Creates a {@link Flow} based on a flux script
33+
*
34+
* @author markus geipel
35+
*
36+
*/
37+
public final class FluxCompiler {
38+
private FluxCompiler() {
39+
// no instances
40+
}
41+
42+
public static Flow compile(final InputStream flux, final Map<String,String> vars ) throws RecognitionException, IOException{
43+
return compileFlow(compileAst(flux), vars);
44+
}
45+
46+
private static CommonTreeNodeStream compileAst(final InputStream flowDef) throws IOException, RecognitionException {
47+
final FluxParser parser = new FluxParser(new CommonTokenStream(new FluxLexer(new ANTLRInputStream(flowDef))));
48+
return new CommonTreeNodeStream(parser.flux().getTree());
49+
}
50+
51+
private static Flow compileFlow(final CommonTreeNodeStream treeNodes, final Map<String, String> vars)
52+
throws RecognitionException {
53+
final FlowBuilder flowBuilder = new FlowBuilder(treeNodes);
54+
flowBuilder.addVaribleAssignements(vars);
55+
final Flow flow = flowBuilder.flux();
56+
return flow;
57+
}
58+
}

0 commit comments

Comments
 (0)