Search in sources :

Example 1 with Event

use of com.amazon.tools.events.Event 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 2 with Event

use of com.amazon.tools.events.Event in project ion-java by amzn.

the class IonJavaCli method getEventStream.

private static void getEventStream(IonReader ionReader, CommandType commandType, ReadContext readContext) throws IOException {
    SymbolTable curTable = ionReader.getSymbolTable();
    boolean isEventStream = isEventStream(ionReader);
    if (isEventStream) {
        readContext.setState(ErrorType.WRITE);
        while (ionReader.next() != null) {
            Event event = eventStreamToEvent(ionReader);
            if (event.getEventType() == EventType.SYMBOL_TABLE && commandType == CommandType.COMPARE) {
                continue;
            }
            readContext.getEventStream().add(event);
        }
    } else {
        readContext.setState(ErrorType.READ);
        ionStreamToEventStream(ionReader, commandType, curTable, readContext);
        readContext.getEventStream().add(new Event(EventType.STREAM_END, null, null, null, null, null, 0));
    }
    if (commandType == CommandType.PROCESS) {
        validateEventStream(readContext.getEventStream());
    }
    return;
}
Also used : SymbolTable(com.amazon.ion.SymbolTable) Event(com.amazon.tools.events.Event)

Example 3 with Event

use of com.amazon.tools.events.Event in project ion-java by amzn.

the class IonJavaCli method compare.

private static boolean compare(CompareContext compareContext, int startI, int endI, int startJ, int endJ) throws IOException {
    List<Event> eventsFirst = compareContext.getEventStreamFirst();
    List<Event> eventsSecond = compareContext.getEventStreamSecond();
    int i = startI;
    int j = startJ;
    while (i <= endI && j <= endJ && i < eventsFirst.size() && j < eventsSecond.size()) {
        Event eventFirst = eventsFirst.get(i);
        Event eventSecond = eventsSecond.get(j);
        SymbolToken fieldNameFirst = eventFirst.getFieldName();
        SymbolToken fieldNameSecond = eventSecond.getFieldName();
        SymbolToken[] annotationFirst = eventFirst.getAnnotations();
        SymbolToken[] annotationSecond = eventSecond.getAnnotations();
        EventType eventTypeFirst = eventFirst.getEventType();
        EventType eventTypeSecond = eventSecond.getEventType();
        if (eventTypeFirst != eventTypeSecond) {
            setReportInfo(i, j, "Didn't match event_type", compareContext);
            return false;
        } else if (eventFirst.getDepth() != eventSecond.getDepth()) {
            setReportInfo(i, j, "Didn't match depth", compareContext);
            return false;
        } else if (eventFirst.getIonType() != eventSecond.getIonType()) {
            setReportInfo(i, j, "Didn't match ion_type", compareContext);
            return false;
        } else if (!isSameSymbolToken(fieldNameFirst, fieldNameSecond)) {
            setReportInfo(i, j, "Didn't match field_name", compareContext);
            return false;
        } else if (!isSameSymbolTokenArray(annotationFirst, annotationSecond)) {
            setReportInfo(i, j, "Didn't match annotation", compareContext);
            return false;
        }
        if (eventTypeFirst == EventType.CONTAINER_START && eventFirst.getIonType() == IonType.STRUCT) {
            int iStart = i;
            int jStart = j;
            ContainerContext containerContextFirst = new ContainerContext(i);
            IonStruct structFirst = parseStruct(containerContextFirst, compareContext, endI, true);
            i = containerContextFirst.getIndex();
            ContainerContext containerContextSecond = new ContainerContext(j);
            IonStruct structSecond = parseStruct(containerContextSecond, compareContext, endJ, false);
            j = containerContextSecond.getIndex();
            if (!Equivalence.ionEquals(structFirst, structSecond)) {
                setReportInfo(iStart, jStart, "Did not find matching field for " + structFirst.toString(), compareContext);
                return false;
            }
        } else if (eventTypeFirst == EventType.SCALAR) {
            boolean compareResult;
            if (compareContext.getType() == ComparisonType.EQUIVS_TIMELINE && eventFirst.getIonType() == IonType.TIMESTAMP) {
                IonTimestamp ionTimestampFirst = (IonTimestamp) eventFirst.getValue();
                IonTimestamp ionTimestampSecond = (IonTimestamp) eventSecond.getValue();
                compareResult = ionTimestampFirst.timestampValue().compareTo(ionTimestampSecond.timestampValue()) == 0;
            } else {
                compareResult = Equivalence.ionEquals(eventFirst.getValue(), eventSecond.getValue());
            }
            if (!compareResult) {
                setReportInfo(i, j, eventFirst.getValue() + " vs. " + eventSecond.getValue(), compareContext);
                return false;
            }
        }
        i++;
        j++;
    }
    if (i <= endI || j <= endJ) {
        setReportInfo(i, j, "two event streams have different size", compareContext);
        return false;
    }
    return true;
}
Also used : IonStruct(com.amazon.ion.IonStruct) SymbolToken(com.amazon.ion.SymbolToken) EventType(com.amazon.tools.events.EventType) IonTimestamp(com.amazon.ion.IonTimestamp) Event(com.amazon.tools.events.Event)

Example 4 with Event

use of com.amazon.tools.events.Event 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 5 with Event

use of com.amazon.tools.events.Event in project ion-java by amzn.

the class IonJavaCli method validateEventStream.

private static void validateEventStream(List<Event> events) {
    if (events.size() == 0)
        return;
    int depth = 0;
    int i = -1;
    while (++i < events.size()) {
        Event event = events.get(i);
        EventType eventType = event.getEventType();
        if (eventType == EventType.CONTAINER_START) {
            depth++;
        } else if (eventType == EventType.CONTAINER_END) {
            if (--depth < 0) {
                throw new IonException("Invalid event stream: Invalid CONTAINER_END event");
            }
        }
        if (isEmbeddedEvent(event)) {
            while (i < events.size()) {
                if (events.get(++i).getEventType() == EventType.CONTAINER_END) {
                    depth--;
                    break;
                }
                while (i < events.size()) {
                    Event aEvent = events.get(i);
                    EventType aEventType = aEvent.getEventType();
                    if (aEventType == EventType.CONTAINER_START) {
                        depth++;
                    } else if (aEventType == EventType.CONTAINER_END) {
                        if (--depth == 0) {
                            throw new IonException("Invalid EventStream: end without STREAM_END event");
                        }
                    } else if (aEventType == EventType.STREAM_END) {
                        if (depth != 1) {
                            throw new IonException("Invalid EventStream: " + "CONTAINER_START and CONTAINER_END not match");
                        }
                        break;
                    }
                    i++;
                }
                if (i == events.size()) {
                    throw new IonException("Invalid EventStream: end without CONTAINER_END event");
                }
            }
        }
    }
    if (depth != 0) {
        throw new IonException("Invalid event stream: CONTAINER_START and CONTAINER_END events doesn't match");
    }
}
Also used : EventType(com.amazon.tools.events.EventType) IonException(com.amazon.ion.IonException) Event(com.amazon.tools.events.Event)

Aggregations

Event (com.amazon.tools.events.Event)11 IonException (com.amazon.ion.IonException)7 EventType (com.amazon.tools.events.EventType)6 IonValue (com.amazon.ion.IonValue)5 IonType (com.amazon.ion.IonType)4 SymbolToken (com.amazon.ion.SymbolToken)3 ImportDescriptor (com.amazon.tools.events.ImportDescriptor)3 IonStruct (com.amazon.ion.IonStruct)2 SymbolTable (com.amazon.ion.SymbolTable)2 IonReader (com.amazon.ion.IonReader)1 IonTimestamp (com.amazon.ion.IonTimestamp)1 IonWriter (com.amazon.ion.IonWriter)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1