+ /*
+ * READ / WRITE
+ */
+ @SuppressWarnings("unchecked")
+ public <A> CompletableFuture<A> write(Class<A> clss) {
+ if (isContentClass(DName.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);
+ }
+ }