Search in sources :

Example 56 with Source

use of org.exist.source.Source in project exist by eXist-db.

the class ContentFunctionsTest method getMetadataFromPdf.

@Test
public void getMetadataFromPdf() throws EXistException, XPathException, PermissionDeniedException, IOException {
    final String mainQuery = "declare namespace html = \"http://www.w3.org/1999/xhtml\";\n" + "declare namespace contentextraction = \"http://exist-db.org/xquery/contentextraction\";\n" + "declare namespace util = \"http://exist-db.org/xquery/util\";\n" + "let $bin := util:binary-doc(\"/db/content-functions-test/minimal.pdf\")\n" + "  return\n" + "    contentextraction:get-metadata($bin)//html:meta[@name = (\"xmpTPg:NPages\", \"Content-Type\")]/@content";
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final Source mainQuerySource = new StringSource(mainQuery);
    try (final DBBroker broker = pool.getBroker();
        final Txn transaction = pool.getTransactionManager().beginTransaction()) {
        final Tuple2<Integer, String> metadata = withCompiledQuery(broker, mainQuerySource, mainCompiledQuery -> {
            final Sequence result = executeQuery(broker, mainCompiledQuery);
            assertEquals(2, result.getItemCount());
            return Tuple(result.itemAt(0).toJavaObject(int.class), result.itemAt(1).getStringValue());
        });
        transaction.commit();
        assertEquals(1, metadata._1.intValue());
        assertEquals("application/pdf", metadata._2);
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) StringSource(org.exist.source.StringSource) Txn(org.exist.storage.txn.Txn) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool) StringSource(org.exist.source.StringSource) Source(org.exist.source.Source)

Example 57 with Source

use of org.exist.source.Source in project exist by eXist-db.

the class ContentFunctionsTest method getMetadataAndContentFromPdf.

@Test
public void getMetadataAndContentFromPdf() throws EXistException, XPathException, PermissionDeniedException, IOException {
    final String mainQuery = "declare namespace html = \"http://www.w3.org/1999/xhtml\";\n" + "declare namespace contentextraction = \"http://exist-db.org/xquery/contentextraction\";\n" + "declare namespace util = \"http://exist-db.org/xquery/util\";\n" + "let $bin := util:binary-doc(\"/db/content-functions-test/minimal.pdf\")\n" + "  return\n" + "    contentextraction:get-metadata-and-content($bin)//html:p[2]/string()";
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final Source mainQuerySource = new StringSource(mainQuery);
    try (final DBBroker broker = pool.getBroker();
        final Txn transaction = pool.getTransactionManager().beginTransaction()) {
        final String content = withCompiledQuery(broker, mainQuerySource, mainCompiledQuery -> {
            final Sequence result = executeQuery(broker, mainCompiledQuery);
            assertEquals(1, result.getItemCount());
            return result.itemAt(0).getStringValue();
        });
        transaction.commit();
        assertEquals("Hello World", content);
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) StringSource(org.exist.source.StringSource) Txn(org.exist.storage.txn.Txn) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool) StringSource(org.exist.source.StringSource) Source(org.exist.source.Source)

Example 58 with Source

use of org.exist.source.Source in project exist by eXist-db.

the class DebuggeeImpl method start.

@Override
public String start(String uri) throws Exception {
    Database db = null;
    ScriptRunner runner = null;
    try {
        db = BrokerPool.getInstance();
        try (final DBBroker broker = db.getBroker()) {
            // Try to find the XQuery
            Source source = SourceFactory.getSource(broker, "", uri, true);
            if (source == null)
                return null;
            XQuery xquery = broker.getBrokerPool().getXQueryService();
            XQueryContext queryContext = new XQueryContext(broker.getBrokerPool());
            // Find correct script load path
            queryContext.setModuleLoadPath(XmldbURI.create(uri).removeLastSegment().toString());
            CompiledXQuery compiled;
            try {
                compiled = xquery.compile(broker, queryContext, source);
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
            String sessionId = String.valueOf(queryContext.hashCode());
            // link debugger session & script
            DebuggeeJointImpl joint = new DebuggeeJointImpl();
            SessionImpl session = new SessionImpl();
            joint.setCompiledScript(compiled);
            queryContext.setDebuggeeJoint(joint);
            joint.continuation(new Init(session, sessionId, "eXist"));
            runner = new ScriptRunner(session, compiled);
            runner.start();
            int count = 0;
            while (joint.firstExpression == null && runner.exception == null && count < 10) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                }
                count++;
            }
            if (runner.exception != null) {
                throw runner.exception;
            }
            if (joint.firstExpression == null) {
                throw new XPathException("Can't run debug session.");
            }
            // queryContext.declareVariable(Debuggee.SESSION, sessionId);
            // XXX: make sure that it started up
            sessions.put(sessionId, session);
            return sessionId;
        }
    } catch (Exception e) {
        if (runner != null)
            runner.stop();
        throw e;
    }
}
Also used : XPathException(org.exist.xquery.XPathException) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext) IOException(java.io.IOException) Source(org.exist.source.Source) IOException(java.io.IOException) XPathException(org.exist.xquery.XPathException) DBBroker(org.exist.storage.DBBroker) Init(org.exist.debuggee.dbgp.packets.Init) Database(org.exist.Database)

