1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2026-03-02 03:49:30 +00:00

v2.10.3.1

This commit is contained in:
gnosygnu
2015-10-18 22:17:57 -04:00
parent 8e18af05b6
commit 4f43f51b18
1935 changed files with 12500 additions and 12889 deletions

View File

@@ -50,7 +50,7 @@ public class Json_doc {
Json_itm rv = Find_nde(root_nde, tmp_qry_bry, 0, 0); if (rv == null) return null;
return (Json_grp)rv;
}
public Json_grp Get_grp(byte[][] qry_bry) {
public Json_grp Get_grp_many(byte[]... qry_bry) {
Json_itm rv = Find_nde(root_nde, qry_bry, qry_bry.length - 1, 0); if (rv == null) return null;
return (Json_grp)rv;
}

View File

@@ -20,8 +20,8 @@ public class Json_doc_srl {
private int indent = -1;
private Bry_bfr bfr = Bry_bfr.reset_(255);
public boolean Ws_enabled() {return ws_enabled;} public void Ws_enabled_(boolean v) {ws_enabled = v;} private boolean ws_enabled = false;
public byte[] Bld() {return bfr.Xto_bry_and_clear();}
public String Bld_as_str() {return bfr.Xto_str_and_clear();}
public byte[] Bld() {return bfr.To_bry_and_clear();}
public String Bld_as_str() {return bfr.To_str_and_clear();}
public Json_doc_srl Write_root(byte[] key, Object val) {
Write_nde_bgn();
Write_obj(false, key, val);

View File

@@ -93,6 +93,6 @@ public class Json_doc_wtr {
bfr.Add_byte(Byte_ascii.Colon);
return this;
}
public byte[] Bld() {return bfr.Xto_bry_and_clear();}
public String Bld_as_str() {return bfr.Xto_str_and_clear();}
public byte[] Bld() {return bfr.To_bry_and_clear();}
public String Bld_as_str() {return bfr.To_str_and_clear();}
}

View File

@@ -23,6 +23,6 @@ public class Json_itm_ {
public static byte[] To_bry(Bry_bfr bfr, Json_itm itm) {
if (itm == null) return Bry_.Empty;
itm.Print_as_json(bfr, 0);
return bfr.Xto_bry_and_clear();
return bfr.To_bry_and_clear();
}
}

View File

@@ -23,7 +23,7 @@ public abstract class Json_itm_base implements Json_itm {
public int Src_end() {return src_end;} protected int src_end;
public abstract Object Data();
public abstract byte[] Data_bry();
public String Print_as_json() {Bry_bfr bfr = Bry_bfr.new_(); Print_as_json(bfr, 0); return bfr.Xto_str_and_clear();}
public String Print_as_json() {Bry_bfr bfr = Bry_bfr.new_(); Print_as_json(bfr, 0); return bfr.To_str_and_clear();}
public abstract void Print_as_json(Bry_bfr bfr, int depth);
@gplx.Virtual public boolean Data_eq(byte[] comp) {return false;}
}

View File

@@ -73,6 +73,6 @@ class Json_itm_str extends Json_itm_base {
break;
}
}
return bfr.Xto_bry_and_clear();
return bfr.To_bry_and_clear();
}
}

View File

@@ -27,5 +27,5 @@ public class Json_itm_tmp implements Json_itm { // TEST:
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_.Xto_str(v));}
public static Json_itm new_int_(int v) {return new Json_itm_tmp(Json_itm_.Tid__int, Int_.To_str(v));}
}

View File

