Search in sources :

Example 11 with SymbolToken

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

the class IonWriterTestCase method testWritingAnnotations.

@Test
public void testWritingAnnotations() throws Exception {
    iw = makeWriter();
    IonDatagram expected = system().newDatagram();
    iw.addTypeAnnotation("a");
    iw.writeNull();
    IonValue v = expected.add().newNull();
    v.addTypeAnnotation("a");
    iw.addTypeAnnotation("b");
    iw.addTypeAnnotation("c");
    iw.writeNull();
    v = expected.add().newNull();
    v.addTypeAnnotation("b");
    v.addTypeAnnotation("c");
    iw.addTypeAnnotation("b");
    iw.addTypeAnnotation("b");
    iw.writeNull();
    v = expected.add().newNull();
    v.setTypeAnnotations("b", "b");
    iw.setTypeAnnotations("b", "b");
    iw.writeNull();
    v = expected.add().newNull();
    v.setTypeAnnotations("b", "b");
    iw.addTypeAnnotation("b");
    iw.setTypeAnnotations("c", "d");
    iw.writeNull();
    v = expected.add().newNull();
    v.setTypeAnnotations("c", "d");
    iw.addTypeAnnotation("b");
    iw.setTypeAnnotations(new String[0]);
    iw.writeNull();
    v = expected.add().newNull();
    v.clearTypeAnnotations();
    iw.addTypeAnnotation("b");
    iw.setTypeAnnotations((String[]) null);
    iw.writeNull();
    v = expected.add().newNull();
    v.clearTypeAnnotations();
    iw.addTypeAnnotation("b");
    iw.setTypeAnnotations();
    iw.writeNull();
    v = expected.add().newNull();
    v.clearTypeAnnotations();
    iw.addTypeAnnotation("b");
    iw.setTypeAnnotationSymbols(new SymbolToken[0]);
    iw.writeNull();
    v = expected.add().newNull();
    v.clearTypeAnnotations();
    iw.addTypeAnnotation("b");
    iw.setTypeAnnotationSymbols((SymbolToken[]) null);
    iw.writeNull();
    v = expected.add().newNull();
    v.clearTypeAnnotations();
    iw.addTypeAnnotation("b");
    iw.setTypeAnnotationSymbols();
    iw.writeNull();
    v = expected.add().newNull();
    v.clearTypeAnnotations();
    assertEquals(expected, reload());
}
Also used : IonValue(com.amazon.ion.IonValue) SymbolToken(com.amazon.ion.SymbolToken) com.amazon.ion.impl._Private_Utils.newSymbolToken(com.amazon.ion.impl._Private_Utils.newSymbolToken) FakeSymbolToken(com.amazon.ion.FakeSymbolToken) IonDatagram(com.amazon.ion.IonDatagram) IonString(com.amazon.ion.IonString) Test(org.junit.Test)

Example 12 with SymbolToken

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

the class LocalSymbolTableTest method testInternUnknownText.

@Test
public void testInternUnknownText() {
    SymbolTable st = makeLocalSymtab(system(), LOCAL_SYMBOLS_ABC, ST_FRED_V2, ST_GINGER_V1);
    String D = "d";
    checkUnknownSymbol(D, st);
    SymbolToken tok = st.intern(D);
    assertSame(D, tok.getText());
    assertEquals(st.getImportedMaxId() + 4, tok.getSid());
    // Force a new instance
    tok = st.intern(new String(D));
    assertSame(D, tok.getText());
    assertEquals(st.getImportedMaxId() + 4, tok.getSid());
}
Also used : SymbolToken(com.amazon.ion.SymbolToken) SymbolTable(com.amazon.ion.SymbolTable) com.amazon.ion.impl._Private_Utils.copyLocalSymbolTable(com.amazon.ion.impl._Private_Utils.copyLocalSymbolTable) Test(org.junit.Test)

Example 13 with SymbolToken

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

the class LocalSymbolTableTest method testFindSymbolToken.

// -------------------------------------------------------------------------
// find()
public void testFindSymbolToken(SymbolTable st) {
    // Existing symbol from imports
    String fredSym = ST_FRED_V2.findKnownSymbol(3);
    SymbolToken tok = st.find(new String(fredSym));
    assertSame(fredSym, tok.getText());
    assertSame(st.getSystemSymbolTable().getMaxId() + 3, tok.getSid());
    String gingerSym = ST_GINGER_V1.findKnownSymbol(1);
    tok = st.find(new String(gingerSym));
    assertSame(gingerSym, tok.getText());
    assertSame(st.getSystemSymbolTable().getMaxId() + ST_FRED_V2.getMaxId() + 1, tok.getSid());
    // Existing local symbol
    tok = st.find(OTHER_A);
    assertSame(A, tok.getText());
    assertEquals(st.getImportedMaxId() + 1, tok.getSid());
    // Non-existing symbol
    assertEquals(null, st.find("not there"));
}
Also used : SymbolToken(com.amazon.ion.SymbolToken)

Example 14 with SymbolToken

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

the class LocalSymbolTableTest method internKnownText.

// -------------------------------------------------------------------------
// intern()
public void internKnownText(SymbolTable st) {
    // Existing symbol from imports
    String fredSym = ST_FRED_V2.findKnownSymbol(3);
    SymbolToken tok = st.intern(new String(fredSym));
    assertSame(fredSym, tok.getText());
    assertSame(st.getSystemSymbolTable().getMaxId() + 3, tok.getSid());
    String gingerSym = ST_GINGER_V1.findKnownSymbol(1);
    tok = st.intern(new String(gingerSym));
    assertSame(gingerSym, tok.getText());
    assertSame(st.getSystemSymbolTable().getMaxId() + ST_FRED_V2.getMaxId() + 1, tok.getSid());
    // Existing local symbol
    tok = st.intern(OTHER_A);
    assertSame(A, tok.getText());
    assertEquals(st.getImportedMaxId() + 1, tok.getSid());
}
Also used : SymbolToken(com.amazon.ion.SymbolToken)

Example 15 with SymbolToken

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

the class IonSystemLite method load_value_helper.

private IonValueLite load_value_helper(IonReader reader, boolean isTopLevel) {
    boolean symbol_is_present = false;
    IonType t = reader.getType();
    if (t == null) {
        return null;
    }
    IonValueLite v;
    if (reader.isNullValue()) {
        v = newNull(t);
    } else {
        switch(t) {
            case BOOL:
                v = newBool(reader.booleanValue());
                break;
            case INT:
                // TODO amzn/ion-java/issues/9  Inefficient since we can't determine the size
                // of the integer in order to avoid making BigIntegers.
                v = newInt(reader.bigIntegerValue());
                break;
            case FLOAT:
                v = newFloat(reader.doubleValue());
                break;
            case DECIMAL:
                v = newDecimal(reader.decimalValue());
                break;
            case TIMESTAMP:
                v = newTimestamp(reader.timestampValue());
                break;
            case SYMBOL:
                v = newSymbol(reader.symbolValue());
                symbol_is_present = true;
                break;
            case STRING:
                v = newString(reader.stringValue());
                break;
            case CLOB:
                v = newClob(reader.newBytes());
                break;
            case BLOB:
                v = newBlob(reader.newBytes());
                break;
            case LIST:
                v = newEmptyList();
                break;
            case SEXP:
                v = newEmptySexp();
                break;
            case STRUCT:
                v = newEmptyStruct();
                break;
            default:
                throw new IonException("unexpected type encountered reading value: " + t.toString());
        }
    }
    // Forget any incoming SIDs on field names.
    if (!isTopLevel && reader.isInStruct()) {
        SymbolToken token = reader.getFieldNameSymbol();
        String text = token.getText();
        if (text != null && token.getSid() != UNKNOWN_SYMBOL_ID) {
            token = newSymbolToken(text, UNKNOWN_SYMBOL_ID);
        }
        v.setFieldNameSymbol(token);
        symbol_is_present = true;
    }
    // Forget any incoming SIDs on annotations.
    // This is a fresh array so we can modify it:
    SymbolToken[] annotations = reader.getTypeAnnotationSymbols();
    if (annotations.length != 0) {
        for (int i = 0; i < annotations.length; i++) {
            SymbolToken token = annotations[i];
            String text = token.getText();
            if (text != null && token.getSid() != UNKNOWN_SYMBOL_ID) {
                annotations[i] = newSymbolToken(text, UNKNOWN_SYMBOL_ID);
            }
        }
        v.setTypeAnnotationSymbols(annotations);
        symbol_is_present = true;
    }
    if (!reader.isNullValue()) {
        switch(t) {
            case BOOL:
            case INT:
            case FLOAT:
            case DECIMAL:
            case TIMESTAMP:
            case SYMBOL:
            case STRING:
            case CLOB:
            case BLOB:
                break;
            case LIST:
            case SEXP:
            case STRUCT:
                // fieldname and annotations off of the parent container
                if (load_children((IonContainerLite) v, reader)) {
                    symbol_is_present = true;
                }
                break;
            default:
                throw new IonException("unexpected type encountered reading value: " + t.toString());
        }
    }
    if (symbol_is_present) {
        v._isSymbolPresent(true);
    }
    return v;
}
Also used : IonType(com.amazon.ion.IonType) SymbolToken(com.amazon.ion.SymbolToken) com.amazon.ion.impl._Private_Utils.newSymbolToken(com.amazon.ion.impl._Private_Utils.newSymbolToken) IonException(com.amazon.ion.IonException) IonTextUtils.printString(com.amazon.ion.util.IonTextUtils.printString)

Aggregations

SymbolToken (com.amazon.ion.SymbolToken)68 SymbolTable (com.amazon.ion.SymbolTable)14 com.amazon.ion.impl._Private_Utils.newSymbolToken (com.amazon.ion.impl._Private_Utils.newSymbolToken)13 IonType (com.amazon.ion.IonType)10 IonValue (com.amazon.ion.IonValue)10 IonException (com.amazon.ion.IonException)9 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)6 IonStruct (com.amazon.ion.IonStruct)4 IOException (java.io.IOException)4 Event (com.amazon.tools.events.Event)3 EventType (com.amazon.tools.events.EventType)3 FakeSymbolToken (com.amazon.ion.FakeSymbolToken)2 IonDatagram (com.amazon.ion.IonDatagram)2 IonSequence (com.amazon.ion.IonSequence)2 IonString (com.amazon.ion.IonString)2 UnknownSymbolException (com.amazon.ion.UnknownSymbolException)2 SavePoint (com.amazon.ion.impl.UnifiedSavePointManagerX.SavePoint)2 ImportDescriptor (com.amazon.tools.events.ImportDescriptor)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2