mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
v2.7.3.1
This commit is contained in:
@@ -82,7 +82,7 @@ public class Bit_ {
|
||||
public static int Shift_lhs(int val, int shift) {return val << shift;}
|
||||
public static int Shift_rhs(int val, int shift) {return val >> shift;}
|
||||
public static int Shift_lhs_to_int(int[] shift_ary, int... val_ary) {
|
||||
int val_len = val_ary.length; if (val_len > shift_ary.length) throw Exc_.new_("vals must be less than shifts", "vals", val_len, "shifts", shift_ary.length);
|
||||
int val_len = val_ary.length; if (val_len > shift_ary.length) throw Err_.new_wo_type("vals must be less than shifts", "vals", val_len, "shifts", shift_ary.length);
|
||||
int rv = 0;
|
||||
for (int i = 0; i < val_len; ++i) {
|
||||
int val = val_ary[i];
|
||||
|
||||
@@ -20,29 +20,29 @@ public class Gfo_fld_base {
|
||||
public byte Row_dlm() {return row_dlm;} public Gfo_fld_base Row_dlm_(byte v) {row_dlm = v; return this;} protected byte row_dlm = Byte_ascii.Nl;
|
||||
public byte Fld_dlm() {return fld_dlm;} public Gfo_fld_base Fld_dlm_(byte v) {fld_dlm = v; return this;} protected byte fld_dlm = Byte_ascii.Pipe;
|
||||
public byte Escape_dlm() {return escape_dlm;} public Gfo_fld_base Escape_dlm_(byte v) {escape_dlm = v; return this;} protected byte escape_dlm = Byte_ascii.Tilde;
|
||||
public byte Quote_dlm() {return quote_dlm;} public Gfo_fld_base Quote_dlm_(byte v) {quote_dlm = v; return this;} protected byte quote_dlm = Byte_ascii.Nil;
|
||||
public byte Quote_dlm() {return quote_dlm;} public Gfo_fld_base Quote_dlm_(byte v) {quote_dlm = v; return this;} protected byte quote_dlm = Byte_ascii.Null;
|
||||
public Gfo_fld_base Escape_reg(byte b) {return Escape_reg(b, b);}
|
||||
public byte[] Escape_decode() {return decode_regy;}
|
||||
public Gfo_fld_base Escape_reg(byte key, byte val) {encode_regy[key] = val; decode_regy[val] = key; return this;} protected byte[] decode_regy = new byte[256]; protected byte[] encode_regy = new byte[256];
|
||||
public Gfo_fld_base Escape_clear() {
|
||||
for (int i = 0; i < 256; i++)
|
||||
decode_regy[i] = Byte_ascii.Nil;
|
||||
decode_regy[i] = Byte_ascii.Null;
|
||||
for (int i = 0; i < 256; i++)
|
||||
encode_regy[i] = Byte_ascii.Nil;
|
||||
encode_regy[i] = Byte_ascii.Null;
|
||||
return this;
|
||||
}
|
||||
Gfo_fld_base Ini_common() {
|
||||
return Escape_reg(Byte_ascii.Nl, Byte_ascii.Ltr_n).Escape_reg(Byte_ascii.Tab, Byte_ascii.Ltr_t).Escape_reg(Byte_ascii.Cr, Byte_ascii.Ltr_r)
|
||||
.Escape_reg(Byte_ascii.Backfeed, Byte_ascii.Ltr_b); // .Escape_reg(Byte_ascii.Nil, Byte_ascii.Num_0)
|
||||
.Escape_reg(Byte_ascii.Backfeed, Byte_ascii.Ltr_b); // .Escape_reg(Byte_ascii.Null, Byte_ascii.Num_0)
|
||||
}
|
||||
protected Gfo_fld_base Ctor_xdat_base() {
|
||||
return Escape_clear().Ini_common()
|
||||
.Fld_dlm_(Byte_ascii.Pipe).Row_dlm_(Byte_ascii.Nl).Escape_dlm_(Byte_ascii.Tilde).Quote_dlm_(Byte_ascii.Nil)
|
||||
.Fld_dlm_(Byte_ascii.Pipe).Row_dlm_(Byte_ascii.Nl).Escape_dlm_(Byte_ascii.Tilde).Quote_dlm_(Byte_ascii.Null)
|
||||
.Escape_reg(Byte_ascii.Pipe, Byte_ascii.Ltr_p).Escape_reg(Byte_ascii.Tilde);
|
||||
}
|
||||
protected Gfo_fld_base Ctor_sql_base() {
|
||||
return Escape_clear().Ini_common()
|
||||
.Fld_dlm_(Byte_ascii.Comma).Row_dlm_(Byte_ascii.Paren_end).Escape_dlm_(Byte_ascii.Backslash).Quote_dlm_(Byte_ascii.Apos)
|
||||
.Escape_reg(Byte_ascii.Backslash).Escape_reg(Byte_ascii.Quote).Escape_reg(Byte_ascii.Apos); // , Escape_eof = Bry_.new_u8("\\Z")
|
||||
.Escape_reg(Byte_ascii.Backslash).Escape_reg(Byte_ascii.Quote).Escape_reg(Byte_ascii.Apos); // , Escape_eof = Bry_.new_a7("\\Z")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ public class Gfo_fld_rdr extends Gfo_fld_base {
|
||||
f += (data[fld_bgn + 16] - Byte_ascii.Num_0) * 100;
|
||||
f += (data[fld_bgn + 17] - Byte_ascii.Num_0) * 10;
|
||||
f += (data[fld_bgn + 18] - Byte_ascii.Num_0);
|
||||
if (data[fld_bgn + 19] != fld_dlm) throw Exc_.new_("csv date is invalid", "txt", String_.new_u8_by_len(data, fld_bgn, 20));
|
||||
if (data[fld_bgn + 19] != fld_dlm) throw Err_.new_wo_type("csv date is invalid", "txt", String_.new_u8_by_len(data, fld_bgn, 20));
|
||||
fld_end = pos + 20;
|
||||
pos = fld_end + 1; ++fld_idx;
|
||||
return DateAdp_.new_(y, M, d, H, m, s, f);
|
||||
@@ -78,7 +78,7 @@ public class Gfo_fld_rdr extends Gfo_fld_base {
|
||||
return;
|
||||
}
|
||||
}
|
||||
throw Exc_.new_("fld_dlm failed", "fld_dlm", (char)fld_dlm, "bgn", fld_bgn);
|
||||
throw Err_.new_wo_type("fld_dlm failed", "fld_dlm", (char)fld_dlm, "bgn", fld_bgn);
|
||||
}
|
||||
public String Read_str_escape() {Move_next_escaped(bfr); return String_.new_u8(bfr.Xto_bry_and_clear());}
|
||||
public byte[] Read_bry_escape() {Move_next_escaped(bfr); return bfr.Xto_bry_and_clear();}
|
||||
@@ -100,13 +100,13 @@ public class Gfo_fld_rdr extends Gfo_fld_base {
|
||||
}
|
||||
else if (b == escape_dlm) {
|
||||
++i;
|
||||
// if (i == data_len) throw Err_.new_("escape char at end of String");
|
||||
// if (i == data_len) throw Err_.new_wo_type("escape char at end of String");
|
||||
b = data[i];
|
||||
byte escape_val = decode_regy[b];
|
||||
if (escape_val == Byte_ascii.Nil) {trg.Add_byte(escape_dlm).Add_byte(b);} //throw Err_.new_fmt_("unknown escape key: key={0}", data[i]);
|
||||
if (escape_val == Byte_ascii.Null) {trg.Add_byte(escape_dlm).Add_byte(b);} //throw Err_.new_fmt_("unknown escape key: key={0}", data[i]);
|
||||
else trg.Add_byte(escape_val);
|
||||
}
|
||||
else if (b == Byte_ascii.Nil) {
|
||||
else if (b == Byte_ascii.Null) {
|
||||
trg.Add(Bry_nil);
|
||||
}
|
||||
else if (b == quote_dlm) {
|
||||
|
||||
@@ -39,7 +39,7 @@ public class Gfo_fld_wtr extends Gfo_fld_base {
|
||||
for (int i = bgn; i < end; i++) {
|
||||
byte b = val[i];
|
||||
byte escape_val = encode_regy[b & 0xFF]; // PATCH.JAVA:need to convert to unsigned byte
|
||||
if (escape_val == Byte_ascii.Nil) bfr.Add_byte(b);
|
||||
if (escape_val == Byte_ascii.Null) bfr.Add_byte(b);
|
||||
else {bfr.Add_byte(escape_dlm); bfr.Add_byte(escape_val);}
|
||||
}
|
||||
return this;
|
||||
|
||||
67
400_xowa/src/gplx/core/json/Json_doc.java
Normal file
67
400_xowa/src/gplx/core/json/Json_doc.java
Normal file
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
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.core.json; import gplx.*; import gplx.core.*;
|
||||
public class Json_doc {
|
||||
public void Ctor(byte[] src, Json_nde root) {this.src = src; this.root = root;}
|
||||
public Bry_bfr Bfr() {return bfr;} Bry_bfr bfr = Bry_bfr.new_();
|
||||
public Number_parser Utl_num_parser() {return utl_num_parser;} Number_parser utl_num_parser = new Number_parser();
|
||||
public byte[] Str_utf8_bry() {return str_utf8_bry;} private byte[] str_utf8_bry = new byte[6];
|
||||
public byte[] Src() {return src;} private byte[] src;
|
||||
public Json_nde Root() {return root;} Json_nde root;
|
||||
public byte[] Get_val_as_bry_or(byte[] qry_bry, byte[] or) {tmp_qry_bry[0] = qry_bry; return Get_val_as_bry_or(tmp_qry_bry, or);}
|
||||
public byte[] Get_val_as_bry_or(byte[][] qry_bry, byte[] or) {
|
||||
Json_itm nde = Find_nde(root, qry_bry, qry_bry.length - 1, 0);
|
||||
return nde == null || nde.Tid() != Json_itm_.Tid_string ? or : nde.Data_bry();
|
||||
}
|
||||
public String Get_val_as_str_or(byte[] qry_bry, String or) {tmp_qry_bry[0] = qry_bry; return Get_val_as_str_or(tmp_qry_bry, or);}
|
||||
public String Get_val_as_str_or(byte[][] qry_bry, String or) {
|
||||
Json_itm nde = Find_nde(root, qry_bry, qry_bry.length - 1, 0);
|
||||
return nde == null || nde.Tid() != Json_itm_.Tid_string ? or : (String)nde.Data();
|
||||
}
|
||||
public Json_grp Get_grp(byte[] qry_bry) {
|
||||
tmp_qry_bry[0] = qry_bry;
|
||||
Json_itm rv = Find_nde(root, tmp_qry_bry, 0, 0); if (rv == null) return null;
|
||||
return (Json_grp)rv;
|
||||
} private byte[][] tmp_qry_bry = new byte[1][];
|
||||
public Json_grp Get_grp(byte[][] qry_bry) {
|
||||
Json_itm rv = Find_nde(root, qry_bry, qry_bry.length - 1, 0); if (rv == null) return null;
|
||||
return (Json_grp)rv;
|
||||
}
|
||||
public Json_itm Find_nde(byte[] key) {
|
||||
tmp_qry_bry[0] = key;
|
||||
return Find_nde(root, tmp_qry_bry, 0, 0);
|
||||
}
|
||||
private Json_itm Find_nde(Json_nde owner, byte[][] paths, int paths_last, int paths_idx) {
|
||||
byte[] path = paths[paths_idx];
|
||||
int subs_len = owner.Len();
|
||||
for (int i = 0; i < subs_len; i++) {
|
||||
Json_itm_kv itm = Json_itm_kv.cast_(owner.Get_at(i)); if (itm == null) continue; // ignore simple props, arrays, ndes
|
||||
if (!itm.Key_eq(path)) continue;
|
||||
if (paths_idx == paths_last) return itm.Val();
|
||||
Json_nde sub_nde = Json_nde.cast_(itm.Val()); if (sub_nde == null) return null; // match, but has not a nde; exit
|
||||
return Find_nde(sub_nde, paths, paths_last, paths_idx + 1);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public static Json_doc new_apos_concat_nl(String... ary) {return new_apos_(String_.Concat_lines_nl(ary));}
|
||||
public static Json_doc new_apos_(String v) {return new_(Bry_.Replace(Bry_.new_u8(v), Byte_ascii.Apos, Byte_ascii.Quote));}
|
||||
public static Json_doc new_(String v) {return new_(Bry_.new_u8(v));}
|
||||
public static Json_doc new_(byte[] v) {
|
||||
synchronized (parser) {return parser.Parse(v);}
|
||||
} private static final Json_parser parser = new Json_parser();
|
||||
}
|
||||
42
400_xowa/src/gplx/core/json/Json_doc_bldr.java
Normal file
42
400_xowa/src/gplx/core/json/Json_doc_bldr.java
Normal file
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
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.core.json; import gplx.*; import gplx.core.*;
|
||||
public class Json_doc_bldr {
|
||||
public Json_nde Nde() {return factory.Nde(-1);}
|
||||
public Json_nde Nde(Json_grp owner) {
|
||||
Json_nde rv = factory.Nde(-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_itm_kv Kv_int(Json_grp owner, String key, int val) {Json_itm_kv rv = factory.Kv(Json_itm_tmp.new_str_(key), Json_itm_tmp.new_int_(val)); owner.Add(rv); return rv;}
|
||||
public Json_itm_kv Kv_str(Json_grp owner, String key, String val) {Json_itm_kv rv = factory.Kv(Json_itm_tmp.new_str_(key), Json_itm_tmp.new_str_(val)); owner.Add(rv); return rv;}
|
||||
public Json_itm_ary Kv_ary(Json_grp owner, String key, Json_itm... subs) {
|
||||
Json_itm key_itm = Json_itm_tmp.new_str_(key);
|
||||
Json_itm_ary val_ary = factory.Ary(-1, -1);
|
||||
Json_itm_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();
|
||||
}
|
||||
89
400_xowa/src/gplx/core/json/Json_doc_srl.java
Normal file
89
400_xowa/src/gplx/core/json/Json_doc_srl.java
Normal file
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
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.core.json; import gplx.*; import gplx.core.*;
|
||||
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 Json_doc_srl Write_root(byte[] key, Object val) {
|
||||
Write_nde_bgn();
|
||||
Write_obj(false, key, val);
|
||||
Write_nde_end();
|
||||
return this;
|
||||
}
|
||||
public void Write_obj(boolean comma, byte[] key, Object val) {
|
||||
Class<?> t = ClassAdp_.ClassOf_obj(val);
|
||||
if (ClassAdp_.Is_array(t))
|
||||
Write_kv_ary(comma, key, (Object[])val);
|
||||
else
|
||||
Write_kv_str(comma, key, Object_.Xto_str_strict_or_empty(val));
|
||||
}
|
||||
private void Write_kv_ary(boolean comma, byte[] key, Object[] val) {
|
||||
Write_key(comma, key); Write_new_line(); // '"key":\n'
|
||||
Write_ary_bgn(); // '[\n'
|
||||
Indent_add(); // -->
|
||||
int len = val.length;
|
||||
for (int i = 0; i < len; i++) {
|
||||
Write_itm_hdr(i != 0); // ', '
|
||||
Write_str(Bry_.new_u8(Object_.Xto_str_strict_or_null(val[i])));
|
||||
Write_new_line();
|
||||
}
|
||||
Indent_del();
|
||||
Write_ary_end();
|
||||
}
|
||||
private void Write_kv_str(boolean comma, byte[] key, String val) {
|
||||
Write_key(comma, key); // "key":
|
||||
Write_str(Bry_.new_u8(val)); // "val"
|
||||
Write_new_line(); // \n
|
||||
}
|
||||
private void Write_key(boolean comma, byte[] key) { // "key":
|
||||
Write_indent();
|
||||
Write_str(key);
|
||||
bfr.Add_byte(Byte_ascii.Colon);
|
||||
}
|
||||
private void Write_indent() {if (ws_enabled && indent > 0) bfr.Add_byte_repeat(Byte_ascii.Space, indent);}
|
||||
private void Write_str(byte[] v) {
|
||||
if (v == null)
|
||||
bfr.Add(Bry_null);
|
||||
else
|
||||
bfr.Add_byte(Byte_ascii.Quote).Add(v).Add_byte(Byte_ascii.Quote);
|
||||
} private static final byte[] Bry_null = Bry_.new_a7("null");
|
||||
private void Write_comma(boolean comma) {
|
||||
if (comma)
|
||||
bfr.Add_byte(Byte_ascii.Comma);
|
||||
else {
|
||||
if (ws_enabled)
|
||||
bfr.Add_byte(Byte_ascii.Space);
|
||||
}
|
||||
if (ws_enabled)
|
||||
bfr.Add_byte(Byte_ascii.Space);
|
||||
}
|
||||
private void Write_ary_bgn() {Indent_add(); Write_indent(); bfr.Add_byte(Byte_ascii.Brack_bgn); Write_new_line();}
|
||||
private void Write_ary_end() { Write_indent(); bfr.Add_byte(Byte_ascii.Brack_end); Write_new_line(); Indent_del();}
|
||||
private void Write_nde_bgn() {Indent_add(); Write_indent(); bfr.Add_byte(Byte_ascii.Curly_bgn); Write_new_line();}
|
||||
private void Write_nde_end() { Write_indent(); bfr.Add_byte(Byte_ascii.Curly_end); Write_new_line(); Indent_del();}
|
||||
private void Write_itm_hdr(boolean comma) {
|
||||
Write_indent();
|
||||
Write_comma(comma);
|
||||
}
|
||||
private void Indent_add() {indent += 2;}
|
||||
private void Indent_del() {indent -= 2;}
|
||||
private void Write_new_line() {if (ws_enabled) bfr.Add_byte_nl();}
|
||||
}
|
||||
45
400_xowa/src/gplx/core/json/Json_doc_tst.java
Normal file
45
400_xowa/src/gplx/core/json/Json_doc_tst.java
Normal file
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
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.core.json; import gplx.*; import gplx.core.*;
|
||||
import org.junit.*;
|
||||
public class Json_doc_tst {
|
||||
Json_qry_mgr_fxt fxt = new Json_qry_mgr_fxt();
|
||||
@Before public void init() {}
|
||||
@Test public void Select() {
|
||||
Json_doc doc = Json_doc.new_apos_(String_.Concat_lines_nl
|
||||
( "{'0':"
|
||||
, " {'0_0':"
|
||||
, " {'0_0_0':'000'"
|
||||
, " },"
|
||||
, " '0_1':"
|
||||
, " {'0_1_0':'010'"
|
||||
, " }"
|
||||
, " }"
|
||||
, "}"
|
||||
));
|
||||
fxt.Test_get_val_as_str(doc, "0/0_0/0_0_0", "000");
|
||||
fxt.Test_get_val_as_str(doc, "0/0_1/0_1_0", "010");
|
||||
fxt.Test_get_val_as_str(doc, "x", null);
|
||||
}
|
||||
}
|
||||
class Json_qry_mgr_fxt {
|
||||
public void Test_get_val_as_str(Json_doc doc, String qry, String expd){
|
||||
byte[][] qry_bry = Bry_.Split(Bry_.new_u8(qry), Byte_ascii.Slash);
|
||||
Tfds.Eq(expd, doc.Get_val_as_str_or(qry_bry, null));
|
||||
}
|
||||
}
|
||||
98
400_xowa/src/gplx/core/json/Json_doc_wtr.java
Normal file
98
400_xowa/src/gplx/core/json/Json_doc_wtr.java
Normal file
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
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.core.json; import gplx.*; import gplx.core.*;
|
||||
public class Json_doc_wtr {
|
||||
private int indent = -2;
|
||||
private Bry_bfr bfr = Bry_bfr.reset_(255);
|
||||
public Json_doc_wtr Indent() {return Indent(indent);}
|
||||
private Json_doc_wtr Indent(int v) {if (v > 0) bfr.Add_byte_repeat(Byte_ascii.Space, v); return this;}
|
||||
public Json_doc_wtr Indent_add() {indent += 2; return this;}
|
||||
public Json_doc_wtr Indent_del() {indent -= 2; return this;}
|
||||
public Json_doc_wtr Nde_bgn() {Indent_add(); Indent(); bfr.Add_byte(Byte_ascii.Curly_bgn).Add_byte_nl(); return this;}
|
||||
public Json_doc_wtr Nde_end() { Indent(); bfr.Add_byte(Byte_ascii.Curly_end).Add_byte_nl(); Indent_del(); return this;}
|
||||
public Json_doc_wtr Ary_bgn() {Indent_add(); Indent(); bfr.Add_byte(Byte_ascii.Brack_bgn).Add_byte_nl(); return this;}
|
||||
public Json_doc_wtr Ary_end() { Indent(); bfr.Add_byte(Byte_ascii.Brack_end).Add_byte_nl(); Indent_del(); return this;}
|
||||
public Json_doc_wtr New_line() {bfr.Add_byte_nl(); return this;}
|
||||
public Json_doc_wtr Str(byte[] v) {
|
||||
if (v == null)
|
||||
bfr.Add(Bry_null);
|
||||
else
|
||||
bfr.Add_byte(Byte_ascii.Quote).Add(v).Add_byte(Byte_ascii.Quote);
|
||||
return this;
|
||||
} private static final byte[] Bry_null = Bry_.new_a7("null");
|
||||
public Json_doc_wtr Int(int v) {bfr.Add_int_variable(v); return this;}
|
||||
public Json_doc_wtr Double(double v) {bfr.Add_double(v); return this;}
|
||||
public Json_doc_wtr Comma() {Indent(); bfr.Add_byte(Byte_ascii.Comma).Add_byte_nl(); return this;}
|
||||
public Json_doc_wtr Kv_ary_empty(boolean comma, byte[] key) {
|
||||
Key_internal(comma, key);
|
||||
bfr.Add_byte(Byte_ascii.Brack_bgn).Add_byte(Byte_ascii.Brack_end);
|
||||
bfr.Add_byte_nl();
|
||||
return this;
|
||||
}
|
||||
public Json_doc_wtr Kv(boolean comma, byte[] key, byte[] val) {
|
||||
Key_internal(comma, key);
|
||||
Str(val);
|
||||
bfr.Add_byte_nl();
|
||||
return this;
|
||||
}
|
||||
public Json_doc_wtr Kv_double(boolean comma, byte[] key, double v) {
|
||||
Key_internal(comma, key);
|
||||
Double(v);
|
||||
bfr.Add_byte_nl();
|
||||
return this;
|
||||
}
|
||||
public Json_doc_wtr Kv(boolean comma, byte[] key, int v) {
|
||||
Key_internal(comma, key);
|
||||
Int(v);
|
||||
bfr.Add_byte_nl();
|
||||
return this;
|
||||
}
|
||||
public Json_doc_wtr Key(boolean comma, byte[] key) {
|
||||
Key_internal(comma, key);
|
||||
bfr.Add_byte_nl();
|
||||
return this;
|
||||
}
|
||||
public Json_doc_wtr Val(boolean comma, int v) {
|
||||
Val_internal(comma);
|
||||
Int(v);
|
||||
New_line();
|
||||
return this;
|
||||
}
|
||||
public Json_doc_wtr Val(boolean comma, byte[] v) {
|
||||
Val_internal(comma);
|
||||
Str(v);
|
||||
New_line();
|
||||
return this;
|
||||
}
|
||||
Json_doc_wtr Val_internal(boolean comma) {
|
||||
Indent();
|
||||
bfr.Add_byte(comma ? Byte_ascii.Comma : Byte_ascii.Space);
|
||||
bfr.Add_byte(Byte_ascii.Space);
|
||||
return this;
|
||||
}
|
||||
Json_doc_wtr Key_internal(boolean comma, byte[] key) {
|
||||
Indent();
|
||||
bfr.Add_byte(comma ? Byte_ascii.Comma : Byte_ascii.Space);
|
||||
bfr.Add_byte(Byte_ascii.Space);
|
||||
Str(key);
|
||||
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();}
|
||||
}
|
||||
29
400_xowa/src/gplx/core/json/Json_factory.java
Normal file
29
400_xowa/src/gplx/core/json/Json_factory.java
Normal file
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
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.core.json; import gplx.*; import gplx.core.*;
|
||||
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 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_itm_kv Kv(Json_itm key, Json_itm val) {return new Json_itm_kv(key, val);}
|
||||
public Json_itm_ary Ary(int bgn, int end) {return new Json_itm_ary(bgn, end);}
|
||||
public Json_nde Nde(int bgn) {return new Json_nde(bgn);}
|
||||
}
|
||||
34
400_xowa/src/gplx/core/json/Json_grp.java
Normal file
34
400_xowa/src/gplx/core/json/Json_grp.java
Normal file
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
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.core.json; import gplx.*; import gplx.core.*;
|
||||
public interface Json_grp extends Json_itm {
|
||||
void Src_end_(int v);
|
||||
int Len();
|
||||
Json_itm Get_at(int i);
|
||||
void Add(Json_itm itm);
|
||||
}
|
||||
class Json_grp_ {
|
||||
public static final Json_grp[] Ary_empty = new Json_grp[0];
|
||||
public static void Print_nl(Bry_bfr bfr) { // \n\n can be caused by nested groups (EX: "[[]]"); only print 1
|
||||
if (bfr.Bfr()[bfr.Len() - 1] != Byte_ascii.Nl)
|
||||
bfr.Add_byte_nl();
|
||||
}
|
||||
public static void Print_indent(Bry_bfr bfr, int depth) {
|
||||
if (depth > 0) bfr.Add_byte_repeat(Byte_ascii.Space, depth * 2); // indent
|
||||
}
|
||||
}
|
||||
116
400_xowa/src/gplx/core/json/Json_itm.java
Normal file
116
400_xowa/src/gplx/core/json/Json_itm.java
Normal file
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
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.core.json; import gplx.*; import gplx.core.*;
|
||||
public interface Json_itm {
|
||||
byte Tid();
|
||||
int Src_bgn();
|
||||
int Src_end();
|
||||
Object Data();
|
||||
byte[] Data_bry();
|
||||
void Print_as_json(Bry_bfr bfr, int depth);
|
||||
boolean Data_eq(byte[] comp);
|
||||
}
|
||||
class Json_itm_null extends Json_itm_base {
|
||||
Json_itm_null() {this.Ctor(-1, -1);}
|
||||
@Override public byte Tid() {return Json_itm_.Tid_null;}
|
||||
@Override public Object Data() {return null;}
|
||||
@Override public void Print_as_json(Bry_bfr bfr, int depth) {bfr.Add(Bry_null);}
|
||||
@Override public byte[] Data_bry() {return Bry_null;}
|
||||
private static final byte[] Bry_null = Bry_.new_a7("null");
|
||||
public static Json_itm_null Null = new Json_itm_null();
|
||||
}
|
||||
class Json_itm_bool extends Json_itm_base {
|
||||
public Json_itm_bool(boolean data) {this.data = data; this.Ctor(-1, -1);} private boolean data;
|
||||
@Override public byte Tid() {return Json_itm_.Tid_bool;}
|
||||
@Override public Object Data() {return data;}
|
||||
@Override public byte[] Data_bry() {return data ? Json_itm_.Const_true : Json_itm_.Const_false;}
|
||||
@Override public void Print_as_json(Bry_bfr bfr, int depth) {bfr.Add(data ? Json_itm_.Const_true: Json_itm_.Const_false);}
|
||||
public static Json_itm_bool Bool_n = new Json_itm_bool(false), Bool_y = new Json_itm_bool(true);
|
||||
}
|
||||
class Json_itm_decimal extends Json_itm_base {
|
||||
public Json_itm_decimal(Json_doc doc, int src_bgn, int src_end) {this.Ctor(src_bgn, src_end); this.doc = doc;} Json_doc doc;
|
||||
@Override public byte Tid() {return Json_itm_.Tid_decimal;}
|
||||
@Override public Object Data() {
|
||||
if (data == null)
|
||||
data = DecimalAdp_.parse_(String_.new_a7(this.Data_bry()));
|
||||
return data;
|
||||
} DecimalAdp data;
|
||||
@Override public byte[] Data_bry() {
|
||||
if (data_bry == null) data_bry = Bry_.Mid(doc.Src(), this.Src_bgn(), this.Src_end());
|
||||
return data_bry;
|
||||
} byte[] data_bry;
|
||||
@Override public void Print_as_json(Bry_bfr bfr, int depth) {bfr.Add_mid(doc.Src(), this.Src_bgn(), this.Src_end());}
|
||||
}
|
||||
class Json_itm_str extends Json_itm_base {
|
||||
public Json_itm_str(Json_doc doc, int src_bgn, int src_end, boolean exact) {this.Ctor(src_bgn + 1, src_end - 1); this.doc = doc; this.exact = exact;} private boolean exact; Json_doc doc;
|
||||
@Override public byte Tid() {return Json_itm_.Tid_string;}
|
||||
@Override public void Print_as_json(Bry_bfr bfr, int depth) {
|
||||
bfr.Add_byte(Byte_ascii.Quote);
|
||||
gplx.html.Html_utl.Escape_html_to_bfr(bfr, doc.Src(), this.Src_bgn(), this.Src_end(), true, true, true, true, false); // false to apos for backwards compatibility
|
||||
bfr.Add_byte(Byte_ascii.Quote);
|
||||
}
|
||||
@Override public Object Data() {
|
||||
if (data_str == null) {
|
||||
if (data_bry == null)
|
||||
data_bry = Data_make_bry();
|
||||
data_str = String_.new_u8(data_bry);
|
||||
}
|
||||
return data_str;
|
||||
} private String data_str;
|
||||
@Override public byte[] Data_bry() {if (data_bry == null) data_bry = Data_make_bry(); return data_bry;}
|
||||
@Override public boolean Data_eq(byte[] comp) {
|
||||
if (exact) return Bry_.Eq(comp, doc.Src(), this.Src_bgn(), this.Src_end());
|
||||
if (data_bry == null) data_bry = Data_make_bry();
|
||||
return Bry_.Match(data_bry, comp);
|
||||
} byte[] data_bry = null;
|
||||
private byte[] Data_make_bry() {
|
||||
byte[] src = doc.Src(); int bgn = this.Src_bgn(), end = this.Src_end();
|
||||
if (exact) return Bry_.Mid(src, bgn, end);
|
||||
Bry_bfr bfr = doc.Bfr();
|
||||
byte[] utf8_bry = doc.Str_utf8_bry();
|
||||
for (int i = bgn; i < end; i++) {
|
||||
byte b = src[i];
|
||||
switch (b) {
|
||||
case Byte_ascii.Backslash:
|
||||
b = src[++i];
|
||||
switch (b) { // NOTE: must properly unescape chars; EX:wd.q:2; DATE:2014-04-23
|
||||
case Byte_ascii.Ltr_t: bfr.Add_byte(Byte_ascii.Tab); break;
|
||||
case Byte_ascii.Ltr_n: bfr.Add_byte(Byte_ascii.Nl); break;
|
||||
case Byte_ascii.Ltr_r: bfr.Add_byte(Byte_ascii.Cr); break;
|
||||
case Byte_ascii.Ltr_b: bfr.Add_byte(Byte_ascii.Backfeed); break;
|
||||
case Byte_ascii.Ltr_f: bfr.Add_byte(Byte_ascii.Formfeed); break;
|
||||
case Byte_ascii.Ltr_u:
|
||||
int utf8_val = gplx.texts.HexDecUtl.parse_or_(src, i + 1, i + 5, -1);
|
||||
int len = gplx.intl.Utf16_.Encode_int(utf8_val, utf8_bry, 0);
|
||||
bfr.Add_mid(utf8_bry, 0, len);
|
||||
i += 4;
|
||||
break; // \uFFFF 4 hex-dec
|
||||
case Byte_ascii.Backslash:
|
||||
case Byte_ascii.Slash:
|
||||
default:
|
||||
bfr.Add_byte(b); break; // \? " \ / b f n r t
|
||||
}
|
||||
break;
|
||||
default:
|
||||
bfr.Add_byte(b);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return bfr.Xto_bry_and_clear();
|
||||
}
|
||||
}
|
||||
24
400_xowa/src/gplx/core/json/Json_itm_.java
Normal file
24
400_xowa/src/gplx/core/json/Json_itm_.java
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
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.core.json; import gplx.*; import gplx.core.*;
|
||||
public class Json_itm_ {
|
||||
public static final Json_itm[] Ary_empty = new Json_itm[0];
|
||||
public static final byte Tid_unknown = 0, Tid_null = 1, Tid_bool = 2, Tid_int = 3, Tid_decimal = 4, Tid_string = 5, Tid_kv = 6, Tid_array = 7, Tid_nde = 8;
|
||||
public static final byte[][] Names = Bry_.Ary("unknown", "null", "boolean", "int", "decimal", "string", "keyval", "array", "nde");
|
||||
public static final byte[] Const_true = Bry_.new_a7("true"), Const_false = Bry_.new_a7("false"), Const_null = Bry_.new_a7("null");
|
||||
}
|
||||
71
400_xowa/src/gplx/core/json/Json_itm_ary.java
Normal file
71
400_xowa/src/gplx/core/json/Json_itm_ary.java
Normal file
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
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.core.json; import gplx.*; import gplx.core.*;
|
||||
public class Json_itm_ary extends Json_itm_base implements Json_grp {
|
||||
public Json_itm_ary(int src_bgn, int src_end) {this.Ctor(src_bgn, src_end);}
|
||||
@Override public byte Tid() {return Json_itm_.Tid_array;}
|
||||
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;} private int subs_len = 0, subs_max = 0;
|
||||
public Json_itm Get_at(int i) {return subs[i];}
|
||||
public Json_itm_ary 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_.CopyTo(subs, 0, new_subs, 0, subs_len);
|
||||
subs = new_subs;
|
||||
}
|
||||
subs[subs_len] = itm;
|
||||
subs_len = new_len;
|
||||
}
|
||||
@Override public void Print_as_json(Bry_bfr bfr, int depth) {
|
||||
if (subs_len == 0) { // empty grp; print on one line (rather than printing across 3)
|
||||
bfr.Add_byte(Byte_ascii.Brack_bgn).Add_byte(Byte_ascii.Brack_end);
|
||||
return;
|
||||
}
|
||||
bfr.Add_byte_nl();
|
||||
Json_grp_.Print_indent(bfr, depth);
|
||||
bfr.Add_byte(Byte_ascii.Brack_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.Brack_end).Add_byte_nl();
|
||||
}
|
||||
public byte[][] Xto_bry_ary() {
|
||||
if (subs_len == 0) return Bry_.Ary_empty;
|
||||
byte[][] rv = new byte[subs_len][];
|
||||
for (int i = 0; i < subs_len; ++i)
|
||||
rv[i] = subs[i].Data_bry();
|
||||
return rv;
|
||||
}
|
||||
private Json_itm[] subs = Json_itm_.Ary_empty;
|
||||
public static Json_itm_ary cast_(Json_itm v) {return v == null || v.Tid() != Json_itm_.Tid_array ? null : (Json_itm_ary)v;}
|
||||
}
|
||||
29
400_xowa/src/gplx/core/json/Json_itm_base.java
Normal file
29
400_xowa/src/gplx/core/json/Json_itm_base.java
Normal file
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
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.core.json; import gplx.*; import gplx.core.*;
|
||||
public abstract class Json_itm_base implements Json_itm {
|
||||
public abstract byte Tid();
|
||||
public void Ctor(int src_bgn, int src_end) {this.src_bgn = src_bgn; this.src_end = src_end;}
|
||||
public int Src_bgn() {return src_bgn;} private int src_bgn;
|
||||
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 abstract void Print_as_json(Bry_bfr bfr, int depth);
|
||||
@gplx.Virtual public boolean Data_eq(byte[] comp) {return false;}
|
||||
}
|
||||
33
400_xowa/src/gplx/core/json/Json_itm_int.java
Normal file
33
400_xowa/src/gplx/core/json/Json_itm_int.java
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
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.core.json; import gplx.*; import gplx.core.*;
|
||||
public class Json_itm_int extends Json_itm_base {
|
||||
public Json_itm_int(Json_doc doc, int src_bgn, int src_end) {this.Ctor(src_bgn, src_end); this.doc = doc;} Json_doc doc;
|
||||
@Override public byte Tid() {return Json_itm_.Tid_int;}
|
||||
public int Data_as_int() {
|
||||
if (data_is_null) {
|
||||
data = doc.Utl_num_parser().Parse(doc.Src(), Src_bgn(), Src_end()).Rv_as_int();
|
||||
data_is_null = false;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
@Override public Object Data() {return Data_as_int();} int data; boolean data_is_null = true;
|
||||
@Override public byte[] Data_bry() {if (data_bry == null) data_bry = Bry_.Mid(doc.Src(), this.Src_bgn(), this.Src_end()); return data_bry;} private byte[] data_bry;
|
||||
@Override public void Print_as_json(Bry_bfr bfr, int depth) {bfr.Add_mid(doc.Src(), this.Src_bgn(), this.Src_end());}
|
||||
public static Json_itm_int cast_(Json_itm v) {return v == null || v.Tid() != Json_itm_.Tid_int ? null : (Json_itm_int)v;}
|
||||
}
|
||||
35
400_xowa/src/gplx/core/json/Json_itm_kv.java
Normal file
35
400_xowa/src/gplx/core/json/Json_itm_kv.java
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
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.core.json; import gplx.*; import gplx.core.*;
|
||||
public class Json_itm_kv extends Json_itm_base {
|
||||
public Json_itm_kv(Json_itm key, Json_itm val) {this.key = key; this.val = val;}
|
||||
@Override public byte Tid() {return Json_itm_.Tid_kv;}
|
||||
public Json_itm Key() {return key;} Json_itm key;
|
||||
public Json_itm Val() {return val;} Json_itm val;
|
||||
public String Key_as_str() {return (String)key.Data();}
|
||||
public boolean Key_eq(byte[] comp) {return ((Json_itm_str)key).Data_eq(comp);}
|
||||
@Override public Object Data() {return null;}
|
||||
@Override public byte[] Data_bry() {return null;}
|
||||
@Override public void Print_as_json(Bry_bfr bfr, int depth) {
|
||||
key.Print_as_json(bfr, depth);
|
||||
bfr.Add_byte(Byte_ascii.Colon);
|
||||
val.Print_as_json(bfr, depth);
|
||||
}
|
||||
public static final Json_itm_kv[] Ary_empty = new Json_itm_kv[0];
|
||||
public static Json_itm_kv cast_(Json_itm v) {return v == null || v.Tid() != Json_itm_.Tid_kv ? null : (Json_itm_kv)v;}
|
||||
}
|
||||
31
400_xowa/src/gplx/core/json/Json_itm_tmp.java
Normal file
31
400_xowa/src/gplx/core/json/Json_itm_tmp.java
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
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.core.json; import gplx.*; import gplx.core.*;
|
||||
public class Json_itm_tmp implements Json_itm {
|
||||
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(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_string, "\"" + v + "\"");}
|
||||
public static Json_itm new_int_(int v) {return new Json_itm_tmp(Json_itm_.Tid_int, Int_.Xto_str(v));}
|
||||
}
|
||||
61
400_xowa/src/gplx/core/json/Json_kv_ary_srl.java
Normal file
61
400_xowa/src/gplx/core/json/Json_kv_ary_srl.java
Normal file
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
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.core.json; import gplx.*; import gplx.core.*;
|
||||
public class Json_kv_ary_srl {
|
||||
public static KeyVal Kv_by_itm(Json_itm itm) {
|
||||
switch (itm.Tid()) {
|
||||
case Json_itm_.Tid_kv:
|
||||
Json_itm_kv kv = (Json_itm_kv)itm;
|
||||
return KeyVal_.new_(kv.Key_as_str(), Val_by_itm(kv.Val()));
|
||||
default:
|
||||
throw Err_.new_unhandled(itm.Tid());
|
||||
}
|
||||
}
|
||||
private static Object Val_by_itm(Json_itm itm) {
|
||||
switch (itm.Tid()) {
|
||||
case Json_itm_.Tid_bool: return Bool_.Xto_str_lower(Bool_.cast_(itm.Data()));
|
||||
case Json_itm_.Tid_int:
|
||||
case Json_itm_.Tid_null:
|
||||
case Json_itm_.Tid_string:
|
||||
case Json_itm_.Tid_decimal: return itm.Data();
|
||||
case Json_itm_.Tid_array: return Val_by_itm_ary((Json_itm_ary)itm);
|
||||
case Json_itm_.Tid_nde: return Val_by_itm_nde((Json_nde)itm);
|
||||
case Json_itm_.Tid_kv: // kv should never be val; EX: "a":"b":c; not possible
|
||||
default: throw Err_.new_unhandled(itm.Tid());
|
||||
}
|
||||
}
|
||||
private static KeyVal[] Val_by_itm_ary(Json_itm_ary itm) {
|
||||
int subs_len = itm.Len();
|
||||
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));
|
||||
rv[i] = kv;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public static KeyVal[] Val_by_itm_nde(Json_nde itm) {
|
||||
int subs_len = itm.Len();
|
||||
KeyVal[] rv = new KeyVal[subs_len];
|
||||
for (int i = 0; i < subs_len; i++) {
|
||||
Json_itm sub = itm.Get_at(i);
|
||||
rv[i] = Kv_by_itm(sub);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
50
400_xowa/src/gplx/core/json/Json_kv_ary_srl_tst.java
Normal file
50
400_xowa/src/gplx/core/json/Json_kv_ary_srl_tst.java
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
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.core.json; import gplx.*; import gplx.core.*;
|
||||
import org.junit.*;
|
||||
public class Json_kv_ary_srl_tst {
|
||||
@Before public void init() {fxt.Clear();} private Json_kv_ary_srl_fxt fxt = new Json_kv_ary_srl_fxt();
|
||||
@Test public void Null() {fxt.Test_parse("{'k0':null}" , fxt.ary_(fxt.kv_str_("k0", null)));}
|
||||
@Test public void Bool_n() {fxt.Test_parse("{'k0':false}" , fxt.ary_(fxt.kv_bool_("k0", false)));}
|
||||
@Test public void Num() {fxt.Test_parse("{'k0':123}" , fxt.ary_(fxt.kv_int_("k0", 123)));}
|
||||
@Test public void Str() {fxt.Test_parse("{'k0':'v0'}" , fxt.ary_(fxt.kv_str_("k0", "v0")));}
|
||||
@Test public void Num_dec() {fxt.Test_parse("{'k0':1.23}" , fxt.ary_(fxt.kv_dec_("k0", DecimalAdp_.parse_("1.23"))));}
|
||||
@Test public void Ary_int() {fxt.Test_parse("{'k0':[1,2,3]}" , fxt.ary_(fxt.kv_obj_("k0", fxt.ary_(fxt.kv_int_("1", 1), fxt.kv_int_("2", 2), fxt.kv_int_("3", 3)))));}
|
||||
@Test public void Ary_empty() {fxt.Test_parse("{'k0':[]}" , fxt.ary_(fxt.kv_obj_("k0", fxt.ary_())));}
|
||||
@Test public void Subs_int() {fxt.Test_parse("{'k0':{'k00':1,'k01':2}}" , fxt.ary_(fxt.kv_obj_("k0", fxt.ary_(fxt.kv_int_("k00", 1), fxt.kv_int_("k01", 2)))));}
|
||||
@Test public void Subs_empty() {fxt.Test_parse("{'k0':{}}" , fxt.ary_(fxt.kv_obj_("k0", fxt.ary_())));}
|
||||
}
|
||||
class Json_kv_ary_srl_fxt {
|
||||
public void Clear() {
|
||||
if (parser == null) {
|
||||
parser = new Json_parser();
|
||||
}
|
||||
} private Json_parser parser;
|
||||
public void Test_parse(String raw_str, KeyVal[] expd) {
|
||||
byte[] raw_bry = Json_parser_tst.Replace_apos(Bry_.new_u8(raw_str));
|
||||
Json_doc doc = parser.Parse(raw_bry);
|
||||
KeyVal[] actl = Json_kv_ary_srl.Val_by_itm_nde(doc.Root());
|
||||
Tfds.Eq_str_lines(KeyVal_.Ary_x_to_str(expd), KeyVal_.Ary_x_to_str(actl));
|
||||
}
|
||||
public KeyVal[] ary_(KeyVal... ary) {return ary;}
|
||||
public KeyVal kv_obj_(String key, Object val) {return KeyVal_.new_(key, val);}
|
||||
public KeyVal kv_str_(String key, String val) {return KeyVal_.new_(key, val);}
|
||||
public KeyVal kv_int_(String key, int val) {return KeyVal_.new_(key, val);}
|
||||
public KeyVal kv_bool_(String key, boolean val) {return KeyVal_.new_(key, Bool_.Xto_str_lower(val));}
|
||||
public KeyVal kv_dec_(String key, DecimalAdp val) {return KeyVal_.new_(key, val.Xto_str());}
|
||||
}
|
||||
93
400_xowa/src/gplx/core/json/Json_nde.java
Normal file
93
400_xowa/src/gplx/core/json/Json_nde.java
Normal file
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
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.core.json; import gplx.*; import gplx.core.*;
|
||||
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;
|
||||
public Json_nde(int src_bgn) {this.Ctor(src_bgn, -1);}
|
||||
@Override public byte Tid() {return Json_itm_.Tid_nde;}
|
||||
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_nde Get(String key) {return Get(Bry_.new_u8(key));}
|
||||
public Json_nde Get(byte[] key) {
|
||||
Json_itm_kv kv = Json_itm_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_itm_kv itm_as_kv = (Json_itm_kv)itm;
|
||||
if (Bry_.Eq(key, itm_as_kv.Key().Data_bry()))
|
||||
return itm;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
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_itm_kv kv = (Json_itm_kv)kv_obj;
|
||||
Json_itm val = kv.Val();
|
||||
if (val == null) return or;
|
||||
return 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_.CopyTo(subs, 0, new_subs, 0, subs_len);
|
||||
subs = new_subs;
|
||||
}
|
||||
subs[subs_len] = (Json_itm)itm;
|
||||
subs_len = new_len;
|
||||
}
|
||||
@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();
|
||||
}
|
||||
public static Json_nde cast_(Json_itm v) {return v == null || v.Tid() != Json_itm_.Tid_nde ? null : (Json_nde)v;}
|
||||
}
|
||||
167
400_xowa/src/gplx/core/json/Json_parser.java
Normal file
167
400_xowa/src/gplx/core/json/Json_parser.java
Normal file
@@ -0,0 +1,167 @@
|
||||
/*
|
||||
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.core.json; import gplx.*; import gplx.core.*;
|
||||
public class Json_parser {
|
||||
public Json_factory Factory() {return factory;} private Json_factory factory = new Json_factory();
|
||||
private byte[] src; private int src_len, pos; private Number_parser num_parser = new Number_parser();
|
||||
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 Json_doc Parse(byte[] src) {
|
||||
Json_doc doc = new Json_doc();
|
||||
this.src = src; this.src_len = src.length; pos = 0;
|
||||
Skip_ws();
|
||||
if (src.length == 0) return null;
|
||||
if (src[pos] != Byte_ascii.Curly_bgn) return null;
|
||||
Skip_ws();
|
||||
// if (src[pos + 1] != Byte_ascii.Quote) return null;
|
||||
// throw Err_.new_wo_type("doc must start with {");
|
||||
Json_nde root = Make_nde(doc);
|
||||
doc.Ctor(src, root);
|
||||
return doc;
|
||||
}
|
||||
Json_nde Make_nde(Json_doc doc) {
|
||||
++pos; // brack_bgn
|
||||
Json_nde nde = new Json_nde(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");
|
||||
}
|
||||
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_itm_kv(key, val);
|
||||
}
|
||||
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_.XtoStr(b));
|
||||
}
|
||||
throw Err_.new_wo_type("eos reached in val");
|
||||
}
|
||||
Json_itm Make_literal(byte[] remainder, int remainder_len, Json_itm singleton) {
|
||||
++pos; // 1st char
|
||||
int literal_end = pos + remainder_len;
|
||||
if (Bry_.Eq(remainder, src, pos, literal_end)) {
|
||||
pos = literal_end;
|
||||
return singleton;
|
||||
}
|
||||
throw Err_.new_wo_type("invalid literal");
|
||||
}
|
||||
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 += 4; break; // \uFFFF 4 hex-dec
|
||||
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");
|
||||
}
|
||||
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);
|
||||
return num_parser.Has_frac()
|
||||
? factory.Decimal(doc, num_bgn, pos)
|
||||
: factory.Int(doc, num_bgn, pos);
|
||||
}
|
||||
Json_itm_ary Make_ary(Json_doc doc) {
|
||||
Json_itm_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_.XtoStr(expd), Char_.XtoStr(src[pos]));
|
||||
}
|
||||
Err err_(byte[] src, int bgn, String fmt, Object... args) {return err_(src, bgn, src.length, fmt, args);}
|
||||
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);
|
||||
return Err_.new_wo_type(msg);
|
||||
}
|
||||
}
|
||||
99
400_xowa/src/gplx/core/json/Json_parser_tst.java
Normal file
99
400_xowa/src/gplx/core/json/Json_parser_tst.java
Normal file
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
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.core.json; import gplx.*; import gplx.core.*;
|
||||
import org.junit.*;
|
||||
public class Json_parser_tst {
|
||||
private final Json_parser_fxt fxt = new Json_parser_fxt();
|
||||
@Before public void init() {fxt.Clear();}
|
||||
@Test public void Null() {fxt.Test_parse_val0("{'k0':null}" , null);}
|
||||
@Test public void Bool_n() {fxt.Test_parse_val0("{'k0':false}" , false);}
|
||||
@Test public void Bool_y() {fxt.Test_parse_val0("{'k0':true}" , true);}
|
||||
@Test public void Num() {fxt.Test_parse_val0("{'k0':123}" , 123);}
|
||||
@Test public void Num_neg() {fxt.Test_parse_val0("{'k0':-123}" , -123);}
|
||||
@Test public void Str() {fxt.Test_parse_val0("{'k0':'v0'}" , "v0");}
|
||||
@Test public void Str_esc_quote() {fxt.Test_parse_val0("{'k0':'a\\\"b'}" , "a\"b");}
|
||||
@Test public void Str_esc_hex4() {fxt.Test_parse_val0("{'k0':'a\\u0021b'}" , "a!b");}
|
||||
@Test public void Num_dec() {fxt.Test_parse("{'k0':1.23}" , fxt.itm_nde_().Add_many(fxt.itm_kv_dec_("k0", "1.23")));}
|
||||
@Test public void Num_exp() {fxt.Test_parse("{'k0':1e+2}" , fxt.itm_nde_().Add_many(fxt.itm_kv_dec_("k0", "1e+2")));}
|
||||
@Test public void Num_mix() {fxt.Test_parse("{'k0':-1.23e-1}" , fxt.itm_nde_().Add_many(fxt.itm_kv_dec_("k0", "-1.23e-1")));}
|
||||
@Test public void Str_many() {fxt.Test_parse("{'k0':'v0','k1':'v1','k2':'v2'}", fxt.itm_nde_().Add_many(fxt.itm_kv_("k0", "v0"), fxt.itm_kv_("k1", "v1"), fxt.itm_kv_("k2", "v2")));}
|
||||
@Test public void Ary_empty() {fxt.Test_parse("{'k0':[]}", fxt.itm_nde_().Add_many(fxt.itm_kv_ary_int_("k0")));}
|
||||
@Test public void Ary_int() {fxt.Test_parse("{'k0':[1,2,3]}", fxt.itm_nde_().Add_many(fxt.itm_kv_ary_int_("k0", 1, 2, 3)));}
|
||||
@Test public void Ary_str() {fxt.Test_parse("{'k0':['a','b','c']}", fxt.itm_nde_().Add_many(fxt.itm_kv_ary_str_("k0", "a", "b", "c")));}
|
||||
@Test public void Ary_ws() {fxt.Test_parse("{'k0': [ 1 , 2 , 3 ] }", fxt.itm_nde_().Add_many(fxt.itm_kv_ary_int_("k0", 1, 2, 3)));}
|
||||
@Test public void Subs_int() {fxt.Test_parse("{'k0':{'k00':1}}", fxt.itm_nde_().Add_many(fxt.itm_kv_("k0", fxt.itm_nde_().Add_many(fxt.itm_kv_("k00", 1)))));}
|
||||
@Test public void Subs_empty() {fxt.Test_parse("{'k0':{}}", fxt.itm_nde_().Add_many(fxt.itm_kv_("k0", fxt.itm_nde_())));}
|
||||
@Test public void Subs_ws() {fxt.Test_parse("{'k0': { 'k00' : 1 } }", fxt.itm_nde_().Add_many(fxt.itm_kv_("k0", fxt.itm_nde_().Add_many(fxt.itm_kv_("k00", 1)))));}
|
||||
@Test public void Ws() {fxt.Test_parse(" { 'k0' : 'v0' } ", fxt.itm_nde_().Add_many(fxt.itm_kv_("k0", "v0")));}
|
||||
public static String Replace_apos_as_str(String v) {return String_.new_u8(Replace_apos(Bry_.new_u8(v)));}
|
||||
public static byte[] Replace_apos(byte[] v) {return Bry_.Replace(v, Byte_ascii.Apos, Byte_ascii.Quote);}
|
||||
}
|
||||
class Json_parser_fxt {
|
||||
public void Clear() {
|
||||
if (parser == null) {
|
||||
parser = new Json_parser();
|
||||
factory = parser.Factory();
|
||||
}
|
||||
} Json_parser parser; Json_factory factory; Bry_bfr tmp_bfr = Bry_bfr.reset_(255);
|
||||
Json_itm itm_int_(int v) {return Json_itm_tmp.new_int_(v);}
|
||||
Json_itm itm_str_(String v) {return Json_itm_tmp.new_str_(v);}
|
||||
public Json_itm_ary itm_ary_() {return factory.Ary(-1, -1);}
|
||||
public Json_nde itm_nde_() {return factory.Nde(-1);}
|
||||
public Json_itm_kv itm_kv_null_(String k) {return factory.Kv(itm_str_(k), factory.Null());}
|
||||
public Json_itm_kv itm_kv_(String k, String v) {return factory.Kv(itm_str_(k), itm_str_(v));}
|
||||
public Json_itm_kv itm_kv_(String k, int v) {return factory.Kv(itm_str_(k), itm_int_(v));}
|
||||
public Json_itm_kv itm_kv_(String k, boolean v) {return factory.Kv(itm_str_(k), v ? factory.Bool_y() : factory.Bool_n());}
|
||||
public Json_itm_kv itm_kv_dec_(String k, String v) {return factory.Kv(itm_str_(k), new Json_itm_tmp(Json_itm_.Tid_decimal, v));}
|
||||
public Json_itm_kv itm_kv_(String k, Json_nde v) {return factory.Kv(itm_str_(k), v);}
|
||||
public Json_itm_kv itm_kv_ary_int_(String k, int... v) {
|
||||
Json_itm_ary ary = factory.Ary(-1, -1);
|
||||
int len = v.length;
|
||||
for (int i = 0; i < len; i++)
|
||||
ary.Add(itm_int_(v[i]));
|
||||
return factory.Kv(itm_str_(k), ary);
|
||||
}
|
||||
public Json_itm_kv itm_kv_ary_str_(String k, String... v) {
|
||||
Json_itm_ary ary = factory.Ary(-1, -1);
|
||||
int len = v.length;
|
||||
for (int i = 0; i < len; i++)
|
||||
ary.Add(itm_str_(v[i]));
|
||||
return factory.Kv(itm_str_(k), ary);
|
||||
}
|
||||
public void Test_parse(String raw_str, Json_itm... expd_ary) {
|
||||
byte[] raw = Json_parser_tst.Replace_apos(Bry_.new_u8(raw_str));
|
||||
Json_doc doc = parser.Parse(raw);
|
||||
doc.Root().Print_as_json(tmp_bfr, 0);
|
||||
String actl = tmp_bfr.Xto_str_and_clear();
|
||||
String expd = Xto_str(raw, doc, expd_ary, 0, expd_ary.length);
|
||||
Tfds.Eq_str_lines(expd, actl, actl);
|
||||
}
|
||||
public void Test_parse_val0(String raw_str, Object expd) {
|
||||
byte[] raw = Json_parser_tst.Replace_apos(Bry_.new_u8(raw_str));
|
||||
Json_doc doc = parser.Parse(raw);
|
||||
Json_itm_kv kv = Json_itm_kv.cast_(doc.Root().Get_at(0)); // assume root has kv as first sub; EX: {"a":"b"}
|
||||
Object actl = kv.Val().Data(); // NOTE: Data_bry is escaped val; EX: a\"b has DataBry of a"b
|
||||
Tfds.Eq(expd, actl);
|
||||
}
|
||||
String Xto_str(byte[] raw, Json_doc doc, Json_itm[] ary, int bgn, int end) {
|
||||
for (int i = bgn; i < end; i++) {
|
||||
Json_itm itm = ary[i];
|
||||
itm.Print_as_json(tmp_bfr, 0);
|
||||
}
|
||||
return tmp_bfr.Xto_str_and_clear();
|
||||
}
|
||||
}
|
||||
154
400_xowa/src/gplx/core/json/Json_wtr.java
Normal file
154
400_xowa/src/gplx/core/json/Json_wtr.java
Normal file
@@ -0,0 +1,154 @@
|
||||
/*
|
||||
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.core.json; import gplx.*; import gplx.core.*;
|
||||
public class Json_wtr {
|
||||
private final Bry_bfr bfr = Bry_bfr.new_(255);
|
||||
private int indent;
|
||||
private boolean nde_itm_is_first;
|
||||
private boolean ary_itm_is_first;
|
||||
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 Bry_bfr Bfr() {return bfr;}
|
||||
public Json_wtr Clear() {
|
||||
indent = 0;
|
||||
nde_itm_is_first = ary_itm_is_first = true;
|
||||
return this;
|
||||
}
|
||||
public Json_wtr Doc_bgn() {return Add_grp_bgn(Sym_nde_bgn);}
|
||||
public Json_wtr Doc_end() {Add_grp_end(Bool_.Y, Sym_nde_end); return Add_nl();}
|
||||
public void Indent_(int v) {this.indent = v;}
|
||||
public Json_wtr Nde_bgn(String nde) {return Nde_bgn(Bry_.new_u8(nde));}
|
||||
public Json_wtr Nde_bgn(byte[] nde) {
|
||||
Add_indent_itm(nde_itm_is_first);
|
||||
Add_key(nde);
|
||||
Add_nl();
|
||||
return Add_grp_bgn(Sym_nde_bgn);
|
||||
}
|
||||
public Json_wtr Nde_end() {Add_grp_end(Bool_.Y, Sym_nde_end); return Add_nl();}
|
||||
public Json_wtr Ary_bgn(String nde) {
|
||||
Add_indent_itm(nde_itm_is_first);
|
||||
Add_key(Bry_.new_u8(nde));
|
||||
Add_nl();
|
||||
ary_itm_is_first = true;
|
||||
return Add_grp_bgn(Sym_ary_bgn);
|
||||
}
|
||||
public Json_wtr Ary_itm_str(String itm) {
|
||||
Add_indent_itm(ary_itm_is_first);
|
||||
Add_itm_bry(Bry_.new_u8(itm));
|
||||
Add_nl();
|
||||
ary_itm_is_first = false;
|
||||
return this;
|
||||
}
|
||||
public Json_wtr Ary_end() {Add_grp_end(Bool_.N, Sym_ary_end); return Add_nl();}
|
||||
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));}
|
||||
private Json_wtr Kv_raw(byte[] key, byte[] val) {
|
||||
Add_indent_itm(nde_itm_is_first);
|
||||
Add_key(key);
|
||||
bfr.Add(val);
|
||||
Add_nl();
|
||||
nde_itm_is_first = false;
|
||||
return this;
|
||||
}
|
||||
public Json_wtr Kv_str(String key, String val) {return Kv_bry(Bry_.new_u8(key), Bry_.new_u8(val));}
|
||||
public Json_wtr Kv_str(byte[] key, String val) {return Kv_bry(key, Bry_.new_u8(val));}
|
||||
public Json_wtr Kv_bry(String key, byte[] val) {return Kv_bry(Bry_.new_u8(key), val);}
|
||||
public Json_wtr Kv_bry(byte[] key, byte[] val) {
|
||||
Add_indent_itm(nde_itm_is_first);
|
||||
Add_key(key);
|
||||
Add_itm_bry(val);
|
||||
Add_nl();
|
||||
nde_itm_is_first = false;
|
||||
return this;
|
||||
}
|
||||
public Json_wtr Kv_bfr(String key, Bry_bfr val) {
|
||||
Add_indent_itm(nde_itm_is_first);
|
||||
Add_key(Bry_.new_u8(key));
|
||||
Add_itm_bry(val.Bfr(), 0, val.Len());
|
||||
Add_nl();
|
||||
nde_itm_is_first = false;
|
||||
val.Clear();
|
||||
return this;
|
||||
}
|
||||
private Json_wtr Add_grp_bgn(byte[] grp_sym) {
|
||||
Add_indent(0);
|
||||
bfr.Add(grp_sym);
|
||||
++indent;
|
||||
nde_itm_is_first = true;
|
||||
return this;
|
||||
}
|
||||
private Json_wtr Add_grp_end(boolean grp_is_nde, byte[] grp_sym) {
|
||||
--indent;
|
||||
if ((grp_is_nde && nde_itm_is_first) || (!grp_is_nde && ary_itm_is_first))
|
||||
Add_nl();
|
||||
Add_indent(0);
|
||||
nde_itm_is_first = false;
|
||||
bfr.Add(grp_sym);
|
||||
return this;
|
||||
}
|
||||
private Json_wtr Add_key(byte[] bry) {
|
||||
Add_itm_bry(bry);
|
||||
bfr.Add_byte_colon();
|
||||
return this;
|
||||
}
|
||||
private void Add_itm_bry(byte[] bry) {Add_itm_bry(bry, 0, bry.length);}
|
||||
private void Add_itm_bry(byte[] bry, int bgn, int end) {
|
||||
bfr.Add_byte(opt_quote_byte);
|
||||
for (int i = bgn; i < end; i++) {
|
||||
byte b = bry[i];
|
||||
switch (b) {
|
||||
case Byte_ascii.Backslash: bfr.Add_byte(Byte_ascii.Backslash).Add_byte(b); break; // "\" -> "\\"; needed else js will usurp \ as escape; EX: "\&" -> "&"; DATE:2014-06-24
|
||||
case Byte_ascii.Quote: bfr.Add_byte(Byte_ascii.Backslash).Add_byte(b); break;
|
||||
case Byte_ascii.Apos: bfr.Add_byte(b); break;
|
||||
case Byte_ascii.Nl: bfr.Add_byte_repeat(Byte_ascii.Backslash, 2).Add_byte(Byte_ascii.Ltr_n); break; // "\n" -> "\\n"
|
||||
case Byte_ascii.Cr: break;// skip
|
||||
default: bfr.Add_byte(b); break;
|
||||
}
|
||||
}
|
||||
bfr.Add_byte(opt_quote_byte);
|
||||
}
|
||||
private void Add_indent_itm(boolean v) {
|
||||
if (v) {
|
||||
bfr.Add_byte_space();
|
||||
}
|
||||
else {
|
||||
Add_indent(-1);
|
||||
bfr.Add(Sym_itm_spr);
|
||||
}
|
||||
}
|
||||
private Json_wtr Add_nl() {
|
||||
if (opt_ws)
|
||||
bfr.Add_byte_nl();
|
||||
return this;
|
||||
}
|
||||
private void Add_indent(int adj) {
|
||||
int level = indent + adj;
|
||||
if (opt_ws && level > 0)
|
||||
bfr.Add_byte_repeat(Byte_ascii.Space, level * 2);
|
||||
}
|
||||
private static final byte[]
|
||||
Sym_nde_bgn = Bry_.new_a7("{")
|
||||
, Sym_nde_end = Bry_.new_a7("}")
|
||||
, Sym_ary_bgn = Bry_.new_a7("[")
|
||||
, Sym_ary_end = Bry_.new_a7("]")
|
||||
, Sym_itm_spr = Bry_.new_a7(", ")
|
||||
;
|
||||
}
|
||||
94
400_xowa/src/gplx/core/json/Json_wtr_tst.java
Normal file
94
400_xowa/src/gplx/core/json/Json_wtr_tst.java
Normal file
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
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.core.json; import gplx.*; import gplx.core.*;
|
||||
import org.junit.*;
|
||||
public class Json_wtr_tst {
|
||||
@Before public void init() {fxt.Clear();} private final Json_wtr_fxt fxt = new Json_wtr_fxt();
|
||||
@Test public void Root() {
|
||||
fxt.Wtr().Doc_bgn().Doc_end();
|
||||
fxt.Test
|
||||
( "{"
|
||||
, "}"
|
||||
);
|
||||
}
|
||||
@Test public void Kv() {
|
||||
fxt.Wtr()
|
||||
.Doc_bgn()
|
||||
.Kv_str("k0", "v0")
|
||||
.Kv_str("k1", "v1")
|
||||
.Doc_end();
|
||||
fxt.Test
|
||||
( "{ 'k0':'v0'"
|
||||
, ", 'k1':'v1'"
|
||||
, "}"
|
||||
);
|
||||
}
|
||||
@Test public void Nde() {
|
||||
fxt.Wtr()
|
||||
.Doc_bgn()
|
||||
.Nde_bgn("s0")
|
||||
.Nde_bgn("s00")
|
||||
.Nde_end()
|
||||
.Nde_end()
|
||||
.Nde_bgn("s1")
|
||||
.Nde_bgn("s10")
|
||||
.Nde_end()
|
||||
.Nde_end()
|
||||
.Doc_end();
|
||||
fxt.Test
|
||||
( "{ 's0':"
|
||||
, " { 's00':"
|
||||
, " {"
|
||||
, " }"
|
||||
, " }"
|
||||
, ", 's1':"
|
||||
, " { 's10':"
|
||||
, " {"
|
||||
, " }"
|
||||
, " }"
|
||||
, "}"
|
||||
);
|
||||
}
|
||||
@Test public void Ary() {
|
||||
fxt.Wtr()
|
||||
.Doc_bgn()
|
||||
.Ary_bgn("a0")
|
||||
.Ary_itm_str("v0")
|
||||
.Ary_itm_str("v1")
|
||||
.Ary_end()
|
||||
.Doc_end();
|
||||
fxt.Test
|
||||
( "{ 'a0':"
|
||||
, " [ 'v0'"
|
||||
, " , 'v1'"
|
||||
, " ]"
|
||||
, "}"
|
||||
);
|
||||
}
|
||||
}
|
||||
class Json_wtr_fxt {
|
||||
private final Json_wtr wtr = new Json_wtr().Opt_quote_byte_(Byte_ascii.Apos);
|
||||
public void Clear() {wtr.Clear();}
|
||||
public Json_wtr Wtr() {return wtr;}
|
||||
public void Test(String... expd) {
|
||||
Tfds.Eq_ary_str
|
||||
( String_.Ary_add(expd, String_.Ary("")) // json_wtr always ends with "}\n"; rather than add "\n" to each test, just add it here
|
||||
, String_.SplitLines_nl(String_.new_u8(wtr.To_bry_and_clear()))
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -24,11 +24,11 @@ class Http_client_rdr__stream implements Http_client_rdr {
|
||||
}
|
||||
public String Read_line() {
|
||||
try {return br.readLine();}
|
||||
catch (IOException e) {throw Exc_.new_exc(e, "net", "Read_line failed");}
|
||||
catch (IOException e) {throw Err_.new_exc(e, "net", "Read_line failed");}
|
||||
}
|
||||
public byte[] Read_line_as_bry() {return Bry_.new_u8(Read_line());}
|
||||
public void Rls() {
|
||||
try {br.close();}
|
||||
catch (IOException e) {throw Exc_.new_exc(e, "net", "Rls failed");}
|
||||
catch (IOException e) {throw Err_.new_exc(e, "net", "Rls failed");}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,15 +26,15 @@ class Http_client_wtr__stream implements Http_client_wtr {
|
||||
}
|
||||
public void Write_bry(byte[] bry) {
|
||||
try {stream.write(bry);}
|
||||
catch (IOException e) {throw Exc_.new_exc(e, "net", "Write_bry failed");}
|
||||
catch (IOException e) {throw Err_.new_exc(e, "net", "Write_bry failed");}
|
||||
}
|
||||
public void Write_str(String s) {
|
||||
try {stream.writeBytes(s);}
|
||||
catch (Exception e) {throw Exc_.new_exc(e, "net", "Write_str failed");}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "net", "Write_str failed");}
|
||||
}
|
||||
public void Write_mid(byte[] bry, int bgn, int end) {
|
||||
try {stream.write(bry, bgn, end - bgn);}
|
||||
catch (IOException e) {throw Exc_.new_exc(e, "net", "Write_mid failed");}
|
||||
catch (IOException e) {throw Err_.new_exc(e, "net", "Write_mid failed");}
|
||||
}
|
||||
public void Write_stream(Io_stream_rdr stream_rdr) {
|
||||
synchronized (tmp_stream_bry) {
|
||||
@@ -48,6 +48,6 @@ class Http_client_wtr__stream implements Http_client_wtr {
|
||||
}
|
||||
public void Rls() {
|
||||
try {stream.close();}
|
||||
catch (IOException e) {throw Exc_.new_exc(e, "net", "Rls failed");}
|
||||
catch (IOException e) {throw Err_.new_exc(e, "net", "Rls failed");}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,13 +20,13 @@ public class Http_request_itm {
|
||||
public Http_request_itm(int type, byte[] url, byte[] protocol, byte[] host, byte[] user_agent
|
||||
, byte[] accept, byte[] accept_language, byte[] accept_encoding, boolean dnt, byte[] x_requested_with, byte[] cookie, byte[] referer
|
||||
, int content_length, byte[] content_type, byte[] content_type_boundary
|
||||
, byte[] connection, byte[] pragma, byte[] cache_control
|
||||
, byte[] connection, byte[] pragma, byte[] cache_control, byte[] origin
|
||||
, Http_post_data_hash post_data_hash
|
||||
) {
|
||||
this.type = type; this.url = url; this.protocol = protocol; this.host = host; this.user_agent = user_agent;
|
||||
this.accept = accept; this.accept_language = accept_language; this.accept_encoding = accept_encoding; this.dnt = dnt; this.x_requested_with = x_requested_with; this.cookie = cookie; this.referer = referer;
|
||||
this.content_length = content_length; this.content_type = content_type; this.content_type_boundary = content_type_boundary;
|
||||
this.connection = connection; this.pragma = pragma; this.cache_control = cache_control;
|
||||
this.connection = connection; this.pragma = pragma; this.cache_control = cache_control; this.origin = origin;
|
||||
this.post_data_hash = post_data_hash;
|
||||
}
|
||||
public int Type() {return type;} private final int type;
|
||||
@@ -47,32 +47,33 @@ public class Http_request_itm {
|
||||
public byte[] Connection() {return connection;} private final byte[] connection;
|
||||
public byte[] Pragma() {return pragma;} private final byte[] pragma;
|
||||
public byte[] Cache_control() {return cache_control;} private final byte[] cache_control;
|
||||
public byte[] Origin() {return origin;} private final byte[] origin;
|
||||
public Http_post_data_hash Post_data_hash() {return post_data_hash;} private final Http_post_data_hash post_data_hash;
|
||||
public String To_str(Bry_bfr bfr) {
|
||||
bfr .Add_kv_line("type" , type == Type_get ? "GET" : "POST")
|
||||
.Add_kv_line("url" , url)
|
||||
.Add_kv_line("protocol" , protocol)
|
||||
.Add_kv_line("host" , host)
|
||||
.Add_kv_line("user_agent" , user_agent)
|
||||
.Add_kv_line("accept" , accept)
|
||||
.Add_kv_line("accept_encoding" , accept_encoding)
|
||||
.Add_kv_line("dnt" , dnt)
|
||||
.Add_kv_line("x_requested_with" , x_requested_with)
|
||||
.Add_kv_line("cookie" , cookie)
|
||||
.Add_kv_line("referer" , referer)
|
||||
.Add_kv_line("content_length" , content_length)
|
||||
.Add_kv_line("content_type" , content_type)
|
||||
.Add_kv_line("content_type_boundary" , content_type_boundary)
|
||||
.Add_kv_line("connection" , connection)
|
||||
.Add_kv_line("pragma" , pragma)
|
||||
.Add_kv_line("cache_control" , cache_control)
|
||||
public String To_str(Bry_bfr bfr, boolean line) {
|
||||
bfr .Add_kv_dlm(line, "type" , type == Type_get ? "GET" : "POST")
|
||||
.Add_kv_dlm(line, "url" , url)
|
||||
.Add_kv_dlm(line, "protocol" , protocol)
|
||||
.Add_kv_dlm(line, "host" , host)
|
||||
.Add_kv_dlm(line, "user_agent" , user_agent)
|
||||
.Add_kv_dlm(line, "accept" , accept)
|
||||
.Add_kv_dlm(line, "accept_encoding" , accept_encoding)
|
||||
.Add_kv_dlm(line, "dnt" , dnt)
|
||||
.Add_kv_dlm(line, "x_requested_with" , x_requested_with)
|
||||
.Add_kv_dlm(line, "cookie" , cookie)
|
||||
.Add_kv_dlm(line, "referer" , referer)
|
||||
.Add_kv_dlm(line, "content_length" , content_length)
|
||||
.Add_kv_dlm(line, "content_type" , content_type)
|
||||
.Add_kv_dlm(line, "content_type_boundary" , content_type_boundary)
|
||||
.Add_kv_dlm(line, "connection" , connection)
|
||||
.Add_kv_dlm(line, "pragma" , pragma)
|
||||
.Add_kv_dlm(line, "cache_control" , cache_control)
|
||||
;
|
||||
if (post_data_hash != null) {
|
||||
int len = post_data_hash.Len();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Http_post_data_itm itm = post_data_hash.Get_at(i);
|
||||
bfr.Add_byte_repeat(Byte_ascii.Space, 2);
|
||||
bfr.Add_kv_line(String_.new_u8(itm.Key()), itm.Val());
|
||||
bfr.Add_kv_dlm(line, String_.new_u8(itm.Key()), itm.Val());
|
||||
}
|
||||
}
|
||||
return bfr.Xto_str_and_clear();
|
||||
|
||||
@@ -20,7 +20,7 @@ import gplx.core.primitives.*; import gplx.core.btries.*;
|
||||
public class Http_request_parser {
|
||||
private boolean dnt;
|
||||
private int type, content_length;
|
||||
private byte[] url, protocol, host, user_agent, accept, accept_language, accept_encoding, x_requested_with, cookie, referer, content_type, content_type_boundary, connection, pragma, cache_control;
|
||||
private byte[] url, protocol, host, user_agent, accept, accept_language, accept_encoding, x_requested_with, cookie, referer, content_type, content_type_boundary, connection, pragma, cache_control, origin;
|
||||
private Http_post_data_hash post_data_hash;
|
||||
private final Bry_bfr tmp_bfr = Bry_bfr.new_(255);
|
||||
private final Http_server_wtr server_wtr; private final boolean log;
|
||||
@@ -29,89 +29,92 @@ public class Http_request_parser {
|
||||
this.dnt = false;
|
||||
this.type = this.content_length = 0;
|
||||
this.url = this.protocol = this.host = this.user_agent = this.accept = this.accept_language = this.accept_encoding = this.x_requested_with = this.cookie
|
||||
= this.referer = this.content_type = this.content_type_boundary = this.connection = this.pragma = this.cache_control = null;
|
||||
= this.referer = this.content_type = this.content_type_boundary = this.connection = this.pragma = this.cache_control = this.origin = null;
|
||||
this.post_data_hash = null;
|
||||
}
|
||||
public Http_request_itm Parse(Http_client_rdr rdr) {
|
||||
this.Clear();
|
||||
boolean reading_post_data = false; boolean post_nl_seen = false;
|
||||
while (true) {
|
||||
String line_str = rdr.Read_line(); if (line_str == null) break; // needed for TEST
|
||||
if (log) server_wtr.Write_str_w_nl(line_str);
|
||||
byte[] line = Bry_.new_u8(line_str);
|
||||
int line_len = line.length;
|
||||
if (line_len == 0) {
|
||||
switch (type) {
|
||||
case Http_request_itm.Type_get: break;
|
||||
case Http_request_itm.Type_post:
|
||||
if (reading_post_data || post_nl_seen) throw Exc_.new_("http.request.parser;invalid new line during post", "request", To_str());
|
||||
post_nl_seen = true; // only allow one \n per POST
|
||||
continue; // ignore line and get next
|
||||
default: throw Exc_.new_unimplemented();
|
||||
synchronized (tmp_bfr) {
|
||||
this.Clear();
|
||||
boolean reading_post_data = false; boolean post_nl_seen = false;
|
||||
while (true) {
|
||||
String line_str = rdr.Read_line(); if (line_str == null) break; // needed for TEST
|
||||
if (log) server_wtr.Write_str_w_nl(line_str);
|
||||
byte[] line = Bry_.new_u8(line_str);
|
||||
int line_len = line.length;
|
||||
if (line_len == 0) {
|
||||
switch (type) {
|
||||
case Http_request_itm.Type_get: break;
|
||||
case Http_request_itm.Type_post:
|
||||
if (reading_post_data || post_nl_seen) throw Err_.new_wo_type("http.request.parser;invalid new line during post", "request", To_str());
|
||||
post_nl_seen = true; // only allow one \n per POST
|
||||
continue; // ignore line and get next
|
||||
default: throw Err_.new_unimplemented();
|
||||
}
|
||||
break; // only GET will reach this line; GET requests always end with blank line; stop;
|
||||
}
|
||||
break; // only GET will reach this line; GET requests always end with blank line; stop;
|
||||
}
|
||||
if (content_type_boundary != null && Bry_.Has_at_bgn(line, content_type_boundary)) {
|
||||
while (true) {
|
||||
if (Bry_.Has_at_end(line, Tkn_content_type_boundary_end)) break; // last form_data pair will end with "--"; stop
|
||||
line = Parse_content_type_boundary(rdr);
|
||||
if (content_type_boundary != null && Bry_.Has_at_bgn(line, content_type_boundary)) {
|
||||
while (true) {
|
||||
if (Bry_.Has_at_end(line, Tkn_content_type_boundary_end)) break; // last form_data pair will end with "--"; stop
|
||||
line = Parse_content_type_boundary(rdr);
|
||||
}
|
||||
break; // assume form_data ends POST request
|
||||
}
|
||||
Object o = trie.Match_bgn(line, 0, line_len);
|
||||
if (o == null) {
|
||||
server_wtr.Write_str_w_nl(String_.Format("http.request.parser; unknown line; line={0} request={1}", line_str, To_str()));
|
||||
continue;
|
||||
}
|
||||
int val_bgn = Bry_finder.Find_fwd_while_ws(line, trie.Match_pos(), line_len); // skip ws after key; EX: "Host: "
|
||||
int tid = ((Int_obj_val)o).Val();
|
||||
switch (tid) {
|
||||
case Tid_get:
|
||||
case Tid_post: Parse_type(tid, val_bgn, line, line_len); break;
|
||||
case Tid_host: this.host = Bry_.Mid(line, val_bgn, line_len); break;
|
||||
case Tid_user_agent: this.user_agent = Bry_.Mid(line, val_bgn, line_len); break;
|
||||
case Tid_accept: this.accept = Bry_.Mid(line, val_bgn, line_len); break;
|
||||
case Tid_accept_language: this.accept_language = Bry_.Mid(line, val_bgn, line_len); break;
|
||||
case Tid_accept_encoding: this.accept_encoding = Bry_.Mid(line, val_bgn, line_len); break;
|
||||
case Tid_dnt: this.dnt = line[val_bgn] == Byte_ascii.Num_1; break;
|
||||
case Tid_x_requested_with: this.x_requested_with = Bry_.Mid(line, val_bgn, line_len); break;
|
||||
case Tid_cookie: this.cookie = Bry_.Mid(line, val_bgn, line_len); break;
|
||||
case Tid_referer: this.referer = Bry_.Mid(line, val_bgn, line_len); break;
|
||||
case Tid_content_length: this.content_length = Bry_.Xto_int_or(line, val_bgn, line_len, -1); break;
|
||||
case Tid_content_type: Parse_content_type(val_bgn, line, line_len); break;
|
||||
case Tid_connection: this.connection = Bry_.Mid(line, val_bgn, line_len); break;
|
||||
case Tid_pragma: this.pragma = Bry_.Mid(line, val_bgn, line_len); break;
|
||||
case Tid_cache_control: this.cache_control = Bry_.Mid(line, val_bgn, line_len); break;
|
||||
case Tid_origin: this.origin = Bry_.Mid(line, val_bgn, line_len); break;
|
||||
default: throw Err_.new_unhandled(tid);
|
||||
}
|
||||
break; // assume form_data ends POST request
|
||||
}
|
||||
Object o = trie.Match_bgn(line, 0, line_len);
|
||||
if (o == null) {
|
||||
server_wtr.Write_str_w_nl(String_.Format("http.request.parser; unknown line; line={0} request={1}", line_str, To_str()));
|
||||
continue;
|
||||
}
|
||||
int val_bgn = Bry_finder.Find_fwd_while_ws(line, trie.Match_pos(), line_len); // skip ws after key; EX: "Host: "
|
||||
int tid = ((Int_obj_val)o).Val();
|
||||
switch (tid) {
|
||||
case Tid_get:
|
||||
case Tid_post: Parse_type(tid, val_bgn, line, line_len); break;
|
||||
case Tid_host: this.host = Bry_.Mid(line, val_bgn, line_len); break;
|
||||
case Tid_user_agent: this.user_agent = Bry_.Mid(line, val_bgn, line_len); break;
|
||||
case Tid_accept: this.accept = Bry_.Mid(line, val_bgn, line_len); break;
|
||||
case Tid_accept_language: this.accept_language = Bry_.Mid(line, val_bgn, line_len); break;
|
||||
case Tid_accept_encoding: this.accept_encoding = Bry_.Mid(line, val_bgn, line_len); break;
|
||||
case Tid_dnt: this.dnt = line[val_bgn] == Byte_ascii.Num_1; break;
|
||||
case Tid_x_requested_with: this.x_requested_with = Bry_.Mid(line, val_bgn, line_len); break;
|
||||
case Tid_cookie: this.cookie = Bry_.Mid(line, val_bgn, line_len); break;
|
||||
case Tid_referer: this.referer = Bry_.Mid(line, val_bgn, line_len); break;
|
||||
case Tid_content_length: this.content_length = Bry_.Xto_int_or(line, val_bgn, line_len, -1); break;
|
||||
case Tid_content_type: Parse_content_type(val_bgn, line, line_len); break;
|
||||
case Tid_connection: this.connection = Bry_.Mid(line, val_bgn, line_len); break;
|
||||
case Tid_pragma: this.pragma = Bry_.Mid(line, val_bgn, line_len); break;
|
||||
case Tid_cache_control: this.cache_control = Bry_.Mid(line, val_bgn, line_len); break;
|
||||
default: throw Exc_.new_unhandled(tid);
|
||||
}
|
||||
return Make_request_itm();
|
||||
}
|
||||
return Make_request_itm();
|
||||
}
|
||||
private void Parse_type(int tid, int val_bgn, byte[] line, int line_len) { // EX: "POST /xowa-cmd:exec_as_json HTTP/1.1"
|
||||
int url_end = Bry_finder.Find_bwd(line, Byte_ascii.Space, line_len); if (url_end == Bry_finder.Not_found) throw Exc_.new_("invalid protocol", "line", line, "request", To_str());
|
||||
int url_end = Bry_finder.Find_bwd(line, Byte_ascii.Space, line_len); if (url_end == Bry_finder.Not_found) throw Err_.new_wo_type("invalid protocol", "line", line, "request", To_str());
|
||||
switch (tid) {
|
||||
case Tid_get : this.type = Http_request_itm.Type_get; break;
|
||||
case Tid_post : this.type = Http_request_itm.Type_post; break;
|
||||
default : throw Exc_.new_unimplemented();
|
||||
default : throw Err_.new_unimplemented();
|
||||
}
|
||||
this.url = Bry_.Mid(line, val_bgn, url_end);
|
||||
this.protocol = Bry_.Mid(line, url_end + 1, line_len);
|
||||
}
|
||||
private void Parse_content_type(int val_bgn, byte[] line, int line_len) { // EX: Content-Type: multipart/form-data; boundary=---------------------------72432484930026
|
||||
int boundary_bgn = Bry_finder.Find_fwd(line, Tkn_boundary, val_bgn, line_len); if (boundary_bgn == Bry_finder.Not_found) throw Exc_.new_("invalid content_type", "line", line, "request", To_str());
|
||||
int boundary_bgn = Bry_finder.Find_fwd(line, Tkn_boundary, val_bgn, line_len); if (boundary_bgn == Bry_finder.Not_found) throw Err_.new_wo_type("invalid content_type", "line", line, "request", To_str());
|
||||
int content_type_end = Bry_finder.Find_bwd(line, Byte_ascii.Semic, boundary_bgn);
|
||||
this.content_type = Bry_.Mid(line, val_bgn, content_type_end);
|
||||
this.content_type_boundary = Bry_.Add(Tkn_content_type_boundary_end, Bry_.Mid(line, boundary_bgn += Tkn_boundary.length, line_len));
|
||||
}
|
||||
private Http_request_itm Make_request_itm() {
|
||||
return new Http_request_itm(type, url, protocol, host, user_agent, accept, accept_language, accept_encoding, dnt, x_requested_with, cookie, referer, content_length, content_type, content_type_boundary, connection, pragma, cache_control, post_data_hash);
|
||||
return new Http_request_itm(type, url, protocol, host, user_agent, accept, accept_language, accept_encoding, dnt, x_requested_with, cookie, referer, content_length, content_type, content_type_boundary, connection, pragma, cache_control, origin, post_data_hash);
|
||||
}
|
||||
private byte[] Parse_content_type_boundary(Http_client_rdr rdr) {
|
||||
if (post_data_hash == null) post_data_hash = new Http_post_data_hash();
|
||||
byte[] line = Bry_.new_u8(rdr.Read_line()); // cur line is already known to be content_type_boundary; skip it
|
||||
byte[] key = Parse_post_data_name(line);
|
||||
String line_str = rdr.Read_line(); // blank-line
|
||||
if (String_.Len_gt_0(line_str)) {throw Exc_.new_("http.request.parser; blank_line should follow content_type_boundary", "request", To_str());}
|
||||
if (String_.Len_gt_0(line_str)) {throw Err_.new_wo_type("http.request.parser; blank_line should follow content_type_boundary", "request", To_str());}
|
||||
while (true) {
|
||||
line = Bry_.new_u8(rdr.Read_line());
|
||||
if (Bry_.Has_at_bgn(line, content_type_boundary)) break;
|
||||
@@ -128,7 +131,7 @@ public class Http_request_parser {
|
||||
pos = Assert_tkn(line, pos, line_len, Tkn_name);
|
||||
int name_end = line_len;
|
||||
if (line[pos] == Byte_ascii.Quote) {
|
||||
if (line[name_end - 1] != Byte_ascii.Quote) throw Exc_.new_("http.request.parser; invalid form at end", "line", line, "request", To_str());
|
||||
if (line[name_end - 1] != Byte_ascii.Quote) throw Err_.new_wo_type("http.request.parser; invalid form at end", "line", line, "request", To_str());
|
||||
++pos;
|
||||
--name_end;
|
||||
}
|
||||
@@ -136,13 +139,13 @@ public class Http_request_parser {
|
||||
}
|
||||
private int Assert_tkn(byte[] src, int src_pos, int src_len, byte[] tkn) {
|
||||
int tkn_len = tkn.length;
|
||||
if (!Bry_.Match(src, src_pos, src_pos + tkn_len, tkn)) throw Exc_.new_("http.request.parser; invalid form_data line", "tkn", tkn, "line", src, "request", To_str());
|
||||
if (!Bry_.Match(src, src_pos, src_pos + tkn_len, tkn)) throw Err_.new_wo_type("http.request.parser; invalid form_data line", "tkn", tkn, "line", src, "request", To_str());
|
||||
int rv = src_pos += tkn_len;
|
||||
return Bry_finder.Find_fwd_while_ws(src, rv, src_len);
|
||||
}
|
||||
private String To_str() {return Make_request_itm().To_str(tmp_bfr);}
|
||||
private String To_str() {return Make_request_itm().To_str(tmp_bfr, Bool_.N);}
|
||||
private static final int Tid_get = 1, Tid_post = 2, Tid_host = 3, Tid_user_agent = 4, Tid_accept = 5, Tid_accept_language = 6, Tid_accept_encoding = 7, Tid_dnt = 8
|
||||
, Tid_x_requested_with = 9, Tid_cookie = 10, Tid_referer = 11, Tid_content_length = 12, Tid_content_type = 13, Tid_connection = 14, Tid_pragma = 15, Tid_cache_control = 16;
|
||||
, Tid_x_requested_with = 9, Tid_cookie = 10, Tid_referer = 11, Tid_content_length = 12, Tid_content_type = 13, Tid_connection = 14, Tid_pragma = 15, Tid_cache_control = 16, Tid_origin = 17;
|
||||
private static final Btrie_slim_mgr trie = Btrie_slim_mgr.ci_ascii_()
|
||||
.Add_str_int("GET" , Tid_get)
|
||||
.Add_str_int("POST" , Tid_post)
|
||||
@@ -160,6 +163,7 @@ public class Http_request_parser {
|
||||
.Add_str_int("Connection:" , Tid_connection)
|
||||
.Add_str_int("Pragma:" , Tid_pragma)
|
||||
.Add_str_int("Cache-Control:" , Tid_cache_control)
|
||||
.Add_str_int("Origin:" , Tid_origin)
|
||||
;
|
||||
private static final byte[] Tkn_boundary = Bry_.new_a7("boundary="), Tkn_content_type_boundary_end = Bry_.new_a7("--")
|
||||
, Tkn_content_disposition = Bry_.new_a7("Content-Disposition:"), Tkn_form_data = Bry_.new_a7("form-data;")
|
||||
|
||||
@@ -16,6 +16,7 @@ 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.core.net; import gplx.*; import gplx.core.*;
|
||||
import gplx.core.consoles.*;
|
||||
class Http_server_wtr__console implements Http_server_wtr {
|
||||
public void Write_str_w_nl(String s) {ConsoleAdp._.WriteLine(s);}
|
||||
public void Write_str_w_nl(String s) {Console_adp__sys.I.Write_str_w_nl(s);}
|
||||
}
|
||||
|
||||
24
400_xowa/src/gplx/core/net/Local_host_.java
Normal file
24
400_xowa/src/gplx/core/net/Local_host_.java
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
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.core.net; import gplx.*; import gplx.core.*;
|
||||
public class Local_host_ {
|
||||
public static String Ip_address() {
|
||||
try {return java.net.InetAddress.getLocalHost().getHostAddress();}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "net", "ip_address failed");}
|
||||
}
|
||||
}
|
||||
23
400_xowa/src/gplx/core/net/Server_socket_adp.java
Normal file
23
400_xowa/src/gplx/core/net/Server_socket_adp.java
Normal file
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
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.core.net; import gplx.*; import gplx.core.*;
|
||||
public interface Server_socket_adp {
|
||||
Server_socket_adp Ctor(int port);
|
||||
Socket_adp Accept();
|
||||
void Rls();
|
||||
}
|
||||
39
400_xowa/src/gplx/core/net/Server_socket_adp__base.java
Normal file
39
400_xowa/src/gplx/core/net/Server_socket_adp__base.java
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
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.core.net; import gplx.*; import gplx.core.*;
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
public class Server_socket_adp__base implements Server_socket_adp {
|
||||
private ServerSocket server_socket;
|
||||
public Server_socket_adp Ctor(int port) {
|
||||
try {this.server_socket = new ServerSocket(port);}
|
||||
catch (IOException e) {throw Err_.new_exc(e, "net", "Get_input_stream failed");}
|
||||
return this;
|
||||
}
|
||||
public Socket_adp Accept() {
|
||||
Socket client_socket = null;
|
||||
try {client_socket = server_socket.accept();}
|
||||
catch (IOException e) {throw Err_.new_exc(e, "net", "Get_input_stream failed");}
|
||||
return new Socket_adp__base(client_socket);
|
||||
}
|
||||
public void Rls() {
|
||||
try {server_socket.close();}
|
||||
catch (IOException e) {throw Err_.new_exc(e, "net", "Rls failed");}
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.net; import gplx.*; import gplx.core.*;
|
||||
public interface Socket_adp {
|
||||
String Ip_address();
|
||||
Object Get_input_stream();
|
||||
Object Get_output_stream();
|
||||
void Rls();
|
||||
|
||||
@@ -21,16 +21,19 @@ import java.net.*;
|
||||
public class Socket_adp__base implements Socket_adp {
|
||||
private final Socket socket;
|
||||
public Socket_adp__base(Socket socket) {this.socket = socket;}
|
||||
public Object Get_input_stream() {
|
||||
public String Ip_address() {
|
||||
return socket.getRemoteSocketAddress().toString();
|
||||
}
|
||||
public Object Get_input_stream() {
|
||||
try {return socket.getInputStream();}
|
||||
catch (IOException e) {throw Exc_.new_exc(e, "net", "Get_input_stream failed");}
|
||||
catch (IOException e) {throw Err_.new_exc(e, "net", "Get_input_stream failed");}
|
||||
}
|
||||
public Object Get_output_stream() {
|
||||
try {return socket.getOutputStream();}
|
||||
catch (IOException e) {throw Exc_.new_exc(e, "net", "Get_output_stream failed");}
|
||||
catch (IOException e) {throw Err_.new_exc(e, "net", "Get_output_stream failed");}
|
||||
}
|
||||
public void Rls() {
|
||||
try {socket.close();}
|
||||
catch (IOException e) {throw Exc_.new_exc(e, "net", "Rls failed");}
|
||||
catch (IOException e) {throw Err_.new_exc(e, "net", "Rls failed");}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,9 +26,9 @@ public class Gfo_pattern_itm_ {
|
||||
int pos = 0;
|
||||
while (true) {
|
||||
boolean last = pos == raw_len;
|
||||
byte b = last ? Byte_ascii.Nil : raw[pos];
|
||||
byte b = last ? Byte_ascii.Null : raw[pos];
|
||||
switch (b) {
|
||||
case Byte_ascii.Nil:
|
||||
case Byte_ascii.Null:
|
||||
if (itm != null) {itm.Compile(raw, itm_bgn, pos); itm = null; itm_bgn = -1;}
|
||||
break;
|
||||
case Byte_ascii.Star:
|
||||
|
||||
@@ -38,7 +38,7 @@ public class Gfo_thread_cmd_download implements Gfo_thread_cmd {
|
||||
case Gfui_dlg_msg_.Btn_yes: Io_mgr.I.DeleteFil(trg); break;
|
||||
case Gfui_dlg_msg_.Btn_no: return Gfo_thread_cmd_.Init_cancel_step;
|
||||
case Gfui_dlg_msg_.Btn_cancel: return Gfo_thread_cmd_.Init_cancel_all;
|
||||
default: throw Exc_.new_unhandled(rslt);
|
||||
default: throw Err_.new_unhandled(rslt);
|
||||
}
|
||||
}
|
||||
usr_dlg.Prog_many(GRP_KEY, "download.bgn", "contacting web server: '~{0}'", src); // update progress; some servers (like WMF dump servers) are slow to respond
|
||||
|
||||
@@ -82,10 +82,10 @@ public class Gfo_thread_cmd_unzip implements Gfo_thread_cmd {
|
||||
case Term_cmd_for_src_noop: break;
|
||||
case Term_cmd_for_src_delete: Io_mgr.I.DeleteFil(src); break;
|
||||
case Term_cmd_for_src_move:
|
||||
if (term_cmd_for_src_url == Io_url_.Empty) throw Exc_.new_("move specified, but no url");
|
||||
if (term_cmd_for_src_url == Io_url_.Empty) throw Err_.new_wo_type("move specified, but no url");
|
||||
Io_mgr.I.MoveFil_args(src, term_cmd_for_src_url, true).Exec();
|
||||
break;
|
||||
default: throw Exc_.new_unhandled(term_cmd_for_src);
|
||||
default: throw Err_.new_unhandled(term_cmd_for_src);
|
||||
}
|
||||
usr_dlg.Prog_many(GRP_KEY, "done", "");
|
||||
return true;
|
||||
@@ -108,7 +108,7 @@ public class Gfo_thread_cmd_unzip implements Gfo_thread_cmd {
|
||||
if (String_.Eq(s, "noop")) return Term_cmd_for_src_noop;
|
||||
else if (String_.Eq(s, "delete")) return Term_cmd_for_src_delete;
|
||||
else if (String_.Eq(s, "move")) return Term_cmd_for_src_move;
|
||||
else throw Exc_.new_unhandled(s);
|
||||
else throw Err_.new_unhandled(s);
|
||||
}
|
||||
static final String GRP_KEY = "xowa.thread.file.unzip";
|
||||
public static final String KEY = "file.unzip";
|
||||
|
||||
@@ -46,7 +46,7 @@ public class Gfo_thread_pool implements GfoInvkAble {
|
||||
private void Run_wkr(Gfo_thread_wkr wkr) {
|
||||
try {wkr.Exec();}
|
||||
catch (Exception e) {
|
||||
usr_dlg.Warn_many("", "", "uncaught exception while running thread; name=~{0} err=~{1}", wkr.Name(), Err_.Message_gplx_brief(e));
|
||||
usr_dlg.Warn_many("", "", "uncaught exception while running thread; name=~{0} err=~{1}", wkr.Name(), Err_.Message_gplx_full(e));
|
||||
}
|
||||
finally {
|
||||
if (wkr.Resume())
|
||||
|
||||
Reference in New Issue
Block a user