@@ -44,7 +44,7 @@ public class Json_kv_ary_srl {
KeyVal[] rv = new KeyVal[subs_len];
for (int i = 0; i < subs_len; i++) {
Json_itm sub = itm.Get_at(i);
KeyVal kv = KeyVal_.new_(Int_.Xto_str(i + Int_.Base1), Val_by_itm(sub));
KeyVal kv = KeyVal_.new_(Int_.To_str(i + Int_.Base1), Val_by_itm(sub));
rv[i] = kv;
}
return rv;

View File

@@ -174,7 +174,7 @@ public class Json_parser {
}
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_.Xto_str(bgn) + " " + String_.new_u8__by_len(src, bgn, 20);
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");

View File

@@ -69,6 +69,6 @@ public abstract class Json_parser__itm__base {
public boolean Kv__has(Json_kv[] ary, int i) {return Kv__bry_or_empty(ary, i) != null;}
protected abstract void Parse_hook_nde(Json_nde sub, Json_kv[] atrs);
protected void Warn(String msg, Json_kv kv) {
Gfo_usr_dlg_.I.Warn_many("", "", msg + ": path=~{0}.~{1} excerpt=~{2}", context, kv.Key_as_bry(), Json_itm_.To_bry(tmp_bfr, cur_itm));
Gfo_usr_dlg_.Instance.Warn_many("", "", msg + ": path=~{0}.~{1} excerpt=~{2}", context, kv.Key_as_bry(), Json_itm_.To_bry(tmp_bfr, cur_itm));
}
}

View File

@@ -79,7 +79,7 @@ class Json_parser_fxt {
byte[] raw = Json_parser_tst.Replace_apos(Bry_.new_u8(raw_str));
Json_doc doc = parser.Parse(raw);
doc.Root_grp().Print_as_json(tmp_bfr, 0);
String actl = tmp_bfr.Xto_str_and_clear();
String actl = tmp_bfr.To_str_and_clear();
String expd = Xto_str(raw, doc, expd_ary, 0, expd_ary.length);
Tfds.Eq_str_lines(expd, actl, actl);
}
@@ -95,6 +95,6 @@ class Json_parser_fxt {
Json_itm itm = ary[i];
itm.Print_as_json(tmp_bfr, 0);
}
return tmp_bfr.Xto_str_and_clear();
return tmp_bfr.To_str_and_clear();
}
}

View File

@@ -0,0 +1,54 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.langs.jsons; import gplx.*; import gplx.langs.*;
public class Json_printer {
private final Json_parser parser = new Json_parser();
private final Json_wtr wtr = new Json_wtr();
public Json_printer Opt_quote_byte_(byte v) {wtr.Opt_quote_byte_(v); return this;}
public Json_wtr Wtr() {return wtr;}
public byte[] To_bry() {return wtr.To_bry_and_clear();}
public String To_str() {return wtr.To_str_and_clear();}
public Json_printer Print_by_bry(byte[] src) {
Json_doc jdoc = parser.Parse(src);
return (jdoc.Root_grp().Tid() == Json_itm_.Tid__ary)
? Print_by_ary(jdoc.Root_ary())
: Print_by_nde(jdoc.Root_nde())
;
}
public Json_printer Print_by_ary(Json_ary ary) {
wtr.Doc_ary_bgn();
int len = ary.Len();
for (int i = 0; i < len; ++i) {
Json_itm itm = ary.Get_at(i);
wtr.Ary_itm_obj(wtr.Get_x(itm));
}
wtr.Doc_ary_end();
return this;
}
public Json_printer Print_by_nde(Json_nde nde) {
wtr.Doc_nde_bgn();
int len = nde.Len();
for (int i = 0; i < len; ++i) {
Json_kv kv = nde.Get_at_as_kv(i);
Object kv_val = wtr.Get_x(kv.Val());
wtr.Kv_obj(kv.Key_as_bry(), kv_val, Type_adp_.To_tid_obj(kv_val));
}
wtr.Doc_nde_end();
return this;
}
}

View File

