1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2026-03-02 03:49:30 +00:00

Embeddable: Create core dbs in proper subdirectory

This commit is contained in:
gnosygnu
2017-10-23 20:50:50 -04:00
parent 1336d44f34
commit 66877212bf
4537 changed files with 311750 additions and 0 deletions

View File

@@ -0,0 +1,74 @@
/*
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.gfml; import gplx.*;
public class GfmlAtr implements GfmlItm {
public int ObjType() {return GfmlObj_.Type_atr;}
public GfmlTkn KeyTkn() {return keyTkn;} GfmlTkn keyTkn; public String Key() {return keyTkn.Val();}
public GfmlTkn DatTkn() {return datTkn;} GfmlTkn datTkn;
public GfmlType Type() {return type;} GfmlType type;
public boolean KeyedSubObj() {return true;}
public int SubObjs_Count() {return subObjs.Count();}
public GfmlObj SubObjs_GetAt(int i) {return (GfmlObj)subObjs.Get_at(i);} GfmlObjList subObjs = GfmlObjList.new_(); // PERF?: make capacity 3 instead of 8
public void SubObjs_Add(GfmlObj o) {subObjs.Add(o);}
public String To_str() {return String_.Concat(this.Key(), "=", this.DatTkn().Val());}
@gplx.Internal protected void Key_set(String v) {keyTkn = GfmlTkn_.val_(v);} // used for 1 test
public static GfmlAtr as_(Object obj) {return obj instanceof GfmlAtr ? (GfmlAtr)obj : null;}
public static GfmlAtr string_(GfmlTkn keyTkn, GfmlTkn datTkn) {return new_(keyTkn, datTkn, GfmlType_.String);}
public static GfmlAtr new_(GfmlTkn keyTkn, GfmlTkn datTkn, GfmlType type) {
GfmlAtr rv = new GfmlAtr();
rv.keyTkn = keyTkn; rv.datTkn = datTkn; rv.type = type;
return rv;
} GfmlAtr() {}
public void UpdateAtr(String key, String dat) {
keyTkn = MakeTknAndReplace(keyTkn, key);
datTkn = MakeTknAndReplace(datTkn, dat);
}
GfmlTkn MakeTknAndReplace(GfmlTkn oldTkn, String s) {
int idx = GetTknIdx(oldTkn);
GfmlTkn tkn = MakeTkn(oldTkn, s);
if (idx != -1)
subObjs.Del_at(idx);
if (idx == -1) idx = 0;
subObjs.Add_at(tkn, idx);
return tkn;
}
int GetTknIdx(GfmlTkn t) {
for (int i = 0; i < subObjs.Count(); i++) {
GfmlObj obj = (GfmlObj)subObjs.Get_at(i);
if (obj == t) return i;
}
return -1;
}
GfmlTkn MakeTkn(GfmlTkn oldTkn, String newStr) {
if (newStr == null) return oldTkn;
if (oldTkn.SubTkns().length > 0) {
GfmlTkn bgn = oldTkn.SubTkns()[0];
GfmlTkn end = oldTkn.SubTkns()[oldTkn.SubTkns().length - 1];
newStr = String_.Replace(newStr, bgn.Raw(), bgn.Raw() + bgn.Raw());
if (bgn.Raw() != end.Raw())
newStr = String_.Replace(newStr, end.Raw(), end.Raw() + end.Raw());
return GfmlTkn_.composite_(oldTkn.TknType(), GfmlTknAry_.ary_(bgn, GfmlTkn_.raw_(newStr), end));
}
else {
boolean hasQuote = String_.Has(newStr, "'");
if (hasQuote)
return GfmlTkn_.composite_("composite", GfmlTknAry_.ary_(GfmlTkn_.new_("'", ""), GfmlTkn_.raw_(String_.Replace(newStr, "'", "''")), GfmlTkn_.new_("'", "")));
else
return GfmlTkn_.raw_(newStr);
}
}
}

View File

@@ -0,0 +1,43 @@
/*
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.gfml; import gplx.*;
public class GfmlDoc {
public GfmlNde RootNde() {return rootNde;} GfmlNde rootNde;
@gplx.Internal protected List_adp UsrMsgs() {return usrMsgs;} List_adp usrMsgs = List_adp_.New();
@gplx.Internal protected GfmlLxrRegy LxrRegy() {return lxrRegy;} GfmlLxrRegy lxrRegy = new GfmlLxrRegy();
@gplx.Internal protected GfmlBldrCmdRegy CmdRegy() {return cmdRegy;} GfmlBldrCmdRegy cmdRegy = GfmlBldrCmdRegy.new_();
@gplx.Internal protected GfmlPragmaMgr PragmaMgr() {return pragmaMgr;} GfmlPragmaMgr pragmaMgr = GfmlPragmaMgr.new_();
@gplx.Internal protected GfmlLxr RootLxr() {return rootLxr;} GfmlLxr rootLxr;
@gplx.Internal protected void RootLxr_set(GfmlLxr v) {rootLxr = v;}
@gplx.Internal protected void Clear() {
usrMsgs.Clear();
rootNde = GfmlNde.named_(GfmlTkn_.cmd_("tkn.gfml.root_tkn", GfmlBldrCmd_pendingTkns_add.Instance), GfmlType_.Null);
rootNde.DocPos_(GfmlDocPos_.Root);
}
@gplx.Internal protected static GfmlDoc new_() {
GfmlDoc rv = new GfmlDoc();
rv.Clear();
return rv;
} GfmlDoc() {}
}
// class GfmlDocEditor {
// public GfmlTkn BgnParen() {return bgnParen;} public GfmlDocEditor BgnParen_(GfmlTkn v) {bgnParen = v; return this;} GfmlTkn bgnParen = GfmlTkn_.new_("(", "");
// public GfmlTkn EndParen() {return endParen;} public GfmlDocEditor EndParen_(GfmlTkn v) {endParen = v; return this;} GfmlTkn endParen = GfmlTkn_.new_(")", "");
// public GfmlTkn BgnBrace() {return bgnBrace;} public GfmlDocEditor BgnBrace_(GfmlTkn v) {bgnBrace = v; return this;} GfmlTkn bgnBrace = GfmlTkn_.new_("{", "");
// public GfmlTkn EndBrace() {return endBrace;} public GfmlDocEditor EndBrace_(GfmlTkn v) {endBrace = v; return this;} GfmlTkn endBrace = GfmlTkn_.new_("}", "");
// public GfmlTkn Hnd() {return hnd;} public GfmlDocEditor Hnd_(GfmlTkn v) {hnd = v; return this;} GfmlTkn hnd = GfmlTkn_.new_(":", "");
// public static final GfmlDocEditor Instance = new GfmlDocEditor(); GfmlDocEditor() {}
// }

View File

@@ -0,0 +1,128 @@
/*
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.gfml; import gplx.*;
class GfmlDocLxrs {
@gplx.Internal protected static void Default_lxr(GfmlLxrRegy regy, GfmlLxr rootLxr) {
GfmlLxr[] ary = new GfmlLxr[] { GfmlDocLxrs.Whitespace_lxr()
, GfmlDocLxrs.Quote0_Eval0_lxr()
, GfmlDocLxrs.Quote1_lxr()
, GfmlDocLxrs.QuoteBlock_lxr()
, GfmlDocLxrs.QuoteBlock1_lxr()
, GfmlDocLxrs.QuoteBlock2_lxr()
, GfmlDocLxrs.NdeBodyBgn_lxr()
, GfmlDocLxrs.NdeBodyEnd_lxr()
, GfmlDocLxrs.NdeHeader_lxr()
, GfmlDocLxrs.NdeInline_lxr()
, GfmlDocLxrs.ElmKey_lxr()
, GfmlDocLxrs.Comment0_lxr()
, GfmlDocLxrs.Comment1_lxr()
, GfmlDocLxrs.NdePropBgn_lxr()
, GfmlDocLxrs.NdePropEnd_lxr()
};
// if (Op_sys.Cur().Tid_is_wnt())
// regy.Add(Comment0a_lxr());
for (GfmlLxr lxr : ary)
regy.Add(lxr);
rootLxr.SubLxr_Add(ary);
}
public static GfmlLxr Root_lxr() {
GfmlTkn txtTkn = GfmlTkn_.cmd_("tkn:text", GfmlBldrCmd_dataTkn_set.Instance);
return GfmlLxr_.general_("lxr:root", txtTkn);
}
public static GfmlLxr Whitespace_lxr() {
GfmlTkn tkn = GfmlTkn_.cmd_("key:gfml.whitespace_0", GfmlBldrCmd_whitespace.Instance);
GfmlLxr rv = GfmlLxr_.range_("lxr:gfml.whitespace_0", String_.Ary(" ", String_.Tab, String_.CrLf, String_.Lf), tkn, false);
return rv;
}
public static GfmlLxr Comment0_lxr() {return GfmlLxr_.frame_("gfml.comment_0", GfmlFrame_.comment_(), "//", String_.Lf);}
// public static GfmlLxr Comment0a_lxr() {return GfmlLxr_.frame_("gfml.comment_0b", GfmlFrame_.comment_(), "//", "\n");}
public static GfmlLxr Comment1_lxr() {
GfmlLxr rv = GfmlLxr_.frame_("gfml.comment_1", GfmlFrame_.comment_(), "/*", "*/");
GfmlLxr escapeBgn = lxr_escape_("gfml.comment_1_escape_bgn", "/*/*", "/*");
GfmlLxr escapeEnd = lxr_escape_("gfml.comment_1_escape_end", "*/*/", "*/");
rv.SubLxr_Add(escapeBgn, escapeEnd, rv);
return rv;
}
public static GfmlLxr Eval0_lxr() {return GfmlLxr_.frame_("gfml.eval_0", GfmlFrame_.eval_(), "<~", ">");}
public static GfmlLxr Quote0_Eval0_lxr() {
GfmlLxr rv = Quote0_lxr();
GfmlLxr eval0 = Eval0_lxr();
rv.SubLxr_Add(eval0);
eval0.SubLxr_Add(rv); // recursive!
return rv;
}
public static GfmlLxr Quote0_lxr() {
GfmlLxr rv = GfmlLxr_.frame_("gfml.quote_0", GfmlFrame_.quote_(), "'", "'");
GfmlLxr escape = lxr_escape_("gfml.quote_0_escape", "''", "'");
rv.SubLxr_Add(escape);
return rv;
}
public static GfmlLxr Quote1_lxr() {
GfmlLxr rv = GfmlLxr_.frame_("gfml.quote_1", GfmlFrame_.quote_(), "\"", "\"");
GfmlLxr escape = lxr_escape_("gfml.quote_1_escape", "\"\"", "\"");
rv.SubLxr_Add(escape);
return rv;
}
public static GfmlLxr QuoteBlock_lxr() {
GfmlLxr rv = GfmlLxr_.frame_("gfml.quote_block_0", GfmlFrame_.quote_(), "|'", "'|");
GfmlLxr escapeBgn = lxr_escape_("gfml.quote_block_0_escape_bgn", "|'|'", "|'");
GfmlLxr escapeEnd = lxr_escape_("gfml.quote_block_0_escape_end", "'|'|", "'|");
rv.SubLxr_Add(escapeBgn, escapeEnd, rv); // NOTE: adding rv makes it recursive
return rv;
}
public static GfmlLxr QuoteBlock1_lxr() {
GfmlLxr rv = GfmlLxr_.frame_("gfml.quote_block_1", GfmlFrame_.quote_(), "<:['\n", "\n']:>");
// GfmlLxr escapeBgn = lxr_escape_("gfml.quote_block_1_escape_bgn", "<~{'\n<~{'\n", "<~{'\n"); // doesn't work; causes dangling errors; need to debug later
// GfmlLxr escapeEnd = lxr_escape_("gfml.quote_block_1_escape_end", "\n'}~>\n'}~>", "\n'}~>");
// rv.SubLxr_Add(rv);
return rv;
}
public static GfmlLxr QuoteBlock2_lxr() {
GfmlLxr rv = GfmlLxr_.frame_("gfml.quote_block_2", GfmlFrame_.quote_(), "<:[\"\n", "\n\"]:>");
return rv;
}
public static GfmlLxr QuoteFold_lxr() {
GfmlLxr rv = GfmlLxr_.frame_("gfml.quote_fold_0", GfmlFrame_.quote_(), "^'", "'^");
GfmlTkn tkn = GfmlTkn_.valConst_("key:gfml.quote_fold_0_whitespace", GfmlTkn_.NullVal, GfmlBldrCmd_whitespace.Instance);
GfmlLxr whitespace = GfmlLxr_.range_("lxr:gfml.quote_fold_0_whitespace", String_.Ary(String_.Tab, String_.CrLf, String_.Lf), tkn, false);
GfmlLxr escapeBgn = lxr_escape_("gfml.quote_fold_0_escape_bgn", "^'^'", "^'");
GfmlLxr escapeEnd = lxr_escape_("gfml.quote_fold_0_escape_end", "'^'^", "'^");
rv.SubLxr_Add(whitespace, escapeBgn, escapeEnd, rv); // NOTE: adding rv makes it recursive
rv.SubLxr_Add(Eval0_lxr(),
Comment0_lxr(),
Comment1_lxr());
return rv;
}
public static GfmlLxr ElmKey_lxr() {return lxr_symbol_("gfml.elm_key_0", "=", GfmlBldrCmd_elemKey_set.Instance);}
public static GfmlLxr NdeHeader_lxr() {return lxr_symbol_("gfml.node_name_0", ":", GfmlBldrCmd_ndeName_set.Instance);}
public static GfmlLxr NdeInline_lxr() {return lxr_symbol_("gfml.node_inline_0", ";", GfmlBldrCmd_ndeInline.Instance);}
public static GfmlLxr NdeBodyBgn_lxr() {return lxr_symbol_("gfml.node_body_0_begin", "{", GfmlBldrCmd_ndeBody_bgn.Instance);}
public static GfmlLxr NdeBodyEnd_lxr() {return lxr_symbol_("gfml.node_body_0_end", "}", GfmlBldrCmd_frameEnd.nde_(GfmlNdeSymType.BodyEnd));}
public static GfmlLxr NdePropBgn_lxr() {return lxr_symbol_("lxr.gfml.node_prop_0_bgn", "[", GfmlBldrCmd_ndeProp_bgn.Instance);}
public static GfmlLxr NdePropEnd_lxr() {return lxr_symbol_("lxr.gfml.node_prop_0_end", "]", GfmlBldrCmd_frameEnd.nde_(GfmlNdeSymType.PrpEnd));}
public static GfmlLxr NdeDot_lxr() {return lxr_symbol_("gfml.node_drill_0", ".", GfmlBldrCmd_ndeDot.Instance);}
public static GfmlLxr NdeHdrBgn_lxr() {return lxr_symbol_("lxr.gfml.node_hdr_0_bgn", "(", GfmlBldrCmd_ndeHdr_bgn.Instance);}
public static GfmlLxr NdeHdrEnd_lxr() {return lxr_symbol_("lxr.gfml.node_hdr_0_end", ")", GfmlBldrCmd_ndeHdr_end.Instance);}
public static GfmlLxr AtrSpr_lxr() {return lxr_symbol_("lxr.gfml.atrSpr", ",", GfmlBldrCmd_atrSpr.Instance);}
static GfmlLxr lxr_escape_(String key, String raw, String escape) {return GfmlLxr_.symbol_(key, raw, escape, GfmlBldrCmd_pendingTkns_add.Instance);}
static GfmlLxr lxr_symbol_(String key, String raw, GfmlBldrCmd cmd) {return GfmlLxr_.symbol_(key, raw, raw, cmd);}
}

