1 | package org.etsi.t3q.visitor.tool; |
---|
2 | |
---|
3 | import java.io.File; |
---|
4 | import java.io.FileNotFoundException; |
---|
5 | import java.io.FileOutputStream; |
---|
6 | import java.io.IOException; |
---|
7 | import java.lang.reflect.Field; |
---|
8 | |
---|
9 | import org.antlr.stringtemplate.StringTemplate; |
---|
10 | import org.antlr.stringtemplate.StringTemplateGroup; |
---|
11 | |
---|
12 | public class VisitorGenerator { |
---|
13 | |
---|
14 | public VisitorGenerator() { |
---|
15 | |
---|
16 | try { |
---|
17 | StringTemplateGroup group = new StringTemplateGroup("visitorGroup", |
---|
18 | "templates"); |
---|
19 | group.setRefreshInterval(0); // no caching |
---|
20 | group.setRefreshInterval(Integer.MAX_VALUE); // no refreshing |
---|
21 | StringTemplate classTemplate = group |
---|
22 | .getInstanceOf("AbstractVisitor"); |
---|
23 | Class<?> c = Class |
---|
24 | .forName("de.ugoe.cs.swe.trex.core.analyzer.rfparser.TTCN3ParserTokenTypes"); |
---|
25 | Field[] fields = c.getFields(); |
---|
26 | |
---|
27 | for (int i = 0; i < fields.length; i++) { |
---|
28 | if (!isOmitField(fields[i])) { |
---|
29 | classTemplate.setAttribute("nodeType", fields[i].getName()); |
---|
30 | } |
---|
31 | } |
---|
32 | File f = new File( |
---|
33 | "src/org/etsi/t3q/visitor/AbstractVisitor.java"); |
---|
34 | saveFile(f, classTemplate.toString()); |
---|
35 | } catch (ClassNotFoundException e) { |
---|
36 | e.printStackTrace(); |
---|
37 | } |
---|
38 | } |
---|
39 | |
---|
40 | // ---------------------------------------------------------------------- |
---|
41 | |
---|
42 | private boolean isOmitField(Field f) { |
---|
43 | if (f.getName().contains("EOF") |
---|
44 | || f.getName().contains("NULL_TREE_LOOKAHEAD")) |
---|
45 | return true; |
---|
46 | return false; |
---|
47 | } |
---|
48 | |
---|
49 | // ---------------------------------------------------------------------- |
---|
50 | |
---|
51 | public void saveFile(File filename, String content) { |
---|
52 | FileOutputStream fs; |
---|
53 | try { |
---|
54 | fs = new FileOutputStream(filename); |
---|
55 | fs.write(content.getBytes()); |
---|
56 | fs.close(); |
---|
57 | } catch (FileNotFoundException e) { |
---|
58 | e.printStackTrace(); |
---|
59 | } catch (IOException e) { |
---|
60 | e.printStackTrace(); |
---|
61 | } |
---|
62 | } |
---|
63 | |
---|
64 | // ---------------------------------------------------------------------- |
---|
65 | |
---|
66 | public static void main(String[] args) { |
---|
67 | new VisitorGenerator(); |
---|
68 | } |
---|
69 | |
---|
70 | } |
---|