cxmlelement.h Source File

API: cxmlelement.h Source File
API
cxmlelement.h
1//==========================================================================
2// CXMLELEMENT.H - part of
3// OMNeT++/OMNEST
4// Discrete System Simulation in C++
5//
6//==========================================================================
7
8/*--------------------------------------------------------------*
9 Copyright (C) 2002-2017 Andras Varga
10 Copyright (C) 2006-2017 OpenSim Ltd.
11
12 This file is distributed WITHOUT ANY WARRANTY. See the file
13 `license' for details on this and other legal matters.
14*--------------------------------------------------------------*/
15
16#ifndef __OMNETPP_CXMLELEMENT_H
17#define __OMNETPP_CXMLELEMENT_H
18
19#include <string>
20#include <map>
21#include <vector>
22#include "simkerneldefs.h"
23#include "cenvir.h"
24#include "fileline.h"
25#include "opp_pooledstring.h"
26
27namespace omnetpp {
28
29class cXMLElement;
30class cModule;
31
35typedef std::vector<cXMLElement*> cXMLElementList;
36
40typedef std::map<std::string,std::string> cXMLAttributeMap;
41
42
75class SIM_API cXMLElement : public cOwnedObject
76{
77 friend class cXMLElementDescriptor; // getAttr(i), getChild(i), etc.
78
79 public:
84 class SIM_API ParamResolver
85 {
86 public:
92 virtual bool resolve(const char *paramname, std::string& value) = 0;
93 virtual ~ParamResolver() {}
94 };
95
96 private:
97 opp_pooledstring value;
98 typedef std::pair<opp_pooledstring,opp_pooledstring> Attr;
99 std::vector<Attr> attrs;
100 cXMLElement *parent = nullptr;
101 cXMLElement *firstChild = nullptr;
102 cXMLElement *lastChild = nullptr;
103 cXMLElement *prevSibling = nullptr;
104 cXMLElement *nextSibling = nullptr;
105 FileLine loc;
106
107 private:
108 void doGetElementsByTagName(const char *tagname, cXMLElementList& list) const;
109
110 public:
111 // internal: constructor
112 cXMLElement(const char *tagname, cXMLElement *parent=nullptr);
113
114 // internal: constructor - for backward compatibility
115 cXMLElement(const char *tagname, const char *ignored, cXMLElement *parent) : cXMLElement(tagname, parent) {}
116
117 // internal: copy constructor
118 cXMLElement(const cXMLElement& other);
119
120 // internal: Destructor. Destroys children too.
121 virtual ~cXMLElement();
122
123 // internal: creates and returns an exact copy of this object. Child nodes are not duplicated.
124 virtual cXMLElement *dup() const override {return new cXMLElement(*this);}
125
126 virtual cXMLElement *dupTree() const;
127
128 virtual void setTagName(const char *tagName) {setName(tagName);}
129
130 // internal: sets source location
131 virtual void setSourceLocation(const char *fname, int line);
132
133 // internal: sets text node within element
134 virtual void setNodeValue(const char *s);
135
136 // internal: sets text node within element
137 virtual void setNodeValue(const char *s, int len);
138
139 // internal: appends to text node within element
140 virtual void appendNodeValue(const char *s, int len);
141
142 // internal: Sets the value of the attribute with the given name.
143 virtual void setAttribute(const char *attr, const char *value);
144
145 // internal: Set attributes of the element: name,value,name,value,...,nullptr
146 virtual void setAttributes(const char **attrs);
147
148 // internal: Appends the given element at the end of the child element list.
149 virtual void appendChild(cXMLElement *node);
150
151 // internal: Inserts the given element just before the specified child element
152 // in the child element list. The where element must be a child of this element.
153 virtual void insertChildBefore(cXMLElement *where, cXMLElement *newnode);
154
155 // internal: Removes the given element from the child element list.
156 // The pointer passed should be a child of this element.
157 virtual cXMLElement *removeChild(cXMLElement *node);
158
159 // internal: matches from root element
160 static cXMLElement *getDocumentElementByPath(cXMLElement *documentnode, const char *pathexpr, ParamResolver *resolver=nullptr);
161
162 private:
163 // internal
164 Attr *findAttr(const char *name) const;
165 void addAttr(const char *name, const char *value);
166 virtual void print(std::ostream& os, int indentLevel) const;
167
168 // internal, for inspectors only; O(n) complexity!
169 int getNumAttrs() const;
170 const char *getAttrName(int index) const;
171 const char *getAttrValue(int index) const;
172 std::string getAttrDesc(int index) const;
173 int getNumChildren() const;
174 cXMLElement *getChild(int index) const;
175
176 public:
179
182 virtual std::string str() const override;
183
192 virtual void forEachChild(cVisitor *v) override;
194
197
201 virtual const char *getTagName() const {return getName();}
202
206 virtual const char *getSourceFileName() const {return loc.getFilename();}
207
211 virtual int getSourceLineNumber() const {return loc.getLineNumber();}
212
217 virtual const char *getSourceLocation() const;
218
223 virtual const char *getNodeValue() const;
224
229 virtual const char *getAttribute(const char *attr) const;
230
234 virtual bool hasAttributes() const;
235
239 virtual cXMLAttributeMap getAttributes() const;
240
244 virtual std::string getXML() const;
246
249
252 virtual cXMLElement *getParentNode() const;
253
257 virtual bool hasChildren() const;
258
263 virtual cXMLElement *getFirstChild() const;
264
269 virtual cXMLElement *getLastChild() const;
270
286 virtual cXMLElement *getNextSibling() const;
287
293 virtual cXMLElement *getPreviousSibling() const;
294
299 virtual cXMLElement *getFirstChildWithTag(const char *tagname) const;
300
315 virtual cXMLElement *getNextSiblingWithTag(const char *tagname) const;
316
320 virtual cXMLElementList getChildren() const;
321
325 virtual cXMLElementList getChildrenByTagName(const char *tagname) const;
326
331 virtual cXMLElementList getElementsByTagName(const char *tagname) const;
333
336
342 virtual cXMLElement *getFirstChildWithAttribute(const char *tagname, const char *attr, const char *attrvalue=nullptr) const;
343
349 virtual cXMLElement *getElementById(const char *idattrvalue) const;
350
380 virtual cXMLElement *getElementByPath(const char *pathexpression, cXMLElement *root=nullptr, ParamResolver *resolver=nullptr) const;
382};
383
391class SIM_API ModNameParamResolver : public cXMLElement::ParamResolver
392{
393 protected:
394 cModule *mod;
395 public:
396 ModNameParamResolver(cModule *mod) {this->mod = mod;}
397 virtual bool resolve(const char *paramname, std::string& value) override;
398};
399
406class SIM_API StringMapParamResolver : public cXMLElement::ParamResolver
407{
408 public:
409 typedef std::map<std::string,std::string> StringMap;
410 protected:
411 StringMap params;
412 public:
413 StringMapParamResolver(const StringMap& m) {params = m;}
414 virtual bool resolve(const char *paramname, std::string& value) override;
415};
416
417} // namespace omnetpp
418
419
420#endif
421
Definition fileline.h:28
virtual bool resolve(const char *paramname, std::string &value) override
virtual bool resolve(const char *paramname, std::string &value) override
This class represents modules in the simulation.
Definition cmodule.h:49
virtual const char * getName() const override
Definition cnamedobject.h:111
Enables traversing the tree of (cObject-rooted) simulation objects.
Definition cvisitor.h:57
Base class for classes that resolve parameters ($PARAM) that occur in in XPath expressions to their v...
Definition cxmlelement.h:85
virtual bool resolve(const char *paramname, std::string &value)=0
Represents an XML element in an XML configuration file.
Definition cxmlelement.h:76
virtual cXMLElementList getElementsByTagName(const char *tagname) const
virtual cXMLElement * getParentNode() const
virtual cXMLElement * dup() const override
Definition cxmlelement.h:124
virtual cXMLElement * getElementById(const char *idattrvalue) const
virtual const char * getTagName() const
Definition cxmlelement.h:201
virtual cXMLElement * getPreviousSibling() const
virtual cXMLElement * getNextSiblingWithTag(const char *tagname) const
virtual const char * getSourceLocation() const
virtual std::string getXML() const
virtual const char * getNodeValue() const
virtual cXMLElement * getLastChild() const
virtual cXMLAttributeMap getAttributes() const
virtual cXMLElement * getFirstChildWithAttribute(const char *tagname, const char *attr, const char *attrvalue=nullptr) const
virtual cXMLElement * getElementByPath(const char *pathexpression, cXMLElement *root=nullptr, ParamResolver *resolver=nullptr) const
virtual cXMLElement * getNextSibling() const
virtual const char * getSourceFileName() const
Definition cxmlelement.h:206
virtual cXMLElementList getChildren() const
virtual bool hasAttributes() const
virtual const char * getAttribute(const char *attr) const
virtual bool hasChildren() const
virtual void forEachChild(cVisitor *v) override
virtual std::string str() const override
virtual cXMLElement * getFirstChildWithTag(const char *tagname) const
virtual int getSourceLineNumber() const
Definition cxmlelement.h:211
virtual cXMLElement * getFirstChild() const
virtual cXMLElementList getChildrenByTagName(const char *tagname) const
Definition opp_pooledstring.h:64