use of com.amazon.ion.IonException in project ion-java by amzn.
the class IonWriterSystemBinary method writeAllBufferedData.
/**
* Empty our buffers, assuming it is safe to do so.
* This is called by {@link #flush()} and {@link #finish()}.
*/
private void writeAllBufferedData() throws IOException {
writeBytes(_user_output_stream);
clearFieldName();
clearAnnotations();
_in_struct = false;
_patch_count = 0;
_patch_symbol_table_count = 0;
_top = 0;
try {
_writer.setPosition(0);
_writer.truncate();
} catch (IOException e) {
throw new IonException(e);
}
}
use of com.amazon.ion.IonException in project ion-java by amzn.
the class IonWriterSystemBinary method getContainer.
protected final IonType getContainer() {
IonType type;
int tid = parentType();
switch(tid) {
case tidList:
type = IonType.LIST;
break;
case tidSexp:
type = IonType.SEXP;
break;
case tidStruct:
type = IonType.STRUCT;
break;
case tidDATAGRAM:
type = IonType.DATAGRAM;
break;
default:
throw new IonException("unexpected parent type " + tid + " does not represent a container");
}
return type;
}
use of com.amazon.ion.IonException in project ion-java by amzn.
the class IonWriterSystemTree method append.
private void append(IonValue value) {
try {
super.startValue();
} catch (IOException e) {
// Shouldn't happen
throw new IonException(e);
}
if (hasAnnotations()) {
SymbolToken[] annotations = getTypeAnnotationSymbols();
// TODO this makes an extra copy of the array
((_Private_IonValue) value).setTypeAnnotationSymbols(annotations);
this.clearAnnotations();
}
if (_in_struct) {
SymbolToken sym = assumeFieldNameSymbol();
IonStruct struct = (IonStruct) _current_parent;
struct.add(sym, value);
this.clearFieldName();
} else {
((IonSequence) _current_parent).add(value);
}
}
use of com.amazon.ion.IonException in project ion-java by amzn.
the class LocalSymbolTable method readLocalSymbolTable.
/**
* Parses the symbol table at the reader's current position.
* @param reader the reader from which to parse the symbol table.
* @param catalog the catalog from which to resolve shared symbol table imports.
* @param isOnStruct true if the reader is already positioned on the symbol table struct; otherwise, false.
* @param symbolsListOut list into which local symbols declared by the parsed symbol table will be deposited.
* @param currentSymbolTable the symbol table currently active in the stream.
* @return a new LocalSymbolTableImports instance, or null if this was an LST append. If null, `currentSymbolTable`
* continues to be the active symbol table.
*/
protected static LocalSymbolTableImports readLocalSymbolTable(IonReader reader, IonCatalog catalog, boolean isOnStruct, List<String> symbolsListOut, SymbolTable currentSymbolTable) {
if (!isOnStruct) {
reader.next();
}
assert reader.getType() == IonType.STRUCT : "invalid symbol table image passed in reader " + reader.getType() + " encountered when a struct was expected";
assert ION_SYMBOL_TABLE.equals(reader.getTypeAnnotations()[0]) : "local symbol tables must be annotated by " + ION_SYMBOL_TABLE;
reader.stepIn();
List<SymbolTable> importsList = new ArrayList<SymbolTable>();
importsList.add(reader.getSymbolTable().getSystemSymbolTable());
IonType fieldType;
boolean foundImportList = false;
boolean foundLocalSymbolList = false;
boolean isAppend = false;
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 SYMBOLS_SID:
{
// empty lists
if (foundLocalSymbolList) {
throw new IonException("Multiple symbol fields found within a single local symbol table.");
}
foundLocalSymbolList = true;
if (fieldType == IonType.LIST) {
reader.stepIn();
IonType type;
while ((type = reader.next()) != null) {
final String text;
if (type == IonType.STRING) {
text = reader.stringValue();
} else {
text = null;
}
symbolsListOut.add(text);
}
reader.stepOut();
}
break;
}
case IMPORTS_SID:
{
if (foundImportList) {
throw new IonException("Multiple imports fields found within a single local symbol table.");
}
foundImportList = true;
if (fieldType == IonType.LIST) {
prepImportsList(importsList, reader, catalog);
} else if (fieldType == IonType.SYMBOL && ION_SYMBOL_TABLE.equals(reader.stringValue())) {
isAppend = true;
}
break;
}
default:
{
// As per the Spec, any other field is ignored
break;
}
}
}
reader.stepOut();
if (isAppend && currentSymbolTable.isLocalTable()) {
// Because the current symbol table is a local symbol table (i.e. not the system symbol table), it can
// be appended in-place.
LocalSymbolTable currentLocalSymbolTable = (LocalSymbolTable) currentSymbolTable;
for (String newSymbol : symbolsListOut) {
currentLocalSymbolTable.putSymbol(newSymbol);
}
return null;
}
return new LocalSymbolTableImports(importsList);
}
use of com.amazon.ion.IonException 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);
}
Aggregations