Clover coverage report -
Coverage timestamp: Sun Nov 1 2009 23:08:24 UTC
file stats: LOC: 366   Methods: 9
NCLOC: 112   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
XindiceCollection.java 64.3% 72.1% 88.9% 72.7%
coverage coverage
 1    /*
 2    * Licensed to the Apache Software Foundation (ASF) under one or more
 3    * contributor license agreements. See the NOTICE file distributed with
 4    * this work for additional information regarding copyright ownership.
 5    * The ASF licenses this file to You under the Apache License, Version 2.0
 6    * (the "License"); you may not use this file except in compliance with
 7    * the License. You may obtain a copy of the License at
 8    *
 9    * http://www.apache.org/licenses/LICENSE-2.0
 10    *
 11    * Unless required by applicable law or agreed to in writing, software
 12    * distributed under the License is distributed on an "AS IS" BASIS,
 13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14    * See the License for the specific language governing permissions and
 15    * limitations under the License.
 16    *
 17    * $Id: XindiceCollection.java 643240 2008-04-01 01:53:39Z natalia $
 18    */
 19   
 20    package org.apache.xindice.client.xmldb;
 21   
 22    import org.apache.xindice.client.xmldb.resources.XMLResourceImpl;
 23    import org.apache.xindice.client.xmldb.resources.BinaryResourceImpl;
 24    import org.apache.xindice.client.xmldb.services.CollectionManagementServiceImpl;
 25    import org.apache.xindice.client.xmldb.services.MetaService;
 26    import org.apache.xindice.client.xmldb.services.TextQueryServiceImpl;
 27    import org.apache.xindice.client.xmldb.services.XPathQueryServiceImpl;
 28    import org.apache.xindice.client.xmldb.services.XUpdateQueryServiceImpl;
 29    import org.apache.xindice.client.xmldb.services.ResourceManagementService;
 30    import org.apache.xindice.client.xmldb.services.ResourceManagementServiceImpl;
 31    import org.apache.xindice.core.FaultCodes;
 32    import org.apache.xindice.core.meta.MetaData;
 33    import org.apache.xindice.xml.NodeSource;
 34   
 35    import org.w3c.dom.Document;
 36    import org.xmldb.api.base.Collection;
 37    import org.xmldb.api.base.ErrorCodes;
 38    import org.xmldb.api.base.Resource;
 39    import org.xmldb.api.base.ResourceSet;
 40    import org.xmldb.api.base.Service;
 41    import org.xmldb.api.base.XMLDBException;
 42   
 43    import java.util.Enumeration;
 44    import java.util.Hashtable;
 45   
 46    /**
 47    * This is a base XML:DB Collection that is extended by all Xindice
 48    * XML:DB API drivers. It includes the functionality that is common to
 49    * all drivers and abstract methods for all other driver specific methods.
 50    * This enables the implementation of drivers in just two classes and the
 51    * use of common services implementations.
 52    *
 53    * @author <a href="mailto:kstaken@xmldatabases.org">Kimbro Staken</a>
 54    * @author <a href="mailto:james.bates@amplexor.com">James Bates</a>
 55    * @version $Revision: 643240 $, $Date: 2008-04-01 01:53:39 +0000 (Tue, 01 Apr 2008) $
 56    */
 57    public abstract class XindiceCollection extends CommonConfigurable implements Collection {
 58   
 59    /**
 60    * Xindice query result meta-info namespace
 61    */
 62    public static final String QUERY_NS = NodeSource.SOURCE_NS;
 63   
 64    /**
 65    * Instantiated named services map
 66    */
 67    protected final Hashtable services = new Hashtable();
 68   
 69    /**
 70    * Path to the collection on target server
 71    */
 72    protected final String collPath;
 73   
 74    /**
 75    * Creates new <code>CollectionImpl</code> instance representing connection
 76    * to server collection.
 77    *
 78    * @exception XMLDBException thrown if a connection could not be established,
 79    * because of URL syntax errors, or connection failure, or if no
 80    * collection with path <code>collPath</code> could be located.
 81    */
 82  3418 public XindiceCollection(String collPath) throws XMLDBException {
 83  3418 this.collPath = collPath.endsWith("/") ? collPath.substring(0, collPath.length() - 1) : collPath;
 84   
 85    // Register all services supported by this collection implementation.
 86  3418 final XPathQueryServiceImpl xpath = new XPathQueryServiceImpl();
 87    // xpath.setSymbolDeserializer(syms);
 88  3418 registerService(xpath);
 89   
 90  3418 final XUpdateQueryServiceImpl xupdate = new XUpdateQueryServiceImpl();
 91  3418 registerService(xupdate);
 92   
 93  3418 final TextQueryServiceImpl text = new TextQueryServiceImpl();
 94  3418 registerService(text);
 95   
 96    // TODO if (this.col.isMetaEnabled()) {
 97  3418 final MetaService meta = new MetaService();
 98  3418 registerService(meta);
 99   
 100  3418 final ResourceManagementService rms = new ResourceManagementServiceImpl();
 101  3418 registerService(rms);
 102   
 103  3418 try {
 104  3418 final CollectionManagementServiceImpl manager = new CollectionManagementServiceImpl();
 105  3418 registerService(manager);
 106   
 107    // CollectionManagementServiceImpl provides both standard access as a
 108    // CollectionManagementService and Xindice specific access as a
 109    // CollectionManager and DatabaseInstanceManager.
 110    // We need to register it explicitly to make it available
 111  3418 services.put("CollectionManager" + manager.getVersion(), manager);
 112  3418 services.put("DatabaseInstanceManager" + manager.getVersion(), manager);
 113    } catch (Exception e) {
 114  0 throw FaultCodes.createXMLDBException(e);
 115    }
 116    }
 117   
 118    /**
 119    * Provides a list of all services known to the collection. If no services
 120    * are known an empty list is returned.
 121    *
 122    * @return An array of registered <code>Service</code> implementations.
 123    * @exception XMLDBException with expected error codes.<br />
 124    * <code>ErrorCodes.VENDOR_ERROR</code> for any vendor
 125    * specific errors that occur.<br />
 126    * <code>ErrorCodes.COLLECTION_CLOSED</code> if the <code>close</code>
 127    * method has been called on the <code>Collection</code><br />
 128    */
 129  0 public Service[] getServices() throws XMLDBException {
 130  0 checkOpen();
 131   
 132  0 Enumeration e = services.elements();
 133  0 Service[] result = new Service[services.size()];
 134   
 135  0 int i = 0;
 136  0 while (e.hasMoreElements()) {
 137  0 result[i] = (Service) e.nextElement();
 138  0 i++;
 139    }
 140   
 141  0 return result;
 142    }
 143   
 144    /**
 145    * Returns a <code>Service</code> instance for the requested service name and version. If
 146    * no <code>Service</code> exists for those parameters a null value is returned.
 147    *
 148    * @param name Description of Parameter
 149    * @param version Description of Parameter
 150    * @return the Service instance or null if no Service could be found.
 151    * @exception XMLDBException with expected error codes.<br />
 152    * <code>ErrorCodes.VENDOR_ERROR</code> for any vendor
 153    * specific errors that occur.<br />
 154    * <code>ErrorCodes.COLLECTION_CLOSED</code> if the <code>close</code>
 155    * method has been called on the <code>Collection</code><br />
 156    */
 157  1179 public org.xmldb.api.base.Service getService(String name, String version) throws XMLDBException {
 158  1179 checkOpen();
 159   
 160  1179 return (Service) services.get(name + version);
 161    }
 162   
 163    /**
 164    * Registers a new Service with this Collection.
 165    *
 166    * @param service Description of Parameter
 167    * @exception XMLDBException
 168    */
 169  20508 public void registerService(org.xmldb.api.base.Service service) throws XMLDBException {
 170   
 171  20508 service.setCollection(this);
 172  20508 services.put(service.getName() + service.getVersion(), service);
 173    }
 174   
 175    /**
 176    * Checks if the collection is still open. Only open collections are safe
 177    * to work with.
 178    *
 179    * @return whether the collection is still open
 180    */
 181    public abstract boolean isOpen();
 182   
 183    /**
 184    * Throws an exception if collection is no longer open
 185    *
 186    * @exception XMLDBException thrown if collection is closed
 187    */
 188  4866 protected void checkOpen() throws XMLDBException {
 189  4866 if (!isOpen()) {
 190  0 throw new XMLDBException(ErrorCodes.COLLECTION_CLOSED);
 191    }
 192    }
 193   
 194    /**
 195    * Returns the name associated with the <code>Collection</code> instance.
 196    *
 197    * @return the name of the object.
 198    */
 199  15 public String getName() {
 200  15 return collPath.substring(collPath.lastIndexOf('/') + 1);
 201    }
 202   
 203    /**
 204    * Returns complete path to collection
 205    *
 206    * @return the collection path
 207    */
 208  254 public String getCanonicalName() {
 209  254 return collPath;
 210    }
 211   
 212    /**
 213    * Returns XML:DB URI that would retrieve this collection
 214    *
 215    * @return a complete XML:DB URI
 216    */
 217    public abstract String getURI();
 218   
 219    /**
 220    * Constructs a new resource that will belong in this collection.
 221    *
 222    * Only XML resources are supported. To save the resource to the database, you
 223    * must first set the resource's XML data, and then call
 224    * <code>storeResource()</code>.
 225    *
 226    * @param name Name for the new resource. If it is empty or <code>null</code>, a name
 227    * will be assigned when storing the resource in the database.
 228    * @param type Type must be either <code>XMLResource</code> or <code>BinaryResource</code>.
 229    * @exception XMLDBException thrown in case of an invalid resource type or name
 230    */
 231  1245 public Resource createResource(String name, String type) throws XMLDBException {
 232  1245 if (!"XMLResource".equals(type) && !"BinaryResource".equals(type)) {
 233  0 throw new XMLDBException(ErrorCodes.UNKNOWN_RESOURCE_TYPE,
 234    "Only XMLResource and BinaryResource supported");
 235    }
 236   
 237  1245 if (name == null || name.length() == 0) {
 238    // fulfill contract stating
 239    // "If id is null or its value is empty then an id is generated by calling createId()."
 240  12 name = createId();
 241  1233 } else if (name.indexOf('/') != -1) {
 242  0 throw new XMLDBException(ErrorCodes.INVALID_RESOURCE,
 243    "Name cannot contain '/'");
 244    }
 245   
 246  1245 if ("XMLResource".equals(type)) {
 247  1233 return new XMLResourceImpl(name, this);
 248    } else {
 249  12 return new BinaryResourceImpl(name, this, null);
 250    }
 251    }
 252   
 253    /**
 254    * Queries the entire collection and resturns the result
 255    *
 256    * @param queryLang <code>XUpdate</code> or <code>XPath</code>
 257    * @param query the text of the query statement
 258    * @param nsMap namespace bindings to use when evaluating query
 259    * @return set containing result of query
 260    * @throws XMLDBException thrown in case of invalid query or other error
 261    */
 262  351 public ResourceSet query(String queryLang, String query, Hashtable nsMap) throws XMLDBException {
 263  351 return query(null, queryLang, query, nsMap);
 264    }
 265   
 266    /**
 267    * Queries a specific document or the entire collection and returns result
 268    *
 269    * @param name name of document to query, or <code>null</code> to query
 270    * entire collection.
 271    * @param queryLang <code>XUpdate</code> or <code>XPath</code>.
 272    * @param query the text of the query statement
 273    * @param nsMap namespace bindings to use when evaluating query
 274    * @return set containing result of query
 275    * @throws XMLDBException thrown in case of invalid query or other error
 276    */
 277    public abstract ResourceSet query(String name, String queryLang, String query, Hashtable nsMap) throws XMLDBException;
 278   
 279    /**
 280    * Creates a new child collection in this collection
 281    *
 282    * @param name The name for new child collection
 283    * @return object representing newly created collection
 284    * @exception XMLDBException thrown if collection creation fails for some
 285    * reason
 286    */
 287    public abstract Collection createCollection(String name) throws XMLDBException;
 288   
 289    /**
 290    * Creates a new child collection.
 291    * If path is a simple name (no '/' character), collection will be created
 292    * in this collection. If path contains more then one element, collection will be
 293    * created in the corresponding sub-collection of this collection (which must
 294    * already exist).
 295    *
 296    * @param path The path for new child collection
 297    * @return object representing newly created collection
 298    * @exception XMLDBException thrown if collection creation fails for some
 299    * reason
 300    */
 301    public abstract Collection createCollection(String path, Document configuration) throws XMLDBException;
 302   
 303    /**
 304    * Removes child collection from this collection
 305    *
 306    * @param name name of child collection
 307    * @exception XMLDBException thrown if collection creation fails for some
 308    * reason
 309    */
 310    public abstract void removeCollection(String name) throws XMLDBException;
 311   
 312    /**
 313    * Returns a list of all indexers for this collection.
 314    *
 315    * @return the list of indexers
 316    * @exception XMLDBException
 317    */
 318    public abstract String[] listIndexers() throws XMLDBException;
 319   
 320    /**
 321    * Creates a new Indexer for this collection.
 322    *
 323    * @param configuration The configuration to use for this indexer.
 324    * @exception XMLDBException
 325    */
 326    public abstract void createIndexer(Document configuration) throws XMLDBException;
 327   
 328    /**
 329    * Drops the indexer from the collection
 330    *
 331    * @param name The name of the indexer to drop.
 332    * @exception XMLDBException
 333    */
 334    public abstract void dropIndexer(String name) throws XMLDBException;
 335   
 336    /**
 337    * Shutsdown the Database instance
 338    *
 339    * @exception XMLDBException
 340    */
 341    public abstract void shutdown() throws XMLDBException;
 342   
 343    /**
 344    * Returns {@link MetaData} object for the specified document or the current
 345    * collection
 346    * @param name name of document to get meta, or <code>null</code> to get meta
 347    * of the collection.
 348    * @return Requested meta data object
 349    * @exception XMLDBException thrown if unable to obtain meta information
 350    */
 351    public abstract MetaData getMetaData(String name) throws XMLDBException;
 352   
 353    /**
 354    * Updates {@link MetaData} object for the specified document or the current
 355    * collection
 356    * @param name name of document to update meta, or <code>null</code> to update meta
 357    * of the collection.
 358    * @param meta meta data object
 359    * @exception XMLDBException thrown if unable to obtain meta information
 360    */
 361    public abstract void setMetaData(String name, MetaData meta) throws XMLDBException;
 362   
 363    public abstract void insertResource(Resource resource) throws XMLDBException;
 364   
 365    public abstract void updateResource(Resource resource) throws XMLDBException;
 366    }