Search in sources :

Example 76 with XPathException

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

the class ExtTestErrorFunction method eval.

@Override
public Sequence eval(final Sequence contextSequence, final Item contextItem) throws XPathException {
    final Sequence arg1 = getCurrentArguments()[0];
    final String name = arg1.itemAt(0).getStringValue();
    final Sequence arg2 = getCurrentArguments().length == 2 ? getCurrentArguments()[1] : null;
    final MapType error = arg2 != null ? (MapType) arg2.itemAt(0) : null;
    final Description description = Description.createTestDescription(suiteName, name, new Annotation[0]);
    // notify JUnit
    try {
        final XPathException errorReason = errorMapAsXPathException(error);
        notifier.fireTestFailure(new Failure(description, errorReason));
    } catch (final XPathException e) {
        // signal internal failure
        notifier.fireTestFailure(new Failure(description, e));
    }
    return Sequence.EMPTY_SEQUENCE;
}
Also used : Description(org.junit.runner.Description) XPathException(org.exist.xquery.XPathException) MapType(org.exist.xquery.functions.map.MapType) Failure(org.junit.runner.notification.Failure)

Example 77 with XPathException

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

the class ExtTestErrorFunction method errorMapAsXPathException.

private XPathException errorMapAsXPathException(final MapType errorMap) throws XPathException {
    final Sequence seqDescription = errorMap.get(new StringValue("description"));
    final String description;
    if (seqDescription != null && !seqDescription.isEmpty()) {
        description = seqDescription.itemAt(0).getStringValue();
    } else {
        description = "";
    }
    final Sequence seqErrorCode = errorMap.get(new StringValue("code"));
    final ErrorCodes.ErrorCode errorCode;
    if (seqErrorCode != null && !seqErrorCode.isEmpty()) {
        errorCode = new ErrorCodes.ErrorCode(((QNameValue) seqErrorCode.itemAt(0)).getQName(), description);
    } else {
        errorCode = ErrorCodes.ERROR;
    }
    final Sequence seqLineNumber = errorMap.get(new StringValue("line-number"));
    final int lineNumber;
    if (seqLineNumber != null && !seqLineNumber.isEmpty()) {
        lineNumber = seqLineNumber.itemAt(0).toJavaObject(int.class);
    } else {
        lineNumber = -1;
    }
    final Sequence seqColumnNumber = errorMap.get(new StringValue("column-number"));
    final int columnNumber;
    if (seqColumnNumber != null && !seqColumnNumber.isEmpty()) {
        columnNumber = seqColumnNumber.itemAt(0).toJavaObject(int.class);
    } else {
        columnNumber = -1;
    }
    final XPathException xpe = new XPathException(lineNumber, columnNumber, errorCode, description);
    final Sequence seqJavaStackTrace = errorMap.get(new StringValue("java-stack-trace"));
    if (seqJavaStackTrace != null && !seqJavaStackTrace.isEmpty()) {
        try {
            xpe.setStackTrace(convertStackTraceElements(seqJavaStackTrace));
        } catch (final NullPointerException e) {
            e.printStackTrace();
        }
    }
    return xpe;
}
Also used : ErrorCodes(org.exist.xquery.ErrorCodes) XPathException(org.exist.xquery.XPathException)

Example 78 with XPathException

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

the class XMLTestRunner method run.

