/*
* Copyright 1999-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* CVS $Id: XmlDbClient.java,v 1.18 2004/03/30 14:01:07 vgritsenko Exp $
*/
package org.apache.xindice.integration.client;
import org.apache.xindice.client.xmldb.services.CollectionManager;
import org.apache.xindice.util.XindiceException;
import org.apache.xindice.xml.dom.DOMParser;
import org.w3c.dom.Document;
import org.xml.sax.ContentHandler;
import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import org.xmldb.api.modules.XMLResource;
/**
* @version CVS $Revision: 1.18 $, $Date: 2004/03/30 14:01:07 $
* @author Vladimir R. Bossicard <vladimir@apache.org>
*/
public class XmlDbClient {
protected String driver;
public XmlDbClient(String driver) throws Exception {
this.driver = driver;
}
public String getDriver() {
return driver;
}
public String getName(String path) throws Exception {
Collection col = DatabaseManager.getCollection(driver + "/" + path);
if (col == null) {
throw new XindiceException("DatabaseManager.getCollection(" + driver + "/" + path + ") returned null");
}
return col.getName();
}
public Collection createCollection(String parent, String name) throws Exception {
return createCollection(parent, name, name);
}
public Collection createCollection(String parent, String path, String name) throws Exception {
// Inline meta data is on
String config =
"<collection compressed=\"true\" name=\"" + name + "\" inline-metadata=\"true\">" +
" <filer class=\"org.apache.xindice.core.filer.BTreeFiler\" />" +
"</collection>";
return createCollection(parent, path, DOMParser.toDocument(config));
}
public Collection createCollection(String parent, String path, Document configuration) throws Exception {
Collection col = DatabaseManager.getCollection(driver + "/" + parent);
if (col == null) {
throw new XindiceException("DatabaseManager.getCollection(" + driver + "/" + parent + ") returned null");
}
CollectionManager service = (CollectionManager) col.getService("CollectionManager", "1.0");
return service.createCollection(path, configuration);
}
public Collection getCollection(String path) throws Exception {
return DatabaseManager.getCollection(driver + "/" + path);
}
public void dropCollection(String path, String name) throws Exception {
Collection col = DatabaseManager.getCollection(driver + "/" + path);
if (col != null) {
CollectionManager service = (CollectionManager) col.getService("CollectionManager", "1.0");
service.dropCollection(name);
}
}
public void createIndexer(String path, String indexDoc) throws Exception {
createIndexer(path, DOMParser.toDocument(indexDoc));
}
public void createIndexer(String path, Document indexDoc) throws Exception {
Collection col = DatabaseManager.getCollection(driver + "/" + path);
if (col == null) {
throw new XindiceException("DatabaseManager.getCollection(" + driver + "/" + path + ") returned null");
}
CollectionManager service = (CollectionManager) col.getService("CollectionManager", "1.0");
service.createIndexer(indexDoc);
}
public String[] listIndexes(String path) throws Exception {
Collection col = DatabaseManager.getCollection(driver + "/" + path);
if (col == null) {
throw new XindiceException("DatabaseManager.getCollection(" + driver + "/" + path + ") returned null");
}
CollectionManager service = (CollectionManager) col.getService("CollectionManager", "1.0");
return service.listIndexers();
}
public void dropIndexer(String path, String name) throws Exception {
Collection col = DatabaseManager.getCollection(driver + "/" + path);
if (col == null) {
throw new XindiceException("DatabaseManager.getCollection(" + driver + "/" + path + ") returned null");
}
CollectionManager service = (CollectionManager) col.getService("CollectionManager", "1.0");
service.dropIndexer(name);
}
public void insertDocument(String path, String name, String doc) throws Exception {
Collection col = DatabaseManager.getCollection(driver + "/" + path);
if (col == null) {
throw new XindiceException("DatabaseManager.getCollection(" + driver + "/" + path + ") returned null");
}
XMLResource document = (XMLResource) col.createResource(name, "XMLResource");
document.setContent(doc);
col.storeResource(document);
}
public int countDocument(String path) throws Exception {
Collection col = DatabaseManager.getCollection(driver + "/" + path);
if (col == null) {
throw new XindiceException("DatabaseManager.getCollection(" + driver + "/" + path + ") returned null");
}
return col.getResourceCount();
}
public String getDocument(String path, String name) throws Exception {
Collection col = DatabaseManager.getCollection(driver + "/" + path);
if (col == null) {
throw new XindiceException("DatabaseManager.getCollection(" + driver + "/" + path + ") returned null");
}
XMLResource document = (XMLResource) col.getResource(name);
if (document == null) {
return null;
}
return document.getContent().toString();
}
public void updateDocument(String path, String name, String doc) throws Exception {
Collection col = DatabaseManager.getCollection(driver + "/" + path);
if (col == null) {
throw new XindiceException("DatabaseManager.getCollection(" + driver + "/" + path + ") returned null");
}
XMLResource document = (XMLResource) col.getResource(name);
document.setContent(doc);
col.storeResource(document);
}
public void getDocumentAsSax(String path, String name, ContentHandler handler) throws Exception {
Collection col = DatabaseManager.getCollection(driver + "/" + path);
if (col == null) {
throw new XindiceException("DatabaseManager.getCollection(" + driver + "/" + path + ") returned null");
}
XMLResource document = (XMLResource) col.getResource(name);
if (document == null) {
return;
}
document.getContentAsSAX(handler);
}
public String[] listDocuments(String path) throws Exception {
Collection col = DatabaseManager.getCollection(driver + "/" + path);
return col.listResources();
}
public void removeDocument(String path, String name) throws Exception {
Collection col = DatabaseManager.getCollection(driver + "/" + path);
if (col == null) {
throw new XindiceException("DatabaseManager.getCollection(" + driver + "/" + path + ") returned null");
}
XMLResource document = (XMLResource) col.getResource(name);
col.removeResource(document);
}
}