001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2006, 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: package org.geotools.filter.text.cql2;
017:
018: import java.util.EmptyStackException;
019: import java.util.Stack;
020:
021: import org.opengis.filter.expression.Literal;
022: import org.opengis.filter.expression.PropertyName;
023:
024: /**
025: * Holds the results of the building process in a stack
026: *
027: * @author Mauricio Pazos - Axios Engineering
028: * @author Gabriel Roldan - Axios Engineering
029: * @version $Id: BuildResultStack.java 25209 2007-04-20 11:09:26Z vmpazos $
030: * @since 2.4
031: */
032: final class BuildResultStack {
033:
034: private Stack stack = new Stack();
035:
036: private String cqlSource;
037:
038: public BuildResultStack(final String cqlSource) {
039: assert cqlSource != null;
040:
041: this .cqlSource = cqlSource;
042: }
043:
044: public Result peek() {
045: return (Result) stack.peek();
046: }
047:
048: public boolean empty() {
049: return stack.empty();
050: }
051:
052: public Result popResult() throws CQLException {
053: Result item = null;
054:
055: try {
056: return (Result) stack.pop();
057: } catch (ClassCastException cce) {
058: throw new CQLException(
059: "Expecting Expression, but found Filter", item
060: .getToken(), cce, this .cqlSource);
061: } catch (EmptyStackException ese) {
062: throw new CQLException("No items on stack");
063: }
064: }
065:
066: public org.opengis.filter.expression.Expression popExpression()
067: throws CQLException {
068: Result item = null;
069:
070: try {
071: item = (Result) stack.pop();
072:
073: return (org.opengis.filter.expression.Expression) item
074: .getBuilt();
075: } catch (ClassCastException cce) {
076: throw new CQLException(
077: "Expecting Expression, but found Filter", item
078: .getToken(), cce, this .cqlSource);
079: } catch (EmptyStackException ese) {
080: throw new CQLException("No items on stack");
081: }
082: }
083:
084: public Literal popLiteral() throws CQLException {
085: Result item = null;
086:
087: try {
088: item = (Result) stack.pop();
089:
090: return (Literal) item.getBuilt();
091: } catch (ClassCastException cce) {
092: throw new CQLException(
093: "Expecting Expression, but found Filter", item
094: .getToken(), cce, this .cqlSource);
095: } catch (EmptyStackException ese) {
096: throw new CQLException("No items on stack");
097: }
098: }
099:
100: public PropertyName popPropertyName() throws CQLException {
101: Result item = null;
102:
103: try {
104: item = (Result) stack.pop();
105:
106: return (PropertyName) item.getBuilt();
107: } catch (ClassCastException cce) {
108: throw new CQLException(
109: "Expecting Expression, but found Filter", item
110: .getToken(), cce, this .cqlSource);
111: } catch (EmptyStackException ese) {
112: throw new CQLException("No items on stack");
113: }
114: }
115:
116: public org.opengis.filter.Filter popFilter() throws CQLException {
117: Result item = null;
118:
119: try {
120: item = (Result) stack.pop();
121:
122: return (org.opengis.filter.Filter) item.getBuilt();
123: } catch (ClassCastException cce) {
124: throw new CQLException(
125: "Expecting Filter, but found Expression", item
126: .getToken(), cce, this .cqlSource);
127: } catch (EmptyStackException ese) {
128: throw new CQLException("No items on stack");
129: }
130: }
131:
132: public PeriodNode popPeriod() throws CQLException {
133: Result item = null;
134:
135: try {
136: item = (Result) stack.pop();
137:
138: return (PeriodNode) item.getBuilt();
139: } catch (ClassCastException cce) {
140: throw new CQLException(
141: "Expecting Filter, but found Expression", item
142: .getToken(), cce, this .cqlSource);
143: } catch (EmptyStackException ese) {
144: throw new CQLException("No items on stack");
145: }
146: }
147:
148: public double popDoubleValue() throws CQLException {
149: try {
150: Literal expr = this .popLiteral();
151: Double number = new Double(expr.getValue().toString());
152:
153: return number.doubleValue();
154: } catch (ClassCastException cce) {
155: throw new CQLException("Expected double");
156: }
157: }
158:
159: public int popIntegerValue() throws CQLException {
160: try {
161: Literal expr = this .popLiteral();
162: Integer number = (Integer) expr.getValue();
163:
164: return number.intValue();
165: } catch (ClassCastException cce) {
166: throw new CQLException("Expected double");
167: }
168: }
169:
170: public String popStringValue() throws CQLException {
171: Literal literal = this .popLiteral();
172:
173: return literal.toString();
174: }
175:
176: public String popIdentifierPart() throws CQLException {
177: try {
178: Result resultPart = (Result) stack.pop();
179: Token token = resultPart.getToken();
180:
181: return token.image;
182: } catch (ClassCastException e) {
183: throw new CQLException("identifier part is expected");
184: }
185: }
186:
187: public String popIdentifier() throws CQLException {
188: try {
189: Result result = (Result) stack.pop();
190: String identifier = (String) result.getBuilt();
191:
192: return identifier;
193: } catch (ClassCastException e) {
194: throw new CQLException("fail in identifier parsing");
195: }
196: }
197:
198: public void push(Result item) {
199: stack.push(item);
200: }
201:
202: public int size() {
203: return stack.size();
204: }
205: }
|