Clover coverage report -
Coverage timestamp: Sun Nov 1 2009 23:08:24 UTC
file stats: LOC: 210   Methods: 10
NCLOC: 106   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ResourceSetImpl.java 50% 38.3% 40% 40.3%
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: ResourceSetImpl.java 541508 2007-05-25 01:54:12Z vgritsenko $
 18    */
 19   
 20    package org.apache.xindice.client.xmldb;
 21   
 22    import org.apache.xindice.client.xmldb.resources.XMLResourceImpl;
 23    import org.apache.xindice.xml.NodeSource;
 24    import org.apache.xindice.xml.SymbolTable;
 25    import org.apache.xindice.xml.TextWriter;
 26    import org.apache.xindice.xml.dom.DOMCompressor;
 27    import org.apache.xindice.xml.dom.DocumentImpl;
 28   
 29    import org.w3c.dom.Document;
 30    import org.w3c.dom.Element;
 31    import org.w3c.dom.Node;
 32    import org.w3c.dom.NodeList;
 33    import org.xmldb.api.base.Resource;
 34    import org.xmldb.api.base.ResourceIterator;
 35    import org.xmldb.api.base.ResourceSet;
 36    import org.xmldb.api.base.XMLDBException;
 37    import org.xmldb.api.modules.XMLResource;
 38   
 39    import java.util.ArrayList;
 40    import java.util.Collections;
 41    import java.util.List;
 42   
 43    /**
 44    * <code>ResourceSet</code> contains a set of resources as returned from a query
 45    * or other operation.
 46    *
 47    * @version $Revision: 541508 $, $Date: 2007-05-24 18:54:12 -0700 (Thu, 24 May 2007) $
 48    */
 49    public class ResourceSetImpl implements ResourceSet {
 50   
 51    public static final String RESOURCE_SET_NS = "http://www.xmldb.org/xapi/ResourceSet";
 52   
 53    protected List resources;
 54    protected org.xmldb.api.base.Collection collection;
 55    private SymbolTable symbols;
 56    private byte[] bytes;
 57   
 58   
 59  357 public ResourceSetImpl(org.xmldb.api.base.Collection collection,
 60    Document doc) throws XMLDBException {
 61  357 this.collection = collection;
 62   
 63  357 if (doc != null) {
 64  357 initResources(doc);
 65    } else {
 66  0 resources = Collections.synchronizedList(new ArrayList());
 67    }
 68    }
 69   
 70  0 public ResourceSetImpl(org.xmldb.api.base.Collection collection,
 71    SymbolTable symbols, byte[] bytes) throws XMLDBException {
 72  0 this.collection = collection;
 73  0 this.symbols = symbols;
 74  0 this.bytes = bytes;
 75   
 76  0 initResources(new DocumentImpl(bytes, symbols, null));
 77    }
 78   
 79  357 protected void initResources(Document document) throws XMLDBException {
 80  357 NodeList nodes = document.getDocumentElement().getChildNodes();
 81  357 this.resources = Collections.synchronizedList(new ArrayList(nodes.getLength()));
 82   
 83  357 int i = 0;
 84  357 while (i < nodes.getLength()) {
 85  531 Node n = nodes.item(i);
 86   
 87  531 String documentId = null;
 88  531 if (n instanceof Element) {
 89  531 documentId = ((Element) n).getAttributeNS(NodeSource.SOURCE_NS, NodeSource.SOURCE_KEY);
 90    }
 91   
 92  531 XMLResource resource;
 93  531 if (bytes != null) {
 94  0 DocumentImpl doc = new DocumentImpl();
 95  0 doc.setSymbols(symbols);
 96  0 doc.importNode(n, true);
 97  0 doc.appendChild(n);
 98  0 byte[] b = DOMCompressor.compress(doc, symbols);
 99  0 resource = new XMLResourceImpl(null, documentId, collection, symbols, b);
 100    } else {
 101  531 resource = new XMLResourceImpl(null, documentId, collection, TextWriter.toString(n));
 102    }
 103   
 104  531 i++;
 105  531 resources.add(resource);
 106    }
 107    }
 108   
 109    /**
 110    * Returns an iterator over all <code>Resource</code> instances stored in
 111    * the set.
 112    *
 113    * @return a ResourceIterator over all <code>Resource</code> instances in the
 114    * set.
 115    * @exception XMLDBException
 116    */
 117  0 public ResourceIterator getIterator() throws XMLDBException {
 118  0 return new ResourceIteratorImpl(resources);
 119    }
 120   
 121    /**
 122    * Returns the <code>Resource</code> instance stored at the index specified
 123    * by <code>index</code>.
 124    *
 125    * @param index the index of the resource to retrieve.
 126    * @return the <code>Resource</code> instance.
 127    * @exception XMLDBException
 128    */
 129  282 public Resource getResource(long index) throws XMLDBException {
 130  282 return (XMLResource) resources.get((int) index);
 131    }
 132   
 133    /**
 134    * Returns the number of resources contained in the set.
 135    *
 136    * @return the number of <code>Resource</code> instances in the set.
 137    * @exception XMLDBException
 138    */
 139  177 public long getSize() throws XMLDBException {
 140  177 return resources.size();
 141    }
 142   
 143    /**
 144    * Adds a <code>Resource</code> instance to the set.
 145    *
 146    * @exception XMLDBException
 147    */
 148  0 public void addResource(Resource res) throws XMLDBException {
 149  0 resources.add(res);
 150    }
 151   
 152    /**
 153    * Removes all <code>Resource</code> instances from the set.
 154    *
 155    * @exception XMLDBException
 156    */
 157  0 public void clear() throws XMLDBException {
 158  0 resources.clear();
 159    }
 160   
 161    /**
 162    * Removes the <code>Resource</code> located at <code>index</code> from the
 163    * set.
 164    *
 165    * @param index The index of the <code>Resource</code> instance to remove.
 166    * @exception XMLDBException
 167    */
 168  0 public void removeResource(long index) throws XMLDBException {
 169  0 resources.remove((int) index);
 170    }
 171   
 172    /**
 173    * Returns a <code>Resource</code> containing an XML representation of all
 174    * resources stored in the set.
 175    *
 176    * @return A <code>Resource</code> instance containing an XML representation
 177    * of all set members.
 178    * @exception XMLDBException
 179    */
 180  0 public Resource getMembersAsResource() throws XMLDBException {
 181    // This impl works but it would be nice if we just got the result set from
 182    // the server in this format instead of having to build it. Right now it's
 183    // pretty innefficient
 184  0 Document doc = new DocumentImpl();
 185   
 186  0 Element set = doc.createElementNS(RESOURCE_SET_NS, "xapi:resourceSet");
 187  0 set.setAttributeNS(RESOURCE_SET_NS, "xapi:collectionURI",
 188    "xmldb:xindice://" + ((XindiceCollection) collection).getCanonicalName());
 189  0 set.setAttribute("xmlns:xapi", RESOURCE_SET_NS);
 190  0 doc.appendChild(set);
 191   
 192  0 int i = 0;
 193  0 while (i < resources.size()) {
 194  0 XMLResource res = (XMLResource) resources.get(i);
 195  0 Element resource = doc.createElementNS(RESOURCE_SET_NS,
 196    "xapi:resource");
 197  0 resource.setAttributeNS(RESOURCE_SET_NS, "xapi:documentID",
 198    res.getDocumentId());
 199   
 200  0 resource.appendChild(doc.importNode(
 201    ((Document) res.getContentAsDOM()).getDocumentElement(), true));
 202   
 203  0 set.appendChild(resource);
 204   
 205  0 i++;
 206    }
 207   
 208  0 return new XMLResourceImpl(null, null, collection, TextWriter.toString(doc));
 209    }
 210    }