Search in sources :

Example 66 with SymbolTable

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

the class IonWriterSystemText method writeLocalSymtab.

@Override
void writeLocalSymtab(SymbolTable symtab) throws IOException {
    SymbolTable[] imports = symtab.getImportedTables();
    LstMinimizing min = _options.getLstMinimizing();
    if (min == null) {
        symtab.writeTo(this);
    } else if (min == LstMinimizing.LOCALS && imports.length > 0) {
        // Copy the symtab, but filter out local symbols.
        IonReader reader = new SymbolTableReader(symtab);
        // move onto and write the struct header
        IonType t = reader.next();
        assert (IonType.STRUCT.equals(t));
        SymbolToken[] a = reader.getTypeAnnotationSymbols();
        // you (should) always have the $ion_symbol_table annotation
        assert (a != null && a.length >= 1);
        // now we'll start a local symbol table struct
        // in the underlying system writer
        setTypeAnnotationSymbols(a);
        stepIn(IonType.STRUCT);
        // step into the symbol table struct and
        // write the values - EXCEPT the symbols field
        reader.stepIn();
        for (; ; ) {
            t = reader.next();
            if (t == null)
                break;
            // get the field name and skip over 'symbols'
            String name = reader.getFieldName();
            if (SYMBOLS.equals(name)) {
                continue;
            }
            writeValue(reader);
        }
        // we're done step out and move along
        stepOut();
    } else // Collapse to IVM
    {
        SymbolTable systemSymtab = symtab.getSystemSymbolTable();
        writeIonVersionMarker(systemSymtab);
    }
    super.writeLocalSymtab(symtab);
}
Also used : IonType(com.amazon.ion.IonType) LstMinimizing(com.amazon.ion.system.IonTextWriterBuilder.LstMinimizing) IonReader(com.amazon.ion.IonReader) SymbolTable(com.amazon.ion.SymbolTable)

Example 67 with SymbolTable

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

the class IonWriterUser method close_local_symbol_table_copy.

/**
 * Closes the diverted writer since the local symbol table
 * is complete (i.e. the struct is closed, on {@link #stepOut()}).
 */
private void close_local_symbol_table_copy() throws IOException {
    assert (symbol_table_being_collected());
    // convert the struct we just wrote with the TreeWriter to a
    // local symbol table
    LocalSymbolTableAsStruct.Factory lstFactory = (LocalSymbolTableAsStruct.Factory) ((_Private_ValueFactory) _symtab_value_factory).getLstFactory();
    SymbolTable symtab = lstFactory.newLocalSymtab(_catalog, _symbol_table_value);
    _symbol_table_value = null;
    _current_writer = _system_writer;
    // now make this symbol table the current symbol table
    this.setSymbolTable(symtab);
}
Also used : ValueFactory(com.amazon.ion.ValueFactory) SymbolTable(com.amazon.ion.SymbolTable)

Example 68 with SymbolTable

use of com.amazon.ion.SymbolTable 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 69 with SymbolTable

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

the class LocalSymbolTable method prepImportsList.

/**
 * Collects the necessary imports from the reader and catalog, and load
 * them into the passed-in {@code importsList}.
 */
private static void prepImportsList(List<SymbolTable> importsList, IonReader reader, IonCatalog catalog) {
    assert IMPORTS.equals(reader.getFieldName());
    reader.stepIn();
    IonType t;
    while ((t = reader.next()) != null) {
        if (!reader.isNullValue() && t == IonType.STRUCT) {
            SymbolTable importedTable = readOneImport(reader, catalog);
            if (importedTable != null) {
                importsList.add(importedTable);
            }
        }
    }
    reader.stepOut();
}
Also used : IonType(com.amazon.ion.IonType) SymbolTable(com.amazon.ion.SymbolTable)

Example 70 with SymbolTable

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

the class IonWriterSystem method setTypeAnnotationSymbols.

public final void setTypeAnnotationSymbols(SymbolToken... annotations) {
    if (annotations == null || annotations.length == 0) {
        _annotation_count = 0;
    } else {
        int count = annotations.length;
        // TODO the following makes two copy passes
        // TODO validate the input
        ensureAnnotationCapacity(count);
        SymbolTable symtab = getSymbolTable();
        for (int i = 0; i < count; i++) {
            SymbolToken sym = annotations[i];
            if (sym.getText() == null) {
                validateSymbolId(sym.getSid());
            }
            sym = _Private_Utils.localize(symtab, sym);
            _annotations[i] = sym;
        }
        _annotation_count = count;
    }
}
Also used : SymbolToken(com.amazon.ion.SymbolToken) com.amazon.ion.impl._Private_Utils.newSymbolToken(com.amazon.ion.impl._Private_Utils.newSymbolToken) 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