mirror of https://github.com/gnosygnu/xowa
parent
5c3d6a173b
commit
19a315b8cd
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,69 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2020 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.jsons;
|
||||
|
||||
import gplx.Bry_;
|
||||
import gplx.List_adp;
|
||||
import gplx.List_adp_;
|
||||
import gplx.String_;
|
||||
|
||||
public class JsonDocBldr {
|
||||
private final List_adp stack = List_adp_.New();
|
||||
private final Json_doc doc = new Json_doc();
|
||||
private Json_grp root;
|
||||
private Json_grp cur;
|
||||
JsonDocBldr() {}
|
||||
public JsonDocBldr Clear(boolean isRootNode) {
|
||||
this.root = isRootNode ? Json_nde.NewByDoc(doc, 0) : Json_ary.NewByDoc(doc, 0, 0);
|
||||
doc.Ctor(Bry_.Empty, root);
|
||||
this.cur = root;
|
||||
stack.Clear();
|
||||
return this;
|
||||
}
|
||||
public JsonDocBldr NdeBgn(String key) {
|
||||
Json_nde nde = Json_nde.NewByDoc(doc, 1);
|
||||
if (cur.Tid() == Json_itm_.Tid__nde) {
|
||||
Json_kv kv = new Json_kv(Json_itm_str.NewByVal(key), nde);
|
||||
cur.Add(kv);
|
||||
}
|
||||
else {
|
||||
cur.Add(nde);
|
||||
}
|
||||
stack.Add(cur);
|
||||
cur = nde;
|
||||
return this;
|
||||
}
|
||||
public JsonDocBldr NdeEnd() {
|
||||
this.cur = (Json_grp)List_adp_.Pop_last(stack);
|
||||
return this;
|
||||
}
|
||||
public JsonDocBldr KvBool(String key, boolean val) {return Kv(key, Json_itm_bool.Get(val));}
|
||||
public JsonDocBldr KvInt(String key, int val) {return Kv(key, Json_itm_int.NewByVal(val));}
|
||||
public JsonDocBldr KvStr(String key, byte[] val) {return Kv(key, Json_itm_str.NewByVal(String_.new_u8(val)));}
|
||||
public JsonDocBldr KvStr(String key, String val) {return Kv(key, Json_itm_str.NewByVal(val));}
|
||||
private JsonDocBldr Kv(String key, Json_itm val) {
|
||||
Json_kv rv = new Json_kv(Json_itm_str.NewByVal(key), val);
|
||||
cur.Add(rv);
|
||||
return this;
|
||||
}
|
||||
public Json_doc ToDoc() {
|
||||
return doc;
|
||||
}
|
||||
public Json_nde ToRootNde() {
|
||||
return doc.Root_nde();
|
||||
}
|
||||
public static JsonDocBldr NewRootNde() {return new JsonDocBldr().Clear(true);}
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.jsons; import gplx.*; import gplx.langs.*;
|
||||
public class Json_doc_bldr {
|
||||
public Json_nde Nde(Json_doc jdoc) {return factory.Nde(jdoc, -1);}
|
||||
public Json_nde Nde(Json_doc jdoc, Json_grp owner) {
|
||||
Json_nde rv = factory.Nde(jdoc, -1);
|
||||
owner.Add(rv);
|
||||
return rv;
|
||||
}
|
||||
public Json_itm Str(byte[] v) {return Str(String_.new_u8(v));}
|
||||
public Json_itm Str(String v) {return Json_itm_tmp.new_str_(v);}
|
||||
public Json_itm Int(int v) {return Json_itm_tmp.new_int_(v);}
|
||||
public Json_kv Kv_int(Json_grp owner, String key, int val) {Json_kv rv = factory.Kv(Json_itm_tmp.new_str_(key), Json_itm_tmp.new_int_(val)); owner.Add(rv); return rv;}
|
||||
public Json_kv Kv_str(Json_grp owner, String key, String val) {Json_kv rv = factory.Kv(Json_itm_tmp.new_str_(key), Json_itm_tmp.new_str_(val)); owner.Add(rv); return rv;}
|
||||
public Json_ary Kv_ary(Json_grp owner, String key, Json_itm... subs) {
|
||||
Json_itm key_itm = Json_itm_tmp.new_str_(key);
|
||||
Json_ary val_ary = factory.Ary(-1, -1);
|
||||
Json_kv kv = factory.Kv(key_itm, val_ary);
|
||||
owner.Add(kv);
|
||||
int len = subs.length;
|
||||
for (int i = 0; i < len; i++)
|
||||
val_ary.Add(subs[i]);
|
||||
return val_ary;
|
||||
}
|
||||
Json_doc doc = new Json_doc(); Json_factory factory = new Json_factory();
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.jsons; import gplx.*; import gplx.langs.*;
|
||||
public class Json_factory {
|
||||
public Json_itm Null() {return Json_itm_null.Null;}
|
||||
public Json_itm Bool_n() {return Json_itm_bool.Bool_n;}
|
||||
public Json_itm Bool_y() {return Json_itm_bool.Bool_y;}
|
||||
public Json_itm_int Int(Json_doc doc, int bgn, int end) {return new Json_itm_int(doc, bgn, end);}
|
||||
public Json_itm_long Long(Json_doc doc, int bgn, int end) {return new Json_itm_long(doc, bgn, end);}
|
||||
public Json_itm Decimal(Json_doc doc, int bgn, int end) {return new Json_itm_decimal(doc, bgn, end);}
|
||||
public Json_itm Str(Json_doc doc, int bgn, int end, boolean exact) {return new Json_itm_str(doc, bgn, end, exact);}
|
||||
public Json_kv Kv(Json_itm key, Json_itm val) {return new Json_kv(key, val);}
|
||||
public Json_ary Ary(int bgn, int end) {return new Json_ary(bgn, end);}
|
||||
public Json_nde Nde(Json_doc doc, int bgn) {return new Json_nde(doc, bgn);}
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.jsons; import gplx.*; import gplx.langs.*;
|
||||
public class Json_itm_tmp implements Json_itm { // TEST:
|
||||
public Json_itm_tmp(byte tid, String data) {this.tid = tid; this.data = data;}
|
||||
public byte Tid() {return tid;} private byte tid;
|
||||
public byte[] Data_bry() {return Bry_.new_u8(Object_.Xto_str_strict_or_empty(data));}
|
||||
public int Src_bgn() {return -1;}
|
||||
public int Src_end() {return -1;}
|
||||
public Object Data() {return data;} private String data;
|
||||
public void Print_as_json(Bry_bfr bfr, int depth) {bfr.Add_str_u8(data);}
|
||||
public boolean Data_eq(byte[] comp) {return false;}
|
||||
public void Clear() {}
|
||||
public static Json_itm new_str_(String v) {return new Json_itm_tmp(Json_itm_.Tid__str, "\"" + v + "\"");}
|
||||
public static Json_itm new_int_(int v) {return new Json_itm_tmp(Json_itm_.Tid__int, Int_.To_str(v));}
|
||||
}
|
@ -1,191 +1,217 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2020 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.jsons;
|
||||
|
||||
import gplx.Array_;
|
||||
import gplx.Bool_;
|
||||
import gplx.Bry_;
|
||||
import gplx.Bry_bfr;
|
||||
import gplx.Byte_ascii;
|
||||
import gplx.DateAdp;
|
||||
import gplx.DateAdp_;
|
||||
import gplx.Err_;
|
||||
import gplx.Hash_adp_bry;
|
||||
import gplx.Int_;
|
||||
import gplx.Long_;
|
||||
import gplx.String_;
|
||||
|
||||
public class Json_nde extends Json_itm_base implements Json_grp {
|
||||
private Json_itm[] subs = Json_itm_.Ary_empty; private int subs_len = 0, subs_max = 0;
|
||||
private Hash_adp_bry subs_hash;
|
||||
public Json_nde(Json_doc jdoc, int src_bgn) {this.jdoc = jdoc; this.Ctor(src_bgn, -1);}
|
||||
@Override public byte Tid() {return Json_itm_.Tid__nde;}
|
||||
public Json_doc Doc() {return jdoc;} private final Json_doc jdoc;
|
||||
public void Src_end_(int v) {this.src_end = v;}
|
||||
@Override public Object Data() {return null;}
|
||||
@Override public byte[] Data_bry() {return null;}
|
||||
public int Len() {return subs_len;}
|
||||
public Json_itm Get_at(int i) {return subs[i];}
|
||||
public Json_itm Get_as_itm_or_null(String key) {return Get_as_itm_or_null(Bry_.new_u8(key));}
|
||||
public Json_itm Get_as_itm_or_null(byte[] key) {if (subs_hash == null) subs_hash = subs_hash_init(); return (Json_itm)subs_hash.Get_by_bry(key);}
|
||||
public Json_ary Get_as_ary(int idx) {return Json_ary.cast(Get_at(idx));}
|
||||
public Json_nde Get_as_nde(String key) {return Json_nde.cast(Get_as_itm_or_null(Bry_.new_u8(key)));}
|
||||
public Json_nde Get_as_nde(int idx) {return Json_nde.cast(Get_at(idx));}
|
||||
public Json_ary Get_as_ary(String key) {return Get_as_ary(Bry_.new_u8(key));}
|
||||
public Json_ary Get_as_ary(byte[] key) {
|
||||
Json_itm rv = Get_as_itm_or_null(key); if (rv == null) throw Err_.new_("json", "key missing", "key", key);
|
||||
return Json_ary.cast(rv);
|
||||
}
|
||||
public Json_ary Get_as_ary_or_null(String key) {return Get_as_ary_or_null(Bry_.new_u8(key));}
|
||||
public Json_ary Get_as_ary_or_null(byte[] key) {
|
||||
Json_itm rv = Get_as_itm_or_null(key);
|
||||
return rv == null
|
||||
? null
|
||||
: Json_ary.cast(rv);
|
||||
}
|
||||
public byte[] Get_as_bry(String key) {
|
||||
byte[] rv = Get_as_bry_or(Bry_.new_u8(key), null); if (rv == null) throw Err_.new_("json", "key missing", "key", key);
|
||||
return rv;
|
||||
}
|
||||
public byte[] Get_as_bry_or(byte[] key, byte[] or) {
|
||||
Json_itm rv = Get_as_itm_or_null(key);
|
||||
return rv == null ? or : rv.Data_bry();
|
||||
}
|
||||
public String Get_as_str(String key) {
|
||||
String rv = Get_as_str_or(key, null); if (rv == null) throw Err_.new_("json", "key missing", "key", key);
|
||||
return rv;
|
||||
}
|
||||
public String Get_as_str_or(String key, String or) {return Get_as_str_or(Bry_.new_u8(key), or);}
|
||||
public String Get_as_str_or(byte[] key, String or) {
|
||||
byte[] rv = Get_as_bry_or(key, null);
|
||||
return rv == null ? or : String_.new_u8(rv);
|
||||
}
|
||||
public int Get_as_int(String key) {
|
||||
int rv = Get_as_int_or(key, Int_.Min_value); if (rv == Int_.Min_value) throw Err_.new_("json", "key missing", "key", key);
|
||||
return rv;
|
||||
}
|
||||
public int Get_as_int_or(String key, int or) {return Get_as_int_or(Bry_.new_u8(key), or);}
|
||||
public int Get_as_int_or(byte[] key, int or) {
|
||||
byte[] rv = Get_as_bry_or(key, null);
|
||||
return rv == null ? or : Bry_.To_int(rv);
|
||||
}
|
||||
public long Get_as_long(String key) {
|
||||
long rv = Get_as_long_or(key, Long_.Min_value); if (rv == Long_.Min_value) throw Err_.new_("json", "key missing", "key", key);
|
||||
return rv;
|
||||
}
|
||||
public long Get_as_long_or(String key, long or) {return Get_as_long_or(Bry_.new_u8(key), or);}
|
||||
public long Get_as_long_or(byte[] key, long or) {
|
||||
byte[] rv = Get_as_bry_or(key, null);
|
||||
return rv == null ? or : Bry_.To_long_or(rv, or);
|
||||
}
|
||||
public boolean Get_as_bool_or(String key, boolean or) {return Get_as_bool_or(Bry_.new_u8(key), or);}
|
||||
public boolean Get_as_bool_or(byte[] key, boolean or) {
|
||||
byte[] rv = Get_as_bry_or(key, null);
|
||||
return rv == null ? or : Bry_.Eq(rv, Bool_.True_bry);
|
||||
}
|
||||
public DateAdp Get_as_date_by_utc(String key) {
|
||||
byte[] rv = Get_as_bry_or(Bry_.new_u8(key), null); if (rv == null) throw Err_.new_("json", "key missing", "key", key);
|
||||
return DateAdp_.parse_gplx(String_.new_u8(rv));
|
||||
}
|
||||
|
||||
// to convert
|
||||
public boolean Has(byte[] key) {return Get_bry(key, null) != null;}
|
||||
public Json_kv Get_at_as_kv(int i) {
|
||||
Json_itm rv_itm = Get_at(i);
|
||||
Json_kv rv = Json_kv.cast(rv_itm); if (rv == null) throw Err_.new_("json", "sub is not kv", "i", i, "src", Bry_.Mid(jdoc.Src(), this.Src_bgn(), src_end));
|
||||
return rv;
|
||||
}
|
||||
|
||||
public Json_kv Get_kv(byte[] key) {return Json_kv.cast(Get_itm(key));}
|
||||
public Json_nde Get(String key) {return Get(Bry_.new_u8(key));}
|
||||
public Json_nde Get(byte[] key) {
|
||||
Json_kv kv = Json_kv.cast(this.Get_itm(key)); if (kv == null) throw Err_.new_("json", "kv not found", "key", key);
|
||||
Json_nde rv = Json_nde.cast(kv.Val()); if (rv == null) throw Err_.new_("json", "nde not found", "key", key);
|
||||
return rv;
|
||||
}
|
||||
public Json_itm Get_itm(byte[] key) {
|
||||
for (int i = 0; i < subs_len; i++) {
|
||||
Json_itm itm = subs[i];
|
||||
if (itm.Tid() == Json_itm_.Tid__kv) {
|
||||
Json_kv itm_as_kv = (Json_kv)itm;
|
||||
if (Bry_.Eq(key, itm_as_kv.Key().Data_bry()))
|
||||
return itm;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public Json_ary Get_ary(String key) {return Get_ary(Bry_.new_u8(key));}
|
||||
public Json_ary Get_ary(byte[] key) {return Json_ary.cast(Get_kv(key).Val_as_ary());}
|
||||
public String Get_str(String key) {return String_.new_u8(Get_bry(Bry_.new_u8(key)));}
|
||||
public byte[] Get_bry(byte[] key) {
|
||||
byte[] rv = Get_bry(key, null); if (rv == null) throw Err_.new_("json", "key missing", "key", key);
|
||||
return rv;
|
||||
}
|
||||
public byte[] Get_bry_or_null(String key) {return Get_bry(Bry_.new_u8(key), null);}
|
||||
public byte[] Get_bry_or_null(byte[] key) {return Get_bry(key, null);}
|
||||
public byte[] Get_bry(byte[] key, byte[] or) {
|
||||
Json_itm kv_obj = Get_itm(key);
|
||||
if (kv_obj == null) return or; // key not found;
|
||||
if (kv_obj.Tid() != Json_itm_.Tid__kv) return or; // key is not a key_val
|
||||
Json_kv kv = (Json_kv)kv_obj;
|
||||
Json_itm val = kv.Val();
|
||||
return (val == null) ? or : val.Data_bry();
|
||||
}
|
||||
public Json_nde Add_many(Json_itm... ary) {
|
||||
int len = ary.length;
|
||||
for (int i = 0; i < len; i++)
|
||||
Add(ary[i]);
|
||||
return this;
|
||||
}
|
||||
public void Add(Json_itm itm) {
|
||||
int new_len = subs_len + 1;
|
||||
if (new_len > subs_max) { // ary too small >>> expand
|
||||
subs_max = new_len * 2;
|
||||
Json_itm[] new_subs = new Json_itm[subs_max];
|
||||
Array_.Copy_to(subs, 0, new_subs, 0, subs_len);
|
||||
subs = new_subs;
|
||||
}
|
||||
subs[subs_len] = (Json_itm)itm;
|
||||
subs_len = new_len;
|
||||
subs_hash = null;
|
||||
}
|
||||
@Override public void Print_as_json(Bry_bfr bfr, int depth) {
|
||||
if (bfr.Len() != 0) bfr.Add_byte_nl();
|
||||
Json_grp_.Print_indent(bfr, depth);
|
||||
bfr.Add_byte(Byte_ascii.Curly_bgn).Add_byte(Byte_ascii.Space);
|
||||
for (int i = 0; i < subs_len; i++) {
|
||||
if (i != 0) {
|
||||
Json_grp_.Print_nl(bfr); Json_grp_.Print_indent(bfr, depth);
|
||||
bfr.Add_byte(Byte_ascii.Comma).Add_byte(Byte_ascii.Space);
|
||||
}
|
||||
subs[i].Print_as_json(bfr, depth + 1);
|
||||
}
|
||||
Json_grp_.Print_nl(bfr); Json_grp_.Print_indent(bfr, depth);
|
||||
bfr.Add_byte(Byte_ascii.Curly_end).Add_byte_nl();
|
||||
}
|
||||
private Hash_adp_bry subs_hash_init() {
|
||||
Hash_adp_bry rv = Hash_adp_bry.cs();
|
||||
for (int i = 0; i < subs_len; ++i) {
|
||||
Json_itm itm = subs[i];
|
||||
if (itm.Tid() == Json_itm_.Tid__kv) {
|
||||
Json_kv itm_as_kv = (Json_kv)itm;
|
||||
rv.Add(itm_as_kv.Key().Data_bry(), itm_as_kv.Val());
|
||||
}
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public static Json_nde cast(Json_itm v) {return v == null || v.Tid() != Json_itm_.Tid__nde ? null : (Json_nde)v;}
|
||||
}
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2020 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.jsons;
|
||||
|
||||
import gplx.Array_;
|
||||
import gplx.Bool_;
|
||||
import gplx.Bry_;
|
||||
import gplx.Bry_bfr;
|
||||
import gplx.Byte_ascii;
|
||||
import gplx.DateAdp;
|
||||
import gplx.DateAdp_;
|
||||
import gplx.Decimal_adp_;
|
||||
import gplx.Err_;
|
||||
import gplx.Hash_adp_bry;
|
||||
import gplx.Int_;
|
||||
import gplx.Long_;
|
||||
import gplx.String_;
|
||||
|
||||
public class Json_nde extends Json_itm_base implements Json_grp {
|
||||
private final int src_bgn;
|
||||
private int src_end;
|
||||
private Json_itm[] subs = Json_itm_.Ary_empty;
|
||||
private int subs_len = 0, subs_max = 0;
|
||||
private Hash_adp_bry subs_hash;
|
||||
|
||||
private Json_nde(Json_doc jdoc, int src_bgn) {
|
||||
this.jdoc = jdoc;
|
||||
this.src_bgn = src_bgn;
|
||||
}
|
||||
@Override public byte Tid() {return Json_itm_.Tid__nde;}
|
||||
public Json_doc Doc() {return jdoc;} private final Json_doc jdoc;
|
||||
public void Src_end_(int v) {this.src_end = v;}
|
||||
@Override public Object Data() {return null;}
|
||||
@Override public byte[] Data_bry() {return null;}
|
||||
public int Len() {return subs_len;}
|
||||
public Json_itm Get_at(int i) {return subs[i];}
|
||||
public Json_itm Get_as_itm_or_null(String key) {return Get_as_itm_or_null(Bry_.new_u8(key));}
|
||||
public Json_itm Get_as_itm_or_null(byte[] key) {if (subs_hash == null) subs_hash = subs_hash_init(); return (Json_itm)subs_hash.Get_by_bry(key);}
|
||||
public Json_ary Get_as_ary(int idx) {return Json_ary.cast(Get_at(idx));}
|
||||
public Json_nde Get_as_nde(String key) {return Json_nde.Cast(Get_as_itm_or_null(Bry_.new_u8(key)));}
|
||||
public Json_nde Get_as_nde(int idx) {return Json_nde.Cast(Get_at(idx));}
|
||||
public Json_ary Get_as_ary(String key) {return Get_as_ary(Bry_.new_u8(key));}
|
||||
public Json_ary Get_as_ary(byte[] key) {
|
||||
Json_itm rv = Get_as_itm_or_null(key); if (rv == null) throw Err_.new_("json", "key missing", "key", key);
|
||||
return Json_ary.cast(rv);
|
||||
}
|
||||
public Json_ary Get_as_ary_or_null(String key) {return Get_as_ary_or_null(Bry_.new_u8(key));}
|
||||
public Json_ary Get_as_ary_or_null(byte[] key) {
|
||||
Json_itm rv = Get_as_itm_or_null(key);
|
||||
return rv == null
|
||||
? null
|
||||
: Json_ary.cast(rv);
|
||||
}
|
||||
public byte[] Get_as_bry(String key) {
|
||||
byte[] rv = Get_as_bry_or(Bry_.new_u8(key), null); if (rv == null) throw Err_.new_("json", "key missing", "key", key);
|
||||
return rv;
|
||||
}
|
||||
public byte[] Get_as_bry_or(byte[] key, byte[] or) {
|
||||
Json_itm rv = Get_as_itm_or_null(key);
|
||||
return rv == null ? or : rv.Data_bry();
|
||||
}
|
||||
public String Get_as_str(String key) {
|
||||
String rv = Get_as_str_or(key, null); if (rv == null) throw Err_.new_("json", "key missing", "key", key);
|
||||
return rv;
|
||||
}
|
||||
public String Get_as_str_or(String key, String or) {return Get_as_str_or(Bry_.new_u8(key), or);}
|
||||
public String Get_as_str_or(byte[] key, String or) {
|
||||
byte[] rv = Get_as_bry_or(key, null);
|
||||
return rv == null ? or : String_.new_u8(rv);
|
||||
}
|
||||
public int Get_as_int(String key) {
|
||||
int rv = Get_as_int_or(key, Int_.Min_value); if (rv == Int_.Min_value) throw Err_.new_("json", "key missing", "key", key);
|
||||
return rv;
|
||||
}
|
||||
public int Get_as_int_or(String key, int or) {return Get_as_int_or(Bry_.new_u8(key), or);}
|
||||
public int Get_as_int_or(byte[] key, int or) {
|
||||
byte[] rv = Get_as_bry_or(key, null);
|
||||
return rv == null ? or : Bry_.To_int(rv);
|
||||
}
|
||||
public long Get_as_long(String key) {
|
||||
long rv = Get_as_long_or(key, Long_.Min_value); if (rv == Long_.Min_value) throw Err_.new_("json", "key missing", "key", key);
|
||||
return rv;
|
||||
}
|
||||
public long Get_as_long_or(String key, long or) {return Get_as_long_or(Bry_.new_u8(key), or);}
|
||||
public long Get_as_long_or(byte[] key, long or) {
|
||||
byte[] rv = Get_as_bry_or(key, null);
|
||||
return rv == null ? or : Bry_.To_long_or(rv, or);
|
||||
}
|
||||
public boolean Get_as_bool_or(String key, boolean or) {return Get_as_bool_or(Bry_.new_u8(key), or);}
|
||||
public boolean Get_as_bool_or(byte[] key, boolean or) {
|
||||
byte[] rv = Get_as_bry_or(key, null);
|
||||
return rv == null ? or : Bry_.Eq(rv, Bool_.True_bry);
|
||||
}
|
||||
public DateAdp Get_as_date_by_utc(String key) {
|
||||
byte[] rv = Get_as_bry_or(Bry_.new_u8(key), null); if (rv == null) throw Err_.new_("json", "key missing", "key", key);
|
||||
return DateAdp_.parse_gplx(String_.new_u8(rv));
|
||||
}
|
||||
|
||||
// to convert
|
||||
public boolean Has(byte[] key) {return Get_bry(key, null) != null;}
|
||||
public Json_kv Get_at_as_kv(int i) {
|
||||
Json_itm rv_itm = Get_at(i);
|
||||
Json_kv rv = Json_kv.Cast(rv_itm);
|
||||
if (rv == null) {
|
||||
byte[] snip = jdoc == null ? Bry_.new_a7("no source") : Bry_.Mid(jdoc.Src(), src_bgn, src_end);
|
||||
throw Err_.new_("json", "sub is not kv", "i", i, "src", snip);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
public Json_kv Get_kv(byte[] key) {return Json_kv.Cast(Get_itm(key));}
|
||||
public Json_nde Get(String key) {return Get(Bry_.new_u8(key));}
|
||||
public Json_nde Get(byte[] key) {
|
||||
Json_kv kv = Json_kv.Cast(this.Get_itm(key)); if (kv == null) throw Err_.new_("json", "kv not found", "key", key);
|
||||
Json_nde rv = Json_nde.Cast(kv.Val()); if (rv == null) throw Err_.new_("json", "nde not found", "key", key);
|
||||
return rv;
|
||||
}
|
||||
public Json_itm Get_itm(byte[] key) {
|
||||
for (int i = 0; i < subs_len; i++) {
|
||||
Json_itm itm = subs[i];
|
||||
if (itm != null && itm.Tid() == Json_itm_.Tid__kv) {
|
||||
Json_kv itm_as_kv = (Json_kv)itm;
|
||||
if (Bry_.Eq(key, itm_as_kv.Key().Data_bry()))
|
||||
return itm;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public Json_ary Get_ary(String key) {return Get_ary(Bry_.new_u8(key));}
|
||||
public Json_ary Get_ary(byte[] key) {return Json_ary.cast(Get_kv(key).Val_as_ary());}
|
||||
public String Get_str(String key) {return String_.new_u8(Get_bry(Bry_.new_u8(key)));}
|
||||
public byte[] Get_bry(byte[] key) {
|
||||
byte[] rv = Get_bry(key, null); if (rv == null) throw Err_.new_("json", "key missing", "key", key);
|
||||
return rv;
|
||||
}
|
||||
public byte[] Get_bry_or_null(String key) {return Get_bry(Bry_.new_u8(key), null);}
|
||||
public byte[] Get_bry_or_null(byte[] key) {return Get_bry(key, null);}
|
||||
public byte[] Get_bry(byte[] key, byte[] or) {
|
||||
Json_itm kv_obj = Get_itm(key);
|
||||
if (kv_obj == null) return or; // key not found;
|
||||
if (kv_obj.Tid() != Json_itm_.Tid__kv) return or; // key is not a key_val
|
||||
Json_kv kv = (Json_kv)kv_obj;
|
||||
Json_itm val = kv.Val();
|
||||
return (val == null) ? or : val.Data_bry();
|
||||
}
|
||||
public void AddKvBool(String key, boolean val) {AddKv(key, Json_itm_bool.Get(val));}
|
||||
public void AddKvInt(String key, int val) {AddKv(key, Json_itm_int.NewByVal(val));}
|
||||
public void AddKvDouble(String key, double val) {AddKv(key, Json_itm_decimal.NewByVal(Decimal_adp_.double_(val)));}
|
||||
public void AddKvStr(String key, byte[] val) {AddKv(key, Json_itm_str.NewByVal(String_.new_u8(val)));}
|
||||
public void AddKvStr(String key, String val) {AddKv(key, Json_itm_str.NewByVal(val));}
|
||||
public void AddKvNde(String key, Json_nde val) {AddKv(key, val);}
|
||||
public void AddKvAry(String key, Json_ary val) {AddKv(key, val);}
|
||||
private void AddKv(String key, Json_itm val) {
|
||||
Json_kv rv = new Json_kv(Json_itm_str.NewByVal(key), val);
|
||||
Add(rv);
|
||||
}
|
||||
public Json_nde Add_many(Json_itm... ary) {
|
||||
int len = ary.length;
|
||||
for (int i = 0; i < len; i++)
|
||||
Add(ary[i]);
|
||||
return this;
|
||||
}
|
||||
public void Add(Json_itm itm) {
|
||||
int new_len = subs_len + 1;
|
||||
if (new_len > subs_max) { // ary too small >>> expand
|
||||
subs_max = new_len * 2;
|
||||
Json_itm[] new_subs = new Json_itm[subs_max];
|
||||
Array_.Copy_to(subs, 0, new_subs, 0, subs_len);
|
||||
subs = new_subs;
|
||||
}
|
||||
subs[subs_len] = (Json_itm)itm;
|
||||
subs_len = new_len;
|
||||
subs_hash = null;
|
||||
}
|
||||
@Override public void Print_as_json(Bry_bfr bfr, int depth) {
|
||||
if (bfr.Len() != 0) bfr.Add_byte_nl();
|
||||
Json_grp_.Print_indent(bfr, depth);
|
||||
bfr.Add_byte(Byte_ascii.Curly_bgn).Add_byte(Byte_ascii.Space);
|
||||
for (int i = 0; i < subs_len; i++) {
|
||||
if (i != 0) {
|
||||
Json_grp_.Print_nl(bfr); Json_grp_.Print_indent(bfr, depth);
|
||||
bfr.Add_byte(Byte_ascii.Comma).Add_byte(Byte_ascii.Space);
|
||||
}
|
||||
subs[i].Print_as_json(bfr, depth + 1);
|
||||
}
|
||||
Json_grp_.Print_nl(bfr); Json_grp_.Print_indent(bfr, depth);
|
||||
bfr.Add_byte(Byte_ascii.Curly_end).Add_byte_nl();
|
||||
}
|
||||
private Hash_adp_bry subs_hash_init() {
|
||||
Hash_adp_bry rv = Hash_adp_bry.cs();
|
||||
for (int i = 0; i < subs_len; ++i) {
|
||||
Json_itm itm = subs[i];
|
||||
if (itm.Tid() == Json_itm_.Tid__kv) {
|
||||
Json_kv itm_as_kv = (Json_kv)itm;
|
||||
rv.Add(itm_as_kv.Key().Data_bry(), itm_as_kv.Val());
|
||||
}
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
public static Json_nde NewByDoc(Json_doc doc, int src_bgn) {return new Json_nde(doc, src_bgn);}
|
||||
public static Json_nde NewByVal() {return new Json_nde(null, -1);}
|
||||
public static Json_nde Cast(Json_itm v) {return v == null || v.Tid() != Json_itm_.Tid__nde ? null : (Json_nde)v;}
|
||||
}
|
||||
|
@ -1,198 +1,197 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2020 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.jsons;
|
||||
|
||||
import gplx.Bool_;
|
||||
import gplx.Bry_;
|
||||
import gplx.Byte_ascii;
|
||||
import gplx.Char_;
|
||||
import gplx.Err;
|
||||
import gplx.Err_;
|
||||
import gplx.Int_;
|
||||
import gplx.String_;
|
||||
import gplx.core.primitives.Gfo_number_parser;
|
||||
|
||||
public class Json_parser {
|
||||
private byte[] src; private int src_len, pos; private final Gfo_number_parser num_parser = new Gfo_number_parser();
|
||||
public Json_factory Factory() {return factory;} private final Json_factory factory = new Json_factory();
|
||||
public Json_doc Parse_by_apos_ary(String... ary) {return Parse_by_apos(String_.Concat_lines_nl(ary));}
|
||||
public Json_doc Parse_by_apos(String s) {return Parse(Bry_.Replace(Bry_.new_u8(s), Byte_ascii.Apos, Byte_ascii.Quote));}
|
||||
public Json_doc Parse(String src) {return Parse(Bry_.new_u8(src));}
|
||||
public Json_doc Parse(byte[] src) {
|
||||
synchronized (factory) {
|
||||
this.src = src; if (src == null) return null;
|
||||
this.src_len = src.length; if (src_len == 0) return null;
|
||||
this.pos = 0;
|
||||
Skip_ws();
|
||||
boolean root_is_nde = true;
|
||||
switch (src[pos]) {
|
||||
case Byte_ascii.Curly_bgn: root_is_nde = Bool_.Y; break;
|
||||
case Byte_ascii.Brack_bgn: root_is_nde = Bool_.N; break;
|
||||
default: return null;
|
||||
}
|
||||
Skip_ws();
|
||||
Json_doc doc = new Json_doc();
|
||||
Json_grp root = null;
|
||||
if (root_is_nde)
|
||||
root = Make_nde(doc);
|
||||
else
|
||||
root = Make_ary(doc);
|
||||
doc.Ctor(src, root);
|
||||
return doc;
|
||||
}
|
||||
}
|
||||
private Json_nde Make_nde(Json_doc doc) {
|
||||
++pos; // brack_bgn
|
||||
Json_nde nde = new Json_nde(doc, pos);
|
||||
while (pos < src_len) {
|
||||
Skip_ws();
|
||||
if (src[pos] == Byte_ascii.Curly_end) {++pos; return nde;}
|
||||
else nde.Add(Make_kv(doc));
|
||||
Skip_ws();
|
||||
switch (src[pos++]) {
|
||||
case Byte_ascii.Comma: break;
|
||||
case Byte_ascii.Curly_end: return nde;
|
||||
default: throw Err_.new_unhandled(src[pos - 1]);
|
||||
}
|
||||
}
|
||||
throw Err_.new_wo_type("eos inside nde");
|
||||
}
|
||||
private Json_itm Make_kv(Json_doc doc) {
|
||||
Json_itm key = Make_string(doc);
|
||||
Skip_ws();
|
||||
Chk(Byte_ascii.Colon);
|
||||
Skip_ws();
|
||||
Json_itm val = Make_val(doc);
|
||||
return new Json_kv(key, val);
|
||||
}
|
||||
private Json_itm Make_val(Json_doc doc) {
|
||||
while (pos < src_len) {
|
||||
byte b = src[pos];
|
||||
switch (b) {
|
||||
case Byte_ascii.Ltr_n: return Make_literal(Bry_null_ull , 3, factory.Null());
|
||||
case Byte_ascii.Ltr_f: return Make_literal(Bry_bool_alse , 4, factory.Bool_n());
|
||||
case Byte_ascii.Ltr_t: return Make_literal(Bry_bool_rue , 3, factory.Bool_y());
|
||||
case Byte_ascii.Quote: return Make_string(doc);
|
||||
case Byte_ascii.Num_0: case Byte_ascii.Num_1: case Byte_ascii.Num_2: case Byte_ascii.Num_3: case Byte_ascii.Num_4:
|
||||
case Byte_ascii.Num_5: case Byte_ascii.Num_6: case Byte_ascii.Num_7: case Byte_ascii.Num_8: case Byte_ascii.Num_9:
|
||||
case Byte_ascii.Dash: return Make_num(doc);
|
||||
case Byte_ascii.Brack_bgn: return Make_ary(doc);
|
||||
case Byte_ascii.Curly_bgn: return Make_nde(doc);
|
||||
}
|
||||
throw Err_.new_unhandled(Char_.To_str(b));
|
||||
}
|
||||
throw Err_.new_wo_type("eos reached in val");
|
||||
}
|
||||
private Json_itm Make_literal(byte[] remainder, int remainder_len, Json_itm singleton) {
|
||||
++pos; // 1st char
|
||||
int literal_end = pos + remainder_len;
|
||||
if (Bry_.Eq(src, pos, literal_end, remainder)) {
|
||||
pos = literal_end;
|
||||
return singleton;
|
||||
}
|
||||
throw Err_.new_("json.parser", "invalid literal", "excerpt", Bry_.Mid_by_len_safe(src, pos - 1, 16));
|
||||
}
|
||||
private Json_itm Make_string(Json_doc doc) {
|
||||
int bgn = pos++; // ++: quote_bgn
|
||||
boolean exact = true;
|
||||
while (pos < src_len) {
|
||||
switch (src[pos]) {
|
||||
case Byte_ascii.Backslash:
|
||||
++pos; // backslash
|
||||
switch (src[pos]) {
|
||||
case Byte_ascii.Ltr_u: pos += 5; break; // \uFFFF 1 u + 4 hex-dec; ISSUE#:486; DATE:2019-06-02
|
||||
default: ++pos; break; // \? " \ / b f n r t
|
||||
}
|
||||
exact = false;
|
||||
break;
|
||||
case Byte_ascii.Quote:
|
||||
return factory.Str(doc, bgn, ++pos, exact); // ++: quote_end
|
||||
default:
|
||||
++pos;
|
||||
break;
|
||||
}
|
||||
}
|
||||
throw Err_.new_wo_type("eos reached inside quote");
|
||||
}
|
||||
private Json_itm Make_num(Json_doc doc) {
|
||||
int num_bgn = pos;
|
||||
boolean loop = true;
|
||||
while (loop) {
|
||||
if (pos == src_len) throw Err_.new_wo_type("eos reached inside num");
|
||||
switch (src[pos]) {
|
||||
case Byte_ascii.Num_0: case Byte_ascii.Num_1: case Byte_ascii.Num_2: case Byte_ascii.Num_3: case Byte_ascii.Num_4:
|
||||
case Byte_ascii.Num_5: case Byte_ascii.Num_6: case Byte_ascii.Num_7: case Byte_ascii.Num_8: case Byte_ascii.Num_9:
|
||||
++pos;
|
||||
break;
|
||||
case Byte_ascii.Dot:
|
||||
case Byte_ascii.Dash: case Byte_ascii.Plus:
|
||||
case Byte_ascii.Ltr_E: case Byte_ascii.Ltr_e: // e e+ e- E E+ E-
|
||||
++pos;
|
||||
break;
|
||||
default:
|
||||
loop = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
num_parser.Parse(src, num_bgn, pos);
|
||||
if (num_parser.Has_frac())
|
||||
return factory.Decimal(doc, num_bgn, pos);
|
||||
else {
|
||||
if (num_parser.Is_int())
|
||||
return factory.Int(doc, num_bgn, pos);
|
||||
else
|
||||
return factory.Long(doc, num_bgn, pos);
|
||||
}
|
||||
}
|
||||
private Json_ary Make_ary(Json_doc doc) {
|
||||
Json_ary rv = factory.Ary(pos++, pos); // brack_bgn
|
||||
while (pos < src_len) {
|
||||
Skip_ws();
|
||||
if (src[pos] == Byte_ascii.Brack_end) {++pos; return rv;}
|
||||
else rv.Add(Make_val(doc));
|
||||
Skip_ws();
|
||||
switch (src[pos]) {
|
||||
case Byte_ascii.Comma: ++pos; break;
|
||||
case Byte_ascii.Brack_end: ++pos; return rv;
|
||||
}
|
||||
}
|
||||
throw Err_.new_wo_type("eos inside ary");
|
||||
}
|
||||
private void Skip_ws() {
|
||||
while (pos < src_len) {
|
||||
switch (src[pos]) {
|
||||
case Byte_ascii.Space: case Byte_ascii.Nl: case Byte_ascii.Tab: case Byte_ascii.Cr: ++pos; break;
|
||||
default: return;
|
||||
}
|
||||
}
|
||||
}
|
||||
private void Chk(byte expd) {
|
||||
if (src[pos] == expd)
|
||||
++pos;
|
||||
else
|
||||
throw err_(src, pos, "expected '{0}' but got '{1}'", Char_.To_str(expd), Char_.To_str(src[pos]));
|
||||
}
|
||||
private Err err_(byte[] src, int bgn, String fmt, Object... args) {return err_(src, bgn, src.length, fmt, args);}
|
||||
private Err err_(byte[] src, int bgn, int src_len, String fmt, Object... args) {
|
||||
String msg = String_.Format(fmt, args) + " " + Int_.To_str(bgn) + " " + String_.new_u8__by_len(src, bgn, 20);
|
||||
return Err_.new_wo_type(msg);
|
||||
}
|
||||
private static final byte[] Bry_bool_rue = Bry_.new_a7("rue"), Bry_bool_alse = Bry_.new_a7("alse"), Bry_null_ull = Bry_.new_a7("ull");
|
||||
public static Json_doc ParseToJdoc(String src) {
|
||||
Json_parser parser = new Json_parser();
|
||||
return parser.Parse(src);
|
||||
}
|
||||
}
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2020 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.jsons;
|
||||
|
||||
import gplx.Bool_;
|
||||
import gplx.Bry_;
|
||||
import gplx.Byte_ascii;
|
||||
import gplx.Char_;
|
||||
import gplx.Err;
|
||||
import gplx.Err_;
|
||||
import gplx.Int_;
|
||||
import gplx.String_;
|
||||
import gplx.core.primitives.Gfo_number_parser;
|
||||
|
||||
public class Json_parser {
|
||||
private byte[] src;
|
||||
private int src_len, pos;
|
||||
private final Gfo_number_parser num_parser = new Gfo_number_parser();
|
||||
public Json_doc Parse_by_apos_ary(String... ary) {return Parse_by_apos(String_.Concat_lines_nl(ary));}
|
||||
public Json_doc Parse_by_apos(String s) {return Parse(Bry_.Replace(Bry_.new_u8(s), Byte_ascii.Apos, Byte_ascii.Quote));}
|
||||
public Json_doc Parse(String src) {return Parse(Bry_.new_u8(src));}
|
||||
public Json_doc Parse(byte[] src) {
|
||||
this.src = src; if (src == null) return null;
|
||||
this.src_len = src.length; if (src_len == 0) return null;
|
||||
this.pos = 0;
|
||||
Skip_ws();
|
||||
boolean root_is_nde = true;
|
||||
switch (src[pos]) {
|
||||
case Byte_ascii.Curly_bgn: root_is_nde = Bool_.Y; break;
|
||||
case Byte_ascii.Brack_bgn: root_is_nde = Bool_.N; break;
|
||||
default: return null;
|
||||
}
|
||||
Skip_ws();
|
||||
Json_doc doc = new Json_doc();
|
||||
Json_grp root = null;
|
||||
if (root_is_nde)
|
||||
root = Make_nde(doc);
|
||||
else
|
||||
root = Make_ary(doc);
|
||||
doc.Ctor(src, root);
|
||||
return doc;
|
||||
}
|
||||
private Json_nde Make_nde(Json_doc doc) {
|
||||
++pos; // brack_bgn
|
||||
Json_nde nde = Json_nde.NewByDoc(doc, pos);
|
||||
while (pos < src_len) {
|
||||
Skip_ws();
|
||||
if (src[pos] == Byte_ascii.Curly_end) {++pos; return nde;}
|
||||
else nde.Add(Make_kv(doc));
|
||||
Skip_ws();
|
||||
switch (src[pos++]) {
|
||||
case Byte_ascii.Comma: break;
|
||||
case Byte_ascii.Curly_end: return nde;
|
||||
default: throw Err_.new_unhandled(src[pos - 1]);
|
||||
}
|
||||
}
|
||||
throw Err_.new_wo_type("eos inside nde");
|
||||
}
|
||||
private Json_itm Make_kv(Json_doc doc) {
|
||||
Json_itm key = Make_string(doc);
|
||||
Skip_ws();
|
||||
Chk(Byte_ascii.Colon);
|
||||
Skip_ws();
|
||||
Json_itm val = Make_val(doc);
|
||||
return new Json_kv(key, val);
|
||||
}
|
||||
private Json_itm Make_val(Json_doc doc) {
|
||||
while (pos < src_len) {
|
||||
byte b = src[pos];
|
||||
switch (b) {
|
||||
case Byte_ascii.Ltr_n: return Make_literal(Bry_null_ull , 3, Json_itm_null.Null);
|
||||
case Byte_ascii.Ltr_f: return Make_literal(Bry_bool_alse , 4, Json_itm_bool.Bool_n);
|
||||
case Byte_ascii.Ltr_t: return Make_literal(Bry_bool_rue , 3, Json_itm_bool.Bool_y);
|
||||
case Byte_ascii.Quote: return Make_string(doc);
|
||||
case Byte_ascii.Num_0: case Byte_ascii.Num_1: case Byte_ascii.Num_2: case Byte_ascii.Num_3: case Byte_ascii.Num_4:
|
||||
case Byte_ascii.Num_5: case Byte_ascii.Num_6: case Byte_ascii.Num_7: case Byte_ascii.Num_8: case Byte_ascii.Num_9:
|
||||
case Byte_ascii.Dash: return Make_num(doc);
|
||||
case Byte_ascii.Brack_bgn: return Make_ary(doc);
|
||||
case Byte_ascii.Curly_bgn: return Make_nde(doc);
|
||||
}
|
||||
throw Err_.new_unhandled(Char_.To_str(b));
|
||||
}
|
||||
throw Err_.new_wo_type("eos reached in val");
|
||||
}
|
||||
private Json_itm Make_literal(byte[] remainder, int remainder_len, Json_itm singleton) {
|
||||
++pos; // 1st char
|
||||
int literal_end = pos + remainder_len;
|
||||
if (Bry_.Eq(src, pos, literal_end, remainder)) {
|
||||
pos = literal_end;
|
||||
return singleton;
|
||||
}
|
||||
throw Err_.new_("json.parser", "invalid literal", "excerpt", Bry_.Mid_by_len_safe(src, pos - 1, 16));
|
||||
}
|
||||
private Json_itm Make_string(Json_doc doc) {
|
||||
int bgn = pos++; // ++: quote_bgn
|
||||
boolean escaped = false;
|
||||
while (pos < src_len) {
|
||||
switch (src[pos]) {
|
||||
case Byte_ascii.Backslash:
|
||||
++pos; // backslash
|
||||
switch (src[pos]) {
|
||||
case Byte_ascii.Ltr_u: pos += 5; break; // \uFFFF 1 u + 4 hex-dec; ISSUE#:486; DATE:2019-06-02
|
||||
default: ++pos; break; // \? " \ / b f n r t
|
||||
}
|
||||
escaped = true;
|
||||
break;
|
||||
case Byte_ascii.Quote:
|
||||
return Json_itm_str.NewByDoc(doc, bgn, ++pos, escaped); // ++: quote_end
|
||||
default:
|
||||
++pos;
|
||||
break;
|
||||
}
|
||||
}
|
||||
throw Err_.new_wo_type("eos reached inside quote");
|
||||
}
|
||||
private Json_itm Make_num(Json_doc doc) {
|
||||
int num_bgn = pos;
|
||||
boolean loop = true;
|
||||
while (loop) {
|
||||
if (pos == src_len) throw Err_.new_wo_type("eos reached inside num");
|
||||
switch (src[pos]) {
|
||||
case Byte_ascii.Num_0: case Byte_ascii.Num_1: case Byte_ascii.Num_2: case Byte_ascii.Num_3: case Byte_ascii.Num_4:
|
||||
case Byte_ascii.Num_5: case Byte_ascii.Num_6: case Byte_ascii.Num_7: case Byte_ascii.Num_8: case Byte_ascii.Num_9:
|
||||
++pos;
|
||||
break;
|
||||
case Byte_ascii.Dot:
|
||||
case Byte_ascii.Dash: case Byte_ascii.Plus:
|
||||
case Byte_ascii.Ltr_E: case Byte_ascii.Ltr_e: // e e+ e- E E+ E-
|
||||
++pos;
|
||||
break;
|
||||
default:
|
||||
loop = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
num_parser.Parse(src, num_bgn, pos);
|
||||
if (num_parser.Has_frac())
|
||||
return Json_itm_decimal.NewByDoc(doc, num_bgn, pos);
|
||||
else {
|
||||
if (num_parser.Is_int())
|
||||
return Json_itm_int.NewByDoc(doc, num_bgn, pos);
|
||||
else
|
||||
return Json_itm_long.NewByDoc(doc, num_bgn, pos);
|
||||
}
|
||||
}
|
||||
private Json_ary Make_ary(Json_doc doc) {
|
||||
Json_ary rv = Json_ary.NewByDoc(doc, pos++, pos); // brack_bgn
|
||||
while (pos < src_len) {
|
||||
Skip_ws();
|
||||
if (src[pos] == Byte_ascii.Brack_end) {++pos; return rv;}
|
||||
else rv.Add(Make_val(doc));
|
||||
Skip_ws();
|
||||
switch (src[pos]) {
|
||||
case Byte_ascii.Comma: ++pos; break;
|
||||
case Byte_ascii.Brack_end: ++pos; return rv;
|
||||
}
|
||||
}
|
||||
throw Err_.new_wo_type("eos inside ary");
|
||||
}
|
||||
private void Skip_ws() {
|
||||
while (pos < src_len) {
|
||||
switch (src[pos]) {
|
||||
case Byte_ascii.Space: case Byte_ascii.Nl: case Byte_ascii.Tab: case Byte_ascii.Cr: ++pos; break;
|
||||
default: return;
|
||||
}
|
||||
}
|
||||
}
|
||||
private void Chk(byte expd) {
|
||||
if (src[pos] == expd)
|
||||
++pos;
|
||||
else
|
||||
throw err_(src, pos, "expected '{0}' but got '{1}'", Char_.To_str(expd), Char_.To_str(src[pos]));
|
||||
}
|
||||
private Err err_(byte[] src, int bgn, String fmt, Object... args) {return err_(src, bgn, src.length, fmt, args);}
|
||||
private Err err_(byte[] src, int bgn, int src_len, String fmt, Object... args) {
|
||||
String msg = String_.Format(fmt, args) + " " + Int_.To_str(bgn) + " " + String_.new_u8__by_len(src, bgn, 20);
|
||||
return Err_.new_wo_type(msg);
|
||||
}
|
||||
private static final byte[] Bry_bool_rue = Bry_.new_a7("rue"), Bry_bool_alse = Bry_.new_a7("alse"), Bry_null_ull = Bry_.new_a7("ull");
|
||||
public static Json_doc ParseToJdoc(String src) {
|
||||
Json_parser parser = new Json_parser();
|
||||
return parser.Parse(src);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,112 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2020 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.mustaches;
|
||||
|
||||
import gplx.Bool_;
|
||||
import gplx.Bry_;
|
||||
import gplx.String_;
|
||||
import gplx.langs.jsons.Json_ary;
|
||||
import gplx.langs.jsons.Json_itm;
|
||||
import gplx.langs.jsons.Json_itm_;
|
||||
import gplx.langs.jsons.Json_kv;
|
||||
import gplx.langs.jsons.Json_nde;
|
||||
import gplx.objects.Object_;
|
||||
|
||||
public class JsonMustacheNde implements Mustache_doc_itm {
|
||||
private final Json_nde nde;
|
||||
public JsonMustacheNde(Json_nde nde) {this.nde = nde;}
|
||||
public boolean Mustache__write(String key, Mustache_bfr bfr) {
|
||||
Json_itm itm = nde.Get_itm(Bry_.new_u8(key));
|
||||
if (itm == null) { // mustacheKey does not exist in current jsonNde
|
||||
return false;
|
||||
}
|
||||
else { // mustacheKey exists
|
||||
switch (itm.Tid()) {
|
||||
// array / bool node -> ignore; EX: `{{#person}}Never shown{{/person}}`
|
||||
case Json_itm_.Tid__bool:
|
||||
case Json_itm_.Tid__ary:
|
||||
case Json_itm_.Tid__nde:
|
||||
return false;
|
||||
// item node -> render it; EX: `Hello {{name}}`
|
||||
default:
|
||||
bfr.Add_bry(Json_kv.Cast(itm).Val_as_bry());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
public Mustache_doc_itm[] Mustache__subs(String key) {
|
||||
Json_itm itm = nde.Get_itm(Bry_.new_u8(key));
|
||||
if (itm == null) { // mustacheKey does not exist in current jsonNde
|
||||
return Mustache_doc_itm_.Ary__bool__n;
|
||||
}
|
||||
else { // mustacheKey exists
|
||||
if (itm.Tid() == Json_itm_.Tid__kv) {
|
||||
Json_kv kv = Json_kv.Cast(itm);
|
||||
switch (kv.Val().Tid()) {
|
||||
// bool node -> render; EX: `{{#person}}Never shown{{/person}}`
|
||||
case Json_itm_.Tid__bool:
|
||||
boolean dataVal = Bool_.Cast(kv.Val().Data());
|
||||
return dataVal ? Mustache_doc_itm_.Ary__bool__y : Mustache_doc_itm_.Ary__bool__n;
|
||||
// array node -> render; EX: `{{#repo}} <b>{{name}}</b>{{/repo}}`
|
||||
case Json_itm_.Tid__ary:
|
||||
return ToJsonMustachNdeAry(itm);
|
||||
// item node -> render only if key matchers
|
||||
default:
|
||||
return new Mustache_doc_itm[] {new JsonMustacheVal(true, key, kv.Val().Data())};
|
||||
}
|
||||
}
|
||||
else {
|
||||
return Mustache_doc_itm_.Ary__bool__n;
|
||||
}
|
||||
}
|
||||
}
|
||||
private static Mustache_doc_itm[] ToJsonMustachNdeAry(Json_itm itm) {
|
||||
Json_ary dataAry = Json_ary.cast_or_null(Json_kv.Cast(itm).Val());
|
||||
int subs_len = dataAry.Len();
|
||||
Mustache_doc_itm[] rv = new Mustache_doc_itm[subs_len];
|
||||
for (int i = 0; i < subs_len; i++) {
|
||||
Json_itm sub = dataAry.Get_at(i);
|
||||
if (sub.Tid() == Json_itm_.Tid__nde) {
|
||||
rv[i] = new JsonMustacheNde((Json_nde)sub);
|
||||
}
|
||||
else {
|
||||
rv[i] = new JsonMustacheVal(false, Mustache_tkn_def.ItemString, sub.Data());
|
||||
}
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
class JsonMustacheVal implements Mustache_doc_itm {
|
||||
private final boolean fromArray;
|
||||
private final String jsonKey;
|
||||
private final Object jsonVal;
|
||||
public JsonMustacheVal(boolean fromArray, String jsonKey, Object jsonVal) {
|
||||
this.fromArray = fromArray;
|
||||
this.jsonKey = jsonKey;
|
||||
this.jsonVal = jsonVal;
|
||||
}
|
||||
public boolean Mustache__write(String mustacheKey, Mustache_bfr bfr) {
|
||||
if ( (String_.Eq(mustacheKey, jsonKey)) // print if `{{match}}`; EX: `{{#prop}}{{prop}}{{/prop}}`
|
||||
|| (String_.Eq(mustacheKey, Mustache_tkn_def.ItemString) && fromArray)) { // print if `{{.}}` and from array; EX: `{{#array}}{{.}}{{/array}}`
|
||||
bfr.Add_bry(Bry_.new_u8(Object_.To_str(jsonVal)));
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public Mustache_doc_itm[] Mustache__subs(String key) {return Mustache_doc_itm_.Ary__empty;}
|
||||
}
|
@ -0,0 +1,275 @@
|
||||
package gplx.xowa.addons.htmls.sidebars;
|
||||
|
||||
import gplx.Bry_;
|
||||
import gplx.Bry_bfr;
|
||||
import gplx.Bry_bfr_;
|
||||
import gplx.Io_url;
|
||||
import gplx.String_;
|
||||
import gplx.langs.jsons.Json_nde;
|
||||
import gplx.langs.mustaches.JsonMustacheNde;
|
||||
import gplx.langs.mustaches.Mustache_bfr;
|
||||
import gplx.langs.mustaches.Mustache_render_ctx;
|
||||
import gplx.langs.mustaches.Mustache_tkn_itm;
|
||||
import gplx.langs.mustaches.Mustache_tkn_parser;
|
||||
import gplx.xowa.Xowe_wiki;
|
||||
import gplx.xowa.langs.msgs.Xol_msg_mgr;
|
||||
|
||||
public class Db_Nav_template {
|
||||
public Mustache_tkn_itm Navigation_root() {return navigation_root;} private Mustache_tkn_itm navigation_root;
|
||||
private Xol_msg_mgr msg_mgr;
|
||||
private Json_nde msgdata;
|
||||
private Xowe_wiki wiki;
|
||||
private static boolean once = true;
|
||||
private static Mustache_tkn_itm menu_root;
|
||||
|
||||
public static void Build_Sidebar(Xowe_wiki wiki, Bry_bfr bfr, byte[] id, byte[] text, byte[] itms) {
|
||||
if (once) {
|
||||
once = false;
|
||||
Io_url template_root = wiki.Appe().Fsys_mgr().Bin_any_dir().GenSubDir_nest("xowa", "xtns", "Skin-Vector", "templates");
|
||||
Mustache_tkn_parser parser = new Mustache_tkn_parser(template_root);
|
||||
menu_root = parser.Parse("Menu");
|
||||
}
|
||||
Json_nde data = s_getMenuData(wiki, text, itms, MENU_TYPE_PORTAL);
|
||||
|
||||
// Bry_bfr tmp_bfr = Bry_bfr_.New();
|
||||
Mustache_render_ctx mctx = new Mustache_render_ctx().Init(new JsonMustacheNde(data));
|
||||
Mustache_bfr mbfr = Mustache_bfr.New_bfr(bfr);
|
||||
menu_root.Render(mbfr, mctx);
|
||||
// byte[] result = mbfr.To_bry_and_clear();
|
||||
// System.out.println(String_.new_u8(result));
|
||||
}
|
||||
public void Init(Xowe_wiki wiki) {
|
||||
Io_url template_root = wiki.Appe().Fsys_mgr().Bin_any_dir().GenSubDir_nest("xowa", "xtns", "Skin-Vector", "templates");
|
||||
Mustache_tkn_parser parser = new Mustache_tkn_parser(template_root);
|
||||
navigation_root = parser.Parse("Navigation");
|
||||
this.wiki = wiki;
|
||||
|
||||
msg_mgr = wiki.Lang().Msg_mgr();
|
||||
build_msg();
|
||||
|
||||
Test();
|
||||
}
|
||||
|
||||
private String[] msgs = new String[] {
|
||||
"vector-opt-out-tooltip",
|
||||
"vector-opt-out",
|
||||
"navigation-heading",
|
||||
"vector-action-toggle-sidebar",
|
||||
"vector-jumptonavigation",
|
||||
"vector-jumptosearch",
|
||||
"vector-jumptocontent",
|
||||
"sitesubtitle",
|
||||
"sitetitle",
|
||||
"tagline"
|
||||
};
|
||||
|
||||
//all thes messages should be preprocessed (per language) as $data["msg-{$message}"] = $this->msg( $message )->text();
|
||||
private void build_msg() {
|
||||
int msg_len = msgs.length;
|
||||
msgdata = Json_nde.NewByVal();
|
||||
for (int i = 0; i < msg_len; i++) {
|
||||
String msg = msgs[i];
|
||||
msgdata.AddKvStr("msg-" + msg, msg_mgr.Val_by_str_or_empty(msg));
|
||||
}
|
||||
}
|
||||
private void Test() {
|
||||
Json_nde jnde = Json_nde.NewByVal();
|
||||
jnde.AddKvStr("class", "CLASS");
|
||||
jnde.AddKvStr("text", "TEXT");
|
||||
jnde.AddKvStr("href", "URL_str");
|
||||
jnde.AddKvBool("exists", true);
|
||||
jnde.AddKvBool("primary", true);
|
||||
jnde.AddKvStr("link-class", String_.Empty);
|
||||
jnde.AddKvStr("context", "subject");
|
||||
|
||||
Json_nde namespaces = Json_nde.NewByVal();
|
||||
namespaces.AddKvNde("subject", jnde);
|
||||
|
||||
//Json_nde data_namespaces = new Json_nde(null, -1);
|
||||
msgdata.AddKvNde("data-namespace-tabs",
|
||||
getMenuData(
|
||||
Bry_.new_a7("namespaces"),
|
||||
namespaces,
|
||||
MENU_TYPE_TABS
|
||||
)
|
||||
);
|
||||
|
||||
Bry_bfr tmp_bfr = Bry_bfr_.New();
|
||||
Mustache_render_ctx mctx = new Mustache_render_ctx().Init(new JsonMustacheNde(msgdata));
|
||||
Mustache_bfr mbfr = Mustache_bfr.New_bfr(tmp_bfr);
|
||||
navigation_root.Render(mbfr, mctx);
|
||||
byte[] result = mbfr.To_bry_and_clear();
|
||||
System.out.println(String_.new_u8(result));
|
||||
}
|
||||
|
||||
/* Vector/SkinVector.php */
|
||||
private static int MENU_TYPE_DROPDOWN = 0, MENU_TYPE_TABS = 1, MENU_TYPE_PORTAL = 2, MENU_TYPE_DEFAULT = 3;
|
||||
private static byte[][] extraClasses = new byte[][] {
|
||||
Bry_.new_a7("vector-menu vector-menu-dropdown vectorMenu"),
|
||||
Bry_.new_a7("vector-menu vector-menu-tabs vectorTabs"),
|
||||
Bry_.new_a7("vector-menu vector-menu-portal portal"),
|
||||
Bry_.new_a7("vector-menu")
|
||||
};
|
||||
private Json_nde getMenuData(byte[] label, Json_nde urls, int type) { return getMenuData(label, urls, type, false); }
|
||||
private Json_nde getMenuData(byte[] label_bry, Json_nde urls, int type, boolean setLabelToSelected) {
|
||||
//private function getMenuData(
|
||||
// string $label,
|
||||
// array $urls = [],
|
||||
// int $type = self::MENU_TYPE_DEFAULT,
|
||||
// bool $setLabelToSelected = false
|
||||
// ) : array {
|
||||
// $skin = $this->getSkin();
|
||||
// $extraClasses = [
|
||||
// self::MENU_TYPE_DROPDOWN => 'vector-menu vector-menu-dropdown vectorMenu',
|
||||
// self::MENU_TYPE_TABS => 'vector-menu vector-menu-tabs vectorTabs',
|
||||
// self::MENU_TYPE_PORTAL => 'vector-menu vector-menu-portal portal',
|
||||
// self::MENU_TYPE_DEFAULT => 'vector-menu',
|
||||
// ];
|
||||
// // A list of classes to apply the list element and override the default behavior.
|
||||
// $listClasses = [
|
||||
// // `.menu` is on the portal for historic reasons.
|
||||
// // It should not be applied elsewhere per T253329.
|
||||
// self::MENU_TYPE_DROPDOWN => 'menu vector-menu-content-list',
|
||||
// ];
|
||||
// $isPortal = $type === self::MENU_TYPE_PORTAL;
|
||||
|
||||
boolean isPortal = type == MENU_TYPE_PORTAL;
|
||||
|
||||
// // For some menu items, there is no language key corresponding with its menu key.
|
||||
// // These inconsitencies are captured in MENU_LABEL_KEYS
|
||||
// $msgObj = $skin->msg( self::MENU_LABEL_KEYS[ $label ] ?? $label );
|
||||
//
|
||||
// $props = [
|
||||
// 'id' => "p-$label",
|
||||
|
||||
String label = String_.new_u8(label_bry);
|
||||
String msg = label; // for now
|
||||
String linkertooltip = String_.Empty;
|
||||
String listClasses = type == MENU_TYPE_DROPDOWN
|
||||
? "menu vector-menu-content-list"
|
||||
: "vector-menu-content-list";
|
||||
String plabel = "p-" + label;
|
||||
|
||||
Json_nde props = Json_nde.NewByVal();
|
||||
props.AddKvStr("id", plabel);
|
||||
|
||||
// 'label-id' => "p-{$label}-label",
|
||||
props.AddKvStr("label-id", plabel +"-label");
|
||||
|
||||
// If no message exists fallback to plain text (T252727)
|
||||
// 'label' => $msgObj->exists() ? $msgObj->text() : $label,
|
||||
props.AddKvStr("label", msg);
|
||||
|
||||
// 'list-classes' => $listClasses[$type] ?? 'vector-menu-content-list',
|
||||
props.AddKvStr("list-classes", listClasses);
|
||||
|
||||
// 'html-items' => '',
|
||||
|
||||
// 'is-dropdown' => $type === self::MENU_TYPE_DROPDOWN,
|
||||
props.AddKvBool("is-dropdown", type == MENU_TYPE_DROPDOWN);
|
||||
props.AddKvStr("html-tooltip", wiki.Msg_mgr().Val_html_accesskey_and_title(plabel));
|
||||
|
||||
// 'html-tooltip' => Linker::tooltip( 'p-' . $label ),
|
||||
props.AddKvStr("html-tooltip", linkertooltip);
|
||||
|
||||
// foreach ( $urls as $key => $item ) {
|
||||
// $props['html-items'] .= $this->getSkin()->makeListItem( $key, $item );
|
||||
// // Check the class of the item for a `selected` class and if so, propagate the items
|
||||
// // label to the main label.
|
||||
// if ( $setLabelToSelected ) {
|
||||
// if ( isset( $item['class'] ) && stripos( $item['class'], 'selected' ) !== false ) {
|
||||
// $props['label'] = $item['text'];
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
props.AddKvStr("html-items", "<li id=\"ca-nstab-main\" class=\"selected\"><a href=\"/wiki/Main_Page\" title=\"View the content page [c]\" accesskey=\"c\">Main Page</a></li>");
|
||||
|
||||
// $afterPortal = '';
|
||||
// if ( $isPortal ) {
|
||||
// // The BaseTemplate::getAfterPortlet method ran the SkinAfterPortlet
|
||||
// // hook and if content is added appends it to the html-after-portal method.
|
||||
// // This replicates that historic behaviour.
|
||||
// // This code should eventually be upstreamed to SkinMustache in core.
|
||||
// // Currently in production this supports the Wikibase 'edit' link.
|
||||
// $content = $this->getAfterPortlet( $label );
|
||||
// if ( $content !== '' ) {
|
||||
// $afterPortal = Html::rawElement(
|
||||
// 'div',
|
||||
// [ 'class' => [ 'after-portlet', 'after-portlet-' . $label ] ],
|
||||
// $content
|
||||
// );
|
||||
// }
|
||||
// }
|
||||
// $props['html-after-portal'] = $afterPortal;
|
||||
//
|
||||
// // Mark the portal as empty if it has no content
|
||||
// $class = ( count( $urls ) == 0 && !$props['html-after-portal'] )
|
||||
// ? 'vector-menu-empty emptyPortlet' : '';
|
||||
// $props['class'] = trim( "$class $extraClasses[$type]" );
|
||||
// return $props;
|
||||
|
||||
props.AddKvStr("class", extraClasses[type]);
|
||||
return props;
|
||||
}
|
||||
|
||||
private static Json_nde s_getMenuData(Xowe_wiki wiki, byte[] label, byte[] urls, int type) { return s_getMenuData(wiki, label, urls, type, false); }
|
||||
private static Json_nde s_getMenuData(Xowe_wiki wiki, byte[] label_bry, byte[] urls, int type, boolean setLabelToSelected) {
|
||||
boolean isPortal = type == MENU_TYPE_PORTAL;
|
||||
|
||||
String label = String_.new_u8(label_bry);
|
||||
String msg = label; // for now
|
||||
String linkertooltip = String_.Empty;
|
||||
String listClasses = type == MENU_TYPE_DROPDOWN
|
||||
? "menu vector-menu-content-list"
|
||||
: "vector-menu-content-list";
|
||||
String plabel = "p-" + label;
|
||||
|
||||
Json_nde props = Json_nde.NewByVal();
|
||||
props.AddKvStr("id", plabel);
|
||||
props.AddKvStr("label-id", plabel +"-label");
|
||||
props.AddKvStr("label", msg);
|
||||
props.AddKvStr("list-classes", listClasses);
|
||||
props.AddKvBool("is-dropdown", type == MENU_TYPE_DROPDOWN);
|
||||
props.AddKvStr("html-tooltip", wiki.Msg_mgr().Val_html_accesskey_and_title(plabel));
|
||||
props.AddKvStr("html-tooltip", linkertooltip);
|
||||
|
||||
// foreach ( $urls as $key => $item ) {
|
||||
// $props['html-items'] .= $this->getSkin()->makeListItem( $key, $item );
|
||||
// // Check the class of the item for a `selected` class and if so, propagate the items
|
||||
// // label to the main label.
|
||||
// if ( $setLabelToSelected ) {
|
||||
// if ( isset( $item['class'] ) && stripos( $item['class'], 'selected' ) !== false ) {
|
||||
// $props['label'] = $item['text'];
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
props.AddKvStr("html-items", urls);
|
||||
|
||||
// $afterPortal = '';
|
||||
// if ( $isPortal ) {
|
||||
// // The BaseTemplate::getAfterPortlet method ran the SkinAfterPortlet
|
||||
// // hook and if content is added appends it to the html-after-portal method.
|
||||
// // This replicates that historic behaviour.
|
||||
// // This code should eventually be upstreamed to SkinMustache in core.
|
||||
// // Currently in production this supports the Wikibase 'edit' link.
|
||||
// $content = $this->getAfterPortlet( $label );
|
||||
// if ( $content !== '' ) {
|
||||
// $afterPortal = Html::rawElement(
|
||||
// 'div',
|
||||
// [ 'class' => [ 'after-portlet', 'after-portlet-' . $label ] ],
|
||||
// $content
|
||||
// );
|
||||
// }
|
||||
// }
|
||||
// $props['html-after-portal'] = $afterPortal;
|
||||
//
|
||||
// // Mark the portal as empty if it has no content
|
||||
// $class = ( count( $urls ) == 0 && !$props['html-after-portal'] )
|
||||
// ? 'vector-menu-empty emptyPortlet' : '';
|
||||
// $props['class'] = trim( "$class $extraClasses[$type]" );
|
||||
// return $props;
|
||||
props.AddKvStr("class", extraClasses[type]);
|
||||
return props;
|
||||
}
|
||||
}
|
@ -0,0 +1,403 @@
|
||||
/* search for '</?section' then find fwd '>'
|
||||
any </section ignore
|
||||
|
||||
within bgn to end need to dig out 'begin' or 'end' or the language sensitivities
|
||||
also the key value
|
||||
|
||||
store
|
||||
name of section, start or end
|
||||
if start position after close >
|
||||
if end position before <
|
||||
*/
|
||||
package gplx.xowa.xtns.lst;
|
||||
import gplx.Bry_;
|
||||
import gplx.List_adp;
|
||||
import gplx.List_adp_;
|
||||
|
||||
import gplx.xowa.Xoa_ttl;
|
||||
import gplx.xowa.Xoae_page;
|
||||
import gplx.xowa.Xowe_wiki;
|
||||
import gplx.xowa.parsers.Xop_ctx;
|
||||
import gplx.xowa.parsers.Xop_root_tkn;
|
||||
import gplx.xowa.parsers.Xop_parser;
|
||||
import gplx.xowa.parsers.Xop_parser_;
|
||||
import gplx.xowa.parsers.Xop_parser_tid_;
|
||||
import gplx.xowa.parsers.Xop_tkn_mkr;
|
||||
import gplx.xowa.parsers.tmpls.Xot_invk_temp;
|
||||
import gplx.xowa.parsers.lnkis.files.Xop_file_logger_;
|
||||
import gplx.xowa.parsers.tmpls.Xot_defn_tmpl;
|
||||
import gplx.Bry_bfr;
|
||||
import gplx.Bry_bfr_;
|
||||
import gplx.Bool_;
|
||||
import gplx.Hash_adp_bry;
|
||||
|
||||
public class Db_Section_list {
|
||||
private List_adp sects;
|
||||
private List_adp heads;
|
||||
private byte[] src;
|
||||
private Xop_ctx ctx;
|
||||
private Xop_ctx sub_ctx;
|
||||
private Xowe_wiki wiki;
|
||||
private Xoa_ttl ttl;
|
||||
private byte[] ttl_bry;
|
||||
private static final byte Include_between = 0, Include_to_eos = 1, Include_to_bos = 2;
|
||||
public Db_Section_list(byte[] src, int langid, Xop_ctx ctx, Xop_ctx sub_ctx, Xoa_ttl ttl, byte[] ttl_bry) {
|
||||
byte b;
|
||||
this.src = src;
|
||||
this.ctx = ctx;
|
||||
this.wiki = ctx.Wiki();
|
||||
this.sub_ctx = sub_ctx;
|
||||
this.ttl = ttl;
|
||||
this.ttl_bry = ttl_bry;
|
||||
int src_len = src.length;
|
||||
int pos = 0;
|
||||
int bgn, end, atr;
|
||||
sects = List_adp_.New();
|
||||
begin_end keyword;
|
||||
switch (langid) {
|
||||
case 1: // german!!!
|
||||
keyword = new DE_begin_end();
|
||||
break;
|
||||
default:
|
||||
keyword = new EN_begin_end();
|
||||
break;
|
||||
}
|
||||
while (pos < src_len) {
|
||||
b = src[pos++];
|
||||
if (b == '<') {
|
||||
if (pos + 10 < src_len && (src[pos] | 32) == 's' && (src[pos+1] | 32) == 'e' && (src[pos+2] | 32) == 'c' && (src[pos+3] | 32) == 't' && (src[pos+4] | 32) == 'i' && (src[pos+5] | 32) == 'o' && (src[pos+6] | 32) == 'n' && src[pos+7] == ' ') {
|
||||
bgn = pos - 1;
|
||||
pos += 8;
|
||||
atr = pos;
|
||||
while (pos < src_len) {
|
||||
b = src[pos++];
|
||||
if (b == '>')
|
||||
break;
|
||||
}
|
||||
if (pos == src_len) // no end found
|
||||
break;
|
||||
end = pos;
|
||||
// now find a keyword
|
||||
begin_end_result bg = keyword.Find(src, atr, end);
|
||||
if (bg != null) {
|
||||
sects.Add(new Section(src, bg.start, bg.type, bgn, end));
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (b == '\n') { // check for headers
|
||||
if (pos + 10 < src_len && src[pos] == '=') {
|
||||
int count = 1;
|
||||
pos++;
|
||||
while (pos < src_len) {
|
||||
b = src[pos++];
|
||||
if (b != '=')
|
||||
break;
|
||||
count++;
|
||||
}
|
||||
// now find the next <nl>
|
||||
if (b != '\n') {
|
||||
int npos = pos;
|
||||
while (npos < src_len) {
|
||||
b = src[npos++];
|
||||
if (b == '\n')
|
||||
break;
|
||||
}
|
||||
if (b == '\n') {
|
||||
// now count any '=' backwards
|
||||
int ncount = 0;
|
||||
while (npos > pos) {
|
||||
b = src[--npos];
|
||||
if (b == '=')
|
||||
ncount++;
|
||||
else
|
||||
break;
|
||||
}
|
||||
if (ncount == count) { // we have a header
|
||||
heads.Add(new Header(src, pos, npos, count));
|
||||
pos = npos + count;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public byte[] Include(byte[] from, byte[] to) {
|
||||
if (to == Lst_pfunc_itm.Null_arg) { // no end arg; EX: {{#lst:page|bgn}}; NOTE: different than {{#lst:page|bgn|}}
|
||||
if (from == Lst_pfunc_itm.Null_arg) { // no bgn arg; EX: {{#lst:page}}
|
||||
return Compile3(src);
|
||||
}
|
||||
else // bgn exists; set end to bgn; EX: {{#lst:page|bgn}} is same as {{#lst:page|bgn|bgn}}; NOTE: {{#lst:page|bgn|}} means write from bgn to eos
|
||||
to = from;
|
||||
}
|
||||
Bry_bfr bfr = Bry_bfr_.New();
|
||||
byte include_mode = Include_between;
|
||||
if (Bry_.Len_eq_0(to))
|
||||
include_mode = Include_to_eos;
|
||||
else if (Bry_.Len_eq_0(from))
|
||||
include_mode = Include_to_bos;
|
||||
int bgn_pos = 0; boolean bgn_found = false; int src_page_bry_len = src.length;
|
||||
int sections_len = sects.Count();
|
||||
for (int i = 0; i < sections_len; i++) {
|
||||
Section sect = (Section)sects.Get_at(i);
|
||||
byte section_tid = (byte)sect.type;
|
||||
if (section_tid == begin_end_result.BEGIN && Matchkey(sect, from)) {
|
||||
int sect_bgn_rhs = sect.end;
|
||||
if (include_mode == Include_to_eos) { // write from cur to eos; EX: {{#lst:page|bgn|}}
|
||||
bfr.Add_mid(src, sect_bgn_rhs, src_page_bry_len);
|
||||
bgn_found = false;
|
||||
break;
|
||||
}
|
||||
else { // bgn and end
|
||||
if (!bgn_found) { // NOTE: !bgn_found to prevent "resetting" of dupe; EX: <s begin=key0/>a<s begin=key0/>b; should start from a not b
|
||||
bgn_pos = sect_bgn_rhs;
|
||||
bgn_found = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (section_tid == begin_end_result.END && Matchkey(sect, to)) {
|
||||
int sect_end_lhs = sect.bgn;
|
||||
if (include_mode == Include_to_bos) { // write from bos to cur; EX: {{#lst:page||end}}
|
||||
bfr.Add_mid(src, 0, sect_end_lhs);
|
||||
bgn_found = false;
|
||||
break;
|
||||
}
|
||||
else {
|
||||
if (bgn_found) { // NOTE: bgn_found to prevent writing from bos; EX: a<s end=key0/>b should not write anything
|
||||
bfr.Add_mid(src, bgn_pos, sect_end_lhs);
|
||||
bgn_found = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (bgn_found) // bgn_found, but no end; write to end of page; EX: "a <section begin=key/> b" -> " b"
|
||||
bfr.Add_mid(src, bgn_pos, src_page_bry_len);
|
||||
|
||||
return Compile3(bfr.To_bry());
|
||||
}
|
||||
public byte[] Exclude(byte[] sect_exclude, byte[] sect_replace) {
|
||||
if (Bry_.Len_eq_0(sect_exclude)) { // no exclude arg; EX: {{#lstx:page}} or {{#lstx:page}}
|
||||
return Compile3(src); // write all and exit
|
||||
}
|
||||
int sections_len = sects.Count();
|
||||
int bgn_pos = 0;
|
||||
Bry_bfr bfr = Bry_bfr_.New();
|
||||
for (int i = 0; i < sections_len; i++) {
|
||||
Section sect = (Section)sects.Get_at(i);
|
||||
byte section_tid = (byte)sect.type;
|
||||
if (section_tid == begin_end_result.BEGIN && Matchkey(sect, sect_exclude)) {
|
||||
bfr.Add_mid(src, bgn_pos, sect.bgn); // write everything from bgn_pos up to exclude
|
||||
}
|
||||
else if (section_tid == begin_end_result.END && Matchkey(sect, sect_exclude)) { // exclude end found
|
||||
if (sect_replace != null)
|
||||
bfr.Add(sect_replace); // write replacement
|
||||
bgn_pos = sect.end; // reset bgn_pos
|
||||
}
|
||||
}
|
||||
bfr.Add_mid(src, bgn_pos, src.length);
|
||||
return Compile3(bfr.To_bry());
|
||||
}
|
||||
public byte[] Header(byte[] lhs_hdr, byte[] rhs_hdr) {
|
||||
return Bry_.Empty;
|
||||
}
|
||||
private boolean Matchkey(Section sect, byte[] find) {
|
||||
if (find == Lst_pfunc_itm.Null_arg) return false;
|
||||
int pos = sect.keybgn;
|
||||
int keylen = sect.keyend - pos;
|
||||
int find_end = find.length;
|
||||
if (find_end != keylen) return false;
|
||||
for (int i = 0; i < find_end; i++) {
|
||||
if (src[pos + i] != find[i]) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
// need ctx hence wiki and page
|
||||
private byte[] Compile(byte[] page_bry) {
|
||||
Xop_root_tkn xtn_root = null;
|
||||
// set recursing flag
|
||||
Xoae_page page = ctx.Page();
|
||||
Bry_bfr full_bfr = wiki.Utl__bfr_mkr().Get_m001();
|
||||
try {
|
||||
wiki.Parser_mgr().Lst__recursing_(true);
|
||||
Hash_adp_bry lst_page_regy = ctx.Lst_page_regy(); if (lst_page_regy == null) lst_page_regy = Hash_adp_bry.cs(); // SEE:NOTE:page_regy; DATE:2014-01-01
|
||||
page.Html_data().Indicators().Enabled_(Bool_.N); // disable <indicator> b/c <page> should not add to current page; PAGE:en.s:The_Parochial_System_(Wilberforce,_1838); DATE:2015-04-29
|
||||
xtn_root = Bld_root_nde(full_bfr, lst_page_regy, page_bry); // NOTE: this effectively reparses page twice; needed b/c of "if {| : ; # *, auto add new_line" which can build different tokens
|
||||
} finally {
|
||||
wiki.Parser_mgr().Lst__recursing_(false);
|
||||
full_bfr.Mkr_rls();
|
||||
}
|
||||
page.Html_data().Indicators().Enabled_(Bool_.Y);
|
||||
if (xtn_root == null) return null;
|
||||
//html_wtr.Write_tkn_to_html(bfr, ctx, hctx, xtn_root.Root_src(), xnde, Xoh_html_wtr.Sub_idx_null, xtn_root);
|
||||
return null;
|
||||
}
|
||||
private byte[] Compile2(byte[] msg_val) {
|
||||
Xowe_wiki wikie = (Xowe_wiki)wiki;
|
||||
Xop_ctx sub_ctx = Xop_ctx.New__sub__reuse_page(wikie.Parser_mgr().Ctx());
|
||||
sub_ctx.Parse_tid_(Xop_parser_tid_.Tid__wtxt);
|
||||
Xop_tkn_mkr tkn_mkr = sub_ctx.Tkn_mkr();
|
||||
Xop_root_tkn sub_root = tkn_mkr.Root(msg_val);
|
||||
return wikie.Parser_mgr().Main().Expand_tmpl(sub_root, sub_ctx, tkn_mkr, msg_val);
|
||||
}
|
||||
private byte[] Compile3(byte[] sub_src) {
|
||||
// parse page; note adding to stack to prevent circular recursions
|
||||
if (!wiki.Parser_mgr().Tmpl_stack_add(ttl.Full_db())) return null;
|
||||
Xot_defn_tmpl tmpl = wiki.Parser_mgr().Main().Parse_text_to_defn_obj(sub_ctx, sub_ctx.Tkn_mkr(), ttl.Ns(), ttl_bry, sub_src); // NOTE: parse as tmpl to ignore <noinclude>
|
||||
wiki.Parser_mgr().Tmpl_stack_del(); // take template off stack; evaluate will never recurse, but will fail if ttl is still on stack; DATE:2014-03-10
|
||||
|
||||
// eval tmpl
|
||||
Bry_bfr tmp_bfr = wiki.Utl__bfr_mkr().Get_m001();
|
||||
try {
|
||||
tmpl.Tmpl_evaluate(sub_ctx, Xot_invk_temp.New_root(ttl.Page_txt()), tmp_bfr);
|
||||
sub_src = tmp_bfr.To_bry_and_clear();
|
||||
} finally {
|
||||
tmp_bfr.Mkr_rls();
|
||||
}
|
||||
return sub_src;
|
||||
}
|
||||
private Xop_root_tkn Bld_root_nde(Bry_bfr page_bfr, Hash_adp_bry lst_page_regy, byte[] wikitext) {
|
||||
Xop_ctx tmp_ctx = Xop_ctx.New__sub__reuse_lst(wiki, ctx, lst_page_regy);
|
||||
tmp_ctx.Page().Ttl_(ctx.Page().Ttl()); // NOTE: must set tmp_ctx.Ttl to ctx.Ttl; EX: Flatland and First World; DATE:2013-04-29
|
||||
tmp_ctx.Lnki().File_logger_(Xop_file_logger_.Noop); // NOTE: set file_wkr to null, else items will be double-counted
|
||||
tmp_ctx.Parse_tid_(Xop_parser_tid_.Tid__defn);
|
||||
Xop_parser tmp_parser = Xop_parser.new_(wiki, wiki.Parser_mgr().Main().Tmpl_lxr_mgr(), wiki.Parser_mgr().Main().Wtxt_lxr_mgr());
|
||||
Xop_root_tkn rv = tmp_ctx.Tkn_mkr().Root(wikitext);
|
||||
tmp_parser.Parse_text_to_wdom(rv, tmp_ctx, tmp_ctx.Tkn_mkr(), wikitext, Xop_parser_.Doc_bgn_bos);
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
||||
class Section {
|
||||
public int keybgn;
|
||||
public int keyend;
|
||||
public int type;
|
||||
public int bgn;
|
||||
public int end;
|
||||
Section(byte[] src, int keybgn, int type, int bgn, int end) {
|
||||
this.type = type;
|
||||
this.bgn = bgn;
|
||||
this.end = end;
|
||||
byte b = src[keybgn];
|
||||
if (b == '\'' || b == '"')
|
||||
keybgn++;
|
||||
keyend = end - 2;
|
||||
while (keyend > bgn) {
|
||||
b = src[keyend - 1];
|
||||
if (b != ' ' && b != '\t' && b != '\n')
|
||||
break;
|
||||
keyend--;
|
||||
}
|
||||
if (b == '\'' || b == '"')
|
||||
keyend--;
|
||||
this.keybgn = keybgn;
|
||||
}
|
||||
}
|
||||
class Header {
|
||||
public int bgn;
|
||||
public int end;
|
||||
public int level;
|
||||
Header(byte[] src, int bgn, int end, int level) {
|
||||
this.level = level;
|
||||
byte b;
|
||||
while (bgn < end) {
|
||||
b = src[bgn];
|
||||
if (b == ' ' || b == '\t')
|
||||
bgn++;
|
||||
else
|
||||
break;
|
||||
}
|
||||
this.bgn = bgn;
|
||||
while (end > bgn) {
|
||||
b = src[end - 1];
|
||||
if (b == ' ' || b == '\t')
|
||||
bgn--;
|
||||
else
|
||||
break;
|
||||
}
|
||||
this.end = end;
|
||||
}
|
||||
}
|
||||
interface begin_end {
|
||||
begin_end_result Find(byte[] src, int bgn, int end);
|
||||
}
|
||||
|
||||
class EN_begin_end implements begin_end {
|
||||
public begin_end_result Find(byte[] src, int bgn, int end) {
|
||||
while (bgn < end) {
|
||||
byte b = src[bgn++];
|
||||
switch (b) {
|
||||
case 'b':
|
||||
case 'B':
|
||||
if ((src[bgn] | 32) == 'e' && (src[bgn+1] | 32) == 'g' && (src[bgn+2] | 32) == 'i' && (src[bgn+3] | 32) == 'n' && src[bgn+4] == '=') {
|
||||
bgn += 5;
|
||||
b = src[bgn];
|
||||
return new begin_end_result(begin_end_result.BEGIN, bgn);
|
||||
}
|
||||
break;
|
||||
case 'e':
|
||||
case 'E':
|
||||
if ((src[bgn] | 32) == 'n' && (src[bgn+1] | 32) == 'd' && src[bgn+2] == '=') {
|
||||
bgn += 3;
|
||||
b = src[bgn];
|
||||
return new begin_end_result(begin_end_result.END, bgn);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
class DE_begin_end implements begin_end {
|
||||
public begin_end_result Find(byte[] src, int bgn, int end) {
|
||||
while (bgn < end) {
|
||||
byte b = src[bgn++];
|
||||
switch (b) {
|
||||
case 'b':
|
||||
case 'B':
|
||||
if ((src[bgn] | 32) == 'e' && (src[bgn+1] | 32) == 'g' && (src[bgn+2] | 32) == 'i' && (src[bgn+3] | 32) == 'n' && src[bgn+4] == '=') {
|
||||
bgn += 5;
|
||||
b = src[bgn];
|
||||
return new begin_end_result(begin_end_result.BEGIN, bgn);
|
||||
}
|
||||
break;
|
||||
// End
|
||||
// Ende
|
||||
case 'e':
|
||||
case 'E':
|
||||
if ((src[bgn] | 32) == 'n' && (src[bgn+1] | 32) == 'd') {
|
||||
if (src[bgn+2] == '=') {
|
||||
bgn += 3;
|
||||
}
|
||||
else if ((src[bgn+2] | 32) == 'e' && src[bgn+3] == '=') {
|
||||
bgn += 4;
|
||||
}
|
||||
else
|
||||
break;
|
||||
b = src[bgn];
|
||||
return new begin_end_result(begin_end_result.END, bgn);
|
||||
}
|
||||
break;
|
||||
// Anfang
|
||||
case 'a':
|
||||
case 'A':
|
||||
if ((src[bgn] | 32) == 'n' && (src[bgn+1] | 32) == 'f' && (src[bgn+2] | 32) == 'a' && (src[bgn+3] | 32) == 'n' && (src[bgn+4] | 32) == 'g' && src[bgn+5] == '=') {
|
||||
bgn += 6;
|
||||
b = src[bgn];
|
||||
return new begin_end_result(begin_end_result.END, bgn);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
class begin_end_result {
|
||||
public static int BEGIN = 1;
|
||||
public static int END = 2;
|
||||
public int type;
|
||||
public int start;
|
||||
begin_end_result(int type, int start) {
|
||||
this.type = type;
|
||||
this.start = start;
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2020 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.jsons;
|
||||
|
||||
import gplx.Bry_bfr;
|
||||
import gplx.Bry_bfr_;
|
||||
import gplx.core.tests.Gftest;
|
||||
import org.junit.Test;
|
||||
|
||||
public class JsonDocBldrTest {
|
||||
private JsonDocBldrTestUtil util = new JsonDocBldrTestUtil();
|
||||
@Test public void Basic() {
|
||||
JsonDocBldr bldr = JsonDocBldr.NewRootNde();
|
||||
bldr.NdeBgn("nde")
|
||||
.KvBool("bool", true)
|
||||
.KvInt("int", 123)
|
||||
.KvStr("str", "abc")
|
||||
.NdeEnd();
|
||||
util.Test(bldr
|
||||
, "{ 'nde':"
|
||||
, " { 'bool':true"
|
||||
, " , 'int':123"
|
||||
, " , 'str':'abc'"
|
||||
, " }"
|
||||
, "}");
|
||||
}
|
||||
}
|
||||
class JsonDocBldrTestUtil {
|
||||
public void Test(JsonDocBldr bldr, String... ary) {
|
||||
Bry_bfr bfr = Bry_bfr_.New();
|
||||
bldr.ToDoc().Root_grp().Print_as_json(bfr, 0);
|
||||
Gftest.Eq__ary__lines(Json_doc.Make_str_by_apos(ary), bfr.To_str_and_clear());
|
||||
}
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2020 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.mustaches;
|
||||
|
||||
import gplx.Bry_;
|
||||
import gplx.Bry_bfr_;
|
||||
import gplx.core.tests.Gftest;
|
||||
import gplx.langs.jsons.Json_doc;
|
||||
import gplx.langs.jsons.Json_parser;
|
||||
import org.junit.Test;
|
||||
|
||||
public class JsonMustacheNdeTest {
|
||||
private JsonMustacheNdeTestUtil util = new JsonMustacheNdeTestUtil();
|
||||
|
||||
@Test public void Str() {
|
||||
util.TestWrite("[{{key}}]", "{'key':'abc'}", "[abc]");
|
||||
}
|
||||
@Test public void Bool() {
|
||||
String mustache = "[{{#key}}y{{/key}}]";
|
||||
util.TestWrite(mustache, "{'key':true}", "[y]");
|
||||
util.TestWrite(mustache, "{'key':false}", "[]");
|
||||
}
|
||||
@Test public void Ary() {
|
||||
String mustache = "[{{#group}}{{key}} {{/group}}]";
|
||||
util.TestWrite(mustache, "{'group':[{'key':'a'}, {'key':'b'}, {'key':'c'}]}", "[a b c ]");
|
||||
}
|
||||
@Test public void SectionPropWithDot() {
|
||||
String mustache = "[{{#key}}{{.}}{{/key}}]";
|
||||
util.TestWrite(mustache, "{'key':'test'}", "[test]");
|
||||
util.TestWrite(mustache, "{'key1':'test'}", "[]");
|
||||
}
|
||||
@Test public void SectionPropWithoutDot() {
|
||||
String mustache = "[{{#prop}}{{propx}}{{/prop}}]";
|
||||
util.TestWrite(mustache, "{'prop':'test'}", "[]");
|
||||
util.TestWrite(mustache, "{'propx':'test'}", "[]");
|
||||
}
|
||||
@Test public void Dot() {// NOTE: online demo gives `([object Object])`; https://mustache.github.io/#demo
|
||||
String mustache = "({{.}})";
|
||||
util.TestWrite(mustache, "{'key':'test'}", "()");
|
||||
}
|
||||
}
|
||||
class JsonMustacheNdeTestUtil {
|
||||
public void TestWrite(String mustache, String json, String expd) {
|
||||
// parse JSON to mustache itm
|
||||
Json_doc jdoc = Json_parser.ParseToJdoc(Json_doc.Make_str_by_apos(json));
|
||||
JsonMustacheNde nde = new JsonMustacheNde(jdoc.Root_nde());
|
||||
|
||||
// parse template
|
||||
Mustache_tkn_itm actl_itm = new Mustache_tkn_parser().Parse(Bry_.new_u8(mustache));
|
||||
|
||||
// render
|
||||
Mustache_bfr bfr = new Mustache_bfr(Bry_bfr_.New());
|
||||
actl_itm.Render(bfr, new Mustache_render_ctx().Init(nde));
|
||||
|
||||
// test
|
||||
Gftest.Eq__ary__lines(expd, bfr.To_str_and_clear());
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,22 @@
|
||||
{{!
|
||||
See @typedef MenuDefinition
|
||||
}}
|
||||
{{! `role` is unnecessary but kept to support selectors in any gadgets or user styles. }}
|
||||
<!-- Please do not use role attribute as CSS selector, it is deprecated. -->
|
||||
<nav id="{{id}}" {{#class}}class="{{.}}"{{/class}} aria-labelledby="{{label-id}}" role="navigation" {{{html-tooltip}}}
|
||||
{{{html-user-language-attributes}}}>
|
||||
{{#is-dropdown}}
|
||||
<input type="checkbox" class="vector-menu-checkbox vectorMenuCheckbox" aria-labelledby="{{label-id}}" />
|
||||
{{/is-dropdown}}
|
||||
<h3 id="{{label-id}}">
|
||||
<span>{{label}}</span>
|
||||
</h3>
|
||||
{{! `body` class for backwards compatibility but let editors know not to use
|
||||
it via HTML comment below: }}
|
||||
<!-- Please do not use the .body class, it is deprecated. -->
|
||||
<div class="body vector-menu-content">
|
||||
<!-- Please do not use the .menu class, it is deprecated. -->
|
||||
<ul class="{{list-classes}}">{{{html-items}}}</ul>
|
||||
{{{html-after-portal}}}
|
||||
</div>
|
||||
</nav>
|
@ -0,0 +1,16 @@
|
||||
<div id="mw-navigation">
|
||||
<h2>{{msg-navigation-heading}}</h2>
|
||||
<div id="mw-head">
|
||||
{{#data-personal-menu}}{{>Menu}}{{/data-personal-menu}}
|
||||
<div id="left-navigation">
|
||||
{{#data-namespace-tabs}}{{>Menu}}{{/data-namespace-tabs}}
|
||||
{{#data-variants}}{{>Menu}}{{/data-variants}}
|
||||
</div>
|
||||
<div id="right-navigation">
|
||||
{{#data-page-actions}}{{>Menu}}{{/data-page-actions}}
|
||||
{{#data-page-actions-more}}{{>Menu}}{{/data-page-actions-more}}
|
||||
{{#data-search-box}}{{>SearchBox}}{{/data-search-box}}
|
||||
</div>
|
||||
</div>
|
||||
{{#data-sidebar}}{{>legacy/Sidebar}}{{/data-sidebar}}
|
||||
</div>
|
@ -0,0 +1,13 @@
|
||||
{{!
|
||||
See @typedef SidebarData
|
||||
string html-logo-attributes for site logo. Must be used inside tag e.g. `class="logo" lang="en-gb"`
|
||||
}}
|
||||
|
||||
<div id="mw-panel">
|
||||
<div id="p-logo" role="banner">
|
||||
<a {{{html-logo-attributes}}}></a>
|
||||
</div>
|
||||
{{#data-portals-first}}{{>Menu}}{{/data-portals-first}}
|
||||
{{#array-portals-rest}}{{>Menu}}{{/array-portals-rest}}
|
||||
{{#data-portals-languages}}{{>Menu}}{{/data-portals-languages}}
|
||||
</div>
|
@ -0,0 +1,69 @@
|
||||
{{!
|
||||
string|null html-site-notice the contents of a banner defined in MediaWiki:Sitenotice.
|
||||
Also used by CentralNotice to inject banners into Vector.
|
||||
Indicator[] array-indicators wiki-defined badges such as "good article",
|
||||
"featured article". An empty array if none are defined.
|
||||
string page-langcode the content language of the article. Assumed to be escaped HTML.
|
||||
string html-title
|
||||
bool page-isarticle
|
||||
string msg-tagline
|
||||
string html-subtitle
|
||||
string html-undelete-link
|
||||
string html-newtalk
|
||||
string msg-vector-jumptonavigation
|
||||
string msg-vector-jumptosearch
|
||||
string html-body-content
|
||||
string html-categories
|
||||
string html-after-content
|
||||
string msg-navigation-heading heading for entire navigation that is
|
||||
usually hidden to screen readers
|
||||
MenuDefinition data-personal-menu
|
||||
MenuDefinition data-namespace-tabs
|
||||
MenuDefinition data-variants
|
||||
MenuDefinition data-page-actions
|
||||
MenuDefinition data-page-actions-more
|
||||
object data-search-box. See SearchBox.mustache for documentation.
|
||||
object data-sidebar. See Sidebar.mustache for documentation.
|
||||
object data-footer for footer template partial. see Footer.mustache for documentation.
|
||||
}}
|
||||
<div id="mw-page-base" class="noprint"></div>
|
||||
<div id="mw-head-base" class="noprint"></div>
|
||||
<div id="content" class="mw-body" role="main">
|
||||
<a id="top"></a>
|
||||
<div id="siteNotice" class="mw-body-content">{{{html-site-notice}}}</div>
|
||||
{{>Indicators}}
|
||||
<h1 id="firstHeading" class="firstHeading" lang="{{page-langcode}}">{{{html-title}}}</h1>
|
||||
<div id="bodyContent" class="mw-body-content">
|
||||
{{#page-isarticle}}<div id="siteSub" class="noprint">{{msg-tagline}}</div>{{/page-isarticle}}
|
||||
<div id="contentSub"{{{html-user-language-attributes}}}>{{{html-subtitle}}}</div>
|
||||
<div id="contentSub2">{{{html-undelete-link}}}</div>
|
||||
{{{html-newtalk}}}
|
||||
{{!
|
||||
Keep this empty `div` for compatibility with gadgets and user scripts
|
||||
using this place to insert extra elements before.
|
||||
}}
|
||||
<div id="jump-to-nav"></div>
|
||||
<a class="mw-jump-link" href="#mw-head">{{msg-vector-jumptonavigation}}</a>
|
||||
<a class="mw-jump-link" href="#searchInput">{{msg-vector-jumptosearch}}</a>
|
||||
{{{html-body-content}}}
|
||||
{{{html-categories}}}
|
||||
</div>
|
||||
</div>
|
||||
{{{html-after-content}}}
|
||||
<div id="mw-navigation">
|
||||
<h2>{{msg-navigation-heading}}</h2>
|
||||
<div id="mw-head">
|
||||
{{#data-personal-menu}}{{>Menu}}{{/data-personal-menu}}
|
||||
<div id="left-navigation">
|
||||
{{#data-namespace-tabs}}{{>Menu}}{{/data-namespace-tabs}}
|
||||
{{#data-variants}}{{>Menu}}{{/data-variants}}
|
||||
</div>
|
||||
<div id="right-navigation">
|
||||
{{#data-page-actions}}{{>Menu}}{{/data-page-actions}}
|
||||
{{#data-page-actions-more}}{{>Menu}}{{/data-page-actions-more}}
|
||||
{{#data-search-box}}{{>SearchBox}}{{/data-search-box}}
|
||||
</div>
|
||||
</div>
|
||||
{{#data-sidebar}}{{>legacy/Sidebar}}{{/data-sidebar}}
|
||||
</div>
|
||||
{{#data-footer}}{{>Footer}}{{/data-footer}}
|
Loading…
Reference in new issue