Search in sources :

Example 56 with XPathException

use of org.exist.xquery.XPathException in project exist by eXist-db.

the class FunResolveQName method eval.

public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().start(this);
        context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
        if (contextSequence != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
        }
    }
    final Sequence qnameSeq = args[0];
    if (qnameSeq.isEmpty()) {
        return EmptySequence.EMPTY_SEQUENCE;
    } else {
        context.pushInScopeNamespaces();
        final String qnameString = args[0].getStringValue();
        if (QName.isQName(qnameString) == VALID.val) {
            try {
                String prefix = QName.extractPrefix(qnameString);
                if (prefix == null) {
                    prefix = "";
                }
                String uri = null;
                final NodeValue node = (NodeValue) args[1].itemAt(0);
                if (node.getImplementationType() == NodeValue.PERSISTENT_NODE) {
                    NodeProxy proxy = (NodeProxy) node;
                    final NodeSet ancestors = proxy.getAncestors(contextId, true);
                    for (NodeProxy ancestor : ancestors) {
                        proxy = ancestor;
                        final ElementImpl e = (ElementImpl) proxy.getNode();
                        uri = findNamespaceURI(e, prefix);
                        if (uri != null) {
                            break;
                        }
                    }
                } else {
                    NodeImpl next = (NodeImpl) node;
                    do {
                        uri = findNamespaceURI((org.exist.dom.memtree.ElementImpl) next, prefix);
                        if (uri != null) {
                            break;
                        } else {
                            next = (NodeImpl) next.getParentNode();
                        }
                    } while (next != null && next.getNodeType() == Node.ELEMENT_NODE);
                }
                if (uri == null && prefix != null && !prefix.isEmpty()) {
                    throw new XPathException(this, ErrorCodes.FONS0004, "No namespace found for prefix. No binding for prefix '" + prefix + "' was found.", args[0]);
                }
                final String localPart = QName.extractLocalName(qnameString);
                final QName qn = new QName(localPart, uri, prefix);
                final QNameValue result = new QNameValue(context, qn);
                if (context.getProfiler().isEnabled()) {
                    context.getProfiler().end(this, "", result);
                }
                context.popInScopeNamespaces();
                return result;
            } catch (final QName.IllegalQNameException e) {
                throw new XPathException(this, ErrorCodes.FOCA0002, "Invalid lexical value. '" + qnameString + "' is not a QName.", args[0]);
            }
        } else {
            throw new XPathException(this, ErrorCodes.FOCA0002, "Invalid lexical value. '" + qnameString + "' is not a QName.", args[0]);
        }
    }
}
Also used : NodeSet(org.exist.dom.persistent.NodeSet) NodeValue(org.exist.xquery.value.NodeValue) NodeImpl(org.exist.dom.memtree.NodeImpl) XPathException(org.exist.xquery.XPathException) QName(org.exist.dom.QName) QNameValue(org.exist.xquery.value.QNameValue) EmptySequence(org.exist.xquery.value.EmptySequence) Sequence(org.exist.xquery.value.Sequence) NodeProxy(org.exist.dom.persistent.NodeProxy) ElementImpl(org.exist.dom.persistent.ElementImpl)

Example 57 with XPathException

use of org.exist.xquery.XPathException in project exist by eXist-db.

the class FunQName method eval.

