Search in sources :

Example 91 with SymbolTable

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

the class _Private_IonWriterBase method transfer_symbol_tables.

private final void transfer_symbol_tables(_Private_ReaderWriter reader) throws IOException {
    SymbolTable reader_symbols = reader.pop_passed_symbol_table();
    if (reader_symbols != null) {
        clear_system_value_stack();
        setSymbolTable(reader_symbols);
        while (reader_symbols != null) {
            // TODO these symtabs are never popped!
            // Why bother pushing them?
            push_symbol_table(reader_symbols);
            reader_symbols = reader.pop_passed_symbol_table();
        }
    }
}
Also used : SymbolTable(com.amazon.ion.SymbolTable)

Example 92 with SymbolTable

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

the class SymbolTableStructCache method makeIonRepresentation.

/**
 * Create a new IonStruct representation of the symbol table.
 * @param factory the {@link ValueFactory} from which to construct the IonStruct.
 */
private void makeIonRepresentation(ValueFactory factory) {
    image = factory.newEmptyStruct();
    image.addTypeAnnotation(ION_SYMBOL_TABLE);
    if (importedTables.length > 0) {
        // The system symbol table may be the first import. If it is, skip it.
        int i = importedTables[0].isSystemTable() ? 1 : 0;
        if (i < importedTables.length) {
            IonList importsList = factory.newEmptyList();
            while (i < importedTables.length) {
                SymbolTable importedTable = importedTables[i];
                IonStruct importStruct = factory.newEmptyStruct();
                importStruct.add(NAME, factory.newString(importedTable.getName()));
                importStruct.add(VERSION, factory.newInt(importedTable.getVersion()));
                importStruct.add(MAX_ID, factory.newInt(importedTable.getMaxId()));
                importsList.add(importStruct);
                i++;
            }
            image.add(IMPORTS, importsList);
        }
    }
    if (symbolTable.getMaxId() > symbolTable.getImportedMaxId()) {
        Iterator<String> localSymbolIterator = symbolTable.iterateDeclaredSymbolNames();
        int sid = symbolTable.getImportedMaxId() + 1;
        while (localSymbolIterator.hasNext()) {
            addSymbol(localSymbolIterator.next(), sid);
            sid++;
        }
    }
}
Also used : IonStruct(com.amazon.ion.IonStruct) IonList(com.amazon.ion.IonList) SymbolTable(com.amazon.ion.SymbolTable)

Example 93 with SymbolTable

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

the class _Private_IonBinaryWriterBuilder method fillLegacyDefaults.

/**
 * Fills all properties and returns an immutable builder.
 */
private _Private_IonBinaryWriterBuilder fillLegacyDefaults() {
    // amzn/ion-java/issues/59 Fix this to use the new writer or eliminate it
    // Ensure that we don't modify the user's builder.
    _Private_IonBinaryWriterBuilder b = copy();
    if (b.getSymtabValueFactory() == null) {
        IonSystem system = IonSystemBuilder.standard().build();
        b.setSymtabValueFactory(system);
    }
    SymbolTable initialSymtab = b.getInitialSymbolTable();
    if (initialSymtab == null) {
        initialSymtab = initialSymtab(LocalSymbolTable.DEFAULT_LST_FACTORY, _Private_Utils.systemSymtab(1), b.getImports());
        b.setInitialSymbolTable(initialSymtab);
    } else if (initialSymtab.isSystemTable()) {
        initialSymtab = initialSymtab(LocalSymbolTable.DEFAULT_LST_FACTORY, initialSymtab, b.getImports());
        b.setInitialSymbolTable(initialSymtab);
    }
    return b.immutable();
}
Also used : IonSystem(com.amazon.ion.IonSystem) SymbolTable(com.amazon.ion.SymbolTable)

Example 94 with SymbolTable

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

the class _Private_IonBinaryWriterBuilder method setInitialSymbolTable.

/**
 * Declares the symbol table to use for encoded data.
 * To avoid conflicts between different data streams, if the given instance
 * is mutable, it will be copied when {@code build()} is called.
 *
 * @param symtab must be a local or system symbol table.
 * May be null, in which case the initial symtab is that of
 * {@code $ion_1_0}.
 *
 * @throws SubstituteSymbolTableException
 * if any imported table is a substitute (see {@link SymbolTable}).
 */
