use of org.exist.storage.serializers.Serializer in project exist by eXist-db.
the class LocalResourceSet method getMembersAsResource.
@Override
public Resource getMembersAsResource() throws XMLDBException {
return this.<Resource>withDb((broker, transaction) -> {
final Serializer serializer = broker.borrowSerializer();
final SAXSerializer handler = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
final StringWriter writer = new StringWriter();
handler.setOutput(writer, outputProperties);
try {
// configure the serializer
collection.setProperty(Serializer.GENERATE_DOC_EVENTS, "false");
serializer.setProperties(outputProperties);
serializer.setUser(user);
serializer.setSAXHandlers(handler, handler);
// serialize results
handler.startDocument();
handler.startPrefixMapping("exist", Namespaces.EXIST_NS);
final AttributesImpl attribs = new AttributesImpl();
attribs.addAttribute("", "hitCount", "hitCount", "CDATA", Integer.toString(resources.size()));
handler.startElement(Namespaces.EXIST_NS, "result", "exist:result", attribs);
Item current;
char[] value;
for (Object resource : resources) {
current = (Item) resource;
if (Type.subTypeOf(current.getType(), Type.NODE)) {
((NodeValue) current).toSAX(broker, handler, outputProperties);
} else {
value = current.toString().toCharArray();
handler.characters(value, 0, value.length);
}
}
handler.endElement(Namespaces.EXIST_NS, "result", "exist:result");
handler.endPrefixMapping("exist");
handler.endDocument();
final Resource res = new LocalXMLResource(user, brokerPool, collection, XmldbURI.EMPTY_URI);
res.setContent(writer.toString());
return res;
} catch (final SAXException e) {
throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, "serialization error", e);
} finally {
SerializerPool.getInstance().returnObject(handler);
broker.returnSerializer(serializer);
}
});
}
use of org.exist.storage.serializers.Serializer in project exist by eXist-db.
the class TestDataGenerator method generate.
public Path[] generate(final DBBroker broker, final Collection collection, final String xqueryContent) throws SAXException {
try {
final DocumentSet docs = collection.allDocs(broker, new DefaultDocumentSet(), true);
final XQuery service = broker.getBrokerPool().getXQueryService();
final XQueryContext context = new XQueryContext(broker.getBrokerPool());
context.declareVariable("filename", "");
context.declareVariable("count", "0");
context.setStaticallyKnownDocuments(docs);
final String query = IMPORT + xqueryContent;
final CompiledXQuery compiled = service.compile(context, query);
for (int i = 0; i < count; i++) {
generatedFiles[i] = Files.createTempFile(prefix, ".xml");
context.declareVariable("filename", generatedFiles[i].getFileName().toString());
context.declareVariable("count", new Integer(i));
final Sequence results = service.execute(broker, compiled, Sequence.EMPTY_SEQUENCE);
final Serializer serializer = broker.borrowSerializer();
try (final Writer out = Files.newBufferedWriter(generatedFiles[i], StandardCharsets.UTF_8)) {
final SAXSerializer sax = new SAXSerializer(out, outputProps);
serializer.setSAXHandlers(sax, sax);
for (final SequenceIterator iter = results.iterate(); iter.hasNext(); ) {
final Item item = iter.nextItem();
if (!Type.subTypeOf(item.getType(), Type.NODE)) {
continue;
}
serializer.toSAX((NodeValue) item);
}
} finally {
broker.returnSerializer(serializer);
}
}
} catch (final XPathException | PermissionDeniedException | LockException | IOException e) {
LOG.error(e.getMessage(), e);
throw new SAXException(e.getMessage(), e);
}
return generatedFiles;
}
use of org.exist.storage.serializers.Serializer in project exist by eXist-db.
the class StylesheetResolverAndCompiler method compileTemplates.
private <E extends Exception> Templates compileTemplates(DBBroker broker, DocumentImpl stylesheet, XSLTErrorsListener<E> errorListener) throws E, TransformerConfigurationException, SAXException {
// factory.setURIResolver(new EXistURIResolver(broker, stylesheet.getCollection().getURI().toString()));
final TemplatesHandler handler = factory(broker.getBrokerPool(), errorListener).newTemplatesHandler();
handler.setSystemId(stylesheet.getBaseURI());
handler.startDocument();
final Serializer serializer = broker.borrowSerializer();
try {
serializer.setSAXHandlers(handler, null);
serializer.toSAX(stylesheet);
} finally {
broker.returnSerializer(serializer);
}
handler.endDocument();
final Templates t = handler.getTemplates();
// check for errors
errorListener.checkForErrors();
return t;
}
use of org.exist.storage.serializers.Serializer in project exist by eXist-db.
the class CollectionRemovalTest method retrieveDoc.
private void retrieveDoc(final XmldbURI uri) throws EXistException, PermissionDeniedException, SAXException, LockException {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Collection test = broker.openCollection(uri, LockMode.READ_LOCK)) {
assertNotNull(test);
try (final LockedDocument lockedDoc = test.getDocumentWithLock(broker, XmldbURI.createInternal("document.xml"), LockMode.READ_LOCK)) {
assertNotNull(lockedDoc);
final Serializer serializer = broker.borrowSerializer();
try {
String xml = serializer.serialize(lockedDoc.getDocument());
} finally {
broker.returnSerializer(serializer);
}
// NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
test.close();
}
}
}
use of org.exist.storage.serializers.Serializer in project exist by eXist-db.
the class ExistDocument method serialize.
private void serialize(final DBBroker broker, final DocumentImpl document, final OutputStream os) throws SAXException, IOException {
final Serializer serializer = broker.borrowSerializer();
// Set custom serialization options when available
if (!configuration.isEmpty()) {
serializer.setProperties(configuration);
}
SAXSerializer saxSerializer = null;
try {
saxSerializer = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
// Serialize document
try (final Writer writer = new OutputStreamWriter(os, UTF_8)) {
saxSerializer.setOutput(writer, configuration.isEmpty() ? null : configuration);
serializer.setSAXHandlers(saxSerializer, saxSerializer);
serializer.toSAX(document);
writer.flush();
}
} finally {
if (saxSerializer != null) {
SerializerPool.getInstance().returnObject(saxSerializer);
}
broker.returnSerializer(serializer);
}
}
Aggregations