package pI.generator; import java.io.File; import java.io.IOException; import java.util.LinkedHashMap; import java.util.Map; import org.ndogen.watch.FileUtils; public class ProcessTemplate { public static final boolean DEBUG = false; public final static String USAGE = "Usage:\n"+ "java -jar pI_ProcessTemplate.jar <template_file> <options>" +"\noptions:" +"\n\t"+"-help:\tDisplay this help." +"\n\t"+"-template <template-file>:\tthe template to process (mandatory)" +"\n\t"+"-json <json-file>:\tjson file used as data model." +"\n\t"+"-out <json-file>:\toutput file." +"\n\t"+"-scope <json-file>:\tscopes the data model to a given variable." +"\n\t"+"-iterate <json-file>:\tprocesses the template file for each element in scoped variable." +"\n\t"+"-console <json-file>:\twrites generated output to console."; public static void main(String[] args) { String jsonFile = ""; String scope = ""; boolean iterate = false; String outFile = ""; boolean console = false; String templateFile = ""; Map<String, String> props = new LinkedHashMap<String, String>(); for (int i = 0; i < args.length; i++) { String cmd = args[i]; if(cmd.equals("-help")) { System.out.println(USAGE); return; } if(cmd.equals("-json")) { jsonFile = args[++i]; } else if(cmd.equals("-template")) { templateFile = args[++i]; } else if(cmd.equals("-out")) { outFile = args[++i]; } else if(cmd.equals("-scope")) { scope = args[++i]; } else if(cmd.equals("-iterate")) { iterate = true; } else if(cmd.equals("-console")) { console = true; } else { int eqIdx = cmd.indexOf('='); if(eqIdx<0) { System.err.println("Illegal parameter: " + cmd); continue; } String key = cmd.substring(0, eqIdx); if(eqIdx >= cmd.length()-1) { System.err.println("No value specified: " + cmd); } String value = cmd.substring(eqIdx+1, cmd.length()); props.put(key, value); } } String jsonData = "{}"; if(!jsonFile.isEmpty()) { try { jsonData = FileUtils.readFile(jsonFile); } catch (IOException e) { e.printStackTrace(); } } if(templateFile.isEmpty()) { System.err.println("No template file specified."); return; } File _templateFile = new File(templateFile); if(!_templateFile.exists()) { System.err.println("Template file does not exist: " + templateFile); return; } String templateDir = _templateFile.getAbsoluteFile().getParent(); String templateName = _templateFile.getName(); TemplateProcessor generator = new TemplateProcessor(templateDir, jsonData, props); boolean writeToFile = !console; String output = generator.process(templateName, scope, outFile, writeToFile, iterate); if(console) { System.out.println(output); } } }