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

@ -37,14 +37,14 @@ import net.yacy.server.serverSwitch;
import net.yacy.server.servletProperties;
public class linkstructure {
public static serverObjects respond(final RequestHeader header, final serverObjects post, final serverSwitch env) {
final servletProperties prop = new servletProperties();
final String ext = header.get(HeaderFramework.CONNECTION_PROP_EXT, "");
//final boolean json = ext.equals("json");
final boolean xml = ext.equals("xml");
final Switchboard sb = (Switchboard) env;
Fulltext fulltext = sb.index.fulltext();
if (post == null) return prop;
@ -53,7 +53,7 @@ public class linkstructure {
int maxnodes = Math.min(post.getInt("maxnodes", 10000), authenticated ? 10000000 : 100);
HyperlinkGraph hlg = new HyperlinkGraph();
int maxdepth = 0;
if (post.get("about", null) != null) try {
// get link structure within a host
String about = post.get("about", null); // may be a URL, a URL hash or a domain hash
@ -71,7 +71,7 @@ public class linkstructure {
hostname = url.getHost();
}
if (hostname == null) return prop;
// now collect _all_ documents inside the domain until a timeout appears
hlg.fill(fulltext.getDefaultConnector(), hostname, null, maxtime, maxnodes);
maxdepth = hlg.findLinkDepth();
@ -82,7 +82,7 @@ public class linkstructure {
DigestURL from = post.get("from", null) == null ? null : new DigestURL(post.get("from", null)); // can be null or must be an url
hlg.path(sb.index, from, to, maxtime, maxnodes);
} catch (final MalformedURLException e) {}
// finally just write out the edge array
writeGraph(prop, hlg, maxdepth);
@ -92,11 +92,11 @@ public class linkstructure {
outgoingHeader.put(HeaderFramework.CORS_ALLOW_ORIGIN, "*");
prop.setOutgoingHeader(outgoingHeader);
}
// return rewrite properties
return prop;
}
private static void writeGraph(final servletProperties prop, final HyperlinkGraph hlg, final int maxdepth) {
int c = 0;
for (HyperlinkEdge e: hlg) {
@ -107,10 +107,8 @@ public class linkstructure {
Integer depth_target = hlg.getDepth(e.target);
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 + "_eol", 1);
c++;
}
prop.put("edges_" + (c-1) + "_eol", 0);
prop.put("edges", c);
prop.put("maxdepth", maxdepth);
}

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

@ -229,6 +229,15 @@ public final class TemplateEngine {
multi_key = keyStream.toByteArray(); //IMPORTANT: no prefix here
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
if (transferUntil(pis, keyStream, appendBytes(mOpen, slashChar, multi_key, mClose))){
bb = pis.read();
@ -237,6 +246,24 @@ public final class TemplateEngine {
}
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;
final String patternKey = getPatternKey(prefix, multi_key);
if(pattern.containsKey(patternKey) && !pattern.get(patternKey).isEmpty()){
@ -254,7 +281,7 @@ public final class TemplateEngine {
.append(ASCII.getBytes(Integer.toString(num)))
.append(close_quotetagn);
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(text);
structure.append(writeTemplate(servletname, pis2, out, pattern, newPrefix(prefix,multi_key,i)));

Loading…
Cancel
Save