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
.ProvidedSession
;
21 import org
.argeo
.cms
.acr
.CmsContentRepository
;
22 import org
.w3c
.dom
.Document
;
23 import org
.w3c
.dom
.Element
;
24 import org
.w3c
.dom
.NodeList
;
26 public class DomContentProvider
implements ContentProvider
, NamespaceContext
{
27 private final Document document
;
30 // TODO centralise in some executor?
31 private final ThreadLocal
<XPath
> xPath
;
33 private String mountPath
;
35 public DomContentProvider(String mountPath
, Document document
) {
36 this.mountPath
= mountPath
;
37 this.document
= document
;
38 this.document
.normalizeDocument();
39 XPathFactory xPathFactory
= XPathFactory
.newInstance();
40 xPath
= new ThreadLocal
<>() {
43 protected XPath
initialValue() {
44 // TODO set the document as namespace context?
45 XPath res
= xPathFactory
.newXPath();
46 res
.setNamespaceContext(DomContentProvider
.this);
53 // public Content get() {
54 // return new DomContent(this, document.getDocumentElement());
57 // public Element createElement(String name) {
58 // return document.createElementNS(null, name);
63 public Content
get(ProvidedSession session
, String mountPath
, String relativePath
) {
64 if ("".equals(relativePath
))
65 return new DomContent(session
, this, document
.getDocumentElement());
66 if (relativePath
.startsWith("/"))
67 throw new IllegalArgumentException("Relative path cannot start with /");
69 String xPathExpression
= '/' + relativePath
;
70 if ("/".equals(mountPath
))
71 xPathExpression
= "/cr:root" + xPathExpression
;
73 NodeList nodes
= (NodeList
) xPath
.get().evaluate(xPathExpression
, document
, XPathConstants
.NODESET
);
74 if (nodes
.getLength() > 1)
75 throw new IllegalArgumentException(
76 "Multiple content found for " + relativePath
+ " under " + mountPath
);
77 if (nodes
.getLength() == 0)
78 throw new ContentNotFoundException("Path " + relativePath
+ " under " + mountPath
+ " was not found");
79 Element element
= (Element
) nodes
.item(0);
80 return new DomContent(session
, this, element
);
81 } catch (XPathExpressionException e
) {
82 throw new IllegalArgumentException("XPath expression " + xPathExpression
+ " cannot be evaluated", e
);
86 public void persist(ProvidedSession session
) {
87 if (mountPath
!= null) {
88 Content mountPoint
= session
.getMountPoint(mountPath
);
89 try (OutputStream out
= mountPoint
.open(OutputStream
.class)) {
90 CmsContentRepository contentRepository
= (CmsContentRepository
) session
.getRepository();
91 contentRepository
.writeDom(document
, out
);
92 } catch (IOException e
) {
93 throw new IllegalStateException("Cannot persist " + mountPath
, e
);
99 public String
getMountPath() {
107 public String
getNamespaceURI(String prefix
) {
108 String namespaceURI
= NamespaceUtils
.getStandardNamespaceURI(prefix
);
109 if (namespaceURI
!= null)
111 return document
.lookupNamespaceURI(prefix
);
115 public String
getPrefix(String namespaceURI
) {
116 String prefix
= NamespaceUtils
.getStandardPrefix(namespaceURI
);
119 return document
.lookupPrefix(namespaceURI
);
123 public Iterator
<String
> getPrefixes(String namespaceURI
) {
124 List
<String
> res
= new ArrayList
<>();
125 res
.add(getPrefix(namespaceURI
));
126 return Collections
.unmodifiableList(res
).iterator();