Search in sources :

Example 1 with NGramMatch

use of org.exist.indexing.ngram.NGramMatch in project exist by eXist-db.

the class AddMatch method eval.

@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    if (args[0].isEmpty()) {
        return args[0];
    }
    final NodeValue nv = (NodeValue) args[0].itemAt(0);
    if (!nv.isPersistentSet()) {
        return nv;
    }
    final NodeProxy node = (NodeProxy) nv;
    final int thisLevel = node.getNodeId().getTreeLevel();
    String matchStr = null;
    NodeId nodeId = null;
    try {
        for (final XMLStreamReader reader = context.getBroker().getXMLStreamReader(node, true); reader.hasNext(); ) {
            final int status = reader.next();
            if (status == XMLStreamConstants.CHARACTERS) {
                matchStr = reader.getText();
                nodeId = (NodeId) reader.getProperty(ExtendedXMLStreamReader.PROPERTY_NODE_ID);
                break;
            }
            final NodeId otherId = (NodeId) reader.getProperty(ExtendedXMLStreamReader.PROPERTY_NODE_ID);
            final int otherLevel = otherId.getTreeLevel();
            if (status == XMLStreamConstants.END_ELEMENT && otherLevel == thisLevel) {
                // exit-for
                break;
            }
        }
    } catch (IOException | XMLStreamException e) {
        throw new XPathException(this, ErrorCodes.FOER0000, "Exception caught while reading document");
    }
    if (nodeId != null) {
        Match match = new NGramMatch(getContextId(), node.getNodeId(), matchStr);
        match.addOffset(0, matchStr.length());
        node.addMatch(match);
    }
    return node;
}
Also used : NodeValue(org.exist.xquery.value.NodeValue) XMLStreamReader(javax.xml.stream.XMLStreamReader) ExtendedXMLStreamReader(org.exist.stax.ExtendedXMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) XPathException(org.exist.xquery.XPathException) NodeId(org.exist.numbering.NodeId) NGramMatch(org.exist.indexing.ngram.NGramMatch) IOException(java.io.IOException) NodeProxy(org.exist.dom.persistent.NodeProxy) Match(org.exist.dom.persistent.Match) NGramMatch(org.exist.indexing.ngram.NGramMatch)

Aggregations

IOException (java.io.IOException)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 Match (org.exist.dom.persistent.Match)1 NodeProxy (org.exist.dom.persistent.NodeProxy)1 NGramMatch (org.exist.indexing.ngram.NGramMatch)1 NodeId (org.exist.numbering.NodeId)1 ExtendedXMLStreamReader (org.exist.stax.ExtendedXMLStreamReader)1 XPathException (org.exist.xquery.XPathException)1 NodeValue (org.exist.xquery.value.NodeValue)1