|
50 | 50 | import org.exist.EXistException;
|
51 | 51 | import org.exist.collections.Collection;
|
52 | 52 | import org.exist.collections.triggers.TriggerException;
|
| 53 | +import org.exist.dom.persistent.DocumentImpl; |
| 54 | +import org.exist.dom.persistent.NodeImpl; |
| 55 | +import org.exist.dom.persistent.NodeProxy; |
53 | 56 | import org.exist.security.PermissionDeniedException;
|
54 | 57 | import org.exist.storage.lock.Lock;
|
| 58 | +import org.exist.storage.serializers.Serializer; |
55 | 59 | import org.exist.storage.txn.TransactionException;
|
56 | 60 | import org.exist.storage.txn.Txn;
|
57 | 61 | import org.exist.util.BinaryValueInputSource;
|
|
61 | 65 | import org.exist.xmldb.XmldbURI;
|
62 | 66 | import org.exist.xquery.*;
|
63 | 67 | import org.exist.xquery.value.*;
|
| 68 | +import org.w3c.dom.Document; |
| 69 | +import org.w3c.dom.Node; |
64 | 70 | import org.xml.sax.SAXException;
|
65 | 71 |
|
66 |
| -import java.io.BufferedOutputStream; |
67 |
| -import java.io.IOException; |
68 |
| -import java.io.OutputStream; |
| 72 | +import javax.annotation.Nullable; |
| 73 | +import java.io.*; |
69 | 74 | import java.net.URI;
|
70 | 75 | import java.nio.file.Files;
|
71 | 76 | import java.nio.file.Path;
|
@@ -253,8 +258,31 @@ protected void eval(final String path, final DataType dataType, final Optional<I
|
253 | 258 | if(data.isPresent()) {
|
254 | 259 | // store the resource
|
255 | 260 | try (final OutputStream os = new BufferedOutputStream(Files.newOutputStream(destPath, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING))) {
|
256 |
| - ((BinaryValue)data.get()).streamBinaryTo(os); |
257 |
| - } catch (final IOException e) { |
| 261 | + if (data.get() instanceof BinaryValue) { |
| 262 | + // binary |
| 263 | + ((BinaryValue)data.get()).streamBinaryTo(os); |
| 264 | + } else { |
| 265 | + // XML |
| 266 | + final Serializer serializer = context.getBroker().borrowSerializer(); |
| 267 | + try { |
| 268 | + serializer.setUser(context.getSubject()); |
| 269 | + serializer.setProperty("omit-xml-declaration", "no"); |
| 270 | + |
| 271 | + final NodeValue nodeValue; |
| 272 | + if (data.get() instanceof NodeValue || data.get() instanceof NodeProxy) { |
| 273 | + nodeValue = (NodeValue) data.get(); |
| 274 | + } else { |
| 275 | + final NodeImpl n = (NodeImpl) data.get(); |
| 276 | + nodeValue = new NodeProxy(n instanceof DocumentImpl ? (DocumentImpl)n : (DocumentImpl)n.getOwnerDocument(), n.getNodeId()); |
| 277 | + } |
| 278 | + try (final Writer writer = new OutputStreamWriter(os)) { |
| 279 | + serializer.serialize(nodeValue, writer); |
| 280 | + } |
| 281 | + } finally { |
| 282 | + context.getBroker().returnSerializer(serializer); |
| 283 | + } |
| 284 | + } |
| 285 | + } catch (final IOException | SAXException e) { |
258 | 286 | throw new XPathException(this, "Cannot serialize file. A problem occurred while serializing the binary data: " + e.getMessage(), e);
|
259 | 287 | }
|
260 | 288 | }
|
@@ -307,9 +335,16 @@ protected void eval(final String path, final DataType dataType, final Optional<I
|
307 | 335 | try (final Txn transaction = context.getBroker().getBrokerPool().getTransactionManager().beginTransaction()) {
|
308 | 336 |
|
309 | 337 | try (final Collection collection = context.getBroker().openCollection(destPath.removeLastSegment(), Lock.LockMode.WRITE_LOCK)) {
|
310 |
| - final BinaryValue binaryValue = (BinaryValue) data.get(); |
311 | 338 | final MimeType mimeType = MimeTable.getInstance().getContentTypeFor(destPath.lastSegment());
|
312 |
| - context.getBroker().storeDocument(transaction, destPath.lastSegment(), new BinaryValueInputSource(binaryValue), mimeType, collection); |
| 339 | + |
| 340 | + if (data.get() instanceof BinaryValue) { |
| 341 | + // binary |
| 342 | + final BinaryValue binaryValue = (BinaryValue) data.get(); |
| 343 | + context.getBroker().storeDocument(transaction, destPath.lastSegment(), new BinaryValueInputSource(binaryValue), mimeType, collection); |
| 344 | + } else { |
| 345 | + // XML |
| 346 | + context.getBroker().storeDocument(transaction, destPath.lastSegment(), (Node)data.get(), mimeType, collection); |
| 347 | + } |
313 | 348 | }
|
314 | 349 | transaction.commit();
|
315 | 350 | } catch (final IOException | PermissionDeniedException | EXistException | LockException | SAXException e) {
|
|
0 commit comments