mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
'v3.6.3.1'
This commit is contained in:
@@ -17,11 +17,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.brys; import gplx.*; import gplx.core.*;
|
||||
public class Bit_heap_wtr {
|
||||
private final gplx.xowa.htmls.core.hzips.Xoh_hzip_int hzip_int = new gplx.xowa.htmls.core.hzips.Xoh_hzip_int().Mode_is_b256_(true);
|
||||
private final Bry_bfr hzip_int_bfr = Bry_bfr.reset_(5);
|
||||
private final gplx.xowa.htmls.core.hzips.Xoh_hzip_int hzip_int = new gplx.xowa.htmls.core.hzips.Xoh_hzip_int().Mode_is_b256_(true);
|
||||
private final Bry_bfr hzip_int_bfr = Bry_bfr_.Reset(5);
|
||||
public int Cur() {return cur;} private int cur;
|
||||
public int Cur_bits() {return cur_bits;} private int cur_bits;
|
||||
public Bry_bfr Heap() {return heap;} private final Bry_bfr heap = Bry_bfr.new_();
|
||||
public Bry_bfr Heap() {return heap;} private final Bry_bfr heap = Bry_bfr_.New();
|
||||
public void Clear() {heap.Clear(); cur = 0; cur_bits = 0;}
|
||||
public void Add_bool(boolean v) {
|
||||
if (v)
|
||||
@@ -67,5 +67,5 @@ public class Bit_heap_wtr {
|
||||
hzip_int_bfr.Clear();
|
||||
}
|
||||
public void Save(Bry_bfr bfr) {}
|
||||
public static final int[] Pow_ary = new int[] {1, 2, 4, 8, 16, 32, 64, 128, 256};
|
||||
public static final int[] Pow_ary = new int[] {1, 2, 4, 8, 16, 32, 64, 128, 256};
|
||||
}
|
||||
|
||||
108
400_xowa/src/gplx/core/brys/evals/Bry_eval_mgr.java
Normal file
108
400_xowa/src/gplx/core/brys/evals/Bry_eval_mgr.java
Normal file
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
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.brys.evals; import gplx.*; import gplx.core.*; import gplx.core.brys.*;
|
||||
public class Bry_eval_mgr {
|
||||
private final Hash_adp_bry hash = Hash_adp_bry.cs();
|
||||
private final byte sym_escape, sym_key_end, sym_grp_bgn, sym_grp_end;
|
||||
public Bry_eval_mgr(byte sym_escape, byte sym_key_end, byte sym_grp_bgn, byte sym_grp_end) {
|
||||
this.sym_escape = sym_escape;
|
||||
this.sym_key_end = sym_key_end;
|
||||
this.sym_grp_bgn = sym_grp_bgn;
|
||||
this.sym_grp_end = sym_grp_end;
|
||||
}
|
||||
public Bry_eval_mgr Add_many(Bry_eval_wkr... ary) {
|
||||
for (Bry_eval_wkr itm : ary)
|
||||
hash.Add(Bry_.new_u8(itm.Key()), itm);
|
||||
return this;
|
||||
}
|
||||
public byte[] Eval(byte[] src) {
|
||||
Bry_eval_rslt rv = Eval_txt(0, src, 0, src.length);
|
||||
return rv == null ? src : rv.Bry();
|
||||
}
|
||||
private Bry_eval_rslt Eval_txt(int depth, byte[] src, int src_bgn, int src_end) {
|
||||
Bry_bfr cur_bfr = null;
|
||||
int cur_pos = src_bgn;
|
||||
while (true) {
|
||||
if (cur_pos == src_end) break;
|
||||
byte cur_byte = src[cur_pos];
|
||||
// cur_byte is ~
|
||||
if (cur_byte == sym_escape) {
|
||||
// create cur_bfr
|
||||
if (cur_bfr == null) {cur_bfr = Bry_bfr_.New(); cur_bfr.Add_mid(src, src_bgn, cur_pos);}
|
||||
|
||||
// eval nxt_byte
|
||||
int nxt_pos = cur_pos + 1;
|
||||
if (nxt_pos == src_end) throw Err_.new_wo_type("bry_eval:escape at eos", "src", src);
|
||||
byte nxt_byte = src[nxt_pos];
|
||||
if (nxt_byte == sym_grp_bgn) { // ~{key|} -> eval;
|
||||
Bry_eval_rslt sub = Eval_txt(depth + 1, src, nxt_pos + 1, src_end); // get "}"
|
||||
cur_bfr.Add(Eval_grp(sub.Bry()));
|
||||
cur_pos = sub.Pos();
|
||||
continue;
|
||||
}
|
||||
else if (nxt_byte == sym_escape) {
|
||||
cur_bfr.Add_byte(nxt_byte);
|
||||
cur_pos = nxt_pos + 1;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else if (depth > 0 && cur_byte == sym_grp_end) {
|
||||
return cur_bfr == null
|
||||
? new Bry_eval_rslt(Bry_.Mid(src, src_bgn, cur_pos), cur_pos + 1)
|
||||
: new Bry_eval_rslt(cur_bfr.To_bry_and_clear(), cur_pos + 1);
|
||||
}
|
||||
if (cur_bfr != null) cur_bfr.Add_byte(cur_byte);
|
||||
++cur_pos;
|
||||
}
|
||||
return cur_bfr == null ? null : new Bry_eval_rslt(cur_bfr.To_bry_and_clear(), src_end);
|
||||
}
|
||||
private byte[] Eval_grp(byte[] src) {
|
||||
// search for "|" or "}"
|
||||
boolean args_is_empty = true;
|
||||
int src_end = src.length;
|
||||
int key_bgn = 0, key_end = src_end;
|
||||
for (int i = key_bgn; i < src_end; ++i) {
|
||||
int key_end_byte = src[i];
|
||||
if (key_end_byte == sym_key_end) {
|
||||
key_end = i;
|
||||
args_is_empty = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// get wkr
|
||||
Bry_eval_wkr wkr = (Bry_eval_wkr)hash.Get_by_mid(src, key_bgn, key_end);
|
||||
if (wkr == null) throw Err_.new_wo_type("bry_eval:key not found", "src", src);
|
||||
Bry_bfr bfr = Bry_bfr_.New();
|
||||
if (args_is_empty) {
|
||||
wkr.Resolve(bfr, src, -1, -1);
|
||||
}
|
||||
else {
|
||||
wkr.Resolve(bfr, src, key_end + 1, src_end);
|
||||
}
|
||||
return bfr.To_bry_and_clear();
|
||||
}
|
||||
public static Bry_eval_mgr Dflt() {return new Bry_eval_mgr(Byte_ascii.Tilde, Byte_ascii.Pipe, Byte_ascii.Curly_bgn, Byte_ascii.Curly_end);}
|
||||
}
|
||||
class Bry_eval_rslt {
|
||||
public Bry_eval_rslt(byte[] bry, int pos) {
|
||||
this.bry = bry; this.pos = pos;
|
||||
}
|
||||
public byte[] Bry() {return bry;} private final byte[] bry;
|
||||
public int Pos() {return pos;} private final int pos;
|
||||
}
|
||||
52
400_xowa/src/gplx/core/brys/evals/Bry_eval_mgr__tst.java
Normal file
52
400_xowa/src/gplx/core/brys/evals/Bry_eval_mgr__tst.java
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
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.brys.evals; import gplx.*; import gplx.core.*; import gplx.core.brys.*;
|
||||
import org.junit.*; import gplx.core.tests.*;
|
||||
public class Bry_eval_mgr__tst {
|
||||
private final Bry_eval_mgr__fxt fxt = new Bry_eval_mgr__fxt();
|
||||
@Test public void Text() {fxt.Test__eval("abc" , "abc");}
|
||||
@Test public void Args_0() {fxt.Test__eval("abc~{test}xyz" , "abctestxyz");}
|
||||
@Test public void Args_n() {fxt.Test__eval("abc~{concat|d|e|f}xyz" , "abcdefxyz");}
|
||||
@Test public void Recur_1() {fxt.Test__eval("abc~{~{test}}xyz" , "abctestxyz");}
|
||||
@Test public void Recur_2() {fxt.Test__eval("abc~{t~{concat|e|s}t}xyz" , "abctestxyz");}
|
||||
@Test public void Grp_end() {fxt.Test__eval("a}b" , "a}b");}
|
||||
@Test public void Escape() {fxt.Test__eval("a~~b" , "a~b");}
|
||||
// @Test public void Eos() {fxt.Test__eval("a~" , "a~");}
|
||||
}
|
||||
class Bry_eval_mgr__fxt {
|
||||
private final Bry_eval_mgr mgr = Bry_eval_mgr.Dflt().Add_many(new Bry_eval_wkr__test(), new Bry_eval_wkr__concat());
|
||||
public Bry_eval_mgr__fxt Test__eval(String raw, String expd) {
|
||||
Gftest.Eq__bry(Bry_.new_u8(expd), mgr.Eval(Bry_.new_u8(raw)));
|
||||
return this;
|
||||
}
|
||||
}
|
||||
class Bry_eval_wkr__test implements Bry_eval_wkr {
|
||||
public String Key() {return "test";}
|
||||
public void Resolve(Bry_bfr rv, byte[] src, int src_bgn, int src_end) {
|
||||
rv.Add_str_a7("test");
|
||||
}
|
||||
}
|
||||
class Bry_eval_wkr__concat implements Bry_eval_wkr {
|
||||
public String Key() {return "concat";}
|
||||
public void Resolve(Bry_bfr rv, byte[] src, int src_bgn, int src_end) {
|
||||
byte[][] ary = Bry_split_.Split(src, src_bgn, src_end, Byte_ascii.Pipe, false);
|
||||
for (byte[] itm : ary) {
|
||||
rv.Add(itm);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,13 +15,8 @@ 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.gfobjs; import gplx.*; import gplx.core.*;
|
||||
public class Gfobj_ary_nde implements Gfobj_ary {
|
||||
public Gfobj_ary_nde(Gfobj_nde[] ary) {this.ary = ary;}
|
||||
public byte Grp_tid() {return Gfobj_grp_.Grp_tid__ary;}
|
||||
public byte Ary_tid() {return Gfobj_ary_.Ary_tid__nde;}
|
||||
public int Len() {return ary.length;}
|
||||
public Object Get_at(int i) {return ary[i];}
|
||||
public Gfobj_nde[] Ary_nde() {return ary;} private Gfobj_nde[] ary;
|
||||
public void Ary_nde_(Gfobj_nde[] v) {this.ary = v;}
|
||||
package gplx.core.brys.evals; import gplx.*; import gplx.core.*; import gplx.core.brys.*;
|
||||
public interface Bry_eval_wkr {
|
||||
String Key();
|
||||
void Resolve(Bry_bfr rv, byte[] src, int args_bgn, int args_end);
|
||||
}
|
||||
@@ -77,7 +77,7 @@ public class Gfo_cache_mgr {
|
||||
Console_adp__sys.Instance.Write_str_w_nl("compacting:");
|
||||
// hash.Sort();
|
||||
// int len = hash.Count();
|
||||
// List_adp deleted = List_adp_.new_();
|
||||
// List_adp deleted = List_adp_.New();
|
||||
// int deleted_size = 0, deleted_count = 0;
|
||||
// for (int i = 0; i < len; i++) {
|
||||
// Gfo_cache_data itm = (Gfo_cache_data)hash.Get_at(i);
|
||||
|
||||
@@ -35,7 +35,7 @@ public class Gfo_cache_mgr_base {
|
||||
public void Compress() {
|
||||
hash.Sort_by(Gfo_cache_itm_comparer.Touched_asc);
|
||||
int del_len = hash.Count() - compress_to;
|
||||
List_adp del_list = List_adp_.new_();
|
||||
List_adp del_list = List_adp_.New();
|
||||
for (int i = 0; i < del_len; i++) {
|
||||
Gfo_cache_itm itm = (Gfo_cache_itm)hash.Get_at(i);
|
||||
del_list.Add(itm);
|
||||
|
||||
@@ -17,8 +17,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.consoles; import gplx.*; import gplx.core.*;
|
||||
public class Gfo_cmd_arg_mgr {
|
||||
private final Ordered_hash hash = Ordered_hash_.New();
|
||||
private final List_adp err_list = List_adp_.new_(), tmp_vals = List_adp_.new_();
|
||||
private final Ordered_hash hash = Ordered_hash_.New();
|
||||
private final List_adp err_list = List_adp_.New(), tmp_vals = List_adp_.New();
|
||||
public String[] Orig_ary() {return orig_ary;} private String[] orig_ary;
|
||||
public void Reset() {
|
||||
hash.Clear();
|
||||
|
||||
@@ -17,8 +17,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.consoles; import gplx.*; import gplx.core.*;
|
||||
public class Gfo_cmd_arg_mgr_printer {
|
||||
private final Gfo_cmd_arg_mgr arg_mgr;
|
||||
private final Bry_bfr tmp_bfr = Bry_bfr.new_();
|
||||
private final Gfo_cmd_arg_mgr arg_mgr;
|
||||
private final Bry_bfr tmp_bfr = Bry_bfr_.New();
|
||||
public Gfo_cmd_arg_mgr_printer(Gfo_cmd_arg_mgr arg_mgr) {this.arg_mgr = arg_mgr;}
|
||||
public boolean Print(Gfo_usr_dlg usr_dlg, String header, String app_name, String key__print_help, String key__print_header, String key__print_args) {
|
||||
if (arg_mgr.Get_by_as_bool(key__print_header))
|
||||
|
||||
@@ -18,7 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx.core.flds; import gplx.*; import gplx.core.*;
|
||||
import gplx.core.encoders.*;
|
||||
public class Gfo_fld_rdr extends Gfo_fld_base {
|
||||
private Bry_bfr bfr = Bry_bfr.new_(); private static final byte[] Bry_nil = Bry_.new_a7("\\0");
|
||||
private Bry_bfr bfr = Bry_bfr_.New(); private static final byte[] Bry_nil = Bry_.new_a7("\\0");
|
||||
public byte[] Data() {return data;} public Gfo_fld_rdr Data_(byte[] v) {data = v; data_len = v.length; pos = 0; return this;} private byte[] data; int data_len;
|
||||
public int Pos() {return pos;} public Gfo_fld_rdr Pos_(int v) {pos = v; return this;} private int pos;
|
||||
public int Fld_bgn() {return fld_bgn;} public Gfo_fld_rdr Fld_bgn_(int v) {fld_bgn = v; return this;} private int fld_bgn;
|
||||
|
||||
@@ -52,5 +52,5 @@ class Gfo_fld_rdr_fxt {
|
||||
wtr.Write_bry_escape_fld(bry);
|
||||
Tfds.Eq(expd, bfr.To_str());
|
||||
return this;
|
||||
} private Bry_bfr bfr = Bry_bfr.new_();
|
||||
} private Bry_bfr bfr = Bry_bfr_.New();
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ package gplx.core.flds; import gplx.*; import gplx.core.*;
|
||||
import gplx.core.ios.*; import gplx.core.encoders.*;
|
||||
public class Gfo_fld_wtr extends Gfo_fld_base {
|
||||
public Bry_bfr Bfr() {return bfr;} public Gfo_fld_wtr Bfr_(Bry_bfr v) {bfr = v; return this;} Bry_bfr bfr;
|
||||
public Gfo_fld_wtr() {this.bfr = Bry_bfr.new_();}
|
||||
public Gfo_fld_wtr() {this.bfr = Bry_bfr_.New();}
|
||||
public Gfo_fld_wtr Write_int_base85_len5_fld(int v) {bfr.Add_base85(v, Base85_.Len_int); bfr.Add_byte(fld_dlm); return this;}
|
||||
public Gfo_fld_wtr Write_int_base85_lenN_fld(int v, int len) {bfr.Add_base85(v, len); bfr.Add_byte(fld_dlm); return this;}
|
||||
public Gfo_fld_wtr Write_int_variable_fld(int v) {bfr.Add_int_variable(v); bfr.Add_byte(fld_dlm); return this;}
|
||||
|
||||
@@ -16,31 +16,16 @@ 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.gfobjs; import gplx.*; import gplx.core.*;
|
||||
public interface Gfobj_ary extends Gfobj_grp {
|
||||
byte Ary_tid();
|
||||
int Len();
|
||||
}
|
||||
class Gfo_ary_str implements Gfobj_ary {
|
||||
public Gfo_ary_str(String[] ary) {this.ary = ary;}
|
||||
public byte Grp_tid() {return Gfobj_grp_.Grp_tid__ary;}
|
||||
public byte Ary_tid() {return Gfobj_ary_.Ary_tid__str;}
|
||||
public int Len() {return ary.length;}
|
||||
public Object Get_at(int i) {return ary[i];}
|
||||
public String[] Ary_str() {return ary;} private final String[] ary;
|
||||
}
|
||||
class Gfo_ary_int implements Gfobj_ary {
|
||||
public Gfo_ary_int(int[] ary) {this.ary = ary;}
|
||||
public byte Grp_tid() {return Gfobj_grp_.Grp_tid__ary;}
|
||||
public byte Ary_tid() {return Gfobj_ary_.Ary_tid__int;}
|
||||
public int Len() {return ary.length;}
|
||||
public Object Get_at(int i) {return ary[i];}
|
||||
public int[] Ary_int() {return ary;} private final int[] ary;
|
||||
}
|
||||
class Gfo_ary_ary implements Gfobj_ary {
|
||||
public Gfo_ary_ary(Gfobj_ary[] ary) {this.ary = ary;}
|
||||
public byte Grp_tid() {return Gfobj_grp_.Grp_tid__ary;}
|
||||
public byte Ary_tid() {return Gfobj_ary_.Ary_tid__ary;}
|
||||
public int Len() {return ary.length;}
|
||||
public Object Get_at(int i) {return ary[i];}
|
||||
public Gfobj_ary[] Ary_ary() {return ary;} private Gfobj_ary[] ary;
|
||||
public class Gfobj_ary implements Gfobj_grp { // NOTE: items in array can vary in types; EX:['a', 1, false]
|
||||
public Gfobj_ary(Object[] ary) {this.ary = ary;}
|
||||
public byte Grp_tid() {return Gfobj_grp_.Grp_tid__ary;}
|
||||
public int Len() {return ary.length;}
|
||||
public Object Get_at(int i) {return ary[i];}
|
||||
public Object[] Ary_obj() {return ary;} private Object[] ary;
|
||||
public Gfobj_ary Ary_(Object[] v) {this.ary = v; return this;}
|
||||
public Gfobj_nde New_nde_at(int i) {
|
||||
Gfobj_nde rv = Gfobj_nde.New();
|
||||
ary[i] = rv;
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,9 +25,11 @@ class Gfobj_fld_ {
|
||||
public static final byte
|
||||
Fld_tid__ary = 0
|
||||
, Fld_tid__nde = 1
|
||||
, Fld_tid__int = 2
|
||||
, Fld_tid__long = 3
|
||||
, Fld_tid__str = 4
|
||||
, Fld_tid__bool = 2
|
||||
, Fld_tid__int = 3
|
||||
, Fld_tid__long = 4
|
||||
, Fld_tid__double = 5
|
||||
, Fld_tid__str = 6
|
||||
;
|
||||
}
|
||||
class Gfobj_fld_str implements Gfobj_fld {
|
||||
@@ -37,6 +39,13 @@ class Gfobj_fld_str implements Gfobj_fld {
|
||||
public Object As_obj() {return val;}
|
||||
public String As_str() {return val;} private String val;
|
||||
}
|
||||
class Gfobj_fld_bool implements Gfobj_fld {
|
||||
public Gfobj_fld_bool(String key, boolean val) {this.key = key; this.val = val;}
|
||||
public String Key() {return key;} private final String key;
|
||||
public byte Fld_tid() {return Gfobj_fld_.Fld_tid__bool;}
|
||||
public Object As_obj() {return val;}
|
||||
public boolean As_bool() {return val;} private boolean val;
|
||||
}
|
||||
class Gfobj_fld_int implements Gfobj_fld {
|
||||
public Gfobj_fld_int(String key, int val) {this.key = key; this.val = val;}
|
||||
public String Key() {return key;} private final String key;
|
||||
@@ -51,6 +60,13 @@ class Gfobj_fld_long implements Gfobj_fld {
|
||||
public Object As_obj() {return val;}
|
||||
public long As_long() {return val;} private long val;
|
||||
}
|
||||
class Gfobj_fld_double implements Gfobj_fld {
|
||||
public Gfobj_fld_double(String key, double val) {this.key = key; this.val = val;}
|
||||
public String Key() {return key;} private final String key;
|
||||
public byte Fld_tid() {return Gfobj_fld_.Fld_tid__double;}
|
||||
public Object As_obj() {return val;}
|
||||
public double As_double() {return val;} private double val;
|
||||
}
|
||||
class Gfobj_fld_nde implements Gfobj_fld {
|
||||
public Gfobj_fld_nde(String key, Gfobj_nde val) {this.key = key; this.val = val;}
|
||||
public String Key() {return key;} private final String key;
|
||||
|
||||
@@ -17,5 +17,5 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.gfobjs; import gplx.*; import gplx.core.*;
|
||||
public interface Gfobj_grp {
|
||||
byte Grp_tid();
|
||||
byte Grp_tid();
|
||||
}
|
||||
|
||||
@@ -22,13 +22,39 @@ public class Gfobj_nde implements Gfobj_grp {
|
||||
public int Len() {return subs == null ? 0 : subs.Len();}
|
||||
public Gfobj_fld Get_at(int i) {return subs == null ? null : (Gfobj_fld)subs.Get_at(i);}
|
||||
public Gfobj_fld Get_by(String k) {return subs == null ? null : (Gfobj_fld)subs.Get_by(k);}
|
||||
public Gfobj_ary Get_ary(String k) {return ((Gfobj_fld_ary)Get_by(k)).As_ary();}
|
||||
public Gfobj_nde Get_nde(int i) {return ((Gfobj_fld_nde)Get_at(i)).As_nde();}
|
||||
public Gfobj_nde Get_nde(String k) {return ((Gfobj_fld_nde)Get_by(k)).As_nde();}
|
||||
public long Get_long(String k) {
|
||||
Gfobj_fld fld = Get_by(k);
|
||||
switch (fld.Fld_tid()) {
|
||||
case Gfobj_fld_.Fld_tid__long: return ((Gfobj_fld_long)fld).As_long();
|
||||
case Gfobj_fld_.Fld_tid__int : return ((Gfobj_fld_int )fld).As_int();
|
||||
default: throw Err_.new_unhandled_default(fld.Fld_tid());
|
||||
}
|
||||
}
|
||||
public int Get_int(String k) {
|
||||
Gfobj_fld fld = Get_by(k);
|
||||
switch (fld.Fld_tid()) {
|
||||
case Gfobj_fld_.Fld_tid__int : return ((Gfobj_fld_int )fld).As_int();
|
||||
default: throw Err_.new_unhandled_default(fld.Fld_tid());
|
||||
}
|
||||
}
|
||||
public byte Get_byte(String k) {return (byte)Get_int(k);}
|
||||
public String Get_str(String k) {return ((Gfobj_fld_str)Get_by(k)).As_str();}
|
||||
public Io_url Get_url(String k) {return Io_url_.new_any_(((Gfobj_fld_str)Get_by(k)).As_str());}
|
||||
public Gfobj_nde Add_fld(Gfobj_fld fld) {if (subs == null) subs = Ordered_hash_.New(); subs.Add(fld.Key(), fld); return this;}
|
||||
public Gfobj_nde Add_bool(String key, boolean val) {return Add_fld(new Gfobj_fld_bool(key, val));}
|
||||
public Gfobj_nde Add_byte(String key, byte val) {return Add_fld(new Gfobj_fld_int(key, val));}
|
||||
public Gfobj_nde Add_int(String key, int val) {return Add_fld(new Gfobj_fld_int(key, val));}
|
||||
public Gfobj_nde Add_long(String key, long val) {return Add_fld(new Gfobj_fld_long(key, val));}
|
||||
public Gfobj_nde Add_str(String key, String val) {return Add_fld(new Gfobj_fld_str(key, val));}
|
||||
public Gfobj_nde Add_url(String key, Io_url val) {return Add_fld(new Gfobj_fld_str(key, val.Raw()));}
|
||||
public Gfobj_nde Add_double(String key, double val) {return Add_fld(new Gfobj_fld_double(key, val));}
|
||||
public Gfobj_nde Add_nde(String key, Gfobj_nde val) {return Add_fld(new Gfobj_fld_nde(key, val));}
|
||||
public Gfobj_nde Add_ary(String key, Gfobj_ary val) {return Add_fld(new Gfobj_fld_ary(key, val));}
|
||||
public Gfobj_nde New_nde(String key) {Gfobj_nde rv = new Gfobj_nde(); Add_fld(new Gfobj_fld_nde(key, rv)); return rv;}
|
||||
public Gfobj_ary_nde New_ary_nde(String key) {Gfobj_ary_nde rv = new Gfobj_ary_nde(null); Add_fld(new Gfobj_fld_ary(key, rv)); return rv;}
|
||||
public Gfobj_nde New_nde(String key) {Gfobj_nde rv = new Gfobj_nde(); Add_fld(new Gfobj_fld_nde(key, rv)); return rv;}
|
||||
public Gfobj_ary New_ary(String key) {Gfobj_ary rv = new Gfobj_ary(null); Add_fld(new Gfobj_fld_ary(key, rv)); return rv;}
|
||||
public Gfobj_ary New_ary(String key, int subs_len) {return New_ary(key).Ary_(new Object[subs_len]);}
|
||||
public static Gfobj_nde New() {return new Gfobj_nde();}
|
||||
}
|
||||
|
||||
92
400_xowa/src/gplx/core/gfobjs/Gfobj_rdr__json.java
Normal file
92
400_xowa/src/gplx/core/gfobjs/Gfobj_rdr__json.java
Normal file
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
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.gfobjs; import gplx.*; import gplx.core.*;
|
||||
import gplx.langs.jsons.*;
|
||||
public class Gfobj_rdr__json {
|
||||
private final Json_parser parser = new Json_parser();
|
||||
public Gfobj_grp Load(Io_url url) {
|
||||
byte[] src = Io_mgr.Instance.LoadFilBryOrNull(url); if (src == null) return null;
|
||||
return this.Parse(src);
|
||||
}
|
||||
public Gfobj_grp Parse(byte[] src) {
|
||||
Json_doc jdoc = parser.Parse(src);
|
||||
if (jdoc.Root_grp().Tid() == Json_itm_.Tid__nde) {
|
||||
Gfobj_nde rv_nde = Gfobj_nde.New();
|
||||
Parse_nde((Json_nde)jdoc.Root_grp(), rv_nde);
|
||||
return rv_nde;
|
||||
}
|
||||
else {
|
||||
Gfobj_ary rv_ary = new Gfobj_ary(null);
|
||||
Parse_ary((Json_ary)jdoc.Root_grp(), rv_ary);
|
||||
return rv_ary;
|
||||
}
|
||||
}
|
||||
private void Parse_nde(Json_nde jnde, Gfobj_nde gnde) {
|
||||
int len = jnde.Len();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Json_kv kv = jnde.Get_at_as_kv(i);
|
||||
String key_str = kv.Key_as_str();
|
||||
Json_itm val = kv.Val();
|
||||
byte val_tid = val.Tid();
|
||||
switch (val_tid) {
|
||||
case Json_itm_.Tid__str: gnde.Add_str (key_str, ((Json_itm_str)val).Data_as_str()); break;
|
||||
case Json_itm_.Tid__bool: gnde.Add_bool (key_str, ((Json_itm_bool)val).Data_as_bool()); break;
|
||||
case Json_itm_.Tid__int: gnde.Add_int (key_str, ((Json_itm_int)val).Data_as_int()); break;
|
||||
case Json_itm_.Tid__long: gnde.Add_long (key_str, ((Json_itm_long)val).Data_as_long()); break;
|
||||
case Json_itm_.Tid__decimal: gnde.Add_double (key_str, ((Json_itm_decimal)val).Data_as_decimal().To_double()); break;
|
||||
case Json_itm_.Tid__null: gnde.Add_str (key_str, null); break;
|
||||
case Json_itm_.Tid__ary:
|
||||
Gfobj_ary sub_ary = new Gfobj_ary(null);
|
||||
gnde.Add_ary(key_str, sub_ary);
|
||||
Parse_ary(Json_ary.cast(val), sub_ary);
|
||||
break;
|
||||
case Json_itm_.Tid__nde:
|
||||
Gfobj_nde sub_gnde = Gfobj_nde.New();
|
||||
gnde.Add_nde(key_str, sub_gnde);
|
||||
Parse_nde(Json_nde.cast(val), sub_gnde);
|
||||
break;
|
||||
default: throw Err_.new_unhandled_default(val_tid);
|
||||
}
|
||||
}
|
||||
}
|
||||
private void Parse_ary(Json_ary jry, Gfobj_ary gry) {
|
||||
int len = jry.Len();
|
||||
Object[] ary = new Object[len];
|
||||
gry.Ary_(ary);
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Json_itm jsub = jry.Get_at(i);
|
||||
switch (jsub.Tid()) {
|
||||
case Json_itm_.Tid__ary: {
|
||||
Gfobj_ary sub_ary = new Gfobj_ary(null);
|
||||
Parse_ary(Json_ary.cast(jsub), sub_ary);
|
||||
ary[i] = sub_ary;
|
||||
break;
|
||||
}
|
||||
case Json_itm_.Tid__nde: {
|
||||
Gfobj_nde sub_ary = Gfobj_nde.New();
|
||||
Parse_nde(Json_nde.cast(jsub), sub_ary);
|
||||
ary[i] = sub_ary;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
ary[i] = jsub.Data();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
88
400_xowa/src/gplx/core/gfobjs/Gfobj_rdr__json_tst.java
Normal file
88
400_xowa/src/gplx/core/gfobjs/Gfobj_rdr__json_tst.java
Normal file
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.gfobjs; import gplx.*; import gplx.core.*;
|
||||
import org.junit.*; import gplx.core.tests.*;
|
||||
import gplx.langs.jsons.*;
|
||||
public class Gfobj_rdr__json_tst {
|
||||
private final Gfobj_wtr__json_fxt fxt = new Gfobj_wtr__json_fxt();
|
||||
@Test public void Type() {
|
||||
fxt.Test__parse(String_.Concat_lines_nl_skip_last
|
||||
( "{ 'k1':true"
|
||||
, ", 'k2':123"
|
||||
, ", 'k3':9876543210"
|
||||
, ", 'k4':1.23"
|
||||
, ", 'k5':null"
|
||||
, ", 'k6':'abc'"
|
||||
, "}"
|
||||
)
|
||||
, fxt.Make__nde
|
||||
( fxt.Make__fld_bool ("k1", true)
|
||||
, fxt.Make__fld_int ("k2", 123)
|
||||
, fxt.Make__fld_long ("k3", 9876543210L)
|
||||
, fxt.Make__fld_double ("k4", 1.23)
|
||||
, fxt.Make__fld_str ("k5", null)
|
||||
, fxt.Make__fld_str ("k6", "abc")
|
||||
));
|
||||
}
|
||||
@Test public void Nested() {
|
||||
fxt.Test__parse(String_.Concat_lines_nl_skip_last
|
||||
( "{ 'a1':'1a'"
|
||||
, ", 'a2':"
|
||||
, " { 'b1':'1b'"
|
||||
, " , 'b2':"
|
||||
, " { 'c1':'1c'"
|
||||
, " }"
|
||||
, " }"
|
||||
, ", 'a3':[1, 2, 3]"
|
||||
, "}"
|
||||
)
|
||||
, fxt.Make__nde
|
||||
( fxt.Make__fld_str ("a1", "1a")
|
||||
, fxt.Make__fld_nde ("a2"
|
||||
, fxt.Make__fld_str("b1", "1b")
|
||||
, fxt.Make__fld_nde("b2"
|
||||
, fxt.Make__fld_str("c1", "1c"))
|
||||
)
|
||||
, fxt.Make__fld_ary ("a3", 1, 2, 3)
|
||||
));
|
||||
}
|
||||
@Test public void Array() {
|
||||
fxt.Test__parse(String_.Concat_lines_nl_skip_last
|
||||
( "["
|
||||
, " [1, 2, 3]"
|
||||
, ", ['a', 'b', 'c']"
|
||||
, ", [true, false]"
|
||||
, ", [9876543210, 9876543211, 9876543212]"
|
||||
//, ", [1.23, 1.24, 1.25]"
|
||||
, ", [{'a':1}, {'b':2}, {'c':3}]"
|
||||
, "]"
|
||||
)
|
||||
, fxt.Make__ary
|
||||
( fxt.Make__ary (1, 2, 3)
|
||||
, fxt.Make__ary ("a", "b", "c")
|
||||
, fxt.Make__ary (true, false)
|
||||
, fxt.Make__ary (9876543210L, 9876543211L, 9876543212L)
|
||||
// , fxt.Make__ary (1.23, 1.24, 1.25)
|
||||
, fxt.Make__ary
|
||||
( fxt.Make__nde(fxt.Make__fld_int("a", 1))
|
||||
, fxt.Make__nde(fxt.Make__fld_int("b", 2))
|
||||
, fxt.Make__nde(fxt.Make__fld_int("c", 3))
|
||||
)
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -20,8 +20,12 @@ import gplx.langs.jsons.*;
|
||||
public class Gfobj_wtr__json {
|
||||
private final Json_wtr wtr = new Json_wtr();
|
||||
public Gfobj_wtr__json Opt_ws_(boolean v) {wtr.Opt_ws_(v); return this;}
|
||||
public Gfobj_wtr__json Opt_backslash_2x_(boolean v) {wtr.Opt_backslash_2x_(v); return this;}
|
||||
public Bry_bfr Bfr() {return wtr.Bfr();}
|
||||
public String To_str() {return wtr.To_str_and_clear();}
|
||||
public void Save(Io_url url) {
|
||||
Io_mgr.Instance.SaveFilBry(url, wtr.To_bry_and_clear());
|
||||
}
|
||||
public Gfobj_wtr__json Write(Gfobj_grp root) {
|
||||
switch (root.Grp_tid()) {
|
||||
case Gfobj_grp_.Grp_tid__nde:
|
||||
@@ -51,6 +55,8 @@ public class Gfobj_wtr__json {
|
||||
case Gfobj_fld_.Fld_tid__str: wtr.Kv_str(itm.Key() , ((Gfobj_fld_str)itm).As_str()); break;
|
||||
case Gfobj_fld_.Fld_tid__int: wtr.Kv_int(itm.Key() , ((Gfobj_fld_int)itm).As_int()); break;
|
||||
case Gfobj_fld_.Fld_tid__long: wtr.Kv_long(itm.Key() , ((Gfobj_fld_long)itm).As_long()); break;
|
||||
case Gfobj_fld_.Fld_tid__bool: wtr.Kv_bool(itm.Key() , ((Gfobj_fld_bool)itm).As_bool()); break;
|
||||
case Gfobj_fld_.Fld_tid__double: wtr.Kv_double(itm.Key() , ((Gfobj_fld_double)itm).As_double()); break;
|
||||
case Gfobj_fld_.Fld_tid__nde: wtr.Nde_bgn(itm.Key()); Write_nde(((Gfobj_fld_nde)itm).As_nde()); wtr.Nde_end();break;
|
||||
case Gfobj_fld_.Fld_tid__ary: wtr.Ary_bgn(itm.Key()); Write_ary(((Gfobj_fld_ary)itm).As_ary()); wtr.Ary_end();break;
|
||||
default: throw Err_.new_unhandled_default(itm.Fld_tid());
|
||||
@@ -58,35 +64,22 @@ public class Gfobj_wtr__json {
|
||||
}
|
||||
private void Write_ary(Gfobj_ary ary) {
|
||||
int len = ary.Len();
|
||||
byte ary_tid = ary.Ary_tid();
|
||||
switch (ary_tid) {
|
||||
case Gfobj_ary_.Ary_tid__str:
|
||||
String[] ary_str = ((Gfo_ary_str)ary).Ary_str();
|
||||
for (int i = 0; i < len; ++i)
|
||||
wtr.Ary_itm_str(ary_str[i]);
|
||||
break;
|
||||
case Gfobj_ary_.Ary_tid__int:
|
||||
int[] ary_int = ((Gfo_ary_int)ary).Ary_int();
|
||||
for (int i = 0; i < len; ++i)
|
||||
wtr.Ary_itm_obj(ary_int[i]);
|
||||
break;
|
||||
case Gfobj_ary_.Ary_tid__nde:
|
||||
Gfobj_nde[] ary_nde = ((Gfobj_ary_nde)ary).Ary_nde();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
wtr.Nde_bgn_ary();
|
||||
Write_nde(ary_nde[i]);
|
||||
wtr.Nde_end();
|
||||
}
|
||||
break;
|
||||
case Gfobj_ary_.Ary_tid__ary:
|
||||
Gfobj_ary[] ary_ary = ((Gfo_ary_ary)ary).Ary_ary();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
wtr.Ary_bgn_ary();
|
||||
Write_ary(ary_ary[i]);
|
||||
wtr.Ary_end();
|
||||
}
|
||||
break;
|
||||
default: throw Err_.new_unhandled_default(ary_tid);
|
||||
Object[] ary_obj = ((Gfobj_ary)ary).Ary_obj();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Object sub_itm = ary_obj[i];
|
||||
Class<?> sub_itm_type = Type_adp_.ClassOf_obj(sub_itm);
|
||||
if (Type_adp_.Eq(sub_itm_type, Gfobj_ary.class)) {
|
||||
wtr.Ary_bgn_ary();
|
||||
Write_ary((Gfobj_ary)sub_itm);
|
||||
wtr.Ary_end();
|
||||
}
|
||||
else if (Type_adp_.Eq(sub_itm_type, Gfobj_nde.class)) {
|
||||
wtr.Nde_bgn_ary();
|
||||
Write_nde((Gfobj_nde)sub_itm);
|
||||
wtr.Nde_end();
|
||||
}
|
||||
else
|
||||
wtr.Ary_itm_obj(sub_itm);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,22 +28,27 @@ public class Gfobj_wtr__json_fxt {
|
||||
}
|
||||
return nde;
|
||||
}
|
||||
public Gfobj_fld Make__fld_str(String key, String val) {return new Gfobj_fld_str(key, val);}
|
||||
public Gfobj_fld Make__fld_int(String key, int val) {return new Gfobj_fld_int(key, val);}
|
||||
public Gfobj_fld Make__fld_long(String key, long val) {return new Gfobj_fld_long(key, val);}
|
||||
public Gfobj_fld Make__fld_bool (String key, boolean val) {return new Gfobj_fld_bool(key, val);}
|
||||
public Gfobj_fld Make__fld_str (String key, String val) {return new Gfobj_fld_str(key, val);}
|
||||
public Gfobj_fld Make__fld_int (String key, int val) {return new Gfobj_fld_int(key, val);}
|
||||
public Gfobj_fld Make__fld_long (String key, long val) {return new Gfobj_fld_long(key, val);}
|
||||
public Gfobj_fld Make__fld_double (String key, double val) {return new Gfobj_fld_double(key, val);}
|
||||
public Gfobj_fld Make__fld_nde(String key, Gfobj_fld... ary) {
|
||||
Gfobj_nde nde = Make__nde(Gfobj_nde.New(), ary);
|
||||
Gfobj_fld_nde rv = new Gfobj_fld_nde(key, nde);
|
||||
return rv;
|
||||
}
|
||||
public Gfobj_fld Make__fld_ary_str(String key, String... ary) {return new Gfobj_fld_ary(key, new Gfo_ary_str(ary));}
|
||||
public Gfobj_fld Make__fld_ary_int(String key, int... ary) {return new Gfobj_fld_ary(key, new Gfo_ary_int(ary));}
|
||||
public Gfobj_fld Make__fld_ary_ary(String key, Gfobj_ary... ary) {return new Gfobj_fld_ary(key, new Gfo_ary_ary(ary));}
|
||||
public Gfobj_fld Make__fld_ary_nde(String key, Gfobj_nde... ary) {return new Gfobj_fld_ary(key, new Gfobj_ary_nde(ary));}
|
||||
public Gfobj_ary Make__ary_int(int... ary) {return new Gfo_ary_int(ary);}
|
||||
public Gfobj_fld Make__fld_ary (String key, Object... ary) {return new Gfobj_fld_ary(key, new Gfobj_ary(ary));}
|
||||
public Gfobj_ary Make__ary (Object... ary) {return new Gfobj_ary(ary);}
|
||||
public Gfobj_wtr__json_fxt Test__write(Gfobj_grp root, String... lines) {
|
||||
String[] expd = Json_doc.Make_str_ary_by_apos(lines);
|
||||
Gftest.Eq__ary(expd, Bry_.Ary(String_.SplitLines_nl(mgr.Write(root).To_str())), "json_write");
|
||||
return this;
|
||||
}
|
||||
public Gfobj_wtr__json_fxt Test__parse(String src, Gfobj_grp expd) {
|
||||
Gfobj_rdr__json rdr = new Gfobj_rdr__json();
|
||||
Gfobj_grp actl = rdr.Parse(Bry_.new_u8(Json_doc.Make_str_by_apos(src)));
|
||||
Gftest.Eq__ary(Bry_.Ary(String_.SplitLines_nl(mgr.Write(expd).To_str())), Bry_.Ary(String_.SplitLines_nl(mgr.Write(actl).To_str())), "json_write");
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,7 +58,7 @@ public class Gfobj_wtr__json_tst {
|
||||
fxt.Test__write
|
||||
( fxt.Make__nde
|
||||
( fxt.Make__fld_str ("k1", "v1")
|
||||
, fxt.Make__fld_ary_str ("k2", "a1", "a2", "a3")
|
||||
, fxt.Make__fld_ary ("k2", "a1", "a2", "a3")
|
||||
, fxt.Make__fld_int ("k3", 3)
|
||||
)
|
||||
, "{ 'k1':'v1'"
|
||||
@@ -76,7 +76,7 @@ public class Gfobj_wtr__json_tst {
|
||||
fxt.Test__write
|
||||
( fxt.Make__nde
|
||||
( fxt.Make__fld_str ("k1", "v1")
|
||||
, fxt.Make__fld_ary_int ("k2", 1, 2, 3)
|
||||
, fxt.Make__fld_ary ("k2", 1, 2, 3)
|
||||
, fxt.Make__fld_int ("k3", 3)
|
||||
)
|
||||
, "{ 'k1':'v1'"
|
||||
@@ -94,7 +94,7 @@ public class Gfobj_wtr__json_tst {
|
||||
fxt.Test__write
|
||||
( fxt.Make__nde
|
||||
( fxt.Make__fld_str ("k1", "v1")
|
||||
, fxt.Make__fld_ary_nde ("k2"
|
||||
, fxt.Make__fld_ary ("k2"
|
||||
, fxt.Make__nde (fxt.Make__fld_str("k21", "v21"))
|
||||
, fxt.Make__nde (fxt.Make__fld_str("k22", "v22"))
|
||||
)
|
||||
@@ -118,9 +118,9 @@ public class Gfobj_wtr__json_tst {
|
||||
fxt.Test__write
|
||||
( fxt.Make__nde
|
||||
( fxt.Make__fld_str ("k1", "v1")
|
||||
, fxt.Make__fld_ary_ary ("k2"
|
||||
, fxt.Make__ary_int (1, 2, 3)
|
||||
, fxt.Make__ary_int (4, 5, 6)
|
||||
, fxt.Make__fld_ary ("k2"
|
||||
, fxt.Make__ary (1, 2, 3)
|
||||
, fxt.Make__ary (4, 5, 6)
|
||||
)
|
||||
, fxt.Make__fld_int ("k3", 3)
|
||||
)
|
||||
@@ -144,7 +144,7 @@ public class Gfobj_wtr__json_tst {
|
||||
}
|
||||
@Test public void Root_ary() {
|
||||
fxt.Test__write
|
||||
( fxt.Make__ary_int(1, 2, 3)
|
||||
( fxt.Make__ary(1, 2, 3)
|
||||
, "[ 1"
|
||||
, ", 2"
|
||||
, ", 3"
|
||||
|
||||
@@ -21,11 +21,11 @@ class Gfo_i18n_itm {
|
||||
public Gfo_i18n_itm(int src, byte[] key, byte[] val, boolean val_fmt_exists, Gfo_i18n_val_cmd val_cmd) {
|
||||
this.src = src; this.key = key; this.val = val; this.val_fmt_exists = val_fmt_exists; this.val_cmd = val_cmd;
|
||||
}
|
||||
public int Src() {return src;} private final int src;
|
||||
public byte[] Key() {return key;} private final byte[] key;
|
||||
public byte[] Val() {return val;} private final byte[] val;
|
||||
public boolean Val_fmt_exists() {return val_fmt_exists;} private final boolean val_fmt_exists;
|
||||
public Gfo_i18n_val_cmd Val_cmd() {return val_cmd;} private final Gfo_i18n_val_cmd val_cmd;
|
||||
public int Src() {return src;} private final int src;
|
||||
public byte[] Key() {return key;} private final byte[] key;
|
||||
public byte[] Val() {return val;} private final byte[] val;
|
||||
public boolean Val_fmt_exists() {return val_fmt_exists;} private final boolean val_fmt_exists;
|
||||
public Gfo_i18n_val_cmd Val_cmd() {return val_cmd;} private final Gfo_i18n_val_cmd val_cmd;
|
||||
public byte[] Bld_none() {
|
||||
return val_cmd == null ? val : val_cmd.Process(src, key, val);
|
||||
}
|
||||
@@ -38,6 +38,6 @@ class Gfo_i18n_itm {
|
||||
}
|
||||
return val_cmd == null ? rv : val_cmd.Process(src, key, rv);
|
||||
}
|
||||
private static final Bry_fmtr tmp_fmtr = Bry_fmtr.new_();
|
||||
private static final Bry_bfr tmp_bfr = Bry_bfr.reset_(255);
|
||||
private static final Bry_fmtr tmp_fmtr = Bry_fmtr.new_();
|
||||
private static final Bry_bfr tmp_bfr = Bry_bfr_.Reset(255);
|
||||
}
|
||||
|
||||
@@ -16,7 +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.ios; import gplx.*; import gplx.core.*;
|
||||
import gplx.core.ios.*;/*IoStream*/
|
||||
import gplx.core.ios.streams.*;/*IoStream*/
|
||||
public class Io_buffer_rdr implements Rls_able {
|
||||
private Io_stream_rdr rdr;
|
||||
Io_buffer_rdr(Io_stream_rdr rdr, Io_url url, int bfr_len) {
|
||||
@@ -43,7 +43,7 @@ public class Io_buffer_rdr implements Rls_able {
|
||||
}
|
||||
private boolean Bfr_load(int bgn, int len) {
|
||||
int read = rdr.Read(bfr, bgn, len);
|
||||
if (read == gplx.core.ios.Io_stream_rdr_.Read_done) {fil_eof = true; return false;}
|
||||
if (read == gplx.core.ios.streams.Io_stream_rdr_.Read_done) {fil_eof = true; return false;}
|
||||
fil_pos += read;
|
||||
bfr_len = bgn + read;
|
||||
if (read < len) fil_eof = true;
|
||||
@@ -69,5 +69,5 @@ public class Io_buffer_rdr implements Rls_able {
|
||||
rv.Bfr_load(0, bfr_len);
|
||||
return rv;
|
||||
}
|
||||
public static final Io_buffer_rdr Null = new Io_buffer_rdr(); Io_buffer_rdr() {}
|
||||
public static final Io_buffer_rdr Null = new Io_buffer_rdr(); Io_buffer_rdr() {}
|
||||
}
|
||||
|
||||
@@ -16,7 +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.ios; import gplx.*; import gplx.core.*;
|
||||
import org.junit.*; import gplx.core.ios.*;
|
||||
import org.junit.*; import gplx.core.ios.*; import gplx.core.ios.streams.*;
|
||||
public class Io_buffer_rdr_tst {
|
||||
@Before public void init() {
|
||||
Io_mgr.Instance.InitEngine_mem();
|
||||
|
||||
@@ -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.ios; import gplx.*; import gplx.core.*;
|
||||
import gplx.core.ios.streams.*;
|
||||
public class Io_line_rdr {
|
||||
public Io_line_rdr (Gfo_usr_dlg usr_dlg, Io_url... urls) {this.usr_dlg = usr_dlg; this.urls = urls; if (urls.length == 0) bfr_state = Bfr_state_end;} Gfo_usr_dlg usr_dlg;
|
||||
public int Url_idx() {return url_idx;} private int url_idx;
|
||||
@@ -141,7 +142,7 @@ public class Io_line_rdr {
|
||||
stream_bry = Bry_.Empty;
|
||||
else
|
||||
stream_bry = Bry_.Mid(stream_bry, nl_pos + 1, stream_bry_len);
|
||||
stream = gplx.core.ios.IoStream_.ary_(stream_bry);
|
||||
stream = gplx.core.ios.streams.IoStream_.ary_(stream_bry);
|
||||
}
|
||||
else {
|
||||
stream = Io_mgr.Instance.OpenStreamRead(url);
|
||||
|
||||
@@ -48,7 +48,7 @@ public class Io_line_rdr_tst {
|
||||
}
|
||||
class Io_line_rdr_fxt {
|
||||
Io_line_rdr rdr;
|
||||
List_adp lines = List_adp_.new_(); Bry_bfr tmp = Bry_bfr.new_();
|
||||
List_adp lines = List_adp_.New(); Bry_bfr tmp = Bry_bfr_.New();
|
||||
public Io_line_rdr_fxt(Io_url... urls) {rdr = new Io_line_rdr(Gfo_usr_dlg_.Test(), urls);}
|
||||
public Io_line_rdr_fxt Load_len_lines_(int v) {return Load_len_(v * 3);} // 3: 2=##, 1=\n
|
||||
public Io_line_rdr_fxt Load_len_(int v) {rdr.Load_len_(v); return this;}
|
||||
|
||||
@@ -23,9 +23,9 @@ public class Io_sort {
|
||||
public Io_url[] Split(Gfo_usr_dlg usr_dlg, Io_url_gen src_fil_gen, Io_url_gen trg_fil_gen, Io_line_rdr_key_gen key_gen) {return Split(usr_dlg, src_fil_gen, trg_fil_gen, Io_sort_split_itm_sorter.Instance, key_gen);}
|
||||
public Io_url[] Split(Gfo_usr_dlg usr_dlg, Io_url_gen src_fil_gen, Io_url_gen trg_fil_gen, ComparerAble row_comparer, Io_line_rdr_key_gen key_gen) {
|
||||
Io_line_rdr rdr = new Io_line_rdr(usr_dlg, src_fil_gen.Prv_urls()).Load_len_(4 * Io_mgr.Len_kb).Key_gen_(key_gen); // NOTE: do not set load_len to memory_max; only want to load in increments
|
||||
List_adp rv = List_adp_.new_();
|
||||
Bry_bfr bfr = Bry_bfr.reset_(Const_bfr_max); int size_cur = 0;
|
||||
List_adp row_list = List_adp_.new_();
|
||||
List_adp rv = List_adp_.New();
|
||||
Bry_bfr bfr = Bry_bfr_.Reset(Const_bfr_max); int size_cur = 0;
|
||||
List_adp row_list = List_adp_.New();
|
||||
while (true) {
|
||||
boolean reading = rdr.Read_next();
|
||||
int size_row = rdr.Itm_pos_end() - rdr.Itm_pos_bgn();
|
||||
|
||||
@@ -17,7 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.ios; import gplx.*; import gplx.core.*;
|
||||
public class Io_sort_fil_basic implements Io_sort_cmd { // 123|bgn|end|1
|
||||
public Io_sort_fil_basic(Gfo_usr_dlg usr_dlg, Io_url_gen url_gen, int flush_len) {this.usr_dlg = usr_dlg; this.url_gen = url_gen; this.flush_len = flush_len;} Io_url_gen url_gen; Bry_bfr bfr = Bry_bfr.new_(); int flush_len; Gfo_usr_dlg usr_dlg;
|
||||
public Io_sort_fil_basic(Gfo_usr_dlg usr_dlg, Io_url_gen url_gen, int flush_len) {this.usr_dlg = usr_dlg; this.url_gen = url_gen; this.flush_len = flush_len;} Io_url_gen url_gen; Bry_bfr bfr = Bry_bfr_.New(); int flush_len; Gfo_usr_dlg usr_dlg;
|
||||
public void Sort_bgn() {}
|
||||
public void Sort_do(Io_line_rdr rdr) {
|
||||
int bgn = rdr.Itm_pos_bgn(), end = rdr.Itm_pos_end();
|
||||
|
||||
@@ -25,7 +25,7 @@ public class Io_sort_misc_tst {
|
||||
}
|
||||
private void tst_Io_url_gen_dir(String dir_str, String fmt, int digits, int calls, String... expd) {
|
||||
Io_url dir = Io_url_.mem_dir_(dir_str);
|
||||
List_adp actl_list = List_adp_.new_();
|
||||
List_adp actl_list = List_adp_.New();
|
||||
Io_url_gen wkr = Io_url_gen_.dir_(dir, fmt, digits);
|
||||
for (int i = 0; i < calls; i++)
|
||||
actl_list.Add(wkr.Nxt_url().Raw());
|
||||
|
||||
@@ -48,7 +48,7 @@ class Io_sort_fxt {
|
||||
Tfds.Eq_ary_str(String_.SplitLines_nl(sorted), String_.SplitLines_nl(actl));
|
||||
}
|
||||
public String GenRandom(int rows, int pad) {
|
||||
List_adp list = List_adp_.new_();
|
||||
List_adp list = List_adp_.New();
|
||||
for (int i = 0; i < rows; i++)
|
||||
list.Add(Int_.To_str_pad_bgn_zero(i, pad) + "|");
|
||||
list.Shuffle();
|
||||
|
||||
@@ -17,6 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.ios; import gplx.*; import gplx.core.*;
|
||||
import java.io.InputStream;
|
||||
import gplx.core.ios.streams.*;
|
||||
public class Io_stream_rdr_process implements Io_stream_rdr {
|
||||
private Process process;
|
||||
private InputStream stream_read;
|
||||
|
||||
@@ -16,8 +16,9 @@ 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.ios; import gplx.*; import gplx.core.*;
|
||||
import gplx.core.ios.streams.*;
|
||||
public class Io_stream_zip_mgr {
|
||||
private final Bry_bfr bfr = Bry_bfr.reset_(256);
|
||||
private final Bry_bfr bfr = Bry_bfr_.Reset(256);
|
||||
private Io_stream_wtr wtr_gzip, wtr_zip, wtr_bzip2;
|
||||
private Io_stream_rdr rdr_gzip, rdr_zip, rdr_bzip2;
|
||||
public byte[] Zip(byte type, byte[] val) {
|
||||
|
||||
30
400_xowa/src/gplx/core/lists/hashs/Hash_adp__int.java
Normal file
30
400_xowa/src/gplx/core/lists/hashs/Hash_adp__int.java
Normal file
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
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.lists.hashs; import gplx.*; import gplx.core.*; import gplx.core.lists.*;
|
||||
import gplx.core.primitives.*;
|
||||
public class Hash_adp__int {
|
||||
private final Hash_adp hash = Hash_adp_.New();
|
||||
private final Int_obj_ref tmp_key = Int_obj_ref.New_neg1();
|
||||
public void Clear() {hash.Clear();}
|
||||
public Object Get_by(int key) {return hash.Get_by_or_fail(tmp_key.Val_(key));}
|
||||
public Object Get_by_or_null(int key) {return hash.Get_by(tmp_key.Val_(key));}
|
||||
public void Add(int key, Object obj) {hash.Add(Int_obj_ref.New(key), obj);}
|
||||
public void Add(Int_obj_ref key, Object obj) {hash.Add(key, obj);}
|
||||
public void Add_if_dupe_use_1st(int key, Object obj) {hash.Add_if_dupe_use_1st(Int_obj_ref.New(key), obj);}
|
||||
public void Add_if_dupe_use_nth(Int_obj_ref key, Object obj) {hash.Add_if_dupe_use_nth(key, obj);}
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
/*
|
||||
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.logs; import gplx.*; import gplx.core.*;
|
||||
public class Gfo_log_fil {
|
||||
private final Bry_bfr fil_bfr = Bry_bfr.new_(), msg_bfr = Bry_bfr.new_();
|
||||
private final String key;
|
||||
private final Io_url dir;
|
||||
private final long size_max;
|
||||
private int file_idx;
|
||||
private Io_url fil_cur;
|
||||
private final Gfo_log_fmtr fmtr = new Gfo_log_fmtr();
|
||||
private final Gfo_log_fil session;
|
||||
public Gfo_log_fil(Gfo_log_fil session, String key, Io_url dir, long size_max) {
|
||||
this.session = session;
|
||||
this.key = key;
|
||||
this.dir = dir;
|
||||
this.size_max = size_max;
|
||||
this.fil_cur = Fil_new();
|
||||
}
|
||||
public void Add(String msg, Object... vals) {
|
||||
fmtr.Add(msg_bfr, msg, vals);
|
||||
Add_by_bfr(msg_bfr);
|
||||
msg_bfr.Clear();
|
||||
}
|
||||
public void Add_by_bfr(Bry_bfr msg_bfr) {
|
||||
if (msg_bfr.Len() + fil_bfr.Len() > size_max) {
|
||||
this.Flush();
|
||||
fil_cur = Fil_new();
|
||||
}
|
||||
fil_bfr.Add_bfr_and_preserve(msg_bfr);
|
||||
if (session != null) session.Add_by_bfr(msg_bfr);
|
||||
}
|
||||
public void Flush() {
|
||||
Io_mgr.Instance.AppendFilBfr(fil_cur, fil_bfr);
|
||||
}
|
||||
private Io_url Fil_new() {
|
||||
String part = size_max == -1 ? "" : "-" + Int_.To_str(++file_idx);
|
||||
return dir.OwnerDir().GenSubFil_ary(key, part, ".log");
|
||||
}
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
/*
|
||||
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.logs; import gplx.*; import gplx.core.*;
|
||||
import gplx.core.btries.*;
|
||||
class Gfo_log_fmtr {
|
||||
private final Btrie_fast_mgr trie = Btrie_fast_mgr.cs()
|
||||
.Add("~|" , Bry_.new_a7("~|<"))
|
||||
.Add("|" , Bry_.new_a7("~||"))
|
||||
.Add("\n" , Bry_.new_a7("~|n"))
|
||||
.Add("\t" , Bry_.new_a7("~|t"))
|
||||
;
|
||||
public void Add(Bry_bfr bfr, String msg, Object... vals) {
|
||||
Add_bry(bfr, Bry_.new_u8(msg));
|
||||
int len = vals.length;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
bfr.Add_byte(Byte_ascii.Pipe);
|
||||
byte[] val_bry = Bry_.new_u8(Object_.Xto_str_strict_or_empty(vals[i]));
|
||||
Add_bry(bfr, val_bry);
|
||||
}
|
||||
bfr.Add_byte_nl();
|
||||
}
|
||||
private void Add_bry(Bry_bfr bfr, byte[] src) {
|
||||
if (src == null) return;
|
||||
int len = src.length; if (len == 0) return;
|
||||
int pos = 0;
|
||||
int add_bgn = -1;
|
||||
while (true) {
|
||||
if (pos == len) break;
|
||||
byte b = src[pos];
|
||||
Object o = trie.Match_bgn_w_byte(b, src, pos, len);
|
||||
if (o == null) {
|
||||
if (add_bgn == -1) add_bgn = pos;
|
||||
}
|
||||
else {
|
||||
if (add_bgn != -1) bfr.Add_mid(src, add_bgn, pos);
|
||||
byte[] repl = (byte[])o;
|
||||
bfr.Add(repl);
|
||||
pos = trie.Match_pos();
|
||||
}
|
||||
}
|
||||
if (add_bgn != -1) bfr.Add_mid(src, add_bgn, len);
|
||||
}
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
/*
|
||||
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.logs; import gplx.*; import gplx.core.*;
|
||||
public class Gfo_log_mgr {
|
||||
private final Ordered_hash fil_list = Ordered_hash_.New();
|
||||
private final Gfo_log_fil session_fil;
|
||||
private final Io_url dir;
|
||||
private final long size_dflt = Io_mgr.Len_mb * 2;
|
||||
public Gfo_log_mgr(Io_url dir) {
|
||||
this.dir = dir;
|
||||
this.session_fil = new Gfo_log_fil(null, "session", dir, -1);
|
||||
}
|
||||
public Gfo_log_fil Fils__get_or_new(String key) {
|
||||
Gfo_log_fil rv = (Gfo_log_fil)fil_list.Get_by(key);
|
||||
if (rv == null) {
|
||||
rv = new Gfo_log_fil(session_fil, key, dir, size_dflt);
|
||||
fil_list.Add(key, rv);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public void Msgs__add(String fil_key, String msg, Object... vals) {
|
||||
Gfo_log_fil fil = (Gfo_log_fil)Fils__get_or_new(fil_key);
|
||||
fil.Add(msg, vals);
|
||||
}
|
||||
}
|
||||
// class Some_log_cls {
|
||||
// private final Gfo_log_mgr log_mgr = new Gfo_log_mgr();
|
||||
// public void Init() {
|
||||
// }
|
||||
// private Gfo_log_fil dedicated;
|
||||
// public void Init_dedicated() {
|
||||
// dedicated = log_mgr.Fil_get_or_new("parse");
|
||||
// }
|
||||
// public void Proc_w_dedicate() {
|
||||
// dedicated.Add("file download failed", "url", "msg");
|
||||
// }
|
||||
// public void Proc_dynamic() {
|
||||
// log_mgr.Msg_add("parse", "file download failed", "url", "msg");
|
||||
// }
|
||||
// }
|
||||
@@ -32,14 +32,14 @@ public class Gfo_inet_conn_ {
|
||||
public static void new_prototype_(int v) {new_prototype = v;} private static int new_prototype = Tid__http;
|
||||
}
|
||||
class Gfo_inet_conn__mem__hash implements Gfo_inet_conn {
|
||||
private final Hash_adp hash = Hash_adp_.new_();
|
||||
private final Hash_adp hash = Hash_adp_.New();
|
||||
public int Tid() {return Gfo_inet_conn_.Tid__mem__hash;}
|
||||
public void Clear() {hash.Clear();}
|
||||
public void Upload_by_bytes(String url, byte[] data) {hash.Add(url, data);}
|
||||
public byte[] Download_as_bytes_or_null(String url) {return (byte[])hash.Get_by(url);}
|
||||
}
|
||||
class Gfo_inet_conn__mem__pile implements Gfo_inet_conn {
|
||||
private final List_adp pile = List_adp_.new_();
|
||||
private final List_adp pile = List_adp_.New();
|
||||
public int Tid() {return Gfo_inet_conn_.Tid__mem__hash;}
|
||||
public void Clear() {pile.Clear();}
|
||||
public void Upload_by_bytes(String url, byte[] data) {pile.Add(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.net.qargs.*;
|
||||
public class Gfo_url {
|
||||
public byte[] Raw() {return raw;} private byte[] raw;
|
||||
public byte Protocol_tid() {return protocol_tid;} private byte protocol_tid;
|
||||
@@ -34,5 +35,5 @@ public class Gfo_url {
|
||||
this.anch = anch;
|
||||
return this;
|
||||
}
|
||||
public static final Gfo_url Empty = new Gfo_url().Ctor(Bry_.Empty, Gfo_protocol_itm.Tid_unknown, Bry_.Empty, Bry_.Ary_empty, null, null);
|
||||
public static final Gfo_url Empty = new Gfo_url().Ctor(Bry_.Empty, Gfo_protocol_itm.Tid_unknown, Bry_.Empty, Bry_.Ary_empty, null, null);
|
||||
}
|
||||
|
||||
@@ -17,10 +17,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.net; import gplx.*; import gplx.core.*;
|
||||
import gplx.core.primitives.*; import gplx.core.btries.*;
|
||||
import gplx.core.net.qargs.*;
|
||||
public class Gfo_url_parser {
|
||||
private final Btrie_slim_mgr protocols = Btrie_slim_mgr.ci_a7(); // ASCII:url_protocol; EX:"http:", "ftp:", etc
|
||||
private final Bry_ary segs_ary = new Bry_ary(4), qargs = new Bry_ary(4);
|
||||
private final Bry_bfr tmp_bfr = Bry_bfr.reset_(500);
|
||||
private final Btrie_slim_mgr protocols = Btrie_slim_mgr.ci_a7(); // ASCII:url_protocol; EX:"http:", "ftp:", etc
|
||||
private final Bry_ary segs_ary = new Bry_ary(4), qargs = new Bry_ary(4);
|
||||
private final Bry_bfr tmp_bfr = Bry_bfr_.Reset(500);
|
||||
public byte[] Relative_url_protocol_bry() {return Gfo_protocol_itm.Itm_https.Key_w_colon_bry();} // NOTE: https b/c any WMF wiki will now default to WMF; DATE:2015-07-26
|
||||
public Gfo_url_parser() {
|
||||
Init_protocols(Gfo_protocol_itm.Ary());
|
||||
@@ -254,5 +255,5 @@ public class Gfo_url_parser {
|
||||
qargs.Clear();
|
||||
segs_ary.Set_at_last(tmp_bfr.To_bry_and_clear());
|
||||
}
|
||||
public static final byte[] Bry_double_slash = new byte[] {Byte_ascii.Slash, Byte_ascii.Slash};
|
||||
public static final byte[] Bry_double_slash = new byte[] {Byte_ascii.Slash, Byte_ascii.Slash};
|
||||
}
|
||||
|
||||
@@ -16,8 +16,9 @@ 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.net.qargs.*;
|
||||
class Gfo_url_parser_fxt {
|
||||
private final Gfo_url_parser parser = new Gfo_url_parser();
|
||||
private final Gfo_url_parser parser = new Gfo_url_parser();
|
||||
private Gfo_url actl;
|
||||
public Gfo_url_parser_fxt Chk_protocol_tid(byte v) {Tfds.Eq_byte(v, actl.Protocol_tid(), "protocol_tid"); return this;}
|
||||
public Gfo_url_parser_fxt Chk_protocol_bry(String v) {Tfds.Eq_str(v, actl.Protocol_bry(), "protocol_bry"); return this;}
|
||||
@@ -35,5 +36,5 @@ class Gfo_url_parser_fxt {
|
||||
parser.Parse_site_fast(site_data, raw_bry, 0, raw_bry.length);
|
||||
String actl = String_.new_u8(raw_bry, site_data.Site_bgn(), site_data.Site_end());
|
||||
Tfds.Eq(expd, actl);
|
||||
} private final Gfo_url_site_data site_data = new Gfo_url_site_data();
|
||||
} private final Gfo_url_site_data site_data = new Gfo_url_site_data();
|
||||
}
|
||||
|
||||
@@ -16,7 +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.ios.*;
|
||||
import gplx.core.ios.*; import gplx.core.ios.streams.*;
|
||||
public interface Http_client_wtr {
|
||||
void Stream_(Object o);
|
||||
void Write_bry(byte[] bry);
|
||||
|
||||
@@ -16,10 +16,10 @@ 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.ios.*;
|
||||
import gplx.core.ios.*; import gplx.core.ios.streams.*;
|
||||
import java.io.*;
|
||||
class Http_client_wtr__stream implements Http_client_wtr {
|
||||
private final byte[] tmp_stream_bry = new byte[1024];
|
||||
private final byte[] tmp_stream_bry = new byte[1024];
|
||||
private DataOutputStream stream;
|
||||
public void Stream_(Object o) {
|
||||
this.stream = new DataOutputStream((OutputStream)o);
|
||||
|
||||
@@ -22,7 +22,7 @@ public class Http_request_parser {
|
||||
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, origin;
|
||||
private Http_post_data_hash post_data_hash;
|
||||
private final Bry_bfr tmp_bfr = Bry_bfr.new_(255);
|
||||
private final Bry_bfr tmp_bfr = Bry_bfr_.New_w_size(255);
|
||||
private final Http_server_wtr server_wtr; private final boolean log;
|
||||
public Http_request_parser(Http_server_wtr server_wtr, boolean log) {this.server_wtr = server_wtr; this.log = log;}
|
||||
public void Clear() {
|
||||
|
||||
25
400_xowa/src/gplx/core/net/downloads/Http_download_wkr.java
Normal file
25
400_xowa/src/gplx/core/net/downloads/Http_download_wkr.java
Normal file
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
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.downloads; import gplx.*; import gplx.core.*; import gplx.core.net.*;
|
||||
public interface Http_download_wkr {
|
||||
String Fail_msg();
|
||||
Http_download_wkr Make_new();
|
||||
long Checkpoint__load_by_trg_fil(Io_url trg_url);
|
||||
byte Exec(gplx.core.progs.Gfo_prog_ui prog_ui, String src_str, Io_url trg_url, long expd_size);
|
||||
void Exec_cleanup();
|
||||
}
|
||||
@@ -15,12 +15,7 @@ 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.gfobjs; import gplx.*; import gplx.core.*;
|
||||
public class Gfobj_ary_ {
|
||||
public static final byte
|
||||
Ary_tid__ary = 0
|
||||
, Ary_tid__nde = 1
|
||||
, Ary_tid__str = 2
|
||||
, Ary_tid__int = 3
|
||||
;
|
||||
package gplx.core.net.downloads; import gplx.*; import gplx.core.*; import gplx.core.net.*;
|
||||
public class Http_download_wkr_ {
|
||||
public static Http_download_wkr Proto = new Http_download_wkr__jre();
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
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.downloads; import gplx.*; import gplx.core.*; import gplx.core.net.*;
|
||||
import gplx.core.progs.*;
|
||||
public abstract class Http_download_wkr__base implements Http_download_wkr {
|
||||
private long expd_size;
|
||||
private Io_url tmp_url, checkpoint_url;
|
||||
private long downloaded;
|
||||
private long checkpoint_interval = 1024 * 1024, checkpoint_nxt = 0;
|
||||
public String Fail_msg() {return fail_msg;} private String fail_msg;
|
||||
public abstract Http_download_wkr Make_new();
|
||||
public byte Exec(gplx.core.progs.Gfo_prog_ui prog_ui, String src_str, Io_url trg_url, long expd_size_val) {
|
||||
this.downloaded = this.Checkpoint__load_by_trg_fil(trg_url);
|
||||
this.checkpoint_nxt = downloaded + checkpoint_interval;
|
||||
this.expd_size = expd_size_val;
|
||||
this.fail_msg = null;
|
||||
|
||||
byte status = this.Exec_hook(prog_ui, src_str, tmp_url, downloaded);
|
||||
switch (status) {
|
||||
case Gfo_prog_ui_.Status__done: {
|
||||
if (expd_size_val != -1) {
|
||||
long actl_size = Io_mgr.Instance.QueryFil(tmp_url).Size();
|
||||
if (expd_size != actl_size) {
|
||||
this.fail_msg = String_.Format("bad size: bad={0} good={1}", actl_size, expd_size);
|
||||
return Gfo_prog_ui_.Status__fail;
|
||||
}
|
||||
}
|
||||
Io_mgr.Instance.MoveFil_args(tmp_url, trg_url, true).Exec();
|
||||
this.Exec_cleanup();
|
||||
break;
|
||||
}
|
||||
case Gfo_prog_ui_.Status__suspended:
|
||||
case Gfo_prog_ui_.Status__fail: {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return status;
|
||||
}
|
||||
protected abstract byte Exec_hook(gplx.core.progs.Gfo_prog_ui prog_ui, String src_str, Io_url trg_url, long downloaded);
|
||||
public void Exec_cleanup() {
|
||||
if (tmp_url != null) Io_mgr.Instance.DeleteFil(tmp_url);
|
||||
if (checkpoint_url != null) Io_mgr.Instance.DeleteFil(checkpoint_url);
|
||||
}
|
||||
public long Checkpoint__load_by_trg_fil(Io_url trg_url) {
|
||||
this.tmp_url = trg_url.GenNewExt(".tmp");
|
||||
this.checkpoint_url = trg_url.GenNewExt(".checkpoint");
|
||||
return this.Checkpoint__load();
|
||||
}
|
||||
private long Checkpoint__load() {
|
||||
byte[] data = Io_mgr.Instance.LoadFilBryOrNull(checkpoint_url);
|
||||
return data == null ? 0 : Long_.parse_or(String_.new_a7(data), 0);
|
||||
}
|
||||
public void Checkpoint__save(long new_val) {
|
||||
if (new_val < checkpoint_nxt) return;
|
||||
Io_mgr.Instance.SaveFilStr(checkpoint_url, Long_.To_str(new_val));
|
||||
downloaded = new_val;
|
||||
checkpoint_nxt += checkpoint_interval;
|
||||
}
|
||||
}
|
||||
@@ -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.net.downloads; import gplx.*; import gplx.core.*; import gplx.core.net.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import gplx.core.progs.*;
|
||||
public class Http_download_wkr__jre extends Http_download_wkr__base {
|
||||
public Http_download_wkr Make_new() {return this;}
|
||||
@Override public byte Exec_hook(Gfo_prog_ui prog_ui, String src_url, Io_url trg_url, long downloaded) {
|
||||
long prog_data_cur = downloaded;
|
||||
boolean prog_resumed = prog_data_cur > 0;
|
||||
|
||||
// get trg stream first to handle bad paths / permission errors
|
||||
Io_mgr.Instance.CreateDirIfAbsent(trg_url.OwnerDir());
|
||||
if (prog_resumed)
|
||||
Io_mgr.Instance.Truncate_fil(trg_url, downloaded);
|
||||
File trg_fil = new File(trg_url.Xto_api());
|
||||
FileOutputStream trg_stream = null;
|
||||
try {trg_stream = new FileOutputStream(trg_fil.getPath(), prog_resumed);} // pass true for append
|
||||
catch (FileNotFoundException e) {throw Err_.new_("download_file", "write failed; permission error?", "trg", trg_url, "err", e.toString());}
|
||||
|
||||
// open src stream
|
||||
InputStream src_stream = null;
|
||||
URL src_url_itm = null;
|
||||
try {src_url_itm = new URL(src_url);}
|
||||
catch (MalformedURLException e) {throw Err_.new_("download_file", "bad url", "src", src_url, "err" + e.toString());}
|
||||
HttpURLConnection src_conn = null;
|
||||
try {
|
||||
// open connection
|
||||
src_conn = (HttpURLConnection)src_url_itm.openConnection();
|
||||
if (prog_resumed)
|
||||
src_conn.addRequestProperty("Range", "bytes=" + Long_.To_str(prog_data_cur) + "-");
|
||||
src_conn.connect();
|
||||
|
||||
// check response code
|
||||
int response_code = src_conn.getResponseCode();
|
||||
if (prog_resumed) {
|
||||
if (response_code != HttpURLConnection.HTTP_PARTIAL)
|
||||
throw Err_.new_("download_file", "server returned non-partial response code", "src", src_url, "code", src_conn.getResponseCode(), "msg", src_conn.getResponseMessage());
|
||||
}
|
||||
else {
|
||||
if (response_code != HttpURLConnection.HTTP_OK)
|
||||
throw Err_.new_("download_file", "server returned non-OK response code", "src", src_url, "code", src_conn.getResponseCode(), "msg", src_conn.getResponseMessage());
|
||||
}
|
||||
src_stream = src_conn.getInputStream();
|
||||
} catch (Exception e) {
|
||||
throw Err_.new_("download_file", "src connection failed", "src", src_url, "err",e.toString());
|
||||
}
|
||||
|
||||
// do downloading
|
||||
try {
|
||||
long prog_data_end = prog_ui.Prog_data_end();
|
||||
if (prog_data_end == -1) prog_data_end = src_conn.getContentLength(); // NOTE: may be -1 if server does not report the length
|
||||
byte data[] = new byte[4096];
|
||||
int read = 0;
|
||||
while ((read = src_stream.read(data)) != -1) {
|
||||
prog_data_cur += read;
|
||||
trg_stream.write(data, 0, read);
|
||||
this.Checkpoint__save(prog_data_cur);
|
||||
if (prog_ui.Prog_notify_and_chk_if_suspended(prog_data_cur, prog_data_end)) return Gfo_prog_ui_.Status__suspended;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw Err_.new_("download_file", "downloading failed", "src", src_url, "trg_url", trg_url, "err", e.toString());
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
if (trg_stream != null) trg_stream.close();
|
||||
if (src_stream != null) src_stream.close();
|
||||
} catch (IOException e) {}
|
||||
if (src_conn != null) src_conn.disconnect();
|
||||
}
|
||||
return Gfo_prog_ui_.Status__done;
|
||||
}
|
||||
}
|
||||
24
400_xowa/src/gplx/core/net/emails/Gfo_email_mgr.java
Normal file
24
400_xowa/src/gplx/core/net/emails/Gfo_email_mgr.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.emails; import gplx.*; import gplx.core.*; import gplx.core.net.*;
|
||||
public interface Gfo_email_mgr {
|
||||
void Send(String to, String subject, String body);
|
||||
}
|
||||
class Gfo_email_mgr__noop implements Gfo_email_mgr {
|
||||
public void Send(String to, String subject, String body) {}
|
||||
}
|
||||
@@ -15,7 +15,7 @@ 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.brys; import gplx.*; import gplx.core.*;
|
||||
public interface Bry_bfr_able {
|
||||
void To_bfr(Bry_bfr bfr);
|
||||
package gplx.core.net.emails; import gplx.*; import gplx.core.*; import gplx.core.net.*;
|
||||
public class Gfo_email_mgr_ {
|
||||
public static Gfo_email_mgr Instance = new Gfo_email_mgr__noop();
|
||||
}
|
||||
22
400_xowa/src/gplx/core/net/emails/Gfo_email_mgr__apache.java
Normal file
22
400_xowa/src/gplx/core/net/emails/Gfo_email_mgr__apache.java
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
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.emails; import gplx.*; import gplx.core.*; import gplx.core.net.*;
|
||||
class Gfo_email_mgr__apache implements Gfo_email_mgr {
|
||||
public void Send(String to, String subject, String body) {
|
||||
}
|
||||
}
|
||||
29
400_xowa/src/gplx/core/net/qargs/Gfo_qarg_enum_itm.java
Normal file
29
400_xowa/src/gplx/core/net/qargs/Gfo_qarg_enum_itm.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.net.qargs; import gplx.*; import gplx.core.*; import gplx.core.net.*;
|
||||
public class Gfo_qarg_enum_itm {
|
||||
private final Hash_adp_bry hash = Hash_adp_bry.cs();
|
||||
public Gfo_qarg_enum_itm(String key) {this.key = Bry_.new_u8(key);}
|
||||
public Gfo_qarg_enum_itm(byte[] key) {this.key = key;}
|
||||
public byte[] Key() {return key;} private final byte[] key;
|
||||
public Gfo_qarg_enum_itm Add(String key, int val) {
|
||||
hash.Add_bry_int(Bry_.new_u8(key), val);
|
||||
return this;
|
||||
}
|
||||
public int Get_as_int_or(byte[] val, int or) {return hash.Get_as_int_or(val, or);}
|
||||
}
|
||||
@@ -15,23 +15,15 @@ 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.brys; import gplx.*; import gplx.core.*;
|
||||
public class Bry_bfr_able_ {
|
||||
public static byte[][] To_bry_ary(Bry_bfr tmp_bfr, Bry_bfr_able[] ary) {
|
||||
package gplx.core.net.qargs; import gplx.*; import gplx.core.*; import gplx.core.net.*;
|
||||
public class Gfo_qarg_enum_mgr {
|
||||
private final Hash_adp_bry hash = Hash_adp_bry.cs();
|
||||
public Gfo_qarg_enum_mgr(Gfo_qarg_enum_itm... ary) {
|
||||
int len = ary.length;
|
||||
byte[][] rv = new byte[len][];
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Bry_bfr_able itm = ary[i];
|
||||
if (itm != null) {
|
||||
itm.To_bfr(tmp_bfr);
|
||||
rv[i] = tmp_bfr.To_bry_and_clear();
|
||||
}
|
||||
Gfo_qarg_enum_itm itm = ary[i];
|
||||
hash.Add_bry_obj(itm.Key(), itm);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public static byte[] To_bry_or_null(Bry_bfr tmp_bfr, Bry_bfr_able itm) {
|
||||
if (itm == null) return null;
|
||||
itm.To_bfr(tmp_bfr);
|
||||
return tmp_bfr.To_bry_and_clear();
|
||||
}
|
||||
public Gfo_qarg_enum_itm Get(byte[] key) {return (Gfo_qarg_enum_itm)hash.Get_by_bry(key);}
|
||||
}
|
||||
@@ -15,13 +15,13 @@ 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.*;
|
||||
package gplx.core.net.qargs; import gplx.*; import gplx.core.*; import gplx.core.net.*;
|
||||
public class Gfo_qarg_itm {
|
||||
public Gfo_qarg_itm(byte[] key_bry, byte[] val_bry) {this.key_bry = key_bry; this.val_bry = val_bry;}
|
||||
public byte[] Key_bry() {return key_bry;} private byte[] key_bry;
|
||||
public byte[] Val_bry() {return val_bry;} private byte[] val_bry;
|
||||
public Gfo_qarg_itm Val_bry_(byte[] v) {val_bry = v; return this;}
|
||||
public static final Gfo_qarg_itm[] Ary_empty = new Gfo_qarg_itm[0];
|
||||
public static final Gfo_qarg_itm[] Ary_empty = new Gfo_qarg_itm[0];
|
||||
public static Gfo_qarg_itm new_key_(String key) {return new Gfo_qarg_itm(Bry_.new_u8(key), Bry_.Empty);}
|
||||
public static Gfo_qarg_itm[] Ary(String... kvs) {
|
||||
int len = kvs.length;
|
||||
@@ -38,7 +38,7 @@ public class Gfo_qarg_itm {
|
||||
}
|
||||
public static String To_str(Gfo_qarg_itm[] ary) {
|
||||
int len = ary.length;
|
||||
Bry_bfr bfr = Bry_bfr.new_();
|
||||
Bry_bfr bfr = Bry_bfr_.New();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Gfo_qarg_itm itm = ary[i];
|
||||
bfr.Add(itm.Key_bry()).Add_byte_eq();
|
||||
62
400_xowa/src/gplx/core/net/qargs/Gfo_qarg_mgr.java
Normal file
62
400_xowa/src/gplx/core/net/qargs/Gfo_qarg_mgr.java
Normal file
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
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.qargs; import gplx.*; import gplx.core.*; import gplx.core.net.*;
|
||||
public class Gfo_qarg_mgr {
|
||||
private final Hash_adp_bry hash = Hash_adp_bry.cs();
|
||||
public Gfo_qarg_mgr Init(Gfo_qarg_itm[] args) {
|
||||
hash.Clear();
|
||||
int len = args.length;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Gfo_qarg_itm arg = args[i];
|
||||
hash.Add_bry_obj(arg.Key_bry(), arg);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
public byte[] Read_bry_or_fail(String key) {return Read_bry_or_fail(Bry_.new_u8(key));}
|
||||
public byte[] Read_bry_or_fail(byte[] key) {byte[] rv = Read_bry_or_null(key); if (rv == null) Fail_when_missing(String_.new_u8(key)); return rv;}
|
||||
public byte[] Read_bry_or_empty(byte[] key) {return Read_bry_or(key, Bry_.Empty);}
|
||||
public byte[] Read_bry_or_null(String key) {return Read_bry_or(Bry_.new_u8(key), null);}
|
||||
public byte[] Read_bry_or_null(byte[] key) {return Read_bry_or(key, null);}
|
||||
public byte[] Read_bry_or(byte[] key, byte[] or) {
|
||||
Gfo_qarg_itm arg = (Gfo_qarg_itm)hash.Get_by_bry(key);
|
||||
return arg == null ? or : arg.Val_bry();
|
||||
}
|
||||
public String Read_str_or_fail(String key) {String rv = Read_str_or_null(Bry_.new_u8(key)); if (rv == null) Fail_when_missing(key); return rv;}
|
||||
public String Read_str_or_null(String key) {return Read_str_or_null(Bry_.new_u8(key));}
|
||||
public String Read_str_or_null(byte[] key) {
|
||||
Gfo_qarg_itm arg = (Gfo_qarg_itm)hash.Get_by_bry(key);
|
||||
return arg == null ? null : String_.new_u8(arg.Val_bry());
|
||||
}
|
||||
public int Read_int_or(String key, int or) {return Read_int_or(Bry_.new_u8(key), or);}
|
||||
public int Read_int_or(byte[] key, int or) {
|
||||
byte[] val = Read_bry_or(key, null);
|
||||
return val == null ? or : Int_.parse_or(String_.new_a7(val), or);
|
||||
}
|
||||
public int Read_enm_as_int_or(Gfo_qarg_enum_itm enm, int or) {
|
||||
Gfo_qarg_itm arg = (Gfo_qarg_itm)hash.Get_by_bry(enm.Key());
|
||||
return arg == null ? or : enm.Get_as_int_or(arg.Val_bry(), or);
|
||||
}
|
||||
private void Fail_when_missing(String key) {throw Err_.new_("", "url_arg missing", "key", key);}
|
||||
// if (url_args.Read_enm(Enm_cmd.Itm) == Enm_cmd.Tid__add) {}
|
||||
|
||||
// public int Read_enm_or_neg1(byte[] key) {
|
||||
// Gfo_qarg_enum_itm enm = enm_mgr.Get(key); if (enm == null) return -1;
|
||||
// Gfo_qarg_itm arg = (Gfo_qarg_itm)hash.Get_by_bry(key); if (arg == null) return -1;
|
||||
// return enm.Get_as_int_or(arg.Val_bry(), -1);
|
||||
// }
|
||||
}
|
||||
@@ -15,11 +15,11 @@ 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.*;
|
||||
package gplx.core.net.qargs; import gplx.*; import gplx.core.*; import gplx.core.net.*;
|
||||
import gplx.langs.htmls.encoders.*;
|
||||
public class Gfo_qarg_mgr {
|
||||
private final List_adp list = List_adp_.new_();
|
||||
private final Hash_adp hash = Hash_adp_bry.cs();
|
||||
public class Gfo_qarg_mgr_old {
|
||||
private final List_adp list = List_adp_.New();
|
||||
private final Hash_adp hash = Hash_adp_bry.cs();
|
||||
public int Len() {return list.Count();}
|
||||
public boolean Match(byte[] key, byte[] val) {
|
||||
Gfo_qarg_itm arg = (Gfo_qarg_itm)hash.Get_by(key);
|
||||
@@ -49,7 +49,7 @@ public class Gfo_qarg_mgr {
|
||||
}
|
||||
arg.Val_bry_(val);
|
||||
}
|
||||
public Gfo_qarg_mgr Load(Gfo_qarg_itm[] ary) {
|
||||
public Gfo_qarg_mgr_old Load(Gfo_qarg_itm[] ary) {
|
||||
hash.Clear();
|
||||
list.Clear();
|
||||
int len = ary.length;
|
||||
@@ -72,7 +72,7 @@ public class Gfo_qarg_mgr {
|
||||
}
|
||||
public byte[] To_bry() {
|
||||
int len = list.Count(); if (len == 0) return Bry_.Empty;
|
||||
Bry_bfr bfr = Bry_bfr.new_();
|
||||
Bry_bfr bfr = Bry_bfr_.New();
|
||||
To_bry(bfr, gplx.langs.htmls.encoders.Gfo_url_encoder_.Href, false);
|
||||
return bfr.To_bry_and_clear();
|
||||
}
|
||||
@@ -18,7 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx.core.primitives; import gplx.*; import gplx.core.*;
|
||||
import gplx.core.brys.*;
|
||||
public class Bry_cache {
|
||||
private final Hash_adp hash = Hash_adp_.new_(); private final Bry_obj_ref hash_ref = Bry_obj_ref.New_empty();
|
||||
private final Hash_adp hash = Hash_adp_.New(); private final Bry_obj_ref hash_ref = Bry_obj_ref.New_empty();
|
||||
public byte[] Get_or_new(String v) {return Get_or_new(Bry_.new_u8(v));}
|
||||
public byte[] Get_or_new(byte[] v) {
|
||||
if (v.length == 0) return Bry_.Empty;
|
||||
@@ -31,5 +31,5 @@ public class Bry_cache {
|
||||
else
|
||||
return ((Bry_obj_ref)rv).Val();
|
||||
}
|
||||
public static final Bry_cache Instance = new Bry_cache(); Bry_cache() {}
|
||||
public static final Bry_cache Instance = new Bry_cache(); Bry_cache() {}
|
||||
}
|
||||
|
||||
@@ -17,8 +17,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.primitives; import gplx.*; import gplx.core.*;
|
||||
public class Int_pool {
|
||||
private final List_adp available_list = List_adp_.new_(); private int available_len;
|
||||
// private final Bry_bfr dbg_bfr = Bry_bfr.new_();
|
||||
private final List_adp available_list = List_adp_.New(); private int available_len;
|
||||
// private final Bry_bfr dbg_bfr = Bry_bfr_.New();
|
||||
private int uid_max = -1;
|
||||
public void Clear() {
|
||||
synchronized (available_list) {
|
||||
|
||||
@@ -17,10 +17,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.primitives; import gplx.*; import gplx.core.*;
|
||||
public class Number_parser {
|
||||
public int Rv_as_int() {return (int)int_val;} private long int_val = 0;
|
||||
public Decimal_adp Rv_as_dec() {return dec_val == null ? Decimal_adp_.long_(int_val) : dec_val;} private Decimal_adp dec_val = null;
|
||||
public boolean Has_err() {return has_err;} private boolean has_err;
|
||||
public boolean Has_frac() {return has_frac;} private boolean has_frac;
|
||||
public int Rv_as_int() {return (int)num_val;} private long num_val = 0;
|
||||
public long Rv_as_long() {return num_val;}
|
||||
public Decimal_adp Rv_as_dec() {return dec_val == null ? Decimal_adp_.long_(num_val) : dec_val;} private Decimal_adp dec_val = null;
|
||||
public boolean Is_int() {return dec_val == null && (num_val >= Int_.Min_value && num_val <= Int_.Max_value);}
|
||||
public boolean Has_err() {return has_err;} private boolean has_err;
|
||||
public boolean Has_frac() {return has_frac;} private boolean has_frac;
|
||||
public boolean Hex_enabled() {return hex_enabled;} public Number_parser Hex_enabled_(boolean v) {hex_enabled = v; return this;} private boolean hex_enabled;
|
||||
public Number_parser Ignore_chars_(byte[] v) {this.ignore_chars = v; return this;} private byte[] ignore_chars;
|
||||
public Number_parser Ignore_space_at_end_y_() {this.ignore_space_at_end = true; return this;} private boolean ignore_space_at_end;
|
||||
@@ -31,7 +33,7 @@ public class Number_parser {
|
||||
public Number_parser Parse(byte[] ary, int bgn, int end) {
|
||||
int loop_bgn = end - 1, loop_end = bgn - 1, exp_multiplier = 1, factor = 10;
|
||||
long multiplier = 1, frc_multiplier = 1;
|
||||
int_val = 0; dec_val = null; boolean comma_nil = true;
|
||||
num_val = 0; dec_val = null; boolean comma_nil = true;
|
||||
long frc_int = 0;
|
||||
has_err = false; has_frac = false; boolean has_exp = false, has_neg = false, exp_neg = false, has_plus = false, has_num = false;
|
||||
boolean input_is_hex = false;
|
||||
@@ -64,14 +66,14 @@ public class Number_parser {
|
||||
case Byte_ascii.Num_7:
|
||||
case Byte_ascii.Num_8:
|
||||
case Byte_ascii.Num_9:
|
||||
int_val += (cur - Byte_ascii.Num_0) * multiplier;
|
||||
num_val += (cur - Byte_ascii.Num_0) * multiplier;
|
||||
multiplier *= factor;
|
||||
has_num = true;
|
||||
break;
|
||||
case Byte_ascii.Dot:
|
||||
if (has_frac) return Has_err_y_();
|
||||
frc_int = int_val;
|
||||
int_val = 0;
|
||||
frc_int = num_val;
|
||||
num_val = 0;
|
||||
frc_multiplier = multiplier;
|
||||
multiplier = 1;
|
||||
has_frac = true;
|
||||
@@ -99,15 +101,15 @@ public class Number_parser {
|
||||
case Byte_ascii.Ltr_e:
|
||||
case Byte_ascii.Ltr_E:
|
||||
if (input_is_hex) {
|
||||
int_val += 14 * multiplier; // NOTE: 14=value of e/E
|
||||
num_val += 14 * multiplier; // NOTE: 14=value of e/E
|
||||
multiplier *= factor;
|
||||
has_num = true;
|
||||
}
|
||||
else {
|
||||
if (has_exp) return Has_err_y_();
|
||||
exp_neg = has_neg;
|
||||
exp_multiplier = (int)Math_.Pow(10, int_val);
|
||||
int_val = 0;
|
||||
exp_multiplier = (int)Math_.Pow(10, num_val);
|
||||
num_val = 0;
|
||||
multiplier = 1;
|
||||
has_exp = true;
|
||||
has_neg = false;
|
||||
@@ -120,7 +122,7 @@ public class Number_parser {
|
||||
case Byte_ascii.Ltr_D:
|
||||
case Byte_ascii.Ltr_F:
|
||||
if (input_is_hex) {
|
||||
int_val += (cur - Byte_ascii.Ltr_A + 10) * multiplier;
|
||||
num_val += (cur - Byte_ascii.Ltr_A + 10) * multiplier;
|
||||
multiplier *= factor;
|
||||
has_num = true;
|
||||
}
|
||||
@@ -133,7 +135,7 @@ public class Number_parser {
|
||||
case Byte_ascii.Ltr_d:
|
||||
case Byte_ascii.Ltr_f:
|
||||
if (input_is_hex) {
|
||||
int_val += (cur - Byte_ascii.Ltr_a + 10) * multiplier;
|
||||
num_val += (cur - Byte_ascii.Ltr_a + 10) * multiplier;
|
||||
multiplier *= factor;
|
||||
has_num = true;
|
||||
}
|
||||
@@ -163,7 +165,7 @@ public class Number_parser {
|
||||
}
|
||||
if (!has_num) return Has_err_y_(); // handles situations wherein just symbols; EX: "+", ".", "-.", " , " etc.
|
||||
if (has_frac) {
|
||||
long full_val = (((int_val * frc_multiplier) + frc_int));
|
||||
long full_val = (((num_val * frc_multiplier) + frc_int));
|
||||
if (has_neg) full_val *= -1;
|
||||
if (has_exp) {
|
||||
if (exp_neg) frc_multiplier *= exp_multiplier; // divide, so apply to frc
|
||||
@@ -172,8 +174,12 @@ public class Number_parser {
|
||||
dec_val = Decimal_adp_.divide_(full_val, frc_multiplier);
|
||||
}
|
||||
else {
|
||||
if (has_neg) int_val *= -1;
|
||||
if (has_exp) int_val = exp_neg ? int_val / exp_multiplier : int_val * exp_multiplier;
|
||||
if (has_neg) num_val *= -1;
|
||||
if (has_exp) {
|
||||
num_val = exp_neg
|
||||
? num_val / exp_multiplier
|
||||
: num_val * exp_multiplier;
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx.core.primitives; import gplx.*; import gplx.core.*;
|
||||
import org.junit.*;
|
||||
public class Number_parser_tst {
|
||||
private final Number_parser_fxt fxt = new Number_parser_fxt();
|
||||
private final Number_parser_fxt fxt = new Number_parser_fxt();
|
||||
@Before public void init() {fxt.Clear();}
|
||||
@Test public void Integer() {
|
||||
fxt.Test_int("1", 1);
|
||||
@@ -28,6 +28,9 @@ public class Number_parser_tst {
|
||||
fxt.Test_int("+1", 1);
|
||||
fxt.Test_int("00001", 1);
|
||||
}
|
||||
@Test public void Long() {
|
||||
fxt.Test_long("9876543210", 9876543210L);
|
||||
}
|
||||
@Test public void Decimal() {
|
||||
fxt.Test_dec("1.23", Decimal_adp_.parse("1.23"));
|
||||
fxt.Test_dec("1.023", Decimal_adp_.parse("1.023"));
|
||||
@@ -69,7 +72,7 @@ public class Number_parser_tst {
|
||||
}
|
||||
}
|
||||
class Number_parser_fxt {
|
||||
private final Number_parser parser = new Number_parser();
|
||||
private final Number_parser parser = new Number_parser();
|
||||
public void Clear() {parser.Clear();}
|
||||
public void Init_ignore(String chars) {parser.Ignore_chars_(Bry_.new_a7(chars));}
|
||||
public void Test_int(String raw, int expd) {
|
||||
@@ -77,6 +80,10 @@ class Number_parser_fxt {
|
||||
int actl = parser.Parse(raw_bry, 0, raw_bry.length).Rv_as_int();
|
||||
Tfds.Eq(expd, actl, raw);
|
||||
}
|
||||
public void Test_long(String raw, long expd) {
|
||||
byte[] raw_bry = Bry_.new_a7(raw);
|
||||
Tfds.Eq(expd, parser.Parse(raw_bry, 0, raw_bry.length).Rv_as_long(), raw);
|
||||
}
|
||||
public void Test_dec(String raw, Decimal_adp expd) {
|
||||
byte[] raw_bry = Bry_.new_a7(raw);
|
||||
Decimal_adp actl = parser.Parse(raw_bry, 0, raw_bry.length).Rv_as_dec();
|
||||
|
||||
46
400_xowa/src/gplx/core/progs/rates/Gfo_rate_list.java
Normal file
46
400_xowa/src/gplx/core/progs/rates/Gfo_rate_list.java
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
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.progs.rates; import gplx.*; import gplx.core.*; import gplx.core.progs.*;
|
||||
import gplx.core.lists.rings.*;
|
||||
public class Gfo_rate_list {
|
||||
private final Ring__long ring;
|
||||
public Gfo_rate_list(int size) {
|
||||
this.ring = new Ring__long(size * 2); // *2 to store both data and time
|
||||
}
|
||||
public void Clear() {ring.Clear(); cur_rate = cur_delta = 0;}
|
||||
public double Cur_rate() {return cur_rate;} private double cur_rate = 0;
|
||||
public double Cur_delta() {return cur_delta;} private double cur_delta;
|
||||
public double Add(long data, long time) {
|
||||
ring.Add(data);
|
||||
ring.Add(time);
|
||||
double new_rate = Cur_calc(data, time);
|
||||
cur_delta = cur_rate == 0 ? new_rate : Math_.Abs_double((new_rate - cur_rate) / cur_rate);
|
||||
cur_rate = new_rate;
|
||||
return cur_rate;
|
||||
}
|
||||
private double Cur_calc(long cur_data, long cur_time) {
|
||||
int len = ring.Len();
|
||||
long data_all = 0;
|
||||
long time_all = 0;
|
||||
for (int i = 0; i < len; i += 2) {
|
||||
data_all += ring.Get_at(i);
|
||||
time_all += ring.Get_at(i + 1);
|
||||
}
|
||||
return data_all / (time_all == 0 ? .001 : time_all);
|
||||
}
|
||||
}
|
||||
36
400_xowa/src/gplx/core/progs/rates/Gfo_rate_list_tst.java
Normal file
36
400_xowa/src/gplx/core/progs/rates/Gfo_rate_list_tst.java
Normal file
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
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.progs.rates; import gplx.*; import gplx.core.*; import gplx.core.progs.*;
|
||||
import org.junit.*; import gplx.core.tests.*;
|
||||
public class Gfo_rate_list_tst {
|
||||
private final Gfo_rate_list_fxt fxt = new Gfo_rate_list_fxt();
|
||||
@Before public void init() {fxt.Clear();}
|
||||
@Test public void Add__1() {fxt.Add(100, 20).Test(5, 5);}
|
||||
@Test public void Add__2() {fxt.Add(100, 20).Add(100, 30).Test(4, .20d);}
|
||||
@Test public void Add__3() {fxt.Add(100, 20).Add(100, 30).Add(100, 50).Test(3, .25d);}
|
||||
@Test public void Add__4() {fxt.Add(100, 20).Add(100, 30).Add(100, 50).Add(600, 0).Test(9, 2);}
|
||||
}
|
||||
class Gfo_rate_list_fxt {
|
||||
private final Gfo_rate_list list = new Gfo_rate_list(6);
|
||||
public void Clear() {list.Clear();}
|
||||
public Gfo_rate_list_fxt Add(long data, long time) {list.Add(data, time); return this;}
|
||||
public void Test(double expd_rate, double expd_delta) {
|
||||
Gftest.Eq__double(expd_rate , list.Cur_rate() , "cur_rate");
|
||||
Gftest.Eq__double(expd_delta, list.Cur_delta() , "cur_delta");
|
||||
}
|
||||
}
|
||||
39
400_xowa/src/gplx/core/progs/rates/Gfo_rate_mgr.java
Normal file
39
400_xowa/src/gplx/core/progs/rates/Gfo_rate_mgr.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.progs.rates; import gplx.*; import gplx.core.*; import gplx.core.progs.*;
|
||||
public class Gfo_rate_mgr {
|
||||
private final Hash_adp hash = Hash_adp_.New();
|
||||
private final int dflt_size;
|
||||
public Gfo_rate_mgr(int dflt_size) {this.dflt_size = dflt_size;}
|
||||
public void Clear() {hash.Clear();}
|
||||
public int Len() {return hash.Count();}
|
||||
public Gfo_rate_list Get_or_new(String k) {
|
||||
Gfo_rate_list rv = (Gfo_rate_list)hash.Get_by(k);
|
||||
if (rv == null) {
|
||||
rv = new Gfo_rate_list(dflt_size);
|
||||
rv.Add(1024 * 1024, 1); // add default rate of 1 MB per second
|
||||
hash.Add(k, rv);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public Gfo_rate_mgr Add_new(String key) {
|
||||
Gfo_rate_list rv = new Gfo_rate_list(dflt_size);
|
||||
hash.Add(key, rv);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
31
400_xowa/src/gplx/core/security/files/Cksum_itm.java
Normal file
31
400_xowa/src/gplx/core/security/files/Cksum_itm.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.security.files; import gplx.*; import gplx.core.*; import gplx.core.security.*;
|
||||
public class Cksum_itm implements gplx.core.brys.Bry_bfr_able {
|
||||
public Cksum_itm(byte[] hash, Io_url file_url, long file_size) {
|
||||
this.Hash = hash; this.File_url = file_url; this.File_size = file_size;
|
||||
}
|
||||
public final byte[] Hash;
|
||||
public final Io_url File_url;
|
||||
public final long File_size;
|
||||
public void To_bfr(Bry_bfr bfr) {
|
||||
bfr.Add(Hash).Add_byte_pipe();
|
||||
bfr.Add_str_u8(File_url.Raw()).Add_byte_pipe();
|
||||
bfr.Add_long_variable(File_size);
|
||||
}
|
||||
}
|
||||
84
400_xowa/src/gplx/core/security/files/Cksum_list.java
Normal file
84
400_xowa/src/gplx/core/security/files/Cksum_list.java
Normal file
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
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.security.files; import gplx.*; import gplx.core.*; import gplx.core.security.*;
|
||||
public class Cksum_list {
|
||||
public Cksum_list(byte type, Cksum_itm[] itms, long itms_size) {
|
||||
this.Type = type; this.Itms = itms; this.Itms_size = itms_size;
|
||||
}
|
||||
public final byte Type;
|
||||
public final Cksum_itm[] Itms;
|
||||
public long Itms_size;
|
||||
public Io_url[] Itms_ary() {
|
||||
int len = Itms.length;
|
||||
Io_url[] rv = new Io_url[len];
|
||||
for (int i = 0; i < len; ++i)
|
||||
rv[i] = Itms[i].File_url;
|
||||
return rv;
|
||||
}
|
||||
|
||||
public static Cksum_list Parse_by_fil(Io_url url) {
|
||||
byte tid = Get_hash_tid_by_ext(url.Ext());
|
||||
return Cksum_list.Parse(tid, url.OwnerDir(), Io_mgr.Instance.LoadFilBry(url));
|
||||
}
|
||||
public static Cksum_list Parse(byte type, Io_url owner_dir, byte[] bry) {
|
||||
List_adp list = List_adp_.New();
|
||||
|
||||
byte[][] lines = Bry_split_.Split_lines(bry);
|
||||
int len = lines.length;
|
||||
long itms_size = 0;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
byte[] line = lines[i]; // EX: "d41d8cd98f00b204e9800998ecf8427e *file.txt"
|
||||
|
||||
// get hash
|
||||
int space_pos = Bry_find_.Find_fwd(line, Byte_ascii.Space);
|
||||
if (space_pos == Bry_find_.Not_found) throw Err_.new_("chsum", "checksum line does not have space", "line", line);
|
||||
byte[] hash = Bry_.Mid(line, 0, space_pos);
|
||||
|
||||
// get file
|
||||
int file_bgn = space_pos + 1;
|
||||
if (line[file_bgn] == Byte_ascii.Star) ++file_bgn; // ignore leading *; EX: "*file.txt" -> "file.txt"
|
||||
byte[] file = Bry_.Mid(line, file_bgn);
|
||||
Io_url file_url = GenSubFil_nest(owner_dir, file);
|
||||
long file_size = Io_mgr.Instance.QueryFil(file_url).Size();
|
||||
itms_size += file_size;
|
||||
|
||||
// add to list
|
||||
Cksum_itm itm = new Cksum_itm(hash, file_url, file_size);
|
||||
list.Add(itm);
|
||||
}
|
||||
return new Cksum_list(type, (Cksum_itm[])list.To_ary_and_clear(Cksum_itm.class), itms_size);
|
||||
}
|
||||
private static Io_url GenSubFil_nest(Io_url dir, byte[] src) { // split "a/b/c" or "a\b\c" -> [a, b, c]
|
||||
byte dir_spr = gplx.core.envs.Op_sys.Lnx.Fsys_dir_spr_byte();
|
||||
int dir_pos = Bry_find_.Find_fwd(src, dir_spr);
|
||||
if (dir_pos == Bry_find_.Not_found) {
|
||||
dir_spr = gplx.core.envs.Op_sys.Wnt.Fsys_dir_spr_byte();
|
||||
dir_pos = Bry_find_.Find_fwd(src, dir_spr);
|
||||
if (dir_pos == Bry_find_.Not_found)
|
||||
return dir.GenSubFil(String_.new_u8(src));
|
||||
}
|
||||
byte[][] parts = Bry_split_.Split(src, dir_spr);
|
||||
return dir.GenSubFil_nest(String_.Ary(parts));
|
||||
}
|
||||
private static byte Get_hash_tid_by_ext(String ext) {
|
||||
if (String_.Eq(ext, ".md5")) return Hash_algo_.Tid__md5;
|
||||
else if (String_.Eq(ext, ".sha1")) return Hash_algo_.Tid__sha1;
|
||||
else if (String_.Eq(ext, ".sha256")) return Hash_algo_.Tid__sha2_256;
|
||||
else throw Err_.new_unhandled_default(ext);
|
||||
}
|
||||
}
|
||||
50
400_xowa/src/gplx/core/security/files/Cksum_list_tst.java
Normal file
50
400_xowa/src/gplx/core/security/files/Cksum_list_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.security.files; import gplx.*; import gplx.core.*; import gplx.core.security.*;
|
||||
import org.junit.*; import gplx.core.tests.*;
|
||||
public class Cksum_list_tst {
|
||||
private final Cksum_list_fxt fxt = new Cksum_list_fxt();
|
||||
@Test public void Basic() {
|
||||
fxt.Init__file("a.txt").Init__file("ab.txt");
|
||||
fxt.Test__parse(String_.Concat_lines_nl_skip_last
|
||||
( "a5e54d1fd7bb69a228ef0dcd2431367e *a.txt"
|
||||
, "90f15b7ca11bd4c70d9047cd29a80040 *ab.txt"
|
||||
), 11
|
||||
, fxt.Make__itm("a5e54d1fd7bb69a228ef0dcd2431367e", "a.txt", 5)
|
||||
, fxt.Make__itm("90f15b7ca11bd4c70d9047cd29a80040", "ab.txt", 6)
|
||||
);
|
||||
}
|
||||
}
|
||||
class Cksum_list_fxt {
|
||||
private final Io_url dir = Io_url_.mem_dir_("mem/dir/");
|
||||
public Cksum_list_fxt() {
|
||||
Io_mgr.Instance.InitEngine_mem();
|
||||
}
|
||||
public Cksum_list_fxt Init__file(String fil_name) {
|
||||
Io_url fil_url = dir.GenSubFil(fil_name);
|
||||
Io_mgr.Instance.SaveFilStr(fil_url, fil_name);
|
||||
return this;
|
||||
}
|
||||
public Cksum_itm Make__itm(String hash, String file_name, long size) {return new Cksum_itm(Bry_.new_u8(hash), dir.GenSubFil(file_name), size);}
|
||||
public Cksum_list_fxt Test__parse(String raw, long expd_size, Cksum_itm... expd_itms) {
|
||||
Cksum_list actl_list = Cksum_list.Parse(gplx.core.security.Hash_algo_.Tid__md5, dir, Bry_.new_u8(raw));
|
||||
Gftest.Eq__long(expd_size, actl_list.Itms_size);
|
||||
Gftest.Eq__ary(expd_itms, actl_list.Itms);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -1,126 +0,0 @@
|
||||
/*
|
||||
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.tests; import gplx.*; import gplx.core.*;
|
||||
import gplx.core.brys.*;
|
||||
public class Gftest {
|
||||
private static final Bry_bfr bfr = Bry_bfr.new_();
|
||||
public static void Eq__ary(String[] expd, byte[][] actl, String msg_fmt, Object... msg_args) {Eq__ary(Bry_.Ary(expd), actl, msg_fmt, msg_args);}
|
||||
public static void Eq__ary(Bry_bfr_able[] expd_ary, Bry_bfr_able[] actl_ary) {Eq__ary(expd_ary, actl_ary, null);}
|
||||
public static void Eq__ary(Bry_bfr_able[] expd_ary, Bry_bfr_able[] actl_ary, String msg_fmt, Object... msg_args) {
|
||||
Eq__ary(Bry_bfr_able_.To_bry_ary(bfr, expd_ary), Bry_bfr_able_.To_bry_ary(bfr, actl_ary), msg_fmt, msg_args);
|
||||
}
|
||||
public static void Eq__ary(byte[][] expd_bry_ary, byte[][] actl_bry_ary, String msg_fmt, Object... msg_args) {
|
||||
boolean[] failures = Calc__failures(Type_adp_.Tid__bry, expd_bry_ary, actl_bry_ary);
|
||||
if (failures != null) {
|
||||
Write_fail_head(bfr, msg_fmt, msg_args);
|
||||
Write_fail_ary(bfr, failures, Type_adp_.Tid__bry, expd_bry_ary, actl_bry_ary);
|
||||
throw Err_.new_wo_type(bfr.To_str_and_clear());
|
||||
}
|
||||
}
|
||||
public static void Eq__str(String expd, String actl) {Eq__str(expd, actl, null);}
|
||||
public static void Eq__str(String expd, String actl, String msg_fmt, Object... msg_args) {
|
||||
if (String_.Eq(expd, actl)) return;
|
||||
Write_fail_head(bfr, msg_fmt, msg_args);
|
||||
bfr.Add_str_a7("expd: ").Add_str_u8(expd).Add_byte_nl();
|
||||
bfr.Add_str_a7("actl: ").Add_str_u8(actl).Add_byte_nl();
|
||||
bfr.Add(Bry__line_end);
|
||||
throw Err_.new_wo_type(bfr.To_str_and_clear());
|
||||
}
|
||||
public static void Eq__bry(byte[] expd, byte[] actl) {Eq__bry(expd, actl, null);}
|
||||
public static void Eq__bry(byte[] expd, byte[] actl, String msg_fmt, Object... msg_args) {
|
||||
if (Bry_.Eq(expd, actl)) return;
|
||||
Write_fail_head(bfr, msg_fmt, msg_args);
|
||||
bfr.Add_str_a7("expd: ").Add(expd).Add_byte_nl();
|
||||
bfr.Add_str_a7("actl: ").Add(actl).Add_byte_nl();
|
||||
bfr.Add(Bry__line_end);
|
||||
throw Err_.new_wo_type(bfr.To_str_and_clear());
|
||||
}
|
||||
private static void Write_fail_head(Bry_bfr bfr, String msg_fmt, Object[] msg_args) {
|
||||
bfr.Add(Bry__line_bgn);
|
||||
if (msg_fmt != null) {
|
||||
bfr.Add_str_u8(String_.Format(msg_fmt, msg_args));
|
||||
bfr.Add(Bry__line_mid);
|
||||
}
|
||||
}
|
||||
private static void Write_fail_ary(Bry_bfr bfr, boolean[] failures, int type_id, Object expd_ary, Object actl_ary) {
|
||||
int len = failures.length;
|
||||
int expd_len = Array_.Len(expd_ary);
|
||||
int actl_len = Array_.Len(actl_ary);
|
||||
for (int i = 0; i < len; ++i) {
|
||||
boolean failure = failures[i];
|
||||
int pad_len = 5 - Int_.DigitCount(i);
|
||||
bfr.Add_int_pad_bgn(Byte_ascii.Num_0, pad_len, i).Add_byte_colon().Add_byte_space();
|
||||
Write__itm(bfr, type_id, expd_ary, expd_len, i);
|
||||
if (failure) {
|
||||
bfr.Add(Bry__item__eq_n).Add_byte_repeat(Byte_ascii.Space, pad_len - 1);
|
||||
Write__itm(bfr, type_id, actl_ary, actl_len, i);
|
||||
}
|
||||
}
|
||||
bfr.Add(Bry__line_end);
|
||||
}
|
||||
private static void Write__itm(Bry_bfr bfr, int type_id, Object ary, int len, int idx) {
|
||||
if (idx < len) {
|
||||
switch (type_id) {
|
||||
case Type_adp_.Tid__bry: bfr.Add((byte[])Array_.Get_at(ary, idx)); break;
|
||||
default: throw Err_.new_unhandled_default(type_id);
|
||||
}
|
||||
}
|
||||
else
|
||||
bfr.Add(Bry__null);
|
||||
bfr.Add_byte_nl();
|
||||
}
|
||||
private static boolean[] Calc__failures(int tid, Object expd_ary, Object actl_ary) {
|
||||
int expd_len = Array_.Len(expd_ary);
|
||||
int actl_len = Array_.Len(actl_ary);
|
||||
int max_len = expd_len > actl_len ? expd_len : actl_len; if (max_len == 0) return null;
|
||||
boolean[] rv = null;
|
||||
for (int i = 0; i < max_len; ++i) {
|
||||
Object expd_obj = i < expd_len ? Array_.Get_at(expd_ary, i) : null;
|
||||
Object actl_obj = i < actl_len ? Array_.Get_at(actl_ary, i) : null;
|
||||
boolean eq = false;
|
||||
switch (tid) {
|
||||
case Type_adp_.Tid__bry: eq = Bry_.Eq((byte[])expd_obj, (byte[])actl_obj); break;
|
||||
}
|
||||
if (!eq) {
|
||||
if (rv == null) {
|
||||
rv = new boolean[max_len];
|
||||
}
|
||||
rv[i] = true;
|
||||
}
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
private static final byte[] Bry__item__eq_n = Bry_.new_a7("!= ") // Bry__item__eq_y = Bry_.new_a7("== "),
|
||||
, Bry__null = Bry_.new_a7("<<NULL>>")
|
||||
, Bry__line_bgn = Bry_.new_a7("\n************************************************************************************************\n")
|
||||
, Bry__line_mid = Bry_.new_a7("\n------------------------------------------------------------------------------------------------\n")
|
||||
, Bry__line_end = Bry_.new_a7( "________________________________________________________________________________________________")
|
||||
;
|
||||
}
|
||||
/*
|
||||
package ns;
|
||||
import org.junit.*; import gplx.core.tests.*;
|
||||
public class Cls1_tst {
|
||||
private final Cls1_fxt fxt = new Cls1_fxt();
|
||||
@Test public void Basic() {}
|
||||
}
|
||||
class Cls1_fxt {
|
||||
private final Cls1 mgr = new Cls1();
|
||||
public Cls1_fxt Test() {return this;}
|
||||
}
|
||||
*/
|
||||
@@ -19,7 +19,7 @@ package gplx.core.tests; import gplx.*; import gplx.core.*;
|
||||
import gplx.core.strings.*;
|
||||
public class Tst_mgr {
|
||||
public Tst_mgr ThrowError_n_() {throwError = false; return this;} private boolean throwError = true;
|
||||
public List_adp Results() {return results;} List_adp results = List_adp_.new_();
|
||||
public List_adp Results() {return results;} List_adp results = List_adp_.New();
|
||||
public Keyval_hash Vars() {return vars;} Keyval_hash vars = new Keyval_hash();
|
||||
public Object Vars_get_by_key(String key) {return vars.Get_val_or(key, null);}
|
||||
public String Vars_get_bry_as_str(String key, int bgn, int end) {
|
||||
@@ -107,7 +107,7 @@ public class Tst_mgr {
|
||||
return sb.To_str_and_clear();
|
||||
}
|
||||
int Max(int max, String s) {int len = String_.Len(s); return len > max ? len : max;}
|
||||
public static final Tst_chkr Null_chkr = new Tst_chkr_null();
|
||||
public static final Tst_chkr Null_chkr = new Tst_chkr_null();
|
||||
}
|
||||
class Tst_itm {
|
||||
public boolean Pass() {return pass;} private boolean pass;
|
||||
|
||||
@@ -16,9 +16,9 @@ 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.threads; import gplx.*; import gplx.core.*;
|
||||
public class Gfo_async_cmd_itm implements GfoInvkAble {
|
||||
private GfoInvkAble invk; private String invk_key; private GfoMsg msg = GfoMsg_.new_cast_("");
|
||||
public Gfo_async_cmd_itm Init(GfoInvkAble invk, String invk_key, Object... args) {
|
||||
public class Gfo_async_cmd_itm implements Gfo_invk {
|
||||
private Gfo_invk invk; private String invk_key; private GfoMsg msg = GfoMsg_.new_cast_("");
|
||||
public Gfo_async_cmd_itm Init(Gfo_invk invk, String invk_key, Object... args) {
|
||||
this.invk = invk; this.invk_key = invk_key;
|
||||
msg.Args_reset();
|
||||
msg.Clear();
|
||||
@@ -31,12 +31,12 @@ public class Gfo_async_cmd_itm implements GfoInvkAble {
|
||||
return this;
|
||||
}
|
||||
public void Exec() {
|
||||
GfoInvkAble_.InvkCmd_msg(invk, invk_key, msg);
|
||||
Gfo_invk_.Invk_by_msg(invk, invk_key, msg);
|
||||
}
|
||||
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
|
||||
if (ctx.Match(k, Invk_exec)) Exec();
|
||||
else return GfoInvkAble_.Rv_unhandled;
|
||||
else return Gfo_invk_.Rv_unhandled;
|
||||
return this;
|
||||
} private static final String Invk_exec = "exec";
|
||||
public static final Gfo_async_cmd_itm[] Ary_empty = new Gfo_async_cmd_itm[0];
|
||||
public static final Gfo_async_cmd_itm[] Ary_empty = new Gfo_async_cmd_itm[0];
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ class Gfo_async_cmd_mkr {
|
||||
// used = (Gfo_async_cmd_itm[])Array_.Resize(used, v);
|
||||
// ary_len = v;
|
||||
// }
|
||||
public Gfo_async_cmd_itm Get(GfoInvkAble invk, String invk_key, Object... args) {
|
||||
public Gfo_async_cmd_itm Get(Gfo_invk invk, String invk_key, Object... args) {
|
||||
Gfo_async_cmd_itm rv = new Gfo_async_cmd_itm();
|
||||
rv.Init(invk, invk_key, args);
|
||||
return rv;
|
||||
|
||||
@@ -17,11 +17,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.threads; import gplx.*; import gplx.core.*;
|
||||
import gplx.core.primitives.*;
|
||||
public class Gfo_async_mgr implements GfoInvkAble {
|
||||
private List_adp queue = List_adp_.new_();
|
||||
public class Gfo_async_mgr implements Gfo_invk {
|
||||
private List_adp queue = List_adp_.New();
|
||||
private Bool_obj_ref running = Bool_obj_ref.n_();
|
||||
private Gfo_async_cmd_mkr cmd_mkr = new Gfo_async_cmd_mkr();
|
||||
public void Queue(GfoInvkAble invk, String invk_key, Object... args) {
|
||||
public void Queue(Gfo_invk invk, String invk_key, Object... args) {
|
||||
Gfo_async_cmd_itm cmd = cmd_mkr.Get(invk, invk_key, args);
|
||||
synchronized (queue) {
|
||||
queue.Add(cmd);
|
||||
@@ -29,7 +29,7 @@ public class Gfo_async_mgr implements GfoInvkAble {
|
||||
synchronized (running) {
|
||||
if (running.Val_n()) {
|
||||
running.Val_y_();
|
||||
gplx.core.threads.Thread_adp_.invk_(Invk_run, this, Invk_run).Start();
|
||||
Thread_adp_.Start_by_key(Invk_run, this, Invk_run);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -52,7 +52,7 @@ public class Gfo_async_mgr implements GfoInvkAble {
|
||||
}
|
||||
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
|
||||
if (ctx.Match(k, Invk_run)) Run();
|
||||
else return GfoInvkAble_.Rv_unhandled;
|
||||
else return Gfo_invk_.Rv_unhandled;
|
||||
return this;
|
||||
} private static final String Invk_run = "run";
|
||||
}
|
||||
|
||||
@@ -16,7 +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.threads; import gplx.*; import gplx.core.*;
|
||||
public interface Gfo_thread_cmd extends GfoInvkAble {
|
||||
public interface Gfo_thread_cmd extends Gfo_invk {
|
||||
void Cmd_ctor();
|
||||
String Async_key();
|
||||
int Async_sleep_interval();
|
||||
|
||||
@@ -17,12 +17,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.threads; import gplx.*; import gplx.core.*;
|
||||
import gplx.core.brys.fmtrs.*;
|
||||
import gplx.gfui.*;
|
||||
import gplx.gfui.*; import gplx.gfui.kits.core.*;
|
||||
public class Gfo_thread_cmd_base implements Gfo_thread_cmd {
|
||||
@gplx.Virtual public String Async_key() {return "undefined";}
|
||||
public void Cmd_ctor() {}
|
||||
public Gfo_thread_cmd_base Ctor(Gfo_usr_dlg usr_dlg, Gfui_kit kit) {this.usr_dlg = usr_dlg; this.kit = kit; return this;} protected Gfo_usr_dlg usr_dlg; protected Gfui_kit kit;
|
||||
public GfoInvkAble Owner() {return owner;} public Gfo_thread_cmd_base Owner_(GfoInvkAble v) {owner = v; return this;} GfoInvkAble owner;
|
||||
public Gfo_invk Owner() {return owner;} public Gfo_thread_cmd_base Owner_(Gfo_invk v) {owner = v; return this;} Gfo_invk owner;
|
||||
public Bry_fmtr_eval_mgr Url_eval_mgr() {return url_eval_mgr;} public Gfo_thread_cmd_base Url_eval_mgr_(Bry_fmtr_eval_mgr v) {url_eval_mgr = v; return this;} Bry_fmtr_eval_mgr url_eval_mgr;
|
||||
public Gfo_thread_cmd Async_next_cmd() {return next_cmd;} public void Async_next_cmd_(Gfo_thread_cmd v) {next_cmd = v;} Gfo_thread_cmd next_cmd;
|
||||
@gplx.Virtual public int Async_sleep_interval() {return Gfo_thread_cmd_.Async_sleep_interval_1_second;}
|
||||
@@ -36,7 +36,7 @@ public class Gfo_thread_cmd_base implements Gfo_thread_cmd {
|
||||
@gplx.Virtual public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
|
||||
if (ctx.Match(k, Invk_owner)) return owner;
|
||||
else if (ctx.Match(k, Invk_async_bgn)) Async_bgn();
|
||||
else return GfoInvkAble_.Rv_unhandled;
|
||||
else return Gfo_invk_.Rv_unhandled;
|
||||
return this;
|
||||
} private static final String Invk_owner = "owner", Invk_async_bgn = "async_bgn";
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.threads; import gplx.*; import gplx.core.*;
|
||||
import gplx.core.brys.fmtrs.*;
|
||||
import gplx.gfui.*;
|
||||
import gplx.gfui.*; import gplx.gfui.kits.core.*;
|
||||
public class Gfo_thread_cmd_download implements Gfo_thread_cmd {
|
||||
public Gfo_thread_cmd Ctor(Gfo_usr_dlg usr_dlg, Gfui_kit kit) {this.usr_dlg = usr_dlg; this.kit = kit; xrg.Prog_dlg_(usr_dlg); return this;}
|
||||
public Gfo_thread_cmd_download Init(String prog_fmt_hdr, String src, Io_url trg) {
|
||||
@@ -25,7 +25,7 @@ public class Gfo_thread_cmd_download implements Gfo_thread_cmd {
|
||||
xrg.Prog_fmt_hdr_(prog_fmt_hdr).Init(src, trg);
|
||||
return this;
|
||||
} String src; protected Gfui_kit kit; Gfo_usr_dlg usr_dlg; Io_url trg;
|
||||
public GfoInvkAble Owner() {return owner;} public Gfo_thread_cmd_download Owner_(GfoInvkAble v) {owner = v; return this;} GfoInvkAble owner;
|
||||
public Gfo_invk Owner() {return owner;} public Gfo_thread_cmd_download Owner_(Gfo_invk v) {owner = v; return this;} Gfo_invk owner;
|
||||
public Bry_fmtr_eval_mgr Url_eval_mgr() {return url_eval_mgr;} public Gfo_thread_cmd_download Url_eval_mgr_(Bry_fmtr_eval_mgr v) {url_eval_mgr = v; return this;} Bry_fmtr_eval_mgr url_eval_mgr;
|
||||
public void Cmd_ctor() {}
|
||||
public Gfo_thread_cmd Async_next_cmd() {return next_cmd;} public void Async_next_cmd_(Gfo_thread_cmd v) {next_cmd = v;} Gfo_thread_cmd next_cmd;
|
||||
@@ -51,7 +51,7 @@ public class Gfo_thread_cmd_download implements Gfo_thread_cmd {
|
||||
}
|
||||
public void Async_prog_run(int async_sleep_sum) {}
|
||||
public boolean Async_running() {return xrg.Prog_running();}
|
||||
public void Async_run() {Thread_adp_.invk_(gplx.xowa.apps.Xoa_thread_.Key_bldr_download, this, Invk_async_bgn).Start();}
|
||||
public void Async_run() {Thread_adp_.Start_by_key(gplx.xowa.apps.Xoa_thread_.Key_bldr_download, this, Invk_async_bgn);}
|
||||
private void Download() {
|
||||
download_pass = true;
|
||||
if (!xrg.Exec()) {
|
||||
@@ -66,7 +66,7 @@ public class Gfo_thread_cmd_download implements Gfo_thread_cmd {
|
||||
else if (ctx.Match(k, Invk_owner)) return owner;
|
||||
else if (ctx.Match(k, Invk_src_)) src = m.ReadStr("v");
|
||||
else if (ctx.Match(k, Invk_trg_)) trg = Bry_fmtr_eval_mgr_.Eval_url(url_eval_mgr, m.ReadBry("v"));
|
||||
else return GfoInvkAble_.Rv_unhandled;
|
||||
else return Gfo_invk_.Rv_unhandled;
|
||||
return this;
|
||||
} private static final String Invk_async_bgn = "async_bgn", Invk_owner = "owner", Invk_src_ = "src_", Invk_trg_ = "trg_";
|
||||
static final String GRP_KEY = "gfo.thread.file.download";
|
||||
|
||||
@@ -17,13 +17,13 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.threads; import gplx.*; import gplx.core.*;
|
||||
import gplx.core.brys.fmtrs.*;
|
||||
import gplx.gfui.*;
|
||||
import gplx.gfui.*; import gplx.gfui.kits.core.*;
|
||||
public class Gfo_thread_cmd_replace implements Gfo_thread_cmd {
|
||||
public Gfo_thread_cmd Init(Gfo_usr_dlg usr_dlg, Gfui_kit kit, Io_url fil) {
|
||||
this.usr_dlg = usr_dlg; this.kit = kit; this.fil = fil;
|
||||
return this;
|
||||
} Gfui_kit kit; Gfo_usr_dlg usr_dlg; Io_url fil;
|
||||
public GfoInvkAble Owner() {return owner;} public Gfo_thread_cmd_replace Owner_(GfoInvkAble v) {owner = v; return this;} GfoInvkAble owner;
|
||||
public Gfo_invk Owner() {return owner;} public Gfo_thread_cmd_replace Owner_(Gfo_invk v) {owner = v; return this;} Gfo_invk owner;
|
||||
public Bry_fmtr_eval_mgr Url_eval_mgr() {return url_eval_mgr;} public Gfo_thread_cmd_replace Url_eval_mgr_(Bry_fmtr_eval_mgr v) {url_eval_mgr = v; return this;} Bry_fmtr_eval_mgr url_eval_mgr;
|
||||
public String Async_key() {return KEY;}
|
||||
public void Cmd_ctor() {}
|
||||
@@ -48,13 +48,13 @@ public class Gfo_thread_cmd_replace implements Gfo_thread_cmd {
|
||||
Io_mgr.Instance.SaveFilStr(fil, raw);
|
||||
usr_dlg.Prog_many(GRP_KEY, "done", "replace completed: ~{0} ~{1}", fil.Raw(), pairs_len);
|
||||
}
|
||||
public List_adp pairs = List_adp_.new_();
|
||||
public List_adp pairs = List_adp_.New();
|
||||
@gplx.Virtual public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
|
||||
if (ctx.Match(k, Invk_async_bgn)) Exec_find_replace();
|
||||
else if (ctx.Match(k, Invk_owner)) return owner;
|
||||
else if (ctx.Match(k, Invk_fil_)) fil = Bry_fmtr_eval_mgr_.Eval_url(url_eval_mgr, m.ReadBry("v"));
|
||||
else if (ctx.Match(k, Invk_add)) pairs.Add(Keyval_.new_(m.ReadStr("find"), m.ReadStr("replace")));
|
||||
else return GfoInvkAble_.Rv_unhandled;
|
||||
else return Gfo_invk_.Rv_unhandled;
|
||||
return this;
|
||||
} private static final String Invk_async_bgn = "async_bgn", Invk_owner = "owner", Invk_fil_ = "fil_", Invk_add = "add";
|
||||
static final String GRP_KEY = "gfo.thread.file.download";
|
||||
|
||||
@@ -17,14 +17,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.threads; import gplx.*; import gplx.core.*;
|
||||
import gplx.core.brys.fmtrs.*; import gplx.core.envs.*;
|
||||
import gplx.gfui.*; import gplx.xowa.bldrs.cmds.utils.*;
|
||||
import gplx.gfui.*; import gplx.gfui.kits.core.*; import gplx.xowa.bldrs.cmds.utils.*;
|
||||
public class Gfo_thread_cmd_unzip implements Gfo_thread_cmd {
|
||||
public Gfo_thread_cmd_unzip Init(Gfo_usr_dlg usr_dlg, Gfui_kit kit, Process_adp bzip2_process, Process_adp zip_process, Process_adp gz_process, Io_url src, Io_url trg) {
|
||||
this.src = src; this.trg = trg; this.kit = kit; this.usr_dlg = usr_dlg;
|
||||
unzip_wkr = new Xob_unzip_wkr().Init(bzip2_process, zip_process, gz_process).Process_run_mode_(Process_adp.Run_mode_async);
|
||||
return this;
|
||||
} private Io_url src, trg; private Gfui_kit kit; private Gfo_usr_dlg usr_dlg; private Xob_unzip_wkr unzip_wkr;
|
||||
public GfoInvkAble Owner() {return owner;} public Gfo_thread_cmd_unzip Owner_(GfoInvkAble v) {owner = v; return this;} GfoInvkAble owner;
|
||||
public Gfo_invk Owner() {return owner;} public Gfo_thread_cmd_unzip Owner_(Gfo_invk v) {owner = v; return this;} Gfo_invk owner;
|
||||
public void Cmd_ctor() {}
|
||||
@gplx.Virtual public String Async_key() {return KEY;}
|
||||
public Gfo_thread_cmd Async_next_cmd() {return next_cmd;} public void Async_next_cmd_(Gfo_thread_cmd v) {next_cmd = v;} Gfo_thread_cmd next_cmd;
|
||||
@@ -102,7 +102,7 @@ public class Gfo_thread_cmd_unzip implements Gfo_thread_cmd {
|
||||
else if (ctx.Match(k, Invk_rename_dir_)) rename_dir = m.ReadYn("v");
|
||||
else if (ctx.Match(k, Invk_delete_trg_if_exists_)) delete_trg_if_exists = m.ReadYn("v");
|
||||
else if (ctx.Match(k, Invk_term_cmd_for_src_)) term_cmd_for_src = Term_cmd_for_src_parse_(m.ReadStr("v"));
|
||||
else return GfoInvkAble_.Rv_unhandled;
|
||||
else return Gfo_invk_.Rv_unhandled;
|
||||
return this;
|
||||
} private static final String Invk_owner = "owner", Invk_src_ = "src_", Invk_trg_ = "trg_", Invk_rename_dir_ = "rename_dir_", Invk_delete_trg_if_exists_ = "delete_trg_if_exists_", Invk_term_cmd_for_src_ = "term_cmd_for_src_";
|
||||
private static byte Term_cmd_for_src_parse_(String s) {
|
||||
|
||||
@@ -16,9 +16,9 @@ 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.threads; import gplx.*; import gplx.core.*;
|
||||
public class Gfo_thread_pool implements GfoInvkAble {
|
||||
public class Gfo_thread_pool implements Gfo_invk {
|
||||
private Object thread_lock = new Object();
|
||||
private List_adp queue = List_adp_.new_();
|
||||
private List_adp queue = List_adp_.New();
|
||||
private GfoMsg run_msg = GfoMsg_.new_cast_(Invk_run_wkr);
|
||||
private boolean running = false;
|
||||
public Gfo_usr_dlg Usr_dlg() {return usr_dlg;} public Gfo_thread_pool Usr_dlg_(Gfo_usr_dlg v) {usr_dlg = v; return this;} private Gfo_usr_dlg usr_dlg = Gfo_usr_dlg_.Noop;
|
||||
@@ -41,21 +41,21 @@ public class Gfo_thread_pool implements GfoInvkAble {
|
||||
running = true;
|
||||
wkr = (Gfo_thread_wkr)List_adp_.Pop_first(queue);
|
||||
}
|
||||
Thread_adp_.Run_invk_msg(wkr.Name(), this, run_msg.Clear().Add("v", wkr));
|
||||
Thread_adp_.Start_by_msg(wkr.Thread__name(), this, run_msg.Clear().Add("v", wkr));
|
||||
}
|
||||
private void Run_wkr(Gfo_thread_wkr wkr) {
|
||||
try {wkr.Exec();}
|
||||
try {wkr.Thread__exec();}
|
||||
catch (Exception e) {
|
||||
usr_dlg.Warn_many("", "", "uncaught exception while running thread; name=~{0} err=~{1}", wkr.Name(), Err_.Message_gplx_full(e));
|
||||
usr_dlg.Warn_many("", "", "uncaught exception while running thread; name=~{0} err=~{1}", wkr.Thread__name(), Err_.Message_gplx_full(e));
|
||||
}
|
||||
finally {
|
||||
if (wkr.Resume())
|
||||
if (wkr.Thread__resume())
|
||||
this.Resume();
|
||||
}
|
||||
}
|
||||
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
|
||||
if (ctx.Match(k, Invk_run_wkr)) Run_wkr((Gfo_thread_wkr)m.ReadObj("v"));
|
||||
else return GfoInvkAble_.Rv_unhandled;
|
||||
else return Gfo_invk_.Rv_unhandled;
|
||||
return this;
|
||||
}
|
||||
private static final String Invk_run_wkr = "run_wkr";
|
||||
|
||||
@@ -17,7 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.threads; import gplx.*; import gplx.core.*;
|
||||
public interface Gfo_thread_wkr {
|
||||
String Name();
|
||||
void Exec();
|
||||
boolean Resume();
|
||||
String Thread__name();
|
||||
boolean Thread__resume();
|
||||
void Thread__exec();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user