org.geolatte.common.cql.CqlLexer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of geolatte-common Show documentation
Show all versions of geolatte-common Show documentation
This GeoLatte-common library contains the transformer framework and other common classes used by other
GeoLatte modules.
/*
* This file is part of the GeoLatte project.
*
* GeoLatte is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GeoLatte is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with GeoLatte. If not, see .
*
* Copyright (C) 2010 - 2010 and Ownership of code is shared by:
* Qmino bvba - Romeinsestraat 18 - 3001 Heverlee (http://www.qmino.com)
* Geovise bvba - Generaal Eisenhowerlei 9 - 2140 Antwerpen (http://www.geovise.com)
*/
package org.geolatte.common.cql;
import org.geolatte.common.cql.lexer.Lexer;
import org.geolatte.common.cql.node.*;
import java.io.PushbackReader;
/**
*
* This is the CQL Lexer. It takes care of correctly parsing WKT strings. The generated {@link Lexer} class does not
* work when parsing CQL strings that include WKT.
*
*
* Creation-Date: 12-Aug-2010
* Creation-Time: 11:20:34
*
*
* @author Bert Vanhooff
* @author Qmino bvba
* @since SDK1.5
*/
public class CqlLexer extends Lexer {
private Token wktToken = null;
private int openBraces = 0;
private String accumulatedWktText = "";
public CqlLexer(@SuppressWarnings("hiding") PushbackReader in) {
super(in);
}
@Override
protected void filter() {
if (state == State.WKT) {
if (wktToken == null) { // just entering the wkt state ..
if (token instanceof TWktEmptySetLiteral) { // nothing special needed for the empty geometry.. return to
// normal and continue
state = State.NORMAL;
return;
}
wktToken = token; // save the current token (we will add text to this)
openBraces = 0; // reset open braces count (when it is back to 0, we are ready)
accumulatedWktText = token.getText();
token = null; // continue parsing the next token
return;
}
else { // already in wkt state ..
boolean tokenIsLeftParen = token instanceof TLeftParen;
boolean tokenIsRightParen = token instanceof TRightParen;
// Count the open braces
if (tokenIsLeftParen)
openBraces++;
else if (tokenIsRightParen)
openBraces--;
accumulatedWktText += token.getText();
// We are done if open braces count is back to 0 (can only happen if the current token is a right brace)
// or is we have an empty token
if (((tokenIsRightParen) && openBraces == 0) || token instanceof TWktEmptySetLiteral) {
state = State.NORMAL; // Set state back to normal
wktToken.setText(accumulatedWktText); // Set token text
token = wktToken; // 'return' the cached token
}
else // We are not done yet
token = null; // continue parsing the next token
}
}
}
}