All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.rabbitmq.jms.parse.sql.SqlToken Maven / Gradle / Ivy

There is a newer version: 3.4.0
Show newest version
/* Copyright (c) 2013-2020 VMware, Inc. or its affiliates. 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 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; is matches the pattern:
     * "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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy