Home

Awesome

jsvelocity

Build Status

Last commit

A Macro Processor.

Process structured data (json, yaml, tables, csv...) with Apache Velocity ( http://velocity.apache.org/ ).

Original post was http://plindenbaum.blogspot.fr/2011/11/processing-json-data-with-apache.html

Compile

The compile process requires GNU make, java oracle JDK 8.* , wget and an internet connection.

make

Usage

java -jar jsvelocity.jar (options) template.vm

Options

    -C, --class
      Add this java Class into the context. class is wrapped into a 
      org.apache.velocity.tools.generic.ClassTool 
      Default: []
    -D, --dir, --directory
      Output directory
    -e, --expr
      Add this JSON-Expression into the context..
      Default: []
    -gson, --gson
      Do not convert json object to java. Keep the com.google.gson.* objects
      Default: false
    -hashtable, --hashtable
      Read the tab delimited file as `List<Map<String,String>>`. First line is 
      header 
      Default: []
    -h, --help
      Show Help
    -c, --instance
      Add this java instance into the context..
      Default: []
    -cstr, --instance-strst
      Add this java instance into the context. Takes 3 parameters (key) 
      (java.class with string constructor) (string-for-constructor) e.g: 
      `mykey java.io.File /tmp`
      Default: []
    -f, --json
      Add this JSON-File into the context..
      Default: []
    -lenient, --lenient
      Use a lenient json parser
      Default: false
    -o, --output
      Output File. Default: standout
    -p, --property, --properties
      Add this Java property file into the context..
      Default: []
    -s, --string
      Add this String into the context..
      Default: []
    -T, --template
      inline Velocity Template expression
    -tsv, --tsv
      Read tab delimited table in file.
      Default: []
    -y, --yaml
      Add this YAML-File into the context..
      Default: []
    -x, xml
      Read File as DOM document.
      Default: []

Examples

Example

$ java -jar dist/jsvelocity.jar -T 'Hello'

Hello

Example

$ java -jar dist/jsvelocity.jar -s c World -T 'Hello $c !'

Hello World !

Example

$ java -jar dist/jsvelocity.jar -s c World -T 'Hello ${tool.before($c,"l")} !'

Hello Wor !

Example

$ java -jar dist/jsvelocity.jar -s c World -T 'Hello ${tool.after($c,"l")} !'

Hello d !

Example

$ java -jar dist/jsvelocity.jar -s c World -T 'Hello ${tool.md5($c)} !'

Hello f5a7924e621e84c9280a9a27e1bcb7f6 !

Example

$ java -jar dist/jsvelocity.jar -s c World -T 'Hello ${tool.sha1($c)} !'

Hello 70c07ec18ef89c5309bbb0937f3a6342411e1fdd !

Example

$ seq 1 10 |paste - - > tmp.tsv
java -jar dist/jsvelocity.jar -tsv c tmp.tsv -T 'matrix[0][1]= ${c[0][1]}'

matrix[0][1]= 2

Example

$ cat test.tsv

A       B
12      23
x       y

$ java -jar dist/jsvelocity.jar --hashtable T jeter.tsv -T '${T[0]["A"]}/${T[1]["B"]}'

12/y

Example

$ java -jar dist/jsvelocity.jar -C c java.lang.String -T '${c.getPackage().getName()}'

java.lang

Example

$ java -jar dist/jsvelocity.jar -e c '[1,2,3]' -T '${c.size()} c[0]=${c[0]}'

3 c[0]=1

Example

$ cat t.yaml 
mylist:
- 'item 1'
- 'item 2'
$ java -jar dist/jsvelocity.jar -y c t.yaml -T '${c.mylist[1]}'

item 2

Default objects

Tools

Tools ( ./src/main/java/com/github/lindenb/jsvelocity/Tools.java ) implement the following methods (non exhaustive... ):

public String capitalize(final Object o) ;
public boolean isBlank(final Object o) ;
public String escapeCsv(final Object o) ;
public String escapeHtml(final Object o);
public String escapeJava(final Object o) ;
public String escapeJson(final Object o) ;
public String escapeXml(final Object o) ;
public String md5(final Object o) ;
public String sha1(final Object o) ;
public String left(final Object o,int l);
public String right(final Object o,int l) ;
public String before(final Object o,Object subo) ;
public String after(final Object o,Object subo) ;
public Integer parseInt(final Object o);
public Double parseDouble(final Object o) ;

Custom Velocity Directives

Divert Directive

"#divert("newfile.txt",false)Hello World#{end}

Javascript Directive

#javascript(1,"A") print(args[1]); for(var i=8;i<11;i++) print(""+i);print(J);#{end}

ReadFile Directive

#readfile("T","input.data")
#readfile("T","input.data","method:table;delim:comma;")
#readfile("T","input.data","method:hash;pkey:y;delim:comma;")
#readfile("T","input.data","method:hashtable;delim:comma;")

Json Directive

#json("variableName")[1,2,3,4,{}]#{end}

Author

Pierre Lindenbaum PhD @yokofakun

History: