01: /*
02: * Copyright (C) Chaperon. All rights reserved.
03: * -------------------------------------------------------------------------
04: * This software is published under the terms of the Apache Software License
05: * version 1.1, a copy of which has been included with this distribution in
06: * the LICENSE file.
07: */
08:
09: package net.sourceforge.chaperon.build;
10:
11: import net.sourceforge.chaperon.model.Violations;
12: import net.sourceforge.chaperon.model.lexicon.Lexeme;
13: import net.sourceforge.chaperon.model.lexicon.Lexicon;
14: import net.sourceforge.chaperon.process.LexicalAutomaton;
15: import net.sourceforge.chaperon.process.PatternAutomaton;
16:
17: import org.apache.commons.logging.Log;
18:
19: /**
20: * This class represents a builder for the lexical automata.
21: *
22: * @author <a href="mailto:stephan@apache.org">Stephan Michels </a>
23: * @version CVS $Id: LexicalAutomatonBuilder.java,v 1.7 2003/12/09 19:55:52 benedikta Exp $
24: */
25: public class LexicalAutomatonBuilder {
26: private Lexicon lexicon = null;
27: private LexicalAutomaton automaton = null;
28: private Log log = null;
29:
30: /**
31: * Create a builder for an lexical automaton.
32: *
33: * @param lexicon Lexicon, which should be used for the automaton.
34: */
35: public LexicalAutomatonBuilder(Lexicon lexicon) {
36: this (lexicon, null);
37: }
38:
39: /**
40: * Create a builder for an lexical automaton.
41: *
42: * @param lexicon Lexicon, which should be used for the automaton.
43: * @param log Log, which should be used.
44: */
45: public LexicalAutomatonBuilder(Lexicon lexicon, Log log) {
46: this .log = log;
47:
48: try {
49: this .lexicon = (Lexicon) lexicon.clone();
50: } catch (CloneNotSupportedException cnse) {
51: throw new IllegalArgumentException(
52: "Lexicon is nor cloneable");
53: }
54:
55: Violations violations = lexicon.validate();
56:
57: if ((violations != null)
58: && (violations.getViolationCount() > 0))
59: throw new IllegalArgumentException("Lexicon is not valid: "
60: + violations.getViolation(0));
61:
62: LexicalAutomaton automaton = new LexicalAutomaton(lexicon
63: .getLexemeCount());
64:
65: Lexeme lexeme;
66: PatternAutomaton definition = null;
67:
68: for (int i = 0; i < lexicon.getLexemeCount(); i++) {
69: lexeme = lexicon.getLexeme(i);
70: automaton.setLexemeSymbol(i,
71: (lexeme.getSymbol() != null) ? lexeme.getSymbol()
72: .getName() : null);
73:
74: definition = (new PatternAutomatonBuilder(lexeme
75: .getDefinition())).getPatternAutomaton();
76: if (definition != null)
77: automaton.setLexemeDefinition(i, definition);
78: else
79: throw new IllegalArgumentException(
80: "Couldn't create PatternAutomaton for "
81: + lexeme.getSymbol() + " of \""
82: + lexeme.getDefinition() + "\"");
83: }
84:
85: this .automaton = automaton;
86: }
87:
88: /**
89: * Return the builded automaton. This method will return null, if an error occurs.
90: *
91: * @return Lexical automaton.
92: */
93: public LexicalAutomaton getLexicalAutomaton() {
94: return automaton;
95: }
96: }
|