Search in sources :

Example 1 with ImportDescriptor

use of com.amazon.tools.events.ImportDescriptor 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);
    ;
}
Also used : IonValue(com.amazon.ion.IonValue) IonType(com.amazon.ion.IonType) IonException(com.amazon.ion.IonException) IonReader(com.amazon.ion.IonReader) Event(com.amazon.tools.events.Event) SymbolTable(com.amazon.ion.SymbolTable) ImportDescriptor(com.amazon.tools.events.ImportDescriptor)

Example 2 with ImportDescriptor

use of com.amazon.tools.events.ImportDescriptor in project ion-java by amzn.

the class IonJavaCli method ionStreamToImportDescriptors.

private static ImportDescriptor[] ionStreamToImportDescriptors(IonReader ionReader) {
    ArrayList<ImportDescriptor> imports = new ArrayList<>();
    ionReader.stepIn();
    while (ionReader.next() != null) {
        String importName = null;
        int maxId = 0;
        int version = 0;
        ionReader.stepIn();
        while (ionReader.next() != null) {
            switch(ionReader.getFieldName()) {
                case "name":
                    importName = ionReader.stringValue();
                    break;
                case "max_id":
                    maxId = ionReader.intValue();
                    break;
                case "version":
                    version = ionReader.intValue();
                    break;
            }
        }
        ionReader.stepOut();
        ImportDescriptor table = new ImportDescriptor(importName, maxId, version);
        imports.add(table);
    }
    ImportDescriptor[] importsArray = imports.toArray(new ImportDescriptor[0]);
    ionReader.stepOut();
    return importsArray;
}
Also used : ArrayList(java.util.ArrayList) ImportDescriptor(com.amazon.tools.events.ImportDescriptor)

Example 3 with ImportDescriptor

use of com.amazon.tools.events.ImportDescriptor in project ion-java by amzn.

the class IonJavaCli method symbolTableToImports.

private static ImportDescriptor[] symbolTableToImports(SymbolTable[] tables) {
    if (tables == null || tables.length == 0)
        return null;
    int size = tables.length;
    ImportDescriptor[] imports = new ImportDescriptor[size];
    for (int i = 0; i < size; i++) {
        ImportDescriptor table = new ImportDescriptor(tables[i]);
        imports[i] = table;
    }
    return imports;
}
Also used : ImportDescriptor(com.amazon.tools.events.ImportDescriptor)

Example 4 with ImportDescriptor

use of com.amazon.tools.events.ImportDescriptor 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);
}
Also used : IonValue(com.amazon.ion.IonValue) IonType(com.amazon.ion.IonType) SymbolToken(com.amazon.ion.SymbolToken) IonException(com.amazon.ion.IonException) EventType(com.amazon.tools.events.EventType) ArrayList(java.util.ArrayList) Event(com.amazon.tools.events.Event) ImportDescriptor(com.amazon.tools.events.ImportDescriptor)

Example 5 with ImportDescriptor

use of com.amazon.tools.events.ImportDescriptor in project ion-java by amzn.

the class IonJavaCli method handleSymbolTableEvent.

// 
// 
// helper functions
// 
// 
private static void handleSymbolTableEvent(ProcessContext processContext, Event event, CommandArgs args, boolean isEmbedded) throws IOException {
    processContext.getIonWriter().close();
    ImportDescriptor[] imports = event.getImports();
    SymbolTable[] symbolTables = new SymbolTable[imports.length];
    for (int i = 0; i < imports.length; i++) {
        SymbolTable symbolTable = ION_SYSTEM.newSharedSymbolTable(imports[i].getImportName(), imports[i].getVersion(), null);
        symbolTables[i] = symbolTable;
    }
    if (!isEmbedded) {
        OutputStream out = args.getOutputFile() == null ? new NoCloseOutputStream(System.out) : new FileOutputStream(processContext.getFile(), true);
        processContext.setIonWriter(args.getOutputFormat().createIonWriterWithImports(out, symbolTables));
    } else {
        processContext.getEmbeddedOut().append(" ");
        processContext.setIonWriter(ION_TEXT_WRITER_BUILDER.withImports(symbolTables).build(processContext.getEmbeddedOut()));
    }
}
Also used : BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) SymbolTable(com.amazon.ion.SymbolTable) ImportDescriptor(com.amazon.tools.events.ImportDescriptor)

Aggregations

ImportDescriptor (com.amazon.tools.events.ImportDescriptor)6 IonType (com.amazon.ion.IonType)3 IonValue (com.amazon.ion.IonValue)3 Event (com.amazon.tools.events.Event)3 IonException (com.amazon.ion.IonException)2 SymbolTable (com.amazon.ion.SymbolTable)2 SymbolToken (com.amazon.ion.SymbolToken)2 EventType (com.amazon.tools.events.EventType)2 ArrayList (java.util.ArrayList)2 IonReader (com.amazon.ion.IonReader)1 BufferedOutputStream (java.io.BufferedOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1