Coverage Report - org.webmacro.engine.ListBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
ListBuilder
100%
12/12
100%
10/10
2.667
ListMacro
85%
17/20
80%
8/10
2.667
 
 1  
 /*
 2  
  * Copyright (C) 1998-2000 Semiotek Inc.  All Rights Reserved.
 3  
  *
 4  
  * Redistribution and use in source and binary forms, with or without
 5  
  * modification, are permitted under the terms of either of the following
 6  
  * Open Source licenses:
 7  
  *
 8  
  * The GNU General Public License, version 2, or any later version, as
 9  
  * published by the Free Software Foundation
 10  
  * (http://www.fsf.org/copyleft/gpl.html);
 11  
  *
 12  
  *  or
 13  
  *
 14  
  * The Semiotek Public License (http://webmacro.org/LICENSE.)
 15  
  *
 16  
  * This software is provided "as is", with NO WARRANTY, not even the
 17  
  * implied warranties of fitness to purpose, or merchantability. You
 18  
  * assume all risks and liabilities associated with its use.
 19  
  *
 20  
  * See www.webmacro.org for more information on the WebMacro project.
 21  
  */
 22  
 
 23  
 
 24  
 package org.webmacro.engine;
 25  
 
 26  
 import org.webmacro.Context;
 27  
 import org.webmacro.FastWriter;
 28  
 import org.webmacro.Macro;
 29  
 import org.webmacro.PropertyException;
 30  
 
 31  
 import java.io.IOException;
 32  
 import java.util.Vector;
 33  
 
 34  
 
 35  
 /**
 36  
  * ListBuilder is used for building argument lists to function calls
 37  
  * or array initializers.  If all of the arguments are compile-time
 38  
  * constants, the build() method returns an Object[], otherwise it
 39  
  * returns a ListMacro.
 40  
  */
 41  
 
 42  2132
 public final class ListBuilder extends Vector implements Builder
 43  
 {
 44  
 
 45  
     /**
 46  
          * 
 47  
          */
 48  
         private static final long serialVersionUID = 1L;
 49  
 
 50  
         public final Object[] buildAsArray (BuildContext bc) throws BuildException
 51  
     {
 52  2128
         Object c[] = new Object[size()];
 53  
 
 54  3944
         for (int i = 0; i < c.length; i++)
 55  
         {
 56  1816
             Object elem = elementAt(i);
 57  1816
             c[i] = (elem instanceof Builder) ?
 58  
                     ((Builder) elem).build(bc) : elem;
 59  
         }
 60  2128
         return c;
 61  
     }
 62  
 
 63  
     public final Object build (BuildContext bc) throws BuildException
 64  
     {
 65  2128
         boolean isMacro = false;
 66  2128
         Object[] c = buildAsArray(bc);
 67  3944
         for (int i = 0; i < c.length; i++)
 68  1816
             if (c[i] instanceof Macro)
 69  1664
                 isMacro = true;
 70  
 
 71  2128
         return (isMacro) ? (Object) new ListMacro(c) : c;
 72  
     }
 73  
 }
 74  
 
 75  
 
 76  
 /**
 77  
  * A list is a sequence of terms. It's used in two common cases:
 78  
  * the items in an array initializer; and the arguments to a
 79  
  * method call.
 80  
  */
 81  
 class ListMacro implements Macro
 82  
 {
 83  
 
 84  
     final private Object[] _content; // the list data
 85  
 
 86  
     /**
 87  
      * create a new list
 88  
      */
 89  
     ListMacro (Object[] content)
 90  1588
     {
 91  1588
         _content = content;
 92  1588
     }
 93  
 
 94  
     public void write (FastWriter out, Context context)
 95  
             throws PropertyException, IOException
 96  
     {
 97  0
         out.write(evaluate(context).toString());
 98  0
     }
 99  
 
 100  
     public String toString ()
 101  
     {
 102  2
         StringBuffer sb = new StringBuffer();
 103  2
         sb.append("(");
 104  4
         for (int i = 0; i < _content.length; i++)
 105  
         {
 106  2
             if (i != 0)
 107  
             {
 108  0
                 sb.append(", ");
 109  
             }
 110  2
             sb.append(_content[i] == null ? "null" : _content[i].toString());
 111  
         }
 112  2
         sb.append(")");
 113  2
         return sb.toString();
 114  
     }
 115  
 
 116  
     public Object evaluate (Context context)
 117  
             throws PropertyException
 118  
     {
 119  42696
         Object[] ret = new Object[_content.length];
 120  95962
         for (int i = 0; i < _content.length; i++)
 121  
         {
 122  53286
             Object m = _content[i];
 123  53286
             if (m instanceof Macro)
 124  
             {
 125  48010
                 m = ((Macro) m).evaluate(context);
 126  
             }
 127  53266
             ret[i] = m;
 128  
         }
 129  42676
         return ret;
 130  
     }
 131  
 }