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