use of com.amazon.ion.IonException 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.IonException 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.IonException 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.ion.IonException in project ion-java by amzn.
the class BaseApp method processStdIn.
protected void processStdIn() {
try {
byte[] buffer = loadAsByteArray(System.in);
IonReader reader = mySystem.newReader(buffer);
process(reader);
} catch (IonException e) {
System.err.println("An error occurred while processing stdin");
System.err.println(e.getMessage());
} catch (IOException e) {
System.err.println("An error occurred while processing stdin");
System.err.println(e.getMessage());
}
}
use of com.amazon.ion.IonException in project ion-java by amzn.
the class IonReaderBinaryIncremental method timestampValue.
@Override
public Timestamp timestampValue() {
requireType(IonType.TIMESTAMP);
if (isNullValue()) {
return null;
}
peekIndex = valueStartPosition;
int firstByte = buffer.peek(peekIndex++);
Integer offset = null;
if (firstByte != VAR_INT_NEGATIVE_ZERO) {
offset = readVarInt(firstByte);
}
int year = readVarUInt();
int month = 0;
int day = 0;
int hour = 0;
int minute = 0;
int second = 0;
BigDecimal fractionalSecond = null;
Timestamp.Precision precision = Timestamp.Precision.YEAR;
if (peekIndex < valueEndPosition) {
month = readVarUInt();
precision = Timestamp.Precision.MONTH;
if (peekIndex < valueEndPosition) {
day = readVarUInt();
precision = Timestamp.Precision.DAY;
if (peekIndex < valueEndPosition) {
hour = readVarUInt();
if (peekIndex >= valueEndPosition) {
throw new IonException("Timestamps may not specify hour without specifying minute.");
}
minute = readVarUInt();
precision = Timestamp.Precision.MINUTE;
if (peekIndex < valueEndPosition) {
second = readVarUInt();
precision = Timestamp.Precision.SECOND;
if (peekIndex < valueEndPosition) {
fractionalSecond = readBigDecimal();
if (fractionalSecond.signum() < 0 || fractionalSecond.compareTo(BigDecimal.ONE) >= 0) {
throw new IonException("The fractional seconds value in a timestamp must be greater" + "than or equal to zero and less than one.");
}
}
}
}
}
}
try {
return Timestamp.createFromUtcFields(precision, year, month, day, hour, minute, second, fractionalSecond, offset);
} catch (IllegalArgumentException e) {
throw new IonException("Illegal timestamp encoding. ", e);
}
}
Aggregations