private void collectionToSAX(ContentHandler handler) throws SAXException, ProcessingException {
AttributesImpl attributes = new AttributesImpl();
try {
Collection collection = DatabaseManager.getCollection(url);
if (collection == null) {
throw new ResourceNotFoundException("Collection " + url +
" not found");
}
if (query != null) {
// Query collection
if (log.isDebugEnabled()) {
this.log.debug("Querying collection " + url + "; query= " + this.query);
}
queryToSAX(handler, collection, null);
} else {
// List collection
if (log.isDebugEnabled()) {
this.log.debug("Listing collection " + url);
}
final String ncollections = Integer.toString(collection.getChildCollectionCount());
final String nresources = Integer.toString(collection.getResourceCount());
attributes.addAttribute("", RESOURCE_COUNT_ATTR,
RESOURCE_COUNT_ATTR, "CDATA", nresources);
attributes.addAttribute("", COLLECTION_COUNT_ATTR,
COLLECTION_COUNT_ATTR, "CDATA", ncollections);
// attributes.addAttribute("", COLLECTION_BASE_ATTR,
// COLLECTION_BASE_ATTR, "CDATA", url);
handler.startDocument();
handler.startPrefixMapping(PREFIX, URI);
handler.startElement(URI, COLLECTIONS, QCOLLECTIONS, attributes);
// Print child collections
String[] collections = collection.listChildCollections();
for (int i = 0; i < collections.length; i++) {
attributes.clear();
attributes.addAttribute("", NAME_ATTR, NAME_ATTR, CDATA, collections[i]);
handler.startElement(URI, COLLECTION,
QCOLLECTION, attributes);
handler.endElement(URI, COLLECTION, COLLECTION);
}
// Print child resources
String[] resources = collection.listResources();
for (int i = 0; i < resources.length; i++) {
attributes.clear();
attributes.addAttribute("", NAME_ATTR, NAME_ATTR, CDATA, resources[i]);
handler.startElement(URI, RESOURCE,
QRESOURCE, attributes);
handler.endElement(URI, RESOURCE, RESOURCE);
}
handler.endElement(URI, COLLECTIONS, QCOLLECTIONS);
handler.endPrefixMapping(PREFIX);
handler.endDocument();
}
collection.close();
} catch (XMLDBException xde) {
String error = "Collection listing failed. Error " + xde.errorCode + ": " + xde.getMessage();
this.log.debug(error, xde);
throw new SAXException(error, xde);
}