1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.nxqd.xmldb;
17
18 import org.xmldb.api.base.Collection;
19 import org.xmldb.api.base.ErrorCodes;
20 import org.xmldb.api.base.Resource;
21 import org.xmldb.api.base.Service;
22 import org.xmldb.api.base.XMLDBException;
23 import org.xmldb.api.modules.XMLResource;
24 import org.xmldb.api.modules.BinaryResource;
25
26 import net.sf.nxqd.xmldb.NxqdDatabase;
27 import net.sf.nxqd.NxqdException;
28 import net.sf.nxqd.NxqdContainer;
29 import net.sf.nxqd.NxqdXMLValue;
30 import net.sf.nxqd.NxqdBlobValue;
31 import net.sf.nxqd.common.NxqdUtils;
32
33 import java.util.List;
34 import java.util.Properties;
35 import java.util.ArrayList;
36 import java.util.logging.Level;
37 import java.util.logging.Logger;
38
39 /**
40 *
41 * @see org.xmldb.api.base.Collection
42 *
43 * @author <a href="mailto:webhiker@sourceforge.net">webhiker</a>
44 * @version 1.0
45 */
46 public class NxqdCollection extends NxqdService implements Collection {
47 private static Logger logger = Logger.getLogger(NxqdCollection.class.getName());
48
49 private NxqdContainer container = null;
50 private NxqdDatabase database = null;
51 private NxqdCollection parent = null;
52 public static final String CollectionManagementServiceTag ="CollectionManagementService";
53 public static final String XPathQueryServiceTag ="XPathQueryService";
54 public static final String XUpdateQueryServiceTag ="XUpdateQueryService";
55 public static final String XQueryServiceTag ="XQueryService";
56
57
58 protected NxqdCollection(String collectionName,
59 NxqdDatabase db,
60 NxqdContainer container,
61 NxqdCollection parent) throws XMLDBException {
62 super(collectionName,"1.0");
63 this.database = db;
64 this.container = container;
65 this.parent = parent;
66 setCollection(this);
67 }
68
69 /**
70 * The <code>getContainer</code> method is used internally to get a handle to the
71 * underlying container.
72 *
73 * @return a <code>NxqdContainer</code> value
74 */
75 protected NxqdContainer getContainer() {
76 return container;
77 }
78
79 /**
80 * The <code>getDatabase</code> method is used internally to get a handle to the
81 * underlying database.
82 *
83 * @return a <code>NxqdDatabase</code> value
84 */
85 protected NxqdDatabase getDatabase() {
86 return database;
87 }
88
89 /**
90 * @see org.xmldb.api.base.Collection#getServices()
91 */
92 public Service[] getServices() throws XMLDBException {
93 return new Service[] {
94 getService(CollectionManagementServiceTag, "1.0"),
95 getService(XPathQueryServiceTag, "1.0"),
96 getService(XUpdateQueryServiceTag, "1.0"),
97 getService(XQueryServiceTag, "1.0")
98 };
99 }
100
101 /**
102 * @see org.xmldb.api.base.Collection#getService(java.lang.String,
103 * java.lang.String)
104 */
105 public Service getService(String name, String version)
106 throws XMLDBException {
107 NxqdService service=null;
108 if (name.equals(XPathQueryServiceTag)) {
109 service = new NxqdXPathQueryService(this);
110 }
111 if (name.equals(CollectionManagementServiceTag)) {
112 service = new NxqdCollectionManagementService(this);
113 }
114 if (name.equals(XQueryServiceTag)) {
115 service = new NxqdXPathQueryService(this);
116 }
117 if (name.equals(XUpdateQueryServiceTag)) {
118 service = new NxqdXUpdateQueryService();
119 }
120 service.setCollection(this);
121 return service;
122 }
123
124 /**
125 * @see org.xmldb.api.base.Collection#getParentCollection()
126 */
127 public Collection getParentCollection() throws XMLDBException {
128 return parent;
129 }
130
131
132 /**
133 * @see org.xmldb.api.base.Collection#getChildCollectionCount()
134 */
135 public int getChildCollectionCount() throws XMLDBException {
136 return database.listChildCollections(this).size();
137 }
138
139 /**
140 * @see org.xmldb.api.base.Collection#listChildCollections()
141 */
142 public String[] listChildCollections() throws XMLDBException {
143 List children = database.listChildCollections(this);
144 String strArray[] = new String[children.size()];
145 for (int i = 0; i < strArray.length; i++) {
146 strArray[i] = children.get(i).toString();
147 }
148 return strArray;
149 }
150
151 /**
152 * @see org.xmldb.api.base.Collection#getChildCollection(java.lang.String)
153 */
154 public Collection getChildCollection(String name) throws XMLDBException {
155 return database.getChildCollection(name, this);
156 }
157
158 /**
159 * @see org.xmldb.api.base.Collection#getResourceCount()
160 */
161 public int getResourceCount() throws XMLDBException {
162 if (container == null)
163 return 0;
164 else {
165 try {
166 return (int)container.getNumDocuments();
167 }
168 catch (NxqdException ne) {
169 throw new XMLDBException(ErrorCodes.VENDOR_ERROR,
170 "Error get resource count :" + ne.getMessage(),
171 ne);
172 }
173 }
174 }
175
176 /**
177 * @see org.xmldb.api.base.Collection#listResources()
178 */
179 public String[] listResources() throws XMLDBException {
180 List docList;
181 String res[];
182 if (container.getName() == null)
183 return new String[0];
184 else {
185 try {
186 docList = container.listDocuments();
187 } catch (NxqdException ne) {
188 throw new XMLDBException(ErrorCodes.VENDOR_ERROR,
189 "Error listing resources :" + ne.getMessage(),
190 ne);
191 }
192 }
193 res = new String[docList.size()];
194 int i;
195 for (i = 0; i < docList.size(); i++) {
196 res[i] = (String) docList.get(i);
197 }
198 return res;
199 }
200
201 /**
202 * @see org.xmldb.api.base.Collection#createResource(java.lang.String,
203 * java.lang.String)
204 */
205 public Resource createResource(String id, String type)
206 throws XMLDBException {
207 try {
208 if (type.equals(XMLResource.RESOURCE_TYPE)) {
209 return new NxqdXMLResource(this, id, type);
210 }
211 if (type.equals(BinaryResource.RESOURCE_TYPE)) {
212 return new NxqdBinaryResource(this, id, type);
213 }
214 throw new NxqdException("Unsupported resource type ("+type+")");
215 }
216 catch (NxqdException ne) {
217 throw new XMLDBException(ErrorCodes.VENDOR_ERROR,
218 "Error creating resource :" + ne.getMessage(),
219 ne);
220 }
221 }
222
223 /**
224 * @see org.xmldb.api.base.Collection#removeResource(org.xmldb.api.base.Resource)
225 */
226 public void removeResource(Resource res) throws XMLDBException {
227 if (container == null) {
228 throw new XMLDBException(ErrorCodes.VENDOR_ERROR,
229 "Collection has been closed");
230 }
231 else {
232 try {
233 container.deleteDocument(res.getId());
234 } catch (NxqdException ne) {
235 throw new XMLDBException(ErrorCodes.VENDOR_ERROR,
236 "Error removing resource :" + ne.getMessage(),
237 ne);
238 }
239 }
240 }
241
242 /**
243 * @see org.xmldb.api.base.Collection#storeResource(org.xmldb.api.base.Resource)
244 */
245 public void storeResource(Resource res) throws XMLDBException {
246 if (container == null) {
247 throw new XMLDBException(ErrorCodes.VENDOR_ERROR,
248 "Collection has been closed");
249 }
250 else {
251 try {
252 if (res.getResourceType()==XMLResource.RESOURCE_TYPE) {
253 container.putDocument(res.getId(),
254 new NxqdXMLValue(res.getContent().toString()));
255 return;
256 }
257 if (res.getResourceType()==BinaryResource.RESOURCE_TYPE) {
258 container.putBlob(res.getId(),
259 new NxqdBlobValue((byte[])res.getContent()));
260 return;
261 }
262 throw new NxqdException("Unsupported resource type ("+res.getResourceType()+")");
263 } catch (NxqdException ne) {
264 throw new XMLDBException(ErrorCodes.VENDOR_ERROR,
265 "Error storing resource :" + ne.getMessage(),
266 ne);
267 }
268 }
269 }
270
271 /**
272 * @see org.xmldb.api.base.Collection#getResource(java.lang.String)
273 */
274 public Resource getResource(String id) throws XMLDBException {
275 if (container == null) {
276 throw new XMLDBException(ErrorCodes.VENDOR_ERROR,
277 "Collection has been closed");
278 }
279 else {
280 try {
281 String textDoc;
282
283 if (container.documentExists(id)) {
284 NxqdResource resource = new NxqdXMLResource(this, id, XMLResource.RESOURCE_TYPE);
285 resource.setContent(container.getDocument(id));
286 return resource;
287 }
288
289 if (container.blobExists(id)) {
290 NxqdResource resource = new NxqdBinaryResource(this, id, BinaryResource.RESOURCE_TYPE);
291 resource.setContent(container.getBlob(id));
292 return resource;
293 }
294 return null;
295 }
296 catch (Throwable ne) {
297 logger.severe("Unexpected error occurred ("+ne.getMessage()+")");
298 return null;
299 }
300 }
301 }
302
303 /**
304 * @see org.xmldb.api.base.Collection#createId()
305 */
306 public String createId() throws XMLDBException {
307 return NxqdUtils.generateUniqueId();
308 }
309
310 /**
311 * @see org.xmldb.api.base.Collection#isOpen()
312 */
313 public boolean isOpen() throws XMLDBException {
314 return (database != null);
315 }
316
317 /**
318 * @see org.xmldb.api.base.Collection#close()
319 */
320 public void close() throws XMLDBException {
321 database = null;
322 container = null;
323 }
324
325 /**
326 * @see java.lang.Object#equals(java.lang.Object)
327 */
328 public boolean equals(Object o) {
329 if (o == null) return false;
330 if (o instanceof NxqdCollection) {
331 NxqdCollection c = (NxqdCollection)o;
332 if (c.getName().equals(getName())) {
333 if (c.getContainer().getName().equals(getContainer().getName())) {
334 return true;
335 }
336 }
337
338 }
339 return false;
340 }
341
342 }