X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2Facr%2Ffs%2FFsContent.java;h=d61827d500eb03e9cebc1755d6266072ebc6ed18;hb=975fb5e581d0650768afc68a0e839657f318e77a;hp=77c2f7a7585fe778834ec9735635b749905581d1;hpb=c0342975a37c70895c2e8f6b341d790700168d7f;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 77c2f7a75..d61827d50 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 @@ -21,9 +21,14 @@ import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; +import java.util.StringJoiner; 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; @@ -69,7 +74,7 @@ public class FsContent extends AbstractContent implements ProvidedContent { // TODO check file names with ':' ? if (isMountBase) { String mountPath = provider.getMountPath(); - if (mountPath != null && !mountPath.equals("/")) { + if (mountPath != null && !mountPath.equals(ContentUtils.ROOT_SLASH)) { Content mountPoint = session.getMountPoint(mountPath); this.name = mountPoint.getName(); } else { @@ -78,9 +83,8 @@ public class FsContent extends AbstractContent implements ProvidedContent { } else { // TODO should we support prefixed name for known types? - // QName providerName = NamespaceUtils.parsePrefixedName(provider, - // path.getFileName().toString()); - QName providerName = new QName(path.getFileName().toString()); + QName providerName = NamespaceUtils.parsePrefixedName(provider, path.getFileName().toString()); +// QName providerName = new QName(path.getFileName().toString()); // TODO remove extension if mounted? this.name = new ContentName(providerName, session); } @@ -143,7 +147,17 @@ public class FsContent extends AbstractContent implements ProvidedContent { // TODO perform trivial file conversion to other formats } if (value instanceof byte[]) { - res = (A) new String((byte[]) value, StandardCharsets.UTF_8); + String str = new String((byte[]) value, StandardCharsets.UTF_8); + String[] arr = str.split("\n"); + if (arr.length == 1) { + res = (A) arr[0]; + } else { + List lst = new ArrayList<>(); + for (String s : arr) { + lst.add(s); + } + res = (A) lst; + } } if (res == null) try { @@ -185,8 +199,20 @@ public class FsContent extends AbstractContent implements ProvidedContent { @Override public Object put(QName key, Object value) { Object previous = get(key); + + String toWrite; + if (value instanceof List) { + StringJoiner sj = new StringJoiner("\n"); + for (Object obj : (List) value) { + sj.add(obj.toString()); + } + toWrite = sj.toString(); + } else { + toWrite = value.toString(); + } + UserDefinedFileAttributeView udfav = Files.getFileAttributeView(path, UserDefinedFileAttributeView.class); - ByteBuffer bb = ByteBuffer.wrap(value.toString().getBytes(StandardCharsets.UTF_8)); + ByteBuffer bb = ByteBuffer.wrap(toWrite.getBytes(StandardCharsets.UTF_8)); try { udfav.write(NamespaceUtils.toPrefixedName(provider, key), bb); } catch (IOException e) { @@ -248,6 +274,8 @@ public class FsContent extends AbstractContent implements ProvidedContent { throw new ContentResourceException("Cannot create new content", e); } + if (classes.length > 0) + fsContent.addContentClasses(classes); if (getSession().getRepository().shouldMount(classes)) { ContentProvider contentProvider = getSession().getRepository().getMountContentProvider(fsContent, true, classes); @@ -309,12 +337,32 @@ public class FsContent extends AbstractContent implements ProvidedContent { @Override public List getContentClasses() { List res = new ArrayList<>(); + Optional> value = getMultiple(CrName.cc.qName(), String.class); + if (!value.isEmpty()) { + for (String s : value.get()) { + QName name = NamespaceUtils.parsePrefixedName(provider, s); + res.add(name); + } + } if (Files.isDirectory(path)) res.add(CrName.collection.qName()); - // TODO add other types return res; } + @Override + public void addContentClasses(QName... contentClass) { + List toWrite = new ArrayList<>(); + for (QName cc : getContentClasses()) { + if (cc.equals(CrName.collection.qName())) + continue; // skip + toWrite.add(NamespaceUtils.toPrefixedName(provider, cc)); + } + for (QName cc : contentClass) { + toWrite.add(NamespaceUtils.toPrefixedName(provider, cc)); + } + put(CrName.cc.qName(), toWrite); + } + /* * ACCESSORS */ @@ -327,6 +375,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 +390,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); } }