]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.cms/src/org/argeo/cms/acr/xml/DomContentProvider.java
Improve ACR.
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / acr / xml / DomContentProvider.java
index 8caac1ad29a2629e11e09b7c72dc2857ead361f2..54013e2ad1301deb66235671fcce278e6be64b58 100644 (file)
@@ -17,6 +17,7 @@ import org.argeo.api.acr.Content;
 import org.argeo.api.acr.ContentNotFoundException;
 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;
@@ -60,27 +61,40 @@ 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;
                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) {
@@ -100,6 +114,10 @@ public class DomContentProvider implements ContentProvider, NamespaceContext {
                return mountPath;
        }
 
+       public void registerPrefix(String prefix, String namespace) {
+               DomUtils.addNamespace(document.getDocumentElement(), prefix, namespace);
+       }
+
        /*
         * NAMESPACE CONTEXT
         */