Search in sources :

Example 66 with XPathException

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;
}
Also used : NodeValue(org.exist.xquery.value.NodeValue) XPathException(org.exist.xquery.XPathException) LockedDocument(org.exist.dom.persistent.LockedDocument) DocumentImpl(org.exist.dom.persistent.DocumentImpl) LuceneIndexWorker(org.exist.indexing.lucene.LuceneIndexWorker) XPathException(org.exist.xquery.XPathException)

Example 67 with XPathException

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());
    }
}
Also used : XPathException(org.exist.xquery.XPathException) LockedDocument(org.exist.dom.persistent.LockedDocument) PermissionDeniedException(org.exist.security.PermissionDeniedException) IOException(java.io.IOException) XmldbURI(org.exist.xmldb.XmldbURI) LuceneIndexWorker(org.exist.indexing.lucene.LuceneIndexWorker)

Example 68 with XPathException

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);
    }
}
Also used : TokenStream(org.apache.lucene.analysis.TokenStream) CharTermAttribute(org.apache.lucene.analysis.tokenattributes.CharTermAttribute) XPathException(org.exist.xquery.XPathException) StringReader(java.io.StringReader) IOException(java.io.IOException)

Example 69 with XPathException

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]);
}
Also used : XPathException(org.exist.xquery.XPathException) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList)

Example 70 with XPathException

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);
}
Also used : TokenStream(org.apache.lucene.analysis.TokenStream) XPathException(org.exist.xquery.XPathException) ArrayList(java.util.ArrayList) Term(org.apache.lucene.index.Term) IOException(java.io.IOException) CharTermAttribute(org.apache.lucene.analysis.tokenattributes.CharTermAttribute) StringReader(java.io.StringReader)

Aggregations

XPathException (org.exist.xquery.XPathException)306 Sequence (org.exist.xquery.value.Sequence)86 IOException (java.io.IOException)61 SAXException (org.xml.sax.SAXException)43 StringValue (org.exist.xquery.value.StringValue)40 PermissionDeniedException (org.exist.security.PermissionDeniedException)34 NodeValue (org.exist.xquery.value.NodeValue)34 DBBroker (org.exist.storage.DBBroker)32 IntegerValue (org.exist.xquery.value.IntegerValue)32 ValueSequence (org.exist.xquery.value.ValueSequence)27 Item (org.exist.xquery.value.Item)26 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)24 EXistException (org.exist.EXistException)23 Path (java.nio.file.Path)22 XmldbURI (org.exist.xmldb.XmldbURI)22 BrokerPool (org.exist.storage.BrokerPool)21 Txn (org.exist.storage.txn.Txn)21 XQueryContext (org.exist.xquery.XQueryContext)21 Element (org.w3c.dom.Element)21 XQuery (org.exist.xquery.XQuery)20