use of com.amazon.ion.SymbolToken in project ion-java by amzn.
the class SharedSymbolTable method newSharedSymbolTable.
/**
* Constructs a new shared symbol table represented by the current value
* of the passed in {@link IonReader}.
*
* @param reader
* the {@link IonReader} positioned on the shared symbol table
* represented as an {@link IonStruct}
* @param isOnStruct
* denotes whether the {@link IonReader} is already positioned on
* the struct; false if it is positioned before the struct
* @return
*/
static SymbolTable newSharedSymbolTable(IonReader reader, boolean isOnStruct) {
if (!isOnStruct) {
IonType t = reader.next();
if (t != IonType.STRUCT) {
throw new IonException("invalid symbol table image passed " + "into reader, " + t + " encountered when a " + "struct was expected");
}
}
String name = null;
int version = -1;
List<String> symbolsList = new ArrayList<String>();
reader.stepIn();
IonType fieldType = null;
while ((fieldType = reader.next()) != null) {
if (reader.isNullValue())
continue;
SymbolToken symTok = reader.getFieldNameSymbol();
int sid = symTok.getSid();
if (sid == SymbolTable.UNKNOWN_SYMBOL_ID) {
// This is a user-defined IonReader or a pure DOM, fall
// back to text
final String fieldName = reader.getFieldName();
sid = getSidForSymbolTableField(fieldName);
}
// different SID!
switch(sid) {
case VERSION_SID:
if (fieldType == IonType.INT) {
version = reader.intValue();
}
break;
case NAME_SID:
if (fieldType == IonType.STRING) {
name = reader.stringValue();
}
break;
case SYMBOLS_SID:
// empty lists
if (fieldType == IonType.LIST) {
reader.stepIn();
{
IonType t;
while ((t = reader.next()) != null) {
String text = null;
if (t == IonType.STRING && !reader.isNullValue()) {
// As per the Spec, if any element of
// the list is the empty string or any
// other type, treat it as null
text = reader.stringValue();
if (text.length() == 0)
text = null;
}
symbolsList.add(text);
}
}
reader.stepOut();
}
break;
default:
break;
}
}
reader.stepOut();
if (name == null || name.length() == 0) {
String message = "shared symbol table is malformed: field 'name' " + "must be a non-empty string.";
throw new IonException(message);
}
// As per the Spec, if 'version' field is missing or not at
// least 1, treat it as 1.
version = (version < 1) ? 1 : version;
Map<String, Integer> symbolsMap = null;
if (!symbolsList.isEmpty()) {
symbolsMap = new HashMap<String, Integer>();
transferNonExistingSymbols(symbolsList, symbolsMap);
} else {
// Empty Map is more efficient than an empty HashMap
symbolsMap = Collections.emptyMap();
}
// We have all necessary data, pass it over to the private constructor.
return new SharedSymbolTable(name, version, symbolsList, symbolsMap);
}
use of com.amazon.ion.SymbolToken in project ion-java by amzn.
the class IonWriterSystem method setTypeAnnotationSymbols.
public final void setTypeAnnotationSymbols(SymbolToken... annotations) {
if (annotations == null || annotations.length == 0) {
_annotation_count = 0;
} else {
int count = annotations.length;
// TODO the following makes two copy passes
// TODO validate the input
ensureAnnotationCapacity(count);
SymbolTable symtab = getSymbolTable();
for (int i = 0; i < count; i++) {
SymbolToken sym = annotations[i];
if (sym.getText() == null) {
validateSymbolId(sym.getSid());
}
sym = _Private_Utils.localize(symtab, sym);
_annotations[i] = sym;
}
_annotation_count = count;
}
}
use of com.amazon.ion.SymbolToken in project ion-java by amzn.
the class ReaderCompare method compareFieldNames.
public static void compareFieldNames(IonReader r1, IonReader r2) {
SymbolToken tok1 = r1.getFieldNameSymbol();
SymbolToken tok2 = r1.getFieldNameSymbol();
String fn = tok1.getText();
assertEquals(fn, tok2.getText());
if (fn != null) {
String f1 = r1.getFieldName();
String f2 = r2.getFieldName();
compareNonNullStrings("field name", fn, f1);
compareNonNullStrings("field name", fn, f2);
}
}
use of com.amazon.ion.SymbolToken in project ion-java by amzn.
the class IonJavaCli method isEmbeddedEvent.
private static boolean isEmbeddedEvent(Event event) {
IonType ionType = event.getIonType();
SymbolToken[] annotations = event.getAnnotations();
return (ionType == IonType.SEXP || ionType == IonType.LIST) && event.getDepth() == 0 && annotations != null && annotations.length > 0 && annotations[0].getText().equals(EMBEDDED_STREAM_ANNOTATION);
}
use of com.amazon.ion.SymbolToken in project ion-java by amzn.
the class IonJavaCli method ionStreamToEvent.
private static Event ionStreamToEvent(IonReader ionReader) throws IllegalStateException {
if (ionReader.getType() == null)
throw new IllegalStateException("Can't convert ionReader null type to Event");
IonType ionType = ionReader.getType();
SymbolToken fieldName = ionReader.getFieldNameSymbol();
SymbolToken[] annotations = ionReader.getTypeAnnotationSymbols();
int depth = ionReader.getDepth();
ImportDescriptor[] imports = null;
EventType eventType;
IonValue value = null;
if (IonType.isContainer(ionType)) {
eventType = EventType.CONTAINER_START;
} else {
eventType = EventType.SCALAR;
value = ION_SYSTEM.newValue(ionReader);
value.clearTypeAnnotations();
if (isIonVersionMarker(value.toString())) {
value.setTypeAnnotationSymbols(_Private_Utils.newSymbolToken("$ion_user_value", 0));
}
}
return new Event(eventType, ionType, fieldName, annotations, value, imports, depth);
}
Aggregations