dap4/d4cdm/src/main/java/dap4/cdm/CDMTypeFcns.java
static public Object
    attributeParse(DataType cdmtype, EnumTypedef en, Object o)
    {
        String so = o.toString();
        if(en != null) {
            switch (cdmtype) {
            case ENUM1:
            case ENUM2:
            case ENUM4:
                if(!(o instanceof Integer))
                    throw new ConversionException(o.toString());
                int eval = (Integer) o;
                String econst = en.lookupEnumString(eval);
                if(econst == null)
                    throw new ConversionException(o.toString());
                return econst;
            default:
                throw new ConversionException(o.toString());
            }
        }
        long lval = 0;
        double dval = 0.0;
        boolean islong = true;
        boolean isdouble = true;
        // Do a quick conversion checks
        try {
            lval = Long.parseLong(so);
        } catch (NumberFormatException nfe) {
            islong = false;
        }
        try {
            dval = Double.parseDouble(so);
        } catch (NumberFormatException nfe) {
            isdouble = false;
        }
        o = null; // default is not convertible
        switch (cdmtype) {
        case BOOLEAN:
            if(so.equalsIgnoreCase("false")
                    || (islong && lval == 0))
                o = Boolean.FALSE;
            else
                o = Boolean.TRUE;
            break;
        case BYTE:
            if(islong) o = Byte.valueOf((byte) lval);
            break;
        case SHORT:
            if(islong) o = Short.valueOf((short) lval);
            break;
        case INT:
            if(islong) o = Integer.valueOf((int) lval);
            break;
        case LONG:
            if(islong) o = Long.valueOf(lval);
            break;
        case UBYTE:  // Keep the proper bit pattern
            if(islong) o = Byte.valueOf((byte) (lval & 0xFFL));
            break;
        case USHORT:
            if(islong) o = Short.valueOf((short) (lval & 0xFFFFL));
            break;
        case UINT:
            if(islong) o = Integer.valueOf((int) (lval & 0xFFFFFFFFL));
            break;
        case ULONG:  //Need to resort to BigInteger
            BigInteger bi = new BigInteger(so);
            bi = bi.and(LONGMASK);
            o = (Long) bi.longValue();
            break;
        case FLOAT:
            if(islong && !isdouble) {
                dval = (double) lval;
                isdouble = true;
            }
            if(isdouble) o = (Float) ((float) dval);
            break;
        case DOUBLE:
            if(islong && !isdouble) {
                dval = (double) lval;
                isdouble = true;
            }
            if(isdouble) o = (Double) (dval);
            break;
        case STRING:
            return so;
        case OPAQUE:  // Big Integer then ByteBuffer
            if(so.startsWith("0x") || so.startsWith("0X"))
                so = so.substring(2);
            bi = new BigInteger(so, 16);
            // Now extract bytes
            byte[] bb = bi.toByteArray();
            o = ByteBuffer.wrap(bb);
            break;
        default:
            throw new ConversionException(o.toString());
        }
        if(o == null)
            throw new ConversionException(o.toString());
        return o;
    }
static public Object
    attributeParse(DataType cdmtype, EnumTypedef en, Object o)
    {
        String so = o.toString();
        if(en != null) {
            switch (cdmtype) {
            case ENUM1:
            case ENUM2:
            case ENUM4:
                if(!(o instanceof Integer))
                    throw new ConversionException(o.toString());
                int eval = (Integer) o;
                String econst = en.lookupEnumString(eval);
                if(econst == null)
                    throw new ConversionException(o.toString());
                return econst;
            default:
                throw new ConversionException(o.toString());
            }
        }
        long lval = 0;
        double dval = 0.0;
        boolean islong = true;
        boolean isdouble = true;
        // Do a quick conversion checks
        try {
            lval = Long.parseLong(so);
        } catch (NumberFormatException nfe) {
            islong = false;
        }
        try {
            dval = Double.parseDouble(so);
        } catch (NumberFormatException nfe) {
            isdouble = false;
        }
        o = null; // default is not convertible
        switch (cdmtype) {
        case BOOLEAN:
            if(so.equalsIgnoreCase("false")
                    || (islong && lval == 0))
                o = Boolean.FALSE;
            else
                o = Boolean.TRUE;
            break;
        case BYTE:
            if(islong) o = Byte.valueOf((byte) lval);
            break;
        case SHORT:
            if(islong) o = Short.valueOf((short) lval);
            break;
        case INT:
            if(islong) o = Integer.valueOf((int) lval);
            break;
        case LONG:
            if(islong) o = Long.valueOf(lval);
            break;
        case UBYTE:  // Keep the proper bit pattern
            if(islong) o = Byte.valueOf((byte) (lval & 0xFFL));
            break;
        case USHORT:
            if(islong) o = Short.valueOf((short) (lval & 0xFFFFL));
            break;
        case UINT:
            if(islong) o = Integer.valueOf((int) (lval & 0xFFFFFFFFL));
            break;
        case ULONG:  //Need to resort to BigInteger
            BigInteger bi = new BigInteger(so);
            bi = bi.and(LONGMASK);
            o = (Long) bi.longValue();
            break;
        case FLOAT:
            if(islong && !isdouble) {
                dval = (double) lval;
                isdouble = true;
            }
            if(isdouble) o = (Float) ((float) dval);
            break;
        case DOUBLE:
            if(islong && !isdouble) {
                dval = (double) lval;
                isdouble = true;
            }
            if(isdouble) o = (Double) (dval);
            break;
        case STRING:
            return so;
        case OPAQUE:  // Big Integer then ByteBuffer
            if(so.startsWith("0x") || so.startsWith("0X"))
                so = so.substring(2);
            bi = new BigInteger(so, 16);
            // Now extract bytes
            byte[] bb = bi.toByteArray();
            o = ByteBuffer.wrap(bb);
            break;
        default:
            throw new ConversionException(o.toString());
        }
        if(o == null)
            throw new ConversionException(o.toString());
        return o;
    }
