use of com.amazon.ion.SymbolToken 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.ion.SymbolToken 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.ion.SymbolToken in project ion-java by amzn.
the class IonReaderBinaryUserX method symbolValue.
@Override
public final SymbolToken symbolValue() {
SymbolToken symbol = super.symbolValue();
validateSymbolToken(symbol);
return symbol;
}
use of com.amazon.ion.SymbolToken in project ion-java by amzn.
the class IonReaderBinaryUserX method getFieldNameSymbol.
@Override
public final SymbolToken getFieldNameSymbol() {
SymbolToken fieldName = super.getFieldNameSymbol();
validateSymbolToken(fieldName);
return fieldName;
}
use of com.amazon.ion.SymbolToken in project ion-java by amzn.
the class Event method writeOutput.
/**
* write Event structure to Event Stream and return the updated eventIndex.
*/
public int writeOutput(IonWriter ionWriterForOutput, int eventIndex) throws IOException {
ionWriterForOutput.stepIn(IonType.STRUCT);
if (this.eventType != null) {
ionWriterForOutput.setFieldName("event_type");
ionWriterForOutput.writeSymbol(this.eventType.toString());
}
if (this.ionType != null) {
ionWriterForOutput.setFieldName("ion_type");
ionWriterForOutput.writeSymbol(this.ionType.toString());
}
if (this.fieldName != null) {
ionWriterForOutput.setFieldName("field_name");
ionWriterForOutput.stepIn(IonType.STRUCT);
ionWriterForOutput.setFieldName("text");
if (this.fieldName.getText() == null) {
ionWriterForOutput.writeNull();
} else {
ionWriterForOutput.writeString(this.fieldName.getText());
}
ionWriterForOutput.setFieldName("import_location");
ionWriterForOutput.writeNull();
ionWriterForOutput.stepOut();
}
if (this.annotations != null && this.annotations.length > 0) {
ionWriterForOutput.setFieldName("annotations");
ionWriterForOutput.stepIn(IonType.LIST);
for (SymbolToken annotation : this.annotations) {
ionWriterForOutput.stepIn(IonType.STRUCT);
ionWriterForOutput.setFieldName("text");
String text = annotation.getText();
if (text == null) {
ionWriterForOutput.writeNull();
ionWriterForOutput.setFieldName("import_location");
ionWriterForOutput.writeNull();
} else {
ionWriterForOutput.writeString(text);
}
ionWriterForOutput.stepOut();
}
ionWriterForOutput.stepOut();
}
if (this.value != null) {
String valueText;
byte[] valueBinary;
try (ByteArrayOutputStream textOut = new ByteArrayOutputStream();
IonWriter textWriter = IonTextWriterBuilder.standard().build(textOut);
ByteArrayOutputStream binaryOut = new ByteArrayOutputStream();
IonWriter binaryWriter = IonBinaryWriterBuilder.standard().build(binaryOut)) {
// write Text
this.value.writeTo(textWriter);
textWriter.finish();
valueText = textOut.toString("utf-8");
ionWriterForOutput.setFieldName("value_text");
ionWriterForOutput.writeString(valueText);
// write binary
this.value.writeTo(binaryWriter);
binaryWriter.finish();
valueBinary = binaryOut.toByteArray();
ionWriterForOutput.setFieldName("value_binary");
ionWriterForOutput.stepIn(IonType.LIST);
for (byte b : valueBinary) {
ionWriterForOutput.writeInt(b & 0xff);
}
ionWriterForOutput.stepOut();
}
}
if (this.imports != null && this.imports.length > 0) {
ionWriterForOutput.setFieldName("imports");
ionWriterForOutput.stepIn(IonType.LIST);
for (ImportDescriptor anImport : this.imports) {
ionWriterForOutput.stepIn(IonType.STRUCT);
ionWriterForOutput.setFieldName("name");
ionWriterForOutput.writeString(anImport.getImportName());
ionWriterForOutput.setFieldName("version");
ionWriterForOutput.writeInt(anImport.getVersion());
ionWriterForOutput.setFieldName("max_id");
ionWriterForOutput.writeInt(anImport.getMaxId());
ionWriterForOutput.stepOut();
}
ionWriterForOutput.stepOut();
}
if (this.depth != -1) {
ionWriterForOutput.setFieldName("depth");
ionWriterForOutput.writeInt(this.depth);
}
ionWriterForOutput.stepOut();
// event index + 1 if we write OutputStream successfully.
return eventIndex + 1;
}
Aggregations