Home /Modules/xml/xml-dom.sleela

xml-dom.sleela

lib/xml/xml-dom.sleela · 259 line(s) · sleela

#sleela 2.5
material;
< xml-dom.sleela -- the self-contained, runnable demonstration of the SLeeLa XML >
< DOM family. The compiler's `run` compiles one directory as a unit, so this >
< demo drives the sibling classes directly: it BUILDS a document with the DOM >
< factory + appendChild/setAttribute, QUERIES it (getElementsByTagName, >
< getElementById, textContent, elementCount, depth), SERIALIZES it (compact and >
< pretty), PARSES the serialized text back into a fresh DOM to show the round >
< trip, and reports the aspect/quality register. >
<  >
< SLeeLa drives the Document; the Document owns the tree and the serializer, and >
< the Parser turns text back into a Document. >
class XmlDom {
  < ---- Descriptive metadata (version 1.11 nested-friendly accessors) ---- >
  << Eleven catalogued attributes with getter/setter pairs, so a GUI, loader,
     or catalog can read and (where appropriate) override this record.
     Values are faithful to the subject's real origin where publicly known. >>
  String metaVersion = "1.0.0";
  int metaId = 7928;
  String metaAuthor = "Kiro Agent";
  String metaDate = "2026-10-28";
  String metaCopyright = "SLeeLa project";
  String metaTrademark = "none";
  String metaCorporation = "none";
  < metaOrigin: National | <nation> | International | Galactic >
  String metaOrigin = "International";
  String metaBirthDate = "2026-10-28";
  < metaScienceRating: maturity/robustness on a 1..10 scale >
  int metaScienceRating = 9;
  < metaNationalSchedule: a real, citable instrument. >
  String metaNationalSchedule = "W3C REC-xml (XML 1.0, Fifth Edition)";

  String getMetaVersion() { return metaVersion; }
  void setMetaVersion(String v) { metaVersion = v; }
  int getMetaId() { return metaId; }
  void setMetaId(int v) { metaId = v; }
  String getAuthor() { return metaAuthor; }
  void setAuthor(String v) { metaAuthor = v; }
  String getDate() { return metaDate; }
  void setDate(String v) { metaDate = v; }
  String getCopyright() { return metaCopyright; }
  void setCopyright(String v) { metaCopyright = v; }
  String getTrademark() { return metaTrademark; }
  void setTrademark(String v) { metaTrademark = v; }
  String getCorporation() { return metaCorporation; }
  void setCorporation(String v) { metaCorporation = v; }
  String getOrigin() { return metaOrigin; }
  void setOrigin(String v) { metaOrigin = v; }
  String getBirthDate() { return metaBirthDate; }
  void setBirthDate(String v) { metaBirthDate = v; }
  int getScienceRating() { return metaScienceRating; }
  void setScienceRating(int v) { metaScienceRating = v; }
  String getNationalSchedule() { return metaNationalSchedule; }
  void setNationalSchedule(String v) { metaNationalSchedule = v; }

  << Build a small catalog document: a <catalog> of two <item> elements, one
     carrying an id and an origin attribute naming the United States. The second
     item's name contains & and < > to exercise escaping; because the built-in
     String is opaque, that content is supplied as an explicit char-cell String[]
     (addTextCells), the sanctioned real char-level path -- so the serializer
     escapes it to &amp; &lt; &gt; correctly. Known-safe content uses plain
     addText/setAttribute and serializes verbatim. >>
  SLXmlDocument buildCatalog() {
    SLXmlDocument doc = new SLXmlDocument();
    doc.create();

    SLXmlNode catalog = doc.newRoot("catalog");
    catalog.setAttribute("xmlns", "urn:sleela:xml:demo");

    SLXmlNode a = doc.createElement("item");
    a.setAttribute("id", "a1");
    a.setAttribute("origin", "United States");
    SLXmlNode an = doc.createElement("name");
    an.addText("Liberty Bell");
    a.appendChild(an);
    SLXmlNode aq = doc.createElement("quality");
    aq.addText("9");
    a.appendChild(aq);
    catalog.appendChild(a);

    SLXmlNode b = doc.createElement("item");
    b.setAttribute("id", "b2");
    b.setAttribute("origin", "International");
    SLXmlNode bn = doc.createElement("name");
    < "Jack & Jill <xml>" as char cells so & < > are escaped on output. >
    String raw = "Jack & Jill <xml>";
    bn.addTextCells(raw, nameCells());
    b.appendChild(bn);
    SLXmlNode bq = doc.createElement("quality");
    bq.addText("7");
    b.appendChild(bq);
    catalog.appendChild(b);

    catalog.addComment(" generated by the SLeeLa XML DOM demo ");
    return doc;
  }

