Search in sources :

Example 51 with XPathException

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

the class FunTokenize 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 = Sequence.EMPTY_SEQUENCE;
    } else {
        String string = stringArg.getStringValue();
        if (string.isEmpty()) {
            result = Sequence.EMPTY_SEQUENCE;
        } else {
            final int flags;
            if (getSignature().getArgumentCount() == 3) {
                flags = parseFlags(this, getArgument(2).eval(contextSequence, contextItem).getStringValue());
            } else {
                flags = 0;
            }
            final String pattern;
            if (getArgumentCount() == 1) {
                pattern = " ";
                string = FunNormalizeSpace.normalize(string);
            } else {
                if (hasLiteral(flags)) {
                    // no need to change anything
                    pattern = getArgument(1).eval(contextSequence, contextItem).getStringValue();
                } else {
                    final boolean ignoreWhitespace = hasIgnoreWhitespace(flags);
                    final boolean caseBlind = hasCaseInsensitive(flags);
                    pattern = translateRegexp(this, getArgument(1).eval(contextSequence, contextItem).getStringValue(), ignoreWhitespace, caseBlind);
                }
            }
            try {
                if (pat == null || (!pattern.equals(pat.pattern())) || flags != pat.flags()) {
                    pat = PatternFactory.getInstance().getPattern(pattern, flags);
                }
                if (pat.matcher("").matches()) {
                    throw new XPathException(this, ErrorCodes.FORX0003, "regular expression could match empty string");
                }
                final String[] tokens = pat.split(string, -1);
                result = new ValueSequence();
                for (final String token : tokens) {
                    result.add(new StringValue(token));
                }
            } catch (final PatternSyntaxException e) {
                throw new XPathException(this, ErrorCodes.FORX0001, "Invalid regular expression: " + e.getMessage(), new StringValue(pattern), e);
            }
        }
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : XPathException(org.exist.xquery.XPathException) ValueSequence(org.exist.xquery.value.ValueSequence) ValueSequence(org.exist.xquery.value.ValueSequence) Sequence(org.exist.xquery.value.Sequence) StringValue(org.exist.xquery.value.StringValue) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 52 with XPathException

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

the class FunMin 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());
        }
    }
    boolean computableProcessing = false;
    Sequence result;
    final Sequence arg = getArgument(0).eval(contextSequence, contextItem);
    if (arg.isEmpty()) {
        result = Sequence.EMPTY_SEQUENCE;
    } else {
        // TODO : test if a range index is defined *iff* it is compatible with the collator
        final Collator collator = getCollator(contextSequence, contextItem, 2);
        final SequenceIterator iter = arg.unorderedIterator();
        AtomicValue min = null;
        while (iter.hasNext()) {
            final Item item = iter.nextItem();
            if (item instanceof QNameValue) {
                throw new XPathException(this, ErrorCodes.FORG0006, "Cannot compare " + Type.getTypeName(item.getType()), arg);
            }
            AtomicValue value = item.atomize();
            // Duration values must either all be xs:yearMonthDuration values or must all be xs:dayTimeDuration values.
            if (Type.subTypeOf(value.getType(), Type.DURATION)) {
                value = ((DurationValue) value).wrap();
                if (value.getType() == Type.YEAR_MONTH_DURATION) {
                    if (min != null && min.getType() != Type.YEAR_MONTH_DURATION) {
                        throw new XPathException(this, ErrorCodes.FORG0006, "Cannot compare " + Type.getTypeName(min.getType()) + " and " + Type.getTypeName(value.getType()), value);
                    }
                } else if (value.getType() == Type.DAY_TIME_DURATION) {
                    if (min != null && min.getType() != Type.DAY_TIME_DURATION) {
                        throw new XPathException(this, ErrorCodes.FORG0006, "Cannot compare " + Type.getTypeName(min.getType()) + " and " + Type.getTypeName(value.getType()), value);
                    }
                } else {
                    throw new XPathException(this, ErrorCodes.FORG0006, "Cannot compare " + Type.getTypeName(value.getType()), value);
                }
            // Any value of type xdt:untypedAtomic is cast to xs:double
            } else if (value.getType() == Type.UNTYPED_ATOMIC) {
                value = value.convertTo(Type.DOUBLE);
            }
            if (min == null) {
                min = value;
            } else {
                if (Type.getCommonSuperType(min.getType(), value.getType()) == Type.ATOMIC) {
                    throw new XPathException(this, ErrorCodes.FORG0006, "Cannot compare " + Type.getTypeName(min.getType()) + " and " + Type.getTypeName(value.getType()), value);
                }
                // Any value of type xdt:untypedAtomic is cast to xs:double
                if (value.getType() == Type.ATOMIC) {
                    value = value.convertTo(Type.DOUBLE);
                }
                // Numeric tests
                if (Type.subTypeOfUnion(value.getType(), Type.NUMBER)) {
                    // Don't mix comparisons
                    if (!Type.subTypeOfUnion(min.getType(), Type.NUMBER)) {
                        throw new XPathException(this, ErrorCodes.FORG0006, "Cannot compare " + Type.getTypeName(min.getType()) + " and " + Type.getTypeName(value.getType()), min);
                    }
                    if (((NumericValue) value).isNaN()) {
                        // Type NaN correctly
                        value = value.promote(min);
                        if (value.getType() == Type.FLOAT) {
                            min = FloatValue.NaN;
                        } else {
                            min = DoubleValue.NaN;
                        }
                        // although result will be NaN, we need to continue on order to type correctly
                        continue;
                    }
                    min = min.promote(value);
                }
                // Ugly test
                if (value instanceof ComputableValue) {
                    if (!(min instanceof ComputableValue)) {
                        throw new XPathException(this, ErrorCodes.FORG0006, "Cannot compare " + Type.getTypeName(min.getType()) + " and " + Type.getTypeName(value.getType()), min);
                    }
                    // Type value correctly
                    value = value.promote(min);
                    min = min.min(collator, value);
                    computableProcessing = true;
                } else {
                    if (computableProcessing) {
                        throw new XPathException(this, ErrorCodes.FORG0006, "Cannot compare " + Type.getTypeName(min.getType()) + " and " + Type.getTypeName(value.getType()), value);
                    }
                    if (Collations.compare(collator, value.getStringValue(), min.getStringValue()) < 0) {
                        min = value;
                    }
                }
            }
        }
        result = min;
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : Item(org.exist.xquery.value.Item) ComputableValue(org.exist.xquery.value.ComputableValue) SequenceIterator(org.exist.xquery.value.SequenceIterator) XPathException(org.exist.xquery.XPathException) QNameValue(org.exist.xquery.value.QNameValue) AtomicValue(org.exist.xquery.value.AtomicValue) Sequence(org.exist.xquery.value.Sequence) NumericValue(org.exist.xquery.value.NumericValue) Collator(com.ibm.icu.text.Collator)

Example 53 with XPathException

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

the class FunNamespaceURI method eval.

@Override
public Sequence eval(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());
        }
    }
    if (contextItem != null) {
        contextSequence = contextItem.toSequence();
    }
    // If we have one argument, we take it into account
    final Sequence seq;
    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");
    }
    final Sequence result;
    if (seq.isEmpty()) {
        result = AnyURIValue.EMPTY_URI;
    } else {
        final Item item = seq.itemAt(0);
        if (!Type.subTypeOf(item.getType(), Type.NODE)) {
            throw new XPathException(this, ErrorCodes.XPTY0004, "Context item is not a node; got: " + Type.getTypeName(item.getType()));
        }
        // TODO : how to improve performance ?
        final Node n = ((NodeValue) item).getNode();
        String ns = n.getNamespaceURI();
        if (ns == null) {
            ns = XMLConstants.NULL_NS_URI;
        }
        result = new AnyURIValue(ns);
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : Item(org.exist.xquery.value.Item) NodeValue(org.exist.xquery.value.NodeValue) XPathException(org.exist.xquery.XPathException) Node(org.w3c.dom.Node) AnyURIValue(org.exist.xquery.value.AnyURIValue) Sequence(org.exist.xquery.value.Sequence)

Example 54 with XPathException

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

the class FunOneOrMore 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());
        }
    }
    final Sequence result = getArgument(0).eval(contextSequence, contextItem);
    if (result.isEmpty()) {
        throw new XPathException(this, ErrorCodes.FORG0004, "fn:one-or-more called with a sequence containing zero items");
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : XPathException(org.exist.xquery.XPathException) Sequence(org.exist.xquery.value.Sequence)

Example 55 with XPathException

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

the class FunPosition method eval.

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 inSequence = context.getContextSequence();
    if (inSequence == null) {
        inSequence = contextSequence;
    }
    if (inSequence == null) {
        throw new XPathException(this, ErrorCodes.XPDY0002, "Undefined context item");
    }
    Sequence result;
    if (inSequence.isEmpty()) {
        result = Sequence.EMPTY_SEQUENCE;
    } else {
        result = new IntegerValue(context.getContextPosition() + 1);
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : XPathException(org.exist.xquery.XPathException) IntegerValue(org.exist.xquery.value.IntegerValue) Sequence(org.exist.xquery.value.Sequence)

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