@Override
public void run(final RunNotifier notifier) {
    try {
        final String pkgName = getClass().getPackage().getName().replace('.', '/');
        final Source query = new ClassLoaderSource(pkgName + "/xml-test-runner.xq");
        final List<java.util.function.Function<XQueryContext, Tuple2<String, Object>>> externalVariableDeclarations = Arrays.asList(context -> new Tuple2<>("doc", doc), context -> new Tuple2<>("id", Sequence.EMPTY_SEQUENCE), // set callback functions for notifying junit!
        context -> new Tuple2<>("test-ignored-function", new FunctionReference(new FunctionCall(context, new ExtTestIgnoredFunction(context, getSuiteName(), notifier)))), context -> new Tuple2<>("test-started-function", new FunctionReference(new FunctionCall(context, new ExtTestStartedFunction(context, getSuiteName(), notifier)))), context -> new Tuple2<>("test-failure-function", new FunctionReference(new FunctionCall(context, new ExtTestFailureFunction(context, getSuiteName(), notifier)))), context -> new Tuple2<>("test-assumption-failed-function", new FunctionReference(new FunctionCall(context, new ExtTestAssumptionFailedFunction(context, getSuiteName(), notifier)))), context -> new Tuple2<>("test-error-function", new FunctionReference(new FunctionCall(context, new ExtTestErrorFunction(context, getSuiteName(), notifier)))), context -> new Tuple2<>("test-finished-function", new FunctionReference(new FunctionCall(context, new ExtTestFinishedFunction(context, getSuiteName(), notifier)))));
        // NOTE: at this stage EXIST_EMBEDDED_SERVER_CLASS_INSTANCE in XSuite will be usable
        final BrokerPool brokerPool = XSuite.EXIST_EMBEDDED_SERVER_CLASS_INSTANCE.getBrokerPool();
        executeQuery(brokerPool, query, externalVariableDeclarations);
    } catch (final DatabaseConfigurationException | IOException | EXistException | PermissionDeniedException | XPathException e) {
        // TODO(AR) what to do here?
        throw new RuntimeException(e);
    }
}
Also used : ClassLoaderSource(org.exist.source.ClassLoaderSource) XPathException(org.exist.xquery.XPathException) IOException(java.io.IOException) EXistException(org.exist.EXistException) ClassLoaderSource(org.exist.source.ClassLoaderSource) Source(org.exist.source.Source) InputSource(org.xml.sax.InputSource) DatabaseConfigurationException(org.exist.util.DatabaseConfigurationException) FunctionReference(org.exist.xquery.value.FunctionReference) PermissionDeniedException(org.exist.security.PermissionDeniedException) FunctionCall(org.exist.xquery.FunctionCall) BrokerPool(org.exist.storage.BrokerPool)

Example 79 with XPathException

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

the class ExtTestAssumptionFailedFunction method eval.

@Override
public Sequence eval(final Sequence contextSequence, final Item contextItem) throws XPathException {
    final Sequence arg1 = getCurrentArguments()[0];
    final String name = arg1.itemAt(0).getStringValue();
    final Sequence arg2 = getCurrentArguments().length == 2 ? getCurrentArguments()[1] : null;
    final MapType assumption = arg2 != null ? (MapType) arg2.itemAt(0) : null;
    final Description description = Description.createTestDescription(suiteName, name, new Annotation[0]);
    // notify JUnit
    try {
        final AssumptionViolatedException assumptionFailureReason = assumptionMapAsAssumptionViolationException(assumption);
        // NOTE: We remove the StackTrace, because it is not useful to have a Java Stack Trace pointing into the XML XQuery Test Suite code
        assumptionFailureReason.setStackTrace(new StackTraceElement[0]);
        notifier.fireTestAssumptionFailed(new Failure(description, assumptionFailureReason));
    } catch (final XPathException e) {
        // signal internal failure
        notifier.fireTestFailure(new Failure(description, e));
    }
    return Sequence.EMPTY_SEQUENCE;
}
Also used : Description(org.junit.runner.Description) AssumptionViolatedException(org.junit.AssumptionViolatedException) XPathException(org.exist.xquery.XPathException) Sequence(org.exist.xquery.value.Sequence) MapType(org.exist.xquery.functions.map.MapType) Failure(org.junit.runner.notification.Failure)

Example 80 with XPathException

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

the class Collations method getCollationFromURI.

/**
 * Get a {@link Comparator}from the specified URI.
 *
 * The original code is from saxon (@linkplain http://saxon.sf.net).
 *
 * @param uri The URI describing the collation and settings
 *
 * @return The Collator for the URI, or null.
 *
 * @throws XPathException If an error occurs whilst constructing the Collator
 */
@Nullable
public static Collator getCollationFromURI(final String uri) throws XPathException {
    final Collator collator;
    if (uri.startsWith(EXIST_COLLATION_URI) || uri.startsWith(UCA_COLLATION_URI) || uri.startsWith("?")) {
        URI u;
        try {
            u = new URI(uri);
        } catch (final URISyntaxException e) {
            return null;
        }
        final String query = u.getQuery();
        if (query == null) {
            collator = Collator.getInstance();
        } else {
            // default is "yes"
            boolean fallback = true;
            String lang = null;
            String version = null;
            String strength = null;
            // default is punct
            String maxVariable = "punct";
            // default is non-ignorable
            String alternate = "non-ignorable";
            // default is "no"
            boolean backwards = false;
            // default is "no"
            boolean normalization = false;
            // default is "no"
            boolean caseLevel = false;
            String caseFirst = null;
            // default is "no"
            boolean numeric = false;
            String reorder = null;
            String decomposition = null;
            final StringTokenizer queryTokenizer = new StringTokenizer(query, ";&");
            while (queryTokenizer.hasMoreElements()) {
                final String param = queryTokenizer.nextToken();
                final int eq = param.indexOf('=');
                if (eq > 0) {
                    final String kw = param.substring(0, eq);
                    if (kw != null) {
                        final String val = param.substring(eq + 1);
                        switch(kw) {
                            case "fallback":
                                fallback = "yes".equals(val);
                                break;
                            case "lang":
                                lang = val;
                                break;
                            case "version":
                                version = val;
                                break;
                            case "strength":
                                strength = val;
                                break;
                            case "maxVariable":
                                maxVariable = val;
                                break;
                            case "alternate":
                                alternate = val;
                                break;
                            case "backwards":
                                backwards = "yes".equals(val);
                                break;
                            case "normalization":
                                normalization = "yes".equals(val);
                                break;
                            case "caseLevel":
                                caseLevel = "yes".equals(val);
                                break;
                            case "caseFirst":
                                caseFirst = val;
                                break;
                            case "numeric":
                                numeric = "yes".equals(val);
                                break;
                            case "reorder":
                                reorder = val;
                                break;
                            case "decomposition":
                                decomposition = val;
                                break;
                            default:
                                logger.warn("Unrecognized Collation parameter: {}", kw);
                                break;
                        }
                    }
                }
            }
            collator = getCollationFromParams(fallback, lang, version, strength, maxVariable, alternate, backwards, normalization, caseLevel, caseFirst, numeric, reorder, decomposition);
        }
    } else if (HTML_ASCII_CASE_INSENSITIVE_COLLATION_URI.equals(uri)) {
        try {
            collator = getHtmlAsciiCaseInsensitiveCollator();
        } catch (final Exception e) {
            throw new XPathException("Unable to instantiate HTML ASCII Case Insensitive Collator: " + e.getMessage(), e);
        }
    } else if (XQTS_ASCII_CASE_BLIND_COLLATION_URI.equals(uri)) {
        try {
            collator = getXqtsAsciiCaseBlindCollator();
        } catch (final Exception e) {
            throw new XPathException("Unable to instantiate XQTS ASCII Case Blind Collator: " + e.getMessage(), e);
        }
    } else if (uri.startsWith("java:")) {
        // java class specified: this should be a subclass of
        // com.ibm.icu.text.RuleBasedCollator
        final String uriClassName = uri.substring("java:".length());
        try {
            final Class<?> collatorClass = Class.forName(uriClassName);
            if (!Collator.class.isAssignableFrom(collatorClass)) {
                final String msg = "The specified collator class '" + collatorClass.getName() + "' is not a subclass of com.ibm.icu.text.Collator";
                logger.error(msg);
                throw new XPathException(ErrorCodes.FOCH0002, msg);
            }
            collator = (Collator) collatorClass.newInstance();
        } catch (final Exception e) {
            final String msg = "The specified collator class " + uriClassName + " could not be found";
            logger.error(msg);
            throw new XPathException(ErrorCodes.FOCH0002, msg, e);
        }
    } else if (UNICODE_CODEPOINT_COLLATION_URI.equals(uri)) {
        collator = null;
    } else {
        final String msg = "Unknown collation : '" + uri + "'";
        logger.error(msg);
        throw new XPathException(ErrorCodes.FOCH0002, msg);
    }
    if (collator != null) {
        // make immutable and therefore thread-safe!
        collator.freeze();
    }
    return collator;
}
Also used : StringTokenizer(java.util.StringTokenizer) XPathException(org.exist.xquery.XPathException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) XPathException(org.exist.xquery.XPathException) Nullable(javax.annotation.Nullable)

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