  < The glyph cells of "Jack & Jill <xml>" (one per index). The opaque built-in >
  < String cannot be sliced in-language, so escapable content is spelled out as >
  < cells, exactly as lib/text/SLString prescribes. >
  String[] nameCells() {
    String[] c = new String[17];
    c[0]="J"; c[1]="a"; c[2]="c"; c[3]="k"; c[4]=" ";
    c[5]="&"; c[6]=" "; c[7]="J"; c[8]="i"; c[9]="l"; c[10]="l";
    c[11]=" "; c[12]="<"; c[13]="x"; c[14]="m"; c[15]="l"; c[16]=">";
    return c;
  }

  < The glyph cells of a tiny document "<doc><p id=\"x1\">hi</p></doc>" used to >
  < demonstrate the text-to-DOM round trip over the char-cell parser. >
  String[] roundTripCells() {
    String[] c = new String[28];
    c[0]="<"; c[1]="d"; c[2]="o"; c[3]="c"; c[4]=">";
    c[5]="<"; c[6]="p"; c[7]=" "; c[8]="i"; c[9]="d"; c[10]="=";
    c[11]="\""; c[12]="x"; c[13]="1"; c[14]="\""; c[15]=">";
    c[16]="h"; c[17]="i";
    c[18]="<"; c[19]="/"; c[20]="p"; c[21]=">";
    c[22]="<"; c[23]="/"; c[24]="d"; c[25]="o"; c[26]="c"; c[27]=">";
    return c;
  }

  void main() {
    print("=== SLeeLa XML DOM demonstration ===");

    SLXmlDocument doc = buildCatalog();

    < ---- structural queries over the live DOM ---- >
    print("document element : " + doc.documentElement().name());
    print("element count    : " + doc.elementCount());
    print("tree depth       : " + doc.depth());

    SLXmlNode items = doc.getElementsByTagName("item");
    print("items found      : " + items.childCount());
    for (int i = 0; i < items.childCount(); i = i + 1) {
      SLXmlNode it = items.childAt(i);
      print("  item id=" + it.getAttribute("id")
          + " origin=" + it.getAttribute("origin")
          + " name=" + nameText(it)
          + " quality=" + qualityText(it));
    }

    SLXmlNode byId = doc.getElementById("a1");
    if (byId != null) {
      print("getElementById(a1) textContent: " + byId.textContent());
    }

    < ---- serialization ---- >
    print("");
    print("--- compact XML ---");
    print(doc.toXml());
    print("");
    print("--- pretty XML ---");
    print(doc.toPrettyXml());

    < ---- round trip: parse a char-cell XML source back into a DOM ---- >
    print("");
    print("--- round trip: parse \"<doc><p id=x1>hi</p></doc>\" from cells ---");
    SLXmlParser parser = new SLXmlParser();
    String[] cells = roundTripCells();
    print("isWellFormed       : " + parser.isWellFormedCells(cells));
    SLXmlDocument back = parser.parseCells(cells);
    print("reparsed root      : " + back.documentElement().name());
    print("reparsed elements  : " + back.elementCount());
    SLXmlNode ps = back.getElementsByTagName("p");
    print("reparsed <p> count : " + ps.childCount());
    SLXmlNode first = ps.childAt(0);
    if (first != null) {
      print("reparsed p id=" + first.getAttribute("id")
          + " text=" + first.textContent());
    }

    < ---- the aspect / quality register ---- >
    print("");
    print("--- XML aspects: the quality of each aspect ---");
    SLXmlAspect aspect = new SLXmlAspect();
    for (int i = 0; i < aspect.count(); i = i + 1) {
      int q = aspect.qualityOf(i);
      print("  " + aspect.nameOf(i) + " [" + aspect.bandOf(q) + " " + q + "/10]"
          + " addressed=" + aspect.isAddressed(i)
          + " item=" + aspect.itemOf(i));
    }
    print("overall quality x10: " + aspect.overallQualityX10()
        + " (origin " + aspect.getOrigin() + ")");

    < ---- the three-rung equality tie (kept in its own method so main stays >
    < small; a very large method strains the VM local-slot budget) ---- >
    demoEquality();

    print("");
    print("=== demonstration complete ===");
  }

