Search in sources :

Example 1 with IonType

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

the class IonReaderBinaryRawX method load_annotation_start_with_value_type.

private final IonType load_annotation_start_with_value_type() throws IOException {
    IonType value_type;
    // we need to skip over the annotations to read
    // the actual type id byte for the value.  We'll
    // save the annotations using a save point, which
    // will pin the input buffers until we free this,
    // not later than the next call to hasNext().
    int alen = readVarUInt();
    _annotations.start(getPosition(), 0);
    skip(alen);
    _annotations.markEnd();
    // this will both get the type id and it will reset the
    // length as well (over-writing the len + annotations value
    // that is there now, before the call)
    _value_tid = read_type_id();
    if (_value_tid == _Private_IonConstants.tidNopPad) {
        throwErrorAt("NOP padding is not allowed within annotation wrappers.");
    }
    if (_value_tid == UnifiedInputStreamX.EOF) {
        throwErrorAt("unexpected EOF encountered where a type descriptor byte was expected");
    }
    if (_value_tid == _Private_IonConstants.tidTypedecl) {
        throwErrorAt("An annotation wrapper may not contain another annotation wrapper.");
    }
    value_type = get_iontype_from_tid(_value_tid);
    assert (value_type != null);
    return value_type;
}
Also used : IonType(com.amazon.ion.IonType) SavePoint(com.amazon.ion.impl.UnifiedSavePointManagerX.SavePoint)

Example 2 with IonType

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

the class IonJavaCli method ionStreamToEventStream.

private static void ionStreamToEventStream(IonReader ionReader, CommandType commandType, SymbolTable curTable, ReadContext readContext) throws IOException {
    if (ionReader.getType() == null)
        return;
    do {
        if (ionReader.isNullValue()) {
            IonValue value = ION_SYSTEM.newValue(ionReader);
            value.clearTypeAnnotations();
            readContext.getEventStream().add(new Event(EventType.SCALAR, ionReader.getType(), ionReader.getFieldNameSymbol(), ionReader.getTypeAnnotationSymbols(), value, null, ionReader.getDepth()));
            continue;
        }
        if (!isSameSymbolTable(ionReader.getSymbolTable(), curTable)) {
            curTable = ionReader.getSymbolTable();
            ImportDescriptor[] imports = symbolTableToImports(curTable.getImportedTables());
            if (commandType != CommandType.COMPARE) {
                readContext.getEventStream().add(new Event(EventType.SYMBOL_TABLE, null, null, null, null, imports, 0));
            }
        }
        if (isEmbeddedStream(ionReader)) {
            // get current Ion type and depth
            IonType curType = ionReader.getType();
            int curDepth = ionReader.getDepth();
            // write a Container_Start event and step in
            readContext.getEventStream().add(ionStreamToEvent(ionReader));
            ionReader.stepIn();
            while (ionReader.next() != null) {
                if (ionReader.getType() != IonType.STRING) {
                    throw new IonException("Elements of embedded streams sets must be strings.");
                }
                String stream = ionReader.stringValue();
                try (IonReader tempIonReader = IonReaderBuilder.standard().build(stream)) {
                    SymbolTable symbolTable = tempIonReader.getSymbolTable();
                    while (tempIonReader.next() != null) {
                        ionStreamToEventStream(tempIonReader, commandType, symbolTable, readContext);
                    }
                }
                readContext.getEventStream().add(new Event(EventType.STREAM_END, null, null, null, null, null, 0));
            }
            // write a Container_End event and step out
            readContext.getEventStream().add(new Event(EventType.CONTAINER_END, curType, null, null, null, null, curDepth));
            ionReader.stepOut();
        } else if (IonType.isContainer(ionReader.getType())) {
            // get current Ion type and depth
            IonType curType = ionReader.getType();
            int curDepth = ionReader.getDepth();
            // write a Container_Start event and step in
            readContext.getEventStream().add(ionStreamToEvent(ionReader));
            ionReader.stepIn();
            // recursive call
            ionReader.next();
            ionStreamToEventStream(ionReader, commandType, curTable, readContext);
            // write a Container_End event and step out
            readContext.getEventStream().add(new Event(EventType.CONTAINER_END, curType, null, null, null, null, curDepth));
            ionReader.stepOut();
        } else {
            readContext.getEventStream().add(ionStreamToEvent(ionReader));
        }
    } while (ionReader.next() != null);
    ;
}
Also used : IonValue(com.amazon.ion.IonValue) IonType(com.amazon.ion.IonType) IonException(com.amazon.ion.IonException) IonReader(com.amazon.ion.IonReader) Event(com.amazon.tools.events.Event) SymbolTable(com.amazon.ion.SymbolTable) ImportDescriptor(com.amazon.tools.events.ImportDescriptor)

