X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;ds=sidebyside;f=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2Facr%2Fxml%2FDomContent.java;h=0686be7cb59b2cf63d034fc8331a1d1884e39f08;hb=f5c293792ad697475d29073069c05063d7218c0b;hp=786b0d63261f2b298cde63194176f9a3e7c629af;hpb=975fb5e581d0650768afc68a0e839657f318e77a;p=lgpl%2Fargeo-commons.git
diff --git a/org.argeo.cms/src/org/argeo/cms/acr/xml/DomContent.java b/org.argeo.cms/src/org/argeo/cms/acr/xml/DomContent.java
index 786b0d632..0686be7cb 100644
--- a/org.argeo.cms/src/org/argeo/cms/acr/xml/DomContent.java
+++ b/org.argeo.cms/src/org/argeo/cms/acr/xml/DomContent.java
@@ -1,5 +1,10 @@
package org.argeo.cms.acr.xml;
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PipedInputStream;
+import java.io.PipedOutputStream;
import java.nio.CharBuffer;
import java.util.ArrayList;
import java.util.HashSet;
@@ -9,18 +14,22 @@ import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ForkJoinPool;
import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;
import javax.xml.namespace.QName;
+import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
import org.argeo.api.acr.Content;
import org.argeo.api.acr.ContentName;
+import org.argeo.api.acr.CrAttributeType;
import org.argeo.api.acr.CrName;
import org.argeo.api.acr.spi.ProvidedContent;
import org.argeo.api.acr.spi.ProvidedSession;
@@ -60,7 +69,7 @@ public class DomContent extends AbstractContent implements ProvidedContent {
if (isLocalRoot()) {// root
String mountPath = provider.getMountPath();
if (mountPath != null) {
- if (ContentUtils.ROOT_SLASH.equals(mountPath)) {
+ if (Content.ROOT_PATH.equals(mountPath)) {
return CrName.root.qName();
}
Content mountPoint = getSession().getMountPoint(mountPath);
@@ -122,24 +131,25 @@ public class DomContent extends AbstractContent implements ProvidedContent {
for (int i = 0; i < attributes.getLength(); i++) {
Attr attr = (Attr) attributes.item(i);
QName key = toQName(attr);
+ if (key.getNamespaceURI().equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI))
+ continue;// skip prefix mapping
result.add(key);
}
return result;
}
- @SuppressWarnings("unchecked")
+// @SuppressWarnings("unchecked")
@Override
public Optional get(QName key, Class clss) {
String namespaceUriOrNull = XMLConstants.NULL_NS_URI.equals(key.getNamespaceURI()) ? null
: key.getNamespaceURI();
if (element.hasAttributeNS(namespaceUriOrNull, key.getLocalPart())) {
String value = element.getAttributeNS(namespaceUriOrNull, key.getLocalPart());
- if (clss.isAssignableFrom(String.class))
- return Optional.of((A) value);
- else
- return Optional.empty();
+// if (isDefaultAttrTypeRequested(clss))
+// return Optional.of((A) CrAttributeType.parse(value));
+ return CrAttributeType.cast(clss, value);
} else
- return null;
+ return Optional.empty();
}
@Override
@@ -226,7 +236,7 @@ public class DomContent extends AbstractContent implements ProvidedContent {
String mountPath = provider.getMountPath();
if (mountPath == null)
return null;
- if (ContentUtils.ROOT_SLASH.equals(mountPath)) {
+ if (Content.ROOT_PATH.equals(mountPath)) {
return null;
}
String[] parent = ContentUtils.getParentPath(mountPath);
@@ -331,6 +341,27 @@ public class DomContent extends AbstractContent implements ProvidedContent {
return super.write(clss);
}
+ @SuppressWarnings("unchecked")
+ @Override
+ public C open(Class clss) throws IOException, IllegalArgumentException {
+ if (InputStream.class.isAssignableFrom(clss)) {
+ PipedOutputStream out = new PipedOutputStream();
+ ForkJoinPool.commonPool().execute(() -> {
+ try {
+ Source source = new DOMSource(element);
+ Result result = new StreamResult(out);
+ provider.getTransformerFactory().newTransformer().transform(source, result);
+ out.flush();
+ out.close();
+ } catch (TransformerException | IOException e) {
+ throw new RuntimeException("Cannot read " + getPath(), e);
+ }
+ });
+ return (C) new PipedInputStream(out);
+ }
+ return super.open(clss);
+ }
+
@Override
public int getSiblingIndex() {
Node curr = element.getPreviousSibling();
@@ -355,7 +386,9 @@ public class DomContent extends AbstractContent implements ProvidedContent {
List res = new ArrayList<>();
if (isLocalRoot()) {
String mountPath = provider.getMountPath();
- if (mountPath != null) {
+ if (Content.ROOT_PATH.equals(mountPath)) {// repository root
+ res.add(CrName.root.qName());
+ } else {
Content mountPoint = getSession().getMountPoint(mountPath);
res.addAll(mountPoint.getContentClasses());
}
@@ -369,7 +402,9 @@ public class DomContent extends AbstractContent implements ProvidedContent {
public void addContentClasses(QName... contentClass) {
if (isLocalRoot()) {
String mountPath = provider.getMountPath();
- if (mountPath != null) {
+ if (Content.ROOT_PATH.equals(mountPath)) {// repository root
+ throw new IllegalArgumentException("Cannot add content classes to repository root");
+ } else {
Content mountPoint = getSession().getMountPoint(mountPath);
mountPoint.addContentClasses(contentClass);
}