1 package org.opensync.xmldb;
2
3 import org.apache.xindice.client.xmldb.services.CollectionManager;
4 import org.apache.xindice.util.XindiceException;
5 import org.apache.xindice.xml.dom.DOMParser;
6 import org.xmldb.api.base.Database;
7 import org.xmldb.api.base.Resource;
8 import org.xmldb.api.base.ResourceIterator;
9 import org.xmldb.api.base.ResourceSet;
10 import org.xmldb.api.base.XMLDBException;
11 import org.xmldb.api.modules.XMLResource;
12 import org.xmldb.api.modules.XPathQueryService;
13
14 /***
15 * <p>Title: DatabaseManager</p>
16 * <p>Description: XML:DB API database Manager (cf. http://www.xmldb.org).</p>
17 */
18
19 public class DatabaseManager {
20
21 String driver = "org.apache.xindice.client.xmldb.DatabaseImpl";
22 Database database;
23
24 public DatabaseManager() throws ClassNotFoundException, IllegalAccessException, InstantiationException, XMLDBException {
25
26 Class c = Class.forName(driver);
27 database = (Database) c.newInstance();
28 org.xmldb.api.DatabaseManager.registerDatabase(database);
29 }
30
31 public void closeDatabase() throws ClassNotFoundException, IllegalAccessException, InstantiationException, XMLDBException {
32 org.xmldb.api.DatabaseManager.deregisterDatabase(database);
33 }
34
35 public org.xmldb.api.base.Collection tx_start(org.xmldb.api.base.Collection col) throws XindiceException, XMLDBException {
36
37
38 String collectionName = "tx_"+col.getName();
39 if (this.getCollection(collectionName) != null) this.removeCollection(collectionName);
40 org.xmldb.api.base.Collection tx_col = this.createCollection(collectionName);
41 return tx_col;
42 }
43
44 public org.xmldb.api.base.Collection tx_commit(org.xmldb.api.base.Collection col) throws XindiceException, XMLDBException {
45
46
47 String tx_collectionName = "tx_"+col.getName();
48 org.xmldb.api.base.Collection tx_col = null;
49 if ( (tx_col = this.getCollection(tx_collectionName)) != null) {
50
51 col = this.copyCollection(tx_col, col);
52 }
53 return col;
54 }
55
56 public org.xmldb.api.base.Collection tx_rollback(org.xmldb.api.base.Collection col) throws XindiceException, XMLDBException {
57
58
59 String tx_collectionName = "tx_"+col.getName();
60 org.xmldb.api.base.Collection tx_col = null;
61 if ( (tx_col = this.getCollection(tx_collectionName)) != null) {
62 this.removeCollection(tx_collectionName);
63 }
64
65 return col;
66 }
67
68 public org.xmldb.api.base.Collection copyCollection(org.xmldb.api.base.Collection src_col, org.xmldb.api.base.Collection dest_col) throws XindiceException, XMLDBException {
69 String[] res = src_col.listResources();
70 for (int i=0; i < res.length; i++) {
71 dest_col.storeResource(src_col.getResource(res[i]));
72 }
73 return dest_col;
74 }
75
76 public org.xmldb.api.base.Collection createCollection(String collectionName) throws XindiceException, XMLDBException {
77
78 org.xmldb.api.base.Collection col = null;
79 col = org.xmldb.api.DatabaseManager.getCollection("xmldb:xindice:///db/");
80 CollectionManager service = (CollectionManager) col.getService("CollectionManager", "1.0");
81
82
83 String collectionConfig =
84 "<collection compressed=\"true\" name=\"" + collectionName + "\">" +
85 " <filer class=\"org.apache.xindice.core.filer.BTreeFiler\" gzip=\"true\"/>" +
86 "</collection>";
87 service.createCollection(collectionName, DOMParser.toDocument(collectionConfig));
88
89 col = this.getCollection(collectionName);
90 return col;
91 }
92
93 public org.xmldb.api.base.Collection getCollection(String collectionName) throws XindiceException, XMLDBException {
94
95 org.xmldb.api.base.Collection col = null;
96 col = org.xmldb.api.DatabaseManager.getCollection("xmldb:xindice:///db/"+collectionName);
97
98
99
100
101 return col;
102 }
103
104 public void removeCollection(String collectionName) throws XindiceException, XMLDBException {
105 org.xmldb.api.base.Collection col = null;
106 col = org.xmldb.api.DatabaseManager.getCollection("xmldb:xindice:///db/");
107 CollectionManager service = (CollectionManager) col.getService("CollectionManager", "1.0");
108 service.removeCollection(collectionName);
109 }
110
111 public void addDocument(org.xmldb.api.base.Collection col, String id, String xmlData) throws XindiceException, XMLDBException {
112
113 XMLResource document = (XMLResource) col.createResource(id, "XMLResource");
114 document.setContent(xmlData);
115 col.storeResource(document);
116
117 }
118
119 public void removeDocument(org.xmldb.api.base.Collection col, String xmlData) throws XindiceException, XMLDBException {
120
121 Resource document = col.getResource(xmlData);
122 col.removeResource(document);
123
124 }
125
126 public XMLResource retrieveDocument(org.xmldb.api.base.Collection col, String id) throws XindiceException, XMLDBException {
127 XMLResource document = null;
128
129 document = (XMLResource) col.getResource(id);
130 if (document != null) {
131
132
133 }
134 else {
135
136 }
137 return document;
138 }
139
140 public ResourceSet xpathQuery(org.xmldb.api.base.Collection col, String xpath) throws XindiceException, XMLDBException {
141
142 XPathQueryService service = (XPathQueryService) col.getService("XPathQueryService", "1.0");
143 ResourceSet resultSet = service.query(xpath);
144
145 return resultSet;
146 }
147
148 public void close(org.xmldb.api.base.Collection col) {
149 try {
150 if (col != null) {
151 col.close();
152 }
153 } catch (XMLDBException e) {
154
155 }
156 }
157
158 public void displayResults(ResourceSet resultSet) throws XMLDBException {
159 ResourceIterator results = resultSet.getIterator();
160 while (results.hasMoreResources()) {
161 Resource res = results.nextResource();
162
163 }
164 }
165
166 public static void main(String[] args) throws Exception {
167
168 DatabaseManager dbMgr = null;
169 org.xmldb.api.base.Collection col = null;
170 org.xmldb.api.base.Collection tx_col = null;
171 try {
172 dbMgr = new DatabaseManager();
173 if ( (col = dbMgr.getCollection("test1")) == null) col = dbMgr.createCollection("test1");
174
175 tx_col = dbMgr.tx_start(col);
176
177
178 String view = "<view keyword=\"theme4\" value=\"val4\"/>";
179
180 if (dbMgr.retrieveDocument(col, "theme4") == null) {
181
182 dbMgr.addDocument(tx_col, "theme4", view);
183 }
184
185
186
187
188
189
190
191
192 dbMgr.tx_commit(col);
193
194 ResourceSet resultSet = dbMgr.xpathQuery(col, "/view[@keyword='theme4' and @value='val4']");
195 dbMgr.displayResults(resultSet);
196
197 } catch(Exception e) {
198 System.out.println(e.getLocalizedMessage());
199 }
200 finally {
201 dbMgr.close(col);
202 }
203 }
204 }