1 package org
.argeo
.cms
.acr
.xml
;
3 import java
.io
.IOException
;
4 import java
.io
.OutputStream
;
5 import java
.util
.ArrayList
;
6 import java
.util
.Collections
;
7 import java
.util
.Iterator
;
10 import javax
.xml
.namespace
.NamespaceContext
;
11 import javax
.xml
.xpath
.XPath
;
12 import javax
.xml
.xpath
.XPathConstants
;
13 import javax
.xml
.xpath
.XPathExpressionException
;
14 import javax
.xml
.xpath
.XPathFactory
;
16 import org
.argeo
.api
.acr
.Content
;
17 import org
.argeo
.api
.acr
.ContentNotFoundException
;
18 import org
.argeo
.api
.acr
.NamespaceUtils
;
19 import org
.argeo
.api
.acr
.spi
.ContentProvider
;
20 import org
.argeo
.api
.acr
.spi
.ProvidedContent
;
21 import org
.argeo
.api
.acr
.spi
.ProvidedSession
;
22 import org
.argeo
.cms
.acr
.CmsContentRepository
;
23 import org
.w3c
.dom
.Document
;
24 import org
.w3c
.dom
.Element
;
25 import org
.w3c
.dom
.NodeList
;
27 public class DomContentProvider
implements ContentProvider
, NamespaceContext
{
28 private final Document document
;
31 // TODO centralise in some executor?
32 private final ThreadLocal
<XPath
> xPath
;
34 private String mountPath
;
36 public DomContentProvider(String mountPath
, Document document
) {
37 this.mountPath
= mountPath
;
38 this.document
= document
;
39 this.document
.normalizeDocument();
40 XPathFactory xPathFactory
= XPathFactory
.newInstance();
41 xPath
= new ThreadLocal
<>() {
44 protected XPath
initialValue() {
45 // TODO set the document as namespace context?
46 XPath res
= xPathFactory
.newXPath();
47 res
.setNamespaceContext(DomContentProvider
.this);
54 // public Content get() {
55 // return new DomContent(this, document.getDocumentElement());
58 // public Element createElement(String name) {
59 // return document.createElementNS(null, name);
64 public ProvidedContent
get(ProvidedSession session
, String mountPath
, String relativePath
) {
65 if ("".equals(relativePath
))
66 return new DomContent(session
, this, document
.getDocumentElement());
67 if (relativePath
.startsWith("/"))
68 throw new IllegalArgumentException("Relative path cannot start with /");
70 String xPathExpression
= '/' + relativePath
;
71 if ("/".equals(mountPath
))
72 xPathExpression
= "/cr:root" + xPathExpression
;
74 NodeList nodes
= (NodeList
) xPath
.get().evaluate(xPathExpression
, document
, XPathConstants
.NODESET
);
75 if (nodes
.getLength() > 1)
76 throw new IllegalArgumentException(
77 "Multiple content found for " + relativePath
+ " under " + mountPath
);
78 if (nodes
.getLength() == 0)
79 throw new ContentNotFoundException("Path " + relativePath
+ " under " + mountPath
+ " was not found");
80 Element element
= (Element
) nodes
.item(0);
81 return new DomContent(session
, this, element
);
82 } catch (XPathExpressionException e
) {
83 throw new IllegalArgumentException("XPath expression " + xPathExpression
+ " cannot be evaluated", e
);
87 public void persist(ProvidedSession session
) {
88 if (mountPath
!= null) {
89 Content mountPoint
= session
.getMountPoint(mountPath
);
90 try (OutputStream out
= mountPoint
.open(OutputStream
.class)) {
91 CmsContentRepository contentRepository
= (CmsContentRepository
) session
.getRepository();
92 contentRepository
.writeDom(document
, out
);
93 } catch (IOException e
) {
94 throw new IllegalStateException("Cannot persist " + mountPath
, e
);
100 public String
getMountPath() {
104 public void registerPrefix(String prefix
, String namespace
) {
105 DomUtils
.addNamespace(document
.getDocumentElement(), prefix
, namespace
);
112 public String
getNamespaceURI(String prefix
) {
113 String namespaceURI
= NamespaceUtils
.getStandardNamespaceURI(prefix
);
114 if (namespaceURI
!= null)
116 return document
.lookupNamespaceURI(prefix
);
120 public String
getPrefix(String namespaceURI
) {
121 String prefix
= NamespaceUtils
.getStandardPrefix(namespaceURI
);
124 return document
.lookupPrefix(namespaceURI
);
128 public Iterator
<String
> getPrefixes(String namespaceURI
) {
129 List
<String
> res
= new ArrayList
<>();
130 res
.add(getPrefix(namespaceURI
));
131 return Collections
.unmodifiableList(res
).iterator();