use of org.exist.storage.serializers.Serializer in project exist by eXist-db.
the class Sync method processRepoDesc.
/**
* Merge repo.xml modified by user with original file. This is necessary because we have to
* remove sensitive information during upload (default password) and need to restore it
* when the package is synchronized back to disk.
*/
private void processRepoDesc(final Path targetFile, final DocumentImpl doc, final SAXSerializer sax, final MemTreeBuilder output) {
try {
final DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final Document original = builder.parse(targetFile.toFile());
final Serializer serializer = context.getBroker().borrowSerializer();
try (final Writer writer = new OutputStreamWriter(new BufferedOutputStream(Files.newOutputStream(targetFile)), StandardCharsets.UTF_8)) {
sax.setOutput(writer, DEFAULT_PROPERTIES);
final StreamSource styleSource = new StreamSource(Sync.class.getResourceAsStream("repo.xsl"));
final SAXTransformerFactory factory = TransformerFactoryAllocator.getTransformerFactory(context.getBroker().getBrokerPool());
final TransformerHandler handler = factory.newTransformerHandler(styleSource);
handler.getTransformer().setParameter("original", original.getDocumentElement());
handler.setResult(new SAXResult(sax));
serializer.reset();
serializer.setProperties(DEFAULT_PROPERTIES);
serializer.setSAXHandlers(handler, handler);
serializer.toSAX(doc);
} finally {
context.getBroker().returnSerializer(serializer);
}
} catch (final ParserConfigurationException e) {
reportError(output, "Parser exception while saving file " + targetFile.toAbsolutePath().toString() + ": " + e.getMessage());
} catch (final SAXException e) {
reportError(output, "SAX exception while saving file " + targetFile.toAbsolutePath().toString() + ": " + e.getMessage());
} catch (final IOException e) {
reportError(output, "IO exception while saving file " + targetFile.toAbsolutePath().toString() + ": " + e.getMessage());
} catch (final TransformerException e) {
reportError(output, "Transformation exception while saving file " + targetFile.toAbsolutePath().toString() + ": " + e.getMessage());
}
}
use of org.exist.storage.serializers.Serializer in project exist by eXist-db.
the class Sync method saveXML.
private void saveXML(final Path targetFile, final DocumentImpl doc, final MemTreeBuilder output) throws IOException {
final SAXSerializer sax = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
try {
final boolean isRepoXML = Files.exists(targetFile) && FileUtils.fileName(targetFile).equals("repo.xml");
if (isRepoXML) {
processRepoDesc(targetFile, doc, sax, output);
} else {
final Serializer serializer = context.getBroker().borrowSerializer();
try (final Writer writer = new OutputStreamWriter(new BufferedOutputStream(Files.newOutputStream(targetFile)), StandardCharsets.UTF_8)) {
sax.setOutput(writer, DEFAULT_PROPERTIES);
serializer.setProperties(DEFAULT_PROPERTIES);
serializer.setSAXHandlers(sax, sax);
serializer.toSAX(doc);
} finally {
context.getBroker().returnSerializer(serializer);
}
}
} catch (final SAXException e) {
reportError(output, "SAX exception while saving file " + targetFile.toAbsolutePath().toString() + ": " + e.getMessage());
} finally {
SerializerPool.getInstance().returnObject(sax);
}
}
use of org.exist.storage.serializers.Serializer in project exist by eXist-db.
the class SerializeToFile method serializeXML.
private void serializeXML(final SequenceIterator siNode, final Properties outputProperties, final Path file, final boolean doAppend) throws XPathException {
final Serializer serializer = context.getBroker().borrowSerializer();
StandardOpenOption[] ops = doAppend ? new StandardOpenOption[] { StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.APPEND } : new StandardOpenOption[] { StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING };
try (final OutputStream os = new BufferedOutputStream(Files.newOutputStream(file, ops));
final Writer writer = new OutputStreamWriter(os, Charset.forName(outputProperties.getProperty(OutputKeys.ENCODING)))) {
serializer.setProperties(outputProperties);
while (siNode.hasNext()) {
final NodeValue nv = (NodeValue) siNode.nextItem();
serializer.serialize(nv, writer);
}
} catch (UnsupportedOperationException | IllegalArgumentException e) {
throw new XPathException(this, "Error wile writing to file: " + e.getMessage(), e);
} catch (final IOException | SAXException e) {
throw new XPathException(this, "Cannot serialize file. A problem occurred while serializing the node set: " + e.getMessage(), e);
} finally {
context.getBroker().returnSerializer(serializer);
}
}
use of org.exist.storage.serializers.Serializer in project exist by eXist-db.
the class EXIUtils method getInputStream.
protected static InputStream getInputStream(Item item, XQueryContext context) throws XPathException, MalformedURLException, IOException {
switch(item.getType()) {
case Type.ANY_URI:
LOG.debug("Streaming xs:anyURI");
// anyURI provided
String url = item.getStringValue();
// Fix URL
if (url.startsWith("/")) {
url = "xmldb:exist://" + url;
}
return new URL(url).openStream();
case Type.ELEMENT:
case Type.DOCUMENT:
LOG.debug("Streaming element or document node");
/*
if (item instanceof NodeProxy) {
NodeProxy np = (NodeProxy) item;
String url = "xmldb:exist://" + np.getDocument().getBaseURI();
LOG.debug("Document detected, adding URL " + url);
streamSource.setSystemId(url);
}
*/
// Node provided
final ConsumerE<ConsumerE<Serializer, IOException>, IOException> withSerializerFn = fn -> {
final Serializer serializer = context.getBroker().borrowSerializer();
try {
fn.accept(serializer);
} finally {
context.getBroker().returnSerializer(serializer);
}
};
NodeValue node = (NodeValue) item;
return new NodeInputStream(context.getBroker().getBrokerPool(), withSerializerFn, node);
default:
LOG.error("Wrong item type {}", Type.getTypeName(item.getType()));
throw new XPathException("wrong item type " + Type.getTypeName(item.getType()));
}
}
use of org.exist.storage.serializers.Serializer in project exist by eXist-db.
the class AbstractCompressFunction method compressElement.
/**
* Adds a element to a archive
*
* @param os
* The Output Stream to add the element to
* @param element
* The element to add to the archive
* @param useHierarchy
* Whether to use a folder hierarchy in the archive file that
* reflects the collection hierarchy
*/
private void compressElement(final OutputStream os, final Element element, final boolean useHierarchy, final String stripOffset) throws XPathException {
final String ns = element.getNamespaceURI();
if (!(element.getNodeName().equals("entry") || (ns != null && !ns.isEmpty()))) {
throw new XPathException(this, "Item must be type of xs:anyURI or element entry.");
}
if (element.getChildNodes().getLength() > 1) {
throw new XPathException(this, "Entry content is not valid XML fragment.");
}
String name = element.getAttribute("name");
// if(name == null)
// throw new XPathException(this, "Entry must have name attribute.");
final String type = element.getAttribute("type");
if ("uri".equals(type)) {
compressFromUri(os, URI.create(element.getFirstChild().getNodeValue()), useHierarchy, stripOffset, element.getAttribute("method"), name);
return;
}
if (useHierarchy) {
name = removeLeadingOffset(name, stripOffset);
} else {
name = name.substring(name.lastIndexOf("/") + 1);
}
if ("collection".equals(type)) {
name += "/";
}
Object entry = null;
try {
entry = newEntry(name);
if (!"collection".equals(type)) {
byte[] value;
final CRC32 chksum = new CRC32();
final Node content = element.getFirstChild();
if (content == null) {
value = new byte[0];
} else {
if (content.getNodeType() == Node.TEXT_NODE) {
String text = content.getNodeValue();
if ("binary".equals(type)) {
// base64 binary
value = Base64.decodeBase64(text);
} else {
// text
value = text.getBytes();
}
} else {
// xml
final Serializer serializer = context.getBroker().borrowSerializer();
try {
serializer.setUser(context.getSubject());
serializer.setProperty("omit-xml-declaration", "no");
getDynamicSerializerOptions(serializer);
value = serializer.serialize((NodeValue) content).getBytes();
} finally {
context.getBroker().returnSerializer(serializer);
}
}
}
if (entry instanceof ZipEntry && "store".equals(element.getAttribute("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);
}
} catch (final IOException | SAXException ioe) {
throw new XPathException(this, ioe.getMessage(), ioe);
} finally {
if (entry != null) {
try {
closeEntry(os);
} catch (final IOException ioe) {
throw new XPathException(this, ioe.getMessage(), ioe);
}
}
}
}
Aggregations