mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
v2.7.2.1
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
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.wikis.domains; import gplx.*; import gplx.xowa.*; import gplx.xowa.wikis.*;
|
||||
import gplx.xowa.langs.*;
|
||||
public class Xow_domain_abrv_xo_ {
|
||||
public static byte[] To_bry(byte[] domain_bry, Xol_lang_itm lang, Xow_domain_type type) { // en.wikipedia.org -> en.w
|
||||
byte[] type_abrv = type.Abrv();
|
||||
if (type.Multi_lang()) // wikipedia,wiktionary,etc..
|
||||
return Bry_.Add(lang.Key(), Byte_ascii.Dot_bry, type_abrv);
|
||||
else if (type_abrv.length > 0) // commons,wbase,species,etc..
|
||||
return type_abrv;
|
||||
else // home;wikia;others
|
||||
return domain_bry;
|
||||
}
|
||||
public static Xow_domain To_itm(byte[] src) {
|
||||
int src_len = src.length;
|
||||
byte[] domain_bry = src; // default to src; handles unknown abrv like "a.wikia.com";"xowa";others
|
||||
Xow_domain_type type = null;
|
||||
int dot_pos = Bry_finder.Find_fwd(src, Byte_ascii.Dot);
|
||||
if (dot_pos != Bry_finder.Not_found) { // dot found; EX: "en.w"
|
||||
type = Xow_domain_type_.Get_abrv_as_itm(src, dot_pos + 1, src_len);
|
||||
if (type != null) { // type found; EX: ".w"
|
||||
Xol_lang_itm lang = Xol_lang_itm_.Get_by_key(src, 0, dot_pos);
|
||||
if (lang != null) // lang found; EX: "en."
|
||||
domain_bry = Bry_.Add(lang.Key(), type.Domain_bry());
|
||||
}
|
||||
}
|
||||
else { // dot missing; EX: "c"
|
||||
type = Xow_domain_type_.Get_abrv_as_itm(src, 0, src_len);
|
||||
if (type != null) { // type found; EX: "c"
|
||||
domain_bry = type.Domain_bry();
|
||||
}
|
||||
}
|
||||
return Xow_domain_.parse(domain_bry); // for consolidation's sake, parse abrv to domain_bry and pass to Xow_domain_.parse_()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.xowa.wikis.domains; import gplx.*; import gplx.xowa.*; import gplx.xowa.wikis.*;
|
||||
import org.junit.*;
|
||||
public class Xow_domain_abrv_xo__tst {
|
||||
@Before public void init() {fxt.Clear();} private final Xow_domain_abrv_xo__fxt fxt = new Xow_domain_abrv_xo__fxt();
|
||||
@Test public void Basic() {
|
||||
fxt.Test("en.wikipedia.org" , "en.w"); // multi.enwiki
|
||||
fxt.Test("fr.wiktionary.org" , "fr.d"); // multi.frwiktionary
|
||||
fxt.Test("commons.wikimedia.org" , "c"); // important.unique.commons
|
||||
fxt.Test("www.wikidata.org" , "wd"); // important.unique.wikidata
|
||||
fxt.Test("home" , "home"); // important.unique.xowa
|
||||
fxt.Test("meta.wikimedia.org" , "meta"); // wikimedia.unique
|
||||
fxt.Test("pl.wikimedia.org" , "pl.m"); // wikimedia.multi
|
||||
fxt.Test("a.b.c" , "a.b.c"); // unkonwn
|
||||
}
|
||||
}
|
||||
class Xow_domain_abrv_xo__fxt {
|
||||
public void Clear() {}
|
||||
public void Test(String domain_str, String expd_abrv) {
|
||||
Xow_domain domain = Xow_domain_.parse(Bry_.new_u8(domain_str));
|
||||
byte[] actl_abrv = Xow_domain_abrv_xo_.To_bry(domain.Domain_bry(), domain.Lang_itm(), domain.Domain_type());
|
||||
Tfds.Eq(expd_abrv, String_.new_u8(actl_abrv), "To_bry");
|
||||
domain = Xow_domain_abrv_xo_.To_itm(actl_abrv);
|
||||
Tfds.Eq(domain_str, domain.Domain_str(), "To_itm");
|
||||
}
|
||||
}
|
||||
105
400_xowa/src/gplx/xowa/wikis/domains/Xow_domain_uid_.java
Normal file
105
400_xowa/src/gplx/xowa/wikis/domains/Xow_domain_uid_.java
Normal file
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
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.wikis.domains; import gplx.*; import gplx.xowa.*; import gplx.xowa.wikis.*;
|
||||
import gplx.xowa.langs.*;
|
||||
public class Xow_domain_uid_ {
|
||||
public static final int
|
||||
Tid_null = 0
|
||||
, Tid_xowa = 1
|
||||
, Tid_commons = 2
|
||||
, Tid_wikidata = 3
|
||||
, Tid_mediawiki = 20
|
||||
, Tid_meta = 21
|
||||
, Tid_incubator = 22
|
||||
, Tid_wmfblog = 23
|
||||
, Tid_species = 24
|
||||
;
|
||||
private static final int
|
||||
Tid_sub_wikipedia = 0
|
||||
, Tid_sub_wiktionary = 1
|
||||
, Tid_sub_wikisource = 2
|
||||
, Tid_sub_wikivoyage = 3
|
||||
, Tid_sub_wikiquote = 4
|
||||
, Tid_sub_wikibooks = 5
|
||||
, Tid_sub_wikiversity = 6
|
||||
, Tid_sub_wikinews = 7
|
||||
, Tid_sub_wikimedia = 8
|
||||
;
|
||||
private static final int Const_system_reserved = 100, Const_lang_reserved = 20;
|
||||
public static int To_int(Xow_domain domain) {
|
||||
int domain_tid = 0;
|
||||
switch (domain.Domain_tid()) {
|
||||
case Xow_domain_type_.Tid_home: return Tid_xowa;
|
||||
case Xow_domain_type_.Tid_commons: return Tid_commons;
|
||||
case Xow_domain_type_.Tid_wikidata: return Tid_wikidata;
|
||||
case Xow_domain_type_.Tid_mediawiki: return Tid_mediawiki;
|
||||
case Xow_domain_type_.Tid_meta: return Tid_meta;
|
||||
case Xow_domain_type_.Tid_incubator: return Tid_incubator;
|
||||
case Xow_domain_type_.Tid_wmfblog: return Tid_wmfblog;
|
||||
case Xow_domain_type_.Tid_species: return Tid_species;
|
||||
case Xow_domain_type_.Tid_wikipedia: domain_tid = Tid_sub_wikipedia; break;
|
||||
case Xow_domain_type_.Tid_wiktionary: domain_tid = Tid_sub_wiktionary; break;
|
||||
case Xow_domain_type_.Tid_wikisource: domain_tid = Tid_sub_wikisource; break;
|
||||
case Xow_domain_type_.Tid_wikivoyage: domain_tid = Tid_sub_wikivoyage; break;
|
||||
case Xow_domain_type_.Tid_wikiquote: domain_tid = Tid_sub_wikiquote; break;
|
||||
case Xow_domain_type_.Tid_wikibooks: domain_tid = Tid_sub_wikibooks; break;
|
||||
case Xow_domain_type_.Tid_wikiversity: domain_tid = Tid_sub_wikiversity; break;
|
||||
case Xow_domain_type_.Tid_wikinews: domain_tid = Tid_sub_wikinews; break;
|
||||
case Xow_domain_type_.Tid_wikimedia: domain_tid = Tid_sub_wikimedia; break;
|
||||
default: throw Exc_.new_unhandled(domain.Domain_tid());
|
||||
}
|
||||
return Const_system_reserved // reserve first 100 slots
|
||||
+ domain_tid // domain_tid assigned above
|
||||
+ (domain.Lang_uid() * Const_lang_reserved) // reserve 20 wikis per lang
|
||||
;
|
||||
}
|
||||
public static Xow_domain To_domain(int tid) {
|
||||
switch (tid) {
|
||||
case Tid_xowa: return Xow_domain.new_(Xow_domain_.Domain_bry_home, Xow_domain_type_.Tid_home, Xol_lang_itm_.Key__unknown);
|
||||
case Tid_commons: return Xow_domain.new_(Xow_domain_.Domain_bry_commons, Xow_domain_type_.Tid_commons, Xol_lang_itm_.Key__unknown);
|
||||
case Tid_wikidata: return Xow_domain.new_(Xow_domain_.Domain_bry_wikidata, Xow_domain_type_.Tid_commons, Xol_lang_itm_.Key__unknown);
|
||||
case Tid_mediawiki: return Xow_domain.new_(Xow_domain_.Domain_bry_mediawiki, Xow_domain_type_.Tid_mediawiki, Xol_lang_itm_.Key__unknown);
|
||||
case Tid_meta: return Xow_domain.new_(Xow_domain_.Domain_bry_meta, Xow_domain_type_.Tid_meta, Xol_lang_itm_.Key__unknown);
|
||||
case Tid_incubator: return Xow_domain.new_(Xow_domain_.Domain_bry_incubator, Xow_domain_type_.Tid_incubator, Xol_lang_itm_.Key__unknown);
|
||||
case Tid_wmfblog: return Xow_domain.new_(Xow_domain_.Domain_bry_wmforg, Xow_domain_type_.Tid_wmfblog, Xol_lang_itm_.Key__unknown);
|
||||
case Tid_species: return Xow_domain.new_(Xow_domain_.Domain_bry_species, Xow_domain_type_.Tid_species, Xol_lang_itm_.Key__unknown);
|
||||
}
|
||||
int tmp = tid - Const_system_reserved;
|
||||
int lang_id = tmp / 20;
|
||||
int type_id = tmp % 20;
|
||||
int tid_int = 0; byte[] tid_bry = null;
|
||||
switch (type_id) {
|
||||
case Tid_sub_wikipedia: tid_int = Xow_domain_type_.Tid_wikipedia; tid_bry = Xow_domain_type_.Key_bry_wikipedia; break;
|
||||
case Tid_sub_wiktionary: tid_int = Xow_domain_type_.Tid_wiktionary; tid_bry = Xow_domain_type_.Key_bry_wiktionary; break;
|
||||
case Tid_sub_wikisource: tid_int = Xow_domain_type_.Tid_wikisource; tid_bry = Xow_domain_type_.Key_bry_wikisource; break;
|
||||
case Tid_sub_wikivoyage: tid_int = Xow_domain_type_.Tid_wikivoyage; tid_bry = Xow_domain_type_.Key_bry_wikivoyage; break;
|
||||
case Tid_sub_wikiquote: tid_int = Xow_domain_type_.Tid_wikiquote; tid_bry = Xow_domain_type_.Key_bry_wikiquote; break;
|
||||
case Tid_sub_wikibooks: tid_int = Xow_domain_type_.Tid_wikibooks; tid_bry = Xow_domain_type_.Key_bry_wikibooks; break;
|
||||
case Tid_sub_wikiversity: tid_int = Xow_domain_type_.Tid_wikiversity; tid_bry = Xow_domain_type_.Key_bry_wikiversity; break;
|
||||
case Tid_sub_wikinews: tid_int = Xow_domain_type_.Tid_wikinews; tid_bry = Xow_domain_type_.Key_bry_wikinews; break;
|
||||
case Tid_sub_wikimedia: tid_int = Xow_domain_type_.Tid_wikimedia; tid_bry = Xow_domain_type_.Key_bry_wikimedia; break;
|
||||
default: throw Exc_.new_unhandled(type_id);
|
||||
}
|
||||
Xol_lang_itm lang = Xol_lang_itm_.Get_by_id(lang_id);
|
||||
Bry_bfr bfr = Xoa_app_.Utl__bfr_mkr().Get_b128();
|
||||
bfr.Add(lang.Key()).Add_byte_dot().Add(tid_bry).Add_byte_dot().Add(Xow_domain_.Seg_bry_org);
|
||||
byte[] domain_bry = bfr.Xto_bry_and_clear();
|
||||
bfr.Mkr_rls();
|
||||
return Xow_domain.new_(domain_bry, tid_int, lang);
|
||||
}
|
||||
}
|
||||
@@ -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.wikis.domains; import gplx.*; import gplx.xowa.*; import gplx.xowa.wikis.*;
|
||||
import org.junit.*;
|
||||
public class Xow_domain_uid__tst {
|
||||
@Before public void init() {fxt.Clear();} private final Xow_domain_uid__fxt fxt = new Xow_domain_uid__fxt();
|
||||
@Test public void Basic() {
|
||||
fxt.Test(Xow_domain_uid_.Tid_commons , "commons.wikimedia.org" , "", Xow_domain_type_.Tid_commons);
|
||||
fxt.Test(100 , "en.wikipedia.org" , "en", Xow_domain_type_.Tid_wikipedia);
|
||||
}
|
||||
}
|
||||
class Xow_domain_uid__fxt {
|
||||
public void Clear() {}
|
||||
public void Test(int tid, String domain_str, String expd_lang, int expd_tid) {
|
||||
byte[] domain_bry = Bry_.new_a7(domain_str);
|
||||
Xow_domain actl_domain = Xow_domain_uid_.To_domain(tid);
|
||||
Tfds.Eq_bry(domain_bry , actl_domain.Domain_bry());
|
||||
Tfds.Eq_bry(Bry_.new_a7(expd_lang) , actl_domain.Lang_key());
|
||||
Tfds.Eq(expd_tid , actl_domain.Domain_tid());
|
||||
Tfds.Eq(tid, Xow_domain_uid_.To_int(actl_domain));
|
||||
}
|
||||
}
|
||||
@@ -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.xowa.wikis.domains.crts; import gplx.*; import gplx.xowa.*; import gplx.xowa.wikis.*; import gplx.xowa.wikis.domains.*;
|
||||
import gplx.core.primitives.*;
|
||||
public interface Xow_domain_crt_itm {
|
||||
boolean Matches(Xow_domain cur, Xow_domain comp);
|
||||
}
|
||||
@@ -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.xowa.wikis.domains.crts; import gplx.*; import gplx.xowa.*; import gplx.xowa.wikis.*; import gplx.xowa.wikis.domains.*;
|
||||
class Xow_domain_crt_itm_ {
|
||||
public static final Xow_domain_crt_itm Null = null;
|
||||
}
|
||||
class Xow_domain_crt_itm__any_wiki implements Xow_domain_crt_itm {
|
||||
public boolean Matches(Xow_domain cur, Xow_domain comp) {return true;}
|
||||
public static final Xow_domain_crt_itm__any_wiki I = new Xow_domain_crt_itm__any_wiki(); Xow_domain_crt_itm__any_wiki() {}
|
||||
}
|
||||
class Xow_domain_crt_itm__in implements Xow_domain_crt_itm {
|
||||
private final Xow_domain_crt_itm[] ary;
|
||||
public Xow_domain_crt_itm__in(Xow_domain_crt_itm[] ary) {this.ary = ary;}
|
||||
public boolean Matches(Xow_domain cur, Xow_domain comp) {
|
||||
int len = ary.length;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Xow_domain_crt_itm itm = ary[i];
|
||||
if (itm.Matches(cur, comp)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
class Xow_domain_crt_itm__any_standard implements Xow_domain_crt_itm {
|
||||
public boolean Matches(Xow_domain cur, Xow_domain comp) {
|
||||
switch (comp.Domain_tid()) {
|
||||
case Xow_domain_type_.Tid_wikipedia:
|
||||
case Xow_domain_type_.Tid_wiktionary:
|
||||
case Xow_domain_type_.Tid_wikisource:
|
||||
case Xow_domain_type_.Tid_wikivoyage:
|
||||
case Xow_domain_type_.Tid_wikiquote:
|
||||
case Xow_domain_type_.Tid_wikibooks:
|
||||
case Xow_domain_type_.Tid_wikiversity:
|
||||
case Xow_domain_type_.Tid_wikinews: return true;
|
||||
default: return false;
|
||||
}
|
||||
}
|
||||
public static final Xow_domain_crt_itm__any_standard I = new Xow_domain_crt_itm__any_standard(); Xow_domain_crt_itm__any_standard() {}
|
||||
}
|
||||
class Xow_domain_crt_itm__none implements Xow_domain_crt_itm {
|
||||
public boolean Matches(Xow_domain cur, Xow_domain comp) {return false;}
|
||||
public static final Xow_domain_crt_itm__none I = new Xow_domain_crt_itm__none(); Xow_domain_crt_itm__none() {}
|
||||
}
|
||||
class Xow_domain_crt_itm__self implements Xow_domain_crt_itm {
|
||||
public boolean Matches(Xow_domain cur, Xow_domain comp) {return Bry_.Eq(cur.Domain_bry(), comp.Domain_bry());}
|
||||
public static final Xow_domain_crt_itm__self I = new Xow_domain_crt_itm__self(); Xow_domain_crt_itm__self() {}
|
||||
}
|
||||
class Xow_domain_crt_itm__same_lang implements Xow_domain_crt_itm {
|
||||
public boolean Matches(Xow_domain cur, Xow_domain comp) {return Bry_.Eq(cur.Lang_orig_key(), comp.Lang_orig_key());}
|
||||
public static final Xow_domain_crt_itm__same_lang I = new Xow_domain_crt_itm__same_lang(); Xow_domain_crt_itm__same_lang() {}
|
||||
}
|
||||
class Xow_domain_crt_itm__same_type implements Xow_domain_crt_itm {
|
||||
public boolean Matches(Xow_domain cur, Xow_domain comp) {return cur.Domain_tid() == comp.Domain_tid();}
|
||||
public static final Xow_domain_crt_itm__same_type I = new Xow_domain_crt_itm__same_type(); Xow_domain_crt_itm__same_type() {}
|
||||
}
|
||||
class Xow_domain_crt_itm__lang implements Xow_domain_crt_itm {
|
||||
private final int lang_uid;
|
||||
public Xow_domain_crt_itm__lang(int lang_uid) {this.lang_uid = lang_uid;}
|
||||
public boolean Matches(Xow_domain cur, Xow_domain comp) {return comp.Lang_orig_uid() == lang_uid;}
|
||||
}
|
||||
class Xow_domain_crt_itm__type implements Xow_domain_crt_itm {
|
||||
private final int wiki_tid;
|
||||
public Xow_domain_crt_itm__type(int wiki_tid) {this.wiki_tid = wiki_tid;}
|
||||
public boolean Matches(Xow_domain cur, Xow_domain comp) {return comp.Domain_tid() == wiki_tid;}
|
||||
}
|
||||
class Xow_domain_crt_itm__wiki implements Xow_domain_crt_itm {
|
||||
private final byte[] domain;
|
||||
public Xow_domain_crt_itm__wiki(byte[] domain) {this.domain = domain;}
|
||||
public boolean Matches(Xow_domain cur, Xow_domain comp) {return Bry_.Eq(comp.Domain_bry(), domain);}
|
||||
}
|
||||
@@ -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.wikis.domains.crts; import gplx.*; import gplx.xowa.*; import gplx.xowa.wikis.*; import gplx.xowa.wikis.domains.*;
|
||||
import gplx.xowa.langs.*;
|
||||
class Xow_domain_crt_itm_parser {
|
||||
public Xow_domain_crt_kv_itm[] Parse_as_kv_itms_or_null(byte[] raw) {
|
||||
List_adp rv = Parse_as_obj_or_null(raw, Bool_.N);
|
||||
return rv == null ? null : (Xow_domain_crt_kv_itm[])rv.To_ary_and_clear(Xow_domain_crt_kv_itm.class);
|
||||
}
|
||||
public Xow_domain_crt_kv_ary[] Parse_as_kv_arys_or_null(byte[] raw) {
|
||||
List_adp rv = Parse_as_obj_or_null(raw, Bool_.Y);
|
||||
return rv == null ? null : (Xow_domain_crt_kv_ary[])rv.To_ary_and_clear(Xow_domain_crt_kv_ary.class);
|
||||
}
|
||||
public List_adp Parse_as_obj_or_null(byte[] raw, boolean is_ary) {
|
||||
List_adp rv = List_adp_.new_();
|
||||
byte[][] line_ary = Bry_.Split_lines(raw);
|
||||
int line_len = line_ary.length;
|
||||
for (int i = 0; i < line_len; ++i) {
|
||||
byte[] line = line_ary[i];
|
||||
if (line.length == 0) continue; // ignore blank lines
|
||||
byte[][] word_ary = Bry_.Split(line, Byte_ascii.Pipe);
|
||||
int word_len = word_ary.length;
|
||||
if (word_len != 2) return null; // not A|B; exit now;
|
||||
Xow_domain_crt_itm key_itm = Xow_domain_crt_itm_parser.I.Parse_as_in(word_ary[0]);
|
||||
if (key_itm == Xow_domain_crt_itm_.Null) return null; // invalid key; exit;
|
||||
if (is_ary) {
|
||||
Xow_domain_crt_itm[] ary_itm = Xow_domain_crt_itm_parser.I.Parse_as_ary(word_ary[1]);
|
||||
if (ary_itm == null) return null;
|
||||
rv.Add(new Xow_domain_crt_kv_ary(key_itm, ary_itm));
|
||||
}
|
||||
else {
|
||||
Xow_domain_crt_itm val_itm = Xow_domain_crt_itm_parser.I.Parse_as_in(word_ary[1]);
|
||||
if (val_itm == Xow_domain_crt_itm_.Null) return null; // invalid val; exit;
|
||||
rv.Add(new Xow_domain_crt_kv_itm(key_itm, val_itm));
|
||||
}
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public Xow_domain_crt_itm Parse_as_in(byte[] raw) {
|
||||
Xow_domain_crt_itm[] in_ary = Parse_as_ary(raw);
|
||||
return in_ary == null ? Xow_domain_crt_itm_.Null : new Xow_domain_crt_itm__in(in_ary);
|
||||
}
|
||||
public Xow_domain_crt_itm[] Parse_as_ary(byte[] raw) {
|
||||
byte[][] terms = Bry_.Split(raw, Byte_ascii.Comma, Bool_.Y);
|
||||
int len = terms.length;
|
||||
Xow_domain_crt_itm[] rv_ary = new Xow_domain_crt_itm[len];
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Xow_domain_crt_itm itm = Parse_itm(terms[i]);
|
||||
if (itm == Xow_domain_crt_itm_.Null) return null; // invalid val; exit;
|
||||
rv_ary[i] = itm;
|
||||
}
|
||||
return rv_ary;
|
||||
}
|
||||
public Xow_domain_crt_itm Parse_itm(byte[] raw) {
|
||||
Xow_domain_crt_itm rv = (Xow_domain_crt_itm)itm_hash.Get_by_bry(raw); if (rv != null) return rv; // singleton; EX: <self>, <same_type>, etc..
|
||||
int raw_len = raw.length;
|
||||
if (Bry_.Has_at_bgn(raw, Wild_lang)) { // EX: *.wikipedia
|
||||
int wiki_tid = Xow_domain_type_.Get_type_as_tid(raw, Wild_lang.length, raw_len);
|
||||
return wiki_tid == Xow_domain_type_.Tid_null ? Xow_domain_crt_itm_.Null : new Xow_domain_crt_itm__type(wiki_tid);
|
||||
}
|
||||
else if (Bry_.Has_at_end(raw, Wild_type)) { // EX: en.*
|
||||
Xol_lang_itm lang_itm = Xol_lang_itm_.Get_by_key(raw, 0, raw_len - Wild_type.length);
|
||||
return lang_itm == null ? Xow_domain_crt_itm_.Null : new Xow_domain_crt_itm__lang(lang_itm.Id());
|
||||
}
|
||||
else
|
||||
return new Xow_domain_crt_itm__wiki(raw); // EX: en.wikipedia.org
|
||||
}
|
||||
private static final Hash_adp_bry itm_hash = Hash_adp_bry.cs_()
|
||||
.Add_str_obj("<self>" , Xow_domain_crt_itm__self.I)
|
||||
.Add_str_obj("<same_type>" , Xow_domain_crt_itm__same_type.I)
|
||||
.Add_str_obj("<same_lang>" , Xow_domain_crt_itm__same_lang.I)
|
||||
.Add_str_obj("<any>" , Xow_domain_crt_itm__any_wiki.I)
|
||||
;
|
||||
private static final byte[] Wild_lang = Bry_.new_a7("*."), Wild_type = Bry_.new_a7(".*");
|
||||
public static final Xow_domain_crt_itm_parser I = new Xow_domain_crt_itm_parser(); Xow_domain_crt_itm_parser() {}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.xowa.wikis.domains.crts; import gplx.*; import gplx.xowa.*; import gplx.xowa.wikis.*; import gplx.xowa.wikis.domains.*;
|
||||
public class Xow_domain_crt_kv_itm_mgr {
|
||||
private final List_adp list = List_adp_.new_();
|
||||
public void Clear() {list.Clear();}
|
||||
@gplx.Internal protected void Add(Xow_domain_crt_kv_itm itm) {list.Add(itm);}
|
||||
public boolean Parse_as_itms(byte[] raw) {
|
||||
this.Clear();
|
||||
Xow_domain_crt_kv_itm[] ary = Xow_domain_crt_itm_parser.I.Parse_as_kv_itms_or_null(raw);
|
||||
if (ary == null) return false; // invalid parse; leave current value as is and exit;
|
||||
int len = ary.length;
|
||||
for (int i = 0; i < len; ++i)
|
||||
this.Add(ary[i]);
|
||||
return true;
|
||||
}
|
||||
public boolean Parse_as_arys(byte[] raw) {
|
||||
this.Clear();
|
||||
Xow_domain_crt_kv_ary[] ary = Xow_domain_crt_itm_parser.I.Parse_as_kv_arys_or_null(raw);
|
||||
if (ary == null) return false; // invalid parse; leave current value as is and exit;
|
||||
int len = ary.length;
|
||||
for (int i = 0; i < len; ++i)
|
||||
list.Add(ary[i]);
|
||||
return true;
|
||||
}
|
||||
public Xow_domain_crt_itm Find_itm(Xow_domain cur, Xow_domain comp) {
|
||||
int len = list.Count();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Xow_domain_crt_kv_itm kv = (Xow_domain_crt_kv_itm)list.Get_at(i);
|
||||
if (kv.Key().Matches(cur, comp)) return kv.Val();
|
||||
}
|
||||
return Xow_domain_crt_itm__none.I;
|
||||
}
|
||||
public Xow_domain_crt_itm[] Find_ary(Xow_domain cur, Xow_domain comp) {
|
||||
int len = list.Count();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Xow_domain_crt_kv_ary kv = (Xow_domain_crt_kv_ary)list.Get_at(i);
|
||||
if (kv.Key().Matches(cur, comp)) return kv.Val();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
class Xow_domain_crt_kv_itm {
|
||||
public Xow_domain_crt_kv_itm(Xow_domain_crt_itm key, Xow_domain_crt_itm val) {this.key = key; this.val = val;}
|
||||
public Xow_domain_crt_itm Key() {return key;} private final Xow_domain_crt_itm key;
|
||||
public Xow_domain_crt_itm Val() {return val;} private final Xow_domain_crt_itm val;
|
||||
}
|
||||
class Xow_domain_crt_kv_ary {
|
||||
public Xow_domain_crt_kv_ary(Xow_domain_crt_itm key, Xow_domain_crt_itm[] val) {this.key = key; this.val = val;}
|
||||
public Xow_domain_crt_itm Key() {return key;} private final Xow_domain_crt_itm key;
|
||||
public Xow_domain_crt_itm[] Val() {return val;} private final Xow_domain_crt_itm[] val;
|
||||
}
|
||||
Reference in New Issue
Block a user