Search in sources :

Example 21 with EXistResource

use of org.exist.xmldb.EXistResource in project exist by eXist-db.

the class XQueryTest method evalLoosesContext_1740891.

/**
 * @see http://sourceforge.net/support/tracker.php?aid=1740891
 */
@Test
public void evalLoosesContext_1740891() throws XMLDBException {
    String module = "module namespace tst = \"urn:test\"; " + "declare namespace util = \"http://exist-db.org/xquery/util\";" + "declare function tst:bar() as element(Bar)* { " + "let $foo := <Foo><Bar/><Bar/><Bar/></Foo> " + "let $query := \"$foo/Bar\" " + "let $bar := util:eval($query) " + "return $bar };";
    String module_name = "module.xqy";
    Resource doc;
    // Store module
    Collection testCollection = getTestCollection();
    doc = testCollection.createResource(module_name, "BinaryResource");
    doc.setContent(module);
    ((EXistResource) doc).setMimeType("application/xquery");
    testCollection.storeResource(doc);
    String query = "import module namespace tst = \"urn:test\"" + "at \"xmldb:exist:///db/test/module.xqy\"; " + "tst:bar()";
    XPathQueryService service = (XPathQueryService) getTestCollection().getService("XPathQueryService", "1.0");
    ResourceSet result = service.query(query);
    assertEquals(3, result.getSize());
    assertEquals("First", "<Bar/>", result.getResource(0).getContent().toString());
    assertEquals("Second", "<Bar/>", result.getResource(1).getContent().toString());
    assertEquals("Third", "<Bar/>", result.getResource(2).getContent().toString());
}
Also used : EXistResource(org.exist.xmldb.EXistResource) XPathQueryService(org.xmldb.api.modules.XPathQueryService) EXistXPathQueryService(org.exist.xmldb.EXistXPathQueryService) XMLResource(org.xmldb.api.modules.XMLResource) EXistResource(org.exist.xmldb.EXistResource) Resource(org.xmldb.api.base.Resource) Collection(org.xmldb.api.base.Collection) ResourceSet(org.xmldb.api.base.ResourceSet)

Example 22 with EXistResource

use of org.exist.xmldb.EXistResource in project exist by eXist-db.

the class AbstractExtractFunction method processCompressedEntry.

/**
 * Processes a compressed entry from an archive
 *
 * @param name The name of the entry
 * @param isDirectory true if the entry is a directory, false otherwise
 * @param is an InputStream for reading the uncompressed data of the entry
 * @param filterParam is an additional param for entry filtering function
 * @param storeParam is an additional param for entry storing function
 *
 * @return the result of processing the compressed entry.
 *
 * @throws XPathException if a query error occurs
 * @throws XMLDBException if a database error occurs
 * @throws IOException if an I/O error occurs
 */
