Search in sources :

Example 56 with IonException

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

the class IonReaderTextRawX method token_contents_load.

protected final StringBuilder token_contents_load(int token_type) throws IOException {
    StringBuilder sb = _current_value_buffer;
    boolean clob_chars_only;
    int c;
    if (_current_value_buffer_loaded) {
        return sb;
    } else if (_current_value_save_point_loaded) {
        assert (!_scanner.isUnfinishedToken() && !_current_value_save_point.isClear());
        // _scanner.load_save_point_contents( _current_value_save_point, sb);
        _scanner.save_point_activate(_current_value_save_point);
        switch(token_type) {
            default:
                _scanner.load_raw_characters(sb);
                break;
            case IonTokenConstsX.TOKEN_SYMBOL_IDENTIFIER:
                _scanner.load_symbol_identifier(sb);
                _value_type = IonType.SYMBOL;
                break;
            case IonTokenConstsX.TOKEN_SYMBOL_OPERATOR:
                _scanner.load_symbol_operator(sb);
                _value_type = IonType.SYMBOL;
                break;
            case IonTokenConstsX.TOKEN_SYMBOL_QUOTED:
                clob_chars_only = (IonType.CLOB == _value_type);
                _scanner.load_single_quoted_string(sb, clob_chars_only);
                _value_type = IonType.SYMBOL;
                break;
            case IonTokenConstsX.TOKEN_STRING_DOUBLE_QUOTE:
                clob_chars_only = (IonType.CLOB == _value_type);
                _scanner.load_double_quoted_string(sb, clob_chars_only);
                _value_type = IonType.STRING;
                break;
            case IonTokenConstsX.TOKEN_STRING_TRIPLE_QUOTE:
                clob_chars_only = (IonType.CLOB == _value_type);
                _scanner.load_triple_quoted_string(sb, clob_chars_only);
                _value_type = IonType.STRING;
                break;
        }
        _scanner.save_point_deactivate(_current_value_save_point);
        _current_value_buffer_loaded = true;
    } else {
        _scanner.save_point_start(_current_value_save_point);
        switch(token_type) {
            case IonTokenConstsX.TOKEN_UNKNOWN_NUMERIC:
            case IonTokenConstsX.TOKEN_INT:
            case IonTokenConstsX.TOKEN_BINARY:
            case IonTokenConstsX.TOKEN_HEX:
            case IonTokenConstsX.TOKEN_FLOAT:
            case IonTokenConstsX.TOKEN_DECIMAL:
            case IonTokenConstsX.TOKEN_TIMESTAMP:
                _value_type = _scanner.load_number(sb);
                break;
            case IonTokenConstsX.TOKEN_SYMBOL_IDENTIFIER:
                _scanner.load_symbol_identifier(sb);
                _value_type = IonType.SYMBOL;
                break;
            case IonTokenConstsX.TOKEN_SYMBOL_OPERATOR:
                _scanner.load_symbol_operator(sb);
                _value_type = IonType.SYMBOL;
                break;
            case IonTokenConstsX.TOKEN_SYMBOL_QUOTED:
                clob_chars_only = (IonType.CLOB == _value_type);
                c = _scanner.load_single_quoted_string(sb, clob_chars_only);
                if (c == UnifiedInputStreamX.EOF) {
                    // String message = "EOF encountered before closing single quote";
                    // parse_error(message);
                    _scanner.unexpected_eof();
                }
                _value_type = IonType.SYMBOL;
                break;
            case IonTokenConstsX.TOKEN_STRING_DOUBLE_QUOTE:
                clob_chars_only = (IonType.CLOB == _value_type);
                c = _scanner.load_double_quoted_string(sb, clob_chars_only);
                if (c == UnifiedInputStreamX.EOF) {
                    // String message = "EOF encountered before closing single quote";
                    // parse_error(message);
                    _scanner.unexpected_eof();
                }
                _value_type = IonType.STRING;
                break;
            case IonTokenConstsX.TOKEN_STRING_TRIPLE_QUOTE:
                clob_chars_only = (IonType.CLOB == _value_type);
                c = _scanner.load_triple_quoted_string(sb, clob_chars_only);
                if (c == UnifiedInputStreamX.EOF) {
                    // String message = "EOF encountered before closing single quote";
                    // parse_error(message);
                    _scanner.unexpected_eof();
                }
                _value_type = IonType.STRING;
                break;
            default:
                String message = "unexpected token " + IonTokenConstsX.getTokenName(token_type) + " encountered";
                throw new IonException(message);
        }
        _current_value_save_point.markEnd();
        _current_value_save_point_loaded = true;
        _current_value_buffer_loaded = true;
        tokenValueIsFinished();
    }
    return sb;
}
Also used : IonException(com.amazon.ion.IonException) SavePoint(com.amazon.ion.impl.UnifiedSavePointManagerX.SavePoint)

Example 57 with IonException

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

the class IonReaderTextRawX method stepIn.

