
// UseJsonTool.java
// Andrew Davison, ad@coe.psu.ac.th, July 2023

/* Using JsonTool and json-simple to display JSON data
   using a template. The template uses a recursive macro,
   which utilizes the space gobbling property.
   https://velocity.apache.org/engine/2.3/developer-guide.html#space-gobbling

   JsonTool: https://velocity.apache.org/tools/3.1/tools-summary.html

   Usage:
    > compile UseJsonTool.java
    > run UseJsonTool <JSON fnm>
   e.g.
    > run UseJsonTool movie.json
    > run UseJsonTool complex.json
    > run UseJsonTool data.json
*/

import java.io.*;
import java.util.*;

import org.apache.velocity.*;
import org.apache.velocity.app.*;
import org.apache.velocity.tools.generic.*;


public class UseJsonTool 
{
  private static final String TEMPLATE_FNM = "jsonTool.vm";


  public static void main(String[] args) throws Exception 
  {
    if (args.length != 1) {
      System.out.println("Usage: run UseJsonTool <JSON fnm>");
      return;
    }

    Properties props = new Properties();
    props.setProperty("parser.space_gobbling", "structured");
           // makes the recursive traverse indentation look better
    Velocity.init(props);

    System.out.println("Loading template: " + TEMPLATE_FNM);
    Template template = Velocity.getTemplate(TEMPLATE_FNM);
     
    VelocityContext vc = new VelocityContext();
    vc.put("jsonTool", new JsonTool());   // load into Velocity
    vc.put("fnm", args[0]);
    
    StringWriter sw = new StringWriter();
    template.merge(vc, sw);
    System.out.println(sw);
  }  // end of main()

}  // end of UseJsonTool class
