Search in sources :

Example 36 with IonException

use of com.amazon.ion.IonException in project ion-java by amzn.

the class IonTokenReader method scanIdentifier.

public Type scanIdentifier(int c) throws IOException {
    // reset our local value buffer
    resetValue();
    // we don't have an identifier type any longer, just string
    this.t = Type.constSymbol;
    // some strings are keywords, most are not
    // mostly we'll guess that it's not a keyword
    this.keyword = null;
    if (!readIdentifierContents(c)) {
        // not quoted (true is quoted)
        // anything in quotes (even single quotes) is not a keyword
        // and here we're not in quoted content (that is handled above)
        // so see if it's also a keyword
        this.keyword = IonTokenReader.matchKeyword(value, 0, value.length());
        if (this.keyword != null) {
            if (this.keyword == Type.kwNull) {
                c = this.read();
                if (c == '.') {
                    int dot = value.length();
                    value.append((char) c);
                    c = this.read();
                    // +1 is "enough roap" so if there are extra letters at the end of the keyword we keep at least 1
                    int added = readIdentifierContents(c, IonTokenConstsX.TN_MAX_NAME_LENGTH + 1);
                    int kw = IonTokenConstsX.keyword(value, dot + 1, dot + added + 1);
                    switch(kw) {
                        case IonTokenConstsX.KEYWORD_NULL:
                        case IonTokenConstsX.KEYWORD_BOOL:
                        case IonTokenConstsX.KEYWORD_INT:
                        case IonTokenConstsX.KEYWORD_FLOAT:
                        case IonTokenConstsX.KEYWORD_DECIMAL:
                        case IonTokenConstsX.KEYWORD_TIMESTAMP:
                        case IonTokenConstsX.KEYWORD_SYMBOL:
                        case IonTokenConstsX.KEYWORD_STRING:
                        case IonTokenConstsX.KEYWORD_BLOB:
                        case IonTokenConstsX.KEYWORD_CLOB:
                        case IonTokenConstsX.KEYWORD_LIST:
                        case IonTokenConstsX.KEYWORD_SEXP:
                        case IonTokenConstsX.KEYWORD_STRUCT:
                            this.keyword = setNullType(value, dot + 1, value.length() - dot - 1);
                            break;
                        default:
                            // not a valid type name - so we have to unread back to the dot
                            for (int ii = value.length(); ii > dot; ) {
                                ii--;
                                char uc = value.charAt(ii);
                                unread(uc);
                            }
                            String message = position() + ": Expected Ion type after 'null.' but found: " + value;
                            throw new IonException(message);
                    }
                } else {
                    unread(c);
                }
            }
            this.t = this.keyword;
            return this.t;
        }
    }
    // see if we're a user type of a member name
    c = this.readIgnoreWhitespace();
    if (c != ':') {
        unread(c);
    } else {
        c = read();
        if (c != ':') {
            unread(c);
            this.t = Type.constMemberName;
        } else {
            this.t = Type.constUserTypeDecl;
        }
    }
    return this.t;
}
Also used : IonException(com.amazon.ion.IonException) IonTextUtils.printCodePointAsString(com.amazon.ion.util.IonTextUtils.printCodePointAsString)

Example 37 with IonException

use of com.amazon.ion.IonException in project ion-java by amzn.

the class IonReaderBuilderTest method testEnableIncrementalReading.