protected Sequence processCompressedEntry(String name, boolean isDirectory, InputStream is, Sequence filterParam, Sequence storeParam) throws IOException, XPathException, XMLDBException {
    String dataType = isDirectory ? "folder" : "resource";
    // call the entry-filter function
    Sequence[] filterParams = new Sequence[3];
    filterParams[0] = new StringValue(name);
    filterParams[1] = new StringValue(dataType);
    filterParams[2] = filterParam;
    Sequence entryFilterFunctionResult = entryFilterFunction.evalFunction(contextSequence, null, filterParams);
    if (BooleanValue.FALSE == entryFilterFunctionResult.itemAt(0)) {
        return Sequence.EMPTY_SEQUENCE;
    } else {
        Sequence entryDataFunctionResult;
        Sequence uncompressedData = Sequence.EMPTY_SEQUENCE;
        if (entryDataFunction.getSignature().getReturnType().getPrimaryType() != Type.EMPTY && entryDataFunction.getSignature().getArgumentCount() == 3) {
            Sequence[] dataParams = new Sequence[3];
            System.arraycopy(filterParams, 0, dataParams, 0, 2);
            dataParams[2] = storeParam;
            entryDataFunctionResult = entryDataFunction.evalFunction(contextSequence, null, dataParams);
            String path = entryDataFunctionResult.itemAt(0).getStringValue();
            Collection root = new LocalCollection(context.getSubject(), context.getBroker().getBrokerPool(), new AnyURIValue("/db").toXmldbURI());
            if (isDirectory) {
                XMLDBAbstractCollectionManipulator.createCollection(root, path);
            } else {
                Resource resource;
                Path file = Paths.get(path).normalize();
                name = FileUtils.fileName(file);
                path = file.getParent().toAbsolutePath().toString();
                Collection target = (path == null) ? root : XMLDBAbstractCollectionManipulator.createCollection(root, path);
                MimeType mime = MimeTable.getInstance().getContentTypeFor(name);
                // copy the input data
                final byte[] entryData;
                try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
                    baos.write(is);
                    entryData = baos.toByteArray();
                }
                try (final InputStream bis = new UnsynchronizedByteArrayInputStream(entryData)) {
                    NodeValue content = ModuleUtils.streamToXML(context, bis);
                    resource = target.createResource(name, "XMLResource");
                    ContentHandler handler = ((XMLResource) resource).setContentAsSAX();
                    handler.startDocument();
                    content.toSAX(context.getBroker(), handler, null);
                    handler.endDocument();
                } catch (SAXException e) {
                    resource = target.createResource(name, "BinaryResource");
                    resource.setContent(entryData);
                }
                if (resource != null) {
                    if (mime != null) {
                        ((EXistResource) resource).setMimeType(mime.getName());
                    }
                    target.storeResource(resource);
                }
            }
        } else {
            // copy the input data
            final byte[] entryData;
            try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
                baos.write(is);
                entryData = baos.toByteArray();
            }
            // try and parse as xml, fall back to binary
            try (final InputStream bis = new UnsynchronizedByteArrayInputStream(entryData)) {
                uncompressedData = ModuleUtils.streamToXML(context, bis);
            } catch (SAXException saxe) {
                if (entryData.length > 0) {
                    try (final InputStream bis = new UnsynchronizedByteArrayInputStream(entryData)) {
                        uncompressedData = BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), bis);
                    }
                }
            }
            // call the entry-data function
            Sequence[] dataParams = new Sequence[4];
            System.arraycopy(filterParams, 0, dataParams, 0, 2);
            dataParams[2] = uncompressedData;
            dataParams[3] = storeParam;
            entryDataFunctionResult = entryDataFunction.evalFunction(contextSequence, null, dataParams);
        }
        return entryDataFunctionResult;
    }
}
Also used : Path(java.nio.file.Path) LocalCollection(org.exist.xmldb.LocalCollection) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) InputStream(java.io.InputStream) XMLResource(org.xmldb.api.modules.XMLResource) EXistResource(org.exist.xmldb.EXistResource) Resource(org.xmldb.api.base.Resource) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) MimeType(org.exist.util.MimeType) ContentHandler(org.xml.sax.ContentHandler) XMLResource(org.xmldb.api.modules.XMLResource) SAXException(org.xml.sax.SAXException) EXistResource(org.exist.xmldb.EXistResource) LocalCollection(org.exist.xmldb.LocalCollection) Collection(org.xmldb.api.base.Collection) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream)

Example 23 with EXistResource

use of org.exist.xmldb.EXistResource in project exist by eXist-db.

the class InteractiveClient method findRecursive.