Example 3 with IonType

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

the class IonJavaCli method processToIonStream.

private static void processToIonStream(ProcessContext processContext, CommandArgs args) throws IOException {
    List<Event> events = processContext.getEventStream();
    int count = 0;
    while (count != events.size()) {
        // update eventIndex
        Event event = events.get(count);
        processContext.setEventIndex(processContext.getEventIndex() + 1);
        processContext.setLastEventType(event.getEventType());
        if (event.getEventType() == EventType.CONTAINER_START) {
            if (isEmbeddedEvent(event)) {
                count = embeddedEventToIon(processContext, args, count, event.getIonType());
            } else {
                IonType type = event.getIonType();
                setFieldName(event, processContext.getIonWriter());
                setAnnotations(event, processContext.getIonWriter());
                processContext.getIonWriter().stepIn(type);
            }
        } else if (event.getEventType().equals(EventType.CONTAINER_END)) {
            processContext.getIonWriter().stepOut();
        } else if (event.getEventType().equals(EventType.SCALAR)) {
            writeIonByType(event, processContext.getIonWriter());
        } else if (event.getEventType().equals(EventType.SYMBOL_TABLE)) {
            handleSymbolTableEvent(processContext, event, args, false);
        } else if (event.getEventType().equals(EventType.STREAM_END)) {
            processContext.getIonWriter().finish();
        }
        count++;
    }
}
Also used : IonType(com.amazon.ion.IonType) Event(com.amazon.tools.events.Event)

Example 4 with IonType

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

the class IonJavaCli method eventStreamToEvent.

