Clover coverage report -
Coverage timestamp: Sun Nov 1 2009 23:08:24 UTC
file stats: LOC: 392   Methods: 19
NCLOC: 164   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
XMLResourceImpl.java 46.2% 67.6% 57.9% 61.1%
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: XMLResourceImpl.java 566457 2007-08-16 01:04:02Z vgritsenko $
 18    */
 19   
 20    package org.apache.xindice.client.xmldb.resources;
 21   
 22    import org.apache.xindice.core.FaultCodes;
 23    import org.apache.xindice.xml.SymbolTable;
 24    import org.apache.xindice.xml.TextWriter;
 25    import org.apache.xindice.xml.dom.DOMParser;
 26    import org.apache.xindice.xml.dom.DocumentImpl;
 27    import org.apache.xindice.xml.sax.SAXEventGenerator;
 28    import org.apache.xindice.xml.sax.SetContentHandler;
 29   
 30    import org.w3c.dom.Node;
 31    import org.xml.sax.ContentHandler;
 32    import org.xml.sax.ErrorHandler;
 33    import org.xml.sax.InputSource;
 34    import org.xml.sax.XMLReader;
 35    import org.xmldb.api.base.ErrorCodes;
 36    import org.xmldb.api.base.XMLDBException;
 37   
 38    import javax.xml.parsers.SAXParser;
 39    import javax.xml.parsers.SAXParserFactory;
 40    import java.io.StringReader;
 41   
 42    /**
 43    * XMLResourceImpl provides an implementation to handle XML resources
 44    * and convert easily between Text, DOM or SAX presentations.
 45    *
 46    * @version $Revision: 566457 $, $Date: 2007-08-15 18:04:02 -0700 (Wed, 15 Aug 2007) $
 47    */
 48    public class XMLResourceImpl implements org.xmldb.api.modules.XMLResource {
 49   
 50    private static final SAXParserFactory saxFactory;
 51   
 52    static {
 53  3 saxFactory = SAXParserFactory.newInstance();
 54  3 saxFactory.setNamespaceAware(true);
 55    }
 56   
 57    /**
 58    * This is a SAX feature that controls how namespaces are reported in SAX.
 59    * By default this feature is <em>on</em>.
 60    *
 61    * @see <a href="http://sax.sourceforge.net/?selected=namespaces">sax.sourceforge.net/?selected=namespaces</a>
 62    */
 63    public static final String SAX_NAMESPACES_FEATURE
 64    = SAXEventGenerator.SAX_NAMESPACES_FEATURE;
 65   
 66    /**
 67    * This is a SAX feature that controls how namespaces are reported in SAX.
 68    * By default this feature is <em>off</em>.
 69    *
 70    * @see <a href="http://sax.sourceforge.net/?selected=namespaces">sax.sourceforge.net/?selected=namespaces</a>
 71    */
 72    public static final String SAX_NAMESPACE_PREFIXES_FEATURE
 73    = SAXEventGenerator.SAX_NAMESPACE_PREFIXES_FEATURE;
 74   
 75   
 76    protected String id;
 77    protected String documentId;
 78    protected org.xmldb.api.base.Collection collection;
 79    protected String content;
 80    private SymbolTable symbols;
 81    private byte[] bytes;
 82   
 83    /*
 84    * State of the SAX_NAMESPACES_FEATURE. True indicates namespace URIs and unprefixed local
 85    * names for element and attribute names will be available.
 86    *
 87    * For SAX2 the default is true.
 88    */
 89    private boolean hasSaxNamespaces = true;
 90   
 91    /*
 92    * State of the SAX_NAMESPACE_PREFIXES_FEATURE. True indicates XML 1.0 names (with prefixes)
 93    * and attributes (including xmlns* attributes) will be available.
 94    *
 95    * For SAX2 the default is off.
 96    */
 97    private boolean hasSaxNamespacesPrefixes;
 98   
 99   
 100    /**
 101    * Constructor for the XMLResourceImpl object
 102    *
 103    * @param id The unique id associated with this resource
 104    * @param collection the parent collection for the resource
 105    */
 106  1233 public XMLResourceImpl(String id, org.xmldb.api.base.Collection collection) {
 107  1233 this(id, id, collection, null);
 108    }
 109   
 110    /**
 111    * Constructor for the XMLResourceImpl object
 112    *
 113    * @param id The unique id associated with this resource
 114    * @param documentId The parent document id associated with this resource
 115    * @param collection the parent collection for the resource
 116    */
 117  0 public XMLResourceImpl(String id, String documentId,
 118    org.xmldb.api.base.Collection collection) {
 119  0 this(id, documentId, collection, null);
 120    }
 121   
 122    /**
 123    * Constructor for the XMLResourceImpl object
 124    *
 125    * @param id The unique id associated with this resource
 126    * @param collection the parent collection for the resource
 127    * @param content the content to associate with the resource
 128    */
 129  0 public XMLResourceImpl(String id, org.xmldb.api.base.Collection collection,
 130    String content) {
 131  0 this(id, id, collection, content);
 132    }
 133   
 134    /**
 135    * Constructor for the XMLResourceImpl object
 136    *
 137    * @param id The unique id associated with this resource
 138    * @param documentId The parent document id associated with this resource
 139    * @param collection the parent collection for the resource
 140    * @param content the content to associate with the resource
 141    */
 142  1933 public XMLResourceImpl(String id, String documentId,
 143    org.xmldb.api.base.Collection collection, String content) {
 144  1933 this.collection = collection;
 145  1933 this.id = id;
 146  1933 this.documentId = documentId;
 147  1933 this.content = content;
 148    }
 149   
 150    /**
 151    * Constructor for the XMLResourceImpl object used in conjunction with
 152    * Wire Compression
 153    *
 154    * @param id The unique id associated with this resource
 155    * @param documentId The document identifier this resource belongs to
 156    * @param collection The parent collection for the resource
 157    * @param syms The collection's symbol table
 158    * @param bytes The content to associate with the resource
 159    */
 160  338 public XMLResourceImpl(String id, String documentId,
 161    org.xmldb.api.base.Collection collection, SymbolTable syms, byte[] bytes) {
 162  338 this.collection = collection;
 163  338 this.id = id;
 164  338 this.documentId = documentId;
 165  338 this.symbols = syms;
 166  338 this.bytes = bytes;
 167    }
 168   
 169    /**
 170    * Constructor for the XMLResourceImpl object used in conjunction with
 171    * Wire Compression
 172    *
 173    * @param id The unique id associated with this resource
 174    * @param collection the parent collection for the resource
 175    * @param syms The collection's symbol table
 176    * @param bytes the content to associate with the resource
 177    */
 178  0 public XMLResourceImpl(String id, org.xmldb.api.base.Collection collection,
 179    SymbolTable syms, byte[] bytes) {
 180  0 this(id, id, collection, syms, bytes);
 181    }
 182   
 183    /**
 184    * Allows SAX feature to be set for the SAX that is generated by
 185    * getContentAsSAX.
 186    *
 187    * @param feature feature name. Currently supported features are namespace
 188    * reporting features described in the specification.
 189    * @param value turn feature on or off.
 190    * @see <a href="http://sax.sourceforge.net/?selected=namespaces">sax.sourceforge.net/?selected=namespaces</a>
 191    */
 192  0 public void setSAXFeature(String feature, boolean value) {
 193   
 194  0 if (SAX_NAMESPACES_FEATURE.equals(feature)) {
 195   
 196  0 hasSaxNamespaces = value;
 197  0 } else if (SAX_NAMESPACE_PREFIXES_FEATURE.equals(feature)) {
 198   
 199  0 hasSaxNamespacesPrefixes = value;
 200    }
 201    }
 202   
 203    /**
 204    * Retrieves whether a SAX feature will be active for the
 205    * SAX generated by the getContentAsSAX() method.
 206    *
 207    * @param feature feature name. Currently supported features are namespace
 208    * reporting features described in the specification
 209    * @return whether the feature is on or off
 210    * @see <a href="http://sax.sourceforge.net/?selected=namespaces">sax.sourceforge.net/?selected=namespaces</a>
 211    * @noinspection SimplifiableIfStatement
 212    */
 213  0 public boolean getSAXFeature(String feature) {
 214   
 215  0 if (SAX_NAMESPACES_FEATURE.equals(feature)) {
 216   
 217  0 return hasSaxNamespaces;
 218  0 } else if (SAX_NAMESPACE_PREFIXES_FEATURE.equals(feature)) {
 219   
 220  0 return hasSaxNamespacesPrefixes;
 221    } else {
 222   
 223  0 return false;
 224    }
 225    }
 226   
 227    /**
 228    * Returns the unique id for the parent document to this <code>Resource</code>
 229    * or null if the <code>Resource</code> does not have a parent document.
 230    *
 231    * @return the id for the parent document of this <code>Resource</code> or
 232    * null if there is no parent document for this <code>Resource</code>.
 233    * @exception XMLDBException
 234    */
 235  60 public String getDocumentId() throws XMLDBException {
 236  60 return documentId;
 237    }
 238   
 239    /**
 240    * Sets the Content attribute of the XMLResourceImpl object. The value being
 241    * set must be String object containing well formed XML text.
 242    *
 243    * @param value The new Content value
 244    * @exception XMLDBException
 245    */
 246  1233 public void setContent(Object value) throws XMLDBException {
 247  1233 if (value == null) {
 248  3 throw new XMLDBException(ErrorCodes.INVALID_RESOURCE);
 249  1230 } else if (value instanceof String) {
 250  1230 this.content = (String) value;
 251  1230 this.bytes = null;
 252    } else {
 253  0 throw new XMLDBException(ErrorCodes.WRONG_CONTENT_TYPE);
 254    }
 255    }
 256   
 257    /**
 258    * Sets the content of the resource from a DOM Node.
 259    *
 260    * @param content Node containing the new content.
 261    * @exception XMLDBException
 262    */
 263  3 public void setContentAsDOM(Node content) throws XMLDBException {
 264  3 if (content != null) {
 265  3 this.content = TextWriter.toString(content);
 266  3 this.bytes = null;
 267    } else {
 268  0 throw new XMLDBException(ErrorCodes.INVALID_RESOURCE);
 269    }
 270    }
 271   
 272    /**
 273    * setContentAsSAX returns a SAX ContentHandler that can be used to set the
 274    * content of the resource.
 275    *
 276    * @return The ContentHandler that is used to insert data into the database.
 277    * @exception XMLDBException
 278    */
 279  0 public ContentHandler setContentAsSAX() throws XMLDBException {
 280  0 return new SetContentHandler(this);
 281    }
 282   
 283    /**
 284    * Gets the parent Collection instance for this resource
 285    *
 286    * @return The ParentCollection value
 287    * @exception XMLDBException
 288    */
 289  0 public org.xmldb.api.base.Collection getParentCollection() throws XMLDBException {
 290  0 return collection;
 291    }
 292   
 293    /**
 294    * Returns the resource type for this Resource.
 295    * <p />
 296    * XML:DB defined resource types are: <p />
 297    * XMLResource - all XML data stored in the database<br />
 298    * BinaryResource - Binary blob data stored in the database<br />
 299    *
 300    * @return the resource type for the Resource.
 301    * @exception XMLDBException
 302    */
 303  0 public String getResourceType() throws XMLDBException {
 304  0 return RESOURCE_TYPE;
 305    }
 306   
 307    /**
 308    * Gets the Id attribute of the XMLResourceImpl object
 309    *
 310    * @return The Id value
 311    * @exception XMLDBException
 312    */
 313  3405 public String getId() throws XMLDBException {
 314  3405 return id;
 315    }
 316   
 317    /**
 318    * Gets the content of the XMLResourceImpl object
 319    *
 320    * @return The Content value
 321    * @exception XMLDBException
 322    */
 323  1674 public Object getContent() throws XMLDBException {
 324  1674 if (bytes != null) {
 325  20 DocumentImpl doc = new DocumentImpl(bytes, symbols, null);
 326  20 return TextWriter.toString(doc);
 327    } else {
 328  1654 return content;
 329    }
 330    }
 331   
 332    /**
 333    * Returns the content of the resource as a DOM Node.
 334    *
 335    * @return The content as a DOM node
 336    * @exception XMLDBException
 337    */
 338  1059 public Node getContentAsDOM() throws XMLDBException {
 339  1059 try {
 340  1059 if (bytes != null) {
 341  0 return new DocumentImpl(bytes, symbols, null);
 342    } else {
 343  1059 return DOMParser.toDocument(content);
 344    }
 345    } catch (Exception e) {
 346  0 throw FaultCodes.createXMLDBException(e);
 347    }
 348    }
 349   
 350    /**
 351    * getContentAsSAX enables retrieving of the content via the use of a SAX
 352    * ContentHandler.
 353    *
 354    * @param handler The ContentHandler to use.
 355    * @exception XMLDBException
 356    */
 357  12 public void getContentAsSAX(ContentHandler handler) throws XMLDBException {
 358    // TODO: should probably use JAXP
 359  12 try {
 360  12 if (bytes != null) {
 361  8 SAXEventGenerator events = new SAXEventGenerator(symbols, bytes);
 362   
 363  8 events.setFeature(SAX_NAMESPACES_FEATURE, hasSaxNamespaces);
 364  8 events.setFeature(SAX_NAMESPACE_PREFIXES_FEATURE, hasSaxNamespacesPrefixes);
 365  8 events.setContentHandler(handler);
 366  8 if (handler instanceof ErrorHandler) {
 367  0 events.setErrorHandler((ErrorHandler) handler);
 368    }
 369   
 370  8 events.start();
 371  4 } else if (content != null) {
 372  4 SAXParser sp = saxFactory.newSAXParser();
 373  4 XMLReader xr = sp.getXMLReader();
 374   
 375  4 xr.setFeature(SAX_NAMESPACES_FEATURE, hasSaxNamespaces);
 376  4 xr.setFeature(SAX_NAMESPACE_PREFIXES_FEATURE, hasSaxNamespacesPrefixes);
 377  4 xr.setContentHandler(handler);
 378  4 if (handler instanceof ErrorHandler) {
 379  0 xr.setErrorHandler((ErrorHandler) handler);
 380    }
 381   
 382  4 xr.parse(new InputSource(new StringReader(content)));
 383    }
 384    } catch (Exception e) {
 385  0 throw FaultCodes.createXMLDBException(e);
 386    }
 387    }
 388   
 389  409 public void setId(String name) {
 390  409 this.id = name;
 391    }
 392    }