ScummVM API documentation
xml_node.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef SHARED_CONF_XML_NODE_H
23 #define SHARED_CONF_XML_NODE_H
24 
25 #include "common/array.h"
26 #include "common/hash-str.h"
27 #include "common/path.h"
28 #include "common/str.h"
29 #include "common/util.h"
30 
31 namespace Ultima {
32 namespace Shared {
33 
34 class XMLTree;
35 
36 class XMLNode {
37 private:
38  XMLTree *_tree;
39  XMLNode *_parent;
40  Common::String _id;
41  Common::String _text;
42  Common::Array<XMLNode *> _nodeList;
43  Common::StringMap _attributes;
44  bool _noClose;
45  Common::String _emptyString;
46 private:
47  static void parseDocTypeElement(const Common::String &s, size_t &nn);
48 
49  void parseNodeText(const Common::String &nodeText);
50 
54  static void trim(Common::String &s);
55 
59  static XMLNode *xmlParseFile(XMLTree *tree, const Common::Path &fname);
60 
61  static Common::String closeTag(const Common::String &s);
62  static Common::String encodeEntity(const Common::String &s);
63 public:
64  XMLNode(XMLTree *tree, XMLNode *parent = nullptr) : _tree(tree), _parent(parent), _noClose(false) {}
65  XMLNode(const XMLNode &n) : _tree(n._tree), _parent(n._parent), _id(n._id),
66  _text(n._text), _nodeList(n._nodeList), _noClose(false) {}
67  ~XMLNode();
68 
69  XMLNode &operator=(const XMLNode &n) {
70  _id = n._id;
71  _text = n._text;
72  _nodeList = n._nodeList;
73  _noClose = n._noClose;
74  return *this;
75  }
76 
77  const Common::String &reference(const Common::String &, bool &);
78  const XMLNode *subtree(const Common::String &) const;
79 
80  const Common::String &id() const {
81  return _id;
82  }
83  const Common::String &text(void) const {
84  return _text;
85  }
86  bool nodeIsText() const {
87  return !_text.empty();
88  }
89  XMLNode *getParent() const {
90  return _parent;
91  }
92  bool hasChildren() const {
93  return !_nodeList.empty();
94  }
95  XMLNode *firstChild() const {
96  return _nodeList.empty() ? nullptr : _nodeList[0];
97  }
98  const Common::StringMap &attributes() const {
99  return _attributes;
100  }
101  bool hasProperty(const Common::String &attrName) const {
102  return _attributes.contains(attrName);
103  }
104  Common::String getProperty(const Common::String &attrName) const {
105  return _attributes.contains(attrName) ? _attributes[attrName] : "";
106  }
107  int getPropertyInt(const Common::String &attrName) const {
108  return _attributes.contains(attrName) ? atol(_attributes[attrName].c_str()) : 0;
109  }
110  bool getPropertyBool(const Common::String &attrName) const {
111  if (_attributes.contains(attrName)) {
112  Common::String str = _attributes[attrName];
113  return toupper(str[0]) == 'T' || str == "1";
114  } else {
115  return false;
116  }
117  }
118  Common::String operator[](const Common::String &attrName) const {
119  return getProperty(attrName);
120  }
121 
122  const Common::Array<XMLNode *> &children() const {
123  return _nodeList;
124  }
125 
128 
134  bool searchPairs(KeyTypeList &ktl, const Common::String &basekey,
135  const Common::String currkey, const unsigned int pos);
136  void selectPairs(KeyTypeList &ktl, const Common::String currkey);
137 
138  Common::String dump(int depth = 0);
139 
140  void xmlAssign(const Common::String &key, const Common::String &value);
141  static XMLNode *xmlParseDoc(XMLTree *tree, const Common::String &s);
142  static XMLNode *xmlParse(XMLTree *tree, const Common::String &s, size_t &pos);
143 
144  void listKeys(const Common::String &, Common::Array<Common::String> &,
145  bool longformat = true) const;
146 
150  void freeDoc();
151 
155  XMLNode *getPrior() const;
156 
160  XMLNode *getNext() const;
161 };
162 
163 } // End of namespace Shared
164 } // End of namespace Ultima
165 
166 #endif
Definition: str.h:59
Definition: xml_tree.h:36
Definition: array.h:52
Definition: path.h:52
Definition: xml_node.h:36
XMLNode * getPrior() const
bool searchPairs(KeyTypeList &ktl, const Common::String &basekey, const Common::String currkey, const unsigned int pos)
Definition: detection.h:27
bool empty() const
Definition: array.h:351
XMLNode * getNext() const
bool contains(const Key &key) const
Definition: hashmap.h:592