NamePool pool = config.getNamePool();
TinyBuilder builder = new TinyBuilder();
NamespaceReducer reducer = new NamespaceReducer();
reducer.setUnderlyingReceiver(builder);
Receiver tree = reducer;
tree.setConfiguration(config);
builder.setConfiguration(config);
tree.open();
int resultSequence = pool.allocate("result", RESULT_NS, "sequence");
int resultDocument = pool.allocate("result", RESULT_NS, "document");
int resultElement = pool.allocate("result", RESULT_NS, "element");
int resultAttribute = pool.allocate("result", RESULT_NS, "attribute");
int resultText = pool.allocate("result", RESULT_NS, "text");
int resultComment = pool.allocate("result", RESULT_NS, "comment");
int resultPI = pool.allocate("result", RESULT_NS, "processing-instruction");
int resultNamespace = pool.allocate("result", RESULT_NS, "namespace");
int resultAtomicValue = pool.allocate("result", RESULT_NS, "atomic-value");
int resultType = pool.allocate("", RESULT_NS, "type");
tree.startElement(resultSequence, -1, 0, 0);
tree.namespace(pool.allocateNamespaceCode("result", RESULT_NS), 0);
tree.startContent();
while (true) {
Item next = iterator.next();
if (next == null) {
break;
}
if (next instanceof NodeInfo) {
switch (((NodeInfo)next).getNodeKind()) {
case Type.DOCUMENT:
tree.startElement(resultDocument, -1, 0, 0);
tree.startContent();
((DocumentInfo)next).copy(tree, NodeInfo.ALL_NAMESPACES, true, 0);
tree.endElement();
break;
case Type.ELEMENT:
tree.startElement(resultElement, -1, 0, 0);
tree.startContent();
((NodeInfo)next).copy(tree, NodeInfo.ALL_NAMESPACES, true, 0);
tree.endElement();
break;
case Type.ATTRIBUTE:
tree.startElement(resultAttribute, -1, 0, 0);
((NodeInfo)next).copy(tree, NodeInfo.ALL_NAMESPACES, true, 0);
tree.startContent();
tree.endElement();
break;
case Type.TEXT:
tree.startElement(resultText, -1, 0, 0);
tree.startContent();
((NodeInfo)next).copy(tree, NodeInfo.ALL_NAMESPACES, true, 0);
tree.endElement();
break;
case Type.COMMENT:
tree.startElement(resultComment, -1, 0, 0);
tree.startContent();
((NodeInfo)next).copy(tree, NodeInfo.ALL_NAMESPACES, true, 0);
tree.endElement();
break;
case Type.PROCESSING_INSTRUCTION:
tree.startElement(resultPI, -1, 0, 0);
tree.startContent();
((NodeInfo)next).copy(tree, NodeInfo.ALL_NAMESPACES, true, 0);
tree.endElement();
break;
case Type.NAMESPACE:
tree.startElement(resultNamespace, -1, 0, 0);
((NodeInfo)next).copy(tree, NodeInfo.ALL_NAMESPACES, true, 0);
tree.startContent();
tree.endElement();
break;
}
} else {
tree.startElement(resultAtomicValue, -1, 0, 0);
tree.attribute(resultType, -1, Type.displayTypeName(next), 0, 0);
tree.startContent();
tree.characters(next.getStringValue(), 0, 0);
tree.endElement();
}
}
tree.endElement();
tree.close();
return builder.getCurrentDocument();
}