Clover coverage report - dom4j - 1.5
Coverage timestamp: vr sep 3 2004 20:47:03 GMT+01:00
file stats: LOC: 306   Methods: 11
NCLOC: 185   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DOMReader.java 82,4% 78% 72,7% 78,6%
coverage coverage
 1    /*
 2    * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
 3    *
 4    * This software is open source.
 5    * See the bottom of this file for the licence.
 6    *
 7    * $Id: DOMReader.java,v 1.15 2004/06/25 08:03:36 maartenc Exp $
 8    */
 9   
 10    package org.dom4j.io;
 11   
 12    import java.util.ArrayList;
 13    import java.util.List;
 14   
 15    import org.dom4j.Branch;
 16    import org.dom4j.Document;
 17    import org.dom4j.DocumentFactory;
 18    import org.dom4j.Element;
 19    import org.dom4j.Namespace;
 20    import org.dom4j.QName;
 21    import org.dom4j.tree.NamespaceStack;
 22   
 23    /** <p><code>DOMReader</code> navigates a W3C DOM tree and creates
 24    * a DOM4J tree from it.</p>
 25    *
 26    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
 27    * @version $Revision: 1.15 $
 28    */
 29    public class DOMReader {
 30   
 31    /** <code>DocumentFactory</code> used to create new document objects */
 32    private DocumentFactory factory;
 33   
 34    /** stack of <code>Namespace</code> and <code>QName</code> objects */
 35    private NamespaceStack namespaceStack;
 36   
 37   
 38  40 public DOMReader() {
 39  40 this.factory = DocumentFactory.getInstance();
 40  40 this.namespaceStack = new NamespaceStack(factory);
 41    }
 42   
 43  0 public DOMReader(DocumentFactory factory) {
 44  0 this.factory = factory;
 45  0 this.namespaceStack = new NamespaceStack(factory);
 46    }
 47   
 48    /** @return the <code>DocumentFactory</code> used to create document objects
 49    */
 50  40 public DocumentFactory getDocumentFactory() {
 51  40 return factory;
 52    }
 53   
 54    /** <p>This sets the <code>DocumentFactory</code> used to create new documents.
 55    * This method allows the building of custom DOM4J tree objects to be implemented
 56    * easily using a custom derivation of {@link DocumentFactory}</p>
 57    *
 58    * @param factory <code>DocumentFactory</code> used to create DOM4J objects
 59    */
 60  0 public void setDocumentFactory(DocumentFactory factory) {
 61  0 this.factory = factory;
 62  0 this.namespaceStack.setDocumentFactory(factory);
 63    }
 64   
 65  40 public Document read(org.w3c.dom.Document domDocument) {
 66  40 if ( domDocument instanceof Document ) {
 67  0 return (Document) domDocument;
 68    }
 69  40 Document document = createDocument();
 70   
 71  40 clearNamespaceStack();
 72   
 73  40 org.w3c.dom.NodeList nodeList = domDocument.getChildNodes();
 74  40 for ( int i = 0, size = nodeList.getLength(); i < size; i++ ) {
 75  52 readTree( nodeList.item(i), document );
 76    }
 77  40 return document;
 78    }
 79   
 80   
 81    // Implementation methods
 82  28180 protected void readTree(org.w3c.dom.Node node, Branch current) {
 83  28180 Element element = null;
 84  28180 Document document = null;
 85  28180 if ( current instanceof Element ) {
 86  28128 element = (Element) current;
 87    }
 88    else {
 89  52 document = (Document) current;
 90    }
 91  28180 switch (node.getNodeType()) {
 92  10238 case org.w3c.dom.Node.ELEMENT_NODE:
 93  10238 readElement(node, current);
 94  10238 break;
 95   
 96  4 case org.w3c.dom.Node.PROCESSING_INSTRUCTION_NODE:
 97  4 if ( current instanceof Element ) {
 98  0 ((Element) current).addProcessingInstruction(
 99    node.getNodeName(), node.getNodeValue()
 100    );
 101    }
 102    else {
 103  4 ((Document) current).addProcessingInstruction(
 104    node.getNodeName(), node.getNodeValue()
 105    );
 106    }
 107  4 break;
 108   
 109  140 case org.w3c.dom.Node.COMMENT_NODE:
 110  140 if ( current instanceof Element ) {
 111  132 ((Element) current).addComment( node.getNodeValue() );
 112    }
 113    else {
 114  8 ((Document) current).addComment( node.getNodeValue() );
 115    }
 116  140 break;
 117   
 118  0 case org.w3c.dom.Node.DOCUMENT_TYPE_NODE:
 119  0 org.w3c.dom.DocumentType domDocType
 120    = (org.w3c.dom.DocumentType) node;
 121   
 122  0 document.addDocType(
 123    domDocType.getName(),
 124    domDocType.getPublicId(),
 125    domDocType.getSystemId()
 126    );
 127  0 break;
 128   
 129  17738 case org.w3c.dom.Node.TEXT_NODE:
 130  17738 element.addText( node.getNodeValue() );
 131  17738 break;
 132   
 133  60 case org.w3c.dom.Node.CDATA_SECTION_NODE:
 134  60 element.addCDATA( node.getNodeValue() );
 135  60 break;
 136   
 137   
 138  0 case org.w3c.dom.Node.ENTITY_REFERENCE_NODE: {
 139    // is there a better way to get the value of an entity?
 140  0 org.w3c.dom.Node firstChild = node.getFirstChild();
 141  0 if ( firstChild != null ) {
 142  0 element.addEntity(
 143    node.getNodeName(),
 144    firstChild.getNodeValue()
 145    );
 146    }
 147    else {
 148  0 element.addEntity( node.getNodeName(), "" );
 149    }
 150    }
 151  0 break;
 152   
 153  0 case org.w3c.dom.Node.ENTITY_NODE:
 154  0 element.addEntity(
 155    node.getNodeName(),
 156    node.getNodeValue()
 157    );
 158  0 break;
 159   
 160  0 default:
 161  0 System.out.println( "WARNING: Unknown DOM node type: " + node.getNodeType() );
 162    }
 163    }
 164   
 165  10238 protected void readElement(org.w3c.dom.Node node, Branch current) {
 166  10238 int previouslyDeclaredNamespaces = namespaceStack.size();
 167   
 168  10238 String namespaceUri = node.getNamespaceURI();
 169  10238 String elementPrefix = node.getPrefix();
 170  10238 if (elementPrefix == null) {
 171  9502 elementPrefix = "";
 172    }
 173   
 174  10238 org.w3c.dom.NamedNodeMap attributeList = node.getAttributes();
 175  10238 if (( attributeList != null ) && ( namespaceUri == null )) {
 176    // test if we have an "xmlns" attribute
 177  9372 org.w3c.dom.Node attribute = attributeList.getNamedItem( "xmlns" );
 178  9372 if ( attribute != null ) {
 179  4 namespaceUri = attribute.getNodeValue();
 180  4 elementPrefix = "";
 181    }
 182    }
 183   
 184  10238 QName qName = namespaceStack.getQName( namespaceUri, node.getLocalName(), node.getNodeName() );
 185  10238 Element element = current.addElement(qName);
 186   
 187  10238 if ( attributeList != null ) {
 188  10238 int size = attributeList.getLength();
 189  10238 List attributes = new ArrayList(size);
 190  10238 for ( int i = 0; i < size; i++ ) {
 191  5658 org.w3c.dom.Node attribute = attributeList.item(i);
 192   
 193    // Define all namespaces first then process attributes later
 194  5658 String name = attribute.getNodeName();
 195  5658 if (name.startsWith("xmlns")) {
 196  104 String prefix = getPrefix(name);
 197  104 String uri = attribute.getNodeValue();
 198   
 199    // if (!uri.equals(namespaceUri) || !prefix.equals(elementPrefix)) {
 200  104 Namespace namespace = namespaceStack.addNamespace( prefix, uri );
 201  104 element.add( namespace );
 202    // }
 203    }
 204    else {
 205  5554 attributes.add( attribute );
 206    }
 207    }
 208   
 209    // now add the attributes, the namespaces should be available
 210  10238 size = attributes.size();
 211  10238 for ( int i = 0; i < size; i++ ) {
 212  5554 org.w3c.dom.Node attribute = (org.w3c.dom.Node) attributes.get(i);
 213  5554 QName attributeQName = namespaceStack.getQName(
 214    attribute.getNamespaceURI(),
 215    attribute.getLocalName(),
 216    attribute.getNodeName()
 217    );
 218  5554 element.addAttribute( attributeQName, attribute.getNodeValue() );
 219    }
 220    }
 221   
 222    // Recurse on child nodes
 223  10238 org.w3c.dom.NodeList children = node.getChildNodes();
 224  10238 for ( int i = 0, size = children.getLength(); i < size; i++ ) {
 225  28128 org.w3c.dom.Node child = children.item(i);
 226  28128 readTree( child, element );
 227    }
 228   
 229    // pop namespaces from the stack
 230  10238 while (namespaceStack.size() > previouslyDeclaredNamespaces) {
 231  104 namespaceStack.pop();
 232    }
 233    }
 234   
 235  0 protected Namespace getNamespace(String prefix, String uri) {
 236  0 return getDocumentFactory().createNamespace(prefix, uri);
 237    }
 238   
 239  40 protected Document createDocument() {
 240  40 return getDocumentFactory().createDocument();
 241    }
 242   
 243  40 protected void clearNamespaceStack() {
 244  40 namespaceStack.clear();
 245  40 if ( ! namespaceStack.contains( Namespace.XML_NAMESPACE ) ) {
 246  40 namespaceStack.push( Namespace.XML_NAMESPACE );
 247    }
 248    }
 249   
 250  104 private String getPrefix(String xmlnsDecl) {
 251  104 int index = xmlnsDecl.indexOf(':', 5);
 252  104 if (index != -1) {
 253  60 return xmlnsDecl.substring(index + 1);
 254    } else {
 255  44 return "";
 256    }
 257    }
 258    }
 259   
 260   
 261   
 262   
 263    /*
 264    * Redistribution and use of this software and associated documentation
 265    * ("Software"), with or without modification, are permitted provided
 266    * that the following conditions are met:
 267    *
 268    * 1. Redistributions of source code must retain copyright
 269    * statements and notices. Redistributions must also contain a
 270    * copy of this document.
 271    *
 272    * 2. Redistributions in binary form must reproduce the
 273    * above copyright notice, this list of conditions and the
 274    * following disclaimer in the documentation and/or other
 275    * materials provided with the distribution.
 276    *
 277    * 3. The name "DOM4J" must not be used to endorse or promote
 278    * products derived from this Software without prior written
 279    * permission of MetaStuff, Ltd. For written permission,
 280    * please contact dom4j-info@metastuff.com.
 281    *
 282    * 4. Products derived from this Software may not be called "DOM4J"
 283    * nor may "DOM4J" appear in their names without prior written
 284    * permission of MetaStuff, Ltd. DOM4J is a registered
 285    * trademark of MetaStuff, Ltd.
 286    *
 287    * 5. Due credit should be given to the DOM4J Project -
 288    * http://www.dom4j.org
 289    *
 290    * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
 291    * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
 292    * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 293    * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 294    * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 295    * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 296    * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 297    * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 298    * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 299    * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 300    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 301    * OF THE POSSIBILITY OF SUCH DAMAGE.
 302    *
 303    * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
 304    *
 305    * $Id: DOMReader.java,v 1.15 2004/06/25 08:03:36 maartenc Exp $
 306    */