Search in sources :

Example 36 with Serializer

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

the class ConstructedNodesRecoveryTest method serialize.

private String serialize(final DBBroker broker, final DocumentImpl doc) throws IOException, SAXException {
    final Serializer serializer = broker.borrowSerializer();
    SAXSerializer sax = null;
    try (final StringWriter writer = new StringWriter()) {
        sax = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
        final Properties outputProperties = new Properties();
        outputProperties.setProperty(OutputKeys.INDENT, "no");
        outputProperties.setProperty(OutputKeys.ENCODING, "UTF-8");
        sax.setOutput(writer, outputProperties);
        serializer.setProperties(outputProperties);
        serializer.setSAXHandlers(sax, sax);
        serializer.toSAX(doc);
        return writer.toString();
    } finally {
        if (sax != null) {
            SerializerPool.getInstance().returnObject(sax);
        }
        broker.returnSerializer(serializer);
    }
}
Also used : StringWriter(java.io.StringWriter) SAXSerializer(org.exist.util.serializer.SAXSerializer) Properties(java.util.Properties) SAXSerializer(org.exist.util.serializer.SAXSerializer) Serializer(org.exist.storage.serializers.Serializer)

Example 37 with Serializer

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

the class AbstractCompressFunction method compressResource.

/**
 * Adds a document to a archive
 *
 * @param os
 *            The Output Stream to add the document to
 * @param doc
 *            The document to add to the archive
 * @param useHierarchy
 *            Whether to use a folder hierarchy in the archive file that
 *            reflects the collection hierarchy
 */
private void compressResource(OutputStream os, DocumentImpl doc, boolean useHierarchy, String stripOffset, String method, String name) throws IOException, SAXException {
    // create an entry in the Tar for the document
    final Object entry;
    if (name != null) {
        entry = newEntry(name);
    } else if (useHierarchy) {
        String docCollection = doc.getCollection().getURI().toString();
        XmldbURI collection = XmldbURI.create(removeLeadingOffset(docCollection, stripOffset));
        entry = newEntry(collection.append(doc.getFileURI()).toString());
    } else {
        entry = newEntry(doc.getFileURI().toString());
    }
    final byte[] value;
    if (doc.getResourceType() == DocumentImpl.XML_FILE) {
        // xml file
        final Serializer serializer = context.getBroker().borrowSerializer();
        try {
            serializer.setUser(context.getSubject());
            serializer.setProperty("omit-xml-declaration", "no");
            getDynamicSerializerOptions(serializer);
            String strDoc = serializer.serialize(doc);
            value = strDoc.getBytes();
        } finally {
            context.getBroker().returnSerializer(serializer);
        }
    } else if (doc.getResourceType() == DocumentImpl.BINARY_FILE && doc.getContentLength() > 0) {
        // binary file
        try (final InputStream is = context.getBroker().getBinaryResource((BinaryDocument) doc);
            final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream(doc.getContentLength() == -1 ? 1024 : (int) doc.getContentLength())) {
            baos.write(is);
            value = baos.toByteArray();
        }
    } else {
        value = new byte[0];
    }
    // close the entry
    final CRC32 chksum = new CRC32();
    if (entry instanceof ZipEntry && "store".equals(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);
    closeEntry(os);
}
Also used : CRC32(java.util.zip.CRC32) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) ZipEntry(java.util.zip.ZipEntry) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) XmldbURI(org.exist.xmldb.XmldbURI) Serializer(org.exist.storage.serializers.Serializer)

Example 38 with Serializer

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

the class CacheFunctions method serializeKey.

private String serializeKey(final Sequence key) throws XPathException {
    final StringBuilder builder = new StringBuilder();
    final Serializer serializer = context.getBroker().borrowSerializer();
    try {
        serializer.setProperties(OUTPUT_PROPERTIES);
        for (final SequenceIterator i = key.iterate(); i.hasNext(); ) {
            final Item item = i.nextItem();
            try {
                final NodeValue node = (NodeValue) item;
                builder.append(serializer.serialize(node));
            } catch (final ClassCastException e) {
                builder.append(item.getStringValue());
            }
        }
        return builder.toString();
    } catch (final SAXException e) {
        throw new XPathException(this, KEY_SERIALIZATION, e);
    } finally {
        context.getBroker().returnSerializer(serializer);
    }
}
Also used : XPathException(org.exist.xquery.XPathException) Serializer(org.exist.storage.serializers.Serializer) SAXException(org.xml.sax.SAXException)

Example 39 with Serializer

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

the class CacheFunctions method toMapKeys.

private String[] toMapKeys(final Sequence keys) throws XPathException {
    final String[] mapKeys = new String[keys.getItemCount()];
    final Serializer serializer = context.getBroker().borrowSerializer();
    try {
        serializer.setProperties(OUTPUT_PROPERTIES);
        int i = 0;
        for (final SequenceIterator it = keys.iterate(); it.hasNext(); ) {
            final Item item = it.nextItem();
            try {
                final NodeValue node = (NodeValue) item;
                mapKeys[i] = serializer.serialize(node);
            } catch (final ClassCastException e) {
                mapKeys[i] = item.getStringValue();
            }
            i++;
        }
    } catch (final SAXException e) {
        throw new XPathException(this, KEY_SERIALIZATION, e);
    } finally {
        context.getBroker().returnSerializer(serializer);
    }
    return mapKeys;
}
Also used : XPathException(org.exist.xquery.XPathException) Serializer(org.exist.storage.serializers.Serializer) SAXException(org.xml.sax.SAXException)

Example 40 with Serializer

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

the class MoveCollectionRecoveryTest method read.

private void read() throws EXistException, PermissionDeniedException, SAXException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        final Serializer serializer = broker.borrowSerializer();
        try (final LockedDocument lockedDoc = broker.getXMLResource(TestConstants.DESTINATION_COLLECTION_URI.append("test3").append(TestConstants.TEST_XML_URI), LockMode.READ_LOCK)) {
            assertNotNull("Document should not be null", lockedDoc);
            String data = serializer.serialize(lockedDoc.getDocument());
            assertNotNull(data);
        } finally {
            broker.returnSerializer(serializer);
        }
    }
}
Also used : LockedDocument(org.exist.dom.persistent.LockedDocument) Serializer(org.exist.storage.serializers.Serializer)

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