001: // You can redistribute this software and/or modify it under the terms of
002: // the Ozone Library License version 1 published by ozone-db.org.
003: //
004: // The original code and portions created by SMB are
005: // Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
006: //
007: // $Id: DatabaseImpl.java,v 1.1 2001/12/18 11:03:24 per_nyfelt Exp $
008:
009: package org.ozoneDB.xml.cli;
010:
011: import java.util.StringTokenizer;
012:
013: import org.xmldb.api.DatabaseManager;
014: import org.xmldb.api.base.Collection;
015: import org.xmldb.api.base.Configurable;
016: import org.xmldb.api.base.Database;
017: import org.xmldb.api.base.ErrorCodes;
018: import org.xmldb.api.base.XMLDBException;
019:
020: import org.ozoneDB.ExternalDatabase;
021:
022: /**
023: *
024: * @author <a href="http://www.smb-tec.com">SMB</a>
025: * @version $Revision: 1.1 $
026: */
027: public class DatabaseImpl extends AbstractConfigurable implements
028: Database {
029:
030: //
031: // constants
032: //
033:
034: private static String DB_HOST = "DB_HOST";
035: private static String DB_NAME = "DB_NAME";
036: private static String DB_PORT = "DB_PORT";
037: private static String COL_NAME = "COL_NAME";
038:
039: public static String CONFORMANCE_LEVEL = "0";
040: public static String DATABASE_NAME = "ozoneXML";
041:
042: // automatically register the database when the class is loaded
043: static {
044: try {
045: org.xmldb.api.DatabaseManager
046: .registerDatabase(new DatabaseImpl());
047: } catch (Exception e) {
048: e.printStackTrace();
049: }
050: }
051:
052: /**
053: * Zero-argument constructor.
054: */
055: public DatabaseImpl() {
056: super ();
057: }
058:
059: /**
060: */
061: public String getName() throws XMLDBException {
062: return DATABASE_NAME;
063: }
064:
065: /**
066: */
067: public Collection getCollection(String uri) throws XMLDBException {
068: // we don't know how to handle this uri
069: if (!acceptsURI(uri))
070: throw new XMLDBException(ErrorCodes.INVALID_URI,
071: "INVALID_URI");
072:
073: Collection collection = null;
074: try {
075: ExternalDatabase db = null;
076: StringBuffer ozoneDB = new StringBuffer();
077: if (getProperty(DB_HOST) == null)
078: ozoneDB.append("ozonedb:local:").append(
079: getProperty(DB_NAME));
080: else
081: ozoneDB.append("ozonedb:remote://").append(
082: getProperty(DB_HOST)).append(":").append(
083: getProperty(DB_PORT));
084:
085: System.out
086: .println("DatabaseImpl.getCollection() - Remote DB: "
087: + getProperty(DB_HOST)
088: + " "
089: + getProperty(DB_PORT));
090: System.out
091: .println("DatabaseImpl.getCollection() - Local DB: "
092: + getProperty(DB_NAME));
093: System.out
094: .println("DatabaseImpl.getCollection() - Load Collection: "
095: + getProperty(COL_NAME));
096:
097: db = ExternalDatabase.openDatabase(ozoneDB.toString());
098: if (db == null)
099: throw new XMLDBException(ErrorCodes.INVALID_DATABASE,
100: "INVALID_DATABASE");
101:
102: db.reloadClasses();
103:
104: collection = CollectionImpl.forName(db,
105: getProperty(COL_NAME));
106:
107: if (collection == null)
108: throw new XMLDBException(ErrorCodes.INVALID_COLLECTION,
109: "INVALID_COLLECTION");
110:
111: } catch (XMLDBException e) {
112: // re-throw this exception
113: throw e;
114: } catch (Exception e) {
115: // encapsulate all other exceptions
116: throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e
117: .toString());
118: }
119: return collection;
120: }
121:
122: /**
123: */
124: public boolean acceptsURI(String uri) throws XMLDBException {
125: return parseURI(uri);
126: }
127:
128: /**
129: */
130: public String getConformanceLevel() throws XMLDBException {
131: return CONFORMANCE_LEVEL;
132: }
133:
134: /**
135: * NON API CONFORM EXTENSION!
136: */
137: // public Collection createRootCollection( String uri, String collectionName )
138: // throws XMLDBException {
139: // // we don't know how to handle this uri
140: // if ( !acceptsURI( uri ) )
141: // throw new XMLDBException( ErrorCodes.INVALID_URI, "INVALID_URI" );
142: // return null;
143: // }
144: private boolean parseURI(String uri) {
145: StringTokenizer tokenizer = new StringTokenizer(uri, ":/?",
146: true);
147: boolean found = false;
148: boolean remote = true;
149: StringBuffer coll = new StringBuffer();
150: StringBuffer database = new StringBuffer();
151: int state = -1;
152: for (int count = 0; tokenizer.hasMoreTokens(); count++) {
153: String token = tokenizer.nextToken();
154: if (count == 0) {
155: if (!token.equals("xmldb"))
156: return false;
157: } else if (count <= 3) {
158: if ((count % 2) == 1 && token.equals(":"))
159: ; // ignore this token
160: else if ((count % 2) == 0) {
161: if (token.equals("ozonexml"))
162: found = true;
163: }
164: } else if (count > 3) {
165: if (count == 4 && !token.equals("/"))
166: return false; // malformatted uri
167: else if (count == 4)
168: ;
169: else if (count == 5 && token.equals("/"))
170: remote = true; // ok, remote datebase
171: else if (count == 5) {
172: remote = false; // ok, local database
173: database.append("/");
174: database.append(token);
175: } else if (count == 6 && remote)
176: setProperty(DB_HOST, token); // remote host found
177: else if (count == 6)
178: database.append(token);
179: else if (count == 7 && remote && token.equals(":"))
180: ;
181: else if (count == 7)
182: database.append(token);
183: else if (count == 8 && remote)
184: setProperty(DB_PORT, token); // remote port found
185: else if (count == 8)
186: database.append(token);
187: else if (count == 9 && token.equals("?"))
188: ;
189: else if (count == 9)
190: database.append(token);
191: else if (token.equals("?"))
192: ;
193: else
194: coll.append(token);
195: }
196: }
197:
198: if (!remote)
199: if (database.length() > 0)
200: setProperty(DB_NAME, database.toString());
201: else
202: return false;
203:
204: if (coll.length() > 0)
205: setProperty(COL_NAME, coll.toString());
206:
207: return true;
208: }
209:
210: }
|