Example 59 with Source

use of org.exist.source.Source in project exist by eXist-db.

the class XIncludeFilter method processXInclude.

/**
 * @param href     The resource to be xincluded
 * @param xpointer The xpointer
 * @return Optionally a ResourceError if it was not possible to retrieve the resource
 * to be xincluded
 * @throws SAXException              If a SAX processing error occurs
 */
protected Optional<ResourceError> processXInclude(final String href, String xpointer) throws SAXException {
    if (href == null) {
        throw new SAXException("No href attribute found in XInclude include element");
    }
    // save some settings
    DocumentImpl prevDoc = document;
    boolean createContainerElements = serializer.createContainerElements;
    serializer.createContainerElements = false;
    // The following comments are the basis for possible external documents
    XmldbURI docUri = null;
    try {
        docUri = XmldbURI.xmldbUriFor(href);
    /*
               if(!stylesheetUri.toCollectionPathURI().equals(stylesheetUri)) {
                   externalUri = stylesheetUri.getXmldbURI();
               }
               */
    } catch (final URISyntaxException e) {
    // could be an external URI!
    }
    // parse the href attribute
    LOG.debug("found href=\"{}\"", href);
    // String xpointer = null;
    // String docName = href;
    Map<String, String> params = null;
    DocumentImpl doc = null;
    org.exist.dom.memtree.DocumentImpl memtreeDoc = null;
    boolean xqueryDoc = false;
    if (docUri != null) {
        final String fragment = docUri.getFragment();
        if (!(fragment == null || fragment.length() == 0)) {
            throw new SAXException("Fragment identifiers must not be used in an xinclude href attribute. To specify an xpointer, use the xpointer attribute.");
        }
        // extract possible parameters in the URI
        params = null;
        final String paramStr = docUri.getQuery();
        if (paramStr != null) {
            params = processParameters(paramStr);
            // strip query part
            docUri = XmldbURI.create(docUri.getRawCollectionPath());
        }
        // Patch 1520454 start
        if (!docUri.isAbsolute() && document != null) {
            final String base = document.getCollection().getURI() + "/";
            final String child = "./" + docUri.toString();
            final URI baseUri = URI.create(base);
            final URI childUri = URI.create(child);
            final URI uri = baseUri.resolve(childUri);
            docUri = XmldbURI.create(uri);
        }
        // retrieve the document
        try {
            doc = serializer.broker.getResource(docUri, Permission.READ);
        } catch (final PermissionDeniedException e) {
            return Optional.of(new ResourceError("Permission denied to read XInclude'd resource", e));
        }
        /* Check if the document is a stored XQuery */
        if (doc != null && doc.getResourceType() == DocumentImpl.BINARY_FILE) {
            xqueryDoc = "application/xquery".equals(doc.getMimeType());
        }
    }
    // The document could not be found: check if it points to an external resource
    if (docUri == null || (doc == null && !docUri.isAbsolute())) {
        try {
            URI externalUri = new URI(href);
            final String scheme = externalUri.getScheme();
            // XQuery context.
            if (scheme == null && moduleLoadPath != null) {
                final String path = externalUri.getSchemeSpecificPart();
                Path f = Paths.get(path);
                if (!f.isAbsolute()) {
                    if (moduleLoadPath.startsWith(XmldbURI.XMLDB_URI_PREFIX)) {
                        final XmldbURI parentUri = XmldbURI.create(moduleLoadPath);
                        docUri = parentUri.append(path);
                        doc = (DocumentImpl) serializer.broker.getXMLResource(docUri);
                        if (doc != null && !doc.getPermissions().validate(serializer.broker.getCurrentSubject(), Permission.READ)) {
                            throw new PermissionDeniedException("Permission denied to read XInclude'd resource");
                        }
                    } else {
                        f = Paths.get(moduleLoadPath, path);
                        externalUri = f.toUri();
                    }
                }
            }
            if (doc == null) {
                final Either<ResourceError, org.exist.dom.memtree.DocumentImpl> external = parseExternal(externalUri);
                if (external.isLeft()) {
                    return Optional.of(external.left().get());
                } else {
                    memtreeDoc = external.right().get();
                }
            }
        } catch (final PermissionDeniedException e) {
            return Optional.of(new ResourceError("Permission denied on XInclude'd resource", e));
        } catch (final ParserConfigurationException | URISyntaxException e) {
            throw new SAXException("XInclude: failed to parse document at URI: " + href + ": " + e.getMessage(), e);
        }
    }
    /* if document has not been found and xpointer is
               * null, throw an exception. If xpointer != null
               * we retry below and interpret docName as
               * a collection.
               */
    if (doc == null && memtreeDoc == null && xpointer == null) {
        return Optional.of(new ResourceError("document " + docUri + " not found"));
    }
    if (xpointer == null && !xqueryDoc) {
        // no xpointer found - just serialize the doc
        if (memtreeDoc == null) {
            serializer.serializeToReceiver(doc, false);
        } else {
            serializer.serializeToReceiver(memtreeDoc, false);
        }
    } else {
        // process the xpointer or the stored XQuery
        Source source = null;
        final XQueryPool pool = serializer.broker.getBrokerPool().getXQueryPool();
        CompiledXQuery compiled = null;
        try {
            if (xpointer == null) {
                source = new DBSource(serializer.broker, (BinaryDocument) doc, true);
            } else {
                xpointer = checkNamespaces(xpointer);
                source = new StringSource(xpointer);
            }
            final XQuery xquery = serializer.broker.getBrokerPool().getXQueryService();
            XQueryContext context;
            compiled = pool.borrowCompiledXQuery(serializer.broker, source);
            if (compiled == null) {
                context = new XQueryContext(serializer.broker.getBrokerPool());
            } else {
                context = compiled.getContext();
                context.prepareForReuse();
            }
            context.declareNamespaces(namespaces);
            context.declareNamespace("xinclude", Namespaces.XINCLUDE_NS);
            // setup the http context if known
            if (serializer.httpContext != null) {
                context.setHttpContext(serializer.httpContext);
            }
            // TODO: change these to putting the XmldbURI in, but we need to warn users!
            if (document != null) {
                context.declareVariable("xinclude:current-doc", document.getFileURI().toString());
                context.declareVariable("xinclude:current-collection", document.getCollection().getURI().toString());
            }
            if (xpointer != null) {
                if (doc != null) {
                    context.setStaticallyKnownDocuments(new XmldbURI[] { doc.getURI() });
                } else if (docUri != null) {
                    context.setStaticallyKnownDocuments(new XmldbURI[] { docUri });
                }
            }
            // pass parameters as variables
            if (params != null) {
                for (final Map.Entry<String, String> entry : params.entrySet()) {
                    context.declareVariable(entry.getKey(), entry.getValue());
                }
            }
            if (compiled == null) {
                try {
                    compiled = xquery.compile(context, source, xpointer != null);
                } catch (final IOException e) {
                    throw new SAXException("I/O error while reading query for xinclude: " + e.getMessage(), e);
                }
            } else {
                compiled.getContext().updateContext(context);
                context.getWatchDog().reset();
            }
            LOG.info("xpointer query: {}", ExpressionDumper.dump((Expression) compiled));
            Sequence contextSeq = null;
            if (memtreeDoc != null) {
                contextSeq = memtreeDoc;
            }
            try {
                final Sequence seq = xquery.execute(serializer.broker, compiled, contextSeq);
                if (Type.subTypeOf(seq.getItemType(), Type.NODE)) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("xpointer found: {}", seq.getItemCount());
                    }
                    NodeValue node;
                    for (final SequenceIterator i = seq.iterate(); i.hasNext(); ) {
                        node = (NodeValue) i.nextItem();
                        serializer.serializeToReceiver(node, false);
                    }
                } else {
                    String val;
                    for (int i = 0; i < seq.getItemCount(); i++) {
                        val = seq.itemAt(i).getStringValue();
                        characters(val);
                    }
                }
            } finally {
                context.runCleanupTasks();
            }
        } catch (final XPathException | PermissionDeniedException e) {
            LOG.warn("xpointer error", e);
            throw new SAXException("Error while processing XInclude expression: " + e.getMessage(), e);
        } finally {
            if (compiled != null) {
                pool.returnCompiledXQuery(source, compiled);
            }
        }
    }
    // restore settings
    document = prevDoc;
    serializer.createContainerElements = createContainerElements;
    return Optional.empty();
}
Also used : NodeValue(org.exist.xquery.value.NodeValue) XPathException(org.exist.xquery.XPathException) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) URISyntaxException(java.net.URISyntaxException) DocumentImpl(org.exist.dom.persistent.DocumentImpl) URI(java.net.URI) XmldbURI(org.exist.xmldb.XmldbURI) StringSource(org.exist.source.StringSource) Source(org.exist.source.Source) DBSource(org.exist.source.DBSource) InputSource(org.xml.sax.InputSource) SAXException(org.xml.sax.SAXException) SequenceIterator(org.exist.xquery.value.SequenceIterator) DBSource(org.exist.source.DBSource) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XmldbURI(org.exist.xmldb.XmldbURI) Path(java.nio.file.Path) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext) IOException(java.io.IOException) Sequence(org.exist.xquery.value.Sequence) XQueryPool(org.exist.storage.XQueryPool) BinaryDocument(org.exist.dom.persistent.BinaryDocument) Expression(org.exist.xquery.Expression) PermissionDeniedException(org.exist.security.PermissionDeniedException) StringSource(org.exist.source.StringSource) Map(java.util.Map) HashMap(java.util.HashMap)

