Uploaded image for project: 'Velocity'
  1. Velocity
  2. VELOCITY-821

recursive call in velocity macro

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Not A Problem
    • None
    • None
    • None

    Description

      template merge object : java.util.Map mergeObj

      Map node3 = new HashMap();
      node3.put("cds_ip", "5");
      node3.put("ls_ip", "6");

      Map node2 = new HashMap();
      node2.put("cds_ip", "3");
      node2.put("ls_ip", "4");
      node2.put("test_node", node3);

      Map node1 = new HashMap();
      node1.put("cds_ip", "1");
      node1.put("ls_ip", "2");
      node1.put("test_node", node2);

      Map node = new HashMap();
      node.put('test_node', node1);

      Map mergeObj = new HashMap();
      mergeObj .put("rootName", "root");
      mergeObj.put("response", node);

      For example, with the following template:

      #macro(recursive $col)
      #foreach($key in $col.keySet())
      #set($type = $col.get("$key").class.getName())
      #if($type.lastIndexOf("Map") > -1)
      <$key>
      #set($map = $col.get("$key"))
      #recursive($map)
      </$key>
      #elseif($type.lastIndexOf("List") > -1)
      #foreach($obj in $col.get("$key"))
      <$key>
      #recursive($obj)
      </$key>
      #end
      #else
      <$key">$col.get("$key")</$key>
      #end
      #end
      #end
      <?xml version='1.0' encoding='utf-8'?>
      <$rootName>
      <response>
      #recursive($response)
      </response>
      </$rootName>

      This call works as expected String:
      <?xml version='1.0' encoding='utf-8'?>
      <root>
      <response>
      <test_node>
      <cds_ip>1</cds_ip>
      <ls_ip>2</ls_ip>
      <test_node>
      <cds_ip>3</cds_ip>
      <ls_ip>4</ls_ip>
      <test_node>
      <cds_ip>5</cds_ip>
      <ls_ip>6</ls_ip>
      </test_node>
      </test_node>
      </test_node>
      </response>
      </root>

      This call works as result String:
      <root>
      <response>
      <test_node>
      <cds_ip>1</cds_ip>
      <test_node>
      <cds_ip>3</cds_ip>
      <test_node>
      <cds_ip>5</cds_ip>
      <ls_ip>6</ls_ip>
      </test_node>
      <ls_ip>6</ls_ip>
      </test_node>
      <ls_ip>6</ls_ip>
      </test_node>
      </response>
      </root>

      modify macro :
      #macro(recursive $col)
      #foreach($key in $col.keySet())
      #set($type = $col.get("$key").class.getName())
      #if($type.lastIndexOf("Map") < 0 && $type.lastIndexOf("List") < 0)
      <$key">$col.get("$key")</$key>
      #end
      #end
      #foreach($key in $col.keySet())
      #set($type = $col.get("$key").class.getName())
      #if($type.lastIndexOf("Map") > -1)
      <$key>
      #set($map = $col.get("$key"))
      #recursive($map)
      </$key>
      #elseif($type.lastIndexOf("List") > -1)
      #foreach($obj in $col.get("$key"))
      <$key>
      #recursive($obj)
      </$key>
      #end
      #end
      #end
      #end

      ---I get expected result String after modified macro
      – however modify macro have a problem
      – It excute two times loopping
      – Do velocity support recursive macro's ?

      Attachments

        Activity

          People

            Unassigned Unassigned
            yookh80 KiHyunYOO
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: