Clover coverage report - groovy - 1.0-beta-8
Coverage timestamp: Fri Dec 17 2004 14:55:55 GMT
file stats: LOC: 237   Methods: 15
NCLOC: 151   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
MarkupBuilder.java 0% 0% 0% 0%
coverage
 1   
 /*
 2   
  $Id: MarkupBuilder.java,v 1.8 2004/12/09 22:07:51 glaforge 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   
 import groovy.util.IndentPrinter;
 50   
 
 51   
 import java.io.PrintWriter;
 52   
 import java.io.Writer;
 53   
 import java.util.Iterator;
 54   
 import java.util.Map;
 55   
 
 56   
 /**
 57   
  * A helper class for creating XML or HTML markup
 58   
  * 
 59   
  * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
 60   
  * @author Stefan Matthias Aust
 61   
  * @version $Revision: 1.8 $
 62   
  */
 63   
 public class MarkupBuilder extends BuilderSupport {
 64   
 
 65   
     private IndentPrinter out;
 66   
     private boolean nospace;
 67   
     private int state;
 68   
     private boolean nodeIsEmpty = true;
 69   
 
 70  0
     public MarkupBuilder() {
 71  0
         this(new IndentPrinter());
 72   
     }
 73   
 
 74  0
     public MarkupBuilder(PrintWriter writer) {
 75  0
         this(new IndentPrinter(writer));
 76   
     }
 77   
 
 78  0
     public MarkupBuilder(Writer writer) {
 79  0
         this(new IndentPrinter(new PrintWriter(writer)));
 80   
     }
 81   
 
 82  0
     public MarkupBuilder(IndentPrinter out) {
 83  0
         this.out = out;
 84   
     }
 85   
 
 86  0
     protected void setParent(Object parent, Object child) {
 87   
     }
 88   
 
 89   
     /*
 90   
     public Object getProperty(String property) {
 91   
         if (property.equals("_")) {
 92   
             nospace = true;
 93   
             return null;
 94   
         } else {
 95   
             Object node = createNode(property);
 96   
             nodeCompleted(getCurrent(), node);
 97   
             return node;
 98   
         }
 99   
     }
 100   
     */
 101   
 
 102  0
     protected Object createNode(Object name) {
 103  0
         toState(1, name);
 104  0
         return name;
 105   
     }
 106   
 
 107  0
     protected Object createNode(Object name, Object value) {
 108  0
         toState(2, name);
 109  0
         out.print(">");
 110  0
         out.print(value.toString());
 111  0
         return name;
 112   
     }
 113   
 
 114  0
     protected Object createNode(Object name, Map attributes, Object value) {
 115  0
         toState(1, name);
 116  0
         for (Iterator iter = attributes.entrySet().iterator(); iter.hasNext();) {
 117  0
             Map.Entry entry = (Map.Entry) iter.next();
 118  0
             out.print(" ");
 119  0
             print(transformName(entry.getKey().toString()));
 120  0
             out.print("='");
 121  0
             print(transformValue(entry.getValue().toString()));
 122  0
             out.print("'");
 123   
         }
 124  0
         if (value != null)
 125   
         {
 126  0
             nodeIsEmpty = false;
 127  0
             out.print(">" + value + "</" + name + ">");
 128   
         }
 129  0
         return name;
 130   
     }
 131   
 
 132  0
     protected Object createNode(Object name, Map attributes) {
 133  0
         return createNode(name, attributes, null);
 134   
     }
 135   
     
 136  0
     protected void nodeCompleted(Object parent, Object node) {
 137  0
         toState(3, node);
 138  0
         out.flush();
 139   
     }
 140   
 
 141  0
     protected void print(Object node) {
 142  0
         out.print(node == null ? "null" : node.toString());
 143   
     }
 144   
 
 145  0
     protected Object getName(String methodName) {
 146  0
         return super.getName(transformName(methodName));
 147   
     }
 148   
 
 149  0
     protected String transformName(String name) {
 150  0
         if (name.startsWith("_")) name = name.substring(1);
 151  0
         return name.replace('_', '-');
 152   
     }
 153   
 
 154  0
     protected String transformValue(String value) {
 155  0
         return value.replaceAll("\\'", "&quot;");
 156   
     }
 157   
 
 158  0
     private void toState(int next, Object name) {
 159  0
         switch (state) {
 160   
             case 0:
 161  0
                 switch (next) {
 162   
                     case 1:
 163   
                     case 2:
 164  0
                         out.print("<");
 165  0
                         print(name);
 166  0
                         break;
 167   
                     case 3:
 168  0
                         throw new Error();
 169   
                 }
 170  0
                 break;
 171   
             case 1:
 172  0
                 switch (next) {
 173   
                     case 1:
 174   
                     case 2:
 175  0
                         out.print(">");
 176  0
                         if (nospace) {
 177  0
                             nospace = false;
 178   
                         } else {
 179  0
                             out.println();
 180  0
                             out.incrementIndent();
 181  0
                             out.printIndent();
 182   
                         }
 183  0
                         out.print("<");
 184  0
                         print(name);
 185  0
                         break;
 186   
                     case 3:
 187  0
                         if (nodeIsEmpty) {
 188  0
                             out.print(" />");
 189   
                         }
 190  0
                         break;
 191   
                 }
 192  0
                 break;
 193   
             case 2:
 194  0
                 switch (next) {
 195   
                     case 1:
 196   
                     case 2:
 197  0
                         throw new Error();
 198   
                     case 3:
 199  0
                         out.print("</");
 200  0
                         print(name);
 201  0
                         out.print(">");
 202  0
                         break;
 203   
                 }
 204  0
                 break;
 205   
             case 3:
 206  0
                 switch (next) {
 207   
                     case 1:
 208   
                     case 2:
 209  0
                         if (nospace) {
 210  0
                             nospace = false;
 211   
                         } else {
 212  0
                             out.println();
 213  0
                             out.printIndent();
 214   
                         }
 215  0
                         out.print("<");
 216  0
                         print(name);
 217  0
                         break;
 218   
                     case 3:
 219  0
                         if (nospace) {
 220  0
                             nospace = false;
 221   
                         } else {
 222  0
                             out.println();
 223  0
                             out.decrementIndent();
 224  0
                             out.printIndent();
 225   
                         }
 226  0
                         out.print("</");
 227  0
                         print(name);
 228  0
                         out.print(">");
 229  0
                         break;
 230   
                 }
 231  0
                 break;
 232   
         }
 233  0
         state = next;
 234   
     }
 235   
 
 236   
 }
 237