OMNeT++ Simulation Library  6.0.3
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 
27 namespace omnetpp {
28 
29 class cXMLElement;
30 class cModule;
31 
35 typedef std::vector<cXMLElement*> cXMLElementList;
36 
40 typedef std::map<std::string,std::string> cXMLAttributeMap;
41 
42 
75 class SIM_API cXMLElement : public cObject, noncopyable
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 tagName;
98  opp_pooledstring value;
99  typedef std::pair<opp_pooledstring,opp_pooledstring> Attr;
100  std::vector<Attr> attrs;
101  cXMLElement *parent = nullptr;
102  cXMLElement *firstChild = nullptr;
103  cXMLElement *lastChild = nullptr;
104  cXMLElement *prevSibling = nullptr;
105  cXMLElement *nextSibling = nullptr;
106  FileLine loc;
107 
108  private:
109  void doGetElementsByTagName(const char *tagname, cXMLElementList& list) const;
110 
111  public:
112  // internal: Constructor
113  cXMLElement(const char *tagname, cXMLElement *parent);
114 
115  // internal: Constructor - for backward compatibility
116  cXMLElement(const char *tagname, const char *ignored, cXMLElement *parent) : cXMLElement(tagname, parent) {}
117 
118  // internal: sets source location
119  virtual void setSourceLocation(const char *fname, int line);
120 
121  // internal: sets text node within element
122  virtual void setNodeValue(const char *s);
123 
124  // internal: sets text node within element
125  virtual void setNodeValue(const char *s, int len);
126 
127  // internal: appends to text node within element
128  virtual void appendNodeValue(const char *s, int len);
129 
130  // internal: Destructor. Destroys children too.
131  virtual ~cXMLElement();
132 
133  // internal: Sets the value of the attribute with the given name.
134  virtual void setAttribute(const char *attr, const char *value);
135 
136  // internal: Set attributes of the element: name,value,name,value,...,nullptr
137  virtual void setAttributes(const char **attrs);
138 
139  // internal: Appends the given element at the end of the child element list.
140  virtual void appendChild(cXMLElement *node);
141 
142  // internal: Inserts the given element just before the specified child element
143  // in the child element list. The where element must be a child of this element.
144  virtual void insertChildBefore(cXMLElement *where, cXMLElement *newnode);
145 
146  // internal: Removes the given element from the child element list.
147  // The pointer passed should be a child of this element.
148  virtual cXMLElement *removeChild(cXMLElement *node);
149 
150  // internal: matches from root element
151  static cXMLElement *getDocumentElementByPath(cXMLElement *documentnode, const char *pathexpr, ParamResolver *resolver=nullptr);
152 
153  private:
154  // internal
155  Attr *findAttr(const char *name) const;
156  void addAttr(const char *name, const char *value);
157  virtual void print(std::ostream& os, int indentLevel) const;
158 
159  // internal, for inspectors only; O(n) complexity!
160  int getNumAttrs() const;
161  const char *getAttrName(int index) const;
162  const char *getAttrValue(int index) const;
163  std::string getAttrDesc(int index) const;
164  int getNumChildren() const;
165  cXMLElement *getChild(int index) const;
166 
167  public:
173  virtual const char *getName() const override {return getTagName();}
174 
178  virtual std::string str() const override;
179 
183  virtual cObject *getOwner() const override {return getParentNode();}
184 
193  virtual void forEachChild(cVisitor *v) override;
195 
198 
202  virtual const char *getTagName() const;
203 
207  virtual const char *getSourceFileName() const {return loc.getFilename();}
208 
212  virtual int getSourceLineNumber() const {return loc.getLineNumber();}
213 
218  virtual const char *getSourceLocation() const;
219 
224  virtual const char *getNodeValue() const;
225 
230  virtual const char *getAttribute(const char *attr) const;
231 
235  virtual bool hasAttributes() const;
236 
240  virtual cXMLAttributeMap getAttributes() const;
241 
245  virtual std::string getXML() const;
247 
253  virtual cXMLElement *getParentNode() const;
254 
258  virtual bool hasChildren() const;
259 
264  virtual cXMLElement *getFirstChild() const;
265 
270  virtual cXMLElement *getLastChild() const;
271 
287  virtual cXMLElement *getNextSibling() const;
288 
294  virtual cXMLElement *getPreviousSibling() const;
295 
300  virtual cXMLElement *getFirstChildWithTag(const char *tagname) const;
301 
316  virtual cXMLElement *getNextSiblingWithTag(const char *tagname) const;
317 
321  virtual cXMLElementList getChildren() const;
322 
326  virtual cXMLElementList getChildrenByTagName(const char *tagname) const;
327 
332  virtual cXMLElementList getElementsByTagName(const char *tagname) const;
334 
343  virtual cXMLElement *getFirstChildWithAttribute(const char *tagname, const char *attr, const char *attrvalue=nullptr) const;
344 
350  virtual cXMLElement *getElementById(const char *idattrvalue) const;
351 
381  virtual cXMLElement *getElementByPath(const char *pathexpression, cXMLElement *root=nullptr, ParamResolver *resolver=nullptr) const;
383 };
384 
393 {
394  protected:
395  cModule *mod;
396  public:
397  ModNameParamResolver(cModule *mod) {this->mod = mod;}
398  virtual bool resolve(const char *paramname, std::string& value) override;
399 };
400 
408 {
409  public:
410  typedef std::map<std::string,std::string> StringMap;
411  protected:
412  StringMap params;
413  public:
414  StringMapParamResolver(const StringMap& m) {params = m;}
415  virtual bool resolve(const char *paramname, std::string& value) override;
416 };
417 
418 } // namespace omnetpp
419 
420 
421 #endif
422 
omnetpp::cModule
This class represents modules in the simulation.
Definition: cmodule.h:48
omnetpp::StringMapParamResolver
A parameter resolver class for cXMLElement::getElementByPath().
Definition: cxmlelement.h:407
omnetpp::cXMLElement::ParamResolver
Base class for classes that resolve parameters ($PARAM) that occur in in XPath expressions to their v...
Definition: cxmlelement.h:84
omnetpp::cXMLElement::getSourceFileName
virtual const char * getSourceFileName() const
Definition: cxmlelement.h:207
omnetpp::cObject
cObject is a lightweight class which serves as the root of the OMNeT++ class hierarchy....
Definition: cobject.h:92
omnetpp::noncopyable
Utility class, to make it impossible to call the operator= and copy constructor of any class derived ...
Definition: cobject.h:415
omnetpp::cVisitor
Enables traversing the tree of (cObject-rooted) simulation objects.
Definition: cvisitor.h:56
omnetpp::FileLine
Definition: fileline.h:27
omnetpp::ModNameParamResolver
A parameter resolver class for cXMLElement cXMLElement::getElementByPath().
Definition: cxmlelement.h:392
omnetpp::cXMLElement::getSourceLineNumber
virtual int getSourceLineNumber() const
Definition: cxmlelement.h:212
omnetpp::cXMLElement
Represents an XML element in an XML configuration file.
Definition: cxmlelement.h:75
omnetpp::opp_pooledstring
Definition: opp_pooledstring.h:63
omnetpp::cXMLElement::getName
virtual const char * getName() const override
Definition: cxmlelement.h:173
omnetpp::cXMLElement::getOwner
virtual cObject * getOwner() const override
Definition: cxmlelement.h:183