Search in sources :

Example 16 with Serializer

use of org.exist.storage.serializers.Serializer in project exist by eXist-db.

the class Sync method processRepoDesc.

/**
 * Merge repo.xml modified by user with original file. This is necessary because we have to
 * remove sensitive information during upload (default password) and need to restore it
 * when the package is synchronized back to disk.
 */
private void processRepoDesc(final Path targetFile, final DocumentImpl doc, final SAXSerializer sax, final MemTreeBuilder output) {
    try {
        final DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        final Document original = builder.parse(targetFile.toFile());
        final Serializer serializer = context.getBroker().borrowSerializer();
        try (final Writer writer = new OutputStreamWriter(new BufferedOutputStream(Files.newOutputStream(targetFile)), StandardCharsets.UTF_8)) {
            sax.setOutput(writer, DEFAULT_PROPERTIES);
            final StreamSource styleSource = new StreamSource(Sync.class.getResourceAsStream("repo.xsl"));
            final SAXTransformerFactory factory = TransformerFactoryAllocator.getTransformerFactory(context.getBroker().getBrokerPool());
            final TransformerHandler handler = factory.newTransformerHandler(styleSource);
            handler.getTransformer().setParameter("original", original.getDocumentElement());
            handler.setResult(new SAXResult(sax));
            serializer.reset();
            serializer.setProperties(DEFAULT_PROPERTIES);
            serializer.setSAXHandlers(handler, handler);
            serializer.toSAX(doc);
        } finally {
            context.getBroker().returnSerializer(serializer);
        }
    } catch (final ParserConfigurationException e) {
        reportError(output, "Parser exception while saving file " + targetFile.toAbsolutePath().toString() + ": " + e.getMessage());
    } catch (final SAXException e) {
        reportError(output, "SAX exception while saving file " + targetFile.toAbsolutePath().toString() + ": " + e.getMessage());
    } catch (final IOException e) {
        reportError(output, "IO exception while saving file " + targetFile.toAbsolutePath().toString() + ": " + e.getMessage());
    } catch (final TransformerException e) {
        reportError(output, "Transformation exception while saving file " + targetFile.toAbsolutePath().toString() + ": " + e.getMessage());
    }
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) StreamSource(javax.xml.transform.stream.StreamSource) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) Document(org.w3c.dom.Document) BinaryDocument(org.exist.dom.persistent.BinaryDocument) SAXException(org.xml.sax.SAXException) SAXResult(javax.xml.transform.sax.SAXResult) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXSerializer(org.exist.util.serializer.SAXSerializer) Serializer(org.exist.storage.serializers.Serializer)

Example 17 with Serializer

use of org.exist.storage.serializers.Serializer in project exist by eXist-db.

the class Sync method saveXML.

private void saveXML(final Path targetFile, final DocumentImpl doc, final MemTreeBuilder output) throws IOException {
    final SAXSerializer sax = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
    try {
        final boolean isRepoXML = Files.exists(targetFile) && FileUtils.fileName(targetFile).equals("repo.xml");
        if (isRepoXML) {
            processRepoDesc(targetFile, doc, sax, output);
        } else {
            final Serializer serializer = context.getBroker().borrowSerializer();
            try (final Writer writer = new OutputStreamWriter(new BufferedOutputStream(Files.newOutputStream(targetFile)), StandardCharsets.UTF_8)) {
                sax.setOutput(writer, DEFAULT_PROPERTIES);
                serializer.setProperties(DEFAULT_PROPERTIES);
                serializer.setSAXHandlers(sax, sax);
                serializer.toSAX(doc);
            } finally {
                context.getBroker().returnSerializer(serializer);
            }
        }
    } catch (final SAXException e) {
        reportError(output, "SAX exception while saving file " + targetFile.toAbsolutePath().toString() + ": " + e.getMessage());
    } finally {
        SerializerPool.getInstance().returnObject(sax);
    }
}
Also used : SAXSerializer(org.exist.util.serializer.SAXSerializer) SAXSerializer(org.exist.util.serializer.SAXSerializer) Serializer(org.exist.storage.serializers.Serializer) SAXException(org.xml.sax.SAXException)

Example 18 with Serializer

use of org.exist.storage.serializers.Serializer in project exist by eXist-db.

the class SerializeToFile method serializeXML.

