Hi all,
I'm a student of computer science (University of Bologna, Italy) and I'm
writing my final thesis. In my thesis i'm comparing some DBMS XML native.
I want to test xindice with bumblebee, but aren't an adapter for xindice,
and i would to implement one, but i need some helps.
I have to implement these two functions:
public void load(String uri) throws AdapterException;
// where @param uri is the URI to load, for example
// docs/2005/test.xml
and
public String evaluate (String query) throws AdapterException;
I'm testing eXist XML native DBMS too, i attach the .java
of bumblebee adapter for eXist, i want write a similar code for xindice
thanks to all
Mauro Maiorca
package bumblebee;
import java.io.File;
import java.util.Properties;
import java.util.StringTokenizer;
import org.exist.xmldb.DatabaseInstanceManager;
import org.exist.xmldb.XQueryService;
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.base.XMLDBException;
import org.xmldb.api.modules.CollectionManagementService;
import org.xmldb.api.modules.XMLResource;
/*
* eXist Open Source Native XML Database
* Copyright (C) 2001-03 Wolfgang M. Meier
* [EMAIL PROTECTED]
* http://exist.sourceforge.net
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* $Id$
*/
/**
* @author Wolfgang Meier ([EMAIL PROTECTED])
*/
public class eXistAdapter implements Adapter {
public final static String driver = "org.exist.xmldb.DatabaseImpl";
private String uri;
private String user;
private String password;
private Collection root;
private Collection collection;
/**
*
*/
public eXistAdapter(Properties properties) {
this.uri =
properties.getProperty("eXist.collection", "xmldb:exist:///db");
this.user = properties.getProperty("eXist.user", "admin");
this.password = properties.getProperty("eXist.password", "");
System.out.println("Initializing eXist from " + uri);
try {
// initialize driver
Class cl = Class.forName(driver);
Database database = (Database) cl.newInstance();
database.setProperty("create-database", "true");
System.out.println("registering database driver...");
DatabaseManager.registerDatabase(database);
System.out.println("getting root collection");
root =
DatabaseManager.getCollection("xmldb:exist:///db", "admin", null);
collection = root;
System.out.println("eXist Database initialized...");
} catch (Exception e) {
System.out.println("Caught exception: " + e.getMessage());
throw new RuntimeException("Database setup failed: " + e.getMessage(), e);
}
}
/* (non-Javadoc)
* @see bumblebee.Adapter#load(java.lang.String)
*/
public void load(String filename) throws AdapterException {
File f = new File(filename);
if(!f.canRead())
throw new AdapterException("cannot read file: " + filename);
int p = filename.lastIndexOf(File.separatorChar);
collection = root;
try {
if(p > -1)
collection = mkcol(root, uri, "", filename.substring(0, p));
XMLResource res = (XMLResource)collection.createResource(f.getName(), "XMLResource");
res.setContent(f);
collection.storeResource(res);
} catch (Exception e) {
throw new AdapterException(e.getMessage(), e);
}
}
/* (non-Javadoc)
* @see bumblebee.Adapter#evaluate(java.lang.String)
*/
public String evaluate(String query) throws AdapterException {
try {
XQueryService service = (XQueryService)collection.getService("XQueryService", "1.0");
service.setProperty("base-uri", "/db");
ResourceSet results = service.query(query);
StringBuffer buf = new StringBuffer();
for(ResourceIterator i = results.getIterator(); i.hasMoreResources(); ) {
Resource r = i.nextResource();
buf.append((String)r.getContent());
}
return buf.toString();
} catch (XMLDBException e) {
throw new AdapterException(e.getMessage(), e);
}
}
private final Collection mkcol(
Collection root,
String baseURI,
String path,
String relPath)
throws XMLDBException {
CollectionManagementService mgtService;
Collection current = root, c;
String token;
StringTokenizer tok = new StringTokenizer(relPath, "/");
while (tok.hasMoreTokens()) {
token = tok.nextToken();
path = path + '/' + token;
c = DatabaseManager.getCollection(baseURI + path, user, password);
if (c == null) {
mgtService =
(CollectionManagementService) current.getService(
"CollectionManagementService",
"1.0");
current = mgtService.createCollection(token);
System.out.println("Created collection " + current.getName() + '.');
} else
current = c;
}
return current;
}
/* (non-Javadoc)
* @see java.lang.Object#finalize()
*/
protected void finalize() throws Throwable {
DatabaseInstanceManager manager = (DatabaseInstanceManager)
root.getService("DatabaseInstanceManager", "1.0");
manager.shutdown();
}
}