use of com.amazon.tools.events.EventType 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;
}
use of com.amazon.tools.events.EventType 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");
}
}
use of com.amazon.tools.events.EventType in project ion-java by amzn.
the class IonJavaCli method parseSequence.
private static IonSequence parseSequence(ContainerContext containerContext, CompareContext compareContext, int end, boolean isFirst, IonSequence ionSequence) {
while (containerContext.increaseIndex() < end) {
Event event = isFirst ? compareContext.getEventStreamFirst().get(containerContext.getIndex()) : compareContext.getEventStreamSecond().get(containerContext.getIndex());
EventType eventType = event.getEventType();
if (eventType == EventType.CONTAINER_START) {
switch(event.getIonType()) {
case LIST:
ionSequence.add(parseSequence(containerContext, compareContext, end, isFirst, ION_SYSTEM.newEmptyList()));
break;
case SEXP:
ionSequence.add(parseSequence(containerContext, compareContext, end, isFirst, ION_SYSTEM.newEmptySexp()));
break;
case STRUCT:
ionSequence.add(parseStruct(containerContext, compareContext, end, isFirst));
break;
}
} else if (eventType == EventType.CONTAINER_END) {
break;
} else if (eventType == EventType.STREAM_END) {
throw new IonException("Invalid ionSequence: eventStream ends without CONTAINER_END");
} else {
IonValue cloneValue = event.getValue().clone();
cloneValue.setTypeAnnotationSymbols(event.getAnnotations());
ionSequence.add(cloneValue);
}
}
return ionSequence;
}
use of com.amazon.tools.events.EventType 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);
}
use of com.amazon.tools.events.EventType in project ion-java by amzn.
the class IonJavaCli method locateEmbeddedStreamBoundaries.
/**
* This function parses an embedded stream into pairs<int,int> that stores index range for
* each string the embedded stream includes.
*
* Input value 'start' must be index of a CONTAINER_START event.
*
* return List is never null. It will return a list with size zero if the embedded stream is empty.
*/
private static List<Pair> locateEmbeddedStreamBoundaries(List<Event> events, int start) {
List<Pair> parsedEvents = new ArrayList<>(0);
int left = start + 1;
int right = start;
int depth = 1;
while (++right < events.size()) {
EventType eventType = events.get(right).getEventType();
if (eventType == EventType.STREAM_END) {
parsedEvents.add(new Pair(left, right));
left = right + 1;
} else if (eventType == EventType.CONTAINER_END) {
if (--depth == 0)
break;
} else if (eventType == EventType.CONTAINER_START) {
depth++;
}
}
return parsedEvents;
}
Aggregations