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
.transform
.TransformerFactory
;
12 import javax
.xml
.xpath
.XPath
;
13 import javax
.xml
.xpath
.XPathConstants
;
14 import javax
.xml
.xpath
.XPathExpressionException
;
15 import javax
.xml
.xpath
.XPathFactory
;
17 import org
.argeo
.api
.acr
.Content
;
18 import org
.argeo
.api
.acr
.ContentNotFoundException
;
19 import org
.argeo
.api
.acr
.CrName
;
20 import org
.argeo
.api
.acr
.NamespaceUtils
;
21 import org
.argeo
.api
.acr
.spi
.ContentProvider
;
22 import org
.argeo
.api
.acr
.spi
.ProvidedContent
;
23 import org
.argeo
.api
.acr
.spi
.ProvidedSession
;
24 import org
.argeo
.cms
.acr
.CmsContentRepository
;
25 import org
.argeo
.cms
.acr
.ContentUtils
;
26 import org
.w3c
.dom
.Document
;
27 import org
.w3c
.dom
.Element
;
28 import org
.w3c
.dom
.NodeList
;
30 public class DomContentProvider
implements ContentProvider
, NamespaceContext
{
31 private final Document document
;
34 // TODO centralise in some executor?
35 private final ThreadLocal
<XPath
> xPath
;
37 private TransformerFactory transformerFactory
;
39 private String mountPath
;
41 public DomContentProvider(String mountPath
, Document document
) {
42 this.mountPath
= mountPath
;
43 this.document
= document
;
44 this.document
.normalizeDocument();
46 transformerFactory
= TransformerFactory
.newInstance();
48 XPathFactory xPathFactory
= XPathFactory
.newInstance();
49 xPath
= new ThreadLocal
<>() {
52 protected XPath
initialValue() {
53 // TODO set the document as namespace context?
54 XPath res
= xPathFactory
.newXPath();
55 res
.setNamespaceContext(DomContentProvider
.this);
62 public ProvidedContent
get(ProvidedSession session
, String relativePath
) {
63 if ("".equals(relativePath
))
64 return new DomContent(session
, this, document
.getDocumentElement());
66 NodeList nodes
= findContent(relativePath
);
67 if (nodes
.getLength() > 1)
68 throw new IllegalArgumentException("Multiple content found for " + relativePath
+ " under " + mountPath
);
69 if (nodes
.getLength() == 0)
70 throw new ContentNotFoundException(session
, mountPath
+ "/" + relativePath
,
71 "Path " + relativePath
+ " under " + mountPath
+ " was not found");
72 Element element
= (Element
) nodes
.item(0);
73 return new DomContent(session
, this, element
);
76 protected NodeList
findContent(String relativePath
) {
77 if (relativePath
.startsWith("/"))
78 throw new IllegalArgumentException("Relative path cannot start with /");
79 String xPathExpression
= '/' + relativePath
;
80 if (ContentUtils
.SLASH_STRING
.equals(mountPath
)) // repository root
81 xPathExpression
= "/" + CrName
.root
.qName() + xPathExpression
;
83 NodeList nodes
= (NodeList
) xPath
.get().evaluate(xPathExpression
, document
, XPathConstants
.NODESET
);
85 } catch (XPathExpressionException e
) {
86 throw new IllegalArgumentException("XPath expression " + xPathExpression
+ " cannot be evaluated", e
);
92 public boolean exists(ProvidedSession session
, String relativePath
) {
93 if ("".equals(relativePath
))
95 NodeList nodes
= findContent(relativePath
);
96 return nodes
.getLength() != 0;
100 public void persist(ProvidedSession session
) {
101 if (mountPath
!= null) {
102 Content mountPoint
= session
.getMountPoint(mountPath
);
103 try (OutputStream out
= mountPoint
.open(OutputStream
.class)) {
104 CmsContentRepository contentRepository
= (CmsContentRepository
) session
.getRepository();
105 contentRepository
.writeDom(document
, out
);
106 } catch (IOException e
) {
107 throw new IllegalStateException("Cannot persist " + mountPath
, e
);
113 public String
getMountPath() {
117 public void registerPrefix(String prefix
, String namespace
) {
118 DomUtils
.addNamespace(document
.getDocumentElement(), prefix
, namespace
);
125 public String
getNamespaceURI(String prefix
) {
126 String namespaceURI
= NamespaceUtils
.getStandardNamespaceURI(prefix
);
127 if (namespaceURI
!= null)
129 return document
.lookupNamespaceURI(prefix
);
133 public String
getPrefix(String namespaceURI
) {
134 String prefix
= NamespaceUtils
.getStandardPrefix(namespaceURI
);
137 return document
.lookupPrefix(namespaceURI
);
141 public Iterator
<String
> getPrefixes(String namespaceURI
) {
142 List
<String
> res
= new ArrayList
<>();
143 res
.add(getPrefix(namespaceURI
));
144 return Collections
.unmodifiableList(res
).iterator();
147 TransformerFactory
getTransformerFactory() {
148 return transformerFactory
;