@Override
public void setInitialSymbolTable(SymbolTable symtab) {
    mutationCheck();
    if (symtab != null) {
        if (symtab.isLocalTable()) {
            SymbolTable[] imports = ((LocalSymbolTable) symtab).getImportedTablesNoCopy();
            for (SymbolTable imported : imports) {
                if (imported.isSubstitute()) {
                    String message = "Cannot encode with substitute symbol table: " + imported.getName();
                    throw new SubstituteSymbolTableException(message);
                }
            }
        } else if (!symtab.isSystemTable()) {
            String message = "symtab must be local or system table";
            throw new IllegalArgumentException(message);
        }
    }
    myInitialSymbolTable = symtab;
    myBinaryWriterBuilder.withInitialSymbolTable(symtab);
}
Also used : SubstituteSymbolTableException(com.amazon.ion.SubstituteSymbolTableException) SymbolTable(com.amazon.ion.SymbolTable)

Example 95 with SymbolTable

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

the class IonWriterSystemBinary method writeRecursive.

int writeRecursive(BlockedByteInputStream datastream, OutputStream userstream, PatchedValues p) throws IOException {
    int totalSize = 0;
    for (int i = 0; i <= p._freePos; ++i) {
        int type = p._types[i];
        int pos = p._positions[i];
        int fnlen = (int) (p._lengths[i] >> 32);
        int vallen = (int) (p._lengths[i] & 0xFFFFFFFF);
        if (p.getParent() == null) {
            if (pos > totalSize) {
                // write whatever data that we have in the datastream (eg external data)
                datastream.writeTo(userstream, pos - totalSize);
                totalSize = pos;
            }
            totalSize += fnlen + vallen;
        }
        // write member name
        if (fnlen > 0) {
            datastream.writeTo(userstream, fnlen);
        }
        switch(type) {
            case TID_ANNOTATION_PATCH:
                IonBinary.writeVarUInt(userstream, vallen);
                datastream.writeTo(userstream, vallen);
                break;
            case TID_SYMBOL_TABLE_PATCH:
                SymbolTable symtab = p._symtabs.remove();
                if (!symtab.isSystemTable()) {
                    byte[] symtabBytes = reverseEncode(1024, symtab);
                    userstream.write(symtabBytes);
                    totalSize += symtabBytes.length;
                }
                break;
            case TID_RAW:
                datastream.writeTo(userstream, vallen);
                break;
            default:
                // write type
                if (vallen >= _Private_IonConstants.lnIsVarLen) {
                    int typeByte = (type << 4) | _Private_IonConstants.lnIsVarLen;
                    userstream.write(typeByte);
                    IonBinary.writeVarUInt(userstream, vallen);
                } else {
                    int typeByte = (type << 4) | vallen;
                    userstream.write(typeByte);
                }
                switch(type) {
                    case _Private_IonConstants.tidList:
                    case _Private_IonConstants.tidSexp:
                    case _Private_IonConstants.tidStruct:
                    case _Private_IonConstants.tidTypedecl:
                        // write the container
                        assert p._children != null;
                        writeRecursive(datastream, userstream, p._children.remove());
                        break;
                    default:
                        datastream.writeTo(userstream, vallen);
                }
        }
    }
    return totalSize;
}
Also used : SymbolTable(com.amazon.ion.SymbolTable)

Aggregations

SymbolTable (com.amazon.ion.SymbolTable)177 Test (org.junit.Test)105 IonValue (com.amazon.ion.IonValue)21 IonDatagram (com.amazon.ion.IonDatagram)18 com.amazon.ion.impl._Private_Utils.copyLocalSymbolTable (com.amazon.ion.impl._Private_Utils.copyLocalSymbolTable)17 SymbolToken (com.amazon.ion.SymbolToken)14 IonStruct (com.amazon.ion.IonStruct)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)13 IonWriter (com.amazon.ion.IonWriter)12 SimpleCatalog (com.amazon.ion.system.SimpleCatalog)12 IonReader (com.amazon.ion.IonReader)11 IonSystem (com.amazon.ion.IonSystem)10 IOException (java.io.IOException)9 IonType (com.amazon.ion.IonType)8 ArrayList (java.util.ArrayList)7 IonException (com.amazon.ion.IonException)6 com.amazon.ion.impl._Private_IonBinaryWriterBuilder (com.amazon.ion.impl._Private_IonBinaryWriterBuilder)6 com.amazon.ion.impl.bin._Private_IonRawWriter (com.amazon.ion.impl.bin._Private_IonRawWriter)6 IonCatalog (com.amazon.ion.IonCatalog)5 IonList (com.amazon.ion.IonList)5