use of com.amazon.ion.IonValue in project ion-java by amzn.
the class IonJavaCli method validateEvent.
private static void validateEvent(String textValue, byte[] binaryValue, EventType eventType, SymbolToken fieldName, IonType ionType, ImportDescriptor[] imports, int depth) {
if (eventType == null)
throw new IllegalStateException("only can convert Struct to Event");
switch(eventType) {
case CONTAINER_START:
if (ionType == null || depth == -1) {
throw new IonException("Invalid CONTAINER_START: missing field(s)");
} else if (!IonType.isContainer(ionType)) {
throw new IonException("Invalid CONTAINER_START: not a container");
} else if (textValue != null || binaryValue != null) {
throw new IonException("Invalid CONTAINER_START: value_binary and value_text are only applicable" + " for SCALAR events");
} else if (imports != null) {
throw new IonException("Invalid CONTAINER_START: imports must only be present with SYMBOL_TABLE " + "events");
}
break;
case SCALAR:
if (ionType == null || textValue == null || binaryValue == null || depth == -1) {
throw new IonException("Invalid SCALAR: missing field(s)");
} else if (imports != null) {
throw new IonException("Invalid SCALAR: imports must only be present with SYMBOL_TABLE " + "events");
}
// compare text value and binary value
IonValue text = ION_SYSTEM.singleValue(textValue);
IonValue binary = ION_SYSTEM.singleValue(binaryValue);
if (!Equivalence.ionEquals(text, binary)) {
throw new IonException("invalid Event: Text value and Binary value are different");
}
break;
case SYMBOL_TABLE:
if (depth == -1) {
throw new IonException("Invalid SYMBOL_TABLE: missing depth");
} else if (imports == null) {
throw new IonException("Invalid SYMBOL_TABLE: missing imports");
} else if (textValue != null && binaryValue != null) {
throw new IonException("Invalid SYMBOL_TABLE: text_value and binary_value " + "are only applicable for SCALAR events");
} else if (fieldName != null && ionType != null) {
throw new IonException("Invalid SYMBOL_TABLE: unnecessary fields");
}
break;
case CONTAINER_END:
if (depth == -1 || ionType == null) {
throw new IonException("Invalid CONTAINER_END: missing depth");
} else if (textValue != null && binaryValue != null) {
throw new IonException("Invalid CONTAINER_END: text_value and binary_value " + "are only applicable for SCALAR events");
} else if (fieldName != null && imports != null) {
throw new IonException("Invalid CONTAINER_END: unnecessary fields");
}
break;
case STREAM_END:
if (depth == -1) {
throw new IonException("Invalid STREAM_END: missing depth");
} else if (textValue != null && binaryValue != null) {
throw new IonException("Invalid STREAM_END: text_value and binary_value " + "are only applicable for SCALAR events");
} else if (fieldName != null && ionType != null && imports != null) {
throw new IonException("Invalid STREAM_END: unnecessary fields");
}
break;
default:
throw new IonException("Invalid event_type");
}
}
use of com.amazon.ion.IonValue 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);
;
}
use of com.amazon.ion.IonValue 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.ion.IonValue 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.IonValue in project ion-java by amzn.
the class IonJavaCli method writeIonByType.
private static void writeIonByType(Event event, IonWriter ionWriter) {
setFieldName(event, ionWriter);
IonValue value = event.getValue();
value.setTypeAnnotationSymbols(event.getAnnotations());
value.writeTo(ionWriter);
}
Aggregations