private static void validateEvent()

in ion-java-cli/src/com/amazon/tools/cli/IonJavaCli.java [1129:1196]


    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");
        }
    }