private void serializeXML(final SequenceIterator siNode, final Properties outputProperties, final Path file, final boolean doAppend) throws XPathException {
    final Serializer serializer = context.getBroker().borrowSerializer();
    StandardOpenOption[] ops = doAppend ? new StandardOpenOption[] { StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.APPEND } : new StandardOpenOption[] { StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING };
    try (final OutputStream os = new BufferedOutputStream(Files.newOutputStream(file, ops));
        final Writer writer = new OutputStreamWriter(os, Charset.forName(outputProperties.getProperty(OutputKeys.ENCODING)))) {
        serializer.setProperties(outputProperties);
        while (siNode.hasNext()) {
            final NodeValue nv = (NodeValue) siNode.nextItem();
            serializer.serialize(nv, writer);
        }
    } catch (UnsupportedOperationException | IllegalArgumentException e) {
        throw new XPathException(this, "Error wile writing to file: " + e.getMessage(), e);
    } catch (final IOException | SAXException e) {
        throw new XPathException(this, "Cannot serialize file. A problem occurred while serializing the node set: " + e.getMessage(), e);
    } finally {
        context.getBroker().returnSerializer(serializer);
    }
}
Also used : StandardOpenOption(java.nio.file.StandardOpenOption) SAXException(org.xml.sax.SAXException) Serializer(org.exist.storage.serializers.Serializer)

Example 19 with Serializer

use of org.exist.storage.serializers.Serializer in project exist by eXist-db.

the class EXIUtils method getInputStream.

