X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2Facr%2Fxml%2FDomContentProvider.java;h=d6e246df53fb9282c9171cc8e2fd977b12a07b4b;hb=f3ea14abccc33b1c3326417a87c91145be776c72;hp=c5fde8d7c521482c26b5a81932d223744026e9c4;hpb=7d2a002f5dcfe8a8c7b29803b70d4b1aff265ed1;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms/src/org/argeo/cms/acr/xml/DomContentProvider.java b/org.argeo.cms/src/org/argeo/cms/acr/xml/DomContentProvider.java index c5fde8d7c..d6e246df5 100644 --- a/org.argeo.cms/src/org/argeo/cms/acr/xml/DomContentProvider.java +++ b/org.argeo.cms/src/org/argeo/cms/acr/xml/DomContentProvider.java @@ -1,5 +1,7 @@ package org.argeo.cms.acr.xml; +import java.io.IOException; +import java.io.OutputStream; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; @@ -13,20 +15,27 @@ import javax.xml.xpath.XPathFactory; import org.argeo.api.acr.Content; import org.argeo.api.acr.ContentNotFoundException; +import org.argeo.api.acr.CrName; +import org.argeo.api.acr.NamespaceUtils; import org.argeo.api.acr.spi.ContentProvider; +import org.argeo.api.acr.spi.ProvidedContent; import org.argeo.api.acr.spi.ProvidedSession; +import org.argeo.cms.acr.CmsContentRepository; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; public class DomContentProvider implements ContentProvider, NamespaceContext { - private Document document; + private final Document document; // XPath // TODO centralise in some executor? private final ThreadLocal xPath; - public DomContentProvider(Document document) { + private String mountPath; + + public DomContentProvider(String mountPath, Document document) { + this.mountPath = mountPath; this.document = document; this.document.normalizeDocument(); XPathFactory xPathFactory = XPathFactory.newInstance(); @@ -35,7 +44,7 @@ public class DomContentProvider implements ContentProvider, NamespaceContext { @Override protected XPath initialValue() { // TODO set the document as namespace context? - XPath res= xPathFactory.newXPath(); + XPath res = xPathFactory.newXPath(); res.setNamespaceContext(DomContentProvider.this); return res; } @@ -53,27 +62,61 @@ public class DomContentProvider implements ContentProvider, NamespaceContext { // } @Override - public Content get(ProvidedSession session, String mountPath, String relativePath) { + public ProvidedContent get(ProvidedSession session, String relativePath) { if ("".equals(relativePath)) return new DomContent(session, this, document.getDocumentElement()); + + NodeList nodes = findContent(relativePath); + if (nodes.getLength() > 1) + throw new IllegalArgumentException("Multiple content found for " + relativePath + " under " + mountPath); + if (nodes.getLength() == 0) + throw new ContentNotFoundException("Path " + relativePath + " under " + mountPath + " was not found"); + Element element = (Element) nodes.item(0); + return new DomContent(session, this, element); + } + + protected NodeList findContent(String relativePath) { if (relativePath.startsWith("/")) throw new IllegalArgumentException("Relative path cannot start with /"); - String xPathExpression = '/' + relativePath; if ("/".equals(mountPath)) - xPathExpression = "/cr:root" + xPathExpression; + xPathExpression = "/" + CrName.root.qName() + xPathExpression; try { NodeList nodes = (NodeList) xPath.get().evaluate(xPathExpression, document, XPathConstants.NODESET); - if (nodes.getLength() > 1) - throw new IllegalArgumentException( - "Multiple content found for " + relativePath + " under " + mountPath); - if (nodes.getLength() == 0) - throw new ContentNotFoundException("Path " + relativePath + " under " + mountPath + " was not found"); - Element element = (Element) nodes.item(0); - return new DomContent(session, this, element); + return nodes; } catch (XPathExpressionException e) { throw new IllegalArgumentException("XPath expression " + xPathExpression + " cannot be evaluated", e); } + + } + + @Override + public boolean exists(ProvidedSession session, String relativePath) { + if ("".equals(relativePath)) + return true; + NodeList nodes = findContent(relativePath); + return nodes.getLength() != 0; + } + + public void persist(ProvidedSession session) { + if (mountPath != null) { + Content mountPoint = session.getMountPoint(mountPath); + try (OutputStream out = mountPoint.open(OutputStream.class)) { + CmsContentRepository contentRepository = (CmsContentRepository) session.getRepository(); + contentRepository.writeDom(document, out); + } catch (IOException e) { + throw new IllegalStateException("Cannot persist " + mountPath, e); + } + } + } + + @Override + public String getMountPath() { + return mountPath; + } + + public void registerPrefix(String prefix, String namespace) { + DomUtils.addNamespace(document.getDocumentElement(), prefix, namespace); } /* @@ -81,11 +124,17 @@ public class DomContentProvider implements ContentProvider, NamespaceContext { */ @Override public String getNamespaceURI(String prefix) { + String namespaceURI = NamespaceUtils.getStandardNamespaceURI(prefix); + if (namespaceURI != null) + return namespaceURI; return document.lookupNamespaceURI(prefix); } @Override public String getPrefix(String namespaceURI) { + String prefix = NamespaceUtils.getStandardPrefix(namespaceURI); + if (prefix != null) + return prefix; return document.lookupPrefix(namespaceURI); }