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

📄 propcodes.java

📁 Open DMT GPS server source code
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
        private int     key        = 0;     // property code        private long    type       = 0L;    // bitmask        private int     arrayCount = 0;        private String  descript   = null;        public Attr(int key, long type, int array, String desc) {            this.key        = key;            this.type       = type;            this.arrayCount = (array > 0)? array : 1;            this.descript   = desc;        }                /* return attribute key (property code) */        public int getKey() {            return this.key;        }                /* return property type */        public long getType() {            return this.type;        }                /* return true if this property is a command */        public boolean isCommand() {            return ((this.type & TYPE_TYPE_MASK) == TYPE_COMMAND);        }                /* return true if this property is numeric */        public boolean isNumeric() {            return ((this.type & TYPE_TYPE_MASK) == TYPE_NUMERIC);        }                /* return true if this property is a decimal numeric */        public boolean isDecimal() {            return this.isNumeric() && ((this.type & TYPE_DEC) != 0);        }                /* return true if this property is a string */        public boolean isString() {            return ((this.type & TYPE_TYPE_MASK) == TYPE_STRING);        }                /* return true if this property is a binary value */        public boolean isBinary() {            return ((this.type & TYPE_TYPE_MASK) == TYPE_BINARY);        }                /* return true if this property is a boolean */        public boolean isBoolean() {            return ((this.type & TYPE_TYPE_MASK) == TYPE_BOOLEAN);        }                /* return true if this property is a GPS value */        public boolean isGPS() {            return ((this.type & TYPE_TYPE_MASK) == TYPE_GPS);        }                /* return the number of bytes per element (numeric only, all else returns '1') */        public int getTypeLength() {            int t = (int)(this.type & TYPE_TYPE_MASK);            switch (t) {                case TYPE_COMMAND:                    return -1;  // invalid                case TYPE_STRING:                    return 1;   // '1' null-terminated String                case TYPE_BINARY:                    return 1;   // '1' byte array                case TYPE_GPS:                    return 1;   // '1' GPS fix                case TYPE_BOOLEAN:                    return 1;   // '1' byte                case TYPE_NUMERIC:                    return (int)(this.type & TYPE_SIZE_MASK);            }            return -1;        }                /* return the number of array elements this property holds */        public int getArrayLength() {            return this.arrayCount;        }                /* return the description */        public String getDescription() {            return (this.descript != null)? this.descript : "";        }                /* parse the specified string value into this properties element types */        public Object[] parseString(String value) {            if ((value == null) || this.isCommand()) {                Print.logError("Invalid value or property: 0x" + StringTools.toHexString(this.getKey()));                return null;            }            int arrayLen = this.getArrayLength();            String val[] = null;            if (arrayLen == 1) {                val = new String[] { value };            } else {                val = StringTools.parseArray(value);                if (val.length != arrayLen) {                    Print.logError("Invalid array length [expected " + arrayLen + ", found=" + val.length + "]");                    return null;                }            }            Object obj[] = null;            switch ((int)(this.getType() & TYPE_TYPE_MASK)) {                case TYPE_STRING:                    obj = new Object[] { val[0] };                    break;                case TYPE_BINARY:                    if (StringTools.isHex(val[0],true)) {                        obj = new Object[] { StringTools.parseHex(val[0],new byte[0]) };                    }                    break;                case TYPE_GPS:                    if (val[0].indexOf(GeoPoint.PointSeparator) > 0) { // <-- not a definitive test                        obj = new Object[] { new GeoPoint(val[0]) };                    }                    break;                case TYPE_BOOLEAN:                    obj = new Object[arrayLen];                    for (int i = 0; i < arrayLen; i++) {                        String v = val[i].trim();                        if (StringTools.isBoolean(v,true)) {                            obj[i] = new Boolean(StringTools.parseBoolean(v,false));                        } else {                            Print.logError("Invalid boolean value: " + v);                            obj = null;                            break;                        }                        //Print.logInfo("Parsed " + obj[i]);                    }                    break;                case TYPE_NUMERIC:                    obj = new Object[arrayLen];                    for (int i = 0; i < arrayLen; i++) {                        String v = val[i].trim();                        if (this.isDecimal()) {                            if (StringTools.isDouble(v,true)) {                                obj[i] = new Double(StringTools.parseDouble(v,0.0));                            } else {                                Print.logError("Invalid decimal value: " + v);                                obj = null;                                break;                            }                        } else {                            if (StringTools.isLong(v,true)) {                                obj[i] = new Long(StringTools.parseLong(v,0L));                            } else {                                Print.logError("Invalid integer value: " + v);                                obj = null;                                break;                            }                        }                        //Print.logInfo("Parsed " + obj[i]);                    }                    break;                case TYPE_COMMAND:                    //??                    break;            }            return obj;        }                /* return String representation */        public String toString() {            StringBuffer sb = new StringBuffer();            sb.append(this.getDescription());            sb.append(" [0x").append(StringTools.toHexString(this.getKey(),16)).append("]");            sb.append(" type=0x").append(StringTools.toHexString(this.getType(),16));            sb.append(" array=").append(this.getArrayLength());            return sb.toString();        }            }        // ------------------------------------------------------------------------    // ------------------------------------------------------------------------    // ------------------------------------------------------------------------        /* return true if the specified code refers to a valid property */    public static boolean isValidPropertyCode(int key)    {        return (PropCodes.getAttr(key) != null);    }        // ------------------------------------------------------------------------    /* return the description of the specified property */    public static String getPropertyDescription(int key)    {        Attr attr = PropCodes.getAttr(key);        return (attr != null)? attr.getDescription() : null;    }        // ------------------------------------------------------------------------    /* parse string into specified property types */    public static Object[] parsePropertyValue(int key, String value)    {                /* nothing to parse? */        if (value == null) {            Print.logError("Null value specified");            return null;        }        /* get property attributes */        PropCodes.Attr attr = PropCodes.getAttr(key);        if (attr == null) {            Print.logError("Property not found: 0x" + StringTools.toHexString(key));            return null;        }                /* return parsed type */        return attr.parseString(value);    }    // ------------------------------------------------------------------------    /* encode set-property longs */    public static byte[] encodePropertyData(int key, Object value[])    {                /* nothing to encode? */        if ((value == null) || (value.length == 0)) {            Print.logError("Null/empty value specified");            return null;        }        /* get property attributes */        PropCodes.Attr attr = PropCodes.getAttr(key);        if (attr == null) {            Print.logError("Property not found: 0x" + StringTools.toHexString(key));            return null;        }                /* proper array length? */        int arrayLen = attr.getArrayLength();        if (arrayLen > value.length) {            Print.logError("Invalid array length [expected " + arrayLen + "]");            return null;        }        /* data buffer */        byte data[] = null;        /* encode data */        if (attr.isString()) {            if (value[0] instanceof String) {                byte vs[] = StringTools.getBytes((String)value[0]);                int vsLen = (vs.length <= 253)? vs.length : 253;                data = new byte[vsLen];                System.arraycopy(vs, 0, data, 0, vsLen);            }        } else        if (attr.isBinary()) {            if (value[0] instanceof byte[]) {                byte vb[] = (byte[])value[0];                int vbLen = (vb.length <= 253)? vb.length : 253;                data = new byte[vbLen];                System.arraycopy(vb, 0, data, 0, vbLen);            }        } else        if (attr.isGPS()) {            if (value[0] instanceof GeoPoint) {                GeoPoint gp = (GeoPoint)value[0];                data = new byte[GeoPoint.ENCODE_HIRES_LEN];                GeoPoint.encodeGeoPoint(gp, data, 0, data.length);            }        } else        if (attr.isBoolean()) {            int elemLen = attr.getTypeLength(), ofs = 0;            data = new byte[elemLen * arrayLen];            for (int i = 0; i < arrayLen; i++) {                long val = 0L;                if (value[i] instanceof Boolean) {                    val = ((Boolean)value[i]).booleanValue()? 1L : 0L;                } else                if (value[i] instanceof Number) {                    val = (((Number)value[i]).longValue() != 0L)? 1L : 0L;                } else {                    // only boolean/numeric object types are allowed                    data = null;                    break;                }                int len = Payload.encodeLong(data, ofs, elemLen, true, val);

⌨️ 快捷键说明

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