001: /*
002: * Copyright (C) Chaperon. All rights reserved.
003: * -------------------------------------------------------------------------
004: * This software is published under the terms of the Apache Software License
005: * version 1.1, a copy of which has been included with this distribution in
006: * the LICENSE file.
007: */
008:
009: package net.sourceforge.chaperon.model.lexicon;
010:
011: import net.sourceforge.chaperon.model.Violations;
012:
013: import java.util.Enumeration;
014: import java.util.Vector;
015:
016: /**
017: * The lexicon represents a collection of lexemes.
018: *
019: * @author <a href="mailto:stephan@apache.org">Stephan Michels </a>
020: * @version CVS $Id: Lexicon.java,v 1.3 2003/12/09 19:55:52 benedikta Exp $
021: */
022: public class Lexicon {
023: private Vector lexemes = new Vector();
024: private String location = null;
025:
026: /**
027: * Add a lexeme to this lexicon.
028: *
029: * @param lexeme Lexeme, which should be added.
030: */
031: public void addLexeme(Lexeme lexeme) {
032: lexemes.addElement(lexeme);
033: }
034:
035: /**
036: * Remove a lexeme from this lexicon.
037: *
038: * @param lexeme Lexeme, which should be removed.
039: */
040: public void removeLexeme(Lexeme lexeme) {
041: lexemes.removeElement(lexeme);
042: }
043:
044: /**
045: * Return a lexeme given by an index.
046: *
047: * @param index Index of the lexeme.
048: *
049: * @return Lexeme.
050: */
051: public Lexeme getLexeme(int index) {
052: return (Lexeme) lexemes.elementAt(index);
053: }
054:
055: /**
056: * Return the count of lexemes in this lexicon.
057: *
058: * @return Count of lexemes.
059: */
060: public int getLexemeCount() {
061: return lexemes.size();
062: }
063:
064: /**
065: * Set the location from the input source.
066: *
067: * @param location Location in the input source.
068: */
069: public void setLocation(String location) {
070: this .location = location;
071: }
072:
073: /**
074: * Returns the location from the input source.
075: *
076: * @return Location in the input source.
077: */
078: public String getLocation() {
079: return location;
080: }
081:
082: /**
083: * Validates the lexicon.
084: *
085: * @return Return a list of violations, if this object isn't valid.
086: */
087: public Violations validate() {
088: Violations violations = new Violations();
089:
090: if (lexemes.size() == 0)
091: violations.addViolation("Lexicon contains not lexemes",
092: location);
093:
094: for (Enumeration en = lexemes.elements(); en.hasMoreElements();)
095: violations.addViolations(((Lexeme) en.nextElement())
096: .validate());
097:
098: return violations;
099: }
100:
101: /**
102: * Create a clone of this lexicon.
103: *
104: * @return Clone of this lexicon.
105: *
106: * @throws CloneNotSupportedException If an exception occurs during the cloning.
107: */
108: public Object clone() throws CloneNotSupportedException {
109: Lexicon clone = new Lexicon();
110:
111: for (int i = 0; i < lexemes.size(); i++)
112: clone.lexemes.addElement(((Lexeme) lexemes.elementAt(i))
113: .clone());
114:
115: clone.location = location;
116:
117: return clone;
118: }
119: }
|