001: /*
002: * hgcommons 7
003: * Hammurapi Group Common Library
004: * Copyright (C) 2003 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023:
024: package biz.hammurapi.antlr;
025:
026: import antlr.CommonToken;
027: import biz.hammurapi.legacy.review.SourceMarker;
028: import biz.hammurapi.util.PoliteVisitor;
029: import biz.hammurapi.util.Visitable;
030: import biz.hammurapi.util.Visitor;
031:
032: /**
033: *
034: * @author Pavel Vlasov
035: * @version $Revision: 1.4 $
036: */
037: public class Token extends CommonToken implements SourceMarker,
038: Comparable, Visitable {
039: public Token() {
040: super ();
041: }
042:
043: public Token(String s) {
044: super (s);
045: }
046:
047: public Token(int t, String txt) {
048: super (t, txt);
049: }
050:
051: private Token nextToken;
052: private Token prevToken;
053:
054: public void setNextToken(Token token) {
055: if (nextToken != token) {
056: nextToken = token;
057: if (nextToken != null) {
058: nextToken.prevToken = this ;
059: }
060:
061: if (listener != null) {
062: listener.onNextTokenChanged(this );
063: }
064: }
065: }
066:
067: public Token getNextToken() {
068: return nextToken;
069: }
070:
071: public void setPrevToken(Token token) {
072: if (prevToken != token) {
073: prevToken = token;
074: if (prevToken != null) {
075: prevToken.nextToken = this ;
076: }
077:
078: if (listener != null) {
079: listener.onPrevTokenChanged(this );
080: }
081: }
082: }
083:
084: public Token getPrevToken() {
085: return prevToken;
086: }
087:
088: private TokenListener listener;
089:
090: /**
091: * @return Returns the listener.
092: */
093: public TokenListener getListener() {
094: return listener;
095: }
096:
097: /**
098: * @param listener The listener to set.
099: */
100: public void setListener(TokenListener listener) {
101: this .listener = listener;
102: }
103:
104: /* (non-Javadoc)
105: * @see antlr.Token#setText(java.lang.String)
106: */
107: public void setText(String newText) {
108: super .setText(newText);
109: if (listener != null) {
110: listener.onTextChanged(this );
111: }
112: }
113:
114: private String sourceURL;
115:
116: public String getSourceURL() {
117: return sourceURL;
118: }
119:
120: /**
121: * Sets source URL for this and all subsequent tokens.
122: * @param sourceURL
123: */
124: public void setSourceURL(String sourceURL) {
125: this .sourceURL = sourceURL;
126: }
127:
128: public int compareTo(Object o) {
129: if (o instanceof Token) {
130: Token t = (Token) o;
131: if (t.getLine() == getLine()) {
132: if (t.getColumn() == getColumn()) {
133: if (getText() == null) {
134: return t.getText() == null ? 0 : 1;
135: }
136:
137: return t.getText() == null ? -1 : getText()
138: .compareTo(t.getText());
139: }
140:
141: return getColumn() - t.getColumn();
142: }
143:
144: return getLine() - t.getLine();
145: }
146:
147: return 1;
148: }
149:
150: public boolean equals(Object obj) {
151: if (obj == this ) {
152: return true;
153: }
154:
155: if (obj == null) {
156: return false;
157: }
158:
159: if (obj instanceof Token && getClass().equals(obj.getClass())) {
160: Token t = (Token) obj;
161: return getType() == t.getType()
162: && ((getText() == null && t.getText() == null) || (getText() != null
163: && t.getText() != null && getText().equals(
164: t.getText())));
165: }
166:
167: return super .equals(obj);
168: }
169:
170: public int hashCode() {
171: int ret = getType();
172: ret ^= getClass().getName().hashCode();
173:
174: if (getText() != null) {
175: ret ^= getText().hashCode();
176: }
177: return ret;
178: }
179:
180: public boolean accept(Visitor visitor) {
181: for (Token t = this ; t != null; t = t.getNextToken()) {
182: if (visitor.visit(t)) {
183: if (visitor instanceof PoliteVisitor) {
184: ((PoliteVisitor) visitor).leave(t);
185: }
186: } else {
187: return false;
188: }
189: }
190: return true;
191: }
192:
193: public Integer getSourceId() {
194: return null;
195: }
196: }
|