/* (non-Javadoc)
	 * @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[], org.exist.xquery.value.Sequence)
	 */
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().start(this);
        context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
        if (contextSequence != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
        }
    }
    // TODO : currently useless (but for empty sequences) since the type is forced :-(
    if (!args[0].isEmpty() && args[0].getItemType() != Type.STRING) {
        throw new XPathException(this, ErrorCodes.XPTY0004, "Namespace URI is of type '" + Type.getTypeName(args[0].getItemType()) + "', 'xs:string' expected", args[0]);
    }
    String namespace;
    if (args[0].isEmpty()) {
        namespace = "";
    } else {
        namespace = args[0].getStringValue();
    }
    final String param = args[1].getStringValue();
    String prefix = null;
    String localName = null;
    try {
        prefix = QName.extractPrefix(param);
        localName = QName.extractLocalName(param);
    } catch (final QName.IllegalQNameException e) {
        final ValueSequence argsSeq = new ValueSequence(args[0]);
        argsSeq.addAll(args[1]);
        throw new XPathException(this, ErrorCodes.FOCA0002, "Invalid lexical form of either prefix or local name.", argsSeq);
    }
    if ((prefix != null && !prefix.isEmpty()) && (namespace == null || namespace.isEmpty())) {
        final ValueSequence argsSeq = new ValueSequence(args[0]);
        argsSeq.addAll(args[1]);
        throw new XPathException(this, ErrorCodes.FOCA0002, "Non-empty namespace prefix with empty namespace URI", argsSeq);
    }
    if (namespace != null) {
        if (namespace.equalsIgnoreCase(Namespaces.XMLNS_NS)) {
            if (prefix == null)
                throw new XPathException(this, ErrorCodes.XQDY0044, "'" + Namespaces.XMLNS_NS + "' can't be use with no prefix");
            else if (!prefix.equalsIgnoreCase("xmlns"))
                throw new XPathException(this, ErrorCodes.XQDY0044, "'" + Namespaces.XMLNS_NS + "' can't be use with prefix '" + prefix + "'");
        }
        if (namespace.equalsIgnoreCase(Namespaces.XML_NS)) {
            if (prefix == null)
                throw new XPathException(this, ErrorCodes.XQDY0044, "'" + Namespaces.XML_NS + "' can't be use with no prefix");
            else if (!prefix.equalsIgnoreCase("xml"))
                throw new XPathException(this, ErrorCodes.XQDY0044, "'" + Namespaces.XML_NS + "' can't be use with prefix '" + prefix + "'");
        }
    }
    if (prefix != null) {
        if (prefix.equalsIgnoreCase("xml") && !namespace.equalsIgnoreCase(Namespaces.XML_NS)) {
            throw new XPathException(this, ErrorCodes.XQDY0044, "prefix 'xml' can be used only with '" + Namespaces.XML_NS + "'");
        }
    }
    final QName qname = new QName(localName, namespace, prefix);
    if (prefix != null && namespace != null) {
        if (context.getURIForPrefix(prefix) == null) {
            // TOCHECK : context.declareInScopeNamespace(prefix, uri) ?
            context.declareNamespace(prefix, namespace);
        }
    // context.declareInScopeNamespace(prefix, namespace);
    }
    if (!XMLNames.isName(qname.getLocalPart())) {
        throw new XPathException(this, ErrorCodes.FOCA0002, "'" + qname.getLocalPart() + "' is not a valid local name.");
    }
    final Sequence result = new QNameValue(context, qname);
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : XPathException(org.exist.xquery.XPathException) QName(org.exist.dom.QName) QNameValue(org.exist.xquery.value.QNameValue) ValueSequence(org.exist.xquery.value.ValueSequence) ValueSequence(org.exist.xquery.value.ValueSequence) Sequence(org.exist.xquery.value.Sequence)

Example 58 with XPathException

use of org.exist.xquery.XPathException in project exist by eXist-db.

the class FunReplace method eval.

@Override
public Sequence eval(final Sequence contextSequence, final Item contextItem) throws XPathException {
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().start(this);
        context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
        if (contextSequence != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
        }
        if (contextItem != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
        }
    }
    final Sequence result;
    final Sequence stringArg = getArgument(0).eval(contextSequence, contextItem);
    if (stringArg.isEmpty()) {
        result = StringValue.EMPTY_STRING;
    } else {
        final String flags;
        if (getSignature().getArgumentCount() == 4) {
            flags = getArgument(3).eval(contextSequence, contextItem).getStringValue();
        } else {
            flags = "";
        }
        final String string = stringArg.getStringValue();
        final Sequence patternSeq = getArgument(1).eval(contextSequence, contextItem);
        final String pattern = patternSeq.getStringValue();
        final Sequence replaceSeq = getArgument(2).eval(contextSequence, contextItem);
        final String replace = replaceSeq.getStringValue();
        final Configuration config = context.getBroker().getBrokerPool().getSaxonConfiguration();
        final List<String> warnings = new ArrayList<>(1);
        try {
            final RegularExpression regularExpression = config.compileRegularExpression(pattern, flags, "XP30", warnings);
            if (!hasLiteral(flags)) {
                final String msg = Replace.checkReplacement(replace);
                if (msg != null) {
                    throw new XPathException(this, ErrorCodes.FORX0004, msg);
                }
            }
            final CharSequence res = regularExpression.replace(string, replace);
            result = new StringValue(res.toString());
        } catch (final net.sf.saxon.trans.XPathException e) {
            switch(e.getErrorCodeLocalPart()) {
                case "FORX0001":
                    throw new XPathException(this, ErrorCodes.FORX0001, e.getMessage());
                case "FORX0002":
                    throw new XPathException(this, ErrorCodes.FORX0002, e.getMessage());
                case "FORX0003":
                    throw new XPathException(this, ErrorCodes.FORX0003, e.getMessage());
                case "FORX0004":
                    throw new XPathException(this, ErrorCodes.FORX0004, e.getMessage());
                default:
                    throw new XPathException(this, e.getMessage());
            }
        }
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : RegularExpression(net.sf.saxon.regex.RegularExpression) Configuration(net.sf.saxon.Configuration) XPathException(org.exist.xquery.XPathException) ArrayList(java.util.ArrayList) Sequence(org.exist.xquery.value.Sequence) StringValue(org.exist.xquery.value.StringValue)

Example 59 with XPathException

use of org.exist.xquery.XPathException in project exist by eXist-db.

the class FunRoot method eval.

/* (non-Javadoc)
         * @see org.exist.xquery.Expression#eval(org.exist.dom.persistent.DocumentSet, org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
         */
public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().start(this);
        context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
        if (contextSequence != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
        }
        if (contextItem != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
        }
    }
    Sequence seq;
    Sequence result;
    Item item;
    if (contextItem != null) {
        contextSequence = contextItem.toSequence();
    }
    if (contextSequence == null || contextSequence.isEmpty()) {
        result = Sequence.EMPTY_SEQUENCE;
    }
    // If we have one argumment, we take it into account
    if (getSignature().getArgumentCount() > 0) {
        seq = getArgument(0).eval(contextSequence, contextItem);
    } else // Otherwise, we take the context sequence and we iterate over it
    {
        seq = contextSequence;
    }
    if (seq == null) {
        throw new XPathException(this, ErrorCodes.XPDY0002, "Undefined context item");
    }
    if (seq.isPersistentSet()) {
        result = new ExtArrayNodeSet(seq.getItemCount());
    } else {
        result = new ValueSequence(seq.getItemCount());
    }
    for (final SequenceIterator i = seq.iterate(); i.hasNext(); ) {
        item = i.nextItem();
        if (!Type.subTypeOf(item.getType(), Type.NODE)) {
            throw new XPathException(this, ErrorCodes.XPTY0004, "Item is not a node; got '" + item + "'", seq);
        }
        final Sequence s = item.toSequence();
        if (s.isPersistentSet()) {
            final NodeProxy p = s.toNodeSet().get(0);
            result.add(new NodeProxy(p.getOwnerDocument()));
        } else {
            if (seq.hasOne() && item.getType() == Type.ATTRIBUTE) {
                result.add(item);
            } else if (item.getType() == Type.DOCUMENT) {
                result.add((DocumentImpl) item);
            } else {
                result.add(((NodeImpl) item).getOwnerDocument());
            }
        }
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : Item(org.exist.xquery.value.Item) ExtArrayNodeSet(org.exist.dom.persistent.ExtArrayNodeSet) SequenceIterator(org.exist.xquery.value.SequenceIterator) NodeImpl(org.exist.dom.memtree.NodeImpl) XPathException(org.exist.xquery.XPathException) ValueSequence(org.exist.xquery.value.ValueSequence) ValueSequence(org.exist.xquery.value.ValueSequence) Sequence(org.exist.xquery.value.Sequence) NodeProxy(org.exist.dom.persistent.NodeProxy) DocumentImpl(org.exist.dom.memtree.DocumentImpl)

Example 60 with XPathException

use of org.exist.xquery.XPathException in project exist by eXist-db.

the class XUpdateProcessor method processQuery.

private Sequence processQuery(String select) throws SAXException {
    XQueryContext context = null;
    try {
        context = new XQueryContext(broker.getBrokerPool());
        context.setStaticallyKnownDocuments(documentSet);
        Map.Entry<String, String> namespaceEntry;
        for (Map.Entry<String, String> stringStringEntry : namespaces.entrySet()) {
            namespaceEntry = stringStringEntry;
            context.declareNamespace(namespaceEntry.getKey(), namespaceEntry.getValue());
        }
        Map.Entry<String, Object> entry;
        for (Map.Entry<String, Object> stringObjectEntry : variables.entrySet()) {
            entry = stringObjectEntry;
            context.declareVariable(entry.getKey(), entry.getValue());
        }
        // TODO(pkaminsk2): why replicate XQuery.compile here?
        final XQueryLexer lexer = new XQueryLexer(context, new StringReader(select));
        final XQueryParser parser = new XQueryParser(lexer);
        final XQueryTreeParser treeParser = new XQueryTreeParser(context);
        parser.xpath();
        if (parser.foundErrors()) {
            throw new SAXException(parser.getErrorMessage());
        }
        final AST ast = parser.getAST();
        if (LOG.isDebugEnabled()) {
            LOG.debug("generated AST: {}", ast.toStringTree());
        }
        final PathExpr expr = new PathExpr(context);
        treeParser.xpath(ast, expr);
        if (treeParser.foundErrors()) {
            throw new SAXException(treeParser.getErrorMessage());
        }
        expr.analyze(new AnalyzeContextInfo());
        final Sequence seq = expr.eval(null, null);
        return seq;
    } catch (final RecognitionException | TokenStreamException e) {
        LOG.warn("error while creating variable", e);
        throw new SAXException(e);
    } catch (final XPathException e) {
        throw new SAXException(e);
    } finally {
        if (context != null) {
            context.reset(false);
        }
    }
}
Also used : AST(antlr.collections.AST) XPathException(org.exist.xquery.XPathException) XQueryContext(org.exist.xquery.XQueryContext) XQueryParser(org.exist.xquery.parser.XQueryParser) XQueryLexer(org.exist.xquery.parser.XQueryLexer) Sequence(org.exist.xquery.value.Sequence) AnalyzeContextInfo(org.exist.xquery.AnalyzeContextInfo) XQueryTreeParser(org.exist.xquery.parser.XQueryTreeParser) SAXException(org.xml.sax.SAXException) TokenStreamException(antlr.TokenStreamException) StringReader(java.io.StringReader) PathExpr(org.exist.xquery.PathExpr) RecognitionException(antlr.RecognitionException)

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