private synchronized boolean findRecursive(final Collection collection, final Path dir, final XmldbURI base) throws XMLDBException {
    Collection c;
    Resource document;
    EXistCollectionManagementService mgtService;
    // The XmldbURIs here aren't really used...
    XmldbURI next;
    MimeType mimeType;
    try {
        final List<Path> files = FileUtils.list(dir);
        int i = 0;
        for (final Path file : files) {
            next = base.append(FileUtils.fileName(file));
            try {
                if (Files.isDirectory(file)) {
                    messageln("entering directory " + file.toAbsolutePath());
                    c = collection.getChildCollection(FileUtils.fileName(file));
                    if (c == null) {
                        mgtService = (EXistCollectionManagementService) collection.getService("CollectionManagementService", "1.0");
                        c = mgtService.createCollection(XmldbURI.xmldbUriFor(FileUtils.fileName(file)));
                    }
                    if (c instanceof Observable && options.verbose) {
                        final ProgressObserver observer = new ProgressObserver();
                        ((Observable) c).addObserver(observer);
                    }
                    findRecursive(c, file, next);
                } else {
                    final long start1 = System.currentTimeMillis();
                    mimeType = MimeTable.getInstance().getContentTypeFor(FileUtils.fileName(file));
                    if (mimeType == null) {
                        messageln("File " + FileUtils.fileName(file) + " has an unknown suffix. Cannot determine file type.");
                        mimeType = MimeType.BINARY_TYPE;
                    }
                    message("storing document " + FileUtils.fileName(file) + " (" + i + " of " + files.size() + ") " + "...");
                    document = collection.createResource(FileUtils.fileName(file), mimeType.getXMLDBType());
                    document.setContent(file);
                    ((EXistResource) document).setMimeType(mimeType.getName());
                    collection.storeResource(document);
                    ++filesCount;
                    messageln(" " + FileUtils.sizeQuietly(file) + " bytes in " + (System.currentTimeMillis() - start1) + "ms.");
                }
            } catch (final URISyntaxException e) {
                errorln("uri syntax exception parsing " + file.toAbsolutePath() + ": " + e.getMessage());
            }
            i++;
        }
        return true;
    } catch (final IOException e) {
        throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, e);
    }
}
Also used : Path(java.nio.file.Path) EXistCollectionManagementService(org.exist.xmldb.EXistCollectionManagementService) ExtendedResource(org.exist.xmldb.ExtendedResource) BinaryResource(org.xmldb.api.modules.BinaryResource) EXistResource(org.exist.xmldb.EXistResource) URISyntaxException(java.net.URISyntaxException) EXistResource(org.exist.xmldb.EXistResource) Collection(org.xmldb.api.base.Collection) XmldbURI(org.exist.xmldb.XmldbURI)

Example 24 with EXistResource

use of org.exist.xmldb.EXistResource in project exist by eXist-db.

the class InteractiveClient method processCommandLineActions.

/**
 * Process the command line options
 *
 * @return true if all are successful, otherwise false
 * @throws java.lang.Exception
 */