protected static InputStream getInputStream(Item item, XQueryContext context) throws XPathException, MalformedURLException, IOException {
    switch(item.getType()) {
        case Type.ANY_URI:
            LOG.debug("Streaming xs:anyURI");
            // anyURI provided
            String url = item.getStringValue();
            // Fix URL
            if (url.startsWith("/")) {
                url = "xmldb:exist://" + url;
            }
            return new URL(url).openStream();
        case Type.ELEMENT:
        case Type.DOCUMENT:
            LOG.debug("Streaming element or document node");
            /*
            if (item instanceof NodeProxy) {
                NodeProxy np = (NodeProxy) item;
                String url = "xmldb:exist://" + np.getDocument().getBaseURI();
                LOG.debug("Document detected, adding URL " + url);
                streamSource.setSystemId(url);
            }
            */
            // Node provided
            final ConsumerE<ConsumerE<Serializer, IOException>, IOException> withSerializerFn = fn -> {
                final Serializer serializer = context.getBroker().borrowSerializer();
                try {
                    fn.accept(serializer);
                } finally {
                    context.getBroker().returnSerializer(serializer);
                }
            };
            NodeValue node = (NodeValue) item;
            return new NodeInputStream(context.getBroker().getBrokerPool(), withSerializerFn, node);
        default:
            LOG.error("Wrong item type {}", Type.getTypeName(item.getType()));
            throw new XPathException("wrong item type " + Type.getTypeName(item.getType()));
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) URL(java.net.URL) Type(org.exist.xquery.value.Type) IOException(java.io.IOException) Logger(org.apache.logging.log4j.Logger) NodeValue(org.exist.xquery.value.NodeValue) Serializer(org.exist.storage.serializers.Serializer) Item(org.exist.xquery.value.Item) NodeInputStream(org.exist.validation.internal.node.NodeInputStream) ConsumerE(com.evolvedbinary.j8fu.function.ConsumerE) LogManager(org.apache.logging.log4j.LogManager) XPathException(org.exist.xquery.XPathException) XQueryContext(org.exist.xquery.XQueryContext) InputStream(java.io.InputStream) NodeValue(org.exist.xquery.value.NodeValue) ConsumerE(com.evolvedbinary.j8fu.function.ConsumerE) XPathException(org.exist.xquery.XPathException) IOException(java.io.IOException) URL(java.net.URL) NodeInputStream(org.exist.validation.internal.node.NodeInputStream) Serializer(org.exist.storage.serializers.Serializer)

Example 20 with Serializer

use of org.exist.storage.serializers.Serializer in project exist by eXist-db.

the class AbstractCompressFunction method compressElement.

/**
 * Adds a element to a archive
 *
 * @param os
 *            The Output Stream to add the element to
 * @param element
 *            The element to add to the archive
 * @param useHierarchy
 *            Whether to use a folder hierarchy in the archive file that
 *            reflects the collection hierarchy
 */
private void compressElement(final OutputStream os, final Element element, final boolean useHierarchy, final String stripOffset) throws XPathException {
    final String ns = element.getNamespaceURI();
    if (!(element.getNodeName().equals("entry") || (ns != null && !ns.isEmpty()))) {
        throw new XPathException(this, "Item must be type of xs:anyURI or element entry.");
    }
    if (element.getChildNodes().getLength() > 1) {
        throw new XPathException(this, "Entry content is not valid XML fragment.");
    }
    String name = element.getAttribute("name");
    // if(name == null)
    // throw new XPathException(this, "Entry must have name attribute.");
    final String type = element.getAttribute("type");
    if ("uri".equals(type)) {
        compressFromUri(os, URI.create(element.getFirstChild().getNodeValue()), useHierarchy, stripOffset, element.getAttribute("method"), name);
        return;
    }
    if (useHierarchy) {
        name = removeLeadingOffset(name, stripOffset);
    } else {
        name = name.substring(name.lastIndexOf("/") + 1);
    }
    if ("collection".equals(type)) {
        name += "/";
    }
    Object entry = null;
    try {
        entry = newEntry(name);
        if (!"collection".equals(type)) {
            byte[] value;
            final CRC32 chksum = new CRC32();
            final Node content = element.getFirstChild();
            if (content == null) {
                value = new byte[0];
            } else {
                if (content.getNodeType() == Node.TEXT_NODE) {
                    String text = content.getNodeValue();
                    if ("binary".equals(type)) {
                        // base64 binary
                        value = Base64.decodeBase64(text);
                    } else {
                        // text
                        value = text.getBytes();
                    }
                } else {
                    // xml
                    final Serializer serializer = context.getBroker().borrowSerializer();
                    try {
                        serializer.setUser(context.getSubject());
                        serializer.setProperty("omit-xml-declaration", "no");
                        getDynamicSerializerOptions(serializer);
                        value = serializer.serialize((NodeValue) content).getBytes();
                    } finally {
                        context.getBroker().returnSerializer(serializer);
                    }
                }
            }
            if (entry instanceof ZipEntry && "store".equals(element.getAttribute("method"))) {
                ((ZipEntry) entry).setMethod(ZipOutputStream.STORED);
                chksum.update(value);
                ((ZipEntry) entry).setCrc(chksum.getValue());
                ((ZipEntry) entry).setSize(value.length);
            }
            putEntry(os, entry);
            os.write(value);
        }
    } catch (final IOException | SAXException ioe) {
        throw new XPathException(this, ioe.getMessage(), ioe);
    } finally {
        if (entry != null) {
            try {
                closeEntry(os);
            } catch (final IOException ioe) {
                throw new XPathException(this, ioe.getMessage(), ioe);
            }
        }
    }
}
Also used : CRC32(java.util.zip.CRC32) Node(org.w3c.dom.Node) ZipEntry(java.util.zip.ZipEntry) Serializer(org.exist.storage.serializers.Serializer) SAXException(org.xml.sax.SAXException)

Aggregations

Serializer (org.exist.storage.serializers.Serializer)64 SAXException (org.xml.sax.SAXException)21 DBBroker (org.exist.storage.DBBroker)16 LockedDocument (org.exist.dom.persistent.LockedDocument)15 SAXSerializer (org.exist.util.serializer.SAXSerializer)14 BrokerPool (org.exist.storage.BrokerPool)11 Properties (java.util.Properties)10 Sequence (org.exist.xquery.value.Sequence)10 Txn (org.exist.storage.txn.Txn)8 XPathException (org.exist.xquery.XPathException)8 StringWriter (java.io.StringWriter)7 Collection (org.exist.collections.Collection)7 IOException (java.io.IOException)6 Item (org.exist.xquery.value.Item)6 Path (java.nio.file.Path)5 EXistException (org.exist.EXistException)5 SequenceIterator (org.exist.xquery.value.SequenceIterator)5 NodeProxy (org.exist.dom.persistent.NodeProxy)4 TransactionManager (org.exist.storage.txn.TransactionManager)4 NodeValue (org.exist.xquery.value.NodeValue)4