Search in sources :

Example 16 with IonException

use of com.amazon.ion.IonException in project ion-java by amzn.

the class IonSystemLite method load_value_helper.

private IonValueLite load_value_helper(IonReader reader, boolean isTopLevel) {
    boolean symbol_is_present = false;
    IonType t = reader.getType();
    if (t == null) {
        return null;
    }
    IonValueLite v;
    if (reader.isNullValue()) {
        v = newNull(t);
    } else {
        switch(t) {
            case BOOL:
                v = newBool(reader.booleanValue());
                break;
            case INT:
                // TODO amzn/ion-java/issues/9  Inefficient since we can't determine the size
                // of the integer in order to avoid making BigIntegers.
                v = newInt(reader.bigIntegerValue());
                break;
            case FLOAT:
                v = newFloat(reader.doubleValue());
                break;
            case DECIMAL:
                v = newDecimal(reader.decimalValue());
                break;
            case TIMESTAMP:
                v = newTimestamp(reader.timestampValue());
                break;
            case SYMBOL:
                v = newSymbol(reader.symbolValue());
                symbol_is_present = true;
                break;
            case STRING:
                v = newString(reader.stringValue());
                break;
            case CLOB:
                v = newClob(reader.newBytes());
                break;
            case BLOB:
                v = newBlob(reader.newBytes());
                break;
            case LIST:
                v = newEmptyList();
                break;
            case SEXP:
                v = newEmptySexp();
                break;
            case STRUCT:
                v = newEmptyStruct();
                break;
            default:
                throw new IonException("unexpected type encountered reading value: " + t.toString());
        }
    }
    // Forget any incoming SIDs on field names.
    if (!isTopLevel && reader.isInStruct()) {
        SymbolToken token = reader.getFieldNameSymbol();
        String text = token.getText();
        if (text != null && token.getSid() != UNKNOWN_SYMBOL_ID) {
            token = newSymbolToken(text, UNKNOWN_SYMBOL_ID);
        }
        v.setFieldNameSymbol(token);
        symbol_is_present = true;
    }
    // Forget any incoming SIDs on annotations.
    // This is a fresh array so we can modify it:
    SymbolToken[] annotations = reader.getTypeAnnotationSymbols();
    if (annotations.length != 0) {
        for (int i = 0; i < annotations.length; i++) {
            SymbolToken token = annotations[i];
            String text = token.getText();
            if (text != null && token.getSid() != UNKNOWN_SYMBOL_ID) {
                annotations[i] = newSymbolToken(text, UNKNOWN_SYMBOL_ID);
            }
        }
        v.setTypeAnnotationSymbols(annotations);
        symbol_is_present = true;
    }
    if (!reader.isNullValue()) {
        switch(t) {
            case BOOL:
            case INT:
            case FLOAT:
            case DECIMAL:
            case TIMESTAMP:
            case SYMBOL:
            case STRING:
            case CLOB:
            case BLOB:
                break;
            case LIST:
            case SEXP:
            case STRUCT:
                // fieldname and annotations off of the parent container
                if (load_children((IonContainerLite) v, reader)) {
                    symbol_is_present = true;
                }
                break;
            default:
                throw new IonException("unexpected type encountered reading value: " + t.toString());
        }
    }
    if (symbol_is_present) {
        v._isSymbolPresent(true);
    }
    return v;
}
Also used : IonType(com.amazon.ion.IonType) SymbolToken(com.amazon.ion.SymbolToken) com.amazon.ion.impl._Private_Utils.newSymbolToken(com.amazon.ion.impl._Private_Utils.newSymbolToken) IonException(com.amazon.ion.IonException) IonTextUtils.printString(com.amazon.ion.util.IonTextUtils.printString)

Example 17 with IonException

use of com.amazon.ion.IonException in project ion-java by amzn.

the class IonSystemLite method clone.

@SuppressWarnings("unchecked")
public <T extends IonValue> T clone(T value) throws IonException {
    // Use "fast clone" when the system is the same.
    if (value.getSystem() == this) {
        return (T) value.clone();
    }
    if (value instanceof IonDatagram) {
        IonDatagram datagram = newDatagram();
        IonWriter writer = _Private_IonWriterFactory.makeWriter(datagram);
        IonReader reader = makeSystemReader(value.getSystem(), value);
        try {
            writer.writeValues(reader);
        } catch (IOException e) {
            throw new IonException(e);
        }
        return (T) datagram;
    }
    IonReader reader = newReader(value);
    reader.next();
    return (T) newValue(reader);
}
Also used : IonDatagram(com.amazon.ion.IonDatagram) IonException(com.amazon.ion.IonException) IonReader(com.amazon.ion.IonReader) IonWriter(com.amazon.ion.IonWriter) IOException(java.io.IOException)

