Search in sources :

Example 31 with SymbolToken

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

the class IonWriterSystemText method writeAnnotations.

void writeAnnotations(SymbolToken[] annotations) throws IOException {
    for (SymbolToken ann : annotations) {
        writeAnnotationToken(ann);
        _output.appendAscii("::");
    }
}
Also used : SymbolToken(com.amazon.ion.SymbolToken)

Example 32 with SymbolToken

use of com.amazon.ion.SymbolToken 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 33 with SymbolToken

use of com.amazon.ion.SymbolToken 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 34 with SymbolToken

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

the class LocalSymbolTable method intern.

public synchronized SymbolToken intern(String text) {
    SymbolToken is = find(text);
    if (is == null) {
        validateSymbol(text);
        int sid = putSymbol(text);
        is = new SymbolTokenImpl(text, sid);
    }
    return is;
}
Also used : SymbolToken(com.amazon.ion.SymbolToken)

Example 35 with SymbolToken

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

the class LocalSymbolTable method find.

public SymbolToken find(String text) {
    // fast null check
    text.getClass();
    // Look in system then imports
    SymbolToken symTok = myImportsList.find(text);
    // Look in local symbols
    if (symTok == null) {
        Integer sid;
        String[] names;
        synchronized (this) {
            sid = mySymbolsMap.get(text);
            names = mySymbolNames;
        }
        if (sid != null) {
            int offset = sid - myFirstLocalSid;
            String internedText = names[offset];
            assert internedText != null;
            symTok = new SymbolTokenImpl(internedText, sid);
        }
    }
    return symTok;
}
Also used : SymbolToken(com.amazon.ion.SymbolToken)

Aggregations

SymbolToken (com.amazon.ion.SymbolToken)68 SymbolTable (com.amazon.ion.SymbolTable)14 com.amazon.ion.impl._Private_Utils.newSymbolToken (com.amazon.ion.impl._Private_Utils.newSymbolToken)13 IonType (com.amazon.ion.IonType)10 IonValue (com.amazon.ion.IonValue)10 IonException (com.amazon.ion.IonException)9 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)6 IonStruct (com.amazon.ion.IonStruct)4 IOException (java.io.IOException)4 Event (com.amazon.tools.events.Event)3 EventType (com.amazon.tools.events.EventType)3 FakeSymbolToken (com.amazon.ion.FakeSymbolToken)2 IonDatagram (com.amazon.ion.IonDatagram)2 IonSequence (com.amazon.ion.IonSequence)2 IonString (com.amazon.ion.IonString)2 UnknownSymbolException (com.amazon.ion.UnknownSymbolException)2 SavePoint (com.amazon.ion.impl.UnifiedSavePointManagerX.SavePoint)2 ImportDescriptor (com.amazon.tools.events.ImportDescriptor)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2