1
2 /* Web Polygraph http://www.web-polygraph.org/
3 * Copyright 2003-2011 The Measurement Factory
4 * Licensed under the Apache License, Version 2.0 */
5
6 #include "base/polygraph.h"
7
8 #include "xml/XmlNode.h"
9 #include "xml/XmlAttr.h"
10 #include "xml/XmlAttrs.h"
11 #include "xml/XmlNodes.h"
12
13
XmlNodes(XmlNode * aParent)14 XmlNodes::XmlNodes(XmlNode *aParent): theParent(aParent) {
15 }
16
XmlNodes(const XmlNodes & nodes)17 XmlNodes::XmlNodes(const XmlNodes &nodes): Array<XmlNode*>(nodes.count()),
18 theParent(0) {
19 for (int i = 0; i < nodes.count(); ++i)
20 *this << *nodes[i];
21 }
22
XmlNodes(const XmlNode & node)23 XmlNodes::XmlNodes(const XmlNode &node): theParent(0) {
24 *this << node;
25 }
26
~XmlNodes()27 XmlNodes::~XmlNodes() {
28 while (count()) {
29 last()->parent(0);
30 delete pop();
31 }
32 }
33
selByTagName(const String & name,Res & res) const34 int XmlNodes::selByTagName(const String &name, Res &res) const {
35 for (int i = 0; i < count(); ++i) {
36 const XmlNode *node = item(i);
37 if (node->name() == name)
38 res.append(node);
39 }
40 return res.count();
41 }
42
selByAttrName(const String & name,Res & res) const43 int XmlNodes::selByAttrName(const String &name, Res &res) const {
44 for (int i = 0; i < count(); ++i) {
45 const XmlNode *node = item(i);
46 if (node->attrs() && node->attrs()->has(name))
47 res.append(node);
48 }
49 return res.count();
50 }
51
selByAttrVal(const String & name,const String & value,Res & res) const52 int XmlNodes::selByAttrVal(const String &name, const String &value, Res &res) const {
53 for (int i = 0; i < count(); ++i) {
54 const XmlNode *node = item(i);
55 if (node->attrs() && node->attrs()->has(name, value))
56 res.append(node);
57 }
58 return res.count();
59 }
60
findByAttrVal(const String & name,const String & value)61 XmlNode *XmlNodes::findByAttrVal(const String &name, const String &value) {
62 for (int i = 0; i < count(); ++i) {
63 XmlNode *const node = item(i);
64 if (node->attrs() && node->attrs()->has(name, value))
65 return node;
66 }
67 return 0;
68 }
69
operator <<(const XmlAttr & a)70 XmlNodes &XmlNodes::operator <<(const XmlAttr &a) {
71 for (int i = 0; i < count(); ++i)
72 item(i)->addAttr(a.clone());
73 return *this;
74 }
75
operator <<(const XmlNode & n)76 XmlNodes &XmlNodes::operator <<(const XmlNode &n) {
77 append(n.clone());
78 last()->parent(theParent);
79 return *this;
80 }
81
operator <<(const XmlNodes & ns)82 XmlNodes &XmlNodes::operator <<(const XmlNodes &ns) {
83 stretch(count() + ns.count());
84 for (int i = 0; i < ns.count(); ++i)
85 *this << *ns[i];
86 return *this;
87 }
88
print(ostream & os,const String & pfx) const89 ostream &XmlNodes::print(ostream &os, const String &pfx) const {
90 for (int i = 0; i < count(); ++i)
91 item(i)->print(os, pfx);
92 return os;
93 }
94