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;
}
Aggregations