]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.cms/src/org/argeo/cms/acr/fs/FsContent.java
Support writing file as XML
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / acr / fs / FsContent.java
index 15917c50359a003affc0b2d853e444a483526019..55ef6ec46d89e7f4dbc01ef0b6ac2352c8901cf9 100644 (file)
@@ -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;
@@ -56,16 +62,16 @@ public class FsContent extends AbstractContent implements ProvidedContent {
 
        private final FsContentProvider provider;
        private final Path path;
-       private final boolean isRoot;
+       private final boolean isMountBase;
        private final QName name;
 
        protected FsContent(ProvidedSession session, FsContentProvider contentProvider, Path path) {
                super(session);
                this.provider = contentProvider;
                this.path = path;
-               this.isRoot = contentProvider.isMountRoot(path);
+               this.isMountBase = contentProvider.isMountBase(path);
                // TODO check file names with ':' ?
-               if (isRoot) {
+               if (isMountBase) {
                        String mountPath = provider.getMountPath();
                        if (mountPath != null && !mountPath.equals("/")) {
                                Content mountPoint = session.getMountPoint(mountPath);
@@ -265,7 +271,7 @@ public class FsContent extends AbstractContent implements ProvidedContent {
 
        @Override
        public Content getParent() {
-               if (isRoot) {
+               if (isMountBase) {
                        String mountPath = provider.getMountPath();
                        if (mountPath == null || mountPath.equals("/"))
                                return null;
@@ -322,4 +328,39 @@ public class FsContent extends AbstractContent implements ProvidedContent {
                return provider;
        }
 
+       /*
+        * READ / WRITE
+        */
+       @SuppressWarnings("unchecked")
+       public <A> CompletableFuture<A> write(Class<A> clss) {
+               if (isContentClass(CrName.collection.qName())) {
+                       throw new IllegalStateException("Cannot directly write to a collection");
+               }
+               if (InputStream.class.isAssignableFrom(clss)) {
+                       CompletableFuture<InputStream> 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<A>) res;
+               } else if (Source.class.isAssignableFrom(clss)) {
+                       CompletableFuture<Source> res = new CompletableFuture<Source>();
+                       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<A>) res;
+               } else {
+                       return super.write(clss);
+               }
+       }
 }