XML message wrapper. More...
Public Types | |
typedef char[][char[]] | AttributeMap |
map of XML node attributes | |
Public Member Functions | |
this (in char[] name) | |
char[] | toString () |
bool | read_file (in char[] path) |
bool | read_buffer (in char[] buffer, in char[] system_id="") |
bool | read (in Stream i, in char[] system_id="") |
bool | read (XMLReaderBase reader, char[] display_path) |
void | write (Stream o, WRITE_MODE mode=WRITE_MODE.FORMAT_SMART) |
write XML stream preserving previous white space and comments | |
void | write (Stream o, XMLFormat format, WRITE_MODE mode=WRITE_MODE.FORMAT_SMART, int indent=0) |
write node with children tree to output stream | |
void | write_formating (Stream o) |
write XML stream with formating | |
void | write_formating (in char[] path) |
void | write_file (in char[] path, WRITE_MODE mode=WRITE_MODE.FORMAT_SMART) |
version (XMLNODE_LOCATION) | |
void | clear () |
XMLNode | assign (XMLNode other) |
void | add_child (XMLNode child) |
add a new child node | |
void | put (in char[] attr_name, in char[] value) |
write access to an attribute | |
char[] | opIndex (in char[] attr_name) |
index operator read access to an attribute | |
void | opIndexAssign (in char[] attr_name, in char[] value) |
index operator write access to an attribute | |
char[] | get (in char[] attr_name, in char[] def="") |
read only access to an attribute | |
char *[] | get_ref (in char[] attr_name, in char[] def="") |
write access to an attribute | |
char[] | subvalue (in char[] name, char[] attr_name, int n=0) |
convenient value access in children node | |
char *[] | subvalue_ref (in char[] name, char[] attr_name, int n=0) |
convenient value access in children node | |
char[] | put_subvalue (in char[] name, char[] attr_name, int n=0) |
convenient storage of distinct values in children node | |
Children | get_children () |
AttributeMap | get_attributes () |
char[] | get_content () |
void | set_content (in char[] s, bool cdata=false) |
Public Attributes | |
XMLPos | _pos |
XMLFormat | _format |
XMLErrorList | _errors |
char[] | _name |
Protected Member Functions | |
this () | |
XMLNode | get_first_child () |
XMLNode | find (in char[] name, int n=0) |
XMLNode | find (in char[] name, char[] attr_name, char[] attr_value, int n=0) |
XMLNode | find_relative (in char[] path) |
XPath find function. | |
XMLNode | create_relative (in char[] path) |
relative XPath create function | |
void | write_worker (Stream o) |
write node with children tree to output stream using original white space | |
void | plain_write_worker (Stream o) |
print node without any white space | |
void | pretty_write_worker (Stream o, XMLFormat format, int indent) |
pretty print node with children tree to output stream | |
void | smart_write_worker (Stream o, XMLFormat format, int indent) |
write node with children tree to output stream using smart formating | |
XMLNode | get_child_relative (inout char[] path, bool create) |
Protected Attributes | |
Children | _children |
AttributeMap | _attributes |
char[] | _leading |
char[] | _content |
char[] | _end_leading |
char[] | _trailing |
XML message wrapper.
Definition at line 2222 of file xmlstorage.d.
typedef char [][char[]] xmlstorage::XMLNode::AttributeMap [inherited] |
map of XML node attributes
Definition at line 506 of file xmlstorage.d.
xmlstorage::XMLMessage::this | ( | in char[] | name ) | [inline] |
Reimplemented from xmlstorage::XMLDoc.
Definition at line 2225 of file xmlstorage.d.
xmlstorage::XMLMessage::this | ( | ) | [inline, protected] |
Reimplemented from xmlstorage::XMLDoc.
Definition at line 2243 of file xmlstorage.d.
{ _pos = new XMLPos(this); }
void xmlstorage::XMLNode::add_child | ( | XMLNode | child ) | [inline, inherited] |
Definition at line 572 of file xmlstorage.d.
References xmlstorage::XMLNode::_attributes, xmlstorage::XMLNode::_children, xmlstorage::XMLNode::_content, xmlstorage::XMLNode::_end_leading, xmlstorage::XMLNode::_leading, xmlstorage::XMLNode::_trailing, and xmlstorage::XMLNode::assign().
Referenced by xmlstorage::XMLNode::assign().
{ _children.assign(other._children); _attributes = other._attributes; _leading = other._leading; _content = other._content; _end_leading = other._end_leading; _trailing = other._trailing; return this; }
void xmlstorage::XMLNode::clear | ( | ) | [inline, inherited] |
Definition at line 559 of file xmlstorage.d.
{ _leading.length = 0; _content.length = 0; _end_leading.length = 0; _trailing.length = 0; _attributes = null; _children.clear(); _name.length = 0; }
XMLNode xmlstorage::XMLNode::create_relative | ( | in char[] | path ) | [inline, protected, inherited] |
relative XPath create function
Definition at line 777 of file xmlstorage.d.
References xmlstorage::XMLNode::get_child_relative().
Referenced by xmlstorage::XMLPos::create_relative().
{ XMLNode node = this; // parse relative path while(path.length) { node = node.get_child_relative(path, true); if (*path == '/') path = path[1..length]; } return node; }
XMLNode xmlstorage::XMLNode::find | ( | in char[] | name, |
int | n = 0 |
||
) | [inline, protected, inherited] |
Definition at line 737 of file xmlstorage.d.
Referenced by xmlstorage::XMLNode::get_child_relative(), xmlstorage::XMLPos::go_down(), and xmlstorage::XMLPos::smart_create().
{ foreach(node; _children) if (node._name == name) if (!n--) return node; return null; }
XMLNode xmlstorage::XMLNode::find | ( | in char[] | name, |
char[] | attr_name, | ||
char[] | attr_value, | ||
int | n = 0 |
||
) | [inline, protected, inherited] |
Definition at line 747 of file xmlstorage.d.
{ foreach(node; _children) if (node._name==name && node.get(attr_name)==attr_value) if (!n--) return node; return null; }
XMLNode xmlstorage::XMLNode::find_relative | ( | in char[] | path ) | [inline, protected, inherited] |
XPath find function.
Definition at line 758 of file xmlstorage.d.
References xmlstorage::XMLNode::get_child_relative().
Referenced by xmlstorage::XMLPos::go().
{ XMLNode node = this; // parse relative path while(path.length) { node = node.get_child_relative(path, false); if (!node) return null; if (*path == '/') path = path[1..length]; } return node; }
char [] xmlstorage::XMLNode::get | ( | in char[] | attr_name, |
in char[] | def = "" |
||
) | [inline, inherited] |
read only access to an attribute
Definition at line 611 of file xmlstorage.d.
Referenced by xmlstorage::XMLPos::get(), xmlstorage::XMLNode::put_subvalue(), and xmlstorage::XMLNode::subvalue().
{ char[]* found = attr_name in _attributes; if (found) return *found; else return def; }
AttributeMap xmlstorage::XMLNode::get_attributes | ( | ) | [inline, inherited] |
Definition at line 676 of file xmlstorage.d.
{ return _attributes; }
XMLNode xmlstorage::XMLNode::get_child_relative | ( | inout char[] | path, |
bool | create | ||
) | [inline, protected, inherited] |
Definition at line 928 of file xmlstorage.d.
References xmlstorage::XMLNode::find(), and xmlstorage::unescape().
Referenced by xmlstorage::XMLNode::create_relative(), and xmlstorage::XMLNode::find_relative().
{ int slash_pos = .find(path, '/'); if (slash_pos == 0) return null; size_t l = slash_pos!=-1? slash_pos: path.length; char[] comp = path[0 .. l]; path = path[l .. length]; // look for [n] and [@attr_name="attr_value"] expressions in path components int bracket_pos = .find(comp, '['); l = bracket_pos!=-1? bracket_pos: comp.length; char[] child_name = comp[0 .. l]; char[] attr_name, attr_value; int n = 0; if (bracket_pos != -1) { char[] expr = unescape(comp[bracket_pos..length], '[', ']'); n = atoi(expr); // read index number if (n) n = n - 1; // convert into zero based index int at_pos = .find(expr, '@'); if (at_pos != -1) { expr = expr[at_pos+1 .. length]; int equal_pos = .find(expr, '='); // read attribute name and value if (equal_pos != -1) { attr_name = unescape(expr[0 .. equal_pos]); attr_value = unescape(expr[equal_pos+1 .. length]); } } } XMLNode child; if (!attr_name.length) // search n.th child node with specified name child = find(child_name, n); else // search n.th child node with specified name and matching attribute value child = find(child_name, attr_name, attr_value, n); if (!child && create) { child = new XMLNode(child_name); add_child(child); if (attr_name.length) this[attr_name] = attr_value; } return child; }
Children xmlstorage::XMLNode::get_children | ( | ) | [inline, inherited] |
Definition at line 671 of file xmlstorage.d.
{ return _children; }
char [] xmlstorage::XMLNode::get_content | ( | ) | [inline, inherited] |
Definition at line 681 of file xmlstorage.d.
References xmlstorage::DecodeXMLString().
{ return DecodeXMLString(_content); }
XMLNode xmlstorage::XMLNode::get_first_child | ( | ) | [inline, protected, inherited] |
Definition at line 729 of file xmlstorage.d.
Referenced by xmlstorage::XMLPos::go_down().
char* [] xmlstorage::XMLNode::get_ref | ( | in char[] | attr_name, |
in char[] | def = "" |
||
) | [inline, inherited] |
write access to an attribute
Definition at line 622 of file xmlstorage.d.
Referenced by xmlstorage::XMLNode::subvalue_ref().
{ char[]* found = attr_name in _attributes; if (found) return found; else { _attributes[attr_name] = def; return &_attributes[attr_name]; } }
char [] xmlstorage::XMLNode::opIndex | ( | in char[] | attr_name ) | [inline, inherited] |
index operator read access to an attribute
Definition at line 599 of file xmlstorage.d.
{ return _attributes[attr_name]; }
void xmlstorage::XMLNode::opIndexAssign | ( | in char[] | attr_name, |
in char[] | value | ||
) | [inline, inherited] |
index operator write access to an attribute
Definition at line 605 of file xmlstorage.d.
{ _attributes[attr_name] = value; }
void xmlstorage::XMLNode::plain_write_worker | ( | Stream | o ) | [inline, protected, inherited] |
print node without any white space
Definition at line 814 of file xmlstorage.d.
References xmlstorage::EncodeXMLString().
{ o.writeString('<' ~ EncodeXMLString(_name)); foreach(key,value; _attributes) o.writeString(' ' ~ EncodeXMLString(key) ~ "=\"" ~ EncodeXMLString(value) ~ "\""); // strip leading white space from content char[] content = _content; while(content.length && isspace(cast(dchar)*content)) content = content[1 .. length]; if (!_children.empty() || content.length) { o.writeString('>' ~ content); foreach(child; _children) child.plain_write_worker(o); o.writeString("</" ~ EncodeXMLString(_name) ~ ">"); } else o.writeString("/>"); }
void xmlstorage::XMLNode::pretty_write_worker | ( | Stream | o, |
XMLFormat | format, | ||
int | indent | ||
) | [inline, protected, inherited] |
pretty print node with children tree to output stream
Definition at line 837 of file xmlstorage.d.
References xmlstorage::XMLFormat::_endl, xmlstorage::EncodeXMLString(), and xmlstorage::XML_INDENT_SPACE.
{ for(int i=indent; i--; ) o.writeString(XML_INDENT_SPACE); o.writeString('<' ~ EncodeXMLString(_name)); foreach(key,value; _attributes) o.writeString(' ' ~ EncodeXMLString(key) ~ "=\"" ~ EncodeXMLString(value) ~ "\""); // strip leading white space from content char[] content = _content; while(content.length && isspace(cast(dchar)*content)) content = content[1 .. length]; if (!_children.empty() || content.length) { o.writeString('>' ~ content); if (!_children.empty()) o.writeString(format._endl); foreach(child; _children) child.pretty_write_worker(o, format, indent+1); for(int i=indent; i--; ) o.writeString(XML_INDENT_SPACE); o.writeString("</" ~ EncodeXMLString(_name) ~ '>' ~ format._endl); } else o.writeString("/>" ~ format._endl); }
void xmlstorage::XMLNode::put | ( | in char[] | attr_name, |
in char[] | value | ||
) | [inline, inherited] |
write access to an attribute
Definition at line 593 of file xmlstorage.d.
Referenced by xmlstorage::XMLPos::smart_create().
{ _attributes[attr_name] = value; }
char [] xmlstorage::XMLNode::put_subvalue | ( | in char[] | name, |
char[] | attr_name, | ||
int | n = 0 |
||
) | [inline, inherited] |
convenient storage of distinct values in children node
Definition at line 659 of file xmlstorage.d.
References xmlstorage::XMLNode::get().
{ XMLNode node = find(name, n); if (!node) { node = new XMLNode(name); add_child(node); } return node.get(attr_name); }
bool xmlstorage::XMLDoc::read | ( | XMLReaderBase | reader, |
char[] | display_path | ||
) | [inline, inherited] |
Definition at line 2155 of file xmlstorage.d.
References xmlstorage::XMLFormat::_endl, xmlstorage::XMLReaderBase::clear_errors(), xmlstorage::XMLReaderBase::get_endl(), xmlstorage::XMLReaderBase::get_errors(), xmlstorage::XMLReaderBase::get_format(), xmlstorage::XMLReaderBase::read(), and xmlstorage::version().
{ version(XMLNODE_LOCATION) { // make a string copy to handle temporary string objects _display_path = display_path.dup; reader._display_path = _display_path; } reader.clear_errors(); reader.read(); _format = reader.get_format(); _format._endl = reader.get_endl(); if (!reader.get_errors().empty()) { _errors = reader.get_errors(); return false; } return true; }
bool xmlstorage::XMLDoc::read | ( | in Stream | i, |
in char[] | system_id = "" |
||
) | [inline, inherited] |
Definition at line 2148 of file xmlstorage.d.
{ XMLReader reader = new XMLReader(this, i); return read(reader, system_id); }
bool xmlstorage::XMLDoc::read_buffer | ( | in char[] | buffer, |
in char[] | system_id = "" |
||
) | [inline, inherited] |
Definition at line 2143 of file xmlstorage.d.
{ return read(new MemoryStream(buffer), system_id); }
bool xmlstorage::XMLDoc::read_file | ( | in char[] | path ) | [inline, inherited] |
Definition at line 2133 of file xmlstorage.d.
{ Stream i = new BufferedFile(path, FileMode.In); scope(exit) i.close(); XMLReader reader = new XMLReader(this, i); return read(reader, path); }
void xmlstorage::XMLNode::set_content | ( | in char[] | s, |
bool | cdata = false |
||
) | [inline, inherited] |
Definition at line 686 of file xmlstorage.d.
References xmlstorage::EncodeXMLString().
{ _content = EncodeXMLString(s, cdata); }
void xmlstorage::XMLNode::smart_write_worker | ( | Stream | o, |
XMLFormat | format, | ||
int | indent | ||
) | [inline, protected, inherited] |
write node with children tree to output stream using smart formating
Definition at line 869 of file xmlstorage.d.
References xmlstorage::XMLFormat::_endl, xmlstorage::EncodeXMLString(), and xmlstorage::XML_INDENT_SPACE.
{ // strip the first line feed from _leading char[] leading = _leading; while(leading.length && *leading=='\n') leading = leading[1 .. length]; if (!leading.length) for(int i=indent; i--; ) o.writeString(XML_INDENT_SPACE); else o.writeString(leading); o.writeString('<' ~ EncodeXMLString(_name)); foreach(key,value; _attributes) o.writeString(' ' ~ EncodeXMLString(key) ~ "=\"" ~ EncodeXMLString(value) ~ "\""); // strip leading white space from content char[] content = _content; while(content.length && isspace(cast(dchar)*content)) content = content[1 .. length]; if (_children.empty() && !content.length) o.writeString("/>"); else { o.writeString(">"); if (!content.length) o.writeString(format._endl); else o.writeString(content); if (!_children.empty()) { foreach(child; _children) child.smart_write_worker(o, format, indent+1); // strip the first line feed from _end_leading char[] end_leading = _end_leading; while(end_leading.length && *end_leading=='\n') end_leading = end_leading[1 .. length]; if (!end_leading.length) for(int i=indent; i--; ) o.writeString(XML_INDENT_SPACE); else o.writeString(end_leading); } else o.writeString(_end_leading); o.writeString("</" ~ EncodeXMLString(_name) ~ '>'); } if (!_trailing.length) o.writeString(format._endl); else o.writeString(_trailing); }
char [] xmlstorage::XMLNode::subvalue | ( | in char[] | name, |
char[] | attr_name, | ||
int | n = 0 |
||
) | [inline, inherited] |
convenient value access in children node
Definition at line 635 of file xmlstorage.d.
References xmlstorage::XMLNode::get().
{ XMLNode node = find(name, n); if (node) return node.get(attr_name); else return ""; }
char* [] xmlstorage::XMLNode::subvalue_ref | ( | in char[] | name, |
char[] | attr_name, | ||
int | n = 0 |
||
) | [inline, inherited] |
convenient value access in children node
Definition at line 646 of file xmlstorage.d.
References xmlstorage::XMLNode::get_ref().
{ XMLNode node = find(name, n); if (node) return node.get_ref(attr_name); else { _attributes[attr_name] = ""; return &_attributes[attr_name]; } }
char [] xmlstorage::XMLMessage::toString | ( | ) | [inline] |
Definition at line 2231 of file xmlstorage.d.
{ Stream m = new MemoryStream(); write(m); return m.toString(); }
xmlstorage::XMLDoc::version | ( | XMLNODE_LOCATION | ) | [inline, inherited] |
Reimplemented from xmlstorage::XMLNode.
Definition at line 2215 of file xmlstorage.d.
{
char[] _display_path;
}
void xmlstorage::XMLNode::write | ( | Stream | o, |
XMLFormat | format, | ||
WRITE_MODE | mode = WRITE_MODE.FORMAT_SMART , |
||
int | indent = 0 |
||
) | [inline, inherited] |
write node with children tree to output stream
Definition at line 696 of file xmlstorage.d.
{ switch(mode) { case WRITE_MODE.FORMAT_PLAIN: plain_write_worker(o); break; case WRITE_MODE.FORMAT_PRETTY: pretty_write_worker(o, format, indent); break; case WRITE_MODE.FORMAT_ORIGINAL: write_worker(o); break; default: // WRITE_MODE.FORMAT_SMART smart_write_worker(o, format, indent); } }
void xmlstorage::XMLDoc::write | ( | Stream | o, |
WRITE_MODE | mode = WRITE_MODE.FORMAT_SMART |
||
) | [inline, inherited] |
write XML stream preserving previous white space and comments
Definition at line 2178 of file xmlstorage.d.
void xmlstorage::XMLDoc::write_file | ( | in char[] | path, |
WRITE_MODE | mode = WRITE_MODE.FORMAT_SMART |
||
) | [inline, inherited] |
Definition at line 2196 of file xmlstorage.d.
{ Stream o = new BufferedFile(path, FileMode.Out); scope(exit) o.close(); write(o, mode); }
void xmlstorage::XMLDoc::write_formating | ( | in char[] | path ) | [inline, inherited] |
Definition at line 2204 of file xmlstorage.d.
{ Stream o = new BufferedFile(path, FileMode.Out); scope(exit) o.close(); write_formating(o); }
void xmlstorage::XMLDoc::write_formating | ( | Stream | o ) | [inline, inherited] |
write XML stream with formating
Definition at line 2191 of file xmlstorage.d.
{ write(o, WRITE_MODE.FORMAT_PRETTY); }
void xmlstorage::XMLNode::write_worker | ( | Stream | o ) | [inline, protected, inherited] |
write node with children tree to output stream using original white space
Definition at line 793 of file xmlstorage.d.
References xmlstorage::EncodeXMLString(), and xmlstorage::XMLNode::write_worker().
Referenced by xmlstorage::XMLNode::write_worker().
{ o.writeString(_leading ~ '<' ~ EncodeXMLString(_name)); foreach(key,value; _attributes) o.writeString(' ' ~ EncodeXMLString(key) ~ "=\"" ~ EncodeXMLString(value) ~ "\""); if (!_children.empty() || _content.length) { o.writeString('>' ~ _content); foreach(child; _children) child.write_worker(o); o.writeString(_end_leading ~ "</" ~ EncodeXMLString(_name) ~ '>'); } else o.writeString("/>"); o.writeString(_trailing); }
AttributeMap xmlstorage::XMLNode::_attributes [protected, inherited] |
Definition at line 718 of file xmlstorage.d.
Referenced by xmlstorage::XMLNode::assign(), and xmlstorage::XMLReaderBase::StartElementHandler().
Children xmlstorage::XMLNode::_children [protected, inherited] |
Definition at line 717 of file xmlstorage.d.
Referenced by xmlstorage::XMLNode::assign().
char [] xmlstorage::XMLNode::_content [protected, inherited] |
Definition at line 721 of file xmlstorage.d.
Referenced by xmlstorage::XMLNode::assign().
char [] xmlstorage::XMLNode::_end_leading [protected, inherited] |
Definition at line 722 of file xmlstorage.d.
Referenced by xmlstorage::XMLNode::assign().
XMLErrorList xmlstorage::XMLDoc::_errors [inherited] |
Definition at line 2213 of file xmlstorage.d.
XMLFormat xmlstorage::XMLDoc::_format [inherited] |
Definition at line 2212 of file xmlstorage.d.
char [] xmlstorage::XMLNode::_leading [protected, inherited] |
Definition at line 720 of file xmlstorage.d.
Referenced by xmlstorage::XMLNode::assign().
char [] xmlstorage::XMLNode::_name [inherited] |
Definition at line 503 of file xmlstorage.d.
Definition at line 2240 of file xmlstorage.d.
char [] xmlstorage::XMLNode::_trailing [protected, inherited] |
Definition at line 723 of file xmlstorage.d.
Referenced by xmlstorage::XMLNode::assign().