static public Object attributeParse ( DataType cdmtype , EnumTypedef en , Object o ) { String so = o . toString ( ) ; if ( en != null ) { switch ( cdmtype ) { case ENUM1 : case ENUM2 : case ENUM4 : if ( ! ( o instanceof Integer ) ) throw new ConversionException ( o . toString ( ) ) ; int eval = ( Integer ) o ; String econst = en . lookupEnumString ( eval ) ; if ( econst == null ) throw new ConversionException ( o . toString ( ) ) ; return econst ; default : throw new ConversionException ( o . toString ( ) ) ; } } long lval = 0 ; double dval = 0.0 ; boolean islong = true ; boolean isdouble = true ; // Do a quick conversion checks try { lval = Long . parseLong ( so ) ; } catch ( NumberFormatException nfe ) { islong = false ; } try { dval = Double . parseDouble ( so ) ; } catch ( NumberFormatException nfe ) { isdouble = false ; } o = null ; // default is not convertible switch ( cdmtype ) { case BOOLEAN : if ( so . equalsIgnoreCase ( "false" ) || ( islong && lval == 0 ) ) o = Boolean . FALSE ; else o = Boolean . TRUE ; break ; case BYTE : if ( islong ) o = Byte . valueOf ( ( byte ) lval ) ; break ; case SHORT : if ( islong ) o = Short . valueOf ( ( short ) lval ) ; break ; case INT : if ( islong ) o = Integer . valueOf ( ( int ) lval ) ; break ; case LONG : if ( islong ) o = Long . valueOf ( lval ) ; break ; case UBYTE : // Keep the proper bit pattern if ( islong ) o = Byte . valueOf ( ( byte ) ( lval & 0xFF L ) ) ; break ; case USHORT : if ( islong ) o = Short . valueOf ( ( short ) ( lval & 0xFFFF L ) ) ; break ; case UINT : if ( islong ) o = Integer . valueOf ( ( int ) ( lval & 0xFFFFFFFF L ) ) ; break ; case ULONG : //Need to resort to BigInteger BigInteger bi = new BigInteger ( so ) ; bi = bi . and ( LONGMASK ) ; o = ( Long ) bi . longValue ( ) ; break ; case FLOAT : if ( islong && ! isdouble ) { dval = ( double ) lval ; isdouble = true ; } if ( isdouble ) o = ( Float ) ( ( float ) dval ) ; break ; case DOUBLE : if ( islong && ! isdouble ) { dval = ( double ) lval ; isdouble = true ; } if ( isdouble ) o = ( Double ) ( dval ) ; break ; case STRING : return so ; case OPAQUE : // Big Integer then ByteBuffer if ( so . startsWith ( "0x" ) || so . startsWith ( "0X" ) ) so = so . substring ( 2 ) ; bi = new BigInteger ( so , 16 ) ; // Now extract bytes byte [ ] bb = bi . toByteArray ( ) ; o = ByteBuffer . wrap ( bb ) ; break ; default : throw new ConversionException ( o . toString ( ) ) ; } if ( o == null ) throw new ConversionException ( o . toString ( ) ) ; return o ; }
convert a string to a specified cdmtype
Note that if en is defined, then we attempt
to convert the string as enum const

@param cdmtype
@param en
@param o
@return
convert a string to a specified cdmtype Note that if en is defined then we attempt to convert the string as enum const