use of org.exist.xquery.XPathException in project exist by eXist-db.
the class Index method eval.
@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
try {
// Retrieve Lucene
LuceneIndexWorker index = (LuceneIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(LuceneIndex.ID);
if (isCalledAs("index")) {
// Get first parameter, this is the document
String path = args[0].itemAt(0).getStringValue();
// Retrieve document from database
try (final LockedDocument lockedDoc = context.getBroker().getXMLResource(XmldbURI.xmldbUriFor(path), LockMode.READ_LOCK)) {
// Verify the document actually exists
final DocumentImpl doc = lockedDoc == null ? null : lockedDoc.getDocument();
if (doc == null) {
throw new XPathException(this, "Document " + path + " does not exist.");
}
boolean flush = args.length == 2 || args[2].effectiveBooleanValue();
// Note: code order is important here,
index.setDocument(doc, ReindexMode.STORE);
index.setMode(ReindexMode.STORE);
// Get 'solr' node from second parameter
NodeValue descriptor = (NodeValue) args[1].itemAt(0);
// Pas document and index instructions to indexer
index.indexNonXML(descriptor);
if (flush) {
// Make sure things are written
index.writeNonXML();
}
}
} else {
// "close"
index.writeNonXML();
}
} catch (Exception ex) {
// PermissionDeniedException
logger.error(ex.getMessage(), ex);
throw new XPathException(this, ex);
}
// Return nothing [status would be nice]
return Sequence.EMPTY_SEQUENCE;
}
use of org.exist.xquery.XPathException in project exist by eXist-db.
the class GetField method eval.
@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
XmldbURI uri = XmldbURI.createInternal(args[0].getStringValue());
String field = args[1].getStringValue();
try (final LockedDocument lockedDoc = context.getBroker().getXMLResource(uri, LockMode.READ_LOCK)) {
if (lockedDoc == null) {
return Sequence.EMPTY_SEQUENCE;
}
// Get the lucene worker
final LuceneIndexWorker index = (LuceneIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(LuceneIndex.ID);
final String content = index.getFieldContent(lockedDoc.getDocument().getDocId(), field);
return content == null ? Sequence.EMPTY_SEQUENCE : new org.exist.xquery.value.StringValue(content);
} catch (PermissionDeniedException e) {
throw new XPathException(this, LuceneModule.EXXQDYFT0001, "Permission denied to read document " + args[0].getStringValue());
} catch (IOException e) {
throw new XPathException(this, LuceneModule.EXXQDYFT0002, "IO error while reading document " + args[0].getStringValue());
}
}
use of org.exist.xquery.XPathException in project exist by eXist-db.
the class XMLToQuery method getTerm.
private String getTerm(String field, String text, Analyzer analyzer) throws XPathException {
String term = null;
try {
TokenStream stream = analyzer.tokenStream(field, new StringReader(text));
CharTermAttribute termAttr = stream.addAttribute(CharTermAttribute.class);
stream.reset();
if (stream.incrementToken()) {
term = termAttr.toString();
}
stream.end();
stream.close();
return term;
} catch (IOException e) {
throw new XPathException("Lucene index error while creating query: " + e.getMessage(), e);
}
}
use of org.exist.xquery.XPathException in project exist by eXist-db.
the class XMLToQuery method parseSpanChildren.
private SpanQuery[] parseSpanChildren(String field, Element node, Analyzer analyzer) throws XPathException {
List<SpanQuery> list = new ArrayList<>(8);
Node child = node.getFirstChild();
while (child != null) {
if (child.getNodeType() == Node.ELEMENT_NODE) {
final String localName = child.getLocalName();
if (null != localName) {
switch(localName) {
case "term":
getSpanTerm(list, field, (Element) child, analyzer);
break;
case "near":
list.add(nearQuery(field, (Element) child, analyzer));
break;
case "first":
list.add(getSpanFirst(field, (Element) child, analyzer));
break;
case "regex":
list.add(getSpanRegex(field, (Element) child, analyzer));
break;
default:
throw new XPathException("Unknown query element: " + child.getNodeName());
}
}
}
child = child.getNextSibling();
}
return list.toArray(new SpanQuery[0]);
}
use of org.exist.xquery.XPathException in project exist by eXist-db.
the class XMLToQuery method nearQuery.
private SpanQuery nearQuery(String field, Element node, Analyzer analyzer) throws XPathException {
int slop = getSlop(node);
if (slop < 0)
slop = 0;
boolean inOrder = true;
if (node.hasAttribute("ordered"))
inOrder = node.getAttribute("ordered").equals("yes");
if (!hasElementContent(node)) {
String qstr = getText(node);
List<SpanTermQuery> list = new ArrayList<>(8);
try {
TokenStream stream = analyzer.tokenStream(field, new StringReader(qstr));
CharTermAttribute termAttr = stream.addAttribute(CharTermAttribute.class);
stream.reset();
while (stream.incrementToken()) {
list.add(new SpanTermQuery(new Term(field, termAttr.toString())));
}
stream.end();
stream.close();
} catch (IOException e) {
throw new XPathException("Error while parsing phrase query: " + qstr);
}
return new SpanNearQuery(list.toArray(new SpanTermQuery[0]), slop, inOrder);
}
SpanQuery[] children = parseSpanChildren(field, node, analyzer);
return new SpanNearQuery(children, slop, inOrder);
}
Aggregations