From 904d7789c0e636c3b2e6a55fc6ec9ee333cc2023 Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Mon, 5 Sep 2022 08:29:46 +0200 Subject: [PATCH] Support writing file as XML --- .../src/org/argeo/cms/acr/fs/FsContent.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/org.argeo.cms/src/org/argeo/cms/acr/fs/FsContent.java b/org.argeo.cms/src/org/argeo/cms/acr/fs/FsContent.java index 77c2f7a75..55ef6ec46 100644 --- a/org.argeo.cms/src/org/argeo/cms/acr/fs/FsContent.java +++ b/org.argeo.cms/src/org/argeo/cms/acr/fs/FsContent.java @@ -24,6 +24,10 @@ import java.util.Set; import java.util.concurrent.CompletableFuture; import javax.xml.namespace.QName; +import javax.xml.transform.Source; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.stream.StreamResult; import org.argeo.api.acr.Content; import org.argeo.api.acr.ContentName; @@ -327,6 +331,7 @@ public class FsContent extends AbstractContent implements ProvidedContent { /* * READ / WRITE */ + @SuppressWarnings("unchecked") public CompletableFuture write(Class clss) { if (isContentClass(CrName.collection.qName())) { throw new IllegalStateException("Cannot directly write to a collection"); @@ -341,7 +346,21 @@ public class FsContent extends AbstractContent implements ProvidedContent { } }); return (CompletableFuture) res; + } else if (Source.class.isAssignableFrom(clss)) { + CompletableFuture res = new CompletableFuture(); + res.thenAccept((source) -> { +// Path targetPath = path.getParent().resolve(path.getFileName()+".xml"); + Path targetPath = path; + try (OutputStream out = Files.newOutputStream(targetPath)) { + StreamResult result = new StreamResult(out); + TransformerFactory.newDefaultInstance().newTransformer().transform(source, result); + } catch (IOException | TransformerException e) { + throw new RuntimeException("Cannot write to " + path, e); + } + }); + return (CompletableFuture) res; + } else { + return super.write(clss); } - return super.write(clss); } } -- 2.30.2