private boolean processCommandLineActions() throws Exception {
    final boolean foundCollection = options.setCol.isPresent();
    // process command-line actions
    if (options.reindex) {
        if (!foundCollection) {
            System.err.println("Please specify target collection with --collection");
            shutdown(false);
            return false;
        }
        try {
            reindex();
        } catch (final XMLDBException e) {
            System.err.println("XMLDBException while reindexing collection: " + getExceptionMessage(e));
            e.printStackTrace();
            return false;
        }
    }
    if (options.rmCol.isPresent()) {
        try {
            rmcol(options.rmCol.get());
        } catch (final XMLDBException e) {
            System.err.println("XMLDBException while removing collection: " + getExceptionMessage(e));
            e.printStackTrace();
            return false;
        }
    }
    if (options.mkCol.isPresent()) {
        try {
            mkcol(options.mkCol.get());
        } catch (final XMLDBException e) {
            System.err.println("XMLDBException during mkcol: " + getExceptionMessage(e));
            e.printStackTrace();
            return false;
        }
    }
    if (options.getDoc.isPresent()) {
        try {
            final Resource res = retrieve(options.getDoc.get());
            if (res != null) {
                // String data;
                if ("XMLResource".equals(res.getResourceType())) {
                    if (options.outputFile.isPresent()) {
                        writeOutputFile(options.outputFile.get(), res.getContent());
                    } else {
                        System.out.println(res.getContent().toString());
                    }
                } else {
                    if (options.outputFile.isPresent()) {
                        ((ExtendedResource) res).getContentIntoAFile(options.outputFile.get());
                        ((EXistResource) res).freeResources();
                    } else {
                        ((ExtendedResource) res).getContentIntoAStream(System.out);
                        System.out.println();
                    }
                }
            }
        } catch (final XMLDBException e) {
            System.err.println("XMLDBException while trying to retrieve document: " + getExceptionMessage(e));
            e.printStackTrace();
            return false;
        }
    } else if (options.rmDoc.isPresent()) {
        if (!foundCollection) {
            System.err.println("Please specify target collection with --collection");
        } else {
            try {
                remove(options.rmDoc.get());
            } catch (final XMLDBException e) {
                System.err.println("XMLDBException during parse: " + getExceptionMessage(e));
                e.printStackTrace();
                return false;
            }
        }
    } else if (!options.parseDocs.isEmpty()) {
        if (!foundCollection) {
            System.err.println("Please specify target collection with --collection");
        } else {
            for (final Path path : options.parseDocs) {
                try {
                    parse(path);
                } catch (final XMLDBException e) {
                    System.err.println("XMLDBException during parse: " + getExceptionMessage(e));
                    e.printStackTrace();
                    return false;
                }
            }
        }
    } else if (options.xpath.isPresent() || !options.queryFiles.isEmpty()) {
        String xpath = null;
        if (!options.queryFiles.isEmpty()) {
            try (final BufferedReader reader = Files.newBufferedReader(options.queryFiles.get(0))) {
                final StringBuilder buf = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                    buf.append(line);
                    buf.append(EOL);
                }
                xpath = buf.toString();
            }
        }
        // if no argument has been found, read query from stdin
        if (options.xpath.isPresent()) {
            final String xpathStr = options.xpath.get();
            if (!xpathStr.equals(CommandlineOptions.XPATH_STDIN)) {
                xpath = xpathStr;
            } else {
                // read from stdin
                try (final BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in))) {
                    final StringBuilder buf = new StringBuilder();
                    String line;
                    while ((line = stdin.readLine()) != null) {
                        buf.append(line);
                        buf.append(EOL);
                    }
                    xpath = buf.toString();
                } catch (final IOException e) {
                    System.err.println("failed to read query from stdin");
                    xpath = null;
                    return false;
                }
            }
        }
        if (xpath != null) {
            try {
                final ResourceSet result = find(xpath);
                final int maxResults = options.howManyResults.filter(n -> n > 0).orElse((int) result.getSize());
                if (options.outputFile.isPresent()) {
                    try (final OutputStream fos = new BufferedOutputStream(Files.newOutputStream(options.outputFile.get()));
                        final BufferedOutputStream bos = new BufferedOutputStream(fos);
                        final PrintStream ps = new PrintStream(bos)) {
                        for (int i = 0; i < maxResults && i < result.getSize(); i++) {
                            final Resource res = result.getResource(i);
                            if (res instanceof ExtendedResource) {
                                ((ExtendedResource) res).getContentIntoAStream(ps);
                            } else {
                                ps.print(res.getContent().toString());
                            }
                        }
                    }
                } else {
                    for (int i = 0; i < maxResults && i < result.getSize(); i++) {
                        final Resource res = result.getResource(i);
                        if (res instanceof ExtendedResource) {
                            ((ExtendedResource) res).getContentIntoAStream(System.out);
                        } else {
                            System.out.println(res.getContent());
                        }
                    }
                }
            } catch (final XMLDBException e) {
                System.err.println("XMLDBException during query: " + getExceptionMessage(e));
                e.printStackTrace();
                return false;
            }
        }
    } else if (options.xupdateFile.isPresent()) {
        try {
            xupdate(options.setDoc, options.xupdateFile.get());
        } catch (final XMLDBException e) {
            System.err.println("XMLDBException during xupdate: " + getExceptionMessage(e));
            return false;
        } catch (final IOException e) {
            System.err.println("IOException during xupdate: " + getExceptionMessage(e));
            return false;
        }
    }
    return true;
}
Also used : Path(java.nio.file.Path) ExtendedResource(org.exist.xmldb.ExtendedResource) ExtendedResource(org.exist.xmldb.ExtendedResource) BinaryResource(org.xmldb.api.modules.BinaryResource) EXistResource(org.exist.xmldb.EXistResource) EXistResource(org.exist.xmldb.EXistResource)

Example 25 with EXistResource

use of org.exist.xmldb.EXistResource in project exist by eXist-db.

the class InteractiveClient method parseGZip.

/**
 * stores given Resource
 *
 * @param fileName simple file or directory
 * @throws XMLDBException in case of database error storing the resource
 * @throws IOException in case of a read error
 * @return true if the operation succeeded
 */