@@ -0,0 +1,88 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.langs.jsons; import gplx.*; import gplx.langs.*;
import org.junit.*;
public class Json_printer_tst {
private final Json_printer_fxt fxt = new Json_printer_fxt();
@Test public void Root_nde() {
fxt.Test_print(Json_doc.Make_str_by_apos("{'k1':'v1','k2':'v2'}"), String_.Concat_lines_nl
( "{ 'k1':'v1'"
, ", 'k2':'v2'"
, "}"
));
}
@Test public void Root_ary() {
fxt.Test_print(Json_doc.Make_str_by_apos("[1,2,3]"), String_.Concat_lines_nl
( "[ 1"
, ", 2"
, ", 3"
, "]"
));
}
@Test public void Ary_w_ary() {
fxt.Test_print(Json_doc.Make_str_by_apos("[[1,2],[3,4]]"), String_.Concat_lines_nl
( "[ "
, " [ 1"
, " , 2"
, " ]"
, ", "
, " [ 3"
, " , 4"
, " ]"
, "]"
));
}
@Test public void Ary_w_nde() {
fxt.Test_print(Json_doc.Make_str_by_apos("[{'k1':'v1','k2':'v2'},{'k3':'v3','k4':'v4'}]"), String_.Concat_lines_nl
( "[ "
, " { 'k1':'v1'"
, " , 'k2':'v2'"
, " }"
, ", "
, " { 'k3':'v3'"
, " , 'k4':'v4'"
, " }"
, "]"
));
}
@Test public void Nde_w_ary() {
fxt.Test_print(Json_doc.Make_str_by_apos("{'k1':[1,2],'k2':[3,4]}"), String_.Concat_lines_nl
( "{ 'k1':"
, " [ 1"
, " , 2"
, " ]"
, ", 'k2':"
, " [ 3"
, " , 4"
, " ]"
, "}"
));
}
// @Test public void Smoke() {
// Json_printer printer = new Json_printer();
// String url = "C:\\temp.json";
// String s = printer.Pretty_print_as_str(Bry_.new_u8(Io_mgr.Instance.LoadFilStr(url)));
// Io_mgr.Instance.SaveFilStr(url, s);
// }
}
class Json_printer_fxt {
private final Json_printer printer = new Json_printer().Opt_quote_byte_(Byte_ascii.Apos);
public void Test_print(String raw, String expd) {
Tfds.Eq_str_lines(expd, printer.Print_by_bry(Bry_.new_u8(raw)).To_str());
}
}

View File