@Test
public void testEnableIncrementalReading() throws IOException {
    IonReaderBuilder builder = IonReaderBuilder.standard();
    assertFalse(builder.isIncrementalReadingEnabled());
    builder.withIncrementalReadingEnabled(true);
    assertTrue(builder.isIncrementalReadingEnabled());
    builder.setIncrementalReadingDisabled();
    assertFalse(builder.isIncrementalReadingEnabled());
    builder.setIncrementalReadingEnabled();
    assertTrue(builder.isIncrementalReadingEnabled());
    builder.withIncrementalReadingEnabled(false);
    assertFalse(builder.isIncrementalReadingEnabled());
    ByteArrayOutputStream data = new ByteArrayOutputStream();
    data.write(_Private_IonConstants.BINARY_VERSION_MARKER_1_0);
    // 5-byte annotation wrapper (incomplete).
    data.write(0xE5);
    IonReader reader1 = builder.build(data.toByteArray());
    try {
        reader1.next();
        fail();
    } catch (IonException e) {
    // Expected; this is a non-incremental reader, but a complete value was not available.
    }
    builder.withIncrementalReadingEnabled(true);
    IonReader reader2 = builder.build(data.toByteArray());
    assertNull(reader2.next());
    IonReader reader3 = builder.build(new ByteArrayInputStream(data.toByteArray()));
    assertNull(reader3.next());
}
Also used : IonException(com.amazon.ion.IonException) ByteArrayInputStream(java.io.ByteArrayInputStream) IonReader(com.amazon.ion.IonReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 38 with IonException

use of com.amazon.ion.IonException in project ion-hive-serde by amzn.

the class IonHiveSerDe method deserialize.

@Override
public Object deserialize(final Writable blob) throws SerDeException {
    final byte[] bytes;
    final int length;
    // TextFormat which produces Text
    if (blob instanceof Text) {
        Text text = (Text) blob;
        // getBytes returns a reference to the current buffer which is only valid up to length
        bytes = text.getBytes();
        length = text.getLength();
    } else if (blob instanceof BytesWritable) {
        BytesWritable bytesWritable = (BytesWritable) blob;
        // getBytes returns a reference to the current buffer which is only valid up to length
        bytes = bytesWritable.getBytes();
        length = bytesWritable.getLength();
    } else {
        throw new SerDeException("Invalid Writable instance, must be either Text or BytesWritable, was " + blob.getClass());
    }
    final IonSystem domFactory = ionFactory.getDomFactory();
    try (final IonReader reader = ionFactory.newReader(bytes, 0, length)) {
        /*
                We're using an IonStruct here because:
                1. We need a key-value store to carry column values
                2. The top-level IonStruct as a context object carries the IonSystem which we use as a ValueFactory in
                   the callbacks created in PathExtractionConfig
                Refer to https://github.com/amzn/ion-hive-serde/issues/61.
            */
        IonStruct struct = domFactory.newEmptyStruct();
        if (!serDeProperties.pathExtractorCaseSensitivity()) {
            struct = new IonStructCaseInsensitiveDecorator(struct);
        }
        final PathExtractor<IonStruct> pathExtractor = serDeProperties.pathExtractor();
        pathExtractor.match(reader, struct);
        return struct;
    } catch (IonException e) {
        // skips if ignoring malformed
        if (serDeProperties.getIgnoreMalformed()) {
            return null;
        }
        throw e;
    } catch (IOException e) {
        throw new SerDeException(e);
    }
}
Also used : IonSystem(com.amazon.ion.IonSystem) IonStruct(com.amazon.ion.IonStruct) IonException(com.amazon.ion.IonException) IonReader(com.amazon.ion.IonReader) IonStructCaseInsensitiveDecorator(com.amazon.ionhiveserde.caseinsensitivedecorator.IonStructCaseInsensitiveDecorator) Text(org.apache.hadoop.io.Text) BytesWritable(org.apache.hadoop.io.BytesWritable) IOException(java.io.IOException) SerDeException(org.apache.hadoop.hive.serde2.SerDeException)

Example 39 with IonException

use of com.amazon.ion.IonException in project ion-java by amzn.

the class IonJavaCli method compareEquivs.

private static boolean compareEquivs(CompareContext compareContext) throws IOException {
    int i = 0;
    int j = 0;
    List<Event> eventStreamFirst = compareContext.getEventStreamFirst();
    List<Event> eventStreamSecond = compareContext.getEventStreamSecond();
    ComparisonType type = compareContext.getType();
    while (i < eventStreamFirst.size() && j < eventStreamSecond.size()) {
        Event eventFirst = eventStreamFirst.get(i);
        Event eventSecond = eventStreamSecond.get(j);
        if (eventFirst.getEventType() == EventType.STREAM_END && eventSecond.getEventType() == EventType.STREAM_END) {
            break;
        } else if (eventFirst.getEventType() == EventType.STREAM_END || eventSecond.getEventType() == EventType.STREAM_END) {
            setReportInfo(i, j, "The input streams had a different number of comparison sets.", compareContext);
            return type == ComparisonType.NON_EQUIVS;
        } else if (!(eventFirst.getIonType() == IonType.LIST || eventFirst.getIonType() == IonType.SEXP) || !(eventSecond.getIonType() == IonType.LIST || eventSecond.getIonType() == IonType.SEXP)) {
            throw new IonException("Comparison sets must be lists or s-expressions.");
        } else if (isEmbeddedEvent(eventFirst) ^ isEmbeddedEvent(eventSecond)) {
            throw new IonException("Embedded streams set expected.");
        }
        List<Pair> pairsFirst;
        List<Pair> pairsSecond;
        if (isEmbeddedEvent(eventFirst) && isEmbeddedEvent(eventSecond)) {
            pairsFirst = locateEmbeddedStreamBoundaries(eventStreamFirst, i);
            pairsSecond = locateEmbeddedStreamBoundaries(eventStreamSecond, j);
        } else {
            pairsFirst = locateContainerBoundaries(eventStreamFirst, i);
            pairsSecond = locateContainerBoundaries(eventStreamSecond, j);
        }
        i = pairsFirst.size() == 0 ? i + 1 : pairsFirst.get(pairsFirst.size() - 1).right + 1;
        j = pairsSecond.size() == 0 ? j + 1 : pairsSecond.get(pairsSecond.size() - 1).right + 1;
        for (int m = 0; m < pairsFirst.size(); m++) {
            for (int n = 0; n < pairsSecond.size(); n++) {
                if (compareContext.getType() == ComparisonType.NON_EQUIVS) {
                    if (m == n)
                        continue;
                }
                Pair pairFirst = pairsFirst.get(m);
                Pair pairSecond = pairsSecond.get(n);
                if (compare(compareContext, pairFirst.left, pairFirst.right, pairSecond.left, pairSecond.right) ^ (type == ComparisonType.EQUIVS || type == ComparisonType.EQUIVS_TIMELINE)) {
                    if (type == ComparisonType.NON_EQUIVS) {
                        setReportInfo(pairFirst.left, pairSecond.left, "Equivalent values in a non-equivs set.", compareContext);
                    }
                    return type == ComparisonType.NON_EQUIVS;
                }
            }
        }
        i++;
        j++;
    }
    return (type == ComparisonType.EQUIVS || type == ComparisonType.EQUIVS_TIMELINE);
}
Also used : IonException(com.amazon.ion.IonException) Event(com.amazon.tools.events.Event)

Example 40 with IonException

use of com.amazon.ion.IonException in project ion-java by amzn.

the class IonJavaCli method processFiles.

// 
// 
// functions for processing
// 
// 
private static void processFiles(IonWriter ionWriterForErrorReport, CommandArgs args, ProcessContext processContext) throws IOException {
    boolean finish = false;
    for (String path : args.getInputFiles()) {
        try (InputStream inputStream = new BufferedInputStream(new FileInputStream(path));
            IonReader ionReader = IonReaderBuilder.standard().build(inputStream)) {
            processContext.setFileName(path);
            ReadContext readContext = new ReadContext(new ArrayList<>());
            try {
                getEventStream(ionReader, CommandType.PROCESS, readContext);
            } catch (IonException | NullPointerException e) {
                new ErrorDescription(readContext.getState(), e.getMessage(), processContext.getFileName(), processContext.getEventIndex()).writeOutput(ionWriterForErrorReport);
                finish = true;
            } catch (Exception e) {
                new ErrorDescription(ErrorType.STATE, e.getMessage(), processContext.getFileName(), processContext.getEventIndex()).writeOutput(ionWriterForErrorReport);
                finish = true;
            }
            processContext.setEventStream(readContext.getEventStream());
            processContext.setEventIndex(0);
            if (args.getOutputFormat() == OutputFormat.EVENTS) {
                processContext.getIonWriter().writeSymbol(EVENT_STREAM);
                processToEventStream(ionWriterForErrorReport, processContext);
            } else {
                processToIonStream(processContext, args);
            }
            if (finish)
                System.exit(IO_ERROR_EXIT_CODE);
            processContext.getIonWriter().finish();
            ionWriterForErrorReport.finish();
        } catch (IonException | NullPointerException e) {
            new ErrorDescription(processContext.getState(), e.getMessage(), processContext.getFileName(), processContext.getEventIndex()).writeOutput(ionWriterForErrorReport);
            System.exit(IO_ERROR_EXIT_CODE);
        } catch (Exception e) {
            new ErrorDescription(ErrorType.STATE, e.getMessage(), processContext.getFileName(), processContext.getEventIndex()).writeOutput(ionWriterForErrorReport);
            System.exit(IO_ERROR_EXIT_CODE);
        }
    }
}
Also used : ErrorDescription(com.amazon.tools.errorReport.ErrorDescription) IonException(com.amazon.ion.IonException) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) IonException(com.amazon.ion.IonException) CmdLineException(org.kohsuke.args4j.CmdLineException) BufferedInputStream(java.io.BufferedInputStream) IonReader(com.amazon.ion.IonReader)

Aggregations

IonException (com.amazon.ion.IonException)66 IOException (java.io.IOException)31 IonReader (com.amazon.ion.IonReader)10 IonType (com.amazon.ion.IonType)9 SymbolToken (com.amazon.ion.SymbolToken)9 IonValue (com.amazon.ion.IonValue)7 Event (com.amazon.tools.events.Event)7 Test (org.junit.Test)6 IonStruct (com.amazon.ion.IonStruct)5 SymbolTable (com.amazon.ion.SymbolTable)5 SavePoint (com.amazon.ion.impl.UnifiedSavePointManagerX.SavePoint)5 InputStream (java.io.InputStream)5 ArrayList (java.util.ArrayList)5 EventType (com.amazon.tools.events.EventType)4 IonWriter (com.amazon.ion.IonWriter)3 IonDatagram (com.amazon.ion.IonDatagram)2 IonLob (com.amazon.ion.IonLob)2 Timestamp (com.amazon.ion.Timestamp)2 BufferManager (com.amazon.ion.impl.IonBinary.BufferManager)2 com.amazon.ion.impl._Private_IonWriter (com.amazon.ion.impl._Private_IonWriter)2