]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/acr/xml/DomContentProvider.java
Support writing file as XML
[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.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 Content get() {
62 // return new DomContent(this, document.getDocumentElement());
63 // }
64
65 // public Element createElement(String name) {
66 // return document.createElementNS(null, name);
67 //
68 // }
69
70 @Override
71 public ProvidedContent get(ProvidedSession session, String relativePath) {
72 if ("".equals(relativePath))
73 return new DomContent(session, this, document.getDocumentElement());
74
75 NodeList nodes = findContent(relativePath);
76 if (nodes.getLength() > 1)
77 throw new IllegalArgumentException("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 }
83
84 protected NodeList findContent(String relativePath) {
85 if (relativePath.startsWith("/"))
86 throw new IllegalArgumentException("Relative path cannot start with /");
87 String xPathExpression = '/' + relativePath;
88 if ("/".equals(mountPath))
89 xPathExpression = "/" + CrName.root.qName() + xPathExpression;
90 try {
91 NodeList nodes = (NodeList) xPath.get().evaluate(xPathExpression, document, XPathConstants.NODESET);
92 return nodes;
93 } catch (XPathExpressionException e) {
94 throw new IllegalArgumentException("XPath expression " + xPathExpression + " cannot be evaluated", e);
95 }
96
97 }
98
99 @Override
100 public boolean exists(ProvidedSession session, String relativePath) {
101 if ("".equals(relativePath))
102 return true;
103 NodeList nodes = findContent(relativePath);
104 return nodes.getLength() != 0;
105 }
106
107 public void persist(ProvidedSession session) {
108 if (mountPath != null) {
109 Content mountPoint = session.getMountPoint(mountPath);
110 try (OutputStream out = mountPoint.open(OutputStream.class)) {
111 CmsContentRepository contentRepository = (CmsContentRepository) session.getRepository();
112 contentRepository.writeDom(document, out);
113 } catch (IOException e) {
114 throw new IllegalStateException("Cannot persist " + mountPath, e);
115 }
116 }
117 }
118
119 @Override
120 public String getMountPath() {
121 return mountPath;
122 }
123
124 public void registerPrefix(String prefix, String namespace) {
125 DomUtils.addNamespace(document.getDocumentElement(), prefix, namespace);
126 }
127
128 /*
129 * NAMESPACE CONTEXT
130 */
131 @Override
132 public String getNamespaceURI(String prefix) {
133 String namespaceURI = NamespaceUtils.getStandardNamespaceURI(prefix);
134 if (namespaceURI != null)
135 return namespaceURI;
136 return document.lookupNamespaceURI(prefix);
137 }
138
139 @Override
140 public String getPrefix(String namespaceURI) {
141 String prefix = NamespaceUtils.getStandardPrefix(namespaceURI);
142 if (prefix != null)
143 return prefix;
144 return document.lookupPrefix(namespaceURI);
145 }
146
147 @Override
148 public Iterator<String> getPrefixes(String namespaceURI) {
149 List<String> res = new ArrayList<>();
150 res.add(getPrefix(namespaceURI));
151 return Collections.unmodifiableList(res).iterator();
152 }
153
154 TransformerFactory getTransformerFactory() {
155 return transformerFactory;
156 }
157
158 }