1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2026-03-02 03:49:30 +00:00
This commit is contained in:
gnosygnu
2014-06-30 00:04:32 -04:00
parent 85594d3cdd
commit bae88e739c
2482 changed files with 198730 additions and 0 deletions

View 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.xowa; import gplx.*;
import gplx.xowa.gui.views.*;
public class Xoa_cur implements GfoInvkAble {
public Xoa_cur(Xoa_app app) {this.app = app;} private Xoa_app app;
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
if (ctx.Match(k, Invk_wiki)) {
Xog_win_itm win = app.Gui_mgr().Browser_win();
return win.Active_tab() == null ? GfoInvkAble_.Null : win.Active_page().Wiki(); // null check when called from mass html gen; DATE:2014-06-04
}
else if (ctx.Match(k, Invk_win)) return app.Gui_mgr().Browser_win();
else if (ctx.Match(k, Invk_user)) return app.User();
else return GfoInvkAble_.Rv_unhandled;
} private static final String Invk_wiki = "wiki", Invk_win = "win", Invk_user = "user";
}

View File

@@ -0,0 +1,38 @@
/*
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.xowa; import gplx.*;
public class Xoa_hive_mgr {
public Xoa_hive_mgr(Xoa_app app) {this.app = app;} private Xoa_app app;
public Xob_xdat_itm Itm() {return xdat_itm;}
public int Find_fil(Io_url hive_root, byte[] ttl) {
Io_url hive_url = hive_root.GenSubFil(Xow_dir_info_.Name_reg_fil);
if (!hive_url.Eq(regy_mgr.Fil()))
regy_mgr.Init(hive_url);
return regy_mgr.Files_find(ttl);
} private Xowd_regy_mgr regy_mgr = new Xowd_regy_mgr(); Int_obj_ref bry_len = Int_obj_ref.zero_(); Xob_xdat_file xdat_rdr = new Xob_xdat_file(); Xob_xdat_itm xdat_itm = new Xob_xdat_itm();
public Xowd_regy_mgr Regy_mgr() {return regy_mgr;}
public Xob_xdat_file Get_rdr(Io_url hive_root, byte[] fil_ext_bry, int fil_idx) {
Bry_bfr tmp_bfr = app.Utl_bry_bfr_mkr().Get_m001();
byte[] tmp_bry = tmp_bfr.Bfr(); bry_len.Val_zero_();
Io_url file = Xow_fsys_mgr.Url_fil(hive_root, fil_idx, fil_ext_bry);
tmp_bry = Io_mgr._.LoadFilBry_reuse(file, tmp_bry, bry_len);
xdat_rdr.Clear().Parse(tmp_bry, bry_len.Val(), file);
tmp_bfr.Mkr_rls().Clear();
return xdat_rdr;
}
}

View File

@@ -0,0 +1,86 @@
/*
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.xowa; import gplx.*;
public class Xoa_sys_cfg implements GfoInvkAble {
private Xoa_app app;
public Xoa_sys_cfg(Xoa_app app) {this.app = app;}
public String Launch_url() {return launch_url;} public Xoa_sys_cfg Launch_url_(String v) {launch_url = v; return this;} private String launch_url = Launch_url_dflt; public static final String Launch_url_dflt = "home/wiki/Main_Page";
public byte[] Lang() {return lang_key;}
public Xoa_sys_cfg Lang_(byte[] v) {
lang_key = Xol_lang_itm_.Get_by_key_or_en(v).Key();
if (app.Stage() == gplx.xowa.apps.Xoa_stage_.Tid_launch) { // do not update user lang unless launched; DATE:2014-05-26
Xol_lang lang = app.Lang_mgr().Get_by_key_or_load(lang_key);
app.User().Lang_(lang);
app.User().Wiki().Html_mgr().Portal_mgr().Init();
app.Gui_mgr().Lang_changed(lang);
}
return this;
} private byte[] lang_key = Xol_lang_.Key_en;
public int Options_version() {return options_version;} public Xoa_sys_cfg Options_version_(int v) {options_version = v; return this;} private int options_version = 1;
public KeyVal[] Options_lang_list() {if (options_lang_list == null) options_lang_list = Options_list_lang_.new_(); return options_lang_list;} private KeyVal[] options_lang_list;
public long Free_mem_when() {return free_mem_when;} long free_mem_when;
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
if (ctx.Match(k, Invk_version)) return Xoa_app_.Version;
else if (ctx.Match(k, Invk_build_date)) return Xoa_app_.Build_date;
else if (ctx.Match(k, Invk_free_mem_when_)) free_mem_when = gplx.ios.Io_size_.parse_or_(m.ReadStr("v"), Io_mgr.Len_mb * 5);
else if (ctx.Match(k, Invk_lang)) return lang_key;
else if (ctx.Match(k, Invk_lang_)) Lang_(m.ReadBry("v"));
else if (ctx.Match(k, Invk_lang_list)) return Options_lang_list();
else if (ctx.Match(k, Invk_options_version)) return options_version;
else if (ctx.Match(k, Invk_options_version_)) options_version = m.ReadInt("v");
else return GfoInvkAble_.Rv_unhandled;
return this;
}
private static final String Invk_version = "version", Invk_build_date = "build_date", Invk_free_mem_when_ = "free_mem_when_", Invk_options_version = "options_version", Invk_options_version_ = "options_version_"
, Invk_lang = "lang", Invk_lang_ = "lang_", Invk_lang_list = "lang_list";
}
class Options_list_lang_ {
public static KeyVal[] new_() {
OrderedHash translated = OrderedHash_.new_bry_();
ListAdp untranslated = ListAdp_.new_();
Add_itm_many(translated, Xol_lang_itm_.Id_en, Xol_lang_itm_.Id_de, Xol_lang_itm_.Id_pl, Xol_lang_itm_.Id_zh_hans, Xol_lang_itm_.Id_zh_hant); // add langs with translations first, so they alphabetize to top of list
int len = Xol_lang_itm_.Id__max;
for (int i = 0; i < len; i++) { // add rest of langs, but sort by code
Xol_lang_itm itm = Xol_lang_itm_.Get_by_id(i);
if (translated.Has(itm.Key())) continue;
untranslated.Add(itm);
}
untranslated.SortBy(Xol_lang_itm_.Comparer_code);
KeyVal[] rv = new KeyVal[len];
int translated_max = translated.Count();
for (int i = 0; i < translated_max; i++)
rv[i] = new_itm((Xol_lang_itm)translated.FetchAt(i));
for (int i = translated_max; i < len; i++)
rv[i] = new_itm((Xol_lang_itm)untranslated.FetchAt(i - translated_max));
return rv;
}
private static KeyVal new_itm(Xol_lang_itm itm) {
String key_str = String_.new_utf8_(itm.Key());
String name_str = String_.new_utf8_(itm.Local_name());
return KeyVal_.new_(key_str, name_str + " [" + key_str + "]");
}
private static void Add_itm_many(OrderedHash translated, int... langs) {
int langs_len = langs.length;
for (int i = 0; i < langs_len; i++) {
Xol_lang_itm itm = Xol_lang_itm_.Get_by_id(langs[i]);
translated.AddReplace(itm.Key(), itm);
}
}
}

View File

@@ -0,0 +1,68 @@
/*
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/>.
*/
//namespace gplx.xowa {
// public class Xoa_url_alias_mgr : GfoInvkAble {
// private Hash_adp_bry hash = Hash_adp_bry.cs_(); private Bry_fmtr fmtr = Bry_fmtr.new_("", "");
// public Xoa_url_alias_mgr(Xoa_app app) {this.app = app;} private Xoa_app app;
// public byte[] Fmt_or_null(byte[] raw) {
// int colon_pos = Bry_finder.Find_fwd(raw, Byte_ascii.Colon); if (colon_pos == Bry_.NotFound) return null;
// byte[] fmt = (byte[])hash.Get_by_mid(raw, 0, colon_pos); if (fmt == null) return null;
// Bry_bfr tmp_bfr = app.Utl_bry_bfr_mkr().Get_b512();
// fmtr.Fmt_(fmt).Bld_bfr_many(tmp_bfr, Bry_.Mid(raw, colon_pos + Int_.Const_dlm_len, raw.length));
// return tmp_bfr.Mkr_rls().XtoAryAndClear();
// }
// public void Clear() {hash.Clear();}
// public void Add_one(byte[] alias, byte[] wiki_key) {hash.Add_bry_obj(alias, wiki_key);}
// public void Add_bulk(byte[] src) { // COPY:add_bulk
// int len = src.length;
// int pos = 0, fld_bgn = 0, fld_idx = 0;
// byte[] alias = Bry_.Empty, wiki = Bry_.Empty;
// Xol_csv_parser csv_parser = Xol_csv_parser._;
// while (true) {
// boolean last = pos == len;
// byte b = last ? Byte_ascii.NewLine : src[pos];
// switch (b) {
// case Byte_ascii.Pipe:
// switch (fld_idx) {
// case 0: alias = csv_parser.Load(src, fld_bgn, pos); break;
// default: throw Err_.unhandled(fld_idx);
// }
// fld_bgn = pos + 1;
// ++fld_idx;
// break;
// case Byte_ascii.NewLine:
// if (fld_bgn < pos) { // guard against trailing new lines
// wiki = csv_parser.Load(src, fld_bgn, pos);
// hash.Add_bry_obj(alias, wiki);
// }
// fld_bgn = pos + 1;
// fld_idx = 0;
// break;
// }
// if (last) break;
// ++pos;
// }
// }
// public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
// if (ctx.Match(k, Invk_add_bulk)) Add_bulk(m.ReadBry("v"));
// else return GfoInvkAble_.Rv_unhandled;
// return this;
// }
// private static final String Invk_add_bulk = "add_bulk";
// }
//}

View File

@@ -0,0 +1,43 @@
/*
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/>.
*/
//namespace gplx.xowa {
// import org.junit.*;
// public class Xoa_url_alias_mgr_tst {
// @Before public void init() {}
// @Test public void Basic() {
// Add_bulk_tst(String_.Concat_lines_nl
// ( "w|en.wikipedia.org/wiki/~{0}"
// , "d|en.wiktionary.org/wiki/~{0}"
// ), KeyVal_.new_("w:Earth", "en.wikipedia.org/wiki/Earth"), KeyVal_.new_("d:Earth", "en.wiktionary.org/wiki/Earth"), KeyVal_.new_("x:Earth", null));
// }
// private void Add_bulk_tst(String raw_str, params KeyVal[] expd_ary) {
// Xoa_app app = Xoa_app_fxt.app_();
// Xoa_url_alias_mgr mgr = new Xoa_url_alias_mgr(app);
// byte[] raw_bry = Bry_.new_ascii_(raw_str);
// mgr.Add_bulk(raw_bry);
// int expd_ary_len = expd_ary.length;
// for (int i = 0; i < expd_ary_len; i++) {
// KeyVal kv = expd_ary[i];
// byte[] ttl = Bry_.new_ascii_safe_null_(kv.Key());
// byte[] expd = Bry_.new_ascii_safe_null_((String)kv.Val());
// byte[] actl = mgr.Fmt_or_null(ttl);
// Tfds.Eq(String_.new_utf8_null_safe_(expd), String_.new_utf8_null_safe_(actl));
// }
// }
// }
//}

View File

@@ -0,0 +1,75 @@
/*
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.xowa; import gplx.*;
interface Xoac_lang_obj {}
public class Xoac_lang_grp implements Cfg_nde_obj, Xoac_lang_obj {
public Xoac_lang_grp(byte[] key) {this.key_bry = key; this.name_bry = key_bry;}
public byte[] Key_bry() {return key_bry;} private byte[] key_bry;
public byte[] Name_bry() {return name_bry;} public Xoac_lang_grp Name_bry_(byte[] v) {name_bry = v; return this;} private byte[] name_bry;
public int Sort_idx() {return sort_idx;} private int sort_idx;
public byte[] Nde_key() {return key_bry;}
public boolean Nde_typ_is_grp() {return true;}
public Cfg_nde_obj Nde_subs_make(byte[] itm_type, byte[] itm_key, byte[][] itm_atrs) {
Cfg_nde_obj rv = null;
if (Bry_.Eq(itm_type, Make_grp)) rv = new Xoac_lang_grp(itm_key);
else if (Bry_.Eq(itm_type, Make_itm)) rv = new Xoac_lang_itm(itm_key);
else throw Err_mgr._.unhandled_(itm_type);
rv.Nde_atrs_set(itm_atrs);
return rv;
}
public int Nde_subs_len() {return itms.Count();}
public Cfg_nde_obj Nde_subs_get_at(int i) {return (Cfg_nde_obj)itms.FetchAt(i);}
public Cfg_nde_obj Nde_subs_get(byte[] key) {return (Cfg_nde_obj)itms.Fetch(key);}
public void Nde_subs_add(byte[] itm_key, Cfg_nde_obj itm_obj) {
if (!itm_obj.Nde_typ_is_grp()) {
Xoac_lang_itm itm = (Xoac_lang_itm)itm_obj;
if (!regy.Has(itm.Key_bry())) regy.Add(itm.Key_bry(), itm);
itm.Grp_(this);
}
itms.Add(itm_key, itm_obj);
}
public void Nde_subs_del(byte[] key) {
Cfg_nde_obj cur_obj = (Cfg_nde_obj)Nde_subs_get(key);
if (!cur_obj.Nde_typ_is_grp()) {
Xoac_lang_itm cur_itm = (Xoac_lang_itm)cur_obj;
Xoac_lang_itm reg_itm = (Xoac_lang_itm)regy.Fetch(key);
if (cur_itm.Uid() == reg_itm.Uid()) regy.Del(cur_itm.Key_bry());
}
itms.Del(key);
}
public void Nde_atrs_set(byte[][] ary) {
int ary_len = ary.length;
if (ary_len > 0) name_bry = ary[0];
if (ary_len > 1) sort_idx = Bry_.X_to_int_or(ary[1], -1);
}
public static final byte[] Make_grp = Bry_.new_utf8_("grp"), Make_itm = Bry_.new_utf8_("itm");
public int Itms_len() {return itms.Count();}
public Cfg_nde_obj Itms_get_at(int i) {return (Cfg_nde_obj)itms.FetchAt(i);}
OrderedHash itms = OrderedHash_.new_bry_();
private static OrderedHash regy = OrderedHash_.new_bry_();
public static Xoac_lang_itm Regy_get_or_null(byte[] key) {return (Xoac_lang_itm)regy.Fetch(key);}
public static Xoac_lang_itm Regy_get_or_new (byte[] key) {
Xoac_lang_itm rv = Regy_get_or_null(key);
if (rv == null) {
rv = new Xoac_lang_itm(key);
regy.Add(key, rv);
}
return rv;
}
}

View File

@@ -0,0 +1,145 @@
/*
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.xowa; import gplx.*;
import org.junit.*; import gplx.xowa.langs.*;
public class Xoac_lang_grp_tst {
Xoac_lang_grp_fxt fxt = new Xoac_lang_grp_fxt();
@Before public void init() {fxt.Clear();}
@Test public void Add_itm_new() {
fxt.Define_bulk(String_.Concat_lines_nl
( "+|euro|itm|fr|French"
, "+|euro|itm|de|German"
)
, fxt.grp_("euro").Itms_
( fxt.itm_("fr").Local_name_("French")
, fxt.itm_("de").Local_name_("German")
));
}
@Test public void Add_itm_nl() {
fxt.Define_bulk(String_.Concat_lines_nl
( ""
, "+|euro|itm|fr|French"
, ""
)
, fxt.grp_("euro").Itms_
( fxt.itm_("fr").Local_name_("French")
));
}
@Test public void Add_grp() {
fxt.Define_bulk(String_.Concat_lines_nl
( "+||grp|euro|European~1"
, "+|euro|itm|fr|French"
)
, fxt.grp_("euro").Name_("European").Sort_idx_(1).Itms_
( fxt.itm_("fr").Local_name_("French")
));
}
@Test public void Set_grp() {
fxt.Define_bulk(String_.Concat_lines_nl
( "+|euro|itm|fr|French"
, "+||grp|euro|European"
)
, fxt.grp_("euro").Name_("European").Itms_
( fxt.itm_("fr").Local_name_("French")
));
}
@Test public void Del() {
fxt.Define_bulk(String_.Concat_lines_nl
( "+|euro|itm|fr|French"
, "+|euro|itm|de|German"
, "-|euro|fr"
)
, fxt.grp_("euro").Itms_
( fxt.itm_("de").Local_name_("German")
));
}
@Test public void Add_grp_nest() {
fxt.Define_bulk(String_.Concat_lines_nl
( "+||grp|wiki"
, "+|wiki|grp|euro|European"
, "+|euro|itm|de|German"
)
, fxt.grp_("wiki").Itms_
( fxt.grp_("euro").Itms_
( fxt.itm_("de").Local_name_("German")
)));
}
}
class Xoac_lang_grp_fxt {
Xoa_lang_mgr lang_mgr; Tst_mgr tst_mgr = new Tst_mgr();
public void Clear() {
Xoa_app app = Xoa_app_fxt.app_();
lang_mgr = app.Lang_mgr();
}
public Xoac_lang_itm_chkr itm_(String key) {return new Xoac_lang_itm_chkr(key);}
public Xoac_lang_grp_chkr grp_(String key) {return new Xoac_lang_grp_chkr(key);}
public Xoac_lang_grp_fxt Define_bulk(String raw, Xoac_lang_grp_chkr... expd) {
lang_mgr.Groups().Set_bulk(Bry_.new_utf8_(raw));
tst_mgr.Tst_ary("", expd, Xto_ary(lang_mgr.Groups()));
return this;
}
Xoac_lang_grp[] Xto_ary(Cfg_nde_root root) {
int len = root.Root_len();
Xoac_lang_grp[] rv = new Xoac_lang_grp[len];
for (int i = 0; i < len; i++) {
rv[i] = (Xoac_lang_grp)root.Root_get_at(i); // ASSUME: root only has grps (no itms)
}
return rv;
}
}
abstract class Xoac_lang_chkr_base implements Tst_chkr {
public abstract Class<?> TypeOf();
public abstract int Chk(Tst_mgr mgr, String path, Object actl);
}
class Xoac_lang_itm_chkr extends Xoac_lang_chkr_base {
public Xoac_lang_itm_chkr(String key) {this.key = key;}
public String Key() {return key;} private String key;
public Xoac_lang_itm_chkr Local_name_(String v) {local_name = v; return this;} private String local_name;
@Override public Class<?> TypeOf() {return Xoac_lang_itm.class;}
@Override public int Chk(Tst_mgr mgr, String path, Object actl_obj) {
Xoac_lang_itm actl = (Xoac_lang_itm)actl_obj;
int rv = 0;
rv += mgr.Tst_val(key == null, path, "key", key, String_.new_utf8_(actl.Key_bry()));
rv += mgr.Tst_val(local_name == null, path, "local_name", local_name, String_.new_utf8_(actl.Local_name_bry()));
return rv;
}
}
class Xoac_lang_grp_chkr extends Xoac_lang_chkr_base {
public Xoac_lang_grp_chkr(String key) {this.key = key;}
public String Key() {return key;} private String key;
public Xoac_lang_grp_chkr Name_(String v) {name = v; return this;} private String name;
public Xoac_lang_grp_chkr Sort_idx_(int v) {sort_idx = v; return this;} private int sort_idx = -1;
public Xoac_lang_grp_chkr Itms_(Xoac_lang_chkr_base... v) {this.itms = v; return this;} private Xoac_lang_chkr_base[] itms;
@Override public Class<?> TypeOf() {return Xoac_lang_grp.class;}
@Override public int Chk(Tst_mgr mgr, String path, Object actl_obj) {
Xoac_lang_grp actl = (Xoac_lang_grp)actl_obj;
int rv = 0;
rv += mgr.Tst_val(key == null, path, "key", key, String_.new_utf8_(actl.Key_bry()));
rv += mgr.Tst_val(name == null, path, "name", name, String_.new_utf8_(actl.Name_bry()));
rv += mgr.Tst_val(sort_idx == -1, path, "sort_idx", sort_idx, actl.Sort_idx());
rv += mgr.Tst_sub_ary(itms, Xto_ary(actl), path, rv);
return rv;
}
Xoac_lang_obj[] Xto_ary(Xoac_lang_grp grp) {
int len = grp.Itms_len();
Xoac_lang_obj[] rv = new Xoac_lang_obj[len];
for (int i = 0; i < len; i++)
rv[i] = (Xoac_lang_obj)grp.Itms_get_at(i); // ASSUME: grp only has itms (no grps)
return rv;
}
}

View File

@@ -0,0 +1,38 @@
/*
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.xowa; import gplx.*;
public class Xoac_lang_itm implements Cfg_nde_obj, Xoac_lang_obj {
public Xoac_lang_itm(byte[] key) {this.key_bry = key; local_name_bry = key_bry; uid = uid_next++;} static int uid_next = 0;
public Xoac_lang_grp Grp() {return grp;} public Xoac_lang_itm Grp_(Xoac_lang_grp v) {grp = v; return this;} private Xoac_lang_grp grp;
public int Uid() {return uid;} private int uid;
public byte[] Key_bry() {return key_bry;} private byte[] key_bry;
public byte[] Local_name_bry() {return local_name_bry;} public Xoac_lang_itm Local_name_bry_(byte[] v) {local_name_bry = v; return this;} private byte[] local_name_bry;
public byte[] Nde_key() {return key_bry;}
public boolean Nde_typ_is_grp() {return false;}
public Cfg_nde_obj Nde_subs_make(byte[] itm_type, byte[] itm_key, byte[][] itm_atrs) {throw Err_mgr._.fmt_(GRP_KEY, "invalid_sub_make", "langs cannot have itms: ~{0} ~{1}", itm_type, itm_key);}
public Cfg_nde_obj Nde_subs_get(byte[] key) {throw Err_mgr._.fmt_(GRP_KEY, "invalid_sub_get", "langs cannot have itms: ~{0}", key);}
public int Nde_subs_len() {return 0;}
public Cfg_nde_obj Nde_subs_get_at(int i) {throw Err_mgr._.fmt_(GRP_KEY, "invalid_sub_get", "langs cannot have itms: ~{0}", i);}
public void Nde_subs_add(byte[] itm_key, Cfg_nde_obj itm_obj) {throw Err_mgr._.fmt_(GRP_KEY, "invalid_sub_add", "langs cannot have itms: ~{0}", String_.new_utf8_(itm_key));}
public void Nde_subs_del(byte[] key) {throw Err_mgr._.fmt_(GRP_KEY, "invalid_sub_del", "langs cannot delete itms: ~{0}", String_.new_utf8_(key));}
public void Nde_atrs_set(byte[][] ary) {
if (ary.length != 1) throw Err_mgr._.fmt_("xowa.langs.itms", "invalid_atrs", "expecting name only: ~{0}", String_.AryXtoStr(String_.Ary(ary)));
local_name_bry = ary[0];
}
static final String GRP_KEY = "xowa.langs.itms";
}

View File

@@ -0,0 +1,37 @@
/*
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.xowa; import gplx.*;
public class Xoac_wiki_cfg_bldr_cmd {
public Xoac_wiki_cfg_bldr_cmd(String key, String text) {this.key = key; this.text = text;}
public String Key() {return key;} private String key;
public String Text() {return text;} private String text;
public String Exec(String_bldr sb, String wiki, String src) {
String sect_txt_bgn = sb.Add("// ").Add(key).Add(".bgn\n").XtoStrAndClear();
String sect_txt_end = sb.Add("// ").Add(key).Add(".end\n").XtoStrAndClear();
String sect_txt_all = sb.Add(sect_txt_bgn).Add(text + "\n").Add(sect_txt_end).XtoStrAndClear(); // NOTE: always add \n; convenience for single line cmds
// int sect_pos_bgn = String_.FindFwd(src, sect_txt_bgn);
// if (sect_pos_bgn == String_.Find_none) // new cmd; add to end of file
return src + sect_txt_all;
// int sect_pos_end = String_.FindFwd(src, sect_txt_end);
// if (sect_pos_end == String_.Find_none)
// throw Err_.new_("section_fail: " + wiki + " " + key);
// try {
// return sb.Add(String_.Mid(src, 0, sect_pos_bgn)).Add(sect_txt_all).Add(String_.Mid(src, sect_pos_end + String_.Len(sect_txt_end), String_.Len(text))).XtoStrAndClear();
// } catch (Exception e) {Err_.Noop(e); throw Err_.new_("section_fail: " + wiki + " " + key);}
}
}

View File

@@ -0,0 +1,35 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa; import gplx.*;
public class Xoac_wiki_cfg_bldr_fil implements GfoInvkAble {
public Xoac_wiki_cfg_bldr_fil(String wiki) {this.wiki = wiki;}
public String Wiki() {return wiki;} private String wiki;
public int Itms_count() {return list.Count();}
public Xoac_wiki_cfg_bldr_cmd Itms_get_at(int i) {return (Xoac_wiki_cfg_bldr_cmd)list.FetchAt(i);}
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
if (ctx.Match(k, Invk_new_cmd_)) {Itms_add(m.ReadStr("id"), m.ReadStr("text"));}
else return GfoInvkAble_.Rv_unhandled;
return this;
} private static final String Invk_new_cmd_ = "new_cmd_";
public Xoac_wiki_cfg_bldr_cmd Itms_add(String key, String text) {
Xoac_wiki_cfg_bldr_cmd rv = new Xoac_wiki_cfg_bldr_cmd(key, text);
list.Add(rv);
return rv;
}
ListAdp list = ListAdp_.new_();
}

View File

@@ -0,0 +1,65 @@
/*
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.xowa; import gplx.*;
interface Xoac_wiki_obj {}
public class Xoac_wiki_grp implements Cfg_nde_obj, Xoac_wiki_obj {
public Xoac_wiki_grp(byte[] key) {this.key_bry = key; this.name_bry = key_bry;}
public byte[] Key_bry() {return key_bry;} private byte[] key_bry;
public byte[] Name_bry() {return name_bry;} private byte[] name_bry;
public boolean Nde_typ_is_grp() {return true;}
public byte[] Nde_key() {return key_bry;}
public Cfg_nde_obj Nde_subs_make(byte[] itm_type, byte[] itm_key, byte[][] itm_atrs) {
Cfg_nde_obj rv = null;
if (Bry_.Eq(itm_type, Make_grp)) rv = new Xoac_wiki_grp(itm_key);
else if (Bry_.Eq(itm_type, Make_itm)) rv = new Xoac_wiki_itm(itm_key);
else throw Err_mgr._.unhandled_(itm_type);
rv.Nde_atrs_set(itm_atrs);
return rv;
}
public int Nde_subs_len() {return itms.Count();}
public Cfg_nde_obj Nde_subs_get_at(int i) {return (Cfg_nde_obj)itms.FetchAt(i);}
public Cfg_nde_obj Nde_subs_get(byte[] key) {return (Cfg_nde_obj)itms.Fetch(key);}
public void Nde_subs_add(byte[] itm_key, Cfg_nde_obj itm_obj) {itms.Add(itm_key, itm_obj);}
public void Nde_subs_del(byte[] key) {itms.Del(key);}
public void Nde_atrs_set(byte[][] ary) {
int ary_len = ary.length;
if (ary_len > 0) name_bry = ary[0];
}
public static final byte[] Make_grp = Bry_.new_utf8_("grp"), Make_itm = Bry_.new_utf8_("itm");
public int Itms_len() {return itms.Count();}
public Cfg_nde_obj Itms_get_at(int i) {return (Cfg_nde_obj)itms.FetchAt(i);}
OrderedHash itms = OrderedHash_.new_bry_();
}
class Xoac_wiki_itm implements Cfg_nde_obj, Xoac_wiki_obj {
public Xoac_wiki_itm(byte[] key) {this.key_bry = key;}
public byte[] Key_bry() {return key_bry;} private byte[] key_bry;
public byte[][] Aliases() {return aliases;} private byte[][] aliases;
public byte[] Nde_key() {return key_bry;}
public boolean Nde_typ_is_grp() {return false;}
public Cfg_nde_obj Nde_subs_make(byte[] itm_type, byte[] itm_key, byte[][] itm_atrs) {throw Err_mgr._.fmt_(GRP_KEY, "invalid_sub_make", "leafs cannot have itms: ~{0} ~{1}", itm_type, itm_key);}
public Cfg_nde_obj Nde_subs_get(byte[] key) {throw Err_mgr._.fmt_(GRP_KEY, "invalid_sub_get", "leafs cannot have itms: ~{0}", key);}
public int Nde_subs_len() {return 0;}
public Cfg_nde_obj Nde_subs_get_at(int i) {throw Err_mgr._.fmt_(GRP_KEY, "invalid_sub_get", "leafs cannot have itms: ~{0}", i);}
public void Nde_subs_add(byte[] itm_key, Cfg_nde_obj itm_obj) {throw Err_mgr._.fmt_(GRP_KEY, "invalid_sub_add", "leafs cannot have itms: ~{0}", String_.new_utf8_(itm_key));}
public void Nde_subs_del(byte[] key) {throw Err_mgr._.fmt_(GRP_KEY, "invalid_sub_del", "leafs cannot delete itms: ~{0}", String_.new_utf8_(key));}
public void Nde_atrs_set(byte[][] ary) {
int ary_len = ary.length;
if (ary_len > 0) aliases = Bry_.Split(ary[0], Byte_ascii.Semic);
}
static final String GRP_KEY = "xowa.wikis.itms";
}

View File

@@ -0,0 +1,99 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa; import gplx.*;
import org.junit.*;
import gplx.xowa.wikis.*;
public class Xoac_wiki_grp_tst {
Xoac_wiki_grp_fxt fxt = new Xoac_wiki_grp_fxt();
@Before public void init() {fxt.Clear();}
@Test public void Add_itm_new() {
fxt.Define_bulk(String_.Concat_lines_nl
( "+|core|itm|commons|commons"
, "+|core|itm|meta|meta;m"
, "+|peer|itm|wiktionary|wikt;wiktionary"
, "+|peer|itm|wikisource|s"
)
, fxt.grp_("core").Itms_
( fxt.itm_("commons").Aliases_("commons")
, fxt.itm_("meta").Aliases_("meta", "m")
)
, fxt.grp_("peer").Itms_
( fxt.itm_("wiktionary").Aliases_("wikt", "wiktionary")
, fxt.itm_("wikisource").Aliases_("s")
)
);
}
}
class Xoac_wiki_grp_fxt {
Xoa_wiki_mgr wiki_mgr; Tst_mgr tst_mgr = new Tst_mgr();
public void Clear() {
Xoa_app app = Xoa_app_fxt.app_();
wiki_mgr = app.Wiki_mgr();
}
public Xoac_wiki_itm_chkr itm_(String key) {return new Xoac_wiki_itm_chkr(key);}
public Xoac_wiki_grp_chkr grp_(String key) {return new Xoac_wiki_grp_chkr(key);}
public Xoac_wiki_grp_fxt Define_bulk(String raw, Xoac_wiki_grp_chkr... expd) {
wiki_mgr.Groups().Set_bulk(Bry_.new_utf8_(raw));
tst_mgr.Tst_ary("", expd, Xto_ary(wiki_mgr.Groups()));
return this;
}
Xoac_wiki_grp[] Xto_ary(Cfg_nde_root root) {
int len = root.Root_len();
Xoac_wiki_grp[] rv = new Xoac_wiki_grp[len];
for (int i = 0; i < len; i++) {
rv[i] = (Xoac_wiki_grp)root.Root_get_at(i); // ASSUME: root only has grps (no itms)
}
return rv;
}
}
interface Xoac_wiki_chkr_obj extends Tst_chkr {}
class Xoac_wiki_itm_chkr implements Xoac_wiki_chkr_obj {
public Xoac_wiki_itm_chkr(String key) {this.key = key;}
public String Key() {return key;} private String key;
public Xoac_wiki_itm_chkr Aliases_(String... v) {aliases = v; return this;} private String[] aliases;
public Class<?> TypeOf() {return Xoac_wiki_itm.class;}
public int Chk(Tst_mgr mgr, String path, Object actl_obj) {
Xoac_wiki_itm actl = (Xoac_wiki_itm)actl_obj;
int rv = 0;
rv += mgr.Tst_val(key == null, path, "key", key, String_.new_utf8_(actl.Key_bry()));
rv += mgr.Tst_val(aliases == null, path, "aliases", String_.AryXtoStr(aliases), String_.AryXtoStr(String_.Ary(actl.Aliases())));
return rv;
}
}
class Xoac_wiki_grp_chkr implements Xoac_wiki_chkr_obj {
public Xoac_wiki_grp_chkr(String key) {this.key = key;}
public String Key() {return key;} private String key;
public Xoac_wiki_grp_chkr Name_(String v) {name = v; return this;} private String name;
public Xoac_wiki_grp_chkr Itms_(Xoac_wiki_chkr_obj... v) {this.itms = v; return this;} private Xoac_wiki_chkr_obj[] itms;
public Class<?> TypeOf() {return Xoac_wiki_grp.class;}
public int Chk(Tst_mgr mgr, String path, Object actl_obj) {
Xoac_wiki_grp actl = (Xoac_wiki_grp)actl_obj;
int rv = 0;
rv += mgr.Tst_val(key == null, path, "key", key, String_.new_utf8_(actl.Key_bry()));
rv += mgr.Tst_val(name == null, path, "name", name, String_.new_utf8_(actl.Name_bry()));
rv += mgr.Tst_sub_ary(itms, Xto_ary(actl), path, rv);
return rv;
}
Xoac_wiki_obj[] Xto_ary(Xoac_wiki_grp grp) {
int len = grp.Itms_len();
Xoac_wiki_obj[] rv = new Xoac_wiki_obj[len];
for (int i = 0; i < len; i++)
rv[i] = (Xoac_wiki_obj)grp.Itms_get_at(i); // ASSUME: grp only has itms (no grps)
return rv;
}
}

View File

@@ -0,0 +1,102 @@
/*
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.xowa; import gplx.*;
public class Xoad_wtr_dump {
public Xoad_wtr_dump(Io_url log_dir) {this.log_dir = log_dir;} Io_url log_dir;
//Xoad_dump_itm[] fil_ary = new Xoad_dump_itm[64]; int fil_ary_len = 0;
int fil_ary_size = 0;
final int bfr_ary_size_max = 1024 * 1024 * 1;
public void Write(byte[] ttl, int page_idx, Gfo_msg_log msg_log) {
int ary_len = msg_log.Ary_len(); boolean flush_me = false;
for (int i = 0; i < ary_len; i++) {
Gfo_msg_data data = msg_log.Ary_get(i);
Xoad_dump_itm rv = GetByItm(data);
if (rv.BfrLen() > bfr_ary_size_max) flush_me = true;
fil_ary_size += rv.Write(ttl, page_idx, data);
}
if (fil_ary_size > bfr_ary_size_max || flush_me)
Flush();
}
public void Flush() {
int len = dump_itms.Count();
for (int i = 0; i < len; i++) {
Xoad_dump_itm data = (Xoad_dump_itm)dump_itms.FetchAt(i);
if (data != null) {
data.Flush(log_dir);
data.Rls();
}
}
dump_itms.Clear();
fil_ary_size = 0;
}
Xoad_dump_itm GetByItm(Gfo_msg_data data) {
byte[] path_bry = data.Item().Path_bry();
Xoad_dump_itm rv = (Xoad_dump_itm)dump_itms.Fetch(path_bry);
if (rv == null) {
rv = new Xoad_dump_itm(path_bry, data.Item().Key_bry());
dump_itms.Add(path_bry, rv);
}
return rv;
} private OrderedHash dump_itms = OrderedHash_.new_bry_();
}
class Xoad_dump_itm {
Bry_bfr bfr = Bry_bfr.new_(4096);
int pageIdx_last;
public int BfrLen() {return bfr.Len();}
public Xoad_dump_itm(byte[] ownerKey, byte[] itmKey) {
fil_name = String_.new_utf8_(ownerKey) + "__" + String_.new_utf8_(itmKey);
} String fil_name;
public void Flush(Io_url log_dir) {
Io_url fil_url = log_dir.GenSubFil_ary(fil_name, ".txt");
Io_mgr._.AppendFilByt(fil_url, bfr.Bfr(), bfr.Len());
bfr.Reset_if_gt(Io_mgr.Len_kb);
}
public void Rls() {
bfr.Rls();
bfr = null;
}
public int Write(byte[] ttl, int page_idx, Gfo_msg_data eny) {
int old = bfr.Len();
if (page_idx != pageIdx_last) {pageIdx_last = page_idx; bfr.Add(ttl).Add_byte_nl();}
bfr.Add_byte_repeat(Byte_ascii.Space, 4);
byte[] src = eny.Src_bry();
if (src.length != 0) {
int mid_bgn = eny.Src_bgn(), mid_end = eny.Src_end();
int all_bgn = mid_bgn - 40; if (all_bgn < 0) all_bgn = 0;
int all_end = mid_end + 40; if (all_end > src.length) all_end = src.length;
Write_mid(src, all_bgn, mid_bgn);
bfr.Add_byte(Byte_ascii.Tilde).Add_byte(Byte_ascii.Num_0).Add_byte(Byte_ascii.Tilde);
Write_mid(src, mid_bgn, mid_end);
bfr.Add_byte(Byte_ascii.Tilde).Add_byte(Byte_ascii.Num_1).Add_byte(Byte_ascii.Tilde);
Write_mid(src, mid_end, all_end);
bfr.Add_byte_nl();
}
return bfr.Len() - old;
}
private void Write_mid(byte[] src, int bgn, int end) {
if (end - bgn == 0) return;
for (int i = bgn; i < end; i++) {
byte b = src[i];
switch (b) {
case Byte_ascii.NewLine: bfr.Add_byte(Byte_ascii.Backslash); bfr.Add_byte(Byte_ascii.Ltr_n); break;
case Byte_ascii.Tab: bfr.Add_byte(Byte_ascii.Backslash); bfr.Add_byte(Byte_ascii.Ltr_t); break;
default: bfr.Add_byte(b); break;
}
}
}
}