]> git.argeo.org Git - lgpl/argeo-commons.git/blob - cms/acr/xml/DomContentProvider.java
Prepare next development cycle
[lgpl/argeo-commons.git] / cms / acr / xml / DomContentProvider.java
1 package org.argeo.cms.acr.xml;
2
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;
8 import java.util.List;
9
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;
16
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.w3c.dom.Document;
26 import org.w3c.dom.Element;
27 import org.w3c.dom.NodeList;
28
29 public class DomContentProvider implements ContentProvider, NamespaceContext {
30 private final Document document;
31
32 // XPath
33 // TODO centralise in some executor?
34 private final ThreadLocal<XPath> xPath;
35
36 private TransformerFactory transformerFactory;
37
38 private String mountPath;
39
40 public DomContentProvider(String mountPath, Document document) {
41 this.mountPath = mountPath;
42 this.document = document;
43 this.document.normalizeDocument();
44
45 transformerFactory = TransformerFactory.newInstance();
46
47 XPathFactory xPathFactory = XPathFactory.newInstance();
48 xPath = new ThreadLocal<>() {
49
50 @Override
51 protected XPath initialValue() {
52 // TODO set the document as namespace context?
53 XPath res = xPathFactory.newXPath();
54 res.setNamespaceContext(DomContentProvider.this);
55 return res;
56 }
57 };
58 }
59
60 @Override
61 public ProvidedContent get(ProvidedSession session, String relativePath) {
62 if ("".equals(relativePath))
63 return new DomContent(session, this, document.getDocumentElement());
64
65 NodeList nodes = findContent(relativePath);
66 if (nodes.getLength() > 1)
67 throw new IllegalArgumentException("Multiple content found for " + relativePath + " under " + mountPath);
68 if (nodes.getLength() == 0)
69 throw new ContentNotFoundException(session, mountPath + "/" + relativePath,
70 "Path " + relativePath + " under " + mountPath + " was not found");
71 Element element = (Element) nodes.item(0);
72 return new DomContent(session, this, element);
73 }
74
75 protected NodeList findContent(String relativePath) {
76 if (relativePath.startsWith("/"))
77 throw new IllegalArgumentException("Relative path cannot start with /");
78 String xPathExpression;
79 if (Content.ROOT_PATH.equals(mountPath)) {// repository root
80 xPathExpression = "/" + CrName.root.get() + '/' + relativePath;
81 } else {
82 String documentNodeName = document.getDocumentElement().getNodeName();
83 xPathExpression = '/' + documentNodeName + '/' + relativePath;
84 }
85 try {
86 NodeList nodes = (NodeList) xPath.get().evaluate(xPathExpression, document, XPathConstants.NODESET);
87 return nodes;
88 } catch (XPathExpressionException e) {
89 throw new IllegalArgumentException("XPath expression " + xPathExpression + " cannot be evaluated", e);
90 }
91
92 }
93
94 @Override
95 public boolean exists(ProvidedSession session, String relativePath) {
96 if ("".equals(relativePath))
97 return true;
98 NodeList nodes = findContent(relativePath);
99 return nodes.getLength() != 0;
100 }
101
102 @Override
103 public void persist(ProvidedSession session) {
104 if (mountPath != null) {
105 Content mountPoint = session.getMountPoint(mountPath);
106 try (OutputStream out = mountPoint.open(OutputStream.class)) {
107 CmsContentRepository contentRepository = (CmsContentRepository) session.getRepository();
108 contentRepository.writeDom(document, out);
109 } catch (IOException e) {
110 throw new IllegalStateException("Cannot persist " + mountPath, e);
111 }
112 }
113 }
114
115 @Override
116 public String getMountPath() {
117 return mountPath;
118 }
119
120 public void registerPrefix(String prefix, String namespace) {
121 DomUtils.addNamespace(document.getDocumentElement(), prefix, namespace);
122 }
123
124 /*
125 * NAMESPACE CONTEXT
126 */
127 @Override
128 public String getNamespaceURI(String prefix) {
129 String namespaceURI = NamespaceUtils.getStandardNamespaceURI(prefix);
130 if (namespaceURI != null)
131 return namespaceURI;
132 return document.lookupNamespaceURI(prefix);
133 }
134
135 @Override
136 public String getPrefix(String namespaceURI) {
137 String prefix = NamespaceUtils.getStandardPrefix(namespaceURI);
138 if (prefix != null)
139 return prefix;
140 return document.lookupPrefix(namespaceURI);
141 }
142
143 @Override
144 public Iterator<String> getPrefixes(String namespaceURI) {
145 List<String> res = new ArrayList<>();
146 res.add(getPrefix(namespaceURI));
147 return Collections.unmodifiableList(res).iterator();
148 }
149
150 TransformerFactory getTransformerFactory() {
151 return transformerFactory;
152 }
153
154 }