use of org.exist.stax.IEmbeddedXMLStreamReader in project exist by eXist-db.
the class StoredNode method getNextSibling.
@Override
public Node getNextSibling() {
if (nodeId.getTreeLevel() == 2 && getOwnerDocument().getCollection().isTempCollection()) {
return null;
}
// handle siblings of level 1, e.g. a comment before/after a document element
if (nodeId.getTreeLevel() == 1) {
final NodeId siblingId = nodeId.nextSibling();
try (final DBBroker broker = ownerDocument.getBrokerPool().getBroker()) {
return broker.objectWith(ownerDocument, siblingId);
} catch (final EXistException e) {
LOG.error("Internal error while reading next sibling node: {}", e.getMessage(), e);
// TODO : throw exception -pb
}
}
// handle siblings of level 1+n
final StoredNode parent = getParentStoredNode();
if (parent != null && parent.isDirty()) {
try (final DBBroker broker = ownerDocument.getBrokerPool().getBroker()) {
final int parentLevel = parent.getNodeId().getTreeLevel();
final int level = nodeId.getTreeLevel();
final IEmbeddedXMLStreamReader reader = broker.getXMLStreamReader(parent, true);
while (reader.hasNext()) {
final int status = reader.next();
final NodeId currentId = (NodeId) reader.getProperty(ExtendedXMLStreamReader.PROPERTY_NODE_ID);
if (status != XMLStreamConstants.END_ELEMENT && currentId.getTreeLevel() == level && currentId.compareTo(nodeId) > 0) {
return reader.getNode();
} else if (status == XMLStreamConstants.END_ELEMENT && currentId.getTreeLevel() == parentLevel) {
// exit while loop
break;
}
}
} catch (final IOException | XMLStreamException | EXistException e) {
LOG.error("Internal error while reading child nodes: {}", e.getMessage(), e);
// TODO : throw exception -pb
}
return null;
}
final NodeId siblingId = nodeId.nextSibling();
return ownerDocument.getNode(siblingId);
}
use of org.exist.stax.IEmbeddedXMLStreamReader in project exist by eXist-db.
the class StoredNode method getLastNode.
protected IStoredNode getLastNode(final IStoredNode node) {
// only applicable to elements with children or attributes
if (!(node.hasChildNodes() || node.hasAttributes())) {
return node;
}
try (final DBBroker broker = ownerDocument.getBrokerPool().getBroker()) {
final int thisLevel = node.getNodeId().getTreeLevel();
final int childLevel = thisLevel + 1;
final IEmbeddedXMLStreamReader reader = broker.getXMLStreamReader(node, true);
while (reader.hasNext()) {
final int status = reader.next();
final NodeId otherId = (NodeId) reader.getProperty(ExtendedXMLStreamReader.PROPERTY_NODE_ID);
final int otherLevel = otherId.getTreeLevel();
// skip descendants
if (otherLevel > childLevel) {
continue;
}
if (status == XMLStreamConstants.END_ELEMENT && otherLevel == thisLevel) {
// exit-while
break;
}
}
return reader.getPreviousNode();
} catch (final IOException | XMLStreamException | EXistException e) {
LOG.error("Internal error while reading child nodes: {}", e.getMessage(), e);
// TODO : throw exception -pb
}
return null;
}
use of org.exist.stax.IEmbeddedXMLStreamReader in project exist by eXist-db.
the class StoredNode method getPreviousSibling.
@Override
public Node getPreviousSibling() {
// if we are the root node, there is no sibling
if (nodeId.equals(NodeId.ROOT_NODE)) {
return null;
}
// handle siblings of level 1, e.g. a comment before/after a document element
if (nodeId.getTreeLevel() == 1) {
final NodeId siblingId = nodeId.precedingSibling();
try (final DBBroker broker = ownerDocument.getBrokerPool().getBroker()) {
return broker.objectWith(ownerDocument, siblingId);
} catch (final EXistException e) {
LOG.error("Internal error while reading previous sibling node: {}", e.getMessage(), e);
// TODO : throw exception -pb
}
}
// handle siblings of level 1+n
final StoredNode parent = getParentStoredNode();
if (parent != null && parent.isDirty()) {
try (final DBBroker broker = ownerDocument.getBrokerPool().getBroker()) {
final int parentLevel = parent.getNodeId().getTreeLevel();
final int level = nodeId.getTreeLevel();
final IEmbeddedXMLStreamReader reader = broker.getXMLStreamReader(parent, true);
IStoredNode last = null;
while (reader.hasNext()) {
final int status = reader.next();
final NodeId currentId = (NodeId) reader.getProperty(ExtendedXMLStreamReader.PROPERTY_NODE_ID);
if (status != XMLStreamConstants.END_ELEMENT) {
if (currentId.getTreeLevel() == level) {
if (currentId.equals(nodeId)) {
return last;
}
last = reader.getNode();
}
} else if (status == XMLStreamConstants.END_ELEMENT && currentId.getTreeLevel() == parentLevel) {
// exit while loop
break;
}
}
} catch (final IOException | XMLStreamException | EXistException e) {
LOG.error("Internal error while reading child nodes: {}", e.getMessage(), e);
// TODO : throw exception -pb
}
return null;
}
final NodeId firstChild = parent.getNodeId().newChild();
if (nodeId.equals(firstChild)) {
return null;
}
final NodeId siblingId = nodeId.precedingSibling();
return ownerDocument.getNode(siblingId);
}
use of org.exist.stax.IEmbeddedXMLStreamReader in project exist by eXist-db.
the class NodeProxy method directMatchAttribute.
@Override
public boolean directMatchAttribute(final DBBroker broker, final NodeTest test, final int contextId) {
if (nodeType != UNKNOWN_NODE_TYPE && nodeType != Node.ELEMENT_NODE) {
return false;
}
try {
final IEmbeddedXMLStreamReader reader = broker.getXMLStreamReader(this, true);
int status = reader.next();
if (status != XMLStreamReader.START_ELEMENT) {
return false;
}
final int attrs = reader.getAttributeCount();
for (int i = 0; i < attrs; i++) {
status = reader.next();
if (status != XMLStreamReader.ATTRIBUTE) {
break;
}
final AttrImpl attr = (AttrImpl) reader.getNode();
if (test.matches(attr)) {
return true;
}
}
return false;
} catch (final IOException | XMLStreamException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
Aggregations