Search in sources :

Example 31 with IonException

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

the class IonWriterSystemBinary method writeAllBufferedData.

/**
 * Empty our buffers, assuming it is safe to do so.
 * This is called by {@link #flush()} and {@link #finish()}.
 */
private void writeAllBufferedData() throws IOException {
    writeBytes(_user_output_stream);
    clearFieldName();
    clearAnnotations();
    _in_struct = false;
    _patch_count = 0;
    _patch_symbol_table_count = 0;
    _top = 0;
    try {
        _writer.setPosition(0);
        _writer.truncate();
    } catch (IOException e) {
        throw new IonException(e);
    }
}
Also used : IonException(com.amazon.ion.IonException) IOException(java.io.IOException)

Example 32 with IonException

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

the class IonWriterSystemBinary method getContainer.

protected final IonType getContainer() {
    IonType type;
    int tid = parentType();
    switch(tid) {
        case tidList:
            type = IonType.LIST;
            break;
        case tidSexp:
            type = IonType.SEXP;
            break;
        case tidStruct:
            type = IonType.STRUCT;
            break;
        case tidDATAGRAM:
            type = IonType.DATAGRAM;
            break;
        default:
            throw new IonException("unexpected parent type " + tid + " does not represent a container");
    }
    return type;
}
Also used : IonType(com.amazon.ion.IonType) IonException(com.amazon.ion.IonException)

Example 33 with IonException

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

the class IonWriterSystemTree method append.

private void append(IonValue value) {
    try {
        super.startValue();
    } catch (IOException e) {
        // Shouldn't happen
        throw new IonException(e);
    }
    if (hasAnnotations()) {
        SymbolToken[] annotations = getTypeAnnotationSymbols();
        // TODO this makes an extra copy of the array
        ((_Private_IonValue) value).setTypeAnnotationSymbols(annotations);
        this.clearAnnotations();
    }
    if (_in_struct) {
        SymbolToken sym = assumeFieldNameSymbol();
        IonStruct struct = (IonStruct) _current_parent;
        struct.add(sym, value);
        this.clearFieldName();
    } else {
        ((IonSequence) _current_parent).add(value);
    }
}
Also used : IonStruct(com.amazon.ion.IonStruct) SymbolToken(com.amazon.ion.SymbolToken) IonException(com.amazon.ion.IonException) IonSequence(com.amazon.ion.IonSequence) IOException(java.io.IOException)

Example 34 with IonException

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

the class LocalSymbolTable method readLocalSymbolTable.

/**
 * Parses the symbol table at the reader's current position.
 * @param reader the reader from which to parse the symbol table.
 * @param catalog the catalog from which to resolve shared symbol table imports.
 * @param isOnStruct true if the reader is already positioned on the symbol table struct; otherwise, false.
 * @param symbolsListOut list into which local symbols declared by the parsed symbol table will be deposited.
 * @param currentSymbolTable the symbol table currently active in the stream.
 * @return a new LocalSymbolTableImports instance, or null if this was an LST append. If null, `currentSymbolTable`
 *   continues to be the active symbol table.
 */
protected static LocalSymbolTableImports readLocalSymbolTable(IonReader reader, IonCatalog catalog, boolean isOnStruct, List<String> symbolsListOut, SymbolTable currentSymbolTable) {
    if (!isOnStruct) {
        reader.next();
    }
    assert reader.getType() == IonType.STRUCT : "invalid symbol table image passed in reader " + reader.getType() + " encountered when a struct was expected";
    assert ION_SYMBOL_TABLE.equals(reader.getTypeAnnotations()[0]) : "local symbol tables must be annotated by " + ION_SYMBOL_TABLE;
    reader.stepIn();
    List<SymbolTable> importsList = new ArrayList<SymbolTable>();
    importsList.add(reader.getSymbolTable().getSystemSymbolTable());
    IonType fieldType;
    boolean foundImportList = false;
    boolean foundLocalSymbolList = false;
    boolean isAppend = false;
    while ((fieldType = reader.next()) != null) {
        if (reader.isNullValue())
            continue;
        SymbolToken symTok = reader.getFieldNameSymbol();
        int sid = symTok.getSid();
        if (sid == SymbolTable.UNKNOWN_SYMBOL_ID) {
            // This is a user-defined IonReader or a pure DOM, fall
            // back to text
            final String fieldName = reader.getFieldName();
            sid = getSidForSymbolTableField(fieldName);
        }
        // different SID!
        switch(sid) {
            case SYMBOLS_SID:
                {
                    // empty lists
                    if (foundLocalSymbolList) {
                        throw new IonException("Multiple symbol fields found within a single local symbol table.");
                    }
                    foundLocalSymbolList = true;
                    if (fieldType == IonType.LIST) {
                        reader.stepIn();
                        IonType type;
                        while ((type = reader.next()) != null) {
                            final String text;
                            if (type == IonType.STRING) {
                                text = reader.stringValue();
                            } else {
                                text = null;
                            }
                            symbolsListOut.add(text);
                        }
                        reader.stepOut();
                    }
                    break;
                }
            case IMPORTS_SID:
                {
                    if (foundImportList) {
                        throw new IonException("Multiple imports fields found within a single local symbol table.");
                    }
                    foundImportList = true;
                    if (fieldType == IonType.LIST) {
                        prepImportsList(importsList, reader, catalog);
                    } else if (fieldType == IonType.SYMBOL && ION_SYMBOL_TABLE.equals(reader.stringValue())) {
                        isAppend = true;
                    }
                    break;
                }
            default:
                {
                    // As per the Spec, any other field is ignored
                    break;
                }
        }
    }
    reader.stepOut();
    if (isAppend && currentSymbolTable.isLocalTable()) {
        // Because the current symbol table is a local symbol table (i.e. not the system symbol table), it can
        // be appended in-place.
        LocalSymbolTable currentLocalSymbolTable = (LocalSymbolTable) currentSymbolTable;
        for (String newSymbol : symbolsListOut) {
            currentLocalSymbolTable.putSymbol(newSymbol);
        }
        return null;
    }
    return new LocalSymbolTableImports(importsList);
}
Also used : IonType(com.amazon.ion.IonType) SymbolToken(com.amazon.ion.SymbolToken) IonException(com.amazon.ion.IonException) ArrayList(java.util.ArrayList) SymbolTable(com.amazon.ion.SymbolTable)

