You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xindice-dev@xml.apache.org by xi...@xml.apache.org on 2004/03/23 18:15:39 UTC

[Xindice Wiki] New: ArticleUpgrade

   Date: 2004-03-23T09:15:36
   Editor: 66.32.90.157 <>
   Wiki: Xindice Wiki
   Page: ArticleUpgrade
   URL: http://wiki.apache.org/xindice/ArticleUpgrade

   no comment

New Page:

For people who are having problems with the code found in the DevX article on Xindice when they use Xindice 1.1 instead of 1.0, the following is an updated class file.

import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.Database;
import org.xmldb.api.base.Resource;
import org.xmldb.api.base.ResourceIterator;
import org.xmldb.api.base.ResourceSet;
import org.xmldb.api.modules.CollectionManagementService;
import org.xmldb.api.modules.XMLResource;
import org.xmldb.api.modules.XPathQueryService;
import org.xmldb.api.modules.XUpdateQueryService;

public class DBmanager {
    Collection rootCollection = null;
    Database database = null;

    public DBmanager() throws Exception {
        Class c = Class.forName("org.apache.xindice.client.xmldb.embed.DatabaseImpl");
        database = (Database) c.newInstance();
        database.setProperty("db-home",".");
        database.setProperty("managed","true");
        DatabaseManager.registerDatabase(database);
        rootCollection = DatabaseManager.getCollection("xmldb:xindice-embed:///db/");
    }

    public boolean createCollection(String collection) throws Exception {
        Collection col = null;
        try {
            CollectionManagementService service = (CollectionManagementService) rootCollection.getService("CollectionManagementService", "1.0");
            col = service.createCollection(collection);
            return true;
        } catch (Exception e) {
            e.printStackTrace(System.err);
            return false;
        } finally {
            if (col != null) col.close();
        }
    }
    
    public void dropCollection(String collection) throws Exception {
        CollectionManagementService service = (CollectionManagementService) rootCollection.getService("CollectionManagementService", "1.0");
        service.removeCollection(collection);
    }

    public boolean putData(String collection, String ID, String data) throws Exception {
        Collection col = null;
        try {
            col = DatabaseManager.getCollection("xmldb:xindice-embed:///db/" + collection);
            XMLResource document = (XMLResource) col.createResource(ID, "XMLResource");
            document.setContent(data);
            col.storeResource(document);
            return true;
        } catch (Exception e) {
            e.printStackTrace(System.err);
            return false;
        } finally {
            if (col != null) col.close();
        }
    }

    public String getData(String collection, String ID) throws Exception {
        Collection col = null;
        try {
            col = DatabaseManager.getCollection("xmldb:xindice-embed:///db/" + collection);
            XMLResource document = (XMLResource) col.getResource(ID);
            if (document != null)
                return (String) document.getContent();
            else
                return "";
        } catch (Exception e) {
            e.printStackTrace(System.err);
            return "";
        } finally {
            if (col != null) col.close();
        }
    }

    public boolean removeData(String collection, String ID) throws Exception {
        Collection col = null;
        try {
            col = DatabaseManager.getCollection("xmldb:xindice-embed:///db/" + collection);
            Resource document = col.getResource(ID);
            col.removeResource(document);
            return true;
        } catch (Exception e) {
            e.printStackTrace(System.err);
            return false;
        } finally {
            if (col != null) col.close();
        }
    }

    public String queryCollection(String collection, String xpath) throws Exception {
        Collection col = null;
        try {
            col = DatabaseManager.getCollection("xmldb:xindice-embed:///db/" + collection);
            XPathQueryService service = (XPathQueryService) col.getService("XPathQueryService", "1.0");
            ResourceSet resultSet = service.query(xpath);
            ResourceIterator results = resultSet.getIterator();
            String allResults = "";
            while (results.hasMoreResources()) {
                Resource res = results.nextResource();
                allResults += "[" + res.getContent() + "]";
            }
            return allResults;
        } catch (Exception e) {
            e.printStackTrace(System.err);
            return "";
        } finally {
            if (col != null) col.close();
        }
    }

    public boolean updateData(String collection, String ID, String xupdate) throws Exception {
        Collection col = null;
        try {
            col = DatabaseManager.getCollection("xmldb:xindice-embed:///db/" + collection);
            XUpdateQueryService service = (XUpdateQueryService) col.getService("XUpdateQueryService", "1.0");
            service.updateResource(ID, xupdate);
            return true;
        } catch (Exception e) {
            e.printStackTrace(System.err);
            return false;
        } finally {
            if (col != null) col.close();
        }
    }
}