mirror of
https://github.com/gnosygnu/xowa.git
synced 2024-10-27 20:34:16 +00:00
Xomw.Preprocessor: Add initial unit tests for DOM [#508]
This commit is contained in:
parent
3b5a1f8ffc
commit
0181a42d3d
@ -25,63 +25,48 @@ public class XophpArray implements Bry_bfr_able {
|
||||
}
|
||||
public XophpArray Add(Object val) {
|
||||
int key = nxt_idx++;
|
||||
XophpArrayItm itm = XophpArrayItm.New_int(key, val);
|
||||
Set_or_add(key, itm);
|
||||
Set(XophpArrayItm.New_int(key, val));
|
||||
return this;
|
||||
}
|
||||
public XophpArray Add(int key, Object val) {
|
||||
nxt_idx = key + 1;
|
||||
XophpArrayItm itm = XophpArrayItm.New_int(key, val);
|
||||
Set_or_add(key, itm);
|
||||
Set(XophpArrayItm.New_int(key, val));
|
||||
return this;
|
||||
}
|
||||
public XophpArray Add(double key, Object val) {
|
||||
int key_as_int = (int)key;
|
||||
nxt_idx = key_as_int + 1;
|
||||
XophpArrayItm itm = XophpArrayItm.New_int(key_as_int, val);
|
||||
Set_or_add(key_as_int, itm);
|
||||
Set(XophpArrayItm.New_int(key_as_int, val));
|
||||
return this;
|
||||
}
|
||||
public XophpArray Add(boolean key, Object val) {
|
||||
int key_as_int = key ? 1 : 0;
|
||||
nxt_idx = key_as_int + 1;
|
||||
XophpArrayItm itm = XophpArrayItm.New_int(key_as_int, val);
|
||||
Set_or_add(key_as_int, itm);
|
||||
Set(XophpArrayItm.New_int(key_as_int, val));
|
||||
return this;
|
||||
}
|
||||
public XophpArray Add(String key, Object val) {
|
||||
XophpArrayItm itm = null;
|
||||
int key_as_int = Int_.Parse_or(key, Int_.Min_value);
|
||||
if (key_as_int == Int_.Min_value) {
|
||||
itm = XophpArrayItm.New_str(key, val);
|
||||
Set_or_add(key, itm);
|
||||
Set(XophpArrayItm.New_str(key, val));
|
||||
}
|
||||
else {
|
||||
itm = XophpArrayItm.New_int(key_as_int, val);
|
||||
Set_or_add(key_as_int, itm);
|
||||
Set(XophpArrayItm.New_int(key_as_int, val));
|
||||
nxt_idx = key_as_int + 1;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
public XophpArrayItm Get_at(int i) {
|
||||
return (XophpArrayItm)hash.Get_at(i);
|
||||
public XophpArray Get_at_ary(int i) {return (XophpArray)Get_at(i);}
|
||||
public String Get_at_str(int i) {return (String)Get_at(i);}
|
||||
public Object Get_at(int i) {
|
||||
if (i < 0 || i >= hash.Len()) return null;
|
||||
XophpArrayItm itm = (XophpArrayItm)hash.Get_at(i);
|
||||
return itm == null ? null : itm.Val();
|
||||
}
|
||||
public void Del_at(int i) {
|
||||
XophpArrayItm itm = (XophpArrayItm)hash.Get_at(i);
|
||||
if (itm != null) {
|
||||
if (itm.Key_as_str() == null)
|
||||
hash.Del(itm.Key_as_int());
|
||||
else
|
||||
hash.Del(itm.Key_as_str());
|
||||
}
|
||||
}
|
||||
private void Set_or_add(Object key, XophpArrayItm val) {
|
||||
XophpArrayItm itm = (XophpArrayItm)hash.Get_by(key);
|
||||
if (itm == null) {
|
||||
hash.Add(key, val);
|
||||
}
|
||||
else {
|
||||
itm.Val_(val.Val());
|
||||
hash.Del(itm.Key());
|
||||
}
|
||||
}
|
||||
public XophpArray Add_many(Object... val) {
|
||||
@ -90,29 +75,23 @@ public class XophpArray implements Bry_bfr_able {
|
||||
}
|
||||
return this;
|
||||
}
|
||||
public Object Get(Object key) {
|
||||
public Object Get_by_obj(Object key) {return Get_by(Object_.Xto_str_strict_or_null(key));}
|
||||
public Object Get_by(int key) {return Get_by(Int_.To_str(key));}
|
||||
public Object Get_by(String key) {
|
||||
XophpArrayItm itm = (XophpArrayItm)hash.Get_by(key);
|
||||
return itm.Val();
|
||||
}
|
||||
public void Set(int key, Object val) {
|
||||
hash.Del(key);
|
||||
this.Add(key, val);
|
||||
this.Set(XophpArrayItm.New_int(key, val));
|
||||
}
|
||||
public void Unset(Object key) {
|
||||
public void Unset(int key) {Unset(Int_.To_str(key));}
|
||||
public void Unset(String key) {
|
||||
hash.Del(key);
|
||||
}
|
||||
public boolean Has(Object key) {
|
||||
public boolean Has_obj(Object key) {return Has(Object_.Xto_str_strict_or_null(key));}
|
||||
public boolean Has(String key) {
|
||||
return hash.Has(key);
|
||||
}
|
||||
public XophpArray Values() {
|
||||
XophpArray rv = new XophpArray();
|
||||
int len = hash.Len();
|
||||
for (int i = 0; i < len; i++) {
|
||||
XophpArrayItm old_itm = (XophpArrayItm)hash.Get_at(i);
|
||||
rv.Add(i, old_itm.Val());
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public XophpArrayItm[] To_ary() {
|
||||
return (XophpArrayItm[])hash.To_ary(XophpArrayItm.class);
|
||||
}
|
||||
@ -127,6 +106,16 @@ public class XophpArray implements Bry_bfr_able {
|
||||
itm.To_bfr(bfr);
|
||||
}
|
||||
}
|
||||
private void Set(XophpArrayItm itm) {
|
||||
String key = itm.Key();
|
||||
XophpArrayItm cur = (XophpArrayItm)hash.Get_by(key);
|
||||
if (cur == null) {
|
||||
hash.Add(key, itm);
|
||||
}
|
||||
else {
|
||||
cur.Val_(itm.Val());
|
||||
}
|
||||
}
|
||||
public static XophpArray New(Object... vals) {
|
||||
XophpArray rv = new XophpArray();
|
||||
for (Object val : vals)
|
||||
|
@ -16,19 +16,16 @@ Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
package gplx.xowa.mediawiki; import gplx.*; import gplx.xowa.*;
|
||||
import gplx.core.brys.*;
|
||||
public class XophpArrayItm implements Bry_bfr_able {
|
||||
public XophpArrayItm(int key_as_int, String key_as_str, Object val) {
|
||||
this.key_is_int = key_as_str == null;
|
||||
this.key_as_int = key_as_int;
|
||||
this.key_as_str = key_as_str;
|
||||
XophpArrayItm(boolean key_is_int, String key, Object val) {
|
||||
this.key_is_int = key_is_int;
|
||||
this.key = key;
|
||||
this.val = val;
|
||||
}
|
||||
public boolean Key_is_int() {return key_is_int;} private final boolean key_is_int;
|
||||
public int Key_as_int() {return key_as_int;} private final int key_as_int;
|
||||
public String Key_as_str() {return key_as_str;} private final String key_as_str;
|
||||
public String Key() {return key;} private final String key;
|
||||
public Object Val() {return val;} public void Val_(Object v) {this.val = v;} private Object val;
|
||||
|
||||
public void To_bfr(Bry_bfr bfr) {
|
||||
String key = key_as_str == null ? Int_.To_str(key_as_int) : key_as_str;
|
||||
bfr.Add_str_u8(key).Add_byte_eq();
|
||||
|
||||
if (Type_.Type_by_obj(val) == XophpArray.class) {
|
||||
@ -41,6 +38,6 @@ public class XophpArrayItm implements Bry_bfr_able {
|
||||
}
|
||||
}
|
||||
|
||||
public static XophpArrayItm New_int(int key, Object val) {return new XophpArrayItm(key, null, val);}
|
||||
public static XophpArrayItm New_str(String key, Object val) {return new XophpArrayItm(-1 , key , val);}
|
||||
public static XophpArrayItm New_int(int key, Object val) {return new XophpArrayItm(Bool_.Y, Int_.To_str(key), val);}
|
||||
public static XophpArrayItm New_str(String key, Object val) {return new XophpArrayItm(Bool_.N, key , val);}
|
||||
}
|
||||
|
@ -17,6 +17,9 @@ package gplx.xowa.mediawiki; import gplx.*; import gplx.xowa.*;
|
||||
public class XophpArrayUtl {
|
||||
public static boolean popBoolOrN(List_adp list) {return Bool_.Cast(List_adp_.Pop_or(list, false));}
|
||||
public static byte[] popBryOrNull(List_adp list) {return (byte[])List_adp_.Pop_or(list, null);}
|
||||
public static boolean isset(XophpArray ary, int idx) {
|
||||
return ary.Get_at(idx) == null;
|
||||
}
|
||||
public static String[] array_keys_str(Ordered_hash array) {
|
||||
int len = array.Len();
|
||||
String[] rv = new String[len];
|
||||
@ -68,10 +71,10 @@ public class XophpArrayUtl {
|
||||
return rv;
|
||||
}
|
||||
private static void array_add(XophpArray ary, XophpArrayItm itm) {
|
||||
if (itm.Key_as_str() == null)
|
||||
if (itm.Key_is_int())
|
||||
ary.Add(itm.Val());
|
||||
else
|
||||
ary.Add(itm.Key_as_str(), itm.Val());
|
||||
ary.Add(itm.Key(), itm.Val());
|
||||
}
|
||||
public static XophpArray array_splice(XophpArray src, int bgn) {return array_splice(src, bgn, src.Len(), null);}
|
||||
public static XophpArray array_splice(XophpArray src, int bgn, int len) {return array_splice(src, bgn, len , null);}
|
||||
|
@ -119,10 +119,6 @@ public class XophpArray_tst { // REF: http://php.net/manual/en/language.types.ar
|
||||
// add new and assert idx is 2
|
||||
ary.Add("c");
|
||||
fxt.Test__array(ary, XophpArrayItm.New_int(2, "c"));
|
||||
|
||||
ary = ary.Values();
|
||||
ary.Add("d");
|
||||
fxt.Test__array(ary, XophpArrayItm.New_int(0, "c"), XophpArrayItm.New_int(1, "d"));
|
||||
}
|
||||
}
|
||||
class XophpArray_fxt {
|
||||
|
31
400_xowa/src/gplx/xowa/mediawiki/XophpType.java
Normal file
31
400_xowa/src/gplx/xowa/mediawiki/XophpType.java
Normal file
@ -0,0 +1,31 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.mediawiki; import gplx.*; import gplx.xowa.*;
|
||||
public class XophpType {
|
||||
private final int type_id;
|
||||
public XophpType(int type_id) {
|
||||
this.type_id = type_id;
|
||||
}
|
||||
public boolean is_string() {
|
||||
return type_id == Type_ids_.Id__str;
|
||||
}
|
||||
public boolean is_array() {
|
||||
return type_id == Type_ids_.Id__array;
|
||||
}
|
||||
public static XophpType New(Object o) {
|
||||
return new XophpType(Type_ids_.To_id_by_obj(o));
|
||||
}
|
||||
}
|
32
400_xowa/src/gplx/xowa/mediawiki/XophpTypeUtl.java
Normal file
32
400_xowa/src/gplx/xowa/mediawiki/XophpTypeUtl.java
Normal file
@ -0,0 +1,32 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.mediawiki; import gplx.*; import gplx.xowa.*;
|
||||
public class XophpTypeUtl {
|
||||
// REF.PHP: https://www.php.net/manual/en/function.is-scalar.php
|
||||
public static boolean is_scalar(Object obj) {
|
||||
if (obj == null) return false;
|
||||
int type_id = Type_ids_.To_id_by_type(obj.getClass());
|
||||
switch (type_id) {
|
||||
case Type_ids_.Id__int:
|
||||
case Type_ids_.Id__float:
|
||||
case Type_ids_.Id__str:
|
||||
case Type_ids_.Id__bool:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
@ -54,6 +54,7 @@ public class XophpUtility {
|
||||
public static boolean isset(byte[] v) {return v != null;}
|
||||
public static boolean isset(int v) {return v != NULL_INT;}
|
||||
public static boolean isset(double v) {return v != NULL_DOUBLE;}
|
||||
public static boolean isset_obj(Object v){return v != null;}
|
||||
public static boolean istrue(int v) {return v != NULL_INT;}
|
||||
public static boolean isnumeric(byte[] src) {
|
||||
if (src == null) return false;
|
||||
|
@ -422,13 +422,13 @@ public class JCSingleton {
|
||||
// );
|
||||
String clz = null;
|
||||
if (configModels.Has(modelId)) {
|
||||
Object val = configModels.Get(modelId);
|
||||
Object val = configModels.Get_by(modelId);
|
||||
if (Type_.Type_by_obj(val) == XophpArray.class) {
|
||||
XophpArray val_as_ary = (XophpArray)val;
|
||||
if (val_as_ary.Has("class")) {
|
||||
Gfo_usr_dlg_.Instance.Warn_many("", "", "JsonConfig: Invalid +$wgJsonConfigModels['modelId'] array " + "value, 'cl"+ "ass' not found");
|
||||
} else {
|
||||
clz = (String)val_as_ary.Get("class");
|
||||
clz = (String)val_as_ary.Get_by("class");
|
||||
}
|
||||
} else {
|
||||
clz = (String)val;
|
||||
|
@ -179,7 +179,7 @@ public class JCValue {
|
||||
value_as_obj.Del_by(key);
|
||||
}
|
||||
else if (value_tid == Value_tid__ary && (fld_type == Type_ids_.Id__str || fld_type == Type_ids_.Id__int)) {
|
||||
tmp = value_as_ary.Get(fld);
|
||||
tmp = value_as_ary.Get_by_obj(fld);
|
||||
if (fld_type == Type_ids_.Id__str)
|
||||
value_as_ary.Unset((String)fld);
|
||||
else
|
||||
@ -203,7 +203,7 @@ public class JCValue {
|
||||
return value_as_obj.Has((String)fld);
|
||||
}
|
||||
else if (value_tid == Value_tid__ary && (fld_type == Type_ids_.Id__str || fld_type == Type_ids_.Id__int)) {
|
||||
return value_as_ary.Has(fld);
|
||||
return value_as_ary.Has_obj(fld);
|
||||
}
|
||||
throw Err_.new_wo_type("Type mismatch for field " + fld);
|
||||
}
|
||||
@ -219,7 +219,7 @@ public class JCValue {
|
||||
return value_as_obj.Get_by_as_obj((String)fld);
|
||||
}
|
||||
else if (value_tid == Value_tid__ary && (fld_type == Type_ids_.Id__str || fld_type == Type_ids_.Id__int)) {
|
||||
return value_as_ary.Get(fld);
|
||||
return value_as_ary.Get_by_obj(fld);
|
||||
}
|
||||
throw Err_.new_wo_type("Type mismatch for field " + fld);
|
||||
}
|
||||
|
@ -0,0 +1,21 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.mediawiki.includes; import gplx.*; import gplx.xowa.*; import gplx.xowa.mediawiki.*;
|
||||
public class XomwExceptionUtl {
|
||||
public static Err New_by_method(Class<?> type, String method, String msg) {
|
||||
return Err_.new_wo_type(Type_.Name(type) + "." + method + ":" + msg);
|
||||
}
|
||||
}
|
@ -101,10 +101,10 @@ public class XomwParser implements XomwParserIface {
|
||||
//
|
||||
// # Regular expression for a non-newline space
|
||||
// static final SPACE_NOT_NL = '(?:\t| |&\#0*160;|&\#[Xx]0*[Aa]0;|\p{Zs})';
|
||||
//
|
||||
// # Flags for preprocessToDom
|
||||
// static final PTD_FOR_INCLUSION = 1;
|
||||
//
|
||||
|
||||
// Flags for preprocessToDom
|
||||
public static final int PTD_FOR_INCLUSION = 1;
|
||||
|
||||
// # Allowed values for this.mOutputType
|
||||
// # Parameter to startExternalParse().
|
||||
// static final OT_HTML = 1; # like parse()
|
||||
|
@ -160,7 +160,7 @@ public abstract class XomwPreprocessor {
|
||||
private Xomw_prepro_accum accum = null;
|
||||
|
||||
public XomwPreprocessor() {
|
||||
this.stack = new XomwPPDStack(this.Factory__accum());
|
||||
this.stack = this.Factory__stack();
|
||||
}
|
||||
public void Init_by_wiki(String... xmlish_elems_ary) {
|
||||
Elements_trie__init_by_wiki(elements_trie__y, ignored_tags_y, xmlish_elems_ary, "noinclude");
|
||||
@ -188,12 +188,13 @@ public abstract class XomwPreprocessor {
|
||||
trie.Add_obj(hook, new Xomw_prepro_elem(type_is_comment ? Xomw_prepro_elem.Type__comment : Xomw_prepro_elem.Type__other, Bry_.new_a7(name)));
|
||||
}
|
||||
|
||||
public abstract byte[] preprocessToDbg(byte[] src, boolean for_inclusion);
|
||||
/**
|
||||
* @param String $text
|
||||
* @param int $flags
|
||||
* @return String
|
||||
*/
|
||||
public byte[] preprocessToXml(byte[] src, boolean for_inclusion) {
|
||||
protected Object preprocessToObj_base(byte[] src, boolean for_inclusion) {
|
||||
// RELIC.PROC_VAR: forInclusion = $flags & Parser::PTD_FOR_INCLUSION;
|
||||
// RELIC.INIT_BY_WIKI: $xmlishElements = parser->getStripList();
|
||||
// RELIC.CLASS_VAR: $xmlishAllowMissingEndTag = [ 'includeonly', 'noinclude', 'onlyinclude' ];
|
||||
@ -810,7 +811,7 @@ public abstract class XomwPreprocessor {
|
||||
}
|
||||
|
||||
// Output any remaining unclosed brackets
|
||||
return (byte[])this.preprocessToObj_term(stack);
|
||||
return this.preprocessToObj_term(stack);
|
||||
}
|
||||
|
||||
private Xomw_prepro_rule Get_rule(byte[] bry) {
|
||||
@ -862,8 +863,8 @@ public abstract class XomwPreprocessor {
|
||||
return rv;
|
||||
}
|
||||
|
||||
protected abstract Xomw_prepro_accum Factory__accum();
|
||||
protected abstract XomwPPDPart Factory__part();
|
||||
protected abstract XomwPPDStack Factory__stack();
|
||||
|
||||
protected abstract Xomw_prepro_accum Accum__set(Xomw_prepro_accum accum);
|
||||
|
||||
|
@ -20,12 +20,9 @@ import gplx.xowa.mediawiki.includes.parsers.preprocessors.*;
|
||||
class XomwPreprocessor_DOM extends XomwPreprocessor { private final Bry_bfr tmp_bfr = Bry_bfr_.New();
|
||||
private Xomw_prepro_accum__dom accum_dom = new Xomw_prepro_accum__dom("");
|
||||
|
||||
@Override protected Xomw_prepro_accum Factory__accum() {return Xomw_prepro_accum__dom.Instance;}
|
||||
@Override protected XomwPPDStack Factory__stack() {return new XomwPPDStack(Xomw_prepro_accum__dom.Instance);}
|
||||
@Override protected XomwPPDPart Factory__part() {return new XomwPPDPart_DOM("");}
|
||||
|
||||
@Override public XomwPPNode preprocessToObj(String text, int flags) {
|
||||
return null;
|
||||
}
|
||||
@Override public XomwPPFrame newFrame() {
|
||||
return null;
|
||||
}
|
||||
@ -37,6 +34,10 @@ class XomwPreprocessor_DOM extends XomwPreprocessor { private final Bry_bfr
|
||||
return accum;
|
||||
}
|
||||
|
||||
@Override public byte[] preprocessToDbg(byte[] src, boolean for_inclusion) {return (byte[])this.preprocessToObj_base(src, for_inclusion);}
|
||||
@Override public XomwPPNode preprocessToObj(String text, int flags) {
|
||||
return (XomwPPNode)preprocessToObj_base(Bry_.new_u8(text), gplx.core.bits.Bitmask_.Has_int(flags, XomwParser.PTD_FOR_INCLUSION));
|
||||
}
|
||||
@Override protected void preprocessToObj_root() {
|
||||
accum_dom.Clear();
|
||||
accum_dom.Add_str_literal("<root>");
|
||||
|
@ -15,10 +15,7 @@ Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.mediawiki.includes.parsers; import gplx.*; import gplx.xowa.*; import gplx.xowa.mediawiki.*; import gplx.xowa.mediawiki.includes.*;
|
||||
import gplx.xowa.mediawiki.includes.parsers.preprocessors.*;
|
||||
class XomwPreprocessor_Hash extends XomwPreprocessor { private XophpArray accum = new XophpArray();
|
||||
@Override public XomwPPNode preprocessToObj(String text, int flags) {
|
||||
return null;
|
||||
}
|
||||
class XomwPreprocessor_Hash extends XomwPreprocessor { private XophpArray accum;
|
||||
|
||||
@Override public XomwPPFrame newFrame() {
|
||||
return null;
|
||||
@ -28,10 +25,20 @@ class XomwPreprocessor_Hash extends XomwPreprocessor { private XophpArray accum
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override protected Xomw_prepro_accum Factory__accum() {return Xomw_prepro_accum__hash.Instance;}
|
||||
@Override protected XomwPPDStack Factory__stack() {return new XomwPPDStack_Hash(Xomw_prepro_accum__hash.Instance);}
|
||||
@Override protected XomwPPDPart Factory__part() {return new XomwPPDPart_Hash("");}
|
||||
@Override protected Xomw_prepro_accum Accum__set(Xomw_prepro_accum accum) {return accum;}
|
||||
@Override protected Xomw_prepro_accum Accum__set(Xomw_prepro_accum accum_obj) {
|
||||
this.accum = ((Xomw_prepro_accum__hash)accum_obj).Ary();
|
||||
return accum_obj;
|
||||
}
|
||||
|
||||
@Override public byte[] preprocessToDbg(byte[] src, boolean for_inclusion) {
|
||||
XomwPPNode_Hash_Tree node = (XomwPPNode_Hash_Tree)this.preprocessToObj_base(src, for_inclusion);
|
||||
return Bry_.new_u8(node.toString());
|
||||
}
|
||||
@Override public XomwPPNode preprocessToObj(String text, int flags) {
|
||||
return (XomwPPNode)preprocessToObj_base(Bry_.new_u8(text), gplx.core.bits.Bitmask_.Has_int(flags, XomwParser.PTD_FOR_INCLUSION));
|
||||
}
|
||||
@Override protected void preprocessToObj_root() {} // NOTE: deliberately empty;
|
||||
|
||||
@Override protected void preprocessToObj_ignore(byte[] src, int bgn, int end) {
|
||||
@ -47,7 +54,7 @@ class XomwPreprocessor_Hash extends XomwPreprocessor { private XophpArray accum
|
||||
int endIndex = accum.Len() - 1;
|
||||
if ( ws_len > 0
|
||||
&& endIndex >= 0) {
|
||||
Object itm_obj = accum.Get(endIndex);
|
||||
Object itm_obj = accum.Get_at(endIndex);
|
||||
if (Type_.Eq_by_obj(itm_obj, Bry_.Cls_ref_type)) {
|
||||
byte[] itm = (byte[])itm_obj;
|
||||
if (XophpString.strspn_fwd__space_or_tab(itm, 0, itm.length, itm.length) == ws_len) {
|
||||
@ -145,35 +152,42 @@ class XomwPreprocessor_Hash extends XomwPreprocessor { private XophpArray accum
|
||||
stack.Get_current_part().eqpos = accum.Len() - 1;
|
||||
}
|
||||
@Override protected Object preprocessToObj_term(XomwPPDStack stack) {
|
||||
Xomw_prepro_accum__hash stack_accum = (Xomw_prepro_accum__hash)stack.Get_accum();
|
||||
XophpArray stack_ary = stack_accum.Ary();
|
||||
int len = stack_ary.Len();
|
||||
for (int i = 0; i < len; i++) {
|
||||
// XomwPPDPart_Hash piece = (XomwPPDPart_Hash)(stack_ary.Get_at(i).Val());
|
||||
// XophpArrayUtl.array_splice(stack_ary, stack_ary.Len(), 0, piece.breakSyntax());
|
||||
}
|
||||
// for ( $stack->stack as $piece ) {
|
||||
// array_splice( $stack->rootAccum, count( $stack->rootAccum ), 0, $piece->breakSyntax() );
|
||||
// }
|
||||
//
|
||||
// # Enable top-level headings
|
||||
|
||||
// // Enable top-level headings
|
||||
// for ( $stack->rootAccum as &$node ) {
|
||||
// if ( is_array( $node ) && $node[PPNode_Hash_Tree::NAME] === 'possible-h' ) {
|
||||
// $node[PPNode_Hash_Tree::NAME] = 'h';
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// $rootStore = [ [ 'root', $stack->rootAccum ] ];
|
||||
// $rootNode = new PPNode_Hash_Tree( $rootStore, 0 );
|
||||
//
|
||||
|
||||
|
||||
XophpArray rootStore = XophpArray.New(XophpArray.New("root", stack.Get_root_accum()));
|
||||
XomwPPNode_Hash_Tree rootNode = new XomwPPNode_Hash_Tree(rootStore, 0);
|
||||
|
||||
// // Cache
|
||||
// $tree = json_encode( $rootStore, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
|
||||
// if ( $tree !== false ) {
|
||||
// $this->cacheSetTree( $text, $flags, $tree );
|
||||
// }
|
||||
//
|
||||
// return $rootNode;
|
||||
return null;
|
||||
return rootNode;
|
||||
}
|
||||
|
||||
private static void addLiteral(XophpArray accum, byte[] text) {addLiteral(accum, String_.new_u8(text));}
|
||||
private static void addLiteral(XophpArray accum, String text) {
|
||||
int n = accum.Len();
|
||||
if (n > 0) {
|
||||
Object itm = accum.Get(n - 1);
|
||||
Object itm = accum.Get_at(n - 1);
|
||||
if (itm != null) {
|
||||
accum.Set(n - 1, ((String)itm) + text);
|
||||
return;
|
||||
|
@ -15,9 +15,11 @@ Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.mediawiki.includes.parsers; import gplx.*; import gplx.xowa.*; import gplx.xowa.mediawiki.*; import gplx.xowa.mediawiki.includes.*;
|
||||
import org.junit.*;
|
||||
public class XomwPreprocessor_DOM__tst {
|
||||
private final XomwPreprocessor_DOM__fxt fxt = new XomwPreprocessor_DOM__fxt();
|
||||
public class XomwPreprocessor__tst {
|
||||
private final XomwPreprocessor__fxt fxt = new XomwPreprocessor__fxt();
|
||||
@Before public void init() {fxt.Clear();}
|
||||
@Test public void Text() {
|
||||
fxt.Init__hash_y();
|
||||
fxt.Test__parse("abc", "<root>abc</root>");
|
||||
}
|
||||
@Test public void Brack() {
|
||||
@ -218,16 +220,37 @@ public class XomwPreprocessor_DOM__tst {
|
||||
fxt.Test__parse("a<noinclude>b</noinclude>c", "<root>a<ignore><noinclude></ignore>b<ignore></noinclude></ignore>c</root>");
|
||||
}
|
||||
}
|
||||
class XomwPreprocessor_DOM__fxt {
|
||||
private final XomwPreprocessor_DOM wkr = new XomwPreprocessor_DOM();
|
||||
class XomwPreprocessor__fxt {
|
||||
private boolean dom_enabled = Bool_.Y, hash_enabled = Bool_.N;
|
||||
private boolean for_inclusion = false;
|
||||
public XomwPreprocessor_DOM__fxt() {
|
||||
wkr.Init_by_wiki("pre");
|
||||
public XomwPreprocessor__fxt() {
|
||||
}
|
||||
public void Clear() {
|
||||
dom_enabled = true;
|
||||
hash_enabled = false;
|
||||
for_inclusion = false;
|
||||
}
|
||||
public void Init__for_inclusion_(boolean v) {for_inclusion = v;}
|
||||
public XomwPreprocessor__fxt Init__hash_y() {hash_enabled = Bool_.Y; return this;}
|
||||
public XomwPreprocessor__fxt Init__dom_n () { dom_enabled = Bool_.N; return this;}
|
||||
public void Test__parse(String src_str, String expd) {
|
||||
byte[] src_bry = Bry_.new_u8(src_str);
|
||||
byte[] actl = wkr.preprocessToXml(src_bry, for_inclusion);
|
||||
Tfds.Eq_str_lines(expd, String_.new_u8(actl), src_str);
|
||||
List_adp list = List_adp_.New();
|
||||
if (hash_enabled) {
|
||||
XomwPreprocessor_Hash wkr_hash = new XomwPreprocessor_Hash();
|
||||
wkr_hash.Init_by_wiki("pre");
|
||||
list.Add(wkr_hash);
|
||||
}
|
||||
if (dom_enabled) {
|
||||
XomwPreprocessor_DOM wkr_dom = new XomwPreprocessor_DOM();
|
||||
wkr_dom.Init_by_wiki("pre");
|
||||
list.Add(wkr_dom);
|
||||
}
|
||||
|
||||
for (int i = 0; i < list.Len(); i++) {
|
||||
XomwPreprocessor wkr = (XomwPreprocessor)list.Get_at(i);
|
||||
byte[] src_bry = Bry_.new_u8(src_str);
|
||||
byte[] actl = wkr.preprocessToDbg(src_bry, for_inclusion);
|
||||
Tfds.Eq_str_lines(expd, String_.new_u8(actl), src_str);
|
||||
}
|
||||
}
|
||||
}
|
@ -23,7 +23,8 @@ public class XomwPPDStack {
|
||||
public final List_adp stack = List_adp_.New();
|
||||
public Xomw_prepro_piece top;
|
||||
private final Xomw_prepro_flags flags = new Xomw_prepro_flags();
|
||||
private Xomw_prepro_accum root_accum, accum;
|
||||
protected Xomw_prepro_accum root_accum;
|
||||
protected Xomw_prepro_accum accum;
|
||||
|
||||
public XomwPPDStack(Xomw_prepro_accum prototype) {
|
||||
root_accum = prototype.Make_new();
|
||||
|
@ -0,0 +1,21 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.mediawiki.includes.parsers.preprocessors; import gplx.*; import gplx.xowa.*; import gplx.xowa.mediawiki.*; import gplx.xowa.mediawiki.includes.*; import gplx.xowa.mediawiki.includes.parsers.*;
|
||||
public class XomwPPDStack_Hash extends XomwPPDStack { public XomwPPDStack_Hash(Xomw_prepro_accum prototype) {super(prototype);
|
||||
this.root_accum = prototype.Make_new();
|
||||
this.accum = root_accum;
|
||||
}
|
||||
}
|
@ -34,7 +34,7 @@ public abstract class XomwPPNode {
|
||||
* Returns false if this is not a tree node.
|
||||
* @return PPNode
|
||||
*/
|
||||
public abstract XomwPPNode[] getChildren();
|
||||
// public abstract XomwPPNode[] getChildren();
|
||||
|
||||
/**
|
||||
* Get the first child of a tree node. False if there isn't one.
|
||||
@ -55,19 +55,19 @@ public abstract class XomwPPNode {
|
||||
* @param String $type
|
||||
* @return boolean|PPNode
|
||||
*/
|
||||
public abstract XomwPPNode getChildrenOfType(String type);
|
||||
// public abstract XomwPPNode getChildrenOfType(String type);
|
||||
|
||||
/**
|
||||
* Returns the length of the array, or false if this is not an array-type node
|
||||
*/
|
||||
public abstract int getLength();
|
||||
// public abstract int getLength();
|
||||
|
||||
/**
|
||||
* Returns an item of an array-type node
|
||||
* @param int $i
|
||||
* @return boolean|PPNode
|
||||
*/
|
||||
public abstract XomwPPNode item(int i);
|
||||
// public abstract XomwPPNode item(int i);
|
||||
|
||||
/**
|
||||
* Get the name of this node. The following names are defined here:
|
||||
@ -91,18 +91,20 @@ public abstract class XomwPPNode {
|
||||
* value PPNode value
|
||||
* @return array
|
||||
*/
|
||||
public abstract Hash_adp splitArg();
|
||||
// public abstract Hash_adp splitArg();
|
||||
|
||||
/**
|
||||
* Split an "<ext>" node into an associative array containing name, attr, inner and close
|
||||
* All values in the resulting array are PPNodes. Inner and close are optional.
|
||||
* @return array
|
||||
*/
|
||||
public abstract Hash_adp splitExt();
|
||||
// public abstract Hash_adp splitExt();
|
||||
|
||||
/**
|
||||
* Split an "<h>" node
|
||||
* @return array
|
||||
*/
|
||||
public abstract Hash_adp splitHeading();
|
||||
// public abstract Hash_adp splitHeading();
|
||||
|
||||
public abstract String toString();
|
||||
}
|
||||
|
@ -0,0 +1,71 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.mediawiki.includes.parsers.preprocessors; import gplx.*; import gplx.xowa.*; import gplx.xowa.mediawiki.*; import gplx.xowa.mediawiki.includes.*; import gplx.xowa.mediawiki.includes.parsers.*;
|
||||
// MW.FILE:Preprocessor_Hash
|
||||
/**
|
||||
* @ingroup Parser
|
||||
*/
|
||||
public class XomwPPNode_Hash_Array extends XomwPPNode { // public $value;
|
||||
//
|
||||
// public function __construct( $value ) {
|
||||
// $this->value = $value;
|
||||
// }
|
||||
|
||||
@Override public String toString() {
|
||||
// return var_export( $this, true );
|
||||
return null;
|
||||
}
|
||||
//
|
||||
// public function getLength() {
|
||||
// return count( $this->value );
|
||||
// }
|
||||
//
|
||||
// public function item( $i ) {
|
||||
// return $this->value[$i];
|
||||
// }
|
||||
|
||||
@Override public String getName() {
|
||||
return "#nodelist";
|
||||
}
|
||||
|
||||
@Override public XomwPPNode getNextSibling() {
|
||||
return null;
|
||||
}
|
||||
|
||||
// public function getChildren() {
|
||||
// return false;
|
||||
// }
|
||||
|
||||
@Override public XomwPPNode getFirstChild() {
|
||||
return null;
|
||||
}
|
||||
|
||||
// public function getChildrenOfType( $name ) {
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// public function splitArg() {
|
||||
// throw new MWException( __METHOD__ . ': not supported' );
|
||||
// }
|
||||
//
|
||||
// public function splitExt() {
|
||||
// throw new MWException( __METHOD__ . ': not supported' );
|
||||
// }
|
||||
//
|
||||
// public function splitHeading() {
|
||||
// throw new MWException( __METHOD__ . ': not supported' );
|
||||
// }
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.mediawiki.includes.parsers.preprocessors; import gplx.*; import gplx.xowa.*; import gplx.xowa.mediawiki.*; import gplx.xowa.mediawiki.includes.*; import gplx.xowa.mediawiki.includes.parsers.*;
|
||||
// MW.FILE:Preprocessor_Hash
|
||||
/**
|
||||
* @ingroup Parser
|
||||
*/
|
||||
public class XomwPPNode_Hash_Attr extends XomwPPNode { public String name, value;
|
||||
private final XophpArray store;
|
||||
private final int index;
|
||||
|
||||
/**
|
||||
* Construct an Object using the data from $store[$index]. The rest of the
|
||||
* store array can be accessed via getNextSibling().
|
||||
*
|
||||
* @param array $store
|
||||
* @param integer $index
|
||||
*/
|
||||
public XomwPPNode_Hash_Attr(XophpArray store, int index) {
|
||||
// XophpArray descriptor = (XophpArray)store.Get_at_val(index);
|
||||
// if (!String_.Eq(XophpArrayUtl.Get_nest_val(XomwPPNode_Hash_Tree.NAME, 0), '@')) {
|
||||
// throw Err_.new_wo_type("XomwPPNode_Hash_Attr.CTOR: invalid name in attribute descriptor");
|
||||
// }
|
||||
// this.name = substr($descriptor[PPNode_Hash_Tree::NAME], 1);
|
||||
// this.value = $descriptor[PPNode_Hash_Tree::CHILDREN][0];
|
||||
this.store = store;
|
||||
this.index = index;
|
||||
}
|
||||
|
||||
@Override public String toString() {
|
||||
return String_.Format("<@{0}>{1}</@{0}>", this.name, Bry_.Escape_html(Bry_.new_u8(this.value)));
|
||||
}
|
||||
|
||||
@Override public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
@Override public XomwPPNode getNextSibling() {
|
||||
return XomwPPNode_Hash_Tree.factory(this.store, this.index + 1);
|
||||
}
|
||||
|
||||
// public function getChildren() {
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
@Override public XomwPPNode getFirstChild() {
|
||||
return null;
|
||||
}
|
||||
|
||||
// public function getChildrenOfType($name) {
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// public function getLength() {
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// public function item($i) {
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// public function splitArg() {
|
||||
// throw new MWException(__METHOD__ . ': not supported');
|
||||
// }
|
||||
//
|
||||
// public function splitExt() {
|
||||
// throw new MWException(__METHOD__ . ': not supported');
|
||||
// }
|
||||
//
|
||||
// public function splitHeading() {
|
||||
// throw new MWException(__METHOD__ . ': not supported');
|
||||
// }
|
||||
}
|
@ -0,0 +1,85 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.mediawiki.includes.parsers.preprocessors; import gplx.*; import gplx.xowa.*; import gplx.xowa.mediawiki.*; import gplx.xowa.mediawiki.includes.*; import gplx.xowa.mediawiki.includes.parsers.*;
|
||||
// MW.FILE:Preprocessor_Hash
|
||||
/**
|
||||
* @ingroup Parser
|
||||
*/
|
||||
public class XomwPPNode_Hash_Text extends XomwPPNode { public String value;
|
||||
private final XophpArray store;
|
||||
private final int index;
|
||||
|
||||
/**
|
||||
* Construct an Object using the data from $store[$index]. The rest of the
|
||||
* store array can be accessed via getNextSibling().
|
||||
*
|
||||
* @param array $store
|
||||
* @param integer $index
|
||||
*/
|
||||
public XomwPPNode_Hash_Text(XophpArray store, int index) {
|
||||
Object value_obj = store.Get_at(index);
|
||||
if (!XophpTypeUtl.is_scalar(value_obj)) {
|
||||
throw XomwExceptionUtl.New_by_method(XomwPPNode_Hash_Text.class, "CTOR", "given Object instead of String");
|
||||
}
|
||||
this.value = Object_.Xto_str_strict_or_null(value_obj);
|
||||
this.store = store;
|
||||
this.index = index;
|
||||
}
|
||||
|
||||
@Override public String toString() {
|
||||
return String_.new_u8(Bry_.Escape_html(Bry_.new_u8(this.value)));
|
||||
}
|
||||
|
||||
@Override public XomwPPNode getNextSibling() {
|
||||
return XomwPPNode_Hash_Tree.factory(this.store, this.index + 1);
|
||||
}
|
||||
|
||||
// public function getChildren() {
|
||||
// return false;
|
||||
// }
|
||||
|
||||
@Override public XomwPPNode getFirstChild() {
|
||||
return null;
|
||||
}
|
||||
|
||||
// public function getChildrenOfType($name) {
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// public function getLength() {
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// public function item($i) {
|
||||
// return false;
|
||||
// }
|
||||
|
||||
@Override public String getName() {
|
||||
return "#text";
|
||||
}
|
||||
|
||||
// public function splitArg() {
|
||||
// throw new MWException(__METHOD__ . ': not supported');
|
||||
// }
|
||||
//
|
||||
// public function splitExt() {
|
||||
// throw new MWException(__METHOD__ . ': not supported');
|
||||
// }
|
||||
//
|
||||
// public function splitHeading() {
|
||||
// throw new MWException(__METHOD__ . ': not supported');
|
||||
// }
|
||||
}
|
@ -0,0 +1,361 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.mediawiki.includes.parsers.preprocessors; import gplx.*; import gplx.xowa.*; import gplx.xowa.mediawiki.*; import gplx.xowa.mediawiki.includes.*; import gplx.xowa.mediawiki.includes.parsers.*;
|
||||
// MW.FILE:Preprocessor_Hash
|
||||
/**
|
||||
* @ingroup Parser
|
||||
*/
|
||||
public class XomwPPNode_Hash_Tree extends XomwPPNode { public final String name;
|
||||
/**
|
||||
* The store array for children of this node. It is "raw" in the sense that
|
||||
* nodes are two-element arrays ("descriptors") rather than PPNode_Hash_*
|
||||
* objects.
|
||||
*/
|
||||
private final XophpArray rawChildren;
|
||||
|
||||
/**
|
||||
* The store array for the siblings of this node, including this node itself.
|
||||
*/
|
||||
private final XophpArray store;
|
||||
|
||||
/**
|
||||
* The index into this.store which contains the descriptor of this node.
|
||||
*/
|
||||
private final int index;
|
||||
|
||||
/**
|
||||
* The offset of the name within descriptors, used in some places for
|
||||
* readability.
|
||||
*/
|
||||
private static final int NAME = 0;
|
||||
|
||||
/**
|
||||
* The offset of the child list within descriptors, used in some places for
|
||||
* readability.
|
||||
*/
|
||||
// private static final int CHILDREN = 1;
|
||||
|
||||
/**
|
||||
* Construct an Object using the data from $store[$index]. The rest of the
|
||||
* store array can be accessed via getNextSibling().
|
||||
*
|
||||
* @param array $store
|
||||
* @param integer $index
|
||||
*/
|
||||
public XomwPPNode_Hash_Tree(XophpArray store, int index) {
|
||||
this.store = store;
|
||||
this.index = index;
|
||||
|
||||
XophpArray list = this.store.Get_at_ary(index);
|
||||
this.name = list.Get_at_str(0);
|
||||
this.rawChildren = ((Xomw_prepro_accum__hash)(list.Get_at(1))).Ary();
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct an appropriate PPNode_Hash_* Object with a class that depends
|
||||
* on what is at the relevant store index.
|
||||
*
|
||||
* @param array $store
|
||||
* @param integer $index
|
||||
* @return PPNode_Hash_Tree|PPNode_Hash_Attr|PPNode_Hash_Text
|
||||
*/
|
||||
public static XomwPPNode factory(XophpArray store, int index) {
|
||||
Object descriptor = store.Get_at(index);
|
||||
if (!XophpUtility.isset_obj(descriptor)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
XophpType type = XophpType.New(descriptor);
|
||||
if (type.is_string()) {
|
||||
return new XomwPPNode_Hash_Text(store, index);
|
||||
}
|
||||
else if (type.is_array()) {
|
||||
XophpArray descriptor_array = (XophpArray)descriptor;
|
||||
XophpArray name_array = (XophpArray)(descriptor_array.Get_by(NAME));
|
||||
if (String_.Has_at_bgn(name_array.Get_at_str(0), "@")) {
|
||||
return new XomwPPNode_Hash_Attr(store, index);
|
||||
}
|
||||
else {
|
||||
return new XomwPPNode_Hash_Text(store, index);
|
||||
}
|
||||
}
|
||||
else {
|
||||
throw XomwExceptionUtl.New_by_method(XomwPPNode_Hash_Tree.class, "factory", "invalid node descriptor");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a node to XML, for debugging
|
||||
*/
|
||||
@Override public String toString() {
|
||||
String inner = "";
|
||||
String attribs = "";
|
||||
for (XomwPPNode node = this.getFirstChild(); node != null; node = node.getNextSibling()) {
|
||||
if (Type_.Eq_by_obj(node, XomwPPNode_Hash_Attr.class)) {
|
||||
XomwPPNode_Hash_Attr node_attr = (XomwPPNode_Hash_Attr)node;
|
||||
attribs += " " + node_attr.name + "=\"" + Bry_.Escape_html(Bry_.new_u8(node_attr.value)) + "\"";
|
||||
} else {
|
||||
inner += node.toString();
|
||||
}
|
||||
}
|
||||
if (String_.Eq(inner, "")) {
|
||||
return "<" + this.name + attribs + "/>";
|
||||
} else {
|
||||
return "<" + this.name + attribs + ">" + inner + "</" + this.name + ">";
|
||||
}
|
||||
}
|
||||
|
||||
// /**
|
||||
// * @return PPNode_Hash_Array
|
||||
// */
|
||||
// public function getChildren() {
|
||||
// $children = [];
|
||||
// foreach (this.rawChildren as $i => $child) {
|
||||
// $children[] = self::factory(this.rawChildren, $i);
|
||||
// }
|
||||
// return new PPNode_Hash_Array($children);
|
||||
// }
|
||||
|
||||
/**
|
||||
* Get the first child, or false if there is none. Note that this will
|
||||
* return a temporary proxy Object: different instances will be returned
|
||||
* if this is called more than once on the same node.
|
||||
*
|
||||
* @return PPNode_Hash_Tree|PPNode_Hash_Attr|PPNode_Hash_Text|boolean
|
||||
*/
|
||||
@Override public XomwPPNode getFirstChild() {
|
||||
if (XophpArrayUtl.isset(this.rawChildren, 0)) {
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
return factory(this.rawChildren, 0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the next sibling, or false if there is none. Note that this will
|
||||
* return a temporary proxy Object: different instances will be returned
|
||||
* if this is called more than once on the same node.
|
||||
*
|
||||
* @return PPNode_Hash_Tree|PPNode_Hash_Attr|PPNode_Hash_Text|boolean
|
||||
*/
|
||||
@Override public XomwPPNode getNextSibling() {
|
||||
return factory(this.store, this.index + 1);
|
||||
}
|
||||
|
||||
// /**
|
||||
// * Get an array of the children with a given node name
|
||||
// *
|
||||
// * @param String $name
|
||||
// * @return PPNode_Hash_Array
|
||||
// */
|
||||
// public function getChildrenOfType($name) {
|
||||
// $children = [];
|
||||
// foreach (this.rawChildren as $i => $child) {
|
||||
// if (is_array($child) && $child[self::NAME] === $name) {
|
||||
// $children[] = self::factory(this.rawChildren, $i);
|
||||
// }
|
||||
// }
|
||||
// return new PPNode_Hash_Array($children);
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * Get the raw child array. For @gplx.Internal protected use.
|
||||
// * @return array
|
||||
// */
|
||||
// public function getRawChildren() {
|
||||
// return this.rawChildren;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * @return boolean
|
||||
// */
|
||||
// public function getLength() {
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * @param int $i
|
||||
// * @return boolean
|
||||
// */
|
||||
// public function item($i) {
|
||||
// return false;
|
||||
// }
|
||||
|
||||
/**
|
||||
* @return String
|
||||
*/
|
||||
@Override public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
// /**
|
||||
// * Split a "<part>" node into an associative array containing:
|
||||
// * - name PPNode name
|
||||
// * - index String index
|
||||
// * - value PPNode value
|
||||
// *
|
||||
// * @throws MWException
|
||||
// * @return array
|
||||
// */
|
||||
// public function splitArg() {
|
||||
// return self::splitRawArg(this.rawChildren);
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * Like splitArg() but for a raw child array. For @gplx.Internal protected use only.
|
||||
// */
|
||||
// public static function splitRawArg(array $children) {
|
||||
// $bits = [];
|
||||
// foreach ($children as $i => $child) {
|
||||
// if (!is_array($child)) {
|
||||
// continue;
|
||||
// }
|
||||
// if ($child[self::NAME] === 'name') {
|
||||
// $bits['name'] = new self($children, $i);
|
||||
// if (isset($child[self::CHILDREN][0][self::NAME])
|
||||
// && $child[self::CHILDREN][0][self::NAME] === '@index'
|
||||
// ) {
|
||||
// $bits['index'] = $child[self::CHILDREN][0][self::CHILDREN][0];
|
||||
// }
|
||||
// } elseif ($child[self::NAME] === 'value') {
|
||||
// $bits['value'] = new self($children, $i);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// if (!isset($bits['name'])) {
|
||||
// throw new MWException('Invalid brace node passed to ' . __METHOD__);
|
||||
// }
|
||||
// if (!isset($bits['index'])) {
|
||||
// $bits['index'] = "";
|
||||
// }
|
||||
// return $bits;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * Split an "<ext>" node into an associative array containing name, attr, inner and close
|
||||
// * All values in the resulting array are PPNodes. Inner and close are optional.
|
||||
// *
|
||||
// * @throws MWException
|
||||
// * @return array
|
||||
// */
|
||||
// public function splitExt() {
|
||||
// return self::splitRawExt(this.rawChildren);
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * Like splitExt() but for a raw child array. For @gplx.Internal protected use only.
|
||||
// */
|
||||
// public static function splitRawExt(array $children) {
|
||||
// $bits = [];
|
||||
// foreach ($children as $i => $child) {
|
||||
// if (!is_array($child)) {
|
||||
// continue;
|
||||
// }
|
||||
// switch ($child[self::NAME]) {
|
||||
// case 'name':
|
||||
// $bits['name'] = new self($children, $i);
|
||||
// break;
|
||||
// case 'attr':
|
||||
// $bits['attr'] = new self($children, $i);
|
||||
// break;
|
||||
// case 'inner':
|
||||
// $bits['inner'] = new self($children, $i);
|
||||
// break;
|
||||
// case 'close':
|
||||
// $bits['close'] = new self($children, $i);
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// if (!isset($bits['name'])) {
|
||||
// throw new MWException('Invalid ext node passed to ' . __METHOD__);
|
||||
// }
|
||||
// return $bits;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * Split an "<h>" node
|
||||
// *
|
||||
// * @throws MWException
|
||||
// * @return array
|
||||
// */
|
||||
// public function splitHeading() {
|
||||
// if (this.name !== 'h') {
|
||||
// throw new MWException('Invalid h node passed to ' . __METHOD__);
|
||||
// }
|
||||
// return self::splitRawHeading(this.rawChildren);
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * Like splitHeading() but for a raw child array. For @gplx.Internal protected use only.
|
||||
// */
|
||||
// public static function splitRawHeading(array $children) {
|
||||
// $bits = [];
|
||||
// foreach ($children as $i => $child) {
|
||||
// if (!is_array($child)) {
|
||||
// continue;
|
||||
// }
|
||||
// if ($child[self::NAME] === '@i') {
|
||||
// $bits['i'] = $child[self::CHILDREN][0];
|
||||
// } elseif ($child[self::NAME] === '@level') {
|
||||
// $bits['level'] = $child[self::CHILDREN][0];
|
||||
// }
|
||||
// }
|
||||
// if (!isset($bits['i'])) {
|
||||
// throw new MWException('Invalid h node passed to ' . __METHOD__);
|
||||
// }
|
||||
// return $bits;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * Split a "<template>" or "<tplarg>" node
|
||||
// *
|
||||
// * @throws MWException
|
||||
// * @return array
|
||||
// */
|
||||
// public function splitTemplate() {
|
||||
// return self::splitRawTemplate(this.rawChildren);
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * Like splitTemplate() but for a raw child array. For @gplx.Internal protected use only.
|
||||
// */
|
||||
// public static function splitRawTemplate(array $children) {
|
||||
// $parts = [];
|
||||
// $bits = [ 'lineStart' => '' ];
|
||||
// foreach ($children as $i => $child) {
|
||||
// if (!is_array($child)) {
|
||||
// continue;
|
||||
// }
|
||||
// switch ($child[self::NAME]) {
|
||||
// case 'title':
|
||||
// $bits['title'] = new self($children, $i);
|
||||
// break;
|
||||
// case 'part':
|
||||
// $parts[] = new self($children, $i);
|
||||
// break;
|
||||
// case '@lineStart':
|
||||
// $bits['lineStart'] = '1';
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// if (!isset($bits['title'])) {
|
||||
// throw new MWException('Invalid node passed to ' . __METHOD__);
|
||||
// }
|
||||
// $bits['parts'] = new PPNode_Hash_Array($parts);
|
||||
// return $bits;
|
||||
// }
|
||||
}
|
Loading…
Reference in New Issue
Block a user