Search in sources :

Example 31 with Serializer

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

the class RpcConnection method serialize.

private void serialize(final DBBroker broker, final Properties properties, final ConsumerE<Serializer, SAXException> toSaxFunction, final Writer writer) throws SAXException, IOException {
    final Serializer serializer = broker.borrowSerializer();
    SAXSerializer saxSerializer = null;
    try {
        serializer.setUser(user);
        serializer.setProperties(properties);
        saxSerializer = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
        saxSerializer.setOutput(writer, properties);
        serializer.setSAXHandlers(saxSerializer, saxSerializer);
        toSaxFunction.accept(serializer);
        writer.flush();
    } finally {
        if (saxSerializer != null) {
            SerializerPool.getInstance().returnObject(saxSerializer);
        }
        broker.returnSerializer(serializer);
    }
}
Also used : SAXSerializer(org.exist.util.serializer.SAXSerializer) SAXSerializer(org.exist.util.serializer.SAXSerializer) Serializer(org.exist.storage.serializers.Serializer)

Example 32 with Serializer

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

the class SystemExportImportTest method serializer.

private String serializer(final DBBroker broker, final DocumentImpl document) throws SAXException {
    final Serializer serializer = broker.borrowSerializer();
    try {
        serializer.setUser(broker.getCurrentSubject());
        serializer.setProperties(contentsOutputProps);
        return serializer.serialize(document);
    } finally {
        broker.returnSerializer(serializer);
    }
}
Also used : Serializer(org.exist.storage.serializers.Serializer)

Example 33 with Serializer

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

the class EXistDbXMLReader method parse.

@Override
public void parse(final InputSource input) {
    if (!(input instanceof EXistDbInputSource)) {
        throw new UnsupportedOperationException("EXistDbXMLReader only accepts EXistDbInputSource");
    }
    EXistDbInputSource source = (EXistDbInputSource) input;
    final Serializer serializer = source.getBroker().borrowSerializer();
    try {
        this.source = input;
        this.contentHandler.setDocumentLocator(this);
        serializer.setSAXHandlers(this.contentHandler, null);
        serializer.toSAX(source.getDocument());
        this.contentHandler.endDocument();
    } catch (SAXParseException e) {
        LOG.error("SaxParseException: {}", e.getMessage(), e);
        try {
            this.errHandler.error(e);
        } catch (Exception e2) {
            LOG.error("Exception handling exception: {}", e2.getMessage(), e2);
        }
    } catch (Exception e) {
        LOG.error("Exception: {}", e.getMessage(), e);
        try {
            /* FIXME:  Do we need to forward the exception to the errHandler, or has this
                 * been done for us? - PLM
		 */
            this.errHandler.error(new SAXParseException("Unable to parse document", null, e));
        } catch (Exception e2) {
            LOG.error("Exception handling exception: {}", e.getMessage(), e2);
        }
    } finally {
        source.getBroker().returnSerializer(serializer);
        this.source = null;
    }
}
Also used : SAXParseException(org.xml.sax.SAXParseException) SAXParseException(org.xml.sax.SAXParseException) Serializer(org.exist.storage.serializers.Serializer)

Example 34 with Serializer

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

the class Stream method eval.

@Override
public Sequence eval(final Sequence[] args, @Nonnull final ResponseWrapper response) throws XPathException {
    if (args[0].isEmpty()) {
        return Sequence.EMPTY_SEQUENCE;
    }
    final Sequence inputNode = args[0];
    final Properties serializeOptions = new Properties();
    final String serOpts = args[1].getStringValue();
    final String[] contents = Option.tokenize(serOpts);
    for (String content : contents) {
        final String[] pair = Option.parseKeyValuePair(content);
        if (pair == null) {
            throw new XPathException(this, "Found invalid serialization option: " + content);
        }
        if (LOG.isDebugEnabled()) {
            logger.debug("Setting serialization property: {} = {}", pair[0], pair[1]);
        }
        serializeOptions.setProperty(pair[0], pair[1]);
    }
    if (!"org.exist.http.servlets.HttpResponseWrapper".equals(response.getClass().getName())) {
        throw new XPathException(this, ErrorCodes.XPDY0002, signature.toString() + " can only be used within the EXistServlet or XQueryServlet");
    }
    final String mediaType = serializeOptions.getProperty("media-type", "application/xml");
    final String encoding = serializeOptions.getProperty("encoding", "UTF-8");
    if (mediaType != null) {
        response.setContentType(mediaType + "; charset=" + encoding);
    }
    try {
        final BrokerPool db = BrokerPool.getInstance();
        try (final DBBroker broker = db.getBroker();
            final PrintWriter output = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), encoding))) {
            final Serializer serializer = broker.borrowSerializer();
            final SerializerPool serializerPool = SerializerPool.getInstance();
            final SAXSerializer sax = (SAXSerializer) serializerPool.borrowObject(SAXSerializer.class);
            try {
                sax.setOutput(output, serializeOptions);
                serializer.setProperties(serializeOptions);
                serializer.setSAXHandlers(sax, sax);
                serializer.toSAX(inputNode, 1, inputNode.getItemCount(), false, false, 0, 0);
            } catch (final SAXException e) {
                e.printStackTrace();
                throw new IOException(e);
            } finally {
                serializerPool.returnObject(sax);
                broker.returnSerializer(serializer);
            }
            output.flush();
        }
        // commit the response
        response.flushBuffer();
    } catch (final IOException e) {
        throw new XPathException(this, "IO exception while streaming node: " + e.getMessage(), e);
    } catch (final EXistException e) {
        throw new XPathException(this, "Exception while streaming node: " + e.getMessage(), e);
    }
    return Sequence.EMPTY_SEQUENCE;
}
Also used : SerializerPool(org.exist.util.serializer.SerializerPool) Sequence(org.exist.xquery.value.Sequence) IOException(java.io.IOException) EXistException(org.exist.EXistException) Properties(java.util.Properties) SAXException(org.xml.sax.SAXException) DBBroker(org.exist.storage.DBBroker) OutputStreamWriter(java.io.OutputStreamWriter) SAXSerializer(org.exist.util.serializer.SAXSerializer) BrokerPool(org.exist.storage.BrokerPool) PrintWriter(java.io.PrintWriter) SAXSerializer(org.exist.util.serializer.SAXSerializer) Serializer(org.exist.storage.serializers.Serializer)

Example 35 with Serializer

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

the class PersistentDomTest method serialize.

private static String serialize(final DBBroker broker, final Node node, @Nullable final Properties outputProperties) throws IOException, SAXException {
    // serialize the results to the response output stream
    final Serializer serializer = broker.borrowSerializer();
    SAXSerializer sax = null;
    try (final StringWriter writer = new StringWriter()) {
        sax = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
        sax.setOutput(writer, outputProperties);
        serializer.setProperties(outputProperties);
        serializer.setSAXHandlers(sax, sax);
        serializer.toSAX(new NodeProxy((NodeHandle) node));
        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) SAXSerializer(org.exist.util.serializer.SAXSerializer) 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