View File

@@ -0,0 +1,69 @@
/*
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.gfml; import gplx.*;
import gplx.core.strings.*;
public class GfmlDocPos implements CompareAble {
public String Path() {if (path == null) MakePath(); return path;} private String path;
public int compareTo(Object obj) {
/* Same: same coord (ex: 0_1 = 0_1)
More: higher level (ex: 0_1 > 0_1_0) or higher idx (ex: 0_1 > 0_2)
Less: lower level (ex: 0_1 < 0) or lower idx (ex: 0_1 < 0_0) */
GfmlDocPos comp = (GfmlDocPos)obj;
for (int i = 0; i < ary.length; i++) {
if (i >= comp.ary.length) return CompareAble_.More; // more ary than comp and whatever ary they share are equal; must be more;
int origVal = ary[i];
int compVal = comp.ary[i];
if (origVal == compVal) continue; // indexes are equal; continue to next
else if (origVal < compVal) return CompareAble_.Less;
else if (origVal > compVal) return CompareAble_.More;
}
if (ary.length < comp.ary.length) return CompareAble_.Less; // less ary than comp, and whatever ary they share are equal; must be less
return Int_.Compare(idx, comp.idx); // compare idx
}
public GfmlDocPos NewClone() {return new GfmlDocPos(ary, idx);}
public GfmlDocPos NewDown(int newIdx) {
int oldLen = ary.length;
int[] newAry = new int[oldLen + 1];
for (int i = 0; i < oldLen; i++)
newAry[i] = ary[i];
newAry[oldLen] = idx;
return new GfmlDocPos(newAry, newIdx);
}
public GfmlDocPos NewUp() {
int oldLen = ary.length; if (oldLen == 0) return GfmlDocPos_.Null;
int[] newAry = new int[oldLen - 1];
for (int i = 0; i < oldLen - 1; i++)
newAry[i] = ary[i];
int newIdx = ary[oldLen - 1];
return new GfmlDocPos(newAry, newIdx);
}
@Override public String toString() {return path;} public String To_str() {return path;}
void MakePath() {
String_bldr sb = String_bldr_.new_();
for (int i = 0; i < ary.length; i++) {
sb.Add(ary[i]);
sb.Add("_");
}
sb.Add(idx);
path = sb.To_str();
}
int[] ary; int idx;
@gplx.Internal protected GfmlDocPos(int[] ary, int idx) {this.ary = ary; this.idx = idx;}
}
class GfmlDocPos_ {
public static final GfmlDocPos Null = new GfmlDocPos(new int[0], -1);
public static final GfmlDocPos Root = new GfmlDocPos(new int[0], 0);
}