private static Event eventStreamToEvent(IonReader ionReader) {
    if (ionReader.getType() != IonType.STRUCT)
        throw new IonException("cant convert null");
    String textValue = null;
    byte[] binaryValue = null;
    IonValue eventValue = null;
    EventType eventType = null;
    IonType ionType = null;
    SymbolToken fieldName = null;
    SymbolToken[] annotations = null;
    ImportDescriptor[] imports = null;
    int depth = -1;
    ionReader.stepIn();
    while (ionReader.next() != null) {
        switch(ionReader.getFieldName()) {
            case "event_type":
                if (eventType != null)
                    throw new IonException("invalid Event: repeat event_type");
                eventType = EventType.valueOf(ionReader.stringValue().toUpperCase());
                break;
            case "ion_type":
                if (ionType != null)
                    throw new IonException("invalid Event: repeat ion_type");
                ionType = IonType.valueOf(ionReader.stringValue().toUpperCase());
                break;
            case "field_name":
                if (fieldName != null)
                    throw new IonException("invalid Event: repeat field_name");
                ionReader.stepIn();
                String fieldText = null;
                int fieldSid = 0;
                while (ionReader.next() != null) {
                    switch(ionReader.getFieldName()) {
                        case "text":
                            fieldText = ionReader.stringValue();
                            break;
                        case "sid":
                            fieldSid = ionReader.intValue();
                            break;
                    }
                }
                fieldName = _Private_Utils.newSymbolToken(fieldText, fieldSid);
                ionReader.stepOut();
                break;
            case "annotations":
                if (annotations != null)
                    throw new IonException("invalid Event: repeat annotations");
                ArrayList<SymbolToken> annotationsList = new ArrayList<>();
                ionReader.stepIn();
                while (ionReader.next() != null) {
                    ionReader.stepIn();
                    String text = null;
                    int sid = 0;
                    while (ionReader.next() != null) {
                        switch(ionReader.getFieldName()) {
                            case "text":
                                text = ionReader.isNullValue() ? null : ionReader.stringValue();
                                break;
                            case "sid":
                                sid = ionReader.intValue();
                                break;
                        }
                    }
                    SymbolToken annotation = _Private_Utils.newSymbolToken(text, sid);
                    annotationsList.add(annotation);
                    ionReader.stepOut();
                }
                annotations = annotationsList.toArray(SymbolToken.EMPTY_ARRAY);
                ionReader.stepOut();
                break;
            case "value_text":
                if (textValue != null)
                    throw new IonException("invalid Event: repeat value_text");
                textValue = ionReader.stringValue();
                break;
            case "value_binary":
                if (binaryValue != null)
                    throw new IonException("invalid Event: repeat binary_value");
                ArrayList<Integer> intArray = new ArrayList<>();
                ionReader.stepIn();
                while (ionReader.next() != null) {
                    intArray.add(ionReader.intValue());
                }
                byte[] binary = new byte[intArray.size()];
                for (int i = 0; i < intArray.size(); i++) {
                    int val = intArray.get(i);
                    binary[i] = (byte) (val & 0xff);
                }
                binaryValue = binary;
                ionReader.stepOut();
                break;
            case "imports":
                if (imports != null)
                    throw new IonException("invalid Event: repeat imports");
                imports = ionStreamToImportDescriptors(ionReader);
                break;
            case "depth":
                if (depth != -1)
                    throw new IonException("invalid Event: repeat depth");
                depth = ionReader.intValue();
                break;
        }
    }
    ionReader.stepOut();
    // validate event
    validateEvent(textValue, binaryValue, eventType, fieldName, ionType, imports, depth);
    if (textValue != null)
        eventValue = ION_SYSTEM.singleValue(textValue);
    return new Event(eventType, ionType, fieldName, annotations, eventValue, imports, depth);
}
Also used : IonValue(com.amazon.ion.IonValue) IonType(com.amazon.ion.IonType) SymbolToken(com.amazon.ion.SymbolToken) IonException(com.amazon.ion.IonException) EventType(com.amazon.tools.events.EventType) ArrayList(java.util.ArrayList) Event(com.amazon.tools.events.Event) ImportDescriptor(com.amazon.tools.events.ImportDescriptor)

Example 5 with IonType

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

the class IonJavaCli method isEmbeddedStream.

private static boolean isEmbeddedStream(IonReader ionReader) {
    IonType ionType = ionReader.getType();
    String[] annotations = ionReader.getTypeAnnotations();
    return (ionType == IonType.SEXP || ionType == IonType.LIST) && ionReader.getDepth() == 0 && annotations.length > 0 && annotations[0].equals(EMBEDDED_STREAM_ANNOTATION);
}
Also used : IonType(com.amazon.ion.IonType)

Aggregations

IonType (com.amazon.ion.IonType)60 Test (org.junit.Test)13 SymbolToken (com.amazon.ion.SymbolToken)10 IonException (com.amazon.ion.IonException)9 IonReader (com.amazon.ion.IonReader)8 SymbolTable (com.amazon.ion.SymbolTable)8 SavePoint (com.amazon.ion.impl.UnifiedSavePointManagerX.SavePoint)7 IonValue (com.amazon.ion.IonValue)6 IonDatagram (com.amazon.ion.IonDatagram)5 IonStruct (com.amazon.ion.IonStruct)5 IonReaderBuilder (com.amazon.ion.system.IonReaderBuilder)4 Event (com.amazon.tools.events.Event)4 ArrayList (java.util.ArrayList)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 BinaryTest (com.amazon.ion.BinaryTest)3 IonBinaryWriter (com.amazon.ion.IonBinaryWriter)3 IonLob (com.amazon.ion.IonLob)3 IonSequence (com.amazon.ion.IonSequence)3 IonString (com.amazon.ion.IonString)3 Span (com.amazon.ion.Span)3