KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > chaperon > model > lexicon > Lexicon


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.model.lexicon;
10
11 import net.sourceforge.chaperon.model.Violations;
12
13 import java.util.Enumeration JavaDoc;
14 import java.util.Vector JavaDoc;
15
16 /**
17  * The lexicon represents a collection of lexemes.
18  *
19  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels </a>
20  * @version CVS $Id: Lexicon.java,v 1.3 2003/12/09 19:55:52 benedikta Exp $
21  */

22 public class Lexicon
23 {
24   private Vector JavaDoc lexemes = new Vector JavaDoc();
25   private String JavaDoc location = null;
26
27   /**
28    * Add a lexeme to this lexicon.
29    *
30    * @param lexeme Lexeme, which should be added.
31    */

32   public void addLexeme(Lexeme lexeme)
33   {
34     lexemes.addElement(lexeme);
35   }
36
37   /**
38    * Remove a lexeme from this lexicon.
39    *
40    * @param lexeme Lexeme, which should be removed.
41    */

42   public void removeLexeme(Lexeme lexeme)
43   {
44     lexemes.removeElement(lexeme);
45   }
46
47   /**
48    * Return a lexeme given by an index.
49    *
50    * @param index Index of the lexeme.
51    *
52    * @return Lexeme.
53    */

54   public Lexeme getLexeme(int index)
55   {
56     return (Lexeme)lexemes.elementAt(index);
57   }
58
59   /**
60    * Return the count of lexemes in this lexicon.
61    *
62    * @return Count of lexemes.
63    */

64   public int getLexemeCount()
65   {
66     return lexemes.size();
67   }
68
69   /**
70    * Set the location from the input source.
71    *
72    * @param location Location in the input source.
73    */

74   public void setLocation(String JavaDoc location)
75   {
76     this.location = location;
77   }
78
79   /**
80    * Returns the location from the input source.
81    *
82    * @return Location in the input source.
83    */

84   public String JavaDoc getLocation()
85   {
86     return location;
87   }
88
89   /**
90    * Validates the lexicon.
91    *
92    * @return Return a list of violations, if this object isn't valid.
93    */

94   public Violations validate()
95   {
96     Violations violations = new Violations();
97
98     if (lexemes.size()==0)
99       violations.addViolation("Lexicon contains not lexemes", location);
100
101     for (Enumeration JavaDoc en = lexemes.elements(); en.hasMoreElements();)
102       violations.addViolations(((Lexeme)en.nextElement()).validate());
103
104     return violations;
105   }
106
107   /**
108    * Create a clone of this lexicon.
109    *
110    * @return Clone of this lexicon.
111    *
112    * @throws CloneNotSupportedException If an exception occurs during the cloning.
113    */

114   public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc
115   {
116     Lexicon clone = new Lexicon();
117
118     for (int i = 0; i<lexemes.size(); i++)
119       clone.lexemes.addElement(((Lexeme)lexemes.elementAt(i)).clone());
120
121     clone.location = location;
122
123     return clone;
124   }
125 }
126
Popular Tags