Implemented a new syntax in the template engine to simplify json APIs

Added also an example for one of the existing APIs. The problem is the
comma separator between objects which must not be there for the last
entry in a sequence. The new syntax adds the separator symbol
automatically.
pull/402/head
Michael Peter Christen 4 years ago
parent 5a7f12a9c1
commit d9602e8325

@ -107,10 +107,8 @@ public class linkstructure {
Integer depth_target = hlg.getDepth(e.target); Integer depth_target = hlg.getDepth(e.target);
prop.put("edges_" + c + "_depthSource", depth_source == null ? -1 : depth_source.intValue()); prop.put("edges_" + c + "_depthSource", depth_source == null ? -1 : depth_source.intValue());
prop.put("edges_" + c + "_depthTarget", depth_target == null ? -1 : depth_target.intValue()); prop.put("edges_" + c + "_depthTarget", depth_target == null ? -1 : depth_target.intValue());
prop.put("edges_" + c + "_eol", 1);
c++; c++;
} }
prop.put("edges_" + (c-1) + "_eol", 0);
prop.put("edges", c); prop.put("edges", c);
prop.put("maxdepth", maxdepth); prop.put("maxdepth", maxdepth);
} }

@ -1,7 +1,7 @@
{ {
"edges" : "#[edges]#", "edges" : "#[edges]#",
"maxdepth" : "#[maxdepth]#", "maxdepth" : "#[maxdepth]#",
"graph" : [#{edges}# "graph" : [#{edges|,}#
{"source":"#[source]#", "target":"#[target]#", "type":"#[type]#", "depthSource":"#[depthSource]#", "depthTarget":"#[depthTarget]#"}#(eol)#::,#(/eol)# {"source":"#[source]#", "target":"#[target]#", "type":"#[type]#", "depthSource":"#[depthSource]#", "depthTarget":"#[depthTarget]#"}
#{/edges}#] #{/edges}#]
} }

@ -229,6 +229,15 @@ public final class TemplateEngine {
multi_key = keyStream.toByteArray(); //IMPORTANT: no prefix here multi_key = keyStream.toByteArray(); //IMPORTANT: no prefix here
keyStream.reset(); //reset stream keyStream.reset(); //reset stream
// read a separator character
byte sep_char = -1;
if (multi_key.length > 3 && multi_key[multi_key.length - 2] == '|') {
sep_char = multi_key[multi_key.length - 1];
byte[] a = new byte[multi_key.length - 2];
System.arraycopy(multi_key, 0, a, 0, multi_key.length - 2);
multi_key = a;
}
//this needs multi_key without prefix //this needs multi_key without prefix
if (transferUntil(pis, keyStream, appendBytes(mOpen, slashChar, multi_key, mClose))){ if (transferUntil(pis, keyStream, appendBytes(mOpen, slashChar, multi_key, mClose))){
bb = pis.read(); bb = pis.read();
@ -237,6 +246,24 @@ public final class TemplateEngine {
} }
final byte[] text=keyStream.toByteArray(); //text between #{key}# an #{/key}# final byte[] text=keyStream.toByteArray(); //text between #{key}# an #{/key}#
byte[] textsep = text;
int p = text.length;
if (sep_char != -1) {
byte[] a = new byte[p + 1];
System.arraycopy(text, 0, a, 0, p);
// put the separator in front of a cr/lb
if (p >= 2 && a[p - 1] < 32 && a[p - 2] < 32) { // cr and lf
a[p] = a[p - 1];
a[p - 1] = a[p - 2];
a[p - 2] = sep_char;
} else if (p >= 1 && a[p - 1] < 32) { // cr or lf
a[p] = a[p - 1];
a[p - 1] = sep_char;
} else {
a[p] = sep_char;
}
textsep = a;
}
int num=0; int num=0;
final String patternKey = getPatternKey(prefix, multi_key); final String patternKey = getPatternKey(prefix, multi_key);
if(pattern.containsKey(patternKey) && !pattern.get(patternKey).isEmpty()){ if(pattern.containsKey(patternKey) && !pattern.get(patternKey).isEmpty()){
@ -254,7 +281,7 @@ public final class TemplateEngine {
.append(ASCII.getBytes(Integer.toString(num))) .append(ASCII.getBytes(Integer.toString(num)))
.append(close_quotetagn); .append(close_quotetagn);
for(int i=0;i < num;i++) { for(int i=0;i < num;i++) {
final PushbackInputStream pis2 = new PushbackInputStream(new ByteArrayInputStream(text)); final PushbackInputStream pis2 = new PushbackInputStream(new ByteArrayInputStream(sep_char != -1 && i < num -1 ? textsep : text));
//System.out.println("recursing with text(prefix="+ multi_key + "_" + i + "_" +"):"); //DEBUG //System.out.println("recursing with text(prefix="+ multi_key + "_" + i + "_" +"):"); //DEBUG
//System.out.println(text); //System.out.println(text);
structure.append(writeTemplate(servletname, pis2, out, pattern, newPrefix(prefix,multi_key,i))); structure.append(writeTemplate(servletname, pis2, out, pattern, newPrefix(prefix,multi_key,i)));

Loading…
Cancel
Save