1 package org.smartcomps.twister.common.persistence;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5 import org.apache.xindice.client.xmldb.services.CollectionManager;
6 import org.apache.xindice.util.XindiceException;
7 import org.apache.xindice.xml.dom.DOMParser;
8 import org.dom4j.Document;
9 import org.dom4j.DocumentException;
10 import org.dom4j.io.DOMReader;
11 import org.dom4j.io.DOMWriter;
12 import org.smartcomps.twister.common.configuration.CommonConfiguration;
13 import org.xmldb.api.DatabaseManager;
14 import org.xmldb.api.base.*;
15 import org.xmldb.api.modules.XMLResource;
16 import org.xmldb.api.modules.XPathQueryService;
17
18 import java.text.MessageFormat;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 /***
23 * Utility class containing main methods to create, update, delete or find XML
24 * document in Xindice. This class can be used directly or derived to
25 * implement more specific XMLDataAccess objects.<br><br>
26 * <b>Note : paths must start with the '/' character</b>
27 */
28 public class XMLDataAccess {
29
30 private static String TWISTER_ID = "twister-id";
31
32 private static Log log = LogFactory.getLog(XMLDataAccess.class);
33 private static DOMReader domReader = null;
34 private static String xindiceURL = null;
35
36 private static MessageFormat CREATE_COLL = new MessageFormat(
37 "<collection compressed=\"true\" name=\"{0}\">" +
38 " <filer class=\"org.apache.xindice.core.filer.BTreeFiler\" gzip=\"true\"/>" +
39 "</collection>");
40
41 public static Collection getRootCollection() throws XMLSessionException {
42 Collection result = null;
43 try {
44 result = DatabaseManager.getCollection(getXindiceURL());
45 } catch (XMLDBException e) {
46 throw new XMLSessionException("Could not find root collection at path " + getXindiceURL(), e);
47 }
48 return result;
49 }
50
51 public static Collection getCollection(String path) throws XMLSessionException {
52 if (!path.startsWith("/")) throw new IllegalArgumentException("A collection path must always start with '/'");
53 Collection result = null;
54 try {
55 result = DatabaseManager.getCollection(getXindiceURL() + path);
56 } catch (XMLDBException e) {
57 throw new XMLSessionException("Could not find collection at path " + getXindiceURL() + path, e);
58 }
59 return result;
60 }
61
62 public static Document getDocument(String collectionPath, String docId) throws XMLSessionException, FinderException {
63 Collection coll = getCollection(collectionPath);
64 if (coll == null) throw new FinderException("Could not find a collection at path " + collectionPath);
65 Document result = null;
66 try {
67 XMLResource doc = (XMLResource) coll.getResource(docId);
68 if (doc == null) throw new FinderException("Could not find any resource from id " + docId);
69 result = getDomReader().read((org.w3c.dom.Document)doc.getContentAsDOM());
70 } catch (XMLDBException e) {
71 throw new XMLSessionException(e);
72 }
73 return result;
74 }
75
76 public static Document getDocument(Collection coll, String docId) throws XMLSessionException, FinderException {
77 Document result = null;
78 try {
79 XMLResource doc = (XMLResource) coll.getResource(docId);
80 if (doc == null) throw new FinderException("Could not find any resource from id " + docId);
81 result = getDomReader().read((org.w3c.dom.Document)doc.getContentAsDOM());
82 } catch (XMLDBException e) {
83 throw new XMLSessionException(e);
84 }
85 return result;
86 }
87
88 public static String queryDocumentForValue(Collection parent, String docId, String query) throws XMLSessionException, FinderException {
89 String result = null;
90 try {
91 XPathQueryService queryService = (XPathQueryService) parent.getService("XPathQueryService", "1.0");
92 ResourceSet set = queryService.queryResource(docId, query);
93 if (set.getSize() == 0 || set.getSize() > 1) {
94 throw new FinderException("The path used in the queryDocumentForValue method should return a set " +
95 "containing only one value. Querying document " + docId + " with query " + query);
96 }
97 ResourceIterator setIter = set.getIterator();
98 XMLResource xmlRes = (XMLResource)setIter.nextResource();
99 // Xindice returns a document, one getFirstChild is needed to get the node
100 // we're insterested in and another one to get the node value.
101 result = xmlRes.getContentAsDOM().getFirstChild().getFirstChild().getNodeValue();
102 } catch (XMLDBException e) {
103 throw new XMLSessionException(e);
104 }
105 return result;
106 }
107
108 public static List queryDocument(Collection parent, String docId, String query) throws XMLSessionException, FinderException {
109 ArrayList result = new ArrayList();
110 try {
111 XPathQueryService queryService = (XPathQueryService) parent.getService("XPathQueryService", "1.0");
112 ResourceSet set = queryService.queryResource(docId, query);
113 if (set.getSize() == 0) {
114 throw new FinderException("The path used in the queryDocument method should return a set " +
115 "containing only one value. Querying document " + docId + " with query " + query);
116 }
117 ResourceIterator setIter = set.getIterator();
118 while (setIter.hasMoreResources()) {
119 XMLResource xmlRes = (XMLResource)setIter.nextResource();
120 Document doc = new DOMReader().read((org.w3c.dom.Document)xmlRes.getContentAsDOM());
121 result.add(doc.getRootElement());
122 }
123 } catch (XMLDBException e) {
124 throw new XMLSessionException(e);
125 }
126 return result;
127 }
128
129 public static Collection createCollection(Collection parent, String collectionName) throws XMLSessionException {
130 Collection createdColl = null;
131 try {
132 CollectionManager parentService = (CollectionManager) parent.getService("CollectionManager", "1.0");
133 Object[] params = {collectionName};
134 createdColl = parentService.createCollection(collectionName, DOMParser.toDocument(CREATE_COLL.format(params)));
135 } catch (XMLDBException e) {
136 throw new XMLSessionException("An exception occured when trying to create a collection "
137 + collectionName + " with parent collection " + parent, e);
138 } catch (XindiceException e) {
139 throw new XMLSessionException("An exception occured when trying to create a collection "
140 + collectionName + " with parent collection " + parent, e);
141 }
142 return createdColl;
143 }
144
145 public static void insertDocument(Collection parent, String docId, Document document) throws XMLSessionException {
146 if (document.selectSingleNode(TWISTER_ID) == null) {
147 document.getRootElement().addElement(TWISTER_ID).setText(docId);
148 }
149 try {
150 XMLResource documentRes = (XMLResource) parent.createResource(docId, "XMLResource");
151 documentRes.setContentAsDOM(new DOMWriter().write(document));
152 parent.storeResource(documentRes);
153 } catch (XMLDBException e) {
154 throw new XMLSessionException("An error occured when trying to insert a document " + docId + " with " +
155 "parent collection " + parent, e);
156 } catch (DocumentException e) {
157 throw new XMLSessionException("An error occured when trying to insert a document " + docId + " with " +
158 "parent collection " + parent, e);
159 }
160 }
161
162 public static void deleteDocument(Collection parent, String docId) throws XMLSessionException {
163 try {
164 XMLResource document = (XMLResource) parent.getResource(docId);
165 parent.removeResource(document);
166 } catch (XMLDBException e) {
167 throw new XMLSessionException("An error occured when trying to delete the document with id " + docId +
168 " from collection " + parent, e);
169 }
170 }
171
172 /***
173 * Removes all documents in a collection. Does nothing if no collection can be found
174 * at the provided path.
175 * @param path to the collection
176 * @throws XMLSessionException
177 */
178 public static void cleanCollection(String path) throws XMLSessionException {
179 Collection coll = getCollection(path);
180 if (coll != null) {
181 try {
182 XPathQueryService queryService = (XPathQueryService) coll.getService("XPathQueryService", "1.0");
183 ResourceIterator results = queryService.query("//").getIterator();
184 while (results.hasMoreResources()) {
185 Resource res = results.nextResource();
186 coll.removeResource(res);
187 }
188 } catch (XMLDBException e) {
189 throw new XMLSessionException(e);
190 }
191 }
192 }
193
194 private static String getXindiceURL() {
195 if (xindiceURL == null) {
196 xindiceURL = CommonConfiguration.getXindiceURL();
197 }
198 return xindiceURL;
199 }
200
201 protected static DOMReader getDomReader() {
202 if (domReader == null) {
203 domReader = new DOMReader();
204 }
205 return domReader;
206 }
207
208 }
This page was automatically generated by Maven