Search in sources :

Example 11 with OtpErlangBinary

use of com.ericsson.otp.erlang.OtpErlangBinary in project erlide_eclipse by erlang.

the class Util method stringValue.

/**
 * Get the string value of an Erlang string, empty if empty list
 *
 * @param o
 *            Erlang string or list
 * @return string value
 */
public static String stringValue(final OtpErlangObject o) {
    if (o instanceof OtpErlangString) {
        final OtpErlangString s = (OtpErlangString) o;
        return s.stringValue();
    } else if (o instanceof OtpErlangList) {
        final OtpErlangList l = (OtpErlangList) o;
        if (l.arity() == 0) {
            return "";
        }
        try {
            return l.stringValue();
        } catch (final OtpErlangException e) {
            ErlLogger.error(e);
            return null;
        }
    } else if (o instanceof OtpErlangBinary) {
        final OtpErlangBinary b = (OtpErlangBinary) o;
        String result;
        result = Util.decode(b.binaryValue(), Charsets.UTF_8);
        if (result == null) {
            result = Util.decode(b.binaryValue(), Charsets.ISO_8859_1);
        }
        if (result == null) {
            ErlLogger.error("bad binary value in stringValue" + " (can't decode): " + o);
        }
        return result;
    }
    // ErlLogger.warn("bad value in stringValue: " + o);
    return null;
}
Also used : OtpErlangList(com.ericsson.otp.erlang.OtpErlangList) OtpErlangException(com.ericsson.otp.erlang.OtpErlangException) OtpErlangString(com.ericsson.otp.erlang.OtpErlangString) OtpErlangBinary(com.ericsson.otp.erlang.OtpErlangBinary) OtpErlangString(com.ericsson.otp.erlang.OtpErlangString)

Example 12 with OtpErlangBinary

use of com.ericsson.otp.erlang.OtpErlangBinary in project erlide_eclipse by erlang.

the class Util method ioListToStringBuilder.

private static StringBuilder ioListToStringBuilder(final OtpErlangObject o, final StringBuilder sb0, final int maxLength) {
    StringBuilder sb = sb0;
    if (sb.length() >= maxLength) {
        return sb;
    }
    if (o instanceof OtpErlangLong) {
        final OtpErlangLong l = (OtpErlangLong) o;
        try {
            sb.append(l.charValue());
        } catch (final OtpErlangRangeException e) {
        }
    } else if (o instanceof OtpErlangString) {
        final OtpErlangString s = (OtpErlangString) o;
        sb.append(s.stringValue());
    } else if (o instanceof OtpErlangList) {
        final OtpErlangList l = (OtpErlangList) o;
        for (final OtpErlangObject i : l) {
            if (sb.length() < maxLength) {
                Util.ioListToStringBuilder(i, sb, maxLength);
            }
        }
        if (sb.length() < maxLength) {
            Util.ioListToStringBuilder(l.getLastTail(), sb, maxLength);
        }
    } else if (o instanceof OtpErlangBinary) {
        final OtpErlangBinary b = (OtpErlangBinary) o;
        String s = Util.decode(b.binaryValue(), Charsets.UTF_8);
        if (s == null) {
            s = new String(b.binaryValue(), Charsets.ISO_8859_1);
        }
        sb.append(s);
    } else if (o != null) {
        sb.append(o.toString());
    }
    if (sb.length() > maxLength) {
        sb = new StringBuilder(sb.substring(0, maxLength));
        sb.append("... <truncated>");
    }
    return sb;
}
Also used : OtpErlangLong(com.ericsson.otp.erlang.OtpErlangLong) OtpErlangList(com.ericsson.otp.erlang.OtpErlangList) OtpErlangObject(com.ericsson.otp.erlang.OtpErlangObject) OtpErlangRangeException(com.ericsson.otp.erlang.OtpErlangRangeException) OtpErlangString(com.ericsson.otp.erlang.OtpErlangString) OtpErlangBinary(com.ericsson.otp.erlang.OtpErlangBinary) OtpErlangString(com.ericsson.otp.erlang.OtpErlangString)

Example 13 with OtpErlangBinary

use of com.ericsson.otp.erlang.OtpErlangBinary in project erlide_eclipse by erlang.

the class IndexedErlangValue method getElementAt.

protected OtpErlangObject getElementAt(final int index) {
    if (value instanceof OtpErlangTuple) {
        final OtpErlangTuple t = (OtpErlangTuple) value;
        final int ofs = record != null ? 1 : 0;
        return t.elementAt(index + ofs);
    } else if (value instanceof OtpErlangList) {
        final OtpErlangList l = (OtpErlangList) value;
        return l.elementAt(index);
    } else if (value instanceof OtpErlangBinary) {
        final OtpErlangBinary bs = (OtpErlangBinary) value;
        int j = bs.binaryValue()[index];
        if (j < 0) {
            j += 256;
        }
        return new OtpErlangLong(j);
    } else if (list != null) {
        return list.elementAt(index);
    }
    return null;
}
Also used : OtpErlangLong(com.ericsson.otp.erlang.OtpErlangLong) OtpErlangList(com.ericsson.otp.erlang.OtpErlangList) OtpErlangTuple(com.ericsson.otp.erlang.OtpErlangTuple) OtpErlangBinary(com.ericsson.otp.erlang.OtpErlangBinary)

Aggregations

OtpErlangBinary (com.ericsson.otp.erlang.OtpErlangBinary)13 OtpErlangObject (com.ericsson.otp.erlang.OtpErlangObject)8 OtpErlangString (com.ericsson.otp.erlang.OtpErlangString)8 OtpErlangList (com.ericsson.otp.erlang.OtpErlangList)5 OtpErlangTuple (com.ericsson.otp.erlang.OtpErlangTuple)5 OtpErlangAtom (com.ericsson.otp.erlang.OtpErlangAtom)4 OtpErlangLong (com.ericsson.otp.erlang.OtpErlangLong)4 Test (org.junit.Test)3 RpcException (org.erlide.runtime.rpc.RpcException)2 OtpErlangChar (com.ericsson.otp.erlang.OtpErlangChar)1 OtpErlangException (com.ericsson.otp.erlang.OtpErlangException)1 OtpErlangMap (com.ericsson.otp.erlang.OtpErlangMap)1 OtpErlangPid (com.ericsson.otp.erlang.OtpErlangPid)1 OtpErlangRangeException (com.ericsson.otp.erlang.OtpErlangRangeException)1 OtpErlangRef (com.ericsson.otp.erlang.OtpErlangRef)1 Subscribe (com.google.common.eventbus.Subscribe)1 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1