use of nu.xom.Node in project teiid by teiid.
the class BinaryXMLCodec method writeDocument.
private void writeDocument(Document doc) throws IOException {
if (DEBUG)
System.err.println("writing document");
writeXMLDeclaration(doc.getBaseURI());
for (int i = 0; i < doc.getChildCount(); i++) {
Node node = doc.getChild(i);
if (node instanceof Element) {
writeElement((Element) node);
} else if (node instanceof Comment) {
writeComment((Comment) node);
} else if (node instanceof ProcessingInstruction) {
writeProcessingInstruction((ProcessingInstruction) node);
} else if (node instanceof DocType) {
writeDocType((DocType) node);
} else {
throw new IllegalAddException("Cannot write node type: " + node);
}
}
writeEndDocument();
if (DEBUG)
System.err.println("finished writing document");
}
use of nu.xom.Node in project teiid by teiid.
the class BinaryXMLCodec method readDocument.
/**
* Parses document from encoded src buffer; tokens appear in document order.
*/
private Document readDocument(ArrayByteList src, InputStream input) throws BinaryParsingException, IOException {
if (DEBUG)
System.err.println("reading document");
readPage(src, input);
Document doc = factory.startMakingDocument();
// doc.setBaseURI(symbols[src.getInt()]);
doc.setBaseURI(getInternedName(src.getInt()));
boolean hasRootElement = false;
int i = 0;
// add children of document, retaining the exact same order found in input
while (src.remaining() > 0) {
Nodes nodes;
// look ahead
int type = src.get();
if (DEBUG)
System.err.println("reading type = " + toString(type));
switch(// three low bits indicate node type
type & 0x07) {
case TEXT:
{
throw new BinaryParsingException("Unreachable text");
}
case ATTRIBUTE:
{
throw new BinaryParsingException("Unreachable attribute");
}
case BEGIN_ELEMENT:
{
if (factory.getClass() == NodeFactory.class) {
// fast path
Element root = readStartTag(src, type);
// reads entire subtree
readElement(src, root, input);
nodes = new Nodes(root);
} else {
// slow path
Element root = readStartTagF(src, type, true);
if (root == null) {
throw new NullPointerException("Factory failed to create root element.");
}
doc.setRootElement(root);
readElementF(src, root, input);
nodes = factory.finishMakingElement(root);
}
break;
}
case END_ELEMENT:
{
throw new BinaryParsingException("Unreachable end of element");
}
case COMMENT:
{
nodes = readCommentF(src, type);
break;
}
case NAMESPACE_DECLARATION:
{
throw new BinaryParsingException("Unreachable namespace declaration");
}
case PROCESSING_INSTRUCTION:
{
nodes = readProcessingInstructionF(src);
break;
}
case DOC_TYPE:
{
nodes = readDocTypeF(src);
break;
}
default:
{
throw new BinaryParsingException("Illegal node type code=" + type);
}
}
// append nodes:
for (int j = 0; j < nodes.size(); j++) {
Node node = nodes.get(j);
if (node instanceof Element) {
// replace fake root with real root
if (hasRootElement) {
throw new IllegalAddException("Factory returned multiple root elements");
}
doc.setRootElement((Element) node);
hasRootElement = true;
} else {
doc.insertChild(node, i);
}
i++;
}
}
if (!hasRootElement)
throw new WellformednessException("Factory attempted to remove the root element");
factory.finishMakingDocument(doc);
if (DEBUG)
System.err.println("finished reading document");
return doc;
}
use of nu.xom.Node in project teiid by teiid.
the class XQueryEvaluator method wrap.
/**
* Converts a xom node into something readable by Saxon
* @param node
* @param config
* @return
*/
static NodeInfo wrap(Node node, Configuration config) {
if (node == null)
// $NON-NLS-1$
throw new IllegalArgumentException("node must not be null");
if (node instanceof DocType)
// $NON-NLS-1$
throw new IllegalArgumentException("DocType can't be queried by XQuery/XPath");
Node root = node;
while (root.getParent() != null) {
root = root.getParent();
}
XOMDocumentWrapper docWrapper = new XOMDocumentWrapper(root, config);
return docWrapper.wrap(node);
}
use of nu.xom.Node in project ma-core-public by infiniteautomation.
the class XOMConverter method convertOutbound.
/* (non-Javadoc)
* @see org.directwebremoting.Converter#convertOutbound(java.lang.Object, org.directwebremoting.OutboundContext)
*/
public OutboundVariable convertOutbound(Object data, OutboundContext outctx) throws MarshallException {
try {
// Using XSLT to convert to a stream. Setup the source
if (!(data instanceof Node)) {
throw new MarshallException(data.getClass());
}
Node node = (Node) data;
String script = EnginePrivate.xmlStringToJavascriptDom(node.toXML());
OutboundVariable ov = new SimpleOutboundVariable(script, outctx, false);
outctx.put(data, ov);
return ov;
} catch (MarshallException ex) {
throw ex;
} catch (Exception ex) {
throw new MarshallException(data.getClass(), ex);
}
}
use of nu.xom.Node in project teiid by teiid.
the class BinaryXMLCodec method readElement.
/**
* Iterative pull parser reading an entire element subtree.
*/
private void readElement(ArrayByteList src, Element current, InputStream input) throws BinaryParsingException, IOException {
while (true) {
Node node = null;
Element down = null;
// look ahead
int type = src.get();
// if (DEBUG) System.err.println("reading type = " + toString(type));
switch(// three low bits indicate node type
type & 0x07) {
case TEXT:
{
node = readText(src, type);
break;
}
case ATTRIBUTE:
{
readAttribute(src, current, type);
continue;
}
case BEGIN_ELEMENT:
{
down = readStartTag(src, type);
node = down;
break;
}
case END_ELEMENT:
{
current = (Element) current.getParent();
// we're done with the root element
if (current == null)
return;
continue;
}
case COMMENT:
{
node = readComment(src, type);
break;
}
case NAMESPACE_DECLARATION:
{
readNamespaceDeclaration(src, current, type);
continue;
}
case PROCESSING_INSTRUCTION:
{
node = readProcessingInstruction(src);
break;
}
case DOC_TYPE:
{
// surrogate hack to identify BEGIN_PAGE
readPage(src, input);
continue;
}
}
// if (DEBUG) System.err.println("read node=" + node.toXML());
current.insertChild(node, current.getChildCount());
// recurse down
if (down != null)
current = down;
}
}
Aggregations