
// XMLWriter.java
// Andrew Davison, ad@coe.psu.ac.th, July 2023

/* Use a template to generate an XML document of pet sales.
   XML is supported using the JDOM library.
   (http://www.jdom.org/).

   Usage:
    > compile XMLWriter.java
    > run XMLWriter
*/


import java.io.*;
import java.util.*;

import org.apache.velocity.*;
import org.apache.velocity.app.*;


public class XMLWriter
{
  private static final String TEMPLATE_FNM = "sales.vm";


  public static void main( String[] args )
                                throws Exception
  {
    Velocity.init();
    System.out.println("Loading template: " + TEMPLATE_FNM);
    Template template = Velocity.getTemplate(TEMPLATE_FNM);
     
    // build data -- a list of 3 name/price maps
    ArrayList<Map<String,String>> pets = new ArrayList<>();
    Map<String,String> map = new HashMap<>();

    map.put("name", "Cow");
    map.put("price", "$100.00");
    pets.add(map);

    map = new HashMap<>();
    map.put("name", "Eagle");
    map.put("price", "$59.99");
    pets.add(map);

    map = new HashMap<>();
    map.put("name", "Shark");
    map.put("price", "$3.99");
    pets.add(map);

    VelocityContext vc = new VelocityContext();
    vc.put("petList", pets);

    StringWriter sw = new StringWriter();
    template.merge(vc, sw);
    System.out.println(sw);
  } /* main */

}  // end of XMLWriter class