@@ -20,14 +20,14 @@ import gplx.core.primitives.*;
public class Json_wtr {
private final Bry_bfr bfr = Bry_bfr.new_(255);
private final Int_ary idx_stack = new Int_ary(4);
private int idx = 0;
private int indent;
private int idx = 0;
public Bry_bfr Bfr() {return bfr;}
public void Indent_(int v) {this.indent = v;}
public void Indent_(int v) {this.indent = v;} private int indent;
public byte Opt_quote_byte() {return opt_quote_byte;} public Json_wtr Opt_quote_byte_(byte v) {opt_quote_byte = v; return this;} private byte opt_quote_byte = Byte_ascii.Quote;
public boolean Opt_ws() {return opt_ws;} public Json_wtr Opt_ws_(boolean v) {opt_ws = v; return this;} private boolean opt_ws = true;
public byte[] To_bry_and_clear() {return bfr.Xto_bry_and_clear();}
public String To_str_and_clear() {return bfr.Xto_str_and_clear();}
public byte[] To_bry_and_clear() {return bfr.To_bry_and_clear();}
public String To_str_and_clear() {return bfr.To_str_and_clear();}
public Json_wtr () {this.Clear();}
public Json_wtr Clear() {
indent = -1;
idx_stack.Clear();
@@ -63,7 +63,7 @@ public class Json_wtr {
public Json_wtr Ary_itm_obj(Object itm) {return Ary_itm_by_type_tid(Type_adp_.To_tid_obj(itm), itm);}
public Json_wtr Ary_itm_by_type_tid(int itm_type_tid, Object itm) {
Write_indent_itm();
Write_val_obj(itm_type_tid, itm);
Write_val_obj(Bool_.Y, itm_type_tid, itm);
Write_nl();
++idx;
return this;
@@ -78,10 +78,10 @@ public class Json_wtr {
}
public Json_wtr Kv_bool(String key, boolean val) {return Kv_bool(Bry_.new_u8(key), val);}
public Json_wtr Kv_bool(byte[] key, boolean val) {return Kv_raw(key, val ? Bool_.True_bry : Bool_.False_bry);}
public Json_wtr Kv_int(String key, int val) {return Kv_raw(Bry_.new_u8(key), Int_.Xto_bry(val));}
public Json_wtr Kv_long(String key, long val) {return Kv_raw(Bry_.new_u8(key), Bry_.new_a7(Long_.Xto_str(val)));}
public Json_wtr Kv_float(String key, float val) {return Kv_raw(Bry_.new_u8(key), Bry_.new_a7(Float_.Xto_str(val)));}
public Json_wtr Kv_double(String key, double val) {return Kv_raw(Bry_.new_u8(key), Bry_.new_a7(Double_.Xto_str(val)));}
public Json_wtr Kv_int(String key, int val) {return Kv_raw(Bry_.new_u8(key), Int_.To_bry(val));}
public Json_wtr Kv_long(String key, long val) {return Kv_raw(Bry_.new_u8(key), Bry_.new_a7(Long_.To_str(val)));}
public Json_wtr Kv_float(String key, float val) {return Kv_raw(Bry_.new_u8(key), Bry_.new_a7(Float_.To_str(val)));}
public Json_wtr Kv_double(String key, double val) {return Kv_raw(Bry_.new_u8(key), Bry_.new_a7(Double_.To_str(val)));}
private Json_wtr Kv_raw(byte[] key, byte[] val) {
Write_indent_itm();
Write_key(key);
@@ -99,10 +99,29 @@ public class Json_wtr {
Write_nl();
return this;
}
public Object Get_x(Json_itm itm) {
switch (itm.Tid()) {
case Json_itm_.Tid__ary:
case Json_itm_.Tid__nde:
return itm;
default:
case Json_itm_.Tid__kv: throw Err_.new_unsupported();
case Json_itm_.Tid__bool:
case Json_itm_.Tid__int:
case Json_itm_.Tid__decimal:
case Json_itm_.Tid__str:
return itm.Data();
}
}
public void Kv_itm_x(byte[] key, Json_itm itm) {
Object val = Get_x(itm);
int val_tid = Type_adp_.To_tid_obj(val);
Kv_obj(key, val, val_tid);
}
public Json_wtr Kv_obj(byte[] key, Object val, int val_tid) {
Write_indent_itm();
Write_key(key);
Write_val_obj(val_tid, val);
Write_val_obj(Bool_.N, val_tid, val);
Write_nl();
return this;
}
@@ -130,7 +149,7 @@ public class Json_wtr {
++idx;
return this;
}
private void Write_val_obj(int type_tid, Object obj) {
private void Write_val_obj(boolean called_by_ary, int type_tid, Object obj) {
switch (type_tid) {
case Type_adp_.Tid__null: bfr.Add(Object_.Bry__null); break;
case Type_adp_.Tid__bool: bfr.Add_bool(Bool_.cast(obj)); break;
@@ -145,42 +164,80 @@ public class Json_wtr {
case Type_adp_.Tid__date:
case Type_adp_.Tid__decimal: Write_str(Bry_.new_u8(Object_.Xto_str_strict_or_empty(obj))); break;
case Type_adp_.Tid__obj:
Class<?> type = obj.getClass();
if (Type_adp_.Eq(type, KeyVal[].class)) {
if (idx == 0) { // if nde, and first item, then put on new line
bfr.Del_by_1();
if (opt_ws) {
bfr.Add_byte_nl();
++indent;
Write_indent();
--indent;
}
}
KeyVal[] kvy = (KeyVal[])obj;
Write_grp_bgn(Sym_nde_bgn, Bool_.N);
int kvy_len = kvy.length;
for (int i = 0; i < kvy_len; ++i) {
KeyVal kv = kvy[i];
Object kv_val = kv.Val();
Kv_obj(Bry_.new_u8(kv.Key()), kv_val, Type_adp_.To_tid_obj(kv_val));
}
Write_grp_end(Bool_.Y, Sym_nde_end);
}
else if (Type_adp_.Is_array(type))
Write_val_ary(obj);
int grp_type = Grp_type__get(obj);
if (grp_type < Grp_type__json_ary)
Write_val_obj__nde(called_by_ary, grp_type, obj);
else
throw Err_.new_unhandled(type);
Write_val_itm__ary(called_by_ary, grp_type, obj);
break;
default: throw Err_.new_unhandled(type_tid);
}
}
private void Write_val_ary(Object ary_obj) {
private void Handle_nde_as_ary_itm_0() {
if (idx == 0) { // if nde, and first item, then put on new line
bfr.Del_by_1();
if (opt_ws) {
bfr.Add_byte_nl();
++indent;
Write_indent();
--indent;
}
}
}
private void Write_val_obj__nde(boolean called_by_ary, int grp_type, Object obj) {
if (grp_type == Grp_type__json_nde) {
if (idx == 0) { // if nde, and first item, then put on new line
if (!called_by_ary) {
bfr.Del_by_1();
if (opt_ws) {
bfr.Add_byte_nl();
++indent;
Write_indent();
--indent;
}
}
}
// else {
bfr.Add_byte_nl();
// }
Write_grp_bgn(Sym_nde_bgn, Bool_.Y);
Json_nde sub_nde = (Json_nde)obj;
int sub_nde_len = sub_nde.Len();
for (int i = 0; i < sub_nde_len; ++i) {
Json_kv sub_kv = sub_nde.Get_at_as_kv(i);
Kv_itm_x(sub_kv.Key_as_bry(), sub_kv.Val());
}
}
else {
Handle_nde_as_ary_itm_0();
Write_grp_bgn(Sym_nde_bgn, Bool_.N);
KeyVal[] kvy = (KeyVal[])obj;
int kvy_len = kvy.length;
for (int i = 0; i < kvy_len; ++i) {
KeyVal kv = kvy[i];
Object kv_val = kv.Val();
Kv_obj(Bry_.new_u8(kv.Key()), kv_val, Type_adp_.To_tid_obj(kv_val));
}
}
Write_grp_end(Bool_.Y, Sym_nde_end);
}
private void Write_val_itm__ary(boolean called_by_ary, int grp_type, Object obj) {
Ary_bgn_keyless();
Object ary = Array_.cast(ary_obj);
int len = Array_.Len(ary);
for (int i = 0; i < len; ++i) {
Object itm = Array_.Get_at(ary, i);
Ary_itm_obj(itm);
if (grp_type == Grp_type__json_ary) {
Json_ary sub_ary = (Json_ary)(obj);
int len = sub_ary.Len();
for (int i = 0; i < len; ++i) {
Json_itm sub_itm = sub_ary.Get_at(i);
Ary_itm_obj(Get_x(sub_itm));
}
}
else {
Object ary = Array_.cast(obj);
int len = Array_.Len(ary);
for (int i = 0; i < len; ++i) {
Object itm = Array_.Get_at(ary, i);
Ary_itm_obj(itm);
}
}
Write_grp_end(Bool_.N, Sym_ary_end);
}
@@ -227,4 +284,13 @@ public class Json_wtr {
, Sym_ary_end = Bry_.new_a7("]")
, Sym_itm_spr = Bry_.new_a7(",")
;
private static final int Grp_type__json_nde = 1, Grp_type__kv_ary = 2, Grp_type__json_ary = 3, Grp_type__obj_ary = 4;
private static int Grp_type__get(Object obj) {
Class<?> type = obj.getClass();
if (Type_adp_.Eq(type, KeyVal[].class)) return Grp_type__kv_ary;
else if (Type_adp_.Is_array(type)) return Grp_type__obj_ary;
else if (Type_adp_.Eq(type, Json_nde.class)) return Grp_type__json_nde;
else if (Type_adp_.Eq(type, Json_ary.class)) return Grp_type__json_ary;
else throw Err_.new_unhandled(type);
}
}