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.extended;
010:
011: import net.sourceforge.chaperon.model.Violations;
012:
013: /**
014: * This class presents a definition of a grammar
015: *
016: * @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
017: * @version CVS $Id: Definition.java,v 1.2 2003/12/14 09:44:21 benedikta Exp $
018: */
019: public class Definition extends PatternList {
020: private String symbol = null;
021: private String location = null;
022:
023: /**
024: * Create an empty definition.
025: */
026: public Definition() {
027: }
028:
029: /**
030: * Create a definition.
031: *
032: * @param ntsymbol The symbol of this definition.
033: */
034: public Definition(String symbol) {
035: setSymbol(symbol);
036: }
037:
038: /**
039: * Set the symbol for this definition
040: *
041: * @param ntsymbol Non terminal symbol
042: */
043: public void setSymbol(String symbol) {
044: if (symbol == null)
045: throw new NullPointerException();
046:
047: this .symbol = symbol;
048: }
049:
050: /**
051: * Return the symbol from this definition
052: *
053: * @return Nonterminal symbol
054: */
055: public String getSymbol() {
056: return symbol;
057: }
058:
059: /**
060: * Set the location from the input source.
061: *
062: * @param location Location in the input source.
063: */
064: public void setLocation(String location) {
065: this .location = location;
066: }
067:
068: /**
069: * Returns the location from the input source.
070: *
071: * @return Location in the input source.
072: */
073: public String getLocation() {
074: return location;
075: }
076:
077: /**
078: * Validates the definition.
079: *
080: * @return Return a list of violations, if this object isn't valid.
081: */
082: public Violations validate() {
083: Violations violations = new Violations();
084:
085: if (symbol == null)
086: violations.addViolation(
087: "No symbol is for the left side defined", location);
088:
089: if (getPatternCount() == 0)
090: violations.addViolation(
091: "No pattern are for the right side defined",
092: location);
093:
094: for (int i = 0; i < getPatternCount(); i++)
095: violations.addViolations(getPattern(i).validate());
096:
097: return violations;
098: }
099:
100: /**
101: * Compares the definition with another definition.
102: *
103: * @param o Other object.
104: *
105: * @return True, if the definition are equal.
106: */
107: public boolean equals(Object o) {
108: if (o == this )
109: return true;
110:
111: if (o instanceof Definition) {
112: Definition definition = (Definition) o;
113:
114: return (symbol.equals(definition.symbol))
115: && (super .equals(o));
116: }
117:
118: return false;
119: }
120:
121: /**
122: * Return a string representation of the definition.
123: *
124: * @return String representation of the definition.
125: */
126: public String toString(PatternSet previous, PatternSet next) {
127: StringBuffer buffer = new StringBuffer();
128:
129: buffer.append(symbol);
130: buffer.append(" := ");
131: buffer.append(super .toString(previous, next));
132:
133: return buffer.toString();
134: }
135:
136: /**
137: * @return
138: *
139: * @throws CloneNotSupportedException
140: */
141: public Object clone() throws CloneNotSupportedException {
142: Definition clone = new Definition();
143:
144: clone.symbol = symbol;
145:
146: for (int i = 0; i < getPatternCount(); i++)
147: clone.addPattern((Pattern) getPattern(i).clone());
148:
149: clone.location = location;
150:
151: return clone;
152: }
153: }
|