Search in sources :

Example 36 with SymbolToken

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

Example 37 with SymbolToken

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

Example 38 with SymbolToken

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

the class ReaderCompare method compareFieldNames.

public static void compareFieldNames(IonReader r1, IonReader r2) {
    SymbolToken tok1 = r1.getFieldNameSymbol();
    SymbolToken tok2 = r1.getFieldNameSymbol();
    String fn = tok1.getText();
    assertEquals(fn, tok2.getText());
    if (fn != null) {
        String f1 = r1.getFieldName();
        String f2 = r2.getFieldName();
        compareNonNullStrings("field name", fn, f1);
        compareNonNullStrings("field name", fn, f2);
    }
}
Also used : SymbolToken(com.amazon.ion.SymbolToken)

Example 39 with SymbolToken

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

the class IonJavaCli method isEmbeddedEvent.

private static boolean isEmbeddedEvent(Event event) {
    IonType ionType = event.getIonType();
    SymbolToken[] annotations = event.getAnnotations();
    return (ionType == IonType.SEXP || ionType == IonType.LIST) && event.getDepth() == 0 && annotations != null && annotations.length > 0 && annotations[0].getText().equals(EMBEDDED_STREAM_ANNOTATION);
}
Also used : IonType(com.amazon.ion.IonType) SymbolToken(com.amazon.ion.SymbolToken)

Example 40 with SymbolToken

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

the class IonJavaCli method ionStreamToEvent.

private static Event ionStreamToEvent(IonReader ionReader) throws IllegalStateException {
    if (ionReader.getType() == null)
        throw new IllegalStateException("Can't convert ionReader null type to Event");
    IonType ionType = ionReader.getType();
    SymbolToken fieldName = ionReader.getFieldNameSymbol();
    SymbolToken[] annotations = ionReader.getTypeAnnotationSymbols();
    int depth = ionReader.getDepth();
    ImportDescriptor[] imports = null;
    EventType eventType;
    IonValue value = null;
    if (IonType.isContainer(ionType)) {
        eventType = EventType.CONTAINER_START;
    } else {
        eventType = EventType.SCALAR;
        value = ION_SYSTEM.newValue(ionReader);
        value.clearTypeAnnotations();
        if (isIonVersionMarker(value.toString())) {
            value.setTypeAnnotationSymbols(_Private_Utils.newSymbolToken("$ion_user_value", 0));
        }
    }
    return new Event(eventType, ionType, fieldName, annotations, value, imports, depth);
}
Also used : IonValue(com.amazon.ion.IonValue) IonType(com.amazon.ion.IonType) SymbolToken(com.amazon.ion.SymbolToken) EventType(com.amazon.tools.events.EventType) Event(com.amazon.tools.events.Event) ImportDescriptor(com.amazon.tools.events.ImportDescriptor)

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