protected synchronized boolean parseGZip(String fileName) throws XMLDBException, IOException {
    // TODO : why is this test for ? Fileshould make it, shouldn't it ? -pb
    fileName = fileName.replace('/', java.io.File.separatorChar).replace('\\', java.io.File.separatorChar);
    final Path file = Paths.get(fileName);
    Resource document;
    // String xml;
    if (current instanceof Observable && options.verbose) {
        final ProgressObserver observer = new ProgressObserver();
        ((Observable) current).addObserver(observer);
    }
    final List<Path> files;
    if (Files.isReadable(file)) {
        // TODO, same logic as for the graphic client
        if (Files.isDirectory(file)) {
            if (options.reindexRecurse) {
                filesCount = 0;
                final long start = System.currentTimeMillis();
                final boolean result = findGZipRecursive(current, file, path);
                messageln("storing " + filesCount + " compressed files took " + ((System.currentTimeMillis() - start) / 1000) + "sec.");
                return result;
            }
            files = FileUtils.list(file);
        } else {
            files = new ArrayList<>();
            files.add(file);
        }
    } else {
        final DirectoryScanner directoryScanner = new DirectoryScanner();
        directoryScanner.setIncludes(new String[] { fileName });
        // TODO(AR) do we need to call scanner.setBasedir()?
        directoryScanner.setCaseSensitive(true);
        directoryScanner.scan();
        final String[] includedFiles = directoryScanner.getIncludedFiles();
        files = new ArrayList<>(includedFiles.length);
        for (final String includedFile : includedFiles) {
            // files.add(baseDir.resolve(includedFile));
            files.add(Paths.get(includedFile));
        }
    }
    final long start0 = System.currentTimeMillis();
    long bytes = 0;
    MimeType mimeType;
    int i = 0;
    for (final Path p : files) {
        i++;
        if (Files.isDirectory(p)) {
            continue;
        }
        final long start = System.currentTimeMillis();
        final String compressedName = FileUtils.fileName(p);
        String localName = compressedName;
        final String[] cSuffix = { ".gz", ".Z" };
        boolean isCompressed = false;
        for (final String suf : cSuffix) {
            if (localName.endsWith(suf)) {
                // Removing compressed prefix to validate
                localName = compressedName.substring(0, localName.length() - suf.length());
                isCompressed = true;
                break;
            }
        }
        mimeType = MimeTable.getInstance().getContentTypeFor(localName);
        if (mimeType == null) {
            mimeType = MimeType.BINARY_TYPE;
        }
        document = current.createResource(compressedName, mimeType.getXMLDBType());
        message("storing document " + compressedName + " (" + i + " of " + Files.size(p) + ") ...");
        document.setContent(isCompressed ? new GZIPInputSource(p) : p);
        ((EXistResource) document).setMimeType(mimeType.getName());
        current.storeResource(document);
        messageln("done.");
        messageln("parsing " + Files.size(p) + (isCompressed ? " compressed" : "") + " bytes took " + (System.currentTimeMillis() - start) + "ms." + EOL);
        bytes += Files.size(p);
    }
    messageln("parsed " + bytes + " compressed bytes in " + (System.currentTimeMillis() - start0) + "ms.");
    return true;
}
Also used : Path(java.nio.file.Path) ExtendedResource(org.exist.xmldb.ExtendedResource) BinaryResource(org.xmldb.api.modules.BinaryResource) EXistResource(org.exist.xmldb.EXistResource) EXistResource(org.exist.xmldb.EXistResource) DirectoryScanner(org.apache.tools.ant.DirectoryScanner)

Aggregations

EXistResource (org.exist.xmldb.EXistResource)42 Collection (org.xmldb.api.base.Collection)20 BinaryResource (org.xmldb.api.modules.BinaryResource)20 Resource (org.xmldb.api.base.Resource)17 XMLResource (org.xmldb.api.modules.XMLResource)17 ResourceSet (org.xmldb.api.base.ResourceSet)12 Path (java.nio.file.Path)11 XMLDBException (org.xmldb.api.base.XMLDBException)10 ExtendedResource (org.exist.xmldb.ExtendedResource)9 EXistXPathQueryService (org.exist.xmldb.EXistXPathQueryService)8 URISyntaxException (java.net.URISyntaxException)6 XPathQueryService (org.xmldb.api.modules.XPathQueryService)6 EXistCollectionManagementService (org.exist.xmldb.EXistCollectionManagementService)5 UserManagementService (org.exist.xmldb.UserManagementService)5 BeforeClass (org.junit.BeforeClass)5 Test (org.junit.Test)5 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)4 MimeType (org.exist.util.MimeType)4 XPathException (org.exist.xquery.XPathException)4 LocalBinaryResource (org.exist.xmldb.LocalBinaryResource)3