001: /*
002: * Geotools2 - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002, Geotools Project Managment Committee (PMC)
005: *
006: * This library 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;
009: * version 2.1 of the License.
010: *
011: * This library 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: */
017: package org.geotools.filter.text.cql2;
018:
019: import org.geotools.filter.text.cql2.ParseException;
020: import org.geotools.filter.text.cql2.Token;
021:
022: /**
023: * This exception is produced when the cql input string has sintax errors.
024: *
025: * @author Mauricio Pazos (Axios Engineering)
026: *
027: * @version $Id: CQLException.java 28415 2007-12-18 09:54:31Z vmpazos $
028: * @since 2.4
029: */
030: public class CQLException extends ParseException {
031: /** for interoperability */
032: private static final long serialVersionUID = 8873756073711225699L;
033:
034: protected Throwable cause = null;
035: private String cqlSource = null;
036:
037: /**
038: * New instance of CQLException
039: *
040: * @param message exception description
041: * @param token current token
042: * @param cause the cause
043: * @param cqlSource string analyzed
044: */
045: public CQLException(String message, Token token, Throwable cause,
046: String cqlSource) {
047: super (message);
048:
049: assert message != null;
050:
051: super .currentToken = token;
052:
053: this .cause = cause;
054: this .cqlSource = cqlSource;
055: }
056:
057: /**
058: * New instance of CQLException
059: *
060: * @param message exception description
061: * @param token current token
062: * @param cqlSource analyzed string
063: */
064: public CQLException(final String message, final Token token,
065: final String cqlSource) {
066: this (message, token, null, cqlSource);
067: }
068:
069: /**
070: * New instance of CQLException
071: *
072: * @param message exception description
073: */
074: public CQLException(String message) {
075: this (message, null, null, null);
076: }
077:
078: /**
079: * returns the cause
080: *
081: * @return the cause
082: */
083: public Throwable getCause() {
084: return cause;
085: }
086:
087: /**
088: * Returns the exception message
089: *
090: * @return a message
091: */
092: public String getMessage() {
093: if (currentToken == null) {
094: return this .getMessage();
095: }
096:
097: return super .getMessage() + ", Current Token : "
098: + currentToken.image;
099: }
100:
101: /**
102: * Returns the syntax error presents in the last sequence of characters analyzed.
103: *
104: * @return the syntax error
105: */
106: public String getSyntaxError() {
107:
108: // builds two lines the first has the source string, the second has
109: // the pointer to syntax error.
110:
111: // First Line
112: StringBuffer msg = new StringBuffer(this .cqlSource);
113: msg.append('\n');
114:
115: // Second Line
116: // searches the last token recognized
117: Token curToken = this .currentToken;
118:
119: while (curToken.next != null)
120: curToken = curToken.next;
121:
122: // add the pointer to error
123: int column = curToken.beginColumn - 1;
124:
125: for (int i = 0; i < column; i++) {
126: msg.append(' ');
127: }
128:
129: msg.append('^').append('\n');
130: msg.append(this.getMessage());
131:
132: return msg.toString();
133: }
134: }
|