Clover coverage report - groovy - 1.0-beta-7
Coverage timestamp: Wed Sep 29 2004 16:55:52 BST
file stats: LOC: 214   Methods: 13
NCLOC: 140   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
DOMBuilder.java 0% 0% 0% 0%
coverage
 1   
 /*
 2   
  $Id: DOMBuilder.java,v 1.6 2004/05/23 20:05:25 spullara Exp $
 3   
 
 4   
  Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
 5   
 
 6   
  Redistribution and use of this software and associated documentation
 7   
  ("Software"), with or without modification, are permitted provided
 8   
  that the following conditions are met:
 9   
 
 10   
  1. Redistributions of source code must retain copyright
 11   
     statements and notices.  Redistributions must also contain a
 12   
     copy of this document.
 13   
 
 14   
  2. Redistributions in binary form must reproduce the
 15   
     above copyright notice, this list of conditions and the
 16   
     following disclaimer in the documentation and/or other
 17   
     materials provided with the distribution.
 18   
 
 19   
  3. The name "groovy" must not be used to endorse or promote
 20   
     products derived from this Software without prior written
 21   
     permission of The Codehaus.  For written permission,
 22   
     please contact info@codehaus.org.
 23   
 
 24   
  4. Products derived from this Software may not be called "groovy"
 25   
     nor may "groovy" appear in their names without prior written
 26   
     permission of The Codehaus. "groovy" is a registered
 27   
     trademark of The Codehaus.
 28   
 
 29   
  5. Due credit should be given to The Codehaus -
 30   
     http://groovy.codehaus.org/
 31   
 
 32   
  THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
 33   
  ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
 34   
  NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 35   
  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
 36   
  THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 37   
  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 38   
  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 39   
  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 40   
  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 41   
  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 42   
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 43   
  OF THE POSSIBILITY OF SUCH DAMAGE.
 44   
 
 45   
  */
 46   
 package groovy.xml;
 47   
 
 48   
 import groovy.util.BuilderSupport;
 49   
 
 50   
 import java.io.IOException;
 51   
 import java.io.Reader;
 52   
 import java.security.AccessController;
 53   
 import java.security.PrivilegedActionException;
 54   
 import java.security.PrivilegedExceptionAction;
 55   
 import java.util.Iterator;
 56   
 import java.util.Map;
 57   
 
 58   
 import javax.xml.parsers.DocumentBuilder;
 59   
 import javax.xml.parsers.DocumentBuilderFactory;
 60   
 import javax.xml.parsers.FactoryConfigurationError;
 61   
 import javax.xml.parsers.ParserConfigurationException;
 62   
 
 63   
 import org.w3c.dom.Document;
 64   
 import org.w3c.dom.Element;
 65   
 import org.w3c.dom.Node;
 66   
 import org.xml.sax.InputSource;
 67   
 import org.xml.sax.SAXException;
 68   
 
 69   
 /**
 70   
  * A helper class for creating a W3C DOM tree
 71   
  * 
 72   
  * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
 73   
  * @version $Revision: 1.6 $
 74   
  */
 75   
 public class DOMBuilder extends BuilderSupport {
 76   
 
 77   
     Document document;
 78   
     DocumentBuilder documentBuilder;
 79   
 
 80  0
     public static DOMBuilder newInstance() throws ParserConfigurationException, FactoryConfigurationError {
 81  0
         DocumentBuilder documentBuilder = null;
 82  0
         try {
 83  0
             documentBuilder = (DocumentBuilder) AccessController.doPrivileged(new PrivilegedExceptionAction() {
 84  0
                 public Object run() throws ParserConfigurationException {
 85  0
                     return DocumentBuilderFactory.newInstance().newDocumentBuilder();
 86   
                 }
 87   
             });
 88   
         } catch (PrivilegedActionException pae) {
 89  0
             Exception e = pae.getException();
 90  0
             if (e instanceof ParserConfigurationException) {
 91  0
                 throw (ParserConfigurationException) e;
 92   
             } else {
 93  0
                 throw new RuntimeException(e);
 94   
             }
 95   
         }
 96  0
         return new DOMBuilder(documentBuilder);
 97   
     }
 98   
 
 99  0
     public static Document parse(Reader reader) throws SAXException, IOException, ParserConfigurationException {
 100  0
             DocumentBuilder documentBuilder = null;
 101  0
             try {
 102  0
                 documentBuilder = (DocumentBuilder) AccessController.doPrivileged(new PrivilegedExceptionAction() {
 103  0
                     public Object run() throws ParserConfigurationException {
 104  0
                         return DocumentBuilderFactory.newInstance().newDocumentBuilder();
 105   
                     }
 106   
                 });
 107   
             } catch (PrivilegedActionException pae) {
 108  0
                 Exception e = pae.getException();
 109  0
                 if (e instanceof ParserConfigurationException) {
 110  0
                     throw (ParserConfigurationException) e;
 111   
                 } else {
 112  0
                     throw new RuntimeException(e);
 113   
                 }
 114   
             }
 115  0
             return documentBuilder.parse(new InputSource(reader));
 116   
     }
 117   
     
 118  0
     public DOMBuilder(Document document) {
 119  0
         this.document = document;
 120   
     }
 121   
 
 122  0
     public DOMBuilder(DocumentBuilder documentBuilder) {
 123  0
         this.documentBuilder = documentBuilder;
 124   
     }
 125   
 
 126  0
     protected void setParent(Object parent, Object child) {
 127  0
         Node current = (Node) parent;
 128  0
         Node node = (Node) child;
 129   
 
 130  0
         current.appendChild(node);
 131   
     }
 132   
 
 133  0
     protected Object createNode(Object name) {
 134  0
         if (document == null) {
 135  0
             document = createDocument();
 136   
         }
 137  0
         if (name instanceof QName) {
 138  0
             QName qname = (QName) name;
 139  0
             return document.createElementNS(qname.getNamespaceURI(), qname.getQualifiedName());
 140   
         }
 141   
         else {
 142  0
             return document.createElement(name.toString());
 143   
         }
 144   
     }
 145   
 
 146  0
     protected Document createDocument() {
 147  0
         if (documentBuilder == null) {
 148  0
             throw new IllegalArgumentException("No Document or DOMImplementation available so cannot create Document");
 149   
         }
 150   
         else {
 151  0
             return documentBuilder.newDocument();
 152   
         }
 153   
     }
 154   
 
 155  0
     protected Object createNode(Object name, Object value) {
 156  0
         Element element = (Element) createNode(name);
 157  0
         element.appendChild(document.createTextNode(value.toString()));
 158  0
         return element;
 159   
     }
 160   
 
 161  0
     protected Object createNode(Object name, Map attributes, Object value) {
 162  0
         Element element = (Element) createNode(name, attributes);
 163  0
         element.appendChild(document.createTextNode(value.toString()));
 164  0
         return element;
 165   
     }
 166   
     
 167  0
     protected Object createNode(Object name, Map attributes) {
 168  0
         Element element = (Element) createNode(name);
 169  0
         for (Iterator iter = attributes.entrySet().iterator(); iter.hasNext();) {
 170  0
             Map.Entry entry = (Map.Entry) iter.next();
 171  0
             String attrName = entry.getKey().toString();
 172  0
             Object value = entry.getValue();
 173  0
             if ("xmlns".equals(attrName)) {
 174  0
                 if (value instanceof Map) {
 175  0
                     appendNamespaceAttributes(element, (Map) value);
 176   
                 }
 177   
                 else {
 178  0
                     throw new IllegalArgumentException("The value of the xmlns attribute must be a Map of QNames to String URIs");
 179   
                 }
 180   
             }
 181   
             else {
 182  0
                 element.setAttribute(attrName, value.toString());
 183   
             }
 184   
         }
 185  0
         return element;
 186   
     }
 187   
 
 188  0
     protected void appendNamespaceAttributes(Element element, Map attributes) {
 189  0
         for (Iterator iter = attributes.entrySet().iterator(); iter.hasNext();) {
 190  0
             Map.Entry entry = (Map.Entry) iter.next();
 191  0
             Object key = entry.getKey();
 192  0
             Object value = entry.getValue();
 193  0
             if (value == null) {
 194  0
                 throw new IllegalArgumentException("The value of key: " + key + " cannot be null");
 195   
             }
 196  0
             if (key instanceof String) {
 197  0
                 String prefix = (String) key;
 198   
                 
 199   
                 //System.out.println("Creating namespace for prefix: " + prefix + " with value: " + value);
 200   
                 
 201   
                 //element.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xmlns:" + prefix, value.toString());
 202  0
                 element.setAttributeNS("", prefix, value.toString());
 203   
             }
 204  0
             else if (key instanceof QName) {
 205  0
                 QName qname = (QName) key;
 206  0
                 element.setAttributeNS(qname.getNamespaceURI(), qname.getQualifiedName(), value.toString());
 207   
             }
 208   
             else {
 209  0
                 throw new IllegalArgumentException("The key: " + key + " should be an instanceof of " + QName.class);
 210   
             }
 211   
         }
 212   
     }
 213   
 }
 214