View File

@@ -0,0 +1,38 @@
/*
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.gfml; import gplx.*;
import gplx.core.strings.*;
public class GfmlDocWtr_ {
public String To_str_and_clear() {return sb.To_str_and_clear();}
public void BuildAttrib(GfmlAtr atr) {Build(atr);}
public void BuildNode(GfmlNde nde) {Build(nde);}
void Build(GfmlItm owner) {
for (int i = 0; i < owner.SubObjs_Count(); i++) {
GfmlObj subObj = owner.SubObjs_GetAt(i);
GfmlItm subItm = GfmlItm_.as_(subObj);
if (subItm == null)
sb.Add(GfmlTkn_.as_(subObj).Raw());
else
Build(subItm);
}
}
String_bldr sb = String_bldr_.new_();
public static String xtoStr_(GfmlNde nde) {
GfmlDocWtr_ wtr = new GfmlDocWtr_();
wtr.BuildNode(nde);
return wtr.To_str_and_clear();
}
}

View File

@@ -0,0 +1,44 @@
/*
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.gfml; import gplx.*;
public class GfmlDoc_ {
public static GfmlDoc parse_any_eol_(String raw) {return parse(String_.Replace(raw, String_.CrLf, String_.Lf));}
public static GfmlDoc parse(String raw) {
GfmlBldr bldr = GfmlBldr_.default_();
return bldr.XtoGfmlDoc(raw);
}
}
class GfmlUsrMsgs {
public static UsrMsg fail_HndTkn_alreadyExists() {return UsrMsg.new_("hndTkn already exists");}
public static UsrMsg fail_KeyTkn_alreadyExists() {return UsrMsg.new_("keyTkn already exists");}
public static UsrMsg fail_DatTkn_notFound() {return UsrMsg.new_("datTkn not found");}
public static UsrMsg fail_Frame_danglingBgn() {return UsrMsg.new_("dangling frame");}
public static void MakeErr(GfmlBldr bldr, UsrMsg um, String raw) {
bldr.Doc().UsrMsgs().Add(um);
GfmlStringHighlighter sh = GfmlStringHighlighter.new_();
sh.Raw_(raw).Mark_(bldr.StreamPos(), '*', "failed");
um.Add("errorPos", bldr.StreamPos());
um.Add("errorHighlight", String_.CrLf + String_.Concat_lines_crlf(sh.Gen()));
}
public static Err gfmlParseError(GfmlBldr bldr) {
Err rv = Err_.new_wo_type("gfml parse error");
for (int i = 0; i < bldr.Doc().UsrMsgs().Count(); i++) {
UsrMsg um = (UsrMsg)bldr.Doc().UsrMsgs().Get_at(i);
rv.Args_add("err" + Int_.To_str(i), um.To_str());
}
return rv;
}
}

View File

@@ -0,0 +1,27 @@
/*
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.gfml; import gplx.*;
public interface GfmlItm extends GfmlObj, To_str_able {
GfmlTkn KeyTkn(); String Key(); // Key() is alternative to Key().Val()
GfmlType Type();
boolean KeyedSubObj();
int SubObjs_Count();
GfmlObj SubObjs_GetAt(int i);
void SubObjs_Add(GfmlObj obj);
}
class GfmlItm_ {
public static GfmlItm as_(Object obj) {return obj instanceof GfmlItm ? (GfmlItm)obj : null;}
}

View File

@@ -0,0 +1,22 @@
/*
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.gfml; import gplx.*;
public class GfmlItmHnds {
public int Count() {return list.Count();} List_adp list = List_adp_.New();
public GfmlNde Get_at(int idx) {return (GfmlNde)list.Get_at(idx);}
public void Add(GfmlNde nde) {list.Add(nde);}
public static GfmlItmHnds new_() {return new GfmlItmHnds();} GfmlItmHnds() {}
}

View File

@@ -0,0 +1,59 @@
/*
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.gfml; import gplx.*;
public class GfmlItmKeys {
public int Count() {return list.Count();}
public boolean Has(String key) {return hash.Has(key);}
public GfmlItm Get_at(int i) {return (GfmlItm)list.Get_at(i);}
public GfmlItm Get_by(String key) {return (GfmlItm)hash.Get_by(key);}
public String FetchDataOr(String key, String or) {
GfmlAtr atr = FetchAtr(key);
return (atr == null) ? or : atr.DatTkn().Val();
}
public String FetchDataOrNull(String key) {return FetchDataOr(key, null);}
public String FetchDataOrFail(String key) {
GfmlAtr atr = FetchAtr(key); if (atr == null) throw Err_.new_missing_key(key);
return atr.DatTkn().Val();
}
public GfmlTkn FetchDataTknOrNull(String key) {
GfmlAtr atr = FetchAtr(key); if (atr == null) return GfmlTkn_.Null;
return atr.DatTkn();
}
public void Add(GfmlItm itm) {
String key = itm.Key();
if (hash.Has(key)) DelDefault(itm); // default attribs are added automatically; drop default when adding newElm
list.Add(itm);
if (!String_.Eq(key, GfmlItmKeys.NullKey))
RegisterKey(key, itm);
}
@gplx.Internal protected void RegisterKey(String key, GfmlItm itm) {
if (hash.Has(key)) hash.Del(key); // replace default
hash.Add(key, itm);
}
void DelDefault(GfmlItm elm) {
GfmlItm toDel = null;
for (Object subObj : list) {
GfmlItm sub = (GfmlItm)subObj;
if (String_.Eq(sub.Key(), elm.Key()))
toDel = sub;
}
if (toDel != null) list.Del(toDel);
}
GfmlAtr FetchAtr(String key) {return GfmlAtr.as_(hash.Get_by(key));}
List_adp list = List_adp_.New(); Hash_adp hash = Hash_adp_.New();
public static GfmlItmKeys new_() {return new GfmlItmKeys();} GfmlItmKeys() {}
@gplx.Internal protected static final String NullKey = "";
}

View File

@@ -0,0 +1,111 @@
/*
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.gfml; import gplx.*;
public class GfmlNde implements GfmlItm {
public static final int OBJ_TYPE = GfmlObj_.Type_nde;
public int ObjType() {return objType;} int objType = GfmlObj_.Type_nde;
public GfmlTkn KeyTkn() {return keyTkn;} public String Key() {return keyTkn.Val();} GfmlTkn keyTkn = GfmlTkn_.Null;
public GfmlType Type() {return type;} GfmlType type;
public boolean KeyedSubObj() {return keyedSubObj;} public GfmlNde KeyedSubObj_(boolean v) {keyedSubObj = v; return this;} private boolean keyedSubObj;
public int ChainId() {return chainId;} public GfmlNde ChainId_(int v) {chainId = v; return this;} int chainId; // can use boolean chainHead, but this is easier for debugging
public int SubObjs_Count() {return subObjs.Count();} GfmlObjList subObjs = GfmlObjList.new_();
public GfmlObj SubObjs_GetAt(int i) {return (GfmlObj)subObjs.Get_at(i);}
public void SubObjs_Add(GfmlObj gobj) {
subObjs.Add(gobj);
GfmlItm subItm = GfmlItm_.as_(gobj);
if ( subItm == null // gobj is tkn: symbol, whitespace, comment
|| subItm.ObjType() == GfmlObj_.Type_prg) // gobj is pragma
return;
if (subItm.KeyedSubObj())
subKeys.Add(subItm);
else
subHnds.Add(GfmlNde.as_(subItm));
}
public String Hnd() {return hndTkn.Val();} GfmlTkn hndTkn = GfmlTkn_.Null;
public GfmlDocPos DocPos() {return docPos;} GfmlDocPos docPos = GfmlDocPos_.Null;
public GfmlItmKeys SubKeys() {return subKeys;} GfmlItmKeys subKeys = GfmlItmKeys.new_();
public GfmlItmHnds SubHnds() {return subHnds;} GfmlItmHnds subHnds = GfmlItmHnds.new_();
public String To_str() {return GfmlDocWtr_.xtoStr_(this);}
public void UpdateNde(String hnd) {
for (int i = 0; i < subHnds.Count(); i++) {
GfmlNde nde = (GfmlNde)subHnds.Get_at(i);
if (String_.Eq(nde.hndTkn.Raw(), hnd)) return;
}
int endAtrPos = PosOf(false, ";", "}");
GfmlTkn bgnParen = GfmlTkn_.new_("(", "");
GfmlTkn endParen = GfmlTkn_.new_(")", "");
GfmlTkn bgnBrace = GfmlTkn_.new_("{", "");
GfmlTkn endBrace = GfmlTkn_.new_("}", "");
GfmlTkn hndTkn = GfmlTkn_.new_(hnd, hnd);
GfmlNde subNde = GfmlNde.new_(hndTkn, GfmlType_.new_any_(), false);
subNde.SubObjs_Add(hndTkn);
subNde.SubObjs_Add(bgnParen);
subNde.SubObjs_Add(endParen);
subNde.SubObjs_Add(bgnBrace);
subNde.SubObjs_Add(endBrace);
subObjs.Add_at(subNde, endAtrPos);
// a(){b(){}}
// a:{} -> a:{b:{}}
}
int PosOf(boolean fwd, String... find) {
int bgn = fwd ? 0 : subObjs.Count() - 1;
int end = fwd ? subObjs.Count() : 0;
int dif = fwd ? 1 : -1;
for (int i = bgn; i != end; i+=dif) {
GfmlObj subObj = (GfmlObj)subObjs.Get_at(i);
GfmlTkn subTkn = GfmlTkn_.as_(subObj);
if (subTkn == null) continue;
if (String_.In(subTkn.Raw(), find)) {
return i;
}
}
return -1;
}
public void UpdateAtr(String key, String val) {
GfmlAtr atr = (GfmlAtr)subKeys.Get_by(key);
if (atr != null) {atr.UpdateAtr(key, val); return;}
val = String_.Replace(val, "'", "''");
GfmlTkn quote = GfmlTkn_.new_("'", "");
GfmlTkn keyTkn = GfmlTkn_.raw_(key);
GfmlTkn valTkn = GfmlTkn_.composite_("composite", GfmlTknAry_.ary_(quote, GfmlTkn_.raw_(val), quote));
GfmlTkn eqTkn = GfmlTkn_.new_("=", "");
atr = GfmlAtr.string_(keyTkn, valTkn);
atr.SubObjs_Add(keyTkn);
atr.SubObjs_Add(eqTkn);
atr.SubObjs_Add(valTkn);
int endAtrPos = PosOf(true, ";", "{");
if (subKeys.Count() != 0) {
subObjs.Add_at(GfmlTkn_.new_(" ", ""), endAtrPos);
endAtrPos++;
}
subObjs.Add_at(atr, endAtrPos);
}
@gplx.Internal protected void ObjType_set_pragma() {objType = GfmlObj_.Type_prg;}
@gplx.Internal protected void KeyTkn_set(GfmlTkn gobj) {keyTkn = gobj;}
@gplx.Internal protected void Type_set(GfmlType val) {type = val;}
@gplx.Internal protected void SubObjs_Clear() {subObjs.Clear();}
@gplx.Internal protected GfmlTkn HndTkn() {return hndTkn;}
@gplx.Internal protected void HndTkn_set(GfmlTkn tkn) {hndTkn = tkn;}
@gplx.Internal protected void Hnd_set(String v) {hndTkn = String_.Len_eq_0(v) ? GfmlTkn_.Null : GfmlTkn_.val_(v);} // NOTE: v is empty for types with empty fldNames
@gplx.Internal protected GfmlNde DocPos_(GfmlDocPos val) {docPos = val; return this;}
public static GfmlNde as_(Object obj) {return obj instanceof GfmlNde ? (GfmlNde)obj : null;}
@gplx.Internal protected static GfmlNde named_(GfmlTkn hndTkn, GfmlType type) {return new GfmlNde(hndTkn, type, false);}
@gplx.Internal protected static GfmlNde new_(GfmlTkn hndTkn, GfmlType type, boolean keyedSubObj) {return new GfmlNde(hndTkn, type, keyedSubObj);}
GfmlNde(GfmlTkn hndTkn, GfmlType type, boolean keyedSubObj) {
this.hndTkn = hndTkn; this.type = type; this.keyedSubObj = keyedSubObj;}
}

View File

@@ -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.gfml; import gplx.*;
interface GfmlScopeItm {
String Key();
GfmlDocPos DocPos();
}
class GfmlScopeRegy {
public boolean Has(String key) {
GfmlScopeList list = (GfmlScopeList)hash.Get_by(key); if (list == null) return false;
return list.Count() > 0;
}
public void Add(GfmlScopeItm itm) {
GfmlScopeList list = ItmOrNew(itm.Key());
list.Add(itm);
}
public void Del(GfmlScopeItm itm) {
GfmlScopeList list = (GfmlScopeList)hash.Get_by(itm.Key()); if (list == null) return;
list.Del(itm);
if (list.Count() == 0) hash.Del(itm.Key());
}
public GfmlScopeItm Get_by(String key, GfmlDocPos pos) {
GfmlScopeList list = (GfmlScopeList)hash.Get_by(key); if (list == null) return null;
return list.Get_by(pos);
}
GfmlScopeList ItmOrNew(String key) {
GfmlScopeList rv = (GfmlScopeList)hash.Get_by(key);
if (rv == null) {
rv = GfmlScopeList.new_(key);
hash.Add(key, rv);
}
return rv;
}
Hash_adp hash = Hash_adp_.New();
public static GfmlScopeRegy new_() {return new GfmlScopeRegy();}
}
class GfmlScopeList {
public String Key() {return key;} private String key;
public int Count() {return list.Count();}
public void Add(GfmlScopeItm itm) {list.Add(itm);}
public void Del(GfmlScopeItm itm) {list.Del(itm);}
public GfmlScopeItm Get_by(GfmlDocPos pos) {
if (list.Count() == 0) return null;
GfmlScopeItm rv = null;
for (Object itemObj : list) {
GfmlScopeItm itm = (GfmlScopeItm)itemObj;
if (CompareAble_.Is(CompareAble_.More_or_same, pos, itm.DocPos()))
rv = itm;
else
break; // ASSUME: insertion is done in order; first lessThan means rest will also be lessThan
}
return rv;
}
List_adp list = List_adp_.New();
public static GfmlScopeList new_(String key) {
GfmlScopeList rv = new GfmlScopeList(); rv.key = key; return rv;
} GfmlScopeList() {}
}