  < Demonstrate and verify the three-rung equality ladder on small, standalone >
  < trees, plus the reflexive / symmetric / idempotent charge guarantees. >
  void demoEquality() {
    print("");
    print("--- equality: the three-rung tie of trust ---");

    < Two small, independently-built, structurally-identical trees: >
    <   <item id="x"><name>Bell</name></item> >
    SLXmlNode treeA = makeItem("x", "Bell");
    SLXmlNode treeB = makeItem("x", "Bell");

    < Tier 1 IDENTITY: the same object in memory. >
    SLXmlNode alias = treeA;
    print("identity  treeA vs alias(treeA): tie=" + treeA.tieName(treeA.tieStrength(alias))
        + " sameReference=" + treeA.sameReference(alias));

    < Tier 3 STRUCTURE: distinct objects, identical shape. >
    print("structure treeA vs treeB(copy) : tie=" + treeA.tieName(treeA.tieStrength(treeB))
        + " equals=" + treeA.equals(treeB)
        + " sameReference=" + treeA.sameReference(treeB));

    < Tier 2 SUBJECT only: same <item id="x">, different child text. >
    SLXmlNode treeC = makeItem("x", "A DIFFERENT CHILD");
    print("subject   treeA vs same-id,diff: tie=" + treeA.tieName(treeA.tieStrength(treeC))
        + " sameSubject=" + treeA.sameSubject(treeC)
        + " equals=" + treeA.equals(treeC));

    < Untied: a different subject entirely (different id). >
    SLXmlNode treeD = makeItem("zzz", "Bell");
    print("untied    treeA vs id=zzz item : tie=" + treeA.tieName(treeA.tieStrength(treeD))
        + " sameSubject=" + treeA.sameSubject(treeD));

    < Charge / idempotency guarantees over the tie. >
    print("guarantee reflexive  treeA.equals(treeA)          : " + treeA.equals(treeA));
    print("guarantee symmetric  A.eq(B)==B.eq(A)             : "
        + (treeA.equals(treeB) == treeB.equals(treeA)));
    print("guarantee idempotent re-ask gives the same answer : "
        + (treeA.equals(treeB) == treeA.equals(treeB)));
  }

  < Build a standalone <item id=.. ><name>text</name></item> subtree for the >
  < equality demonstration. Children are bound to typed locals before use. >
  SLXmlNode makeItem(String id, String text) {
    SLXmlNode item = new SLXmlNode();
    item.asElement("item");
    item.setAttribute("id", id);
    SLXmlNode nm = new SLXmlNode();
    nm.asElement("name");
    nm.addText(text);
    item.appendChild(nm);
    return item;
  }

  < The text of an item's <name> child ("" when absent). >
  String nameText(SLXmlNode item) {
    SLXmlNode nm = item.firstElementByTag("name");
    if (nm == null) { return ""; }
    return nm.textContent();
  }
  < The text of an item's <quality> child ("" when absent). >
  String qualityText(SLXmlNode item) {
    SLXmlNode q = item.firstElementByTag("quality");
    if (q == null) { return ""; }
    return q.textContent();
  }
}