]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/acr/xml/DomContentProvider.java
Rename transaction package
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / 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.xpath.XPath;
12 import javax.xml.xpath.XPathConstants;
13 import javax.xml.xpath.XPathExpressionException;
14 import javax.xml.xpath.XPathFactory;
15
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;
26
27 public class DomContentProvider implements ContentProvider, NamespaceContext {
28 private final Document document;
29
30 // XPath
31 // TODO centralise in some executor?
32 private final ThreadLocal<XPath> xPath;
33
34 private String mountPath;
35
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<>() {
42
43 @Override
44 protected XPath initialValue() {
45 // TODO set the document as namespace context?
46 XPath res = xPathFactory.newXPath();
47 res.setNamespaceContext(DomContentProvider.this);
48 return res;
49 }
50 };
51 }
52
53 // @Override
54 // public Content get() {
55 // return new DomContent(this, document.getDocumentElement());
56 // }
57
58 // public Element createElement(String name) {
59 // return document.createElementNS(null, name);
60 //
61 // }
62
63 @Override
64 public ProvidedContent get(ProvidedSession session, String relativePath) {
65 if ("".equals(relativePath))
66 return new DomContent(session, this, document.getDocumentElement());
67
68 NodeList nodes = findContent(relativePath);
69 if (nodes.getLength() > 1)
70 throw new IllegalArgumentException("Multiple content found for " + relativePath + " under " + mountPath);
71 if (nodes.getLength() == 0)
72 throw new ContentNotFoundException("Path " + relativePath + " under " + mountPath + " was not found");
73 Element element = (Element) nodes.item(0);
74 return new DomContent(session, this, element);
75 }
76
77 protected NodeList findContent(String relativePath) {
78 if (relativePath.startsWith("/"))
79 throw new IllegalArgumentException("Relative path cannot start with /");
80 String xPathExpression = '/' + relativePath;
81 if ("/".equals(mountPath))
82 xPathExpression = "/cr:root" + xPathExpression;
83 try {
84 NodeList nodes = (NodeList) xPath.get().evaluate(xPathExpression, document, XPathConstants.NODESET);
85 return nodes;
86 } catch (XPathExpressionException e) {
87 throw new IllegalArgumentException("XPath expression " + xPathExpression + " cannot be evaluated", e);
88 }
89
90 }
91
92 @Override
93 public boolean exists(ProvidedSession session, String relativePath) {
94 if ("".equals(relativePath))
95 return true;
96 NodeList nodes = findContent(relativePath);
97 return nodes.getLength() != 0;
98 }
99
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);
108 }
109 }
110 }
111
112 @Override
113 public String getMountPath() {
114 return mountPath;
115 }
116
117 public void registerPrefix(String prefix, String namespace) {
118 DomUtils.addNamespace(document.getDocumentElement(), prefix, namespace);
119 }
120
121 /*
122 * NAMESPACE CONTEXT
123 */
124 @Override
125 public String getNamespaceURI(String prefix) {
126 String namespaceURI = NamespaceUtils.getStandardNamespaceURI(prefix);
127 if (namespaceURI != null)
128 return namespaceURI;
129 return document.lookupNamespaceURI(prefix);
130 }
131
132 @Override
133 public String getPrefix(String namespaceURI) {
134 String prefix = NamespaceUtils.getStandardPrefix(namespaceURI);
135 if (prefix != null)
136 return prefix;
137 return document.lookupPrefix(namespaceURI);
138 }
139
140 @Override
141 public Iterator<String> getPrefixes(String namespaceURI) {
142 List<String> res = new ArrayList<>();
143 res.add(getPrefix(namespaceURI));
144 return Collections.unmodifiableList(res).iterator();
145 }
146
147 }