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);
}
}
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);
}
}
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;
}
}
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;
}
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);
}
}
Aggregations