OMNeT++ Simulation Library  5.4.1
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 
25 namespace omnetpp {
26 
27 class cXMLElement;
28 class cModule;
29 
33 typedef std::vector<cXMLElement*> cXMLElementList;
34 
38 typedef std::map<std::string,std::string> cXMLAttributeMap;
39 
40 
73 class SIM_API cXMLElement : public cObject, noncopyable
74 {
75  friend class cXMLElementDescriptor; // getAttr(i), getChild(i), etc.
76 
77  public:
82  class SIM_API ParamResolver
83  {
84  public:
90  virtual bool resolve(const char *paramname, std::string& value) = 0;
91  virtual ~ParamResolver() {}
92  };
93 
94  private:
95  std::string ename;
96  std::string srcloc;
97  std::string value;
98  cXMLAttributeMap attrs;
99  cXMLElement *parent;
100  cXMLElement *firstChild;
101  cXMLElement *lastChild;
102  cXMLElement *prevSibling;
103  cXMLElement *nextSibling;
104 
105  private:
106  void doGetElementsByTagName(const char *tagname, cXMLElementList& list) const;
107 
108  public:
109  // internal: Constructor
110  cXMLElement(const char *tagname, const char *srcloc, cXMLElement *parent);
111 
112  // internal: sets text node within element
113  virtual void setNodeValue(const char *s, int len);
114 
115  // internal: appends to text node within element
116  virtual void appendNodeValue(const char *s, int len);
117 
118  // internal: Destructor. Destroys children too.
119  virtual ~cXMLElement();
120 
121  // internal: Sets the value of the attribute with the given name.
122  virtual void setAttribute(const char *attr, const char *value);
123 
124  // internal: Appends the given element at the end of the child element list.
125  virtual void appendChild(cXMLElement *node);
126 
127  // internal: Inserts the given element just before the specified child element
128  // in the child element list. The where element must be a child of this element.
129  virtual void insertChildBefore(cXMLElement *where, cXMLElement *newnode);
130 
131  // internal: Removes the given element from the child element list.
132  // The pointer passed should be a child of this element.
133  virtual cXMLElement *removeChild(cXMLElement *node);
134 
135  // internal: matches from root element
136  static cXMLElement *getDocumentElementByPath(cXMLElement *documentnode, const char *pathexpr, ParamResolver *resolver=nullptr);
137 
138  private:
139  // internal
140  virtual void print(std::ostream& os, int indentLevel) const;
141 
142  // internal, for inspectors only; O(n) complexity!
143  int getNumAttrs() const;
144  std::string getAttr(int index) const;
145  int getNumChildren() const;
146  cXMLElement *getChild(int index) const;
147 
148  public:
154  virtual const char *getName() const override {return getTagName();}
155 
159  virtual std::string str() const override;
160 
164  virtual cObject *getOwner() const override {return getParentNode();}
165 
174  virtual void forEachChild(cVisitor *v) override;
176 
179 
183  virtual const char *getTagName() const;
184 
189  virtual const char *getSourceLocation() const;
190 
195  virtual const char *getNodeValue() const;
196 
201  virtual const char *getAttribute(const char *attr) const;
202 
206  virtual bool hasAttributes() const;
207 
211  virtual const cXMLAttributeMap& getAttributes() const;
212 
216  virtual std::string getXML() const;
218 
224  virtual cXMLElement *getParentNode() const;
225 
229  virtual bool hasChildren() const;
230 
235  virtual cXMLElement *getFirstChild() const;
236 
241  virtual cXMLElement *getLastChild() const;
242 
258  virtual cXMLElement *getNextSibling() const;
259 
265  virtual cXMLElement *getPreviousSibling() const;
266 
271  virtual cXMLElement *getFirstChildWithTag(const char *tagname) const;
272 
287  virtual cXMLElement *getNextSiblingWithTag(const char *tagname) const;
288 
292  virtual cXMLElementList getChildren() const;
293 
297  virtual cXMLElementList getChildrenByTagName(const char *tagname) const;
298 
303  virtual cXMLElementList getElementsByTagName(const char *tagname) const;
305 
314  virtual cXMLElement *getFirstChildWithAttribute(const char *tagname, const char *attr, const char *attrvalue=nullptr) const;
315 
321  virtual cXMLElement *getElementById(const char *idattrvalue) const;
322 
352  virtual cXMLElement *getElementByPath(const char *pathexpression, cXMLElement *root=nullptr, ParamResolver *resolver=nullptr) const;
354 };
355 
364 {
365  protected:
366  cModule *mod;
367  public:
368  ModNameParamResolver(cModule *mod) {this->mod = mod;}
369  virtual bool resolve(const char *paramname, std::string& value) override;
370 };
371 
379 {
380  public:
381  typedef std::map<std::string,std::string> StringMap;
382  protected:
383  StringMap params;
384  public:
385  StringMapParamResolver(const StringMap& m) {params = m;}
386  virtual bool resolve(const char *paramname, std::string& value) override;
387 };
388 
389 } // namespace omnetpp
390 
391 
392 #endif
393 
Root of the OMNeT++ class hierarchy. cObject is a lightweight class without any data members...
Definition: cobject.h:58
Represents an XML element in an XML configuration file.
Definition: cxmlelement.h:73
A parameter resolver class for cXMLElement cXMLElement::getElementByPath().
Definition: cxmlelement.h:363
virtual const char * getName() const override
Definition: cxmlelement.h:154
virtual cObject * getOwner() const override
Definition: cxmlelement.h:164
This class represents modules in the simulation.
Definition: cmodule.h:47
Base class for classes that resolve parameters ($PARAM) that occur in in XPath expressions to their v...
Definition: cxmlelement.h:82
A parameter resolver class for cXMLElement::getElementByPath().
Definition: cxmlelement.h:378
Enables traversing the tree of (cObject-rooted) simulation objects.
Definition: cvisitor.h:56
Utility class, to make it impossible to call the operator= and copy constructor of any class derived ...
Definition: cobject.h:311
Definition: cabstracthistogram.h:21