Example 35 with IonException

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

the class SharedSymbolTable method newSharedSymbolTable.

/**
 * Constructs a new shared symbol table represented by the current value
 * of the passed in {@link IonReader}.
 *
 * @param reader
 *          the {@link IonReader} positioned on the shared symbol table
 *          represented as an {@link IonStruct}
 * @param isOnStruct
 *          denotes whether the {@link IonReader} is already positioned on
 *          the struct; false if it is positioned before the struct
 * @return
 */
static SymbolTable newSharedSymbolTable(IonReader reader, boolean isOnStruct) {
    if (!isOnStruct) {
        IonType t = reader.next();
        if (t != IonType.STRUCT) {
            throw new IonException("invalid symbol table image passed " + "into reader, " + t + " encountered when a " + "struct was expected");
        }
    }
    String name = null;
    int version = -1;
    List<String> symbolsList = new ArrayList<String>();
    reader.stepIn();
    IonType fieldType = null;
    while ((fieldType = reader.next()) != null) {
        if (reader.isNullValue())
            continue;
        SymbolToken symTok = reader.getFieldNameSymbol();
        int sid = symTok.getSid();
        if (sid == SymbolTable.UNKNOWN_SYMBOL_ID) {
            // This is a user-defined IonReader or a pure DOM, fall
            // back to text
            final String fieldName = reader.getFieldName();
            sid = getSidForSymbolTableField(fieldName);
        }
        // different SID!
        switch(sid) {
            case VERSION_SID:
                if (fieldType == IonType.INT) {
                    version = reader.intValue();
                }
                break;
            case NAME_SID:
                if (fieldType == IonType.STRING) {
                    name = reader.stringValue();
                }
                break;
            case SYMBOLS_SID:
                // empty lists
                if (fieldType == IonType.LIST) {
                    reader.stepIn();
                    {
                        IonType t;
                        while ((t = reader.next()) != null) {
                            String text = null;
                            if (t == IonType.STRING && !reader.isNullValue()) {
                                // As per the Spec, if any element of
                                // the list is the empty string or any
                                // other type, treat it as null
                                text = reader.stringValue();
                                if (text.length() == 0)
                                    text = null;
                            }
                            symbolsList.add(text);
                        }
                    }
                    reader.stepOut();
                }
                break;
            default:
                break;
        }
    }
    reader.stepOut();
    if (name == null || name.length() == 0) {
        String message = "shared symbol table is malformed: field 'name' " + "must be a non-empty string.";
        throw new IonException(message);
    }
    // As per the Spec, if 'version' field is missing or not at
    // least 1, treat it as 1.
    version = (version < 1) ? 1 : version;
    Map<String, Integer> symbolsMap = null;
    if (!symbolsList.isEmpty()) {
        symbolsMap = new HashMap<String, Integer>();
        transferNonExistingSymbols(symbolsList, symbolsMap);
    } else {
        // Empty Map is more efficient than an empty HashMap
        symbolsMap = Collections.emptyMap();
    }
    // We have all necessary data, pass it over to the private constructor.
    return new SharedSymbolTable(name, version, symbolsList, symbolsMap);
}
Also used : IonType(com.amazon.ion.IonType) SymbolToken(com.amazon.ion.SymbolToken) IonException(com.amazon.ion.IonException) ArrayList(java.util.ArrayList)

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