Example 18 with IonException

use of com.amazon.ion.IonException in project ion-java by amzn.

the class IonValueLite method writeTo.

final void writeTo(IonWriter writer, SymbolTableProvider symbolTableProvider) {
    if (writer.isInStruct() && !((_Private_IonWriter) writer).isFieldNameSet()) {
        SymbolToken tok = getFieldNameSymbol(symbolTableProvider);
        if (tok == null) {
            throw new IllegalStateException("Field name not set");
        }
        writer.setFieldNameSymbol(tok);
    }
    SymbolToken[] annotations = getTypeAnnotationSymbols(symbolTableProvider);
    writer.setTypeAnnotationSymbols(annotations);
    try {
        writeBodyTo(writer, symbolTableProvider);
    } catch (IOException e) {
        throw new IonException(e);
    }
}
Also used : SymbolToken(com.amazon.ion.SymbolToken) com.amazon.ion.impl._Private_Utils.newSymbolToken(com.amazon.ion.impl._Private_Utils.newSymbolToken) IonException(com.amazon.ion.IonException) com.amazon.ion.impl._Private_IonWriter(com.amazon.ion.impl._Private_IonWriter) IOException(java.io.IOException)

Example 19 with IonException

use of com.amazon.ion.IonException in project ion-java by amzn.

the class IonValueLite method toString.

public String toString(IonTextWriterBuilder writerBuilder) {
    StringBuilder buf = new StringBuilder(1024);
    try {
        IonWriter writer = writerBuilder.build(buf);
        writeTo(writer);
        writer.finish();
    } catch (IOException e) {
        throw new IonException(e);
    }
    return buf.toString();
}
Also used : IonException(com.amazon.ion.IonException) IonWriter(com.amazon.ion.IonWriter) com.amazon.ion.impl._Private_IonWriter(com.amazon.ion.impl._Private_IonWriter) IOException(java.io.IOException)

Example 20 with IonException

use of com.amazon.ion.IonException in project ion-java by amzn.

the class JarInfo method loadBuildProperties.

// TODO writeTo(IonWriter)
// ========================================================================
private void loadBuildProperties(List<Manifest> manifests) throws IonException {
    boolean propertiesLoaded = false;
    for (Manifest manifest : manifests) {
        boolean success = tryLoadBuildProperties(manifest);
        if (success && propertiesLoaded) {
            // In the event of conflicting manifests, fail instead of risking returning incorrect version info.
            throw new IonException("Found multiple manifests with ion-java version info on the classpath.");
        }
        propertiesLoaded |= success;
    }
    if (!propertiesLoaded) {
        throw new IonException("Unable to locate manifest with ion-java version info on the classpath.");
    }
}
Also used : IonException(com.amazon.ion.IonException) Manifest(java.util.jar.Manifest)

Aggregations

IonException (com.amazon.ion.IonException)66 IOException (java.io.IOException)31 IonReader (com.amazon.ion.IonReader)10 IonType (com.amazon.ion.IonType)9 SymbolToken (com.amazon.ion.SymbolToken)9 IonValue (com.amazon.ion.IonValue)7 Event (com.amazon.tools.events.Event)7 Test (org.junit.Test)6 IonStruct (com.amazon.ion.IonStruct)5 SymbolTable (com.amazon.ion.SymbolTable)5 SavePoint (com.amazon.ion.impl.UnifiedSavePointManagerX.SavePoint)5 InputStream (java.io.InputStream)5 ArrayList (java.util.ArrayList)5 EventType (com.amazon.tools.events.EventType)4 IonWriter (com.amazon.ion.IonWriter)3 IonDatagram (com.amazon.ion.IonDatagram)2 IonLob (com.amazon.ion.IonLob)2 Timestamp (com.amazon.ion.Timestamp)2 BufferManager (com.amazon.ion.impl.IonBinary.BufferManager)2 com.amazon.ion.impl._Private_IonWriter (com.amazon.ion.impl._Private_IonWriter)2