GfmlDataNde.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. XOWA: the XOWA Offline Wiki Application
  3. Copyright (C) 2012-2017 gnosygnu@gmail.com
  4. XOWA is licensed under the terms of the General Public License (GPL) Version 3,
  5. or alternatively under the terms of the Apache License Version 2.0.
  6. You may use XOWA according to either of these licenses as is most appropriate
  7. for your project on a case-by-case basis.
  8. The terms of each license can be found in the source code repository:
  9. GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
  10. Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
  11. */
  12. package gplx.gfml;
  13. import gplx.core.stores.*; import gplx.core.gfo_ndes.*;
  14. import gplx.frameworks.invks.GfoMsg;
  15. import gplx.frameworks.invks.GfoMsg_;
  16. import gplx.types.basics.utls.StringUtl;
  17. public class GfmlDataNde {
  18. public GfmlDoc Doc() {return gdoc;} GfmlDoc gdoc;
  19. public DataRdr XtoRdr() {
  20. GfmlDataRdr rv = new GfmlDataRdr();
  21. rv.SetNode(gdoc.RootNde());
  22. return rv;
  23. }
  24. public DataWtr XtoWtr() {
  25. GfmlDataWtr2 rv = new GfmlDataWtr2();
  26. rv.Doc_set(gdoc);
  27. return rv;
  28. }
  29. public static GfmlDataNde new_any_eol_(String raw) {return new_(StringUtl.Replace(raw, StringUtl.CrLf, StringUtl.Nl));}
  30. public static GfmlDataNde new_(String raw) {
  31. GfmlDataNde rv = new GfmlDataNde();
  32. GfmlBldr bldr = GfmlBldr_.default_();
  33. bldr.Doc().RootLxr().SubLxr_Add
  34. ( GfmlDocLxrs.AtrSpr_lxr()
  35. , GfmlDocLxrs.NdeDot_lxr()
  36. , GfmlDocLxrs.NdeHdrBgn_lxr()
  37. , GfmlDocLxrs.NdeHdrEnd_lxr()
  38. );
  39. rv.gdoc = bldr.XtoGfmlDoc(raw);
  40. return rv;
  41. }
  42. public static GfoMsg XtoMsg(String raw) {
  43. GfmlDoc gdoc = GfmlDataNde.new_any_eol_(raw).Doc();
  44. return XtoMsg(gdoc.RootNde());
  45. }
  46. public static GfoMsg XtoMsgNoRoot(String raw) {
  47. GfmlDoc gdoc = GfmlDataNde.new_any_eol_(raw).Doc();
  48. GfoMsg msg = XtoMsg(gdoc.RootNde());
  49. return (GfoMsg)msg.Subs_getAt(0);
  50. }
  51. private static String StringUtl_Coalesce(String s, String alt) {return StringUtl.IsNullOrEmpty(s) ? alt : s;}
  52. static GfoMsg XtoMsg(GfmlNde gnde) {
  53. String msgKey = StringUtl_Coalesce(gnde.Key(), gnde.Hnd());
  54. GfoMsg msg = GfoMsg_.new_parse_(msgKey);
  55. for (int i = 0; i < gnde.SubKeys().Count(); i++) {
  56. GfmlItm subItm = (GfmlItm)gnde.SubKeys().Get_at(i);
  57. if (subItm.ObjType() == GfmlObj_.Type_atr) {
  58. GfmlAtr subAtr = (GfmlAtr)subItm;
  59. String subAtrKey = StringUtl.IsNullOrEmpty(subAtr.Key()) ? "" : subAtr.Key(); // NOTE: needs to be "" or else will fail in GfoConsole; key will be evaluated against NullKey in GfsCtx
  60. msg.Add(subAtrKey, subAtr.DatTkn().Val());
  61. }
  62. else {
  63. GfmlNde subNde = (GfmlNde)subItm;
  64. GfoMsg subMsg = XtoMsg(subNde);
  65. msg.Subs_add(subMsg);
  66. }
  67. }
  68. for (int i = 0; i < gnde.SubHnds().Count(); i++) {
  69. GfmlItm subItm = (GfmlItm)gnde.SubHnds().Get_at(i);
  70. GfmlNde subNde = (GfmlNde)subItm;
  71. GfoMsg subMsg = XtoMsg(subNde);
  72. msg.Subs_add(subMsg);
  73. }
  74. return msg;
  75. }
  76. }
  77. class GfmlDataWtr2 extends DataWtr_base implements DataWtr {
  78. @Override public void WriteData(String name, Object val) {
  79. GfmlTkn nameTkn = GfmlTkn_.raw_(name);
  80. GfmlTkn valTkn = GfmlTkn_.raw_(To_str(val));
  81. GfmlAtr atr = GfmlAtr.new_(nameTkn, valTkn, GfmlType_.String);
  82. GfmlNde nde = gdoc.RootNde().SubHnds().Get_at(0);
  83. nde.SubKeys().Add(atr);
  84. }
  85. public void InitWtr(String key, Object val) {}
  86. public void WriteTableBgn(String name, GfoFldList fields) {}
  87. @Override public void WriteNodeBgn(String nodeName) {}
  88. public void WriteLeafBgn(String leafName) {}
  89. @Override public void WriteNodeEnd() {}
  90. public void WriteLeafEnd() {}
  91. public void Clear() {}
  92. public String To_str() {return "";}
  93. String To_str(Object obj) {
  94. if (obj == null) return "''";
  95. String s = obj.toString();
  96. return StringUtl.Concat("'", StringUtl.Replace(s, "'", "''"), "'");
  97. }
  98. @Override public SrlMgr SrlMgr_new(Object o) {return new GfmlDataWtr2();}
  99. public void Doc_set(GfmlDoc v) {gdoc = v;} GfmlDoc gdoc;
  100. }