public void stepIn() {
    if (_value_type == null || _eof) {
        throw new IllegalStateException();
    }
    switch(_value_type) {
        case STRUCT:
        case LIST:
        case SEXP:
            break;
        default:
            throw new IllegalStateException("Unexpected value type: " + _value_type);
    }
    int new_state = get_state_at_container_start(_value_type);
    set_state(new_state);
    push_container_state(_value_type);
    _scanner.tokenIsFinished();
    try {
        finish_value(null);
    } catch (IOException e) {
        throw new IonException(e);
    }
    if (_v.isNull()) {
        _eof = true;
        // there are no contents in a null container
        _has_next_called = true;
    }
    _value_type = null;
    if (_debug)
        System.out.println("stepInto() new depth: " + getDepth());
}
Also used : IonException(com.amazon.ion.IonException) IOException(java.io.IOException) SavePoint(com.amazon.ion.impl.UnifiedSavePointManagerX.SavePoint)

Example 58 with IonException

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

the class IonManagedBinaryWriterGoodTest method allGoodFiles.

@Test
public void allGoodFiles() throws Exception {
    byte[] testData = _Private_Utils.loadFileBytes(myTestFile);
    IonReader reader = IonReaderBuilder.standard().build(testData);
    writer.writeValues(reader);
    reader.close();
    writer.finish();
    final byte[] data = writer.getBytes();
    final IonValue actual;
    try {
        actual = system().getLoader().load(data);
    } catch (final Exception e) {
        throw new IonException("Bad generated data:\n" + hexDump(data), e);
    }
    final IonValue expected = system().getLoader().load(testData);
    assertEquals(expected, actual);
    additionalValueAssertions(actual);
}
Also used : IonValue(com.amazon.ion.IonValue) IonException(com.amazon.ion.IonException) IonReader(com.amazon.ion.IonReader) IonException(com.amazon.ion.IonException) Test(org.junit.Test)

Example 59 with IonException

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

the class IonRawBinaryWriterTest method assertValue.

protected final void assertValue(final String literal) throws IOException {
    writer.finish();
    final byte[] data = writer.getBytes();
    final IonValue actual;
    try {
        actual = system().singleValue(data);
    } catch (final Exception e) {
        throw new IonException("Bad generated data:\n" + hexDump(data), e);
    }
    final IonValue expected = system().singleValue(literal);
    assertEquals(expected, actual);
    additionalValueAssertions(actual);
    // prepare for next value
    writer.reset();
    writeIVMIfPossible();
}
Also used : IonValue(com.amazon.ion.IonValue) IonException(com.amazon.ion.IonException) IOException(java.io.IOException) IonException(com.amazon.ion.IonException)

Example 60 with IonException

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

the class LocalSymbolTable method readOneImport.

/**
 * Returns a {@link SymbolTable} representation of a single import
 * declaration from the passed-in reader and catalog.
 *
 * @return
 *          symbol table representation of the import; null if the import
 *          declaration is malformed
 */
private static SymbolTable readOneImport(IonReader ionRep, IonCatalog catalog) {
    assert (ionRep.getType() == IonType.STRUCT);
    String name = null;
    int version = -1;
    int maxid = -1;
    ionRep.stepIn();
    IonType t;
    while ((t = ionRep.next()) != null) {
        if (ionRep.isNullValue())
            continue;
        SymbolToken symTok = ionRep.getFieldNameSymbol();
        int field_id = symTok.getSid();
        if (field_id == UNKNOWN_SYMBOL_ID) {
            // this is a user defined reader or a pure DOM
            // we fall back to text here
            final String fieldName = ionRep.getFieldName();
            field_id = getSidForSymbolTableField(fieldName);
        }
        switch(field_id) {
            case NAME_SID:
                if (t == IonType.STRING) {
                    name = ionRep.stringValue();
                }
                break;
            case VERSION_SID:
                if (t == IonType.INT) {
                    version = ionRep.intValue();
                }
                break;
            case MAX_ID_SID:
                if (t == IonType.INT) {
                    maxid = ionRep.intValue();
                }
                break;
            default:
                // we just ignore anything else as "open content"
                break;
        }
    }
    ionRep.stepOut();
    // Ignore import clauses with malformed name field.
    if (name == null || name.length() == 0 || name.equals(ION)) {
        return null;
    }
    if (version < 1) {
        version = 1;
    }
    SymbolTable itab = null;
    if (catalog != null) {
        itab = catalog.getTable(name, version);
    }
    if (maxid < 0) {
        if (itab == null || version != itab.getVersion()) {
            String message = "Import of shared table " + IonTextUtils.printString(name) + " lacks a valid max_id field, but an exact match was not" + " found in the catalog";
            if (itab != null) {
                message += " (found version " + itab.getVersion() + ")";
            }
            // TODO custom exception
            throw new IonException(message);
        }
        // Exact match is found, but max_id is undefined in import
        // declaration, set max_id to largest sid of shared symtab
        maxid = itab.getMaxId();
    }
    if (itab == null) {
        assert maxid >= 0;
        // Construct substitute table with max_id undefined symbols
        itab = new SubstituteSymbolTable(name, version, maxid);
    } else if (itab.getVersion() != version || itab.getMaxId() != maxid) {
        // A match was found BUT specs are not an exact match
        // Construct a substitute with correct specs, containing the
        // original import table that was found
        itab = new SubstituteSymbolTable(itab, version, maxid);
    }
    return itab;
}
Also used : IonType(com.amazon.ion.IonType) SymbolToken(com.amazon.ion.SymbolToken) IonException(com.amazon.ion.IonException) SymbolTable(com.amazon.ion.SymbolTable)

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