1 package org.xmldb.api; 2 3 55 56 import org.xmldb.api.base.*; 57 58 import java.util.*; 59 60 68 public class DatabaseManager 69 { 70 protected static final String URI_PREFIX = "xmldb:"; 71 static Properties properties = new Properties(); 72 static Hashtable databases = new Hashtable(); 73 74 83 public static Database[] getDatabases () { 84 Enumeration e = databases.elements(); 85 Database[] result = new Database[databases.size()]; 86 87 int i = 0; 88 while (e.hasMoreElements()) { 89 result[i] = (Database) e.nextElement(); 90 i++; 91 } 92 93 return result; 94 } 95 96 107 public static void registerDatabase (Database database) throws XMLDBException { 108 if ((database.getName() == null) || (database.getName().equals(""))) { 109 throw new XMLDBException(ErrorCodes.INVALID_DATABASE); 110 } 111 112 databases.put(database.getName(), database); 113 } 114 115 125 public static void deregisterDatabase (Database database) 126 throws XMLDBException { 127 databases.remove(database.getName()); 128 } 129 130 152 public static org.xmldb.api.base.Collection getCollection (String uri) 153 throws XMLDBException { 154 Database db = getDatabase(uri); 155 156 uri = stripURIPrefix(uri); 157 158 return (org.xmldb.api.base.Collection) db.getCollection(uri); 159 } 160 161 175 public static String getConformanceLevel (String uri) throws XMLDBException { 176 Database database = getDatabase(uri); 177 return database.getConformanceLevel(); 178 } 179 180 186 public static String getProperty (String name) { 187 return properties.getProperty(name); 188 } 189 190 196 public static void setProperty (String name, String value) { 197 properties.put(name, value); 198 } 199 200 207 protected static Database getDatabase(String uri) throws XMLDBException { 208 if (!uri.startsWith(URI_PREFIX)) { 209 throw new XMLDBException(ErrorCodes.INVALID_URI); 210 } 211 212 int end = uri.indexOf(":", URI_PREFIX.length()); 213 if (end == -1) { 214 throw new XMLDBException(ErrorCodes.INVALID_URI); 215 } 216 217 String databaseName = uri.substring(URI_PREFIX.length(), end); 218 219 Database db = (Database) databases.get(databaseName); 220 if (db == null) { 221 throw new XMLDBException(ErrorCodes.NO_SUCH_DATABASE); 222 } 223 224 return db; 225 } 226 227 234 protected static String stripURIPrefix(String uri) throws XMLDBException { 235 if (!uri.startsWith(URI_PREFIX)) { 236 throw new XMLDBException(ErrorCodes.INVALID_URI); 237 } 238 239 String dbURI = uri.substring(URI_PREFIX.length(), uri.length()); 240 return dbURI; 241 } 242 } 243 | Popular Tags |