Coverage Report - org.webmacro.engine.MapBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
MapBuilder
0%
0/18
0%
0/14
3
MapMacro
0%
0/18
0%
0/6
3
 
 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  
 package org.webmacro.engine;
 23  
 
 24  
 import org.webmacro.Context;
 25  
 import org.webmacro.FastWriter;
 26  
 import org.webmacro.Macro;
 27  
 import org.webmacro.PropertyException;
 28  
 
 29  
 import java.io.IOException;
 30  
 import java.util.HashMap;
 31  
 import java.util.Iterator;
 32  
 import java.util.Map;
 33  
 
 34  
 /**
 35  
  * MapBuilder is used during the parsing/building phase of a template to create
 36  
  * user defined Map types, using a syntax that is similar to:<pre>
 37  
  *     #set $map = { "key" => "value", "key2" => "value2" }
 38  
  * </pre>
 39  
  *
 40  
  * MapBuilder ultimately creates a Map that understands <code>org.webmacro.Macro</code>'s
 41  
  * for <i>keys</i> and <i>values</i>, so $Variable references are okay for both.
 42  
  *
 43  
  * @author e_ridge
 44  
  * @since May 13, 2003
 45  
  */
 46  0
 public class MapBuilder extends HashMap implements Builder
 47  
 {
 48  
         private static final long serialVersionUID = 1L;
 49  
 
 50  
         public Object build (BuildContext pc) throws BuildException
 51  
     {
 52  0
         Map ret = new HashMap(size());
 53  0
         boolean isMacro = false;
 54  0
         for (Iterator itr = entrySet().iterator(); itr.hasNext(); )
 55  
         {
 56  0
             Map.Entry entry = (Map.Entry) itr.next();
 57  0
             Object key = entry.getKey();
 58  0
             Object value = entry.getValue();
 59  
 
 60  0
             if (key instanceof Builder)
 61  0
                 key = ((Builder) key).build(pc);
 62  0
             if (value instanceof Builder)
 63  0
                 value = ((Builder) value).build(pc);
 64  
 
 65  0
             if (!isMacro)
 66  0
                 isMacro = key instanceof Macro || value instanceof Macro;
 67  
 
 68  0
             ret.put(key, value);
 69  0
         }
 70  
 
 71  0
         if (isMacro)
 72  0
             return new MapMacro(ret);
 73  
         else
 74  0
             return ret;
 75  
     }
 76  
 }
 77  
 
 78  
 class MapMacro implements Macro
 79  
 {
 80  
     private final Map _map;
 81  
 
 82  
     MapMacro (Map map)
 83  0
     {
 84  0
         _map = map;
 85  0
     }
 86  
 
 87  
     public void write (FastWriter out, Context context) throws PropertyException, IOException
 88  
     {
 89  0
         out.write(evaluate(context).toString());
 90  0
     }
 91  
 
 92  
     public String toString ()
 93  
     {
 94  0
         return _map.toString();
 95  
     }
 96  
 
 97  
     public Object evaluate (Context context) throws PropertyException
 98  
     {
 99  0
         Map ret = new HashMap(_map.size());
 100  0
         for (Iterator itr = _map.entrySet().iterator(); itr.hasNext(); )
 101  
         {
 102  0
             Map.Entry entry = (Map.Entry) itr.next();
 103  0
             Object key = entry.getKey();
 104  0
             Object value = entry.getValue();
 105  
 
 106  0
             if (key instanceof Macro)
 107  0
                 key = ((Macro) key).evaluate(context);
 108  0
             if (value instanceof Macro)
 109  0
                 value = ((Macro) value).evaluate(context);
 110  0
             ret.put(key, value);
 111  0
         }
 112  
 
 113  0
         return ret;
 114  
     }
 115  
 }