X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;ds=inline;f=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2Facr%2Ffs%2FFsContent.java;h=55ef6ec46d89e7f4dbc01ef0b6ac2352c8901cf9;hb=904d7789c0e636c3b2e6a55fc6ec9ee333cc2023;hp=76dc9da71d65bf95053679e2d99cf6e969623f69;hpb=00fec502abb8eeccb813fe33d5e9ba53436b44d6;p=lgpl%2Fargeo-commons.git
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 76dc9da71..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
@@ -8,6 +8,7 @@ import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
+import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.FileTime;
import java.nio.file.attribute.UserDefinedFileAttributeView;
import java.time.Instant;
@@ -20,8 +21,13 @@ import java.util.List;
import java.util.Map;
import java.util.Optional;
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;
@@ -322,4 +328,39 @@ public class FsContent extends AbstractContent implements ProvidedContent {
return provider;
}
+ /*
+ * READ / WRITE
+ */
+ @SuppressWarnings("unchecked")
+ public CompletableFuture write(Class clss) {
+ if (isContentClass(CrName.collection.qName())) {
+ throw new IllegalStateException("Cannot directly write to a collection");
+ }
+ if (InputStream.class.isAssignableFrom(clss)) {
+ CompletableFuture res = new CompletableFuture<>();
+ res.thenAccept((in) -> {
+ try {
+ Files.copy(in, path, StandardCopyOption.REPLACE_EXISTING);
+ } catch (IOException e) {
+ throw new RuntimeException("Cannot write to " + path, e);
+ }
+ });
+ 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);
+ }
+ }
}