Search in sources :

Example 1 with FileSystemResolver

use of org.expath.pkg.repo.FileSystemStorage.FileSystemResolver in project exist by eXist-db.

the class ExistPkgExtension method setupPackage.

// TODO: Must not be here (in the parsing class).  See the comment at the
// end of parseDescriptor().
private void setupPackage(Package pkg, ExistPkgInfo info) throws PackageException {
    // TODO: FIXME: Bad, BAD design!  But will be resolved naturally by moving the
    // install code within the storage class (because we are writing on disk)...
    final FileSystemResolver res = (FileSystemResolver) pkg.getResolver();
    final Path classpath = res.resolveResourceAsFile(".exist/classpath.txt");
    // create [pkg_dir]/.exist/classpath.txt if not already
    final Path exist = classpath.getParent();
    if (!Files.exists(exist)) {
        try {
            Files.createDirectories(exist);
        } catch (final IOException e) {
            throw new PackageException("Impossible to create directory: " + exist);
        }
    }
    final Set<String> jars = info.getJars();
    try (final Writer out = Files.newBufferedWriter(classpath)) {
        for (final String jar : jars) {
            StreamSource jar_src;
            try {
                jar_src = res.resolveComponent(jar);
            } catch (final NotExistException ex) {
                final String msg = "Inconsistent package descriptor, the JAR file is not in the package: ";
                throw new PackageException(msg + jar, ex);
            }
            final URI uri = URI.create(jar_src.getSystemId());
            final Path file = Paths.get(uri);
            out.write(file.normalize().toString());
            out.write("\n");
        }
    } catch (final IOException ex) {
        throw new PackageException("Error writing the eXist classpath file: " + classpath, ex);
    }
}
Also used : Path(java.nio.file.Path) StreamSource(javax.xml.transform.stream.StreamSource) PackageException(org.expath.pkg.repo.PackageException) NotExistException(org.expath.pkg.repo.Storage.NotExistException) IOException(java.io.IOException) URI(java.net.URI) Writer(java.io.Writer) FileSystemResolver(org.expath.pkg.repo.FileSystemStorage.FileSystemResolver)

Example 2 with FileSystemResolver

use of org.expath.pkg.repo.FileSystemStorage.FileSystemResolver in project exist by eXist-db.

the class ExistRepository method resolveXQueryModule.

/**
 * Resolve an XQuery Module.
 *
 * @param namespace the namespace of the module
 * @return the path to the module, or null
 *
 * @throws XPathException with:
 *      XQST0046 if the namespace URI is invalid
 *      XQST0059 if an error occurs loading the module
 */
public Path resolveXQueryModule(final String namespace) throws XPathException {
    final URI uri;
    try {
        uri = new URI(namespace);
    } catch (final URISyntaxException ex) {
        throw new XPathException(ErrorCodes.XQST0046, "Invalid URI: " + namespace, ex);
    }
    for (final Packages pp : myParent.listPackages()) {
        final Package pkg = pp.latest();
        // FIXME: Rely on having a file system storage, that's probably a bad design!
        final FileSystemResolver resolver = (FileSystemResolver) pkg.getResolver();
        final ExistPkgInfo info = (ExistPkgInfo) pkg.getInfo("exist");
        if (info != null) {
            final String f = info.getXQuery(uri);
            if (f != null) {
                return resolver.resolveComponentAsFile(f);
            }
        }
        // declared here to be used in catch
        String sysid = null;
        Source src = null;
        try {
            src = pkg.resolve(namespace, URISpace.XQUERY);
            if (src != null) {
                sysid = src.getSystemId();
                return Paths.get(new URI(sysid));
            }
        } catch (final URISyntaxException ex) {
            throw new XPathException(ErrorCodes.XQST0046, "Error parsing the URI of the query library: " + sysid, ex);
        } catch (final PackageException ex) {
            throw new XPathException(ErrorCodes.XQST0059, "Error resolving the query library: " + namespace, ex);
        } finally {
            if (src != null && src instanceof StreamSource) {
                final StreamSource streamSource = ((StreamSource) src);
                try {
                    if (streamSource.getInputStream() != null) {
                        streamSource.getInputStream().close();
                    } else if (streamSource.getReader() != null) {
                        streamSource.getReader().close();
                    }
                } catch (final IOException e) {
                    LOG.warn("Unable to close pkg source: {}", e.getMessage(), e);
                }
            }
        }
    }
    return null;
}
Also used : XPathException(org.exist.xquery.XPathException) StreamSource(javax.xml.transform.stream.StreamSource) URISyntaxException(java.net.URISyntaxException) Package(org.expath.pkg.repo.Package) IOException(java.io.IOException) URI(java.net.URI) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) FileSystemResolver(org.expath.pkg.repo.FileSystemStorage.FileSystemResolver)

Aggregations

IOException (java.io.IOException)2 URI (java.net.URI)2 StreamSource (javax.xml.transform.stream.StreamSource)2 FileSystemResolver (org.expath.pkg.repo.FileSystemStorage.FileSystemResolver)2 Writer (java.io.Writer)1 URISyntaxException (java.net.URISyntaxException)1 Path (java.nio.file.Path)1 Source (javax.xml.transform.Source)1 XPathException (org.exist.xquery.XPathException)1 Package (org.expath.pkg.repo.Package)1 PackageException (org.expath.pkg.repo.PackageException)1 NotExistException (org.expath.pkg.repo.Storage.NotExistException)1