use of com.amazon.tools.events.Event 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.Event 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.Event in project ion-java by amzn.
the class IonJavaCli method embeddedEventToIon.
private static int embeddedEventToIon(ProcessContext processContext, CommandArgs args, int count, IonType ionType) throws IOException {
processContext.getIonWriter().addTypeAnnotation(EMBEDDED_STREAM_ANNOTATION);
processContext.getIonWriter().stepIn(ionType);
List<Event> events = processContext.getEventStream();
int depth = 1;
boolean finish = false;
while (++count < events.size()) {
StringBuilder out = new StringBuilder();
ProcessContext embeddedContext = new ProcessContext(null, 0, null, null, ION_TEXT_WRITER_BUILDER.withImports(_Private_Utils.systemSymtab(1)).build(out));
embeddedContext.setEmbeddedOut(out);
try {
do {
Event event = events.get(count);
processContext.setEventIndex(processContext.getEventIndex() + 1);
processContext.setLastEventType(event.getEventType());
if (event.getEventType() == EventType.STREAM_END) {
break;
} else if (event.getEventType() == EventType.SCALAR) {
writeIonByType(event, embeddedContext.getIonWriter());
} else if (event.getEventType() == EventType.CONTAINER_START) {
depth++;
setFieldName(event, embeddedContext.getIonWriter());
setAnnotations(event, embeddedContext.getIonWriter());
embeddedContext.getIonWriter().stepIn(event.getIonType());
} else if (event.getEventType() == EventType.CONTAINER_END) {
depth--;
if (depth == 0) {
if (event.getIonType() == IonType.SEXP || event.getIonType() == IonType.LIST) {
finish = true;
break;
} else {
throw new IonException("invalid CONTAINER_END");
}
}
embeddedContext.getIonWriter().stepOut();
} else if (event.getEventType() == EventType.SYMBOL_TABLE) {
handleSymbolTableEvent(embeddedContext, event, args, true);
}
} while (++count < events.size());
if (!finish) {
embeddedContext.getIonWriter().finish();
processContext.getIonWriter().writeString(out.toString());
}
} finally {
IonWriter ionWriter = embeddedContext.getIonWriter();
if (ionWriter != null) {
try {
ionWriter.close();
} catch (IOException e) {
System.err.println(e.getMessage());
System.exit(IO_ERROR_EXIT_CODE);
}
}
}
if (finish) {
break;
}
}
processContext.getIonWriter().stepOut();
return count;
}
use of com.amazon.tools.events.Event in project ion-java by amzn.
the class IonJavaCli method compareEquivs.
private static boolean compareEquivs(CompareContext compareContext) throws IOException {
int i = 0;
int j = 0;
List<Event> eventStreamFirst = compareContext.getEventStreamFirst();
List<Event> eventStreamSecond = compareContext.getEventStreamSecond();
ComparisonType type = compareContext.getType();
while (i < eventStreamFirst.size() && j < eventStreamSecond.size()) {
Event eventFirst = eventStreamFirst.get(i);
Event eventSecond = eventStreamSecond.get(j);
if (eventFirst.getEventType() == EventType.STREAM_END && eventSecond.getEventType() == EventType.STREAM_END) {
break;
} else if (eventFirst.getEventType() == EventType.STREAM_END || eventSecond.getEventType() == EventType.STREAM_END) {
setReportInfo(i, j, "The input streams had a different number of comparison sets.", compareContext);
return type == ComparisonType.NON_EQUIVS;
} else if (!(eventFirst.getIonType() == IonType.LIST || eventFirst.getIonType() == IonType.SEXP) || !(eventSecond.getIonType() == IonType.LIST || eventSecond.getIonType() == IonType.SEXP)) {
throw new IonException("Comparison sets must be lists or s-expressions.");
} else if (isEmbeddedEvent(eventFirst) ^ isEmbeddedEvent(eventSecond)) {
throw new IonException("Embedded streams set expected.");
}
List<Pair> pairsFirst;
List<Pair> pairsSecond;
if (isEmbeddedEvent(eventFirst) && isEmbeddedEvent(eventSecond)) {
pairsFirst = locateEmbeddedStreamBoundaries(eventStreamFirst, i);
pairsSecond = locateEmbeddedStreamBoundaries(eventStreamSecond, j);
} else {
pairsFirst = locateContainerBoundaries(eventStreamFirst, i);
pairsSecond = locateContainerBoundaries(eventStreamSecond, j);
}
i = pairsFirst.size() == 0 ? i + 1 : pairsFirst.get(pairsFirst.size() - 1).right + 1;
j = pairsSecond.size() == 0 ? j + 1 : pairsSecond.get(pairsSecond.size() - 1).right + 1;
for (int m = 0; m < pairsFirst.size(); m++) {
for (int n = 0; n < pairsSecond.size(); n++) {
if (compareContext.getType() == ComparisonType.NON_EQUIVS) {
if (m == n)
continue;
}
Pair pairFirst = pairsFirst.get(m);
Pair pairSecond = pairsSecond.get(n);
if (compare(compareContext, pairFirst.left, pairFirst.right, pairSecond.left, pairSecond.right) ^ (type == ComparisonType.EQUIVS || type == ComparisonType.EQUIVS_TIMELINE)) {
if (type == ComparisonType.NON_EQUIVS) {
setReportInfo(pairFirst.left, pairSecond.left, "Equivalent values in a non-equivs set.", compareContext);
}
return type == ComparisonType.NON_EQUIVS;
}
}
}
i++;
j++;
}
return (type == ComparisonType.EQUIVS || type == ComparisonType.EQUIVS_TIMELINE);
}
use of com.amazon.tools.events.Event 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);
}
Aggregations