pike.git
/
lib
/
modules
/
Parser.pmod
/
XML.pmod
/
SloppyDOM.pmod
version
»
Context lines:
10
20
40
80
file
none
3
pike.git/lib/modules/Parser.pmod/XML.pmod/SloppyDOM.pmod:57:
string get_node_value() {return 0;} void set_node_value (string value); string get_node_name(); int get_node_type(); Node get_parent_node() {return parent_node;} //NodeList get_child_nodes(); Node get_first_child() {return 0;} Node get_last_child() {return 0;} Node get_previous_sibling()
-
{return
parent_node && parent_node->_get_child_by_pos (pos_in_parent - 1);}
+
{
+
return
parent_node && parent_node->_get_child_by_pos (pos_in_parent - 1);
+
}
Node get_next_sibling()
-
{return
parent_node && parent_node->_get_child_by_pos (pos_in_parent + 1);}
+
{
+
return
parent_node && parent_node->_get_child_by_pos (pos_in_parent + 1);
+
}
//NamedNodeMap get_attributes() {return 0;} Document get_owner_document() {return owner_document;} Node insert_before(Node new_child, Node ref_child); Node replace_child(Node new_child, Node old_child); Node remove_child(Node old_child); Node append_child(Node new_child); int has_child_nodes() {return 0;} Node clone_node (int|void deep);
pike.git/lib/modules/Parser.pmod/XML.pmod/SloppyDOM.pmod:176:
//! @note //! Not DOM compliant. { String.Buffer res = String.Buffer(); _xml_format (res); return res->get(); } // Internals.
-
static
constant class_name = "Node";
+
protected
constant class_name = "Node";
/*protected*/ int pos_in_parent; /*protected*/ Document _get_doc() {return owner_document;} /*protected*/ void _text_content (String.Buffer into); /*protected*/ void _xml_format (String.Buffer into);
-
/*protected*/ void _destruct_tree() {destruct (this
_object(
)
)
;}
+
/*protected*/ void _destruct_tree() {destruct (this);}
#define WS "%*[ \t\n\r]" #define NAME "%[^][ \t\n\r/@(){},=.]" void simple_path_error (string msg, mixed... args) { if (sizeof (args)) msg = sprintf (msg, @args); msg += sprintf ("%s node%s.\n", class_name,
-
this
_object()
->node_name ?
-
" " + this
_object()
->node_name : "");
+
this->node_name ? " " + this->node_name : "");
error (msg); };
-
static
array(string) parse_trivial_node_type_path (string path)
+
protected
array(string) parse_trivial_node_type_path (string path)
{ string orig_path = path; while (sscanf (path, WS"."WS"%s", string rest) == 3) { if (has_prefix (rest, "//")) path = rest[2..]; else if (has_prefix (rest, "/")) path = rest[1..]; else break; } if (sscanf (path, WS""NAME""WS"("WS"%s", string node_type, string rest) == 5) { string arg;
pike.git/lib/modules/Parser.pmod/XML.pmod/SloppyDOM.pmod:227:
simple_path_error ("Invalid node type %O in %O in ", node_type, orig_path); if (arg && node_type != "processing-instruction") simple_path_error ("Cannot give an argument to the node type %s " "in %O in ", node_type, orig_path); return ({node_type, arg}); } return ({0, 0}); }
-
static
string sprintf_name (int flag) {return "";}
-
static
string sprintf_attr (int flag) {return "";}
-
static
string sprintf_content (int flag) {return "";}
+
protected
string sprintf_name (int flag) {return "";}
+
protected
string sprintf_attr (int flag) {return "";}
+
protected
string sprintf_content (int flag) {return "";}
string _sprintf (int flag) { switch (flag) { case 'O': return "SloppyDOM." + class_name + "(" + sprintf_name ('O') + ")"; case 'V': string res = sprintf_name ('V') + sprintf_attr ('V'); string c = sprintf_content ('V'); if (sizeof (c))
pike.git/lib/modules/Parser.pmod/XML.pmod/SloppyDOM.pmod:251:
res += (sizeof (res) ? ":" : "") + "\n " + replace (c, "\n", "\n "); else res += (sizeof (res) ? ": " : "") + c; return "SloppyDOM." + class_name + "(" + res + ")"; } } } #define CHECK_CONTENT \ if (stringp (content)) \
-
content = sloppy_parse_fragment (content, this
_object(
)
)
;
+
content = sloppy_parse_fragment (content, this);
#define NODE_AT(POS) (stringp (content[POS]) ? make_node (POS) : content[POS])
-
static
class NodeWithChildren
+
protected
class NodeWithChildren
{ inherit Node; Node get_first_child() { CHECK_CONTENT; if (content && sizeof (content)) return NODE_AT (0); return 0; }
pike.git/lib/modules/Parser.pmod/XML.pmod/SloppyDOM.pmod:276:
{ CHECK_CONTENT; if (content && sizeof (content)) return NODE_AT (-1); return 0; } int has_child_nodes() {return content && sizeof (content);} // Internals.
-
static
constant class_name = "NodeWithChildren";
+
protected
constant class_name = "NodeWithChildren";
/*protected*/ string|array(string|Node) content; /*protected*/ Node _get_child_by_pos (int pos) { if (pos < 0) return 0; CHECK_CONTENT; if (content && pos < sizeof (content)) return NODE_AT (pos); return 0; } /*protected*/ void _destruct_tree() { if (arrayp (content)) foreach (content, string|Node child) if (objectp (child)) child->_destruct_tree();
-
destruct (this
_object(
)
)
;
+
destruct (this);
} /*protected*/ Node make_node (int pos) { Document doc = _get_doc(); string text = content[pos]; Node node; if (has_prefix (text, "&")) { text = text[1..sizeof (text) - 2]; if (string decoded = !doc->raw_values &&
pike.git/lib/modules/Parser.pmod/XML.pmod/SloppyDOM.pmod:321:
else if (has_prefix (text, "<?")) { sscanf (text[..sizeof (text) - 3], "<?%[^ \t\n\r]%*[ \t\n\r]%s", string target, string data); node = ProcessingInstruction (doc, target, data || ""); } else if (has_prefix (text, "<![CDATA[")) node = CDATASection (doc, text[9..sizeof (text) - 4]); else node = Text (doc, text); content[pos] = node;
-
node->parent_node = this
_object()
;
+
node->parent_node = this;
node->pos_in_parent = pos; return node; } /*protected*/ void make_all_nodes() { CHECK_CONTENT; if (arrayp (content)) for (int i = sizeof (content) - 1; i >= 0; i--) if (stringp (content[i])) make_node (i); }
-
static
void format_attrs (mapping(string:string) attrs, String.Buffer into)
+
protected
void format_attrs (mapping(string:string) attrs, String.Buffer into)
{ if (owner_document->raw_values) foreach (indices (attrs), string attr) { string var = attrs[attr]; if (has_value (var, "\"")) into->add (" ", attr, "='", var, "'"); else into->add (" ", attr, "=\"", var, "\""); } else
pike.git/lib/modules/Parser.pmod/XML.pmod/SloppyDOM.pmod:378:
child->_text_content (into); } else if (has_prefix (child, "&") || has_prefix (child, "<![CDATA[")) make_node (i)->_text_content (into); else if (!has_prefix (child, "<!--") && !has_prefix (child, "<?")) into->add (child); } }
-
static
void xml_format_children (String.Buffer into)
+
protected
void xml_format_children (String.Buffer into)
{ if (stringp (content)) into->add (content); if (arrayp (content)) foreach (content, string|Node child) if (stringp (child)) into->add (child); else child->_xml_format (into); }
-
static
string sprintf_content (int flag)
+
protected
string sprintf_content (int flag)
{ if (stringp (content)) return sprintf ("%O", content); if (arrayp (content)) return map (content, lambda (string|Node child) { if (stringp (child)) return sprintf ("%O", child); return child->_sprintf (flag); }) * ",\n"; return ""; } } #define CHECK_LOOKUP_MAPPING if (!id_prefix) fix_lookup_mapping();
-
static
int last_used_id = 0;
+
protected
int last_used_id = 0;
-
static
class NodeWithChildElements
+
protected
class NodeWithChildElements
//! Node with child elements. { inherit NodeWithChildren; string get_attribute (string name); //NodeList get_elements_by_tag_name (string tag_name); array(Element) get_elements (string name) //! Lightweight variant of @[get_elements_by_tag_name] that returns
pike.git/lib/modules/Parser.pmod/XML.pmod/SloppyDOM.pmod:499:
"th"); }; mapping(string:string)|Node|array(Node|int) res; if (name == "") { if (sscanf (path, "@"WS NAME WS"%s", name, path)) { if (!sizeof (name)) simple_path_error ("No attribute name after @ in ");
-
foreach (rec_search ? (
{this_object(
)
})
+ get_descendant_elements() :
-
(
{this_object(
)
})
, NodeWithChildElements node) {
+
foreach (rec_search ? (
{this}
) + get_descendant_elements() :
+
(
{this}
), NodeWithChildElements node) {
mapping(string:string) attr = node->attributes; if (!mappingp (attr)) simple_path_error ("Cannot access an attribute %O in ", name); if (name == "*") { if (res) res += attr; else res = attr + ([]); } else if (string val = attr[name]) { if (res) res[name] = val; else res = ([name: val]);
pike.git/lib/modules/Parser.pmod/XML.pmod/SloppyDOM.pmod:905:
res->_xml_format (collected); string formatted = collected->get(); //werror ("%O: path %O, leaf result %O\n", this, orig_path, formatted); return formatted; } //werror ("%O: path %O, leaf result %O\n", this, orig_path, res); return res; }
-
static
constant class_name = "NodeWithChildElements";
+
protected
constant class_name = "NodeWithChildElements";
-
static
string id_prefix;
+
protected
string id_prefix;
-
static
void fix_lookup_mapping()
+
protected
void fix_lookup_mapping()
{ id_prefix = (string) ++last_used_id + ":"; CHECK_CONTENT; if (content) { mapping lm = _get_doc()->_lookup_mapping; foreach (content, string|Node child) if (objectp (child) && child->node_type == ELEMENT_NODE) lm[id_prefix + child->node_name] += ({child}); } }
pike.git/lib/modules/Parser.pmod/XML.pmod/SloppyDOM.pmod:954:
foreach (content, string|Node node) if (objectp (node) && node->node_type == ELEMENT_NODE) return document_element = node; } return document_element; } #if 0 // Disabled for now since the tree can't be manipulated anyway. Element create_element (string tag_name)
-
{return
Element (this
_object()
, tag_name);}
+
{
+
return
Element (this, tag_name);
+
}
//DocumentFragment create_document_fragment(); Text create_text_node (string data)
-
{return
Text (this
_object()
, data);}
+
{
+
return
Text (this, data);
+
}
Comment create_comment (string data)
-
{return
Comment (this
_object()
, data);}
+
{
+
return
Comment (this, data);
+
}
CDATASection create_cdata_section (string data)
-
{return
CDATASection (this
_object()
, data);}
+
{
+
return
CDATASection (this, data);
+
}
ProcessingInstruction create_processing_instruction (string target, string data)
-
{return
ProcessingInstruction (this
_object()
, target, data);}
+
{
+
return
ProcessingInstruction (this, target, data);
+
}
//Attr create_attribute (string name, string|void default_value); EntityReference create_entity_reference (string name)
-
{return
EntityReference (this
_object()
, name);}
+
{
+
return
EntityReference (this, name);
+
}
#endif //NodeList get_elements_by_tag_name (string tagname); array(Element) get_elements (string name) //! Note that this one looks among the top level elements, as //! opposed to @[get_elements_by_tag_name]. This means that if the //! document is correct, you can only look up the single top level //! element here. //!
pike.git/lib/modules/Parser.pmod/XML.pmod/SloppyDOM.pmod:998:
else { CHECK_LOOKUP_MAPPING; return _lookup_mapping[id_prefix + name] || ({}); } } int get_raw_values() {return raw_values;} //! @note //! Not DOM compliant.
-
static
void create (void|string|array(string|Node) c, void|int raw_vals)
+
protected
void create (void|string|array(string|Node) c, void|int raw_vals)
{ content = c; raw_values = raw_vals; } // Internals.
-
static
constant class_name = "Document";
+
protected
constant class_name = "Document";
/*protected*/ int raw_values;
-
static
Element document_element = 0;
+
protected
Element document_element = 0;
/*protected*/ mapping(string:array(Node)) _lookup_mapping = ([]);
-
/*protected*/ Document _get_doc()
{return
this
_object()
;}
+
/*protected*/ Document _get_doc()
{
return
this;
}
/*protected*/ void _xml_format (String.Buffer into) {xml_format_children (into);}
-
static
void
destroy
()
+
protected
void
_destruct
()
{ if (arrayp (content)) foreach (content, string|Node child) if (objectp (child)) child->_destruct_tree(); } } //class Attr {} class Element
pike.git/lib/modules/Parser.pmod/XML.pmod/SloppyDOM.pmod:1041:
mapping(string:string) attributes; constant node_type = ELEMENT_NODE; int get_node_type() { return ELEMENT_NODE; } string get_node_name() { return node_name; } string get_tag_name() { return node_name; } //NamedNodeMap get_attributes(); string get_attribute (string name)
-
{return
attributes[name] || "";}
+
{
+
return
attributes[name] || "";
+
}
void set_attribute (string name, string value)
-
{attributes
[name] = value;}
+
{
+
attributes
[name] = value;
+
}
void remove_attribute (string name)
-
{m
_delete (attributes, name);}
+
{
+
m
_delete (attributes, name);
+
}
//Attr get_attribute_node (string name); //Attr set_attribute_node (Attr new_attr); //Attr remove_attribute_node (Attr old_attr); //void normalize();
-
static
void create (Document owner, string name, void|mapping(string:string) attr)
+
protected
void create (Document owner, string name, void|mapping(string:string) attr)
{ owner_document = owner; node_name = name; attributes = attr || ([]); } // Internals.
-
static
constant class_name = "Element";
+
protected
constant class_name = "Element";
/*protected*/ void _xml_format (String.Buffer into) { into->add ("<", node_name); format_attrs (attributes, into); if (content && sizeof (content)) { into->add (">"); xml_format_children (into); into->add ("</", node_name, ">"); } else into->add (" />"); }
-
static
string sprintf_name() {return node_name;}
+
protected
string sprintf_name() {return node_name;}
-
static
string sprintf_attr()
+
protected
string sprintf_attr()
{ if (sizeof (attributes)) return "(" + map ((array) attributes, lambda (array pair) { return sprintf ("%s=%O", pair[0], pair[1]); }) * ", " + ")"; else return ""; } }
pike.git/lib/modules/Parser.pmod/XML.pmod/SloppyDOM.pmod:1104:
string node_value; string get_node_value() { return node_value; } void set_node_value(string data) {node_value = data;} string get_data() {return node_value;} void set_data (string data) {node_value = data;} int get_length() {return sizeof (node_value);} string substring_data (int offset, int count)
-
{return
node_value[offset..offset + count - 1];}
+
{
+
return
node_value[offset..offset + count - 1];
+
}
void append_data (string arg)
-
{node
_value += arg;}
+
{
+
node
_value += arg;
+
}
void insert_data (int offset, string arg)
-
{node
_value = node_value[..offset - 1] + arg + node_value[offset..];}
+
{
+
node
_value = node_value[..offset - 1] + arg + node_value[offset..];
+
}
void delete_data (int offset, int count)
-
{node
_value = node_value[..offset - 1] + node_value[offset + count..];}
+
{
+
node
_value = node_value[..offset - 1] + node_value[offset + count..];
+
}
void replace_data (int offset, int count, string arg)
-
{node
_value = node_value[..offset - 1] + arg + node_value[offset + count..];}
+
{
+
node
_value = node_value[..offset - 1] + arg + node_value[offset + count..];
+
}
mapping(string:string)|Node|array(mapping(string:string)|Node)|string simple_path (string path, void|int xml_format) { [string node_type, string arg] = parse_trivial_node_type_path (path); if (node_type == "text") return node_value; return xml_format && ""; } // Internals.
-
static
constant class_name = "CharacterData";
+
protected
constant class_name = "CharacterData";
/*protected*/ void _text_content (String.Buffer into) { if (owner_document->raw_values) { // Serial replace's are currently faster than one parallell. into->add (replace (replace (replace (node_value, "&", "&"), "<", "<"), ">", ">")); } else into->add (node_value); }
-
static
string sprintf_content (int flag) {return sprintf ("%O", node_value);}
+
protected
string sprintf_content (int flag) {return sprintf ("%O", node_value);}
} class Text { inherit CharacterData; constant node_type = TEXT_NODE; int get_node_type() { return TEXT_NODE; } string get_node_name() { return "#text"; } //Text split_text (int offset);
-
static
void create (Document owner, string data)
+
protected
void create (Document owner, string data)
{ owner_document = owner; node_value = data; } // Internals.
-
static
constant class_name = "Text";
+
protected
constant class_name = "Text";
/*protected*/ void _xml_format (String.Buffer into) { // Serial replace's are currently faster than one parallell. into->add (replace (replace (replace (node_value, "&", "&"), "<", "<"), ">", ">")); } }
pike.git/lib/modules/Parser.pmod/XML.pmod/SloppyDOM.pmod:1188:
string get_node_name() { return "#comment"; } mapping(string:string)|Node|array(mapping(string:string)|Node)|string simple_path (string path, void|int xml_format) { [string node_type, string arg] = parse_trivial_node_type_path (path); if (node_type == "comment") return node_value; return xml_format && ""; }
-
static
void create (Document owner, string data)
+
protected
void create (Document owner, string data)
{ owner_document = owner; node_value = data; } // Internals.
-
static
constant class_name = "Comment";
+
protected
constant class_name = "Comment";
/*protected*/ void _xml_format (String.Buffer into) { into->add ("<!--", node_value, "-->"); } } class CDATASection { inherit Text; constant node_type = CDATA_SECTION_NODE; int get_node_type() { return CDATA_SECTION_NODE; } string get_node_name() { return "#cdata-section"; }
-
static
void create (Document owner, string data)
+
protected
void create (Document owner, string data)
{ owner_document = owner; node_value = data; } // Internals.
-
static
constant class_name = "CDATASection";
+
protected
constant class_name = "CDATASection";
/*protected*/ void _text_content (String.Buffer into) { if (owner_document->raw_values) into->add ("<![CDATA[", node_value, "]]>"); else into->add (node_value); } /*protected*/ void _xml_format (String.Buffer into)
pike.git/lib/modules/Parser.pmod/XML.pmod/SloppyDOM.pmod:1264:
//Node get_next_sibling(); mapping(string:string)|Node|array(mapping(string:string)|Node)|string simple_path (string path, void|int xml_format) { [string node_type, string arg] = parse_trivial_node_type_path (path); if (node_type == "text") return "&" + node_name + ";"; return xml_format && ""; }
-
static
void create (Document owner, string name)
+
protected
void create (Document owner, string name)
{ owner_document = owner; node_name = name; } // Internals.
-
static
constant class_name = "EntityReference";
+
protected
constant class_name = "EntityReference";
/*protected*/ void _text_content (String.Buffer into) { if (owner_document->raw_values) into->add ("&", node_name, ";"); else if (string decoded = Parser.html_entities[node_name] || Parser.decode_numeric_xml_entity (node_name)) into->add (decoded); else error ("Cannot decode entity reference %O.\n", node_name); }
-
static
string sprintf_name() {return node_name;}
+
protected
string sprintf_name() {return node_name;}
/*protected*/ void _xml_format (String.Buffer into) { into->add ("&", node_name, ";"); } } class ProcessingInstruction { inherit Node;
pike.git/lib/modules/Parser.pmod/XML.pmod/SloppyDOM.pmod:1320:
mapping(string:string)|Node|array(mapping(string:string)|Node)|string simple_path (string path, void|int xml_format) { [string node_type, string arg] = parse_trivial_node_type_path (path); if (node_type == "processing-instruction") if (!arg) return node_value; else if (arg == node_name) return node_value; return xml_format && ""; }
-
static
void create (Document owner, string t, string data)
+
protected
void create (Document owner, string t, string data)
{ owner_document = owner; node_name = t; node_value = data; } // Internals.
-
static
constant class_name = "ProcessingInstruction";
+
protected
constant class_name = "ProcessingInstruction";
/*protected*/ void _text_content (String.Buffer into) { if (owner_document->raw_values) { // Serial replace's are currently faster than one parallell. into->add (replace (replace (replace (node_value, "&", "&"), "<", "<"), ">", ">")); }
pike.git/lib/modules/Parser.pmod/XML.pmod/SloppyDOM.pmod:1352:
} /*protected*/ void _xml_format (String.Buffer into) { if (sizeof (node_value)) into->add ("<?", node_name, " ", node_value, "?>"); else into->add ("<?", node_name, "?>"); }
-
static
string sprintf_name() {return node_name;}
-
static
string sprintf_content (int flag) {return sprintf ("%O", node_value);}
+
protected
string sprintf_name() {return node_name;}
+
protected
string sprintf_content (int flag) {return sprintf ("%O", node_value);}
} // Internals.
-
static
int(0..0) return_zero() {return 0;}
+
protected
int(0..0) return_zero() {return 0;}
-
static
array sloppy_parser_container_callback (
+
protected
array sloppy_parser_container_callback (
Parser.HTML p, mapping(string:string) args, string content, Node cur) { if (Parser.HTML ent_p = p->entity_parser) foreach (indices (args), string arg) args[arg] = ent_p->finish (args[arg])->read(); Element element = Element (cur->_get_doc(), p->tag_name(), args); element->parent_node = cur; element->content = content; return ({element}); }
-
static
array|int sloppy_parser_tag_callback (Parser.HTML p, string text, Node cur)
+
protected
array|int sloppy_parser_tag_callback (Parser.HTML p, string text, Node cur)
{ if (text[-2] != '/') { sscanf (text, "<%[^ \t\n\r>]", text); p->add_container (text, sloppy_parser_container_callback); return 1; } mapping(string:string) args = p->tag_args(); if (Parser.HTML ent_p = p->entity_parser) foreach (indices (args), string arg) args[arg] = ent_p->finish (args[arg])->read(); Element element = Element (cur->_get_doc(), p->tag_name(), args); element->parent_node = cur; element->content = ""; return ({element}); }
-
static
array sloppy_parser_entity_callback (Parser.HTML p, string text, Node cur)
+
protected
array sloppy_parser_entity_callback (Parser.HTML p, string text, Node cur)
{ text = p->tag_name(); if (string chr = Parser.decode_numeric_xml_entity (text)) return ({chr}); EntityReference ent = EntityReference (cur->_get_doc(), text); ent->parent_node = cur; return ({ent}); }
-
static
class SloppyParser
+
protected
class SloppyParser
{ inherit Parser.HTML; Parser.HTML entity_parser; }
-
static
SloppyParser sloppy_parser_template =
+
protected
SloppyParser sloppy_parser_template =
lambda() { SloppyParser p = SloppyParser(); p->lazy_entity_end (1); p->match_tag (0); p->xml_tag_syntax (3); p->mixed_mode (1); p->add_quote_tag ("!--", return_zero, "--"); p->add_quote_tag ("![CDATA[", return_zero, "]]"); p->add_quote_tag ("?", return_zero, "?"); p->_set_tag_callback (sloppy_parser_tag_callback); p->_set_entity_callback ((mixed) return_zero); // Cast due to type inference bug. return p; }();
-
static
array(string|Node) sloppy_parse_fragment (string frag, Node cur)
+
protected
array(string|Node) sloppy_parse_fragment (string frag, Node cur)
{ Parser.HTML p = sloppy_parser_template->clone(); if (!cur->_get_doc()->raw_values) p->entity_parser = Parser.html_entity_parser(); p->set_extra (cur); array(string|Node) res = p->finish (frag)->read(); for (int i = sizeof (res) - 1; i >= 0; i--) if (objectp (res[i])) res[i]->pos_in_parent = i; return res; }