KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > chaperon > build > LexicalAutomatonBuilder


1 /*
2  * Copyright (C) Chaperon. All rights reserved.
3  * -------------------------------------------------------------------------
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  */

8
9 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 {
27   private Lexicon lexicon = null;
28   private LexicalAutomaton automaton = null;
29   private Log log = null;
30
31   /**
32    * Create a builder for an lexical automaton.
33    *
34    * @param lexicon Lexicon, which should be used for the automaton.
35    */

36   public LexicalAutomatonBuilder(Lexicon lexicon)
37   {
38     this(lexicon, null);
39   }
40
41   /**
42    * Create a builder for an lexical automaton.
43    *
44    * @param lexicon Lexicon, which should be used for the automaton.
45    * @param log Log, which should be used.
46    */

47   public LexicalAutomatonBuilder(Lexicon lexicon, Log log)
48   {
49     this.log = log;
50
51     try
52     {
53       this.lexicon = (Lexicon)lexicon.clone();
54     }
55     catch (CloneNotSupportedException JavaDoc cnse)
56     {
57       throw new IllegalArgumentException JavaDoc("Lexicon is nor cloneable");
58     }
59
60     Violations violations = lexicon.validate();
61
62     if ((violations!=null) && (violations.getViolationCount()>0))
63       throw new IllegalArgumentException JavaDoc("Lexicon is not valid: "+violations.getViolation(0));
64
65     LexicalAutomaton automaton = new LexicalAutomaton(lexicon.getLexemeCount());
66
67     Lexeme lexeme;
68     PatternAutomaton definition = null;
69
70     for (int i = 0; i<lexicon.getLexemeCount(); i++)
71     {
72       lexeme = lexicon.getLexeme(i);
73       automaton.setLexemeSymbol(i, (lexeme.getSymbol()!=null) ? lexeme.getSymbol().getName() : null);
74
75       definition = (new PatternAutomatonBuilder(lexeme.getDefinition())).getPatternAutomaton();
76       if (definition!=null)
77         automaton.setLexemeDefinition(i, definition);
78       else
79         throw new IllegalArgumentException JavaDoc("Couldn't create PatternAutomaton for "+
80                                            lexeme.getSymbol()+" of \""+lexeme.getDefinition()+"\"");
81     }
82
83     this.automaton = automaton;
84   }
85
86   /**
87    * Return the builded automaton. This method will return null, if an error occurs.
88    *
89    * @return Lexical automaton.
90    */

91   public LexicalAutomaton getLexicalAutomaton()
92   {
93     return automaton;
94   }
95 }
96
Popular Tags