KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > chaperon > model > extended > Definition


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.extended;
10
11 import net.sourceforge.chaperon.model.Violations;
12
13 /**
14  * This class presents a definition of a grammar
15  *
16  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels</a>
17  * @version CVS $Id: Definition.java,v 1.2 2003/12/14 09:44:21 benedikta Exp $
18  */

19 public class Definition extends PatternList
20 {
21   private String JavaDoc symbol = null;
22   private String JavaDoc location = null;
23
24   /**
25    * Create an empty definition.
26    */

27   public Definition() {}
28
29   /**
30    * Create a definition.
31    *
32    * @param ntsymbol The symbol of this definition.
33    */

34   public Definition(String JavaDoc symbol)
35   {
36     setSymbol(symbol);
37   }
38
39   /**
40    * Set the symbol for this definition
41    *
42    * @param ntsymbol Non terminal symbol
43    */

44   public void setSymbol(String JavaDoc symbol)
45   {
46     if (symbol==null)
47       throw new NullPointerException JavaDoc();
48
49     this.symbol = symbol;
50   }
51
52   /**
53    * Return the symbol from this definition
54    *
55    * @return Nonterminal symbol
56    */

57   public String JavaDoc getSymbol()
58   {
59     return symbol;
60   }
61
62   /**
63    * Set the location from the input source.
64    *
65    * @param location Location in the input source.
66    */

67   public void setLocation(String JavaDoc location)
68   {
69     this.location = location;
70   }
71
72   /**
73    * Returns the location from the input source.
74    *
75    * @return Location in the input source.
76    */

77   public String JavaDoc getLocation()
78   {
79     return location;
80   }
81
82   /**
83    * Validates the definition.
84    *
85    * @return Return a list of violations, if this object isn't valid.
86    */

87   public Violations validate()
88   {
89     Violations violations = new Violations();
90
91     if (symbol==null)
92       violations.addViolation("No symbol is for the left side defined", location);
93
94     if (getPatternCount()==0)
95       violations.addViolation("No pattern are for the right side defined", location);
96
97     for (int i = 0; i<getPatternCount(); i++)
98       violations.addViolations(getPattern(i).validate());
99
100     return violations;
101   }
102
103   /**
104    * Compares the definition with another definition.
105    *
106    * @param o Other object.
107    *
108    * @return True, if the definition are equal.
109    */

110   public boolean equals(Object JavaDoc o)
111   {
112     if (o==this)
113       return true;
114
115     if (o instanceof Definition)
116     {
117       Definition definition = (Definition)o;
118
119       return (symbol.equals(definition.symbol)) && (super.equals(o));
120     }
121
122     return false;
123   }
124
125   /**
126    * Return a string representation of the definition.
127    *
128    * @return String representation of the definition.
129    */

130   public String JavaDoc toString(PatternSet previous, PatternSet next)
131   {
132     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
133
134     buffer.append(symbol);
135     buffer.append(" := ");
136     buffer.append(super.toString(previous, next));
137
138     return buffer.toString();
139   }
140
141   /**
142    * @return
143    *
144    * @throws CloneNotSupportedException
145    */

146   public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc
147   {
148     Definition clone = new Definition();
149
150     clone.symbol = symbol;
151
152     for (int i = 0; i<getPatternCount(); i++)
153       clone.addPattern((Pattern)getPattern(i).clone());
154
155     clone.location = location;
156
157     return clone;
158   }
159 }
160
Popular Tags