// You can redistribute this software and/or modify it under the terms of
// the Ozone Library License version 1 published by ozone-db.org.
//
// The original code and portions created by SMB are
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
//
// $Id: DatabaseImpl.java,v 1.1 2001/12/18 11:03:24 per_nyfelt Exp $
package org.ozoneDB.xml.cli;
import java.util.StringTokenizer;
import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.Configurable;
import org.xmldb.api.base.Database;
import org.xmldb.api.base.ErrorCodes;
import org.xmldb.api.base.XMLDBException;
import org.ozoneDB.ExternalDatabase;
/**
*
* @author <a href="http://www.smb-tec.com">SMB</a>
* @version $Revision: 1.1 $
*/
public class DatabaseImpl extends AbstractConfigurable implements Database {
//
// constants
//
private static String DB_HOST = "DB_HOST";
private static String DB_NAME = "DB_NAME";
private static String DB_PORT = "DB_PORT";
private static String COL_NAME = "COL_NAME";
public static String CONFORMANCE_LEVEL = "0";
public static String DATABASE_NAME = "ozoneXML";
// automatically register the database when the class is loaded
static {
try {
org.xmldb.api.DatabaseManager.registerDatabase( new DatabaseImpl() );
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Zero-argument constructor.
*/
public DatabaseImpl() {
super();
}
/**
*/
public String getName() throws XMLDBException {
return DATABASE_NAME;
}
/**
*/
public Collection getCollection( String uri )
throws XMLDBException {
// we don't know how to handle this uri
if ( !acceptsURI( uri ) )
throw new XMLDBException( ErrorCodes.INVALID_URI, "INVALID_URI" );
Collection collection = null;
try {
ExternalDatabase db = null;
StringBuffer ozoneDB = new StringBuffer();
if ( getProperty( DB_HOST ) == null )
ozoneDB.append( "ozonedb:local:").append( getProperty( DB_NAME ) );
else
ozoneDB.append( "ozonedb:remote://" ).append( getProperty( DB_HOST ) ).append(
":").append( getProperty( DB_PORT ) );
System.out.println("DatabaseImpl.getCollection() - Remote DB: " + getProperty(DB_HOST) + " " + getProperty(DB_PORT));
System.out.println("DatabaseImpl.getCollection() - Local DB: " + getProperty(DB_NAME));
System.out.println("DatabaseImpl.getCollection() - Load Collection: " + getProperty(COL_NAME));
db = ExternalDatabase.openDatabase( ozoneDB.toString() );
if (db == null)
throw new XMLDBException( ErrorCodes.INVALID_DATABASE, "INVALID_DATABASE" );
db.reloadClasses();
collection = CollectionImpl.forName( db, getProperty( COL_NAME ) );
if (collection == null)
throw new XMLDBException( ErrorCodes.INVALID_COLLECTION, "INVALID_COLLECTION" );
} catch ( XMLDBException e ) {
// re-throw this exception
throw e;
} catch ( Exception e ) {
// encapsulate all other exceptions
throw new XMLDBException( ErrorCodes.VENDOR_ERROR, e.toString() );
}
return collection;
}
/**
*/
public boolean acceptsURI( String uri ) throws XMLDBException {
return parseURI( uri );
}
/**
*/
public String getConformanceLevel() throws XMLDBException {
return CONFORMANCE_LEVEL;
}
/**
* NON API CONFORM EXTENSION!
*/
// public Collection createRootCollection( String uri, String collectionName )
// throws XMLDBException {
// // we don't know how to handle this uri
// if ( !acceptsURI( uri ) )
// throw new XMLDBException( ErrorCodes.INVALID_URI, "INVALID_URI" );
// return null;
// }
private boolean parseURI( String uri ) {
StringTokenizer tokenizer = new StringTokenizer( uri, ":/?", true );
boolean found = false;
boolean remote = true;
StringBuffer coll = new StringBuffer();
StringBuffer database = new StringBuffer();
int state = -1;
for (int count=0; tokenizer.hasMoreTokens(); count++) {
String token = tokenizer.nextToken();
if ( count == 0 ) {
if ( !token.equals( "xmldb" ) )
return false;
}
else if ( count <= 3) {
if ( (count % 2) == 1 && token.equals( ":" ) )
; // ignore this token
else if ( (count % 2) == 0 ) {
if ( token.equals( "ozonexml" ) )
found = true;
}
}
else if ( count > 3 ) {
if ( count == 4 && !token.equals( "/") )
return false; // malformatted uri
else if ( count == 4 )
;
else if ( count == 5 && token.equals( "/" ) )
remote = true; // ok, remote datebase
else if ( count == 5 ) {
remote = false; // ok, local database
database.append( "/" );
database.append( token );
}
else if ( count == 6 && remote )
setProperty( DB_HOST, token ); // remote host found
else if ( count == 6 )
database.append( token );
else if ( count == 7 && remote && token.equals( ":" ) )
;
else if ( count == 7 )
database.append( token );
else if ( count == 8 && remote )
setProperty( DB_PORT, token ); // remote port found
else if ( count == 8 )
database.append( token );
else if ( count == 9 && token.equals( "?" ) )
;
else if ( count == 9 )
database.append( token );
else if ( token.equals( "?" ) )
;
else
coll.append( token );
}
}
if ( !remote )
if ( database.length() > 0 )
setProperty( DB_NAME, database.toString() );
else
return false;
if ( coll.length() > 0 )
setProperty( COL_NAME, coll.toString() );
return true;
}
}