Search in sources :

Example 6 with IEmbeddedXMLStreamReader

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);
}
Also used : DBBroker(org.exist.storage.DBBroker) XMLStreamException(javax.xml.stream.XMLStreamException) NodeId(org.exist.numbering.NodeId) EXistException(org.exist.EXistException) IOException(java.io.IOException) IEmbeddedXMLStreamReader(org.exist.stax.IEmbeddedXMLStreamReader)

Example 7 with IEmbeddedXMLStreamReader

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;
}
Also used : DBBroker(org.exist.storage.DBBroker) XMLStreamException(javax.xml.stream.XMLStreamException) NodeId(org.exist.numbering.NodeId) IOException(java.io.IOException) EXistException(org.exist.EXistException) IEmbeddedXMLStreamReader(org.exist.stax.IEmbeddedXMLStreamReader)

Example 8 with IEmbeddedXMLStreamReader

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);
}
Also used : DBBroker(org.exist.storage.DBBroker) XMLStreamException(javax.xml.stream.XMLStreamException) NodeId(org.exist.numbering.NodeId) EXistException(org.exist.EXistException) IOException(java.io.IOException) IEmbeddedXMLStreamReader(org.exist.stax.IEmbeddedXMLStreamReader)

Example 9 with IEmbeddedXMLStreamReader

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);
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) IEmbeddedXMLStreamReader(org.exist.stax.IEmbeddedXMLStreamReader)

Aggregations

IEmbeddedXMLStreamReader (org.exist.stax.IEmbeddedXMLStreamReader)9 IOException (java.io.IOException)8 XMLStreamException (javax.xml.stream.XMLStreamException)8 NodeId (org.exist.numbering.NodeId)6 EXistException (org.exist.EXistException)5 DBBroker (org.exist.storage.DBBroker)4 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 Analyzer (org.apache.lucene.analysis.Analyzer)1 TokenStream (org.apache.lucene.analysis.TokenStream)1 CharTermAttribute (org.apache.lucene.analysis.tokenattributes.CharTermAttribute)1 OffsetAttribute (org.apache.lucene.analysis.tokenattributes.OffsetAttribute)1 Term (org.apache.lucene.index.Term)1 PhraseQuery (org.apache.lucene.search.PhraseQuery)1 Query (org.apache.lucene.search.Query)1 State (org.apache.lucene.util.AttributeSource.State)1 DocumentImpl (org.exist.dom.persistent.DocumentImpl)1 StoredNode (org.exist.dom.persistent.StoredNode)1 EmbeddedXMLStreamReader (org.exist.stax.EmbeddedXMLStreamReader)1 ExtendedXMLStreamReader (org.exist.stax.ExtendedXMLStreamReader)1