Home /Modules/xml/SLXmlAttribute.sleela

SLXmlAttribute.sleela

lib/xml/SLXmlAttribute.sleela · 165 line(s) · sleela

<< lib/xml/SLXmlAttribute.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines SLXmlAttribute -- a single name/value attribute on a DOM
     element, with namespace-aware prefix/local parts. >>
#sleela 2.5
material;
< Family: xml. One attribute of an element node: a qualified name and a string >
< value. The qualified name may carry a prefix ("xml:lang" -> prefix "xml", >
< local "lang"); a bare name has an empty prefix. Values are stored unescaped; >
< the serializer escapes them on output. >
class SLXmlAttribute {
  < ---- 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 = 7922;
  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-names (Namespaces in XML 1.0)";

  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; }

  < The qualified name exactly as given (e.g. "xml:lang"). >
  String qname;
  < The unescaped string value. >
  String value;
  << Optional char-cell view of `value` (one glyph per cell). Supplied when the
     value may contain & < > " so the serializer can escape it; absent for
     known-safe values, which serialize verbatim. >>
  String[] valueCellView;
  boolean valueHasCells = false;

  < Build an attribute from a qualified name and value. >
  void set(String qualifiedName, String v) {
    qname = qualifiedName;
    value = v;
  }

  < Attach a char-cell view of the value for correct escaping on output. >
  void setValueCells(String[] cells) {
    valueCellView = cells;
    valueHasCells = true;
  }
  boolean hasCells() { return valueHasCells; }
  String[] valueCells() { return valueCellView; }

  String getName() { return qname; }
  String getValue() { return value; }
  void setValue(String v) { value = v; }

  << Namespace parts.
     The built-in String is opaque (no in-language slicing), so a qualified name
     is decomposed over an explicit char-cell String[] -- the sanctioned real
     char-level mechanism, exactly as lib/text/SLString does. A caller that knows
     the prefix/local split may also set them directly via setQualified(). >>
  String nsPrefix = "";
  String nsLocal = "";
  boolean split = false;

  < Record an explicit prefix/local split (skips cell scanning). >
  void setQualified(String prefixPart, String localPart, String v) {
    qname = localPart;
    if (prefixPart != "") { qname = prefixPart + ":" + localPart; }
    value = v;
    nsPrefix = prefixPart;
    nsLocal = localPart;
    split = true;
  }

  < Decompose qname into prefix/local over a supplied char-cell view (the cells >
  < of qname, one glyph per cell). Finds the first ":" cell. Real char-level. >
  void resolveFromCells(String[] cells) {
    int n = arrayLength(cells);
    int colon = -1;
    for (int i = 0; i < n; i = i + 1) {
      if (cells[i] == ":") { colon = i; }
      if (colon >= 0) { i = n; }
    }
    if (colon < 0) {
      nsPrefix = "";
      nsLocal = joinRange(cells, 0, n);
    } else {
      nsPrefix = joinRange(cells, 0, colon);
      nsLocal = joinRange(cells, colon + 1, n);
    }
    split = true;
  }
  < Rejoin cells in [from,to) into a String (real; mirrors substringCells). >
  String joinRange(String[] cells, int from, int to) {
    String out = "";
    int i = from;
    if (i < 0) { i = 0; }
    int end = to;
    if (end > arrayLength(cells)) { end = arrayLength(cells); }
    while (i < end) { out = out + cells[i]; i = i + 1; }
    return out;
  }

  < Namespace prefix ("" when unprefixed or not yet resolved). >
  String prefix() { return nsPrefix; }
  < Local name ("" when not yet resolved; equals qname for a bare name). >
  String localName() { return nsLocal; }

  < True when this attribute declares a namespace (xmlns or xmlns:prefix). >
  boolean isNamespaceDeclaration() {
    if (qname == null) { return false; }
    if (qname == "xmlns") { return true; }
    return nsPrefix == "xmlns";
  }

  << ---- equality (the same three-rung tie, for an attribute) ----
     Tier 1 identity is the native == on objects. Tier 2/3 collapse for an
     attribute (it has no subtree): two attributes are the SAME SUBJECT and the
     SAME STRUCTURE exactly when their qualified name and value match, compared
     case-sensitively. Reflexive, symmetric, and deterministic. >>
  boolean sameReference(SLXmlAttribute other) { return this == other; }

  boolean equals(SLXmlAttribute other) {
    if (other == null) { return false; }
    if (this == other) { return true; }
    if (!strEq(qname, other.getName())) { return false; }
    return strEq(value, other.getValue());
  }
  < Alias, mirroring SLXmlNode.sameSubject (identical to equals here). >
  boolean sameSubject(SLXmlAttribute other) { return equals(other); }

  < Null-safe, case-sensitive string equality (two nulls are equal). >
  boolean strEq(String a, String b) {
    if (a == null) { return b == null; }
    if (b == null) { return false; }
    return a == b;
  }
}