Clover coverage report - groovy - 1.0-beta-7
Coverage timestamp: Wed Sep 29 2004 16:55:52 BST
file stats: LOC: 214   Methods: 10
NCLOC: 111   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
BuilderSupport.java 0% 0% 0% 0%
coverage
 1   
 /*
 2   
  $Id: BuilderSupport.java,v 1.7 2004/07/31 03:37:31 jstrachan 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   
 
 49   
 import groovy.lang.Closure;
 50   
 import groovy.lang.GroovyObjectSupport;
 51   
 
 52   
 import java.util.List;
 53   
 import java.util.Map;
 54   
 
 55   
 import org.codehaus.groovy.runtime.InvokerHelper;
 56   
 
 57   
 /**
 58   
  * An abstract base class for creating arbitrary nested trees of objects
 59   
  * or events
 60   
  * 
 61   
  * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
 62   
  * @version $Revision: 1.7 $
 63   
  */
 64   
 public abstract class BuilderSupport extends GroovyObjectSupport {
 65   
 
 66   
     private Object current;
 67   
     private Closure nameMappingClosure;
 68   
     private BuilderSupport proxyBuilder;
 69   
     
 70  0
     public BuilderSupport() {
 71  0
         this.proxyBuilder = this;
 72   
     }
 73   
     
 74  0
     public BuilderSupport(BuilderSupport proxyBuilder) {
 75  0
         this(null, proxyBuilder);
 76   
     }
 77   
     
 78  0
     public BuilderSupport(Closure nameMappingClosure, BuilderSupport proxyBuilder) {
 79  0
         this.nameMappingClosure = nameMappingClosure;
 80  0
         this.proxyBuilder = proxyBuilder;
 81   
     }
 82   
     
 83  0
     public Object invokeMethod(String methodName, Object args) {
 84  0
         Object name = getName(methodName);
 85  0
         return doInvokeMethod(methodName, name, args);
 86   
     }
 87   
     
 88  0
     protected Object doInvokeMethod(String methodName, Object name, Object args) {
 89  0
         Object node = null;
 90  0
         Closure closure = null;
 91  0
         List list = InvokerHelper.asList(args);
 92   
 
 93   
         //System.out.println("Called invokeMethod with name: " + name + " arguments: " + list);
 94   
 
 95  0
         switch (list.size()) {
 96   
                 case 0:
 97  0
                     break;
 98   
                     case 1:
 99   
                     {
 100  0
                             Object object = list.get(0);
 101  0
                             if (object instanceof Map) {
 102  0
                                 node = proxyBuilder.createNode(name, (Map) object);
 103  0
                             } else if (object instanceof Closure) {
 104  0
                                 closure = (Closure) object;
 105  0
                                 node = proxyBuilder.createNode(name);
 106   
                             } else {
 107  0
                                 node = proxyBuilder.createNode(name, object);
 108   
                             }
 109   
                     }
 110  0
                     break;
 111   
                     case 2:
 112   
                     {
 113  0
                         Object object1 = list.get(0);
 114  0
                         Object object2 = list.get(1);
 115  0
                         if (object1 instanceof Map) {
 116  0
                             if (object2 instanceof Closure) {
 117  0
                                 closure = (Closure) object2;
 118  0
                                 node = proxyBuilder.createNode(name, (Map) object1);
 119   
                             } else {
 120  0
                                 node = proxyBuilder.createNode(name, (Map) object1, object2);
 121   
                             }
 122   
                         } else {
 123  0
                             if (object2 instanceof Closure) {
 124  0
                                 closure = (Closure) object2;
 125  0
                                 node = proxyBuilder.createNode(name, object1);
 126   
                             }
 127   
                         }
 128   
                     }
 129  0
                     break;
 130   
                     case 3:
 131   
                     {
 132  0
                         Object attributes = list.get(0);
 133  0
                         Object value = list.get(1);
 134  0
                         closure = (Closure) list.get(2);
 135  0
                         node = proxyBuilder.createNode(name, (Map) attributes, value);
 136   
                     }
 137  0
                     break;
 138   
         }
 139   
 
 140  0
             if (node == null) {
 141  0
                 node = proxyBuilder.createNode(name);
 142   
             }
 143   
         
 144  0
         if (current != null) {
 145  0
             proxyBuilder.setParent(current, node);
 146   
         }
 147   
 
 148  0
         if (closure != null) {
 149   
             // push new node on stack
 150  0
             Object oldCurrent = current;
 151  0
             current = node;
 152   
 
 153   
             // lets register the builder as the delegate
 154  0
             setClosureDelegate(closure, node);
 155  0
             closure.call();
 156   
 
 157  0
             current = oldCurrent;
 158   
         }
 159   
 
 160  0
         proxyBuilder.nodeCompleted(current, node);
 161  0
         return node;
 162   
     }
 163   
 
 164   
     /**
 165   
      * A strategy method to allow derived builders to use
 166   
      * builder-trees and switch in different kinds of builders.
 167   
      * This method should call the setDelegate() method on the closure
 168   
      * which by default passes in this but if node is-a builder
 169   
      * we could pass that in instead (or do something wacky too)
 170   
      *
 171   
      * @param closure the closure on which to call setDelegate()
 172   
      * @param node the node value that we've just created, which could be
 173   
      * a builder
 174   
      */
 175  0
     protected void setClosureDelegate(Closure closure, Object node) {
 176  0
         closure.setDelegate(this);
 177   
     }
 178   
 
 179   
     protected abstract void setParent(Object parent, Object child);
 180   
     protected abstract Object createNode(Object name);
 181   
     protected abstract Object createNode(Object name, Object value);
 182   
     protected abstract Object createNode(Object name, Map attributes);
 183   
     protected abstract Object createNode(Object name, Map attributes, Object value);
 184   
 
 185   
     /**
 186   
      * A hook to allow names to be converted into some other object
 187   
      * such as a QName in XML or ObjectName in JMX
 188   
      * @param methodName
 189   
      * @return
 190   
      */
 191  0
     protected Object getName(String methodName) {
 192  0
         if (nameMappingClosure != null) {
 193  0
             return nameMappingClosure.call(methodName);
 194   
         }
 195  0
         return methodName;
 196   
     }
 197   
 
 198   
 
 199   
     /**
 200   
      * A hook to allow nodes to be processed once they have had all of their
 201   
      * children applied
 202   
      */
 203  0
     protected void nodeCompleted(Object parent, Object node) {
 204   
     }
 205   
 
 206  0
     protected Object getCurrent() {
 207  0
         return current;
 208   
     }
 209   
     
 210  0
     protected void setCurrent(Object current) {
 211  0
         this.current = current;
 212   
     }
 213   
 }
 214