⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 whereclausesyntaxanalyzer.java

📁 esri的ArcGIS Server超级学习模板程序(for java)
💻 JAVA
字号:
package com.esri.solutions.jitk.web.wfs.data.query.sql;

import java.util.ArrayList;
import java.util.List;

public class WhereClauseSyntaxAnalyzer {
	public List<LogicalOperator> _operators;
	public List<Tuple> _tuples;
	public String _wc;
	public Tuple _currentTuple;
	public int _unmatchedParenCount = 0;
	public int _notParenCount = 0;
	
	public WhereClauseSyntaxAnalyzer(String whereClause) {
		_wc = whereClause;
		_operators = new ArrayList<LogicalOperator>();
		_tuples = new ArrayList<Tuple>();
		_unmatchedParenCount = 0;
	}
	
	public void analyze() throws InvalidTokenException, InvalidSyntaxException {
		if (_wc.length() != 0) {
			WhereClauseTokenizer tokenizer = new WhereClauseTokenizer(_wc);
			_currentTuple = new Tuple();
			WhereClauseToken token = tokenizer.getNextToken();
			
			while (token != null) {
				pushToken(token);
				token = tokenizer.getNextToken();
			}
			
			if (!(_currentTuple.isComplete() || _currentTuple.isEmpty())) {
				throw new InvalidSyntaxException("Last encountered tuple is incomplete");
			}
			
			if (_unmatchedParenCount != 0) {
				throw new InvalidSyntaxException("Unmatched parenthesis in statement");
			}
			
			if (_tuples.size() < 1) {
				throw new InvalidSyntaxException("No tuple encountered");
			}
			
			if (_tuples.size() != (_operators.size() + 1 - _notParenCount)) {
				throw new InvalidSyntaxException("Proportion of tuples does not match that of logical operators");
			}
		}
	}
	
	protected void pushToken(WhereClauseToken token) throws InvalidSyntaxException  {
		WhereClauseTokenType tType = token.getType();
		
		switch (tType) {
		case FieldName:
		case Literal:
			_currentTuple.pushOperand(token);
			break;
			
		case RelationalOperator:
			if (_currentTuple.getRelationalOperator() == null) {
				_currentTuple.setRelationalOperator(token);
			} else {
				throw new InvalidSyntaxException(token.getToken() + " is the second relation opertor encountered for a tuple, only one per tuple allowed.");
			}
			break;
		
		case LogicalOperator: 
		case Parenthesis:
			if (_currentTuple.isComplete() || _currentTuple.isEmpty()) {
				_operators.add(0, toLogicalOperator(token.getToken()));
				
				if (token.getToken().equals("(")) {
					_unmatchedParenCount++;
					_notParenCount++;
				} else if (token.getToken().equals(")")) {
					_unmatchedParenCount--;
					_notParenCount++;
				} else if (token.getToken().equals("NOT")) {
					_notParenCount++;
				}
			}
			else {
				throw new InvalidSyntaxException(token.getToken() + " detected before a tuple was complete");
			}
			break;
			
		case Whitespace:
			//Ignore whitespace, i.e. do nothing
			break;	
			
		default:
			throw new InvalidSyntaxException(token.getToken() + " is an unknow token type");
		}		
		
		if (_currentTuple.isComplete()) {
			_tuples.add(0, _currentTuple);
			_currentTuple = new Tuple();
		}			
	}
	
	public LogicalOperator toLogicalOperator(String s) throws InvalidSyntaxException {
		LogicalOperator op;
		
		if (s.equalsIgnoreCase("AND")) {
			op = LogicalOperator.AND;
		} else if (s.equalsIgnoreCase("OR")) {
			op = LogicalOperator.OR;
		} else if (s.equalsIgnoreCase("NOT")) {
			op = LogicalOperator.NOT;
		} else if (s.equalsIgnoreCase("IN")) {
			op = LogicalOperator.IN;
		} else if (s.equalsIgnoreCase("(")) {
			op = LogicalOperator.LeftParen;
		} else if (s.equalsIgnoreCase(")")) {
			op = LogicalOperator.RightParen;
		} else {
			throw new InvalidSyntaxException("Logical Operator not an expected operator");
		}
		
		return op;
	}

	public List<LogicalOperator> getOperators() {
		return _operators;
	}
	
	public List<Tuple> getCurrentTuple() {
		return _tuples;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -