public Sequence<Item> decode() throws IOException, XQueryException {
final XDMTreeBuilder handler = new XDMTreeBuilder();
final List<Item> items = new ArrayList<Item>(256);
boolean prevNodeStart = false;
final ObjectInput input = _input;
while(true) {
final byte ev = input.readByte();
if(ev == XQEventEncoder.EV_EOF) {
handler.endItem(true);
break;
}
switch(ev) {
case XQEventEncoder.EV_ATOM: {
final AtomicValue atom;
try {
atom = AtomicValue.readAtomicValue(input);
} catch (ClassNotFoundException e) {
throw new IllegalStateException(e);
}
handler.evAtomicValue(atom);
items.add(atom);
break;
}
case XQEventEncoder.EV_DTM_NODE: {
final DTMNodeBase dtmNode;
try {
dtmNode = DTMNodeBase.readFrom(input, _serContext);
} catch (ClassNotFoundException e) {
throw new IllegalStateException(e);
}
items.add(dtmNode);
break;
}
case XQEventEncoder.EV_NODE_START:
prevNodeStart = true;
continue;
case XQEventEncoder.EV_NODE_END: {
prevNodeStart = false;
handler.reset();
break;
}
case XQEventEncoder.EV_END_ITEM:
handler.endItem(false);
break;
case XQEventEncoder.EV_START_DOC:
handler.evStartDocument();
break;
case XQEventEncoder.EV_END_DOC:
handler.evEndDocument();
break;
case XQEventEncoder.EV_START_ELEM: {
String prefix = readString(input);
String localName = readString(input);
String namespaceURI = readString(input);
handler.evStartElement(prefix, localName, namespaceURI);
break;
}
case XQEventEncoder.EV_END_ELEM: {
String prefix = readString(input);
String localName = readString(input);
String namespaceURI = readString(input);
handler.evEndElement(prefix, localName, namespaceURI);
break;
}
case XQEventEncoder.EV_ATTR: {
QualifiedName qname = QualifiedName.readFrom(input);
String value = readString(input);
handler.evAttribute(qname, value);
break;
}
case XQEventEncoder.EV_NAMESPACE: {
String prefix = readString(input);
String uri = readString(input);
handler.evNamespace(prefix, uri);
break;
}
case XQEventEncoder.EV_TEXT: {
final int length = input.readInt();
final char[] ch = new char[length];
for(int i = 0; i < length; i++) {
ch[i] = input.readChar();
}
handler.evText(ch, 0, length);
break;
}
case XQEventEncoder.EV_CDATA: {
final int length = input.readInt();
final char[] ch = new char[length];
for(int i = 0; i < length; i++) {
ch[i] = input.readChar();
}
handler.evCData(ch, 0, length);
break;
}
case XQEventEncoder.EV_COMMENT: {
final int length = input.readInt();
final char[] ch = new char[length];
for(int i = 0; i < length; i++) {
ch[i] = input.readChar();
}
handler.evComment(ch, 0, length);
break;
}
case XQEventEncoder.EV_PI: {