mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
v1.6.5.1
This commit is contained in:
156
400_xowa/src/gplx/xowa/gui/urls/Xog_url_wkr.java
Normal file
156
400_xowa/src/gplx/xowa/gui/urls/Xog_url_wkr.java
Normal file
@@ -0,0 +1,156 @@
|
||||
/*
|
||||
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.gui.urls; import gplx.*; import gplx.xowa.*; import gplx.xowa.gui.*;
|
||||
import gplx.xowa.files.*; import gplx.xowa.gui.views.*;
|
||||
public class Xog_url_wkr {
|
||||
private Xoh_href href = new Xoh_href();
|
||||
private Xoa_app app; private Xog_win_itm win; private Xoa_page page;
|
||||
public byte Href_tid() {return href.Tid();}
|
||||
public Xog_url_wkr Parse(Xog_win_itm win, String href_str) {
|
||||
if (href_str == null) return this; // text is not link; return;
|
||||
byte[] href_bry = Bry_.new_utf8_(href_str);
|
||||
this.win = win; this.app = win.App(); this.page = win.Active_page();
|
||||
app.Href_parser().Parse(href, href_bry, page.Wiki(), page.Ttl().Page_url());
|
||||
return this;
|
||||
}
|
||||
public Xoa_url Exec() {
|
||||
byte[] href_bry = href.Raw();
|
||||
switch (href.Tid()) {
|
||||
case Xoh_href.Tid_null: return Rslt_handled; // url is invalid; return handled (which effectively ignores)
|
||||
case Xoh_href.Tid_xowa: return Exec_url_xowa(app); // xowa:app.version
|
||||
case Xoh_href.Tid_http: return Exec_url_http(app); // http://site.org
|
||||
case Xoh_href.Tid_anchor: return Exec_url_anchor(win); // #anchor
|
||||
case Xoh_href.Tid_xcmd: return Exec_url_xcmd(win); // /xcmd/app.version
|
||||
case Xoh_href.Tid_file: return Exec_url_file(app, page, win, href_bry); // file:///xowa/A.png
|
||||
default: return Exec_url_page(app, page, win, href_bry); // Page /wiki/Page
|
||||
}
|
||||
}
|
||||
private Xoa_url Exec_url_xowa(Xoa_app app) { // EX: xowa:app.version
|
||||
// NOTE: must catch exception else it will bubble to SWT browser and raise secondary exception of xowa is not a registered protocol
|
||||
try {app.Gfs_mgr().Run_str(String_.new_utf8_(href.Page()));}
|
||||
catch (Exception e) {app.Gui_mgr().Kit().Ask_ok("", "", Err_.Message_gplx_brief(e));}
|
||||
return Rslt_handled;
|
||||
}
|
||||
private Xoa_url Exec_url_http(Xoa_app app) { // EX: http:a.org
|
||||
app.Fsys_mgr().App_mgr().Exec_view_web(href.Raw());
|
||||
return Rslt_handled;
|
||||
}
|
||||
private Xoa_url Exec_url_anchor(Xog_win_itm win) { // EX: #anchor
|
||||
win.Active_html_itm().Scroll_page_by_id_gui(String_.new_utf8_(href.Anchor())); // NOTE: was originally directly; changed to call on thread; DATE:2014-05-03
|
||||
return Rslt_handled;
|
||||
}
|
||||
private Xoa_url Exec_url_xcmd(Xog_win_itm win) { // EX: /xcmd/
|
||||
byte[] xowa_href_bry = href.Page();
|
||||
int xowa_href_bry_len = xowa_href_bry.length;
|
||||
int slash_pos = Bry_finder.Find_fwd(xowa_href_bry, Byte_ascii.Slash); if (slash_pos == Bry_.NotFound) slash_pos = xowa_href_bry_len;
|
||||
byte[] xowa_cmd_bry = Bry_.Mid(xowa_href_bry, 0, slash_pos);
|
||||
String xowa_cmd_str = String_.new_utf8_(xowa_cmd_bry);
|
||||
GfoMsg m = GfoMsg_.new_cast_(xowa_cmd_str);
|
||||
if (String_.Eq(xowa_cmd_str, Xog_win_itm.Invk_eval))
|
||||
m.Add("cmd", String_.new_utf8_(xowa_href_bry, slash_pos + 1, xowa_href_bry_len));
|
||||
win.Invk(GfsCtx.new_(), 0, xowa_cmd_str, m);
|
||||
return Rslt_handled;
|
||||
}
|
||||
private Xoa_url Exec_url_file(Xoa_app app, Xoa_page page, Xog_win_itm win, byte[] href_bry) { // EX: file:///xowa/A.png
|
||||
href_bry = app.Url_converter_url().Decode(href_bry);
|
||||
Io_url href_url = Io_url_.http_any_(String_.new_utf8_(href_bry), Op_sys.Cur().Tid_is_wnt());
|
||||
gplx.gfui.Gfui_html html_box = win.Active_html_box();
|
||||
String xowa_ttl = page.Wiki().Gui_mgr().Cfg_browser().Content_editable()
|
||||
? html_box.Html_active_atr_get_str(gplx.xowa.html.Xoh_html_tag.Nde_xowa_title_str, null)
|
||||
: Xoh_dom_.Title_by_href(app.Url_converter_comma(), app.Utl_bry_bfr_mkr().Get_b512().Mkr_rls(), href_bry, Bry_.new_utf8_(html_box.Html_doc_html()));
|
||||
if (!Io_mgr._.ExistsFil(href_url)) {
|
||||
Xof_xfer_itm xfer_itm = new Xof_xfer_itm();
|
||||
byte[] title = app.Url_converter_url().Decode(Bry_.new_utf8_(xowa_ttl));
|
||||
xfer_itm.Atrs_by_lnki(Xop_lnki_type.Id_none, -1, -1, -1, Xof_doc_thumb.Null, Xof_doc_page.Null).Atrs_by_ttl(title, Bry_.Empty);
|
||||
page.Wiki().File_mgr().Find_meta(xfer_itm);
|
||||
page.File_queue().Clear();
|
||||
page.File_queue().Add(xfer_itm); // NOTE: set elem_id to "impossible" number, otherwise it will auto-update an image on the page with a super-large size; [[File:Alfred Sisley 062.jpg]]
|
||||
page.Wiki().File_mgr().Repo_mgr().Xfer_mgr().Force_orig_y_();
|
||||
page.File_queue().Exec(Xof_exec_tid.Tid_viewer_app, win.Usr_dlg(), page.Wiki(), page);
|
||||
page.Wiki().File_mgr().Repo_mgr().Xfer_mgr().Force_orig_n_();
|
||||
}
|
||||
if (Io_mgr._.ExistsFil(href_url)) {
|
||||
ProcessAdp media_player = app.Fsys_mgr().App_mgr().App_by_ext(href_url.Ext());
|
||||
media_player.Run(href_url);
|
||||
}
|
||||
return Rslt_handled;
|
||||
}
|
||||
private Xoa_url Exec_url_page(Xoa_app app, Xoa_page page, Xog_win_itm win, byte[] href_bry) { // EX: "Page"; "/wiki/Page"; // rewritten; DATE:2014-01-19
|
||||
Xoa_url rv = new Xoa_url();
|
||||
Xow_wiki wiki = page.Wiki();
|
||||
app.Url_parser().Parse(rv, href_bry); // needed for query_args
|
||||
byte[] anchor_bry = href.Anchor();
|
||||
byte[] page_bry = rv.Page_bry();
|
||||
byte[][] segs_ary = rv.Segs_ary();
|
||||
int segs_ary_len = segs_ary.length;
|
||||
boolean use_main_page = false;
|
||||
if ( segs_ary_len > 0 // handle "Special:Search/Earth" which creates segs[1] {"Special:Search"} and page="Earth"
|
||||
|| href.Tid() == Xoh_href.Tid_site) { // NOTE: if site, must always (a) zap Segs_ary and (b) force correct page; see tests; DATE:2014-01-21
|
||||
int segs_bgn = 0;
|
||||
boolean segs_iterate = true;
|
||||
if (href.Tid() == Xoh_href.Tid_site) { // site, handle multiple segs; EX: "home/wiki/", "home/wiki/Help:Contents"; DATE:2014-01-21
|
||||
if (segs_ary_len < 2) { // only 0 or 1 seg; usually occurs for logo and other xwiki links to Main_Page; EX: "/site/en.wikipedia.org/wiki/"; "/site/en.wikipedia.org/"
|
||||
page_bry = wiki.Init_assert().Props().Main_page(); // use Main_page; DATE:2014-02-16
|
||||
use_main_page = true;
|
||||
segs_iterate = false;
|
||||
}
|
||||
else
|
||||
segs_bgn = 2; // start from seg_2; seg_0="/en.wikipedia.org/" and seg_1="/wiki/"; note that > 2 segs possible; EX: "/site/en.wikipedia.org/wiki/A/B/C
|
||||
}
|
||||
if (segs_iterate) {
|
||||
Bry_bfr tmp_bfr = wiki.Utl_bry_bfr_mkr().Get_b128();
|
||||
for (int i = segs_bgn; i < segs_ary_len; i++) {
|
||||
tmp_bfr.Add(segs_ary[i]);
|
||||
tmp_bfr.Add_byte(Byte_ascii.Slash);
|
||||
}
|
||||
tmp_bfr.Add(page_bry);
|
||||
page_bry = tmp_bfr.Mkr_rls().XtoAryAndClear();
|
||||
}
|
||||
rv.Segs_ary_(Bry_.Ary_empty);
|
||||
}
|
||||
Gfo_url_arg[] qargs = rv.Args();
|
||||
int qargs_len = qargs.length;
|
||||
if (qargs_len > 0) { // remove anchors from qargs; EX: "to=B#mw_pages"
|
||||
for (int i = 0; i < qargs_len; i++) {
|
||||
Gfo_url_arg arg = qargs[i];
|
||||
int anchor_pos = Bry_finder.Find_bwd(arg.Val_bry(), Byte_ascii.Hash); // NOTE: must .FindBwd to handle Category args like de.wikipedia.org/wiki/Kategorie:Begriffskl%C3%A4rung?pagefrom=#::12%20PANZERDIVISION#mw-pages; DATE:2013-06-18
|
||||
if (anchor_pos != Bry_.NotFound)
|
||||
arg.Val_bry_(Bry_.Mid(arg.Val_bry(), 0, anchor_pos));
|
||||
}
|
||||
}
|
||||
if (!Bry_.Eq(page.Wiki().Domain_bry(), href.Wiki())) {// xwiki; EX: "file:///site/en.wiktionary.org/wiki/a"; EX: (1) goto w:Anything; (2) click on "anything" in wikt; "anything" will be parsed by en.wiki's rules, not en.wikt; DATE:2013-01-30
|
||||
wiki = app.Wiki_mgr().Get_by_key_or_make(href.Wiki()).Init_assert(); // get xwiki and set to wiki
|
||||
if (use_main_page)
|
||||
page_bry = wiki.Props().Main_page(); // get Main_page for new wiki; DATE:2014-02-23
|
||||
Xoa_ttl tmp_ttl = Xoa_ttl.parse_(wiki, page_bry); // reparse ttl according to xwiki's case_match rules; NOTE: do not use rv.Page_bry() or else will lose sub_pages (A/B/C); DATE:2014-02-21
|
||||
if (tmp_ttl != null)
|
||||
page_bry = tmp_ttl.Full_db();
|
||||
}
|
||||
rv.Wiki_(wiki);
|
||||
rv.Wiki_bry_(wiki.Domain_bry()); // needed b/c url_parser.Parse(href) will result in wiki of "wiki" for "/wiki/Page"
|
||||
rv.Page_bry_(page_bry);
|
||||
rv.Anchor_bry_(anchor_bry);
|
||||
return rv;
|
||||
}
|
||||
public static Xoa_url Rslt_handled = null;
|
||||
public static Xoa_url Exec_url(Xog_win_itm win, String href_str) {
|
||||
Xog_url_wkr url_wkr = new Xog_url_wkr();
|
||||
url_wkr.Parse(win, href_str);
|
||||
return url_wkr.Exec();
|
||||
}
|
||||
}
|
||||
100
400_xowa/src/gplx/xowa/gui/urls/Xog_url_wkr_tst.java
Normal file
100
400_xowa/src/gplx/xowa/gui/urls/Xog_url_wkr_tst.java
Normal file
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
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.gui.urls; import gplx.*; import gplx.xowa.*; import gplx.xowa.gui.*;
|
||||
import org.junit.*; import gplx.xowa.gui.views.*;
|
||||
public class Xog_url_wkr_tst {
|
||||
@Before public void init() {fxt.Clear();} private Xog_url_wkr_fxt fxt = new Xog_url_wkr_fxt();
|
||||
@Test public void term() {fxt.Clear();}
|
||||
@Test public void Basic() {
|
||||
fxt.Init_exec("file:///wiki/A").Expd_tid_(Xoh_href.Tid_wiki).Expd_page_("A").Test();
|
||||
}
|
||||
@Test public void Basic_question() {
|
||||
fxt.Init_exec("file:///wiki/A?").Expd_tid_(Xoh_href.Tid_wiki).Expd_page_("A?").Expd_qargs_("").Test();
|
||||
}
|
||||
@Test public void Basic_question_text() {
|
||||
fxt.Init_exec("file:///wiki/A?B").Expd_tid_(Xoh_href.Tid_wiki).Expd_page_("A?B").Expd_qargs_("").Test();
|
||||
}
|
||||
@Test public void Redirect() {
|
||||
fxt.Init_exec("file:///wiki/A?redirect=no").Expd_tid_(Xoh_href.Tid_wiki).Expd_page_("A").Expd_qargs_("?redirect=no").Test();
|
||||
}
|
||||
@Test public void Search() {
|
||||
fxt.Init_exec("file:///wiki/Special:Search/Moon%3Ffulltext%3Dy%26xowa_page_index%3D1").Expd_tid_(Xoh_href.Tid_wiki).Expd_page_("Special:Search/Moon").Expd_qargs_("?fulltext=y&xowa_page_index=1").Test();
|
||||
}
|
||||
@Test public void Ctg() {
|
||||
fxt.Init_exec("file:///wiki/Category:A?pagefrom=A#mw-pages").Expd_tid_(Xoh_href.Tid_wiki).Expd_page_("Category:A").Expd_qargs_("?pagefrom=A").Expd_anchor_("mw-pages").Test();
|
||||
}
|
||||
@Test public void Xwiki() {
|
||||
Xow_ns_mgr ns_mgr = fxt.App().Wiki_mgr().Get_by_key_or_make(Bry_.new_ascii_("en.wiktionary.org")).Ns_mgr();
|
||||
|
||||
ns_mgr.Ns_main().Case_match_(Xow_ns_case_.Id_all);
|
||||
fxt.Init_exec("file:///site/en.wiktionary.org/wiki/a").Expd_tid_(Xoh_href.Tid_site).Expd_wiki_("en.wiktionary.org").Expd_page_("a").Test();
|
||||
|
||||
ns_mgr.Ns_category().Case_match_(Xow_ns_case_.Id_all);
|
||||
fxt.Init_exec("file:///site/en.wiktionary.org/wiki/Category:a").Expd_tid_(Xoh_href.Tid_site).Expd_wiki_("en.wiktionary.org").Expd_page_("Category:a").Test();
|
||||
|
||||
fxt.Init_exec("file:///site/en.wiktionary.org/wiki/A/B/C").Expd_tid_(Xoh_href.Tid_site).Expd_page_("A/B/C").Test();
|
||||
}
|
||||
@Test public void Xwiki_site_sidebar() {// PURPOSE: make sure sidebar links don't fail; DATE:2014-01-21
|
||||
fxt.Init_exec("file:///site/en.wikipedia.org/wiki/A").Expd_tid_(Xoh_href.Tid_site).Expd_wiki_("en.wikipedia.org").Expd_page_("A").Test();
|
||||
}
|
||||
@Test public void Xwiki_site_logo() { // DATE:2014-01-21
|
||||
fxt.Init_exec("file:///site/en.wikipedia.org/wiki/").Expd_tid_(Xoh_href.Tid_site).Expd_wiki_("en.wikipedia.org").Expd_page_("Main_Page").Test();
|
||||
fxt.Init_exec("file:///site/en.wikipedia.org/wiki").Expd_tid_(Xoh_href.Tid_site).Expd_wiki_("en.wikipedia.org").Expd_page_("Main_Page").Test();
|
||||
fxt.Init_exec("file:///site/en.wikipedia.org/").Expd_tid_(Xoh_href.Tid_site).Expd_wiki_("en.wikipedia.org").Expd_page_("Main_Page").Test();
|
||||
}
|
||||
@Test public void Main_page() {// PURPOSE: Main_page does not update to use Main_page of xwiki; DATE:2014-02-23
|
||||
fxt.App().User().Wiki().Xwiki_mgr().Add_full("zh.wikipedia.org", "zh.wikipedia.org");
|
||||
gplx.xowa.wikis.Xoa_wiki_regy.Make_wiki_dir(fxt.App(), "zh.wikipedia.org");
|
||||
fxt.App().Wiki_mgr().Get_by_key_or_make(Bry_.new_ascii_("zh.wikipedia.org")).Props().Main_page_(Bry_.new_ascii_("Zh_Main_Page"));
|
||||
fxt.App().Wiki_mgr().Get_by_key_or_make(Bry_.new_ascii_("en.wikipedia.org")).Props().Main_page_(Bry_.new_ascii_("En_Main_Page"));
|
||||
fxt.Init_exec("file:///site/zh.wikipedia.org/").Expd_tid_(Xoh_href.Tid_site).Expd_wiki_("zh.wikipedia.org").Expd_page_("Zh_Main_Page").Test();
|
||||
fxt.Init_exec("file:///site/en.wikipedia.org/").Expd_tid_(Xoh_href.Tid_site).Expd_wiki_("en.wikipedia.org").Expd_page_("En_Main_Page").Test(); // still stuck at Zh
|
||||
}
|
||||
}
|
||||
class Xog_url_wkr_fxt {
|
||||
private Xoa_app app; private Xow_wiki wiki;
|
||||
private Xog_win_itm win;
|
||||
private Xog_url_wkr url_wkr = new Xog_url_wkr();
|
||||
private String init_raw;
|
||||
public Xoa_app App() {return app;}
|
||||
public Xog_url_wkr_fxt Expd_tid_(byte v) {expd_tid = v; return this;} private byte expd_tid;
|
||||
public Xog_url_wkr_fxt Expd_wiki_(String v) {expd_wiki = v; return this;} private String expd_wiki;
|
||||
public Xog_url_wkr_fxt Expd_page_(String v) {expd_page = v; return this;} private String expd_page;
|
||||
public Xog_url_wkr_fxt Expd_anchor_(String v) {expd_anchor = v; return this;} private String expd_anchor;
|
||||
public Xog_url_wkr_fxt Expd_qargs_(String v) {expd_qargs = v; return this;} private String expd_qargs;
|
||||
public void Clear() {
|
||||
app = Xoa_app_fxt.app_();
|
||||
wiki = Xoa_app_fxt.wiki_tst_(app);
|
||||
Xoa_app_fxt.Init_gui(app);
|
||||
win = app.Gui_mgr().Browser_win();
|
||||
win.Active_page_(Xoa_page.test_(wiki, Xoa_ttl.parse_(wiki, Bry_.new_utf8_("test")))); // TODO: remove unnecessary page init
|
||||
expd_wiki = expd_page = expd_qargs = expd_anchor = null;
|
||||
}
|
||||
public Xog_url_wkr_fxt Init_exec(String raw) {
|
||||
this.init_raw = raw;
|
||||
return this;
|
||||
}
|
||||
public void Test() {
|
||||
Xoa_url url = url_wkr.Parse(win, init_raw).Exec();
|
||||
Tfds.Eq(expd_tid, url_wkr.Href_tid());
|
||||
Tfds.Eq(expd_page, String_.new_utf8_(url.Page_bry()));
|
||||
if (expd_wiki != null) Tfds.Eq(expd_wiki , String_.new_utf8_(url.Wiki_bry()));
|
||||
if (expd_anchor != null) Tfds.Eq(expd_anchor , String_.new_utf8_(url.Anchor_bry()));
|
||||
if (expd_qargs != null) Tfds.Eq(expd_qargs , String_.new_utf8_(url.Args_all_as_bry()));
|
||||
}
|
||||
}
|
||||
@@ -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/>.
|
||||
*/
|
||||
package gplx.xowa.gui.urls.url_macros; import gplx.*; import gplx.xowa.*; import gplx.xowa.gui.*; import gplx.xowa.gui.urls.*;
|
||||
public class Xog_url_macro_grp implements GfoInvkAble {
|
||||
public ByteTrieMgr_slim Trie() {return trie;} private ByteTrieMgr_slim trie = ByteTrieMgr_slim.cs_();
|
||||
public void Del(byte[] abrv) {trie.Del(abrv);}
|
||||
public void Set(String abrv, String fmt) {Set(Bry_.new_utf8_(abrv), Bry_.new_utf8_(fmt));}
|
||||
public void Set(byte[] abrv, byte[] fmt) {trie.Add(abrv, new Xog_url_macro_itm(abrv, fmt));}
|
||||
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
|
||||
if (ctx.Match(k, Invk_clear)) trie.Clear();
|
||||
else if (ctx.Match(k, Invk_set)) Set(m.ReadBry("abrv"), m.ReadBry("fmt"));
|
||||
else if (ctx.Match(k, Invk_del)) Del(m.ReadBry("abrv"));
|
||||
else return GfoInvkAble_.Rv_unhandled;
|
||||
return this;
|
||||
}
|
||||
private static final String Invk_clear = "clear", Invk_set = "set", Invk_del = "del";
|
||||
}
|
||||
class Xog_url_macro_itm {
|
||||
private Bry_fmtr fmtr;
|
||||
public Xog_url_macro_itm(byte[] abrv, byte[] fmt) {this.abrv = abrv; this.fmt = fmt;}
|
||||
public byte[] Abrv() {return abrv;} private byte[] abrv;
|
||||
public byte[] Fmt() {return fmt;} private byte[] fmt;
|
||||
public byte[] Fmtr_exec(Bry_bfr bfr, Object... args) {
|
||||
if (fmtr == null) fmtr = new Bry_fmtr().Fmt_(fmt).Compile();
|
||||
fmtr.Bld_bfr_many(bfr, args);
|
||||
return bfr.XtoAryAndClear();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
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.gui.urls.url_macros; import gplx.*; import gplx.xowa.*; import gplx.xowa.gui.*; import gplx.xowa.gui.urls.*;
|
||||
public class Xog_url_macro_mgr {
|
||||
private Bry_bfr bfr = Bry_bfr.reset_(255);
|
||||
public Xog_url_macro_mgr() {
|
||||
this.Init();
|
||||
}
|
||||
public void Lang_default_(byte[] v) {lang_default = v;} private byte[] lang_default = Bry_.new_ascii_("en");
|
||||
public Xog_url_macro_grp Types_mgr() {return types_mgr;} private Xog_url_macro_grp types_mgr = new Xog_url_macro_grp();
|
||||
public Xog_url_macro_grp Custom_mgr() {return custom_mgr;} private Xog_url_macro_grp custom_mgr = new Xog_url_macro_grp();
|
||||
public byte[] Fmt_or_null(byte[] raw) {
|
||||
int raw_len = raw.length;
|
||||
int dot_pos = -1;
|
||||
for (int i = 0; i < raw_len; i++) {
|
||||
byte b = raw[i];
|
||||
switch (b) {
|
||||
case Byte_ascii.Dot:
|
||||
dot_pos = i;
|
||||
break;
|
||||
case Byte_ascii.Colon:
|
||||
return Fmt_or_null__type(raw, raw_len, dot_pos, i);
|
||||
}
|
||||
}
|
||||
return Unhandled;
|
||||
}
|
||||
private byte[] Fmt_or_null__type(byte[] raw, int raw_len, int dot_pos, int colon_pos) {
|
||||
boolean dot_missing = dot_pos == -1;
|
||||
int type_bgn = dot_pos + 1, type_end = colon_pos; // +1 to start type after dot;
|
||||
if (dot_missing) type_bgn = 0;
|
||||
Object custom_obj = custom_mgr.Trie().MatchAtCurExact(raw, 0, type_end); // match entire prefix
|
||||
if (custom_obj == null) {
|
||||
Object type_obj = types_mgr.Trie().MatchAtCurExact(raw, type_bgn, type_end);
|
||||
if (type_obj == null) return Unhandled; // type abrv is not known; exit; EX: "en.unknown:Page"; "Page"
|
||||
byte[] lang_bry = dot_missing ? lang_default : Bry_.Mid(raw, 0, dot_pos);
|
||||
Xog_url_macro_itm type_itm = (Xog_url_macro_itm)type_obj;
|
||||
return type_itm.Fmtr_exec(bfr, lang_bry, Bry_.Mid(raw, colon_pos + 1, raw_len));
|
||||
}
|
||||
else {
|
||||
Xog_url_macro_itm custom_itm = (Xog_url_macro_itm)custom_obj;
|
||||
return custom_itm.Fmtr_exec(bfr, Bry_.Mid(raw, colon_pos + 1, raw_len));
|
||||
}
|
||||
}
|
||||
private void Init() {
|
||||
types_mgr.Set("w" , "~{0}.wikipedia.org/wiki/~{1}");
|
||||
types_mgr.Set("d" , "~{0}.wiktionary.org/wiki/~{1}");
|
||||
types_mgr.Set("s" , "~{0}.wikisource.org/wiki/~{1}");
|
||||
types_mgr.Set("v" , "~{0}.wikivoyage.org/wiki/~{1}");
|
||||
types_mgr.Set("q" , "~{0}.wikiquote.org/wiki/~{1}");
|
||||
types_mgr.Set("b" , "~{0}.wikibooks.org/wiki/~{1}");
|
||||
types_mgr.Set("u" , "~{0}.wikiversity.org/wiki/~{1}");
|
||||
types_mgr.Set("n" , "~{0}.wikinews.org/wiki/~{1}");
|
||||
types_mgr.Set("a" , "~{0}.wikia.com/wiki/~{1}");
|
||||
custom_mgr.Set("c" , "commons.wikimedia.org/wiki/~{0}");
|
||||
custom_mgr.Set("wd" , "www.wikidata.org/wiki/~{0}");
|
||||
custom_mgr.Set("wd.q" , "www.wikidata.org/wiki/Q~{0}");
|
||||
custom_mgr.Set("wd.p" , "www.wikidata.org/wiki/Property:P~{0}");
|
||||
custom_mgr.Set("sp" , "wikispecies.wikimedia.org/wiki/~{0}");
|
||||
custom_mgr.Set("meta" , "meta.wikimedia.org/wiki/~{0}");
|
||||
custom_mgr.Set("s.w" , "simple.wikipedia.org/wiki/~{0}");
|
||||
custom_mgr.Set("s.d" , "simple.wiktionary.org/wiki/~{0}");
|
||||
custom_mgr.Set("s.b" , "simple.wikibooks.org/wiki/~{0}");
|
||||
custom_mgr.Set("s.q" , "simple.wikiquote.org/wiki/~{0}");
|
||||
custom_mgr.Set("?" , "Special:Search/~{0}?fulltext=y");
|
||||
}
|
||||
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
|
||||
if (ctx.Match(k, Invk_lang_default)) return String_.new_utf8_(lang_default);
|
||||
else if (ctx.Match(k, Invk_lang_default_)) lang_default = m.ReadBry("v");
|
||||
else if (ctx.Match(k, Invk_types)) return types_mgr;
|
||||
else if (ctx.Match(k, Invk_custom)) return custom_mgr;
|
||||
else return GfoInvkAble_.Rv_unhandled;
|
||||
return this;
|
||||
}
|
||||
private static final String Invk_lang_default = "lang_default", Invk_lang_default_ = "lang_default_", Invk_types = "types", Invk_custom = "custom";
|
||||
public static final byte[] Unhandled = null;
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
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.gui.urls.url_macros; import gplx.*; import gplx.xowa.*; import gplx.xowa.gui.*; import gplx.xowa.gui.urls.*;
|
||||
import org.junit.*;
|
||||
public class Xog_url_macro_mgr_tst {
|
||||
@Before public void init() {fxt.Clear();} private Xog_url_macro_mgr_fxt fxt = new Xog_url_macro_mgr_fxt();
|
||||
@Test public void Custom_basic() {fxt.Test("?:Page" , "Special:Search/Page?fulltext=y");}
|
||||
@Test public void Type_basic() {fxt.Test("en.w:Page" , "en.wikipedia.org/wiki/Page");}
|
||||
@Test public void Type_main() {fxt.Test("en.w:" , "en.wikipedia.org/wiki/");}
|
||||
@Test public void Type_tid() {fxt.Test("w:Page" , "en.wikipedia.org/wiki/Page");}
|
||||
@Test public void Type_lang() {fxt.Test("fr.w:Page" , "fr.wikipedia.org/wiki/Page");}
|
||||
@Test public void Type_unhandled() {fxt.Test("x:A" , null);}
|
||||
@Test public void Type_unhandled_ns() {fxt.Test("Help:A" , null);}
|
||||
@Test public void Type_custom() {fxt.Test("wd.q:123" , "www.wikidata.org/wiki/Q123");}
|
||||
@Test public void Type_del() {
|
||||
fxt.Test("w:A", "en.wikipedia.org/wiki/A");
|
||||
fxt.Abrv_mgr().Types_mgr().Del(Bry_.new_utf8_("w"));
|
||||
fxt.Test("w:A", null);
|
||||
}
|
||||
@Test public void Type_set() {
|
||||
fxt.Abrv_mgr().Types_mgr().Set("w", "~{0}.~{1}");
|
||||
fxt.Test("w.A", null);
|
||||
}
|
||||
@Test public void Lang_default() {
|
||||
fxt.Abrv_mgr().Lang_default_(Bry_.new_ascii_("fr"));
|
||||
fxt.Test("w:Page", "fr.wikipedia.org/wiki/Page");
|
||||
}
|
||||
@Test public void Precedence() { // PURPOSE: Custom should take precedence over type
|
||||
fxt.Abrv_mgr().Custom_mgr().Set("w", "custom:~{0}");
|
||||
fxt.Test("w:Page" , "custom:Page");
|
||||
}
|
||||
}
|
||||
class Xog_url_macro_mgr_fxt {
|
||||
public void Clear() {
|
||||
abrv_mgr = new Xog_url_macro_mgr();
|
||||
}
|
||||
public Xog_url_macro_mgr Abrv_mgr() {return abrv_mgr;} private Xog_url_macro_mgr abrv_mgr;
|
||||
public void Test(String raw, String expd) {
|
||||
Tfds.Eq(expd, String_.new_ascii_(abrv_mgr.Fmt_or_null(Bry_.new_ascii_(raw))));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user