Clover coverage report - groovy - 1.0-beta-8
Coverage timestamp: Fri Dec 17 2004 14:55:55 GMT
file stats: LOC: 275   Methods: 20
NCLOC: 177   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
Node.java 0% 0% 0% 0%
coverage
 1   
 /*
 2   
  $Id: Node.java,v 1.8 2004/06/01 15:55:48 tug 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.util;
 47   
 
 48   
 import java.io.PrintWriter;
 49   
 import java.util.ArrayList;
 50   
 import java.util.Collection;
 51   
 import java.util.Collections;
 52   
 import java.util.Iterator;
 53   
 import java.util.List;
 54   
 import java.util.Map;
 55   
 
 56   
 import org.codehaus.groovy.runtime.InvokerHelper;
 57   
 
 58   
 /**
 59   
  * Represents an arbitrary tree node which can be used for structured  metadata which can be any arbitrary XML-like tree.
 60   
  * A node can have a name, a value and an optional Map of attributes.
 61   
  * Typically the name is a String and a value is either a String or a List of other Nodes.
 62   
  * Though the types are extensible to provide a flexible structure. 
 63   
  * e.g. you could use a QName as the name which includes a namespace URI and a local name. Or a JMX ObjectName etc.
 64   
  * So this class can represent metadata like {foo a=1 b="abc"} or nested metadata like {foo a=1 b="123" { bar x=12 text="hello" }}
 65   
  * 
 66   
  * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
 67   
  * @version $Revision: 1.8 $
 68   
  */
 69   
 public class Node {
 70   
 
 71   
     private Node parent;
 72   
     private Object name;
 73   
     private Map attributes;
 74   
     private Object value;
 75   
 
 76  0
     public Node(Node parent, Object name) {
 77  0
         this(parent, name, Collections.EMPTY_MAP, Collections.EMPTY_LIST);
 78   
     }
 79   
 
 80  0
     public Node(Node parent, Object name, Object value) {
 81  0
         this(parent, name, Collections.EMPTY_MAP, value);
 82   
     }
 83   
 
 84  0
     public Node(Node parent, Object name, Map attributes) {
 85  0
         this(parent, name, attributes, Collections.EMPTY_LIST);
 86   
     }
 87   
 
 88  0
     public Node(Node parent, Object name, Map attributes, Object value) {
 89  0
         this.parent = parent;
 90  0
         this.name = name;
 91  0
         this.attributes = attributes;
 92  0
         this.value = value;
 93   
         
 94  0
         if (parent != null) {
 95  0
             Object parentValue = parent.value();
 96  0
             List parentList = null;
 97  0
             if (parentValue instanceof List) {
 98  0
                 parentList = (List) parentValue;
 99   
             }
 100   
             else {
 101  0
                 parentList = new ArrayList();
 102  0
                 parentList.add(parentValue);
 103  0
                 parent.setValue(parentList);
 104   
             }
 105  0
             parentList.add(this);
 106   
         }
 107   
     }
 108   
 
 109  0
     public String text() {
 110  0
         if (value instanceof String) {
 111  0
             return (String) value;
 112   
         }
 113  0
         else if (value instanceof Collection) {
 114  0
             Collection coll = (Collection) value;
 115  0
             String previousText = null;
 116  0
             StringBuffer buffer = null;
 117  0
             for (Iterator iter = coll.iterator(); iter.hasNext();) {
 118  0
                 Object child = iter.next();
 119  0
                 if (child instanceof String) {
 120  0
                     String childText = (String) child;
 121  0
                     if (previousText == null) {
 122  0
                         previousText = childText;
 123   
                     }
 124   
                     else {
 125  0
                         if (buffer == null) {
 126  0
                             buffer = new StringBuffer();
 127  0
                             buffer.append(previousText);
 128   
                         }
 129  0
                         buffer.append(childText);
 130   
                     }
 131   
                 }
 132   
             }
 133  0
             if (buffer != null) {
 134  0
                 return buffer.toString();
 135   
             }
 136   
             else {
 137  0
                 if (previousText != null) {
 138  0
                     return previousText;
 139   
                 }
 140   
             }
 141   
         }
 142  0
         return "";
 143   
     }
 144   
 
 145  0
     public Iterator iterator() {
 146  0
         return children().iterator();
 147   
     }
 148   
     
 149  0
     public List children() {
 150  0
         if (value == null) {
 151  0
             return Collections.EMPTY_LIST;
 152   
         }
 153  0
         else if (value instanceof List) {
 154  0
             return (List) value;
 155   
         }
 156   
         else {
 157   
             // we're probably just a String
 158  0
             return Collections.singletonList(value);
 159   
         }
 160   
     }
 161   
 
 162  0
     public Map attributes() {
 163  0
         return attributes;
 164   
     }
 165   
 
 166  0
     public Object attribute(Object key) {
 167  0
         return (attributes != null) ? attributes.get(key) : null;
 168   
     }
 169   
     
 170  0
     public Object name() {
 171  0
         return name;
 172   
     }
 173   
 
 174  0
     public Object value() {
 175  0
         return value;
 176   
     }
 177   
 
 178  0
     public void setValue(Object value) {
 179  0
         this.value = value;
 180   
     }
 181   
 
 182  0
     public Node parent() {
 183  0
         return parent;
 184   
     }
 185   
 
 186  0
     public Object get(String key) {
 187  0
         if (key.charAt(0) == '@') {
 188  0
             String attributeName = key.substring(1);
 189  0
             return attributes().get(attributeName);
 190   
         }
 191   
         else {
 192   
             // iterate through list looking for node with name 'key'
 193  0
             List answer = new ArrayList();
 194  0
             for (Iterator iter = children().iterator(); iter.hasNext();) {
 195  0
                 Object child = iter.next();
 196  0
                 if (child instanceof Node) {
 197  0
                     Node childNode = (Node) child;
 198  0
                     if (key.equals(childNode.name())) {
 199  0
                         answer.add(childNode);
 200   
                     }
 201   
                 }
 202   
             }
 203  0
             return answer;
 204   
         }
 205   
     }
 206   
 
 207   
 //    public Object get(int idx) {
 208   
 //        return children().get(idx);
 209   
 //    }
 210   
 
 211   
 
 212   
 
 213   
     /**
 214   
      * Provide a collection of all the nodes in the tree
 215   
      * using a depth first traversal
 216   
      */
 217  0
     public List depthFirst() {
 218  0
         List answer = new ArrayList();
 219  0
         answer.add(this);
 220  0
         answer.addAll(depthFirstRest());
 221  0
         return answer;
 222   
     }
 223   
     
 224  0
     private  List depthFirstRest() {
 225  0
         List answer = new ArrayList();
 226  0
         for (Iterator iter = InvokerHelper.asIterator(value); iter.hasNext(); ) {
 227  0
             Object child = iter.next();
 228  0
             if (child instanceof Node) {
 229  0
                 Node childNode = (Node) child;
 230  0
                 List children = childNode.depthFirstRest();
 231  0
                 answer.add(childNode);
 232  0
                 answer.addAll(children);
 233   
             }
 234   
         }
 235  0
         return answer;
 236   
     }
 237   
 
 238   
     /**
 239   
      * Provide a collection of all the nodes in the tree
 240   
      * using a bredth first traversal
 241   
      */
 242  0
     public List breadthFirst() {
 243  0
         List answer = new ArrayList();
 244  0
         answer.add(this);
 245  0
         answer.addAll(breadthFirstRest());
 246  0
         return answer;
 247   
     }
 248   
     
 249  0
     private  List breadthFirstRest() {
 250  0
         List answer = new ArrayList();
 251  0
         for (Iterator iter = InvokerHelper.asIterator(value); iter.hasNext(); ) {
 252  0
             Object child = iter.next();
 253  0
             if (child instanceof Node) {
 254  0
                 Node childNode = (Node) child;
 255  0
                 answer.add(childNode);
 256   
             }
 257   
         }
 258  0
         List copy = new ArrayList(answer);
 259  0
         for (Iterator iter = copy.iterator(); iter.hasNext(); ) {
 260  0
             Node childNode = (Node) iter.next();
 261  0
             List children = childNode.breadthFirstRest();
 262  0
             answer.addAll(children);
 263   
         }
 264  0
         return answer;
 265   
     }
 266   
 
 267  0
     public String toString() {
 268  0
         return name + "[attributes=" + attributes + "; value=" + value + "]";
 269   
     }
 270   
 
 271  0
     public void print(PrintWriter out) {
 272  0
         new NodePrinter(out).print(this);
 273   
     }
 274   
 }
 275