Package org.apache.xindice.integration.client

Source Code of org.apache.xindice.integration.client.XmlDbClient

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*
* $Id: XmlDbClient.java 521531 2007-03-22 23:40:56Z vgritsenko $
*/

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 $Revision: 521531 $, $Date: 2007-03-22 19:40:56 -0400 (Thu, 22 Mar 2007) $
* @author Vladimir R. Bossicard <vladimir@apache.org>
*/
public class XmlDbClient {

    protected String driver;
    protected String compression = "true";

    public XmlDbClient(String driver) throws Exception {
        this.driver = driver;
    }

    public XmlDbClient(String driver, boolean compression) throws Exception {
        this.driver = driver;
        this.compression = compression ? "true" : "false";
    }

    public String getDriver() {
        return driver;
    }

    public String getCompression() {
        return compression;
    }

    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='" + compression + "' 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);
    }
}
TOP

Related Classes of org.apache.xindice.integration.client.XmlDbClient

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.