1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 package name.angoca.zemucan.grammarReader.api;
30
31 import java.lang.reflect.Constructor;
32 import java.lang.reflect.InvocationTargetException;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.StringTokenizer;
36
37 import name.angoca.zemucan.AbstractZemucanException;
38 import name.angoca.zemucan.core.graph.model.Graph;
39 import name.angoca.zemucan.core.graph.model.StartingNode;
40 import name.angoca.zemucan.tools.Constants;
41 import name.angoca.zemucan.tools.configurator.Configurator;
42
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 public final class GrammarReaderController implements
72 InterfaceGrammarReaderController {
73
74
75
76 private static GrammarReaderController instance;
77
78
79
80 private static final Logger LOGGER = LoggerFactory
81 .getLogger(GrammarReaderController.class);
82
83
84
85
86
87 public static void destroyInstance() {
88 GrammarReaderController.LOGGER
89 .debug("Destroying GrammarReaderController instance.");
90
91 if (GrammarReaderController.instance != null) {
92 GrammarReaderController.instance = null;
93 }
94
95 assert GrammarReaderController.instance == null;
96 }
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115 public static GrammarReaderController
116 throws AbstractZemucanException {
117 if (GrammarReaderController.instance == null) {
118 GrammarReaderController.LOGGER
119 .debug("Creating GrammarReaderController instance");
120 synchronized (GrammarReaderController.class) {
121 GrammarReaderController.instance = new GrammarReaderController();
122 }
123 }
124
125 assert GrammarReaderController.instance != null;
126 return GrammarReaderController.instance;
127 }
128
129
130
131
132
133
134
135
136
137
138
139 public static Graph
140 throws AbstractZemucanException {
141 Graph masterGraph = null;
142 if (graphs.length > 1) {
143 masterGraph = graphs[0];
144 for (int i = 1; i < graphs.length; i += 1) {
145 final Graph graph = graphs[i];
146
147 graph.merge(masterGraph);
148 }
149
150 masterGraph.simplifyGraph();
151 } else if (graphs.length == 1) {
152 masterGraph = graphs[0];
153 } else {
154 assert false;
155 }
156
157 return masterGraph;
158 }
159
160
161
162
163
164
165
166
167
168
169
170
171
172 public static String
173 final List<String>
174 String globalDelimiters = "";
175 for (final String delims : delimiters) {
176 for (int j = 0; j < delims.length(); j += 1) {
177 final char character = delims.charAt(j);
178 if (globalDelimiters.indexOf(character) == -1) {
179 globalDelimiters += delims.charAt(j);
180 }
181 }
182 }
183 GrammarReaderController.LOGGER
184 .debug("Delimiters merged: " + globalDelimiters);
185 return globalDelimiters;
186 }
187
188
189
190
191 private final String globalDelimiters;
192
193
194
195
196 private final StartingNode startingNode;
197
198
199
200
201 private final Graph unifiedGgraph;
202
203
204
205
206
207
208
209
210
211
212 private GrammarReaderController() throws AbstractZemucanException {
213
214 final Constructor<?> constructor = this.getConstructor();
215
216
217 final String grammarFileDescriptors = Configurator.getInstance()
218 .getProperty(Constants.GRAMMAR_FILE_DESCRIPTORS_PROPERTY);
219 final StringTokenizer tokenFileDescriptors = new StringTokenizer(
220 grammarFileDescriptors, ";");
221
222 final int filesQty = tokenFileDescriptors.countTokens();
223 final int position = 0;
224
225 final List<Graph> graphs = new ArrayList<Graph>(filesQty);
226 final List<String> grammarDelimiters = new ArrayList<String>(filesQty);
227
228
229 while (tokenFileDescriptors.hasMoreTokens()) {
230 final String fileDescriptor = tokenFileDescriptors.nextToken();
231 this.readGraph(constructor, position, graphs, grammarDelimiters,
232 fileDescriptor);
233 }
234 this.globalDelimiters = GrammarReaderController
235 .processDelimiters(grammarDelimiters);
236 this.unifiedGgraph = GrammarReaderController.mergeGraphs(graphs
237 .toArray(new Graph[0]));
238 this.startingNode = this.unifiedGgraph.getStartingNode();
239 this.unifiedGgraph.checkDuplicates();
240 }
241
242
243
244
245
246
247
248
249
250
251
252
253 private Constructor<?>
254 throws AbstractGrammarReaderException {
255
256 final String grammarReaderName = Configurator.getInstance()
257 .getProperty(Constants.GRAMMAR_READER_NAME_PROPERTY);
258 if ((grammarReaderName == null) || grammarReaderName.equals("")) {
259 throw new GrammarReaderNotDefinedException();
260 }
261
262
263 Class<?> clazz = null;
264 try {
265 clazz = Class.forName(grammarReaderName);
266 } catch (final ClassNotFoundException e) {
267 throw new GeneralGrammarReaderProblemException(e);
268 }
269
270 Constructor<?> constructor = null;
271 try {
272 constructor = clazz.getConstructor(Graph.class, String.class);
273 } catch (final NoSuchMethodException e) {
274 throw new GeneralGrammarReaderProblemException(e);
275 }
276 return constructor;
277 }
278
279
280
281
282
283
284
285
286 @Override
287 public String
288 return this.globalDelimiters;
289 }
290
291
292
293
294
295
296 Graph
297 return this.unifiedGgraph;
298 }
299
300
301
302
303
304
305
306
307 @Override
308 public StartingNode
309 throws AbstractZemucanException {
310 return this.startingNode;
311 }
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330 private int readGraph(final Constructor<?> constructor, int position,
331 final List<Graph> graphs, final List<String> grammarDelimiters,
332 final String fileDescriptor) throws AbstractZemucanException {
333 final boolean extraNodes = !Boolean.parseBoolean(System
334 .getProperty(Constants.WITHOUT_EXTRA_NODES));
335 graphs.add(position, new Graph(fileDescriptor, extraNodes));
336 try {
337
338 final Object grammar = constructor.newInstance(
339 graphs.get(position), fileDescriptor);
340
341 final Graph newGraph = ((AbstractGrammarReader) grammar)
342 .generateGraph();
343 if (newGraph != graphs.get(position)) {
344 graphs.remove(position);
345 graphs.add(position, newGraph);
346 }
347
348 ((AbstractGrammarReader) grammar).getStartingNode();
349
350
351 grammarDelimiters.add(position,
352 ((AbstractGrammarReader) grammar).getDelimiters());
353
354 position += 1;
355 } catch (final IllegalArgumentException e1) {
356
357
358 throw new GeneralGrammarReaderProblemException(e1);
359 } catch (final InstantiationException e1) {
360 throw new GeneralGrammarReaderProblemException(e1);
361 } catch (final IllegalAccessException e1) {
362
363
364 throw new GeneralGrammarReaderProblemException(e1);
365 } catch (final InvocationTargetException e1) {
366 throw new GeneralGrammarReaderProblemException(e1);
367 }
368 return position;
369 }
370 }