com.rabbitmq.jms.parse.sql.SqlToken Maven / Gradle / Ivy
/* Copyright (c) 2013 Pivotal Software, Inc. All rights reserved. */ package com.rabbitmq.jms.parse.sql; import java.util.List; /** * Tokens are lexically extracted from SQL selector expressions. * A token has a type {@link SqlTokenType} and a value. The value can be either a {@link String} or a list of {@link String}s. */ class SqlToken { private final SqlTokenType tokType; private final String tokValue; private final List
matches the pattern: *tokValueList; SqlToken(SqlTokenType tokType, String tokValue) { this(tokType, tokValue, null); } SqlToken(SqlTokenType tokType, List tokValueList) { this(tokType, null, tokValueList); if (tokType.valueType() != SqlTokenValueType.LIST) throw new IllegalArgumentException("type not a LIST"); } private SqlToken(SqlTokenType tokType, String tokValue, List tokValueList) { this.tokType = tokType; this.tokValue = tokValue; this.tokValueList = tokValueList; } SqlTokenType type() { return this.tokType; } List getList() { return this.tokValueList; } double getFloat() { if (this.tokType.valueType() == SqlTokenValueType.FLOAT) return(stringToFloat(this.tokValue)); return 0.0F; } long getLong() { if (this.tokType.valueType() == SqlTokenValueType.LONG) return(Long.valueOf(this.tokValue)); return 0L; } long getHex() { if (this.tokType.valueType() == SqlTokenValueType.HEX) return(hexToLong(this.tokValue)); return 0L; } String getString() { if (this.tokType.valueType() == SqlTokenValueType.STRING) return(unEscape(this.tokValue)); return ""; } String getIdent() { if (this.tokType.valueType() == SqlTokenValueType.IDENT) return(String.valueOf(this.tokValue)); return ""; } public String toString() { StringBuilder sb = new StringBuilder(this.tokType.opCode()); try { switch (this.tokType.valueType()) { case FLOAT: sb.append(": ").append(getFloat()); break; case HEX: sb.append(": ").append(getHex()); break; case IDENT: sb.append(": ").append(getIdent()); break; case LONG: sb.append(": ").append(getLong()); break; case STRING: sb.append(": ").append(this.tokValue); break; case LIST: sb.append(": ").append(this.tokValueList); break; default: break; } } catch (Exception e) { sb.append("invalid(").append(this.tokValue).append(')'); } return sb.toString(); } private static final String unEscape(String s) { final int len = s.length()-1; StringBuilder sb = new StringBuilder(); for (int i=1; i s "0x[0-9a-fA-F]+"
. * * @param s - hex token string to convert to a long integer * @return the long integer equal in value to the hex token */ private static final long hexToLong(String s) { final int len = s.length(); long result = 0L; for (int i=2; i