Example 60 with Source

use of org.exist.source.Source in project exist by eXist-db.

the class XQueryTestRunner method extractTestInfo.

private static XQueryTestInfo extractTestInfo(final Path path) throws InitializationError {
    try {
        final Configuration config = getConfiguration();
        final XQueryContext xqueryContext = new XQueryContext(config);
        final Source xquerySource = new FileSource(path, UTF_8, false);
        final XQuery xquery = new XQuery();
        final CompiledXQuery compiledXQuery = xquery.compile(xqueryContext, xquerySource);
        String moduleNsPrefix = null;
        String moduleNsUri = null;
        final List<XQueryTestInfo.TestFunctionDef> testFunctions = new ArrayList<>();
        final Iterator<UserDefinedFunction> localFunctions = compiledXQuery.getContext().localFunctions();
        while (localFunctions.hasNext()) {
            final UserDefinedFunction localFunction = localFunctions.next();
            final FunctionSignature localFunctionSignature = localFunction.getSignature();
            String testName = null;
            boolean isTest = false;
            final Annotation[] annotations = localFunctionSignature.getAnnotations();
            if (annotations != null) {
                for (final Annotation annotation : annotations) {
                    final QName annotationName = annotation.getName();
                    if (annotationName.getNamespaceURI().equals(XQSUITE_NAMESPACE)) {
                        if (annotationName.getLocalPart().startsWith("assert")) {
                            isTest = true;
                            if (testName != null) {
                                break;
                            }
                        } else if (annotationName.getLocalPart().equals("name")) {
                            final LiteralValue[] annotationValues = annotation.getValue();
                            if (annotationValues != null && annotationValues.length > 0) {
                                testName = annotationValues[0].getValue().getStringValue();
                                if (isTest) {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            if (isTest) {
                if (testName == null) {
                    testName = localFunctionSignature.getName().getLocalPart();
                }
                if (moduleNsPrefix == null) {
                    moduleNsPrefix = localFunctionSignature.getName().getPrefix();
                }
                if (moduleNsUri == null) {
                    moduleNsUri = localFunctionSignature.getName().getNamespaceURI();
                }
                testFunctions.add(new XQueryTestInfo.TestFunctionDef(testName));
            }
        }
        return new XQueryTestInfo(moduleNsPrefix, moduleNsUri, testFunctions);
    } catch (final DatabaseConfigurationException | IOException | PermissionDeniedException | XPathException e) {
        throw new InitializationError(e);
    }
}
Also used : Configuration(org.exist.util.Configuration) InitializationError(org.junit.runners.model.InitializationError) ClassLoaderSource(org.exist.source.ClassLoaderSource) Source(org.exist.source.Source) FileSource(org.exist.source.FileSource) DatabaseConfigurationException(org.exist.util.DatabaseConfigurationException) QName(org.exist.dom.QName) FileSource(org.exist.source.FileSource) IOException(java.io.IOException) PermissionDeniedException(org.exist.security.PermissionDeniedException)

Aggregations

Source (org.exist.source.Source)83 StringSource (org.exist.source.StringSource)62 Sequence (org.exist.xquery.value.Sequence)43 BrokerPool (org.exist.storage.BrokerPool)42 DBBroker (org.exist.storage.DBBroker)42 Txn (org.exist.storage.txn.Txn)40 Test (org.junit.Test)35 StringInputSource (org.exist.util.StringInputSource)32 DBSource (org.exist.source.DBSource)23 IOException (java.io.IOException)19 PermissionDeniedException (org.exist.security.PermissionDeniedException)19 InputSource (org.xml.sax.InputSource)15 EXistException (org.exist.EXistException)12 XmldbURI (org.exist.xmldb.XmldbURI)11 XQueryPool (org.exist.storage.XQueryPool)9 XQueryContext (org.exist.xquery.XQueryContext)9 FileSource (org.exist.source.FileSource)8 CompiledXQuery (org.exist.xquery.CompiledXQuery)8 XQuery (org.exist.xquery.XQuery)8 Collection (org.exist.collections.Collection)7