use of org.exist.storage.serializers.Serializer in project exist by eXist-db.
the class STXTemplatesCache method compileTemplate.
private Templates compileTemplate(final DBBroker broker, final DocumentImpl stylesheet) throws TransformerConfigurationException, SAXException {
final Serializer serializer = broker.borrowSerializer();
try {
final TemplatesHandler thandler = factory.newTemplatesHandler();
serializer.setSAXHandlers(thandler, null);
serializer.toSAX(stylesheet);
return thandler.getTemplates();
} finally {
broker.returnSerializer(serializer);
}
}
use of org.exist.storage.serializers.Serializer in project exist by eXist-db.
the class RESTServer method writeResultJSON.
private void writeResultJSON(final HttpServletResponse response, final DBBroker broker, final Txn transaction, final Sequence results, int howmany, int start, final Properties outputProperties, final boolean wrap, final long compilationTime, final long executionTime) throws BadRequestException {
// calculate number of results to return
final int rlen = results.getItemCount();
if (!results.isEmpty()) {
if ((start < 1) || (start > rlen)) {
throw new BadRequestException("Start parameter out of range");
}
// FD : correct bound evaluation
if (((howmany + start) > rlen) || (howmany <= 0)) {
howmany = rlen - start + 1;
}
} else {
howmany = 0;
}
final Serializer serializer = broker.borrowSerializer();
outputProperties.setProperty(Serializer.GENERATE_DOC_EVENTS, "false");
try {
serializer.setProperties(outputProperties);
try (Writer writer = new OutputStreamWriter(response.getOutputStream(), outputProperties.getProperty(OutputKeys.ENCODING))) {
final JSONObject root = new JSONObject();
root.addObject(new JSONSimpleProperty("start", Integer.toString(start), true));
root.addObject(new JSONSimpleProperty("count", Integer.toString(howmany), true));
root.addObject(new JSONSimpleProperty("hits", Integer.toString(results.getItemCount()), true));
if (outputProperties.getProperty(Serializer.PROPERTY_SESSION_ID) != null) {
root.addObject(new JSONSimpleProperty("session", outputProperties.getProperty(Serializer.PROPERTY_SESSION_ID)));
}
root.addObject(new JSONSimpleProperty("compilationTime", Long.toString(compilationTime), true));
root.addObject(new JSONSimpleProperty("executionTime", Long.toString(executionTime), true));
final JSONObject data = new JSONObject("data");
root.addObject(data);
Item item;
for (int i = --start; i < start + howmany; i++) {
item = results.itemAt(i);
if (Type.subTypeOf(item.getType(), Type.NODE)) {
final NodeValue value = (NodeValue) item;
JSONValue json;
if ("json".equals(outputProperties.getProperty("method", "xml"))) {
json = new JSONValue(serializer.serialize(value), false);
json.setSerializationDataType(JSONNode.SerializationDataType.AS_LITERAL);
} else {
json = new JSONValue(serializer.serialize(value));
json.setSerializationType(JSONNode.SerializationType.AS_ARRAY);
}
data.addObject(json);
} else {
final JSONValue json = new JSONValue(item.getStringValue());
json.setSerializationType(JSONNode.SerializationType.AS_ARRAY);
data.addObject(json);
}
}
root.serialize(writer, true);
writer.flush();
}
} catch (final IOException | XPathException | SAXException e) {
throw new BadRequestException("Error while serializing xml: " + e.toString(), e);
} finally {
broker.returnSerializer(serializer);
}
}
use of org.exist.storage.serializers.Serializer in project exist by eXist-db.
the class NodeProxy method toSAX.
@Override
public void toSAX(final DBBroker broker, final ContentHandler handler, final Properties properties) throws SAXException {
final Serializer serializer = broker.borrowSerializer();
try {
serializer.setProperty(Serializer.GENERATE_DOC_EVENTS, "false");
if (properties != null) {
serializer.setProperties(properties);
}
if (handler instanceof LexicalHandler) {
serializer.setSAXHandlers(handler, (LexicalHandler) handler);
} else {
serializer.setSAXHandlers(handler, null);
}
serializer.toSAX(this);
} finally {
broker.returnSerializer(serializer);
}
}
use of org.exist.storage.serializers.Serializer in project exist by eXist-db.
the class ExtendedDOMSerializer method startNode.
@Override
protected void startNode(Node node) throws TransformerException {
if (node.getNodeType() == NodeImpl.REFERENCE_NODE) {
final SAXSerializer handler = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
handler.setReceiver(receiver);
final Serializer serializer = broker.borrowSerializer();
serializer.setSAXHandlers(handler, handler);
try {
serializer.setProperties(outputProperties);
serializer.setProperty(Serializer.GENERATE_DOC_EVENTS, "false");
} catch (final SAXNotRecognizedException | SAXNotSupportedException e) {
// Nothing to do ?
}
try {
serializer.toSAX(((ReferenceNode) node).getReference());
} catch (final SAXException e) {
throw new TransformerException(e.getMessage(), e);
} finally {
SerializerPool.getInstance().returnObject(handler);
broker.returnSerializer(serializer);
}
} else {
super.startNode(node);
}
}
use of org.exist.storage.serializers.Serializer in project exist by eXist-db.
the class XQuerySerializer method serializeXML.
private void serializeXML(final Sequence sequence, final int start, final int howmany, final boolean wrap, final boolean typed, final long compilationTime, final long executionTime) throws SAXException, XPathException {
final Serializer serializer = broker.borrowSerializer();
SAXSerializer sax = null;
try {
sax = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
sax.setOutput(writer, outputProperties);
serializer.setProperties(outputProperties);
serializer.setSAXHandlers(sax, sax);
serializer.toSAX(sequence, start, howmany, wrap, typed, compilationTime, executionTime);
} catch (SAXNotSupportedException | SAXNotRecognizedException e) {
throw new SAXException(e.getMessage(), e);
} finally {
if (sax != null) {
SerializerPool.getInstance().returnObject(sax);
}
broker.returnSerializer(serializer);
}
}
Aggregations