Search in sources :

Example 16 with SymbolToken

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

the class IonValueLite method writeTo.

final void writeTo(IonWriter writer, SymbolTableProvider symbolTableProvider) {
    if (writer.isInStruct() && !((_Private_IonWriter) writer).isFieldNameSet()) {
        SymbolToken tok = getFieldNameSymbol(symbolTableProvider);
        if (tok == null) {
            throw new IllegalStateException("Field name not set");
        }
        writer.setFieldNameSymbol(tok);
    }
    SymbolToken[] annotations = getTypeAnnotationSymbols(symbolTableProvider);
    writer.setTypeAnnotationSymbols(annotations);
    try {
        writeBodyTo(writer, symbolTableProvider);
    } catch (IOException e) {
        throw new IonException(e);
    }
}
Also used : SymbolToken(com.amazon.ion.SymbolToken) com.amazon.ion.impl._Private_Utils.newSymbolToken(com.amazon.ion.impl._Private_Utils.newSymbolToken) IonException(com.amazon.ion.IonException) com.amazon.ion.impl._Private_IonWriter(com.amazon.ion.impl._Private_IonWriter) IOException(java.io.IOException)

Example 17 with SymbolToken

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

the class IonValueLite method getFieldNameSymbol.

public final SymbolToken getFieldNameSymbol(SymbolTableProvider symbolTableProvider) {
    int sid = _fieldId;
    String text = _fieldName;
    if (text != null) {
        if (sid == UNKNOWN_SYMBOL_ID) {
            SymbolToken tok = symbolTableProvider.getSymbolTable().find(text);
            if (tok != null) {
                return tok;
            }
        }
    } else if (sid > 0) {
        text = symbolTableProvider.getSymbolTable().findKnownSymbol(sid);
    } else if (sid != 0) {
        // not a struct field
        return null;
    }
    return _Private_Utils.newSymbolToken(text, sid);
}
Also used : SymbolToken(com.amazon.ion.SymbolToken) com.amazon.ion.impl._Private_Utils.newSymbolToken(com.amazon.ion.impl._Private_Utils.newSymbolToken)

Example 18 with SymbolToken

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

the class IonValueLite method attemptClearSymbolIDValues.

/**
 * Sets this value's symbol table to null, and erases any SIDs here and
 * recursively.
 *
 * @return true if all SID's have been successfully removed, otherwise false
 */
boolean attemptClearSymbolIDValues() {
    boolean sidsRemain = false;
    if (_fieldName != null) {
        _fieldId = UNKNOWN_SYMBOL_ID;
    } else if (_fieldId > UNKNOWN_SYMBOL_ID) {
        // retaining the field SID, as it couldn't be cleared due to loss of context
        // TODO - for SID handling consistency; this should attempt resolution first
        sidsRemain = true;
    }
    if (_annotations != null) {
        for (int i = 0; i < _annotations.length; i++) {
            SymbolToken annotation = _annotations[i];
            // _annotations may have nulls at the end.
            if (annotation == null)
                break;
            String text = annotation.getText();
            // TODO - for SID handling consistency; this should attempt resolution first, not just drop entry
            if (text != null && annotation.getSid() != UNKNOWN_SYMBOL_ID) {
                _annotations[i] = newSymbolToken(text, UNKNOWN_SYMBOL_ID);
            }
        }
    }
    return !sidsRemain;
}
Also used : SymbolToken(com.amazon.ion.SymbolToken) com.amazon.ion.impl._Private_Utils.newSymbolToken(com.amazon.ion.impl._Private_Utils.newSymbolToken)

Example 19 with SymbolToken

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

the class IonValueLite method getTypeAnnotationSymbols.

public final SymbolToken[] getTypeAnnotationSymbols(SymbolTableProvider symbolTableProvider) {
    // first we have to count the number of non-null
    // elements there are in the annotations array
    int count = 0;
    if (_annotations != null) {
        for (int i = 0; i < _annotations.length; i++) {
            if (_annotations[i] != null) {
                count++;
            }
        }
    }
    // if there aren't any, we're done
    if (count == 0) {
        return SymbolToken.EMPTY_ARRAY;
    }
    SymbolToken[] users_copy = new SymbolToken[count];
    for (int i = 0; i < count; i++) {
        SymbolToken token = _annotations[i];
        String text = token.getText();
        if (text != null && token.getSid() == UNKNOWN_SYMBOL_ID) {
            // TODO amzn/ion-java/issues/27 We should memoize the result of symtab lookups
            // into _annotations.
            // See getFieldNameSymbol() for challenges doing so.
            SymbolToken interned = symbolTableProvider.getSymbolTable().find(text);
            if (interned != null) {
                token = interned;
            }
        }
        users_copy[i] = token;
    }
    return users_copy;
}
Also used : SymbolToken(com.amazon.ion.SymbolToken) com.amazon.ion.impl._Private_Utils.newSymbolToken(com.amazon.ion.impl._Private_Utils.newSymbolToken)

Example 20 with SymbolToken

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

the class IonValueLite method hashTypeAnnotations.

protected int hashTypeAnnotations(final int original, SymbolTableProvider symbolTableProvider) {
    final SymbolToken[] tokens = getTypeAnnotationSymbols(symbolTableProvider);
    if (tokens.length == 0) {
        return original;
    }
    // prime to salt sid of annotation
    final int sidHashSalt = 127;
    // prime to salt text of annotation
    final int textHashSalt = 31;
    final int prime = 8191;
    int result = original ^ TYPE_ANNOTATION_HASH_SIGNATURE;
    result = prime * original + tokens.length;
    for (final SymbolToken token : tokens) {
        String text = token.getText();
        int tokenHashCode = text == null ? token.getSid() * sidHashSalt : text.hashCode() * textHashSalt;
        // mixing to account for small text and sid deltas
        tokenHashCode ^= (tokenHashCode << 19) ^ (tokenHashCode >> 13);
        result = prime * result + tokenHashCode;
        // mixing at each step to make the hash code order-dependent
        result ^= (result << 25) ^ (result >> 7);
    }
    return result;
}
Also used : SymbolToken(com.amazon.ion.SymbolToken) com.amazon.ion.impl._Private_Utils.newSymbolToken(com.amazon.ion.impl._Private_Utils.newSymbolToken)

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