mirror of
https://github.com/gnosygnu/xowa.git
synced 2025-05-31 22:44:34 +00:00
Wikibase: Refactor wbase prop_writer to static functions
This commit is contained in:
parent
6748463711
commit
c1100c6d66
@ -22,7 +22,7 @@ class Scrib_lib_wikibase_srl_visitor implements Wbase_claim_visitor {
|
||||
public void Visit_str(Wbase_claim_string itm) {
|
||||
rv = new Keyval[2];
|
||||
rv[0] = Keyval_.new_(Scrib_lib_wikibase_srl.Key_type, Wbase_claim_type_.Reg.Get_str_or(itm.Val_tid(), Wbase_claim_type_.Itm__unknown.Key_str()));
|
||||
rv[1] = Keyval_.new_(Scrib_lib_wikibase_srl.Key_value, String_.new_u8(itm.Val_str()));
|
||||
rv[1] = Keyval_.new_(Scrib_lib_wikibase_srl.Key_value, String_.new_u8(itm.Val_bry()));
|
||||
}
|
||||
public void Visit_entity(Wbase_claim_entity itm) {
|
||||
rv = new Keyval[2];
|
||||
|
@ -97,7 +97,7 @@ public class Wdata_doc_wtr {
|
||||
case Wbase_claim_type_.Tid__string:
|
||||
Wbase_claim_string claim_str = (Wbase_claim_string)prop;
|
||||
wtr.Val(Bool_.Y, Wbase_claim_type_.Itm__string.Key_bry());
|
||||
wtr.Val(Bool_.Y, claim_str.Val_str());
|
||||
wtr.Val(Bool_.Y, claim_str.Val_bry());
|
||||
break;
|
||||
case Wbase_claim_type_.Tid__entity:
|
||||
Wbase_claim_entity claim_entity = (Wbase_claim_entity)prop;
|
||||
|
@ -18,24 +18,36 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx.xowa.xtns.wbases; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
|
||||
import gplx.core.brys.fmtrs.*;
|
||||
import gplx.xowa.langs.*;
|
||||
import gplx.xowa.xtns.wbases.core.*; import gplx.xowa.xtns.wbases.claims.*; import gplx.xowa.xtns.wbases.claims.itms.*; import gplx.xowa.xtns.wbases.hwtrs.*;
|
||||
class Wdata_prop_val_visitor implements Wbase_claim_visitor {
|
||||
private Wdata_wiki_mgr wdata_mgr; private Xoae_app app; private Bry_bfr bfr; private byte[] lang_key;
|
||||
import gplx.xowa.xtns.wbases.core.*; import gplx.xowa.xtns.wbases.claims.*; import gplx.xowa.xtns.wbases.claims.itms.*; import gplx.xowa.xtns.wbases.hwtrs.*; import gplx.xowa.xtns.wbases.claims.itms.times.*;
|
||||
public class Wdata_prop_val_visitor implements Wbase_claim_visitor {
|
||||
private Wdata_wiki_mgr wdata_mgr; private Xoae_app app; private Bry_bfr bfr;
|
||||
private Xol_lang_itm lang;
|
||||
private final Bry_bfr tmp_time_bfr = Bry_bfr_.Reset(255); private final Bry_fmtr tmp_time_fmtr = Bry_fmtr.new_();
|
||||
private Wdata_hwtr_msgs msgs;
|
||||
public Wdata_prop_val_visitor(Xoae_app app, Wdata_wiki_mgr wdata_mgr) {this.app = app; this.wdata_mgr = wdata_mgr;}
|
||||
public void Init(Bry_bfr bfr, Wdata_hwtr_msgs msgs, byte[] lang_key) {
|
||||
this.bfr = bfr; this.msgs = msgs; this.lang_key = lang_key;
|
||||
}
|
||||
public void Visit_str(Wbase_claim_string itm) {
|
||||
bfr.Add(itm.Val_str());
|
||||
this.bfr = bfr; this.msgs = msgs;
|
||||
this.lang = app.Lang_mgr().Get_by(lang_key);
|
||||
if (lang == null) lang = app.Lang_mgr().Lang_en(); // TEST: needed for one test; DATE:2016-10-20
|
||||
}
|
||||
public void Visit_str(Wbase_claim_string itm) {Write_str(bfr, itm.Val_bry());}
|
||||
public static void Write_str(Bry_bfr bfr, byte[] bry) {bfr.Add(bry);}
|
||||
public void Visit_time(Wbase_claim_time itm) {
|
||||
itm.Write_to_bfr(bfr, tmp_time_bfr, tmp_time_fmtr, msgs, Bry_.Empty); // for now, don't bother passing ttl; only used for error msg; DATE:2015-08-03
|
||||
Write_time(bfr, tmp_time_bfr, tmp_time_fmtr, msgs, Bry_.Empty, -1, itm.Time_as_date()); // for now, don't bother passing ttl; only used for error msg; DATE:2015-08-03
|
||||
}
|
||||
public void Visit_monolingualtext(Wbase_claim_monolingualtext itm) {bfr.Add(itm.Text());} // phrase only; PAGE:en.w:Alberta; EX: {{#property:motto}} -> "Fortis et libre"; DATE:2014-08-28
|
||||
public void Visit_entity(Wbase_claim_entity itm) {
|
||||
Wdata_doc entity_doc = wdata_mgr.Doc_mgr.Get_by_xid_or_null(itm.Page_ttl_db());
|
||||
public static void Write_time(Bry_bfr bfr, Bry_bfr tmp_bfr, Bry_fmtr tmp_fmtr, Wdata_hwtr_msgs msgs, byte[] page_url, int pid, Wbase_date date) {
|
||||
try {
|
||||
Wbase_date_.To_bfr(bfr, tmp_fmtr, tmp_bfr, msgs, date);
|
||||
if (date.Calendar_is_julian()) bfr.Add_byte_space().Add(msgs.Time_julian());
|
||||
} catch (Exception e) {
|
||||
Gfo_usr_dlg_.Instance.Warn_many("", "", "failed to write time; ttl=~{0} pid=~{1} err=~{2}", page_url, pid, Err_.Message_gplx_log(e));
|
||||
}
|
||||
}
|
||||
public void Visit_monolingualtext(Wbase_claim_monolingualtext itm) {Write_langtext(bfr, itm.Text());}
|
||||
public static void Write_langtext(Bry_bfr bfr, byte[] text) {bfr.Add(text);} // phrase only; PAGE:en.w:Alberta; EX: {{#property:motto}} -> "Fortis et libre"; DATE:2014-08-28
|
||||
public void Visit_entity(Wbase_claim_entity itm) {Write_entity(bfr, wdata_mgr, lang.Key_bry(), itm.Page_ttl_db());}
|
||||
public static void Write_entity(Bry_bfr bfr, Wdata_wiki_mgr wdata_mgr, byte[] lang_key, byte[] entity_ttl_db) {
|
||||
Wdata_doc entity_doc = wdata_mgr.Doc_mgr.Get_by_xid_or_null(entity_ttl_db);
|
||||
if (entity_doc == null) return; // NOTE: wiki may refer to entity that no longer exists; EX: {{#property:p1}} which links to Q1, but p1 links to Q2 and Q2 was deleted; DATE:2014-02-01
|
||||
byte[] label = entity_doc.Label_list__get(lang_key);
|
||||
if (label == null && !Bry_.Eq(lang_key, Xol_lang_itm_.Key_en)) // NOTE: some properties may not exist in language of wiki; default to english; DATE:2013-12-19
|
||||
@ -43,17 +55,14 @@ class Wdata_prop_val_visitor implements Wbase_claim_visitor {
|
||||
if (label != null) // if label is still not found, don't add null reference
|
||||
bfr.Add(label);
|
||||
}
|
||||
public void Visit_quantity(Wbase_claim_quantity itm) {
|
||||
// get val
|
||||
byte[] val_bry = itm.Amount();
|
||||
long val = Bry_.To_long_or(val_bry, Byte_ascii.Comma_bry, 0, val_bry.length, 0); // NOTE: must cast to long for large numbers; EX:{{#property:P1082}} PAGE:en.w:Earth; DATE:2015-08-02
|
||||
|
||||
// get lo, hi
|
||||
long lo = itm.Lbound_as_num().To_long();
|
||||
long hi = itm.Ubound_as_num().To_long();
|
||||
public void Visit_quantity(Wbase_claim_quantity itm) {Write_quantity(bfr, wdata_mgr, lang, itm.Amount(), itm.Lbound(), itm.Ubound(), itm.Unit());}
|
||||
public static void Write_quantity(Bry_bfr bfr, Wdata_wiki_mgr wdata_mgr, Xol_lang_itm lang, byte[] val_bry, byte[] lo_bry, byte[] hi_bry, byte[] unit) {
|
||||
// get val, lo, hi
|
||||
long val = Bry_.To_long_or(val_bry, Byte_ascii.Comma_bry, 0, val_bry.length, 0); // NOTE: must cast to long for large numbers; EX:{{#property:P1082}} PAGE:en.w:Earth; DATE:2015-08-02
|
||||
long lo = Bry_.To_long_or(lo_bry, Byte_ascii.Comma_bry, 0, lo_bry.length, 0);
|
||||
long hi = Bry_.To_long_or(hi_bry, Byte_ascii.Comma_bry, 0, hi_bry.length, 0);
|
||||
|
||||
// fmt val
|
||||
Xol_lang_itm lang = app.Lang_mgr().Get_by(lang_key);
|
||||
if (lo == val && hi == val) // lo, hi, val are same; print val only;
|
||||
bfr.Add(lang.Num_mgr().Format_num_by_long(val)); // amount; EX: 1,234
|
||||
else {
|
||||
@ -73,19 +82,20 @@ class Wdata_prop_val_visitor implements Wbase_claim_visitor {
|
||||
|
||||
// output unit
|
||||
bfr.Add_byte_space();
|
||||
int unit_qid_bgn = Bry_find_.Find_fwd(itm.Unit(), Wikidata_url);
|
||||
int unit_qid_bgn = Bry_find_.Find_fwd(unit, Wikidata_url);
|
||||
if (unit_qid_bgn == Bry_find_.Not_found) // entity missing; just output unit literally
|
||||
bfr.Add(itm.Unit()); // unit; EX: 1
|
||||
bfr.Add(unit); // unit; EX: "meter"
|
||||
else { // entity exists; EX:"http://www.wikidata.org/entity/Q11573" (meter)
|
||||
byte[] xid = Bry_.Mid(itm.Unit(), Wikidata_url.length);
|
||||
byte[] xid = Bry_.Mid(unit, Wikidata_url.length);
|
||||
Wdata_doc entity_doc = wdata_mgr.Doc_mgr.Get_by_xid_or_null(xid);
|
||||
bfr.Add(entity_doc.Label_list__get_or_fallback(lang));
|
||||
}
|
||||
}
|
||||
public void Visit_globecoordinate(Wbase_claim_globecoordinate itm) {
|
||||
bfr.Add(itm.Lat());
|
||||
public void Visit_globecoordinate(Wbase_claim_globecoordinate itm) {Write_geo(bfr, wdata_mgr, lang, itm.Lat(), itm.Lng());}
|
||||
public static void Write_geo(Bry_bfr bfr, Wdata_wiki_mgr wdata_mgr, Xol_lang_itm lang, byte[] lat, byte[] lng) {
|
||||
bfr.Add(lat);
|
||||
bfr.Add(Bry__geo_dlm);
|
||||
bfr.Add(itm.Lng());
|
||||
bfr.Add(lng);
|
||||
}
|
||||
private static final byte[] Wikidata_url = Bry_.new_a7("http://www.wikidata.org/entity/");
|
||||
private static final byte[] Bry__geo_dlm = Bry_.new_a7(", ");
|
||||
|
@ -63,76 +63,71 @@ public class Wdata_prop_val_visitor_ {
|
||||
}
|
||||
}
|
||||
|
||||
// loop datavalue_ary to get tid, val_obj
|
||||
// loop datavalue_ary to get (a) tid,; (b) val_obj
|
||||
byte tid = Byte_.Max_value_127;
|
||||
Object val_obj = null;
|
||||
len = datavalue_ary.length;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Keyval datavalue_itm = datavalue_ary[i];
|
||||
String key = datavalue_itm.Key();
|
||||
if (String_.Eq(key, Wdata_dict_datavalue.Itm__type.Key_str()))
|
||||
tid = Wbase_claim_type_.Get_tid_or_unknown((String)datavalue_itm.Val());
|
||||
else if (String_.Eq(key, Wdata_dict_datavalue.Itm__value.Key_str()))
|
||||
val_obj = datavalue_itm.Val();
|
||||
byte datavalue_tid = Wdata_dict_datavalue.Reg.Get_tid_or_max_and_log(page_url, datavalue_itm.Key()); if (datavalue_tid == Byte_.Max_value_127) continue;
|
||||
switch (datavalue_tid) {
|
||||
case Wdata_dict_datavalue.Tid__type: tid = Wbase_claim_type_.Get_tid_or_unknown((String)datavalue_itm.Val()); break;
|
||||
case Wdata_dict_datavalue.Tid__value: val_obj = datavalue_itm.Val(); break;
|
||||
case Wdata_dict_datavalue.Tid__error: break; // ignore: "Can only construct GlobeCoordinateValue with a String globe parameter"
|
||||
}
|
||||
}
|
||||
|
||||
// get claim and render
|
||||
// NOTE: converting to claim before writing; used to write directly; less efficient, but more consistent; DATE:2016-10-20
|
||||
Wbase_claim_base claim = null;
|
||||
// write claim
|
||||
switch (tid) {
|
||||
case Wbase_claim_type_.Tid__entity: claim = Make__entity (page_url, pid, snak_tid, (Keyval[])val_obj); break;
|
||||
case Wbase_claim_type_.Tid__string: claim = new Wbase_claim_string(pid, snak_tid, To_bry_by_str(val_obj)); break;
|
||||
case Wbase_claim_type_.Tid__time: claim = Make__time (page_url, pid, snak_tid, (Keyval[])val_obj); break;
|
||||
case Wbase_claim_type_.Tid__globecoordinate: claim = Make__geo (page_url, pid, snak_tid, (Keyval[])val_obj); break;
|
||||
case Wbase_claim_type_.Tid__quantity: claim = Make__quantity (page_url, pid, snak_tid, (Keyval[])val_obj); break;
|
||||
case Wbase_claim_type_.Tid__monolingualtext: claim = Make__langtext (page_url, pid, snak_tid, (Keyval[])val_obj); break;
|
||||
case Wbase_claim_type_.Tid__entity: Write_entity (bfr, wdata_mgr, lang, page_url, (Keyval[])val_obj); break;
|
||||
case Wbase_claim_type_.Tid__string: Wdata_prop_val_visitor.Write_str(bfr, To_bry_by_str(val_obj)); break;
|
||||
case Wbase_claim_type_.Tid__time: Write_time (bfr, wdata_mgr, wiki, page_url, pid, snak_tid, (Keyval[])val_obj); break;
|
||||
case Wbase_claim_type_.Tid__globecoordinate: Write_geo (bfr, wdata_mgr, lang, page_url, (Keyval[])val_obj); break;
|
||||
case Wbase_claim_type_.Tid__quantity: Write_quantity (bfr, wdata_mgr, lang, page_url, (Keyval[])val_obj); break;
|
||||
case Wbase_claim_type_.Tid__monolingualtext: Write_langtext (bfr, page_url, (Keyval[])val_obj); break;
|
||||
default: throw Err_.new_unhandled_default(tid);
|
||||
}
|
||||
wdata_mgr.Resolve_claim(bfr, wiki.Domain_itm(), claim);
|
||||
lang.Comma_wkr().Comma__itm(bfr, sub_idx, sub_len);
|
||||
}
|
||||
private static Wbase_claim_entity Make__entity(byte[] page_url, int pid, byte snak_tid, Keyval[] kvs) {
|
||||
private static void Write_entity(Bry_bfr bfr, Wdata_wiki_mgr wdata_mgr, Xol_lang_itm lang, byte[] page_url, Keyval[] kvs) {
|
||||
byte entity_tid = Byte_.Max_value_127;
|
||||
byte[] entity_id_bry = null;
|
||||
byte[] entity_id = null;
|
||||
|
||||
// get p/q and number; PAGE:en.v:Mongolia; EX: [numeric-id=6498663, entity-type=item]; DATE:2016-10-18
|
||||
int len = kvs.length;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Keyval kv = kvs[i];
|
||||
byte tid = Wbase_claim_entity_.Reg.Get_tid_or_max_and_log(page_url, kv.Key()); if (tid == Byte_.Max_value_127) continue;
|
||||
switch (tid) {
|
||||
case Wbase_claim_entity_.Tid__entity_type: entity_tid = Wbase_claim_entity_type_.Reg.Get_tid_or_fail(kv.Val_to_bry()); break;
|
||||
case Wbase_claim_entity_.Tid__numeric_id: entity_id_bry = kv.Val_to_bry(); break;
|
||||
case Wbase_claim_entity_.Tid__numeric_id: entity_id = kv.Val_to_bry(); break;
|
||||
case Wbase_claim_entity_.Tid__id: break; // ignore
|
||||
}
|
||||
}
|
||||
return new Wbase_claim_entity(pid, snak_tid, entity_tid, entity_id_bry);
|
||||
entity_id = Wbase_claim_entity.To_xid__db(entity_tid, entity_id); // get p/q and number; PAGE:en.v:Mongolia; EX: [numeric-id=6498663, entity-type=item]; DATE:2016-10-18
|
||||
Wdata_prop_val_visitor.Write_entity(bfr, wdata_mgr, lang.Key_bry(), entity_id);
|
||||
}
|
||||
private static Wbase_claim_time Make__time(byte[] page_url, int pid, byte snak_tid, Keyval[] kvs) {
|
||||
byte[] time = null, timezone = null;
|
||||
byte[] precision = null, before = null, after = null;
|
||||
byte[] calendar = null;
|
||||
private static void Write_time(Bry_bfr bfr, Wdata_wiki_mgr wdata_mgr, Xowe_wiki wiki, byte[] page_url, int pid, byte snak_tid, Keyval[] kvs) {
|
||||
byte[] time = null, calendar = null;
|
||||
int precision = 0, before = 0, after = 0;
|
||||
int len = kvs.length;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Keyval kv = kvs[i];
|
||||
byte val_tid = Wbase_claim_time_.Reg.Get_tid_or_max_and_log(page_url, kv.Key()); if (val_tid == Byte_.Max_value_127) continue;
|
||||
switch (val_tid) {
|
||||
case Wbase_claim_time_.Tid__time: time = To_bry_by_str(kv.Val()); break;
|
||||
case Wbase_claim_time_.Tid__before: before = To_bry_by_int(kv.Val()); break;
|
||||
case Wbase_claim_time_.Tid__after: after = To_bry_by_int(kv.Val()); break;
|
||||
case Wbase_claim_time_.Tid__precision: precision = To_bry_by_int(kv.Val()); break;
|
||||
case Wbase_claim_time_.Tid__calendarmodel: calendar = To_bry_by_str(kv.Val()); break; // Bry_.Eq(Bry_.new_u8((String)kv.Val()), Wbase_claim_time.Calendar_julian);
|
||||
case Wbase_claim_time_.Tid__timezone: timezone = To_bry_by_str(kv.Val()); break; // if (!String_.Eq((String)kv.Val(), "0")) throw Err_.new_unimplemented();
|
||||
case Wbase_claim_time_.Tid__before: before = Int_.cast(kv.Val()); break;
|
||||
case Wbase_claim_time_.Tid__after: after = Int_.cast(kv.Val()); break;
|
||||
case Wbase_claim_time_.Tid__precision: precision = Int_.cast(kv.Val()); break;
|
||||
case Wbase_claim_time_.Tid__calendarmodel: calendar = To_bry_by_str(kv.Val()); break;
|
||||
case Wbase_claim_time_.Tid__timezone: break;
|
||||
}
|
||||
}
|
||||
// Xow_parser_mgr parser_mgr = wiki.Parser_mgr();
|
||||
// date = Wbase_date.Parse(time, precision_int, before_int, after_int, calendar_is_julian);
|
||||
// Wbase_claim_time.Write_to_bfr(bfr, parser_mgr.Wbase__time__bfr(), parser_mgr.Wbase__time__fmtr()
|
||||
// , parser_mgr.Wbase__time__msgs(), page_url, Bry_.Empty, date, calendar_is_julian
|
||||
// );
|
||||
return new Wbase_claim_time(pid, snak_tid, time, timezone, before, after, precision, calendar);
|
||||
|
||||
Wbase_date date = Wbase_date_.Parse(time, precision, before, after, Bry_.Eq(calendar, Wbase_claim_time.Calendar_julian));
|
||||
Xow_parser_mgr parser_mgr = wiki.Parser_mgr();
|
||||
Wdata_prop_val_visitor.Write_time(bfr, parser_mgr.Wbase__time__bfr(), parser_mgr.Wbase__time__fmtr(), parser_mgr.Wbase__time__msgs(), page_url, pid, date);
|
||||
}
|
||||
private static Wbase_claim_quantity Make__quantity(byte[] page_url, int pid, byte snak_tid, Keyval[] kvs) {
|
||||
private static void Write_quantity(Bry_bfr bfr, Wdata_wiki_mgr wdata_mgr, Xol_lang_itm lang, byte[] page_url, Keyval[] kvs) {
|
||||
byte[] amount = null, lbound = null, ubound = null, unit = null;
|
||||
int len = kvs.length;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
@ -145,10 +140,10 @@ public class Wdata_prop_val_visitor_ {
|
||||
case Wbase_claim_quantity_.Tid__upperbound: ubound = To_bry_by_str(kv.Val()); break;
|
||||
}
|
||||
}
|
||||
return new Wbase_claim_quantity(pid, snak_tid, amount, unit, ubound, lbound);
|
||||
Wdata_prop_val_visitor.Write_quantity(bfr, wdata_mgr, lang, amount, lbound, ubound, unit);
|
||||
}
|
||||
private static Wbase_claim_globecoordinate Make__geo(byte[] page_url, int pid, byte snak_tid, Keyval[] kvs) {
|
||||
byte[] lat = null, lng = null, prc = null, alt = null, glb = null;
|
||||
private static void Write_geo(Bry_bfr bfr, Wdata_wiki_mgr wdata_mgr, Xol_lang_itm lang, byte[] page_url, Keyval[] kvs) {
|
||||
byte[] lat = null, lng = null;
|
||||
int len = kvs.length;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Keyval kv = kvs[i];
|
||||
@ -156,25 +151,25 @@ public class Wdata_prop_val_visitor_ {
|
||||
switch (val_tid) {
|
||||
case Wbase_claim_globecoordinate_.Tid__latitude: lat = To_bry_by_double(kv.Val()); break;
|
||||
case Wbase_claim_globecoordinate_.Tid__longitude: lng = To_bry_by_double(kv.Val()); break;
|
||||
case Wbase_claim_globecoordinate_.Tid__altitude: alt = To_bry_by_str(kv.Val()); break;
|
||||
case Wbase_claim_globecoordinate_.Tid__precision: prc = To_bry_by_double(kv.Val()); break;
|
||||
case Wbase_claim_globecoordinate_.Tid__globe: glb = To_bry_by_str(kv.Val()); break;
|
||||
case Wbase_claim_globecoordinate_.Tid__altitude: break;
|
||||
case Wbase_claim_globecoordinate_.Tid__precision: break;
|
||||
case Wbase_claim_globecoordinate_.Tid__globe: break;
|
||||
}
|
||||
}
|
||||
return new Wbase_claim_globecoordinate(pid, snak_tid, lat, lng, alt, prc, glb);
|
||||
Wdata_prop_val_visitor.Write_geo(bfr, wdata_mgr, lang, lat, lng);
|
||||
}
|
||||
private static Wbase_claim_monolingualtext Make__langtext(byte[] page_url, int pid, byte snak_tid, Keyval[] kvs) {
|
||||
byte[] lang = null, text = null;
|
||||
private static void Write_langtext(Bry_bfr bfr, byte[] page_url, Keyval[] kvs) {
|
||||
byte[] text = null;
|
||||
int len = kvs.length;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Keyval kv = kvs[i];
|
||||
byte val_tid = Wbase_claim_monolingualtext_.Reg.Get_tid_or_max_and_log(page_url, kv.Key()); if (val_tid == Byte_.Max_value_127) continue;
|
||||
switch (val_tid) {
|
||||
case Wbase_claim_monolingualtext_.Tid__language: lang = To_bry_by_str(kv.Val()); break;
|
||||
case Wbase_claim_monolingualtext_.Tid__text: text = To_bry_by_str(kv.Val()); break;
|
||||
case Wbase_claim_monolingualtext_.Tid__language: break;
|
||||
}
|
||||
}
|
||||
return new Wbase_claim_monolingualtext(pid, snak_tid, lang, text);
|
||||
Wdata_prop_val_visitor.Write_langtext(bfr, text);
|
||||
}
|
||||
private static int To_pid_int(byte[] pid) {return Bry_.To_int_or(pid, 1, pid.length, -1);} // skip "P" at bgn; EX: "p123" -> 123
|
||||
private static byte[] To_bry_by_str(Object o) {
|
||||
@ -185,8 +180,4 @@ public class Wdata_prop_val_visitor_ {
|
||||
double rv = Double_.cast(o);
|
||||
return Bry_.new_a7(Double_.To_str(rv));
|
||||
}
|
||||
private static byte[] To_bry_by_int(Object o) {
|
||||
int rv = Int_.cast(o);
|
||||
return Int_.To_bry(rv);
|
||||
}
|
||||
}
|
||||
|
@ -31,12 +31,7 @@ public class Wbase_claim_entity extends Wbase_claim_base {
|
||||
public String Entity_tid_str() {return Wbase_claim_entity_type_.Reg.Get_str_or_fail(entity_tid);}
|
||||
public byte[] Entity_tid_bry() {return Wbase_claim_entity_type_.Reg.Get_bry_or_fail(entity_tid);}
|
||||
|
||||
public byte[] Page_ttl_db() {
|
||||
return entity_tid == Wbase_claim_entity_type_.Tid__item
|
||||
? Bry_.Add(Wdata_wiki_mgr.Ttl_prefix_qid_bry_db, entity_id_bry)
|
||||
: Bry_.Add(Wdata_wiki_mgr.Ttl_prefix_pid_bry, entity_id_bry)
|
||||
;
|
||||
}
|
||||
public byte[] Page_ttl_db() {return To_xid__db(entity_tid, entity_id_bry);}
|
||||
public byte[] Page_ttl_gui() {
|
||||
return entity_tid == Wbase_claim_entity_type_.Tid__item
|
||||
? Bry_.Add(Wdata_wiki_mgr.Ttl_prefix_qid_bry_gui, entity_id_bry)
|
||||
@ -47,4 +42,11 @@ public class Wbase_claim_entity extends Wbase_claim_base {
|
||||
@Override public String toString() {// TEST:
|
||||
return String_.Concat_with_str("|", Wbase_claim_value_type_.Reg.Get_str_or_fail(this.Snak_tid()), Wbase_claim_type_.Reg.Get_str_or_fail(this.Val_tid()), this.Entity_tid_str(), Int_.To_str(entity_id));
|
||||
}
|
||||
|
||||
public static byte[] To_xid__db(byte tid, byte[] bry) { // EX: 'item,2' -> q2; 'property,2' -> Property:P2
|
||||
return tid == Wbase_claim_entity_type_.Tid__item
|
||||
? Bry_.Add(Wdata_wiki_mgr.Ttl_prefix_qid_bry_db, bry)
|
||||
: Bry_.Add(Wdata_wiki_mgr.Ttl_prefix_pid_bry, bry)
|
||||
;
|
||||
}
|
||||
}
|
||||
|
@ -39,16 +39,18 @@ public class Wbase_claim_quantity extends Wbase_claim_base {
|
||||
if (lbound_as_num == null) lbound_as_num = To_decimal("lower", lbound);
|
||||
return lbound_as_num;
|
||||
} private Decimal_adp lbound_as_num;
|
||||
private Decimal_adp To_decimal(String type, byte[] v) {
|
||||
if (v == null) throw Err_.new_("wbase", "value is null", "type", type);
|
||||
int len = v.length; if (len == 0) throw Err_.new_("wbase", "value is empty", "type", type);
|
||||
if (v[0] == Byte_ascii.Plus) v = Bry_.Mid(v, 1);
|
||||
return Decimal_adp_.parse(String_.new_a7(v));
|
||||
}
|
||||
|
||||
@Override public void Welcome(Wbase_claim_visitor visitor) {visitor.Visit_quantity(this);}
|
||||
@Override public String toString() {// TEST:
|
||||
return String_.Concat_with_str("|", Wbase_claim_value_type_.Reg.Get_str_or_fail(this.Snak_tid()), Wbase_claim_type_.Reg.Get_str_or_fail(this.Val_tid()), String_.new_u8(amount), String_.new_u8(unit), String_.new_u8(ubound), String_.new_u8(lbound));
|
||||
}
|
||||
|
||||
public static final byte[] Unit_1 = Bry_.new_a7("1");
|
||||
public static Decimal_adp To_decimal(String name, byte[] bry) {
|
||||
if (bry == null) throw Err_.new_wo_type("wbase.claim: value is null", "name", name);
|
||||
int len = bry.length;
|
||||
if (len == 0) throw Err_.new_wo_type("wbase.claim: value is empty", "name", name);
|
||||
if (bry[0] == Byte_ascii.Plus) bry = Bry_.Mid(bry, 1);
|
||||
return Decimal_adp_.parse(String_.new_a7(bry));
|
||||
}
|
||||
}
|
||||
|
@ -18,14 +18,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx.xowa.xtns.wbases.claims.itms; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; import gplx.xowa.xtns.wbases.*; import gplx.xowa.xtns.wbases.claims.*;
|
||||
import gplx.xowa.xtns.wbases.claims.enums.*;
|
||||
public class Wbase_claim_string extends Wbase_claim_base {
|
||||
public Wbase_claim_string(int pid, byte snak_tid, byte[] val) {super(pid, snak_tid);
|
||||
this.val = val;
|
||||
public Wbase_claim_string(int pid, byte snak_tid, byte[] val_bry) {super(pid, snak_tid);
|
||||
this.val_bry = val_bry;
|
||||
}
|
||||
@Override public byte Val_tid() {return Wbase_claim_type_.Tid__string;}
|
||||
public byte[] Val_str() {return val;} private final byte[] val;
|
||||
public byte[] Val_bry() {return val_bry;} private final byte[] val_bry;
|
||||
|
||||
@Override public void Welcome(Wbase_claim_visitor visitor) {visitor.Visit_str(this);}
|
||||
@Override public String toString() {// TEST:
|
||||
return String_.Concat_with_str("|", Wbase_claim_value_type_.Reg.Get_str_or_fail(this.Snak_tid()), Wbase_claim_type_.Reg.Get_str_or_fail(this.Val_tid()), String_.new_u8(val));
|
||||
return String_.Concat_with_str("|", Wbase_claim_value_type_.Reg.Get_str_or_fail(this.Snak_tid()), Wbase_claim_type_.Reg.Get_str_or_fail(this.Val_tid()), String_.new_u8(val_bry));
|
||||
}
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ public class Wbase_claim_time extends Wbase_claim_base {
|
||||
public void Calendar_ttl_(byte[] v) {calendar_ttl = v;}
|
||||
|
||||
public Wbase_date Time_as_date() {
|
||||
if (time_as_date == null) time_as_date = Wbase_date.Parse(time, this.Precision_int(), this.Before_int(), this.After_int(), this.Calendar_is_julian());
|
||||
if (time_as_date == null) time_as_date = Wbase_date_.Parse(time, this.Precision_int(), this.Before_int(), this.After_int(), this.Calendar_is_julian());
|
||||
return time_as_date;
|
||||
} private Wbase_date time_as_date;
|
||||
public int Precision_int() {
|
||||
@ -74,10 +74,10 @@ public class Wbase_claim_time extends Wbase_claim_base {
|
||||
boolean calendar_is_julian = this.Calendar_is_julian();
|
||||
byte[] calendar_display = null;
|
||||
if (calendar_is_julian) {
|
||||
date = Wbase_date.Xto_julian(date);
|
||||
date = Wbase_date_.To_julian(date);
|
||||
calendar_display = msgs.Time_julian();
|
||||
}
|
||||
Wbase_date.Xto_str(bfr, tmp_time_fmtr, tmp_time_bfr, msgs, date);
|
||||
Wbase_date_.To_bfr(bfr, tmp_time_fmtr, tmp_time_bfr, msgs, date);
|
||||
if (calendar_display != null)
|
||||
bfr.Add_byte_space().Add(calendar_display);
|
||||
} catch (Exception e) {
|
||||
@ -89,10 +89,10 @@ public class Wbase_claim_time extends Wbase_claim_base {
|
||||
try {
|
||||
byte[] calendar_display = null;
|
||||
if (calendar_is_julian) {
|
||||
date = Wbase_date.Xto_julian(date);
|
||||
date = Wbase_date_.To_julian(date);
|
||||
calendar_display = msgs.Time_julian();
|
||||
}
|
||||
Wbase_date.Xto_str(bfr, tmp_time_fmtr, tmp_time_bfr, msgs, date);
|
||||
Wbase_date_.To_bfr(bfr, tmp_time_fmtr, tmp_time_bfr, msgs, date);
|
||||
if (calendar_display != null)
|
||||
bfr.Add_byte_space().Add(calendar_display);
|
||||
} catch (Exception e) {
|
||||
|
@ -16,8 +16,6 @@ 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.xtns.wbases.claims.itms.times; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; import gplx.xowa.xtns.wbases.*; import gplx.xowa.xtns.wbases.claims.*; import gplx.xowa.xtns.wbases.claims.itms.*;
|
||||
import gplx.core.brys.fmtrs.*;
|
||||
import gplx.xowa.xtns.wbases.hwtrs.*;
|
||||
public class Wbase_date {
|
||||
public Wbase_date(long year, int month, int day, int hour, int minute, int second, int precision, int before, int after, boolean calendar_is_julian) {
|
||||
this.year = year; this.month = month; this.day = day; this.hour = hour; this.minute = minute; this.second = second;
|
||||
@ -33,151 +31,6 @@ public class Wbase_date {
|
||||
public int Before() {return before;} private final int before;
|
||||
public int After() {return after;} private final int after;
|
||||
public boolean Calendar_is_julian() {return calendar_is_julian;} private final boolean calendar_is_julian;
|
||||
public static Wbase_date Parse(byte[] date, int precision, int before, int after, boolean calendar_is_julian) {// EX:+00000002001-02-03T04:05:06Z
|
||||
int year_sign = 1;
|
||||
switch (date[0]) {
|
||||
case Byte_ascii.Plus: break;
|
||||
case Byte_ascii.Dash: year_sign = -1; break;
|
||||
default: throw Err_.new_unhandled(date[0]);
|
||||
}
|
||||
int year_end = Bry_find_.Find_fwd(date, Byte_ascii.Dash, 1);
|
||||
long year = Long_.parse_or(String_.new_a7(date, 1, year_end), -1); if (year == -1) throw Err_.new_wo_type("parse failed", "raw", String_.new_a7(date));
|
||||
int month = Bry_.To_int_or(date, year_end + 1, year_end + 3, -1);
|
||||
int day = Bry_.To_int_or(date, year_end + 4, year_end + 6, -1);
|
||||
int hour = Bry_.To_int_or(date, year_end + 7, year_end + 9, -1);
|
||||
int minute = Bry_.To_int_or(date, year_end + 10, year_end + 12, -1);
|
||||
int second = Bry_.To_int_or(date, year_end + 13, year_end + 15, -1);
|
||||
return new Wbase_date(year * year_sign, month, day, hour, minute, second, precision, before, after, calendar_is_julian);
|
||||
}
|
||||
public static Wbase_date Xto_julian(Wbase_date date) {
|
||||
int a = (int)Math_.Floor((14 - date.Month() / 12));
|
||||
int y = (int)date.Year() + 4800 - a;
|
||||
int m = date.Month() + 12 * a - 3;
|
||||
int julian = date.Day() + (int)Math_.Floor((153 * m + 2) / 5) + 365 * y + (int)Math_.Floor(y / 4) - (int)Math_.Floor(y / 100) + (int)Math_.Floor(y / 400) - 32045;
|
||||
int c = julian + 32082;
|
||||
int d = (int)Math_.Floor((4 * c + 3) / 1461);
|
||||
int e = c - (int)Math_.Floor((1461 * d) / 4);
|
||||
int n = (int)Math_.Floor((5 * e + 2) / 153);
|
||||
int new_y = d - 4800 + (int)Math_.Floor(n / 10);
|
||||
int new_m = n + 3 - 12 * (int)Math_.Floor(n / 10);
|
||||
int new_d = e - (int)Math_.Floor((153 * n + 2) / 5) + 1;
|
||||
return new Wbase_date(new_y, new_m, new_d, date.Hour(), date.Minute(), date.Second(), date.precision, date.before, date.after, date.calendar_is_julian);
|
||||
}
|
||||
public static void Xto_str(Bry_bfr bfr, Bry_fmtr tmp_fmtr, Bry_bfr tmp_bfr, Wdata_hwtr_msgs msgs, Wbase_date date) {
|
||||
boolean calendar_is_julian = date.calendar_is_julian;
|
||||
if (calendar_is_julian)
|
||||
date = Xto_julian(date);
|
||||
long year = date.Year();
|
||||
int months_bgn = msgs.Month_bgn_idx();
|
||||
byte[][] months = msgs.Ary();
|
||||
int precision = date.precision;
|
||||
byte[] time_spr = msgs.Sym_time_spr();
|
||||
switch (precision) {
|
||||
case Wbase_date.Fmt_ym: // EX: "Feb 2001"
|
||||
bfr.Add(months[months_bgn + date.Month() - List_adp_.Base1]);
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add_long_variable(year);
|
||||
break;
|
||||
case Wbase_date.Fmt_ymd: // EX: "3 Feb 2001"
|
||||
bfr.Add_int_variable(date.Day());
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add(months[months_bgn + date.Month() - List_adp_.Base1]);
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add_long_variable(date.Year());
|
||||
break;
|
||||
case Wbase_date.Fmt_ymdh: // EX: "4:00 3 Feb 2011"
|
||||
bfr.Add_int_variable(date.Hour());
|
||||
bfr.Add(time_spr);
|
||||
bfr.Add_int_fixed(0, 2);
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add_int_variable(date.Day());
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add(months[months_bgn + date.Month() - List_adp_.Base1]);
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add_long_variable(date.Year());
|
||||
break;
|
||||
case Wbase_date.Fmt_ymdhn: // EX: "4:05 3 Feb 2011"
|
||||
bfr.Add_int_variable(date.Hour());
|
||||
bfr.Add(time_spr);
|
||||
bfr.Add_int_fixed(date.Minute(), 2);
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add_int_variable(date.Day());
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add(months[months_bgn + date.Month() - List_adp_.Base1]);
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add_long_variable(date.Year());
|
||||
break;
|
||||
default:
|
||||
if (precision <= 9) // y, round to (9 - prec)
|
||||
Xto_str_fmt_y(bfr, tmp_fmtr, tmp_bfr, msgs, date, precision);
|
||||
else { // EX: "4:05:06 3 Feb 2011"
|
||||
bfr.Add_int_variable(date.Hour());
|
||||
bfr.Add(time_spr);
|
||||
bfr.Add_int_fixed(date.Minute(), 2);
|
||||
bfr.Add(time_spr);
|
||||
bfr.Add_int_fixed(date.Second(), 2);
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add_int_variable(date.Day());
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add(months[months_bgn + date.Month() - List_adp_.Base1]);
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add_long_variable(date.Year());
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (calendar_is_julian)
|
||||
bfr.Add(msgs.Time_julian());
|
||||
Xto_str_beforeafter(bfr, tmp_fmtr, tmp_bfr, msgs, date);
|
||||
}
|
||||
private static void Xto_str_beforeafter(Bry_bfr bfr, Bry_fmtr tmp_fmtr, Bry_bfr tmp_bfr, Wdata_hwtr_msgs msgs, Wbase_date date) {
|
||||
byte[] bry = null;
|
||||
int before = date.before;
|
||||
int after = date.after;
|
||||
if (before == 0) {
|
||||
if (after != 0)
|
||||
bry = tmp_bfr.Add(msgs.Sym_plus()).Add_int_variable(after).To_bry_and_clear();
|
||||
}
|
||||
else {
|
||||
if (after == 0)
|
||||
bry = tmp_bfr.Add(msgs.Sym_minus()).Add_int_variable(before).To_bry_and_clear();
|
||||
else if (before == after)
|
||||
bry = tmp_bfr.Add(msgs.Sym_plusminus()).Add_int_variable(before).To_bry_and_clear();
|
||||
else
|
||||
bry = tmp_bfr.Add(msgs.Sym_minus()).Add_int_variable(before).Add(msgs.Sym_list_comma()).Add(msgs.Sym_plus()).Add_int_variable(after).To_bry_and_clear();
|
||||
}
|
||||
if (bry != null) {
|
||||
bry = tmp_fmtr.Fmt_(msgs.Sym_fmt_parentheses()).Bld_bry_many(tmp_bfr, bry);
|
||||
bfr.Add_byte_space().Add(bry);
|
||||
}
|
||||
}
|
||||
private static void Xto_str_fmt_y(Bry_bfr bfr, Bry_fmtr tmp_fmtr, Bry_bfr tmp_bfr, Wdata_hwtr_msgs msgs, Wbase_date date, int precision) {
|
||||
int year_pow = 9 - precision;
|
||||
byte[] year_fmt = msgs.Ary()[msgs.Time_year_idx() + year_pow];
|
||||
long year = date.Year();
|
||||
byte[] repl_fmt = null;
|
||||
if (year <= 0) { // negative
|
||||
if (year_pow < 4) // negative years < 999 get "BC"
|
||||
repl_fmt = msgs.Time_relative_bc();
|
||||
else // negative years > 999 get "ago"
|
||||
repl_fmt = msgs.Time_relative_ago();
|
||||
}
|
||||
else {
|
||||
if (year_pow > 4) // positive years > 999 get "in time"
|
||||
repl_fmt = msgs.Time_relative_in();
|
||||
}
|
||||
if (repl_fmt != null)
|
||||
year_fmt = tmp_fmtr.Fmt_(repl_fmt).Bld_bry_many(tmp_bfr, year_fmt);
|
||||
if (year <= 0)
|
||||
year *= -1; // convert negative to positive; note that negative year will be reported with "BC" / "ago"
|
||||
switch (year_pow) {
|
||||
case 0: break; // noop
|
||||
default:
|
||||
year = (int)(year / Math_.Pow(10, year_pow));
|
||||
break;
|
||||
}
|
||||
byte[] year_bry = tmp_fmtr.Fmt_(year_fmt).Bld_bry_many(tmp_bfr, year);
|
||||
bfr.Add(year_bry);
|
||||
}
|
||||
public static final int
|
||||
Fmt_y = 9
|
||||
, Fmt_ym = 10
|
||||
|
@ -0,0 +1,166 @@
|
||||
/*
|
||||
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.xtns.wbases.claims.itms.times; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; import gplx.xowa.xtns.wbases.*; import gplx.xowa.xtns.wbases.claims.*; import gplx.xowa.xtns.wbases.claims.itms.*;
|
||||
import gplx.core.brys.fmtrs.*;
|
||||
import gplx.xowa.xtns.wbases.hwtrs.*;
|
||||
public class Wbase_date_ {
|
||||
public static Wbase_date Parse(byte[] date, int precision, int before, int after, boolean calendar_is_julian) {// EX:+00000002001-02-03T04:05:06Z
|
||||
int year_sign = 1;
|
||||
switch (date[0]) {
|
||||
case Byte_ascii.Plus: break;
|
||||
case Byte_ascii.Dash: year_sign = -1; break;
|
||||
default: throw Err_.new_unhandled(date[0]);
|
||||
}
|
||||
int year_end = Bry_find_.Find_fwd(date, Byte_ascii.Dash, 1);
|
||||
long year = Long_.parse_or(String_.new_a7(date, 1, year_end), -1); if (year == -1) throw Err_.new_wo_type("parse failed", "raw", String_.new_a7(date));
|
||||
int month = Bry_.To_int_or(date, year_end + 1, year_end + 3, -1);
|
||||
int day = Bry_.To_int_or(date, year_end + 4, year_end + 6, -1);
|
||||
int hour = Bry_.To_int_or(date, year_end + 7, year_end + 9, -1);
|
||||
int minute = Bry_.To_int_or(date, year_end + 10, year_end + 12, -1);
|
||||
int second = Bry_.To_int_or(date, year_end + 13, year_end + 15, -1);
|
||||
return new Wbase_date(year * year_sign, month, day, hour, minute, second, precision, before, after, calendar_is_julian);
|
||||
}
|
||||
public static Wbase_date To_julian(Wbase_date date) {
|
||||
int a = (int)Math_.Floor((14 - date.Month() / 12));
|
||||
int y = (int)date.Year() + 4800 - a;
|
||||
int m = date.Month() + 12 * a - 3;
|
||||
int julian = date.Day() + (int)Math_.Floor((153 * m + 2) / 5) + 365 * y + (int)Math_.Floor(y / 4) - (int)Math_.Floor(y / 100) + (int)Math_.Floor(y / 400) - 32045;
|
||||
int c = julian + 32082;
|
||||
int d = (int)Math_.Floor((4 * c + 3) / 1461);
|
||||
int e = c - (int)Math_.Floor((1461 * d) / 4);
|
||||
int n = (int)Math_.Floor((5 * e + 2) / 153);
|
||||
int new_y = d - 4800 + (int)Math_.Floor(n / 10);
|
||||
int new_m = n + 3 - 12 * (int)Math_.Floor(n / 10);
|
||||
int new_d = e - (int)Math_.Floor((153 * n + 2) / 5) + 1;
|
||||
return new Wbase_date(new_y, new_m, new_d, date.Hour(), date.Minute(), date.Second(), date.Precision(), date.Before(), date.After(), date.Calendar_is_julian());
|
||||
}
|
||||
public static void To_bfr(Bry_bfr bfr, Bry_fmtr tmp_fmtr, Bry_bfr tmp_bfr, Wdata_hwtr_msgs msgs, Wbase_date date) {
|
||||
boolean calendar_is_julian = date.Calendar_is_julian();
|
||||
if (calendar_is_julian) date = To_julian(date);
|
||||
long year = date.Year();
|
||||
int months_bgn = msgs.Month_bgn_idx();
|
||||
byte[][] months = msgs.Ary();
|
||||
int precision = date.Precision();
|
||||
byte[] time_spr = msgs.Sym_time_spr();
|
||||
switch (precision) {
|
||||
case Wbase_date.Fmt_ym: // EX: "Feb 2001"
|
||||
bfr.Add(months[months_bgn + date.Month() - List_adp_.Base1]);
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add_long_variable(year);
|
||||
break;
|
||||
case Wbase_date.Fmt_ymd: // EX: "3 Feb 2001"
|
||||
bfr.Add_int_variable(date.Day());
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add(months[months_bgn + date.Month() - List_adp_.Base1]);
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add_long_variable(date.Year());
|
||||
break;
|
||||
case Wbase_date.Fmt_ymdh: // EX: "4:00 3 Feb 2011"
|
||||
bfr.Add_int_variable(date.Hour());
|
||||
bfr.Add(time_spr);
|
||||
bfr.Add_int_fixed(0, 2);
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add_int_variable(date.Day());
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add(months[months_bgn + date.Month() - List_adp_.Base1]);
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add_long_variable(date.Year());
|
||||
break;
|
||||
case Wbase_date.Fmt_ymdhn: // EX: "4:05 3 Feb 2011"
|
||||
bfr.Add_int_variable(date.Hour());
|
||||
bfr.Add(time_spr);
|
||||
bfr.Add_int_fixed(date.Minute(), 2);
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add_int_variable(date.Day());
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add(months[months_bgn + date.Month() - List_adp_.Base1]);
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add_long_variable(date.Year());
|
||||
break;
|
||||
default:
|
||||
if (precision <= 9) // y, round to (9 - prec)
|
||||
Xto_str_fmt_y(bfr, tmp_fmtr, tmp_bfr, msgs, date, precision);
|
||||
else { // EX: "4:05:06 3 Feb 2011"
|
||||
bfr.Add_int_variable(date.Hour());
|
||||
bfr.Add(time_spr);
|
||||
bfr.Add_int_fixed(date.Minute(), 2);
|
||||
bfr.Add(time_spr);
|
||||
bfr.Add_int_fixed(date.Second(), 2);
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add_int_variable(date.Day());
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add(months[months_bgn + date.Month() - List_adp_.Base1]);
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add_long_variable(date.Year());
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (calendar_is_julian)
|
||||
bfr.Add(msgs.Time_julian());
|
||||
Xto_str_beforeafter(bfr, tmp_fmtr, tmp_bfr, msgs, date);
|
||||
}
|
||||
private static void Xto_str_beforeafter(Bry_bfr bfr, Bry_fmtr tmp_fmtr, Bry_bfr tmp_bfr, Wdata_hwtr_msgs msgs, Wbase_date date) {
|
||||
byte[] bry = null;
|
||||
int before = date.Before();
|
||||
int after = date.After();
|
||||
if (before == 0) {
|
||||
if (after != 0)
|
||||
bry = tmp_bfr.Add(msgs.Sym_plus()).Add_int_variable(after).To_bry_and_clear();
|
||||
}
|
||||
else {
|
||||
if (after == 0)
|
||||
bry = tmp_bfr.Add(msgs.Sym_minus()).Add_int_variable(before).To_bry_and_clear();
|
||||
else if (before == after)
|
||||
bry = tmp_bfr.Add(msgs.Sym_plusminus()).Add_int_variable(before).To_bry_and_clear();
|
||||
else
|
||||
bry = tmp_bfr.Add(msgs.Sym_minus()).Add_int_variable(before).Add(msgs.Sym_list_comma()).Add(msgs.Sym_plus()).Add_int_variable(after).To_bry_and_clear();
|
||||
}
|
||||
if (bry != null) {
|
||||
bry = tmp_fmtr.Fmt_(msgs.Sym_fmt_parentheses()).Bld_bry_many(tmp_bfr, bry);
|
||||
bfr.Add_byte_space().Add(bry);
|
||||
}
|
||||
}
|
||||
private static void Xto_str_fmt_y(Bry_bfr bfr, Bry_fmtr tmp_fmtr, Bry_bfr tmp_bfr, Wdata_hwtr_msgs msgs, Wbase_date date, int precision) {
|
||||
int year_pow = 9 - precision;
|
||||
byte[] year_fmt = msgs.Ary()[msgs.Time_year_idx() + year_pow];
|
||||
long year = date.Year();
|
||||
byte[] repl_fmt = null;
|
||||
if (year <= 0) { // negative
|
||||
if (year_pow < 4) // negative years < 999 get "BC"
|
||||
repl_fmt = msgs.Time_relative_bc();
|
||||
else // negative years > 999 get "ago"
|
||||
repl_fmt = msgs.Time_relative_ago();
|
||||
}
|
||||
else {
|
||||
if (year_pow > 4) // positive years > 999 get "in time"
|
||||
repl_fmt = msgs.Time_relative_in();
|
||||
}
|
||||
if (repl_fmt != null)
|
||||
year_fmt = tmp_fmtr.Fmt_(repl_fmt).Bld_bry_many(tmp_bfr, year_fmt);
|
||||
if (year <= 0)
|
||||
year *= -1; // convert negative to positive; note that negative year will be reported with "BC" / "ago"
|
||||
switch (year_pow) {
|
||||
case 0: break; // noop
|
||||
default:
|
||||
year = (int)(year / Math_.Pow(10, year_pow));
|
||||
break;
|
||||
}
|
||||
byte[] year_bry = tmp_fmtr.Fmt_(year_fmt).Bld_bry_many(tmp_bfr, year);
|
||||
bfr.Add(year_bry);
|
||||
}
|
||||
}
|
@ -68,7 +68,7 @@ class Wbase_date_fxt {
|
||||
public int Init_before() {return init_before;} public Wbase_date_fxt Init_before_(int v) {init_before = v; return this;} private int init_before;
|
||||
public int Init_after() {return init_after;} public Wbase_date_fxt Init_after_(int v) {init_after = v; return this;} private int init_after;
|
||||
public void Test_parse(String raw, long expd_y, int expd_m, int expd_d, int expd_h, int expd_n, int expd_s) {
|
||||
Wbase_date actl_date = Wbase_date.Parse(Bry_.new_a7(raw), Wbase_date.Fmt_ymdhns, init_before, init_after, init_calendar_is_julian);
|
||||
Wbase_date actl_date = Wbase_date_.Parse(Bry_.new_a7(raw), Wbase_date.Fmt_ymdhns, init_before, init_after, init_calendar_is_julian);
|
||||
Tfds.Eq(expd_y, actl_date.Year());
|
||||
Tfds.Eq(expd_m, actl_date.Month());
|
||||
Tfds.Eq(expd_d, actl_date.Day());
|
||||
@ -78,15 +78,15 @@ class Wbase_date_fxt {
|
||||
}
|
||||
public void Test_julian(int[] orig_ary, int[] expd) {
|
||||
Wbase_date orig = new Wbase_date(orig_ary[0], orig_ary[1], orig_ary[2], 0, 0, 0, 0, 0, 0, init_calendar_is_julian);
|
||||
Wbase_date actl = Wbase_date.Xto_julian(orig);
|
||||
Wbase_date actl = Wbase_date_.To_julian(orig);
|
||||
Tfds.Eq(expd[0], (int)actl.Year(), "y");
|
||||
Tfds.Eq(expd[1], actl.Month(), "m");
|
||||
Tfds.Eq(expd[2], actl.Day(), "d");
|
||||
}
|
||||
public void Test_xto_str(String raw, int precision, String expd) {
|
||||
if (msgs == null) msgs = Wdata_hwtr_msgs.new_en_();
|
||||
Wbase_date date = Wbase_date.Parse(Bry_.new_a7(raw), precision, init_before, init_after, init_calendar_is_julian);
|
||||
Wbase_date.Xto_str(tmp_bfr, tmp_time_fmtr, tmp_time_bfr, msgs, date);
|
||||
Wbase_date date = Wbase_date_.Parse(Bry_.new_a7(raw), precision, init_before, init_after, init_calendar_is_julian);
|
||||
Wbase_date_.To_bfr(tmp_bfr, tmp_time_fmtr, tmp_time_bfr, msgs, date);
|
||||
Tfds.Eq(expd, tmp_bfr.To_str_and_clear());
|
||||
}
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ class Wdata_visitor__html_wtr implements Wbase_claim_visitor {
|
||||
return this;
|
||||
}
|
||||
public void Visit_str(Wbase_claim_string itm) {
|
||||
tmp_bfr.Add(itm.Val_str());
|
||||
tmp_bfr.Add(itm.Val_bry());
|
||||
}
|
||||
public void Visit_entity(Wbase_claim_entity itm) {
|
||||
int entity_id = itm.Entity_id();
|
||||
|
@ -411,7 +411,7 @@ class Xob_wdata_db_visitor implements Wbase_claim_visitor {
|
||||
public Xob_wdata_db_visitor(Wdata_wiki_mgr wdata_mgr) {this.wdata_mgr = wdata_mgr;}
|
||||
public void Init(byte[] lang_key) {this.lang_key = lang_key;}
|
||||
public byte[] Rv() {return rv;} private byte[] rv;
|
||||
public void Visit_str(Wbase_claim_string itm) {rv = itm.Val_str();}
|
||||
public void Visit_str(Wbase_claim_string itm) {rv = itm.Val_bry();}
|
||||
public void Visit_monolingualtext(Wbase_claim_monolingualtext itm) {rv = Bry_.Add_w_dlm(Byte_ascii.Pipe, itm.Lang(), itm.Text());}
|
||||
public void Visit_quantity(Wbase_claim_quantity itm) {rv = itm.Amount();}
|
||||
public void Visit_time(Wbase_claim_time itm) {rv = itm.Time();}
|
||||
|
Loading…
Reference in New Issue
Block a user