OMNeT++ Simulation Library  5.6.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  const char *ename = nullptr;
96  const char *value = nullptr;
97  const char **attrs = nullptr; // name,value,name,value,...,nullptr
98  cXMLElement *parent = nullptr;
99  cXMLElement *firstChild = nullptr;
100  cXMLElement *lastChild = nullptr;
101  cXMLElement *prevSibling = nullptr;
102  cXMLElement *nextSibling = nullptr;
103  const char *filename = nullptr;
104  int lineNumber = -1;
105  int numAttrs = 0;
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);
113 
114  // internal: Constructor - for backward compatibility
115  cXMLElement(const char *tagname, const char *ignored, cXMLElement *parent) : cXMLElement(tagname, parent) {}
116 
117  // internal: sets source location
118  virtual void setSourceLocation(const char *fname, int line);
119 
120  // internal: sets text node within element
121  virtual void setNodeValue(const char *s);
122 
123  // internal: sets text node within element
124  virtual void setNodeValue(const char *s, int len);
125 
126  // internal: appends to text node within element
127  virtual void appendNodeValue(const char *s, int len);
128 
129  // internal: Destructor. Destroys children too.
130  virtual ~cXMLElement();
131 
132  // internal: Sets the value of the attribute with the given name.
133  virtual void setAttribute(const char *attr, const char *value);
134 
135  // internal: Set attributes of the element: name,value,name,value,...,nullptr
136  virtual void setAttributes(const char **attrs);
137 
138  // internal: Appends the given element at the end of the child element list.
139  virtual void appendChild(cXMLElement *node);
140 
141  // internal: Inserts the given element just before the specified child element
142  // in the child element list. The where element must be a child of this element.
143  virtual void insertChildBefore(cXMLElement *where, cXMLElement *newnode);
144 
145  // internal: Removes the given element from the child element list.
146  // The pointer passed should be a child of this element.
147  virtual cXMLElement *removeChild(cXMLElement *node);
148 
149  // internal: matches from root element
150  static cXMLElement *getDocumentElementByPath(cXMLElement *documentnode, const char *pathexpr, ParamResolver *resolver=nullptr);
151 
152  private:
153  // internal
154  void deleteAttrs();
155  const char **findAttr(const char *attr) const;
156  const char **addAttr(const char *attr);
157  static const char *getPooledName(const char *s);
158  static const char *makeValue(const char *s);
159  static void freeValue(const char *s);
160  virtual void print(std::ostream& os, int indentLevel) const;
161 
162  // internal, for inspectors only; O(n) complexity!
163  int getNumAttrs() const;
164  const char *getAttrName(int index) const;
165  const char *getAttrValue(int index) const;
166  std::string getAttrDesc(int index) const;
167  int getNumChildren() const;
168  cXMLElement *getChild(int index) const;
169 
170  public:
176  virtual const char *getName() const override {return getTagName();}
177 
181  virtual std::string str() const override;
182 
186  virtual cObject *getOwner() const override {return getParentNode();}
187 
196  virtual void forEachChild(cVisitor *v) override;
198 
201 
205  virtual const char *getTagName() const;
206 
210  virtual const char *getSourceFileName() const {return filename;}
211 
215  virtual int getSourceLineNumber() const {return lineNumber;}
216 
221  virtual const char *getSourceLocation() const;
222 
227  virtual const char *getNodeValue() const;
228 
233  virtual const char *getAttribute(const char *attr) const;
234 
238  virtual bool hasAttributes() const;
239 
243  virtual cXMLAttributeMap getAttributes() const;
244 
248  virtual std::string getXML() const;
250 
256  virtual cXMLElement *getParentNode() const;
257 
261  virtual bool hasChildren() const;
262 
267  virtual cXMLElement *getFirstChild() const;
268 
273  virtual cXMLElement *getLastChild() const;
274 
290  virtual cXMLElement *getNextSibling() const;
291 
297  virtual cXMLElement *getPreviousSibling() const;
298 
303  virtual cXMLElement *getFirstChildWithTag(const char *tagname) const;
304 
319  virtual cXMLElement *getNextSiblingWithTag(const char *tagname) const;
320 
324  virtual cXMLElementList getChildren() const;
325 
329  virtual cXMLElementList getChildrenByTagName(const char *tagname) const;
330 
335  virtual cXMLElementList getElementsByTagName(const char *tagname) const;
337 
346  virtual cXMLElement *getFirstChildWithAttribute(const char *tagname, const char *attr, const char *attrvalue=nullptr) const;
347 
353  virtual cXMLElement *getElementById(const char *idattrvalue) const;
354 
384  virtual cXMLElement *getElementByPath(const char *pathexpression, cXMLElement *root=nullptr, ParamResolver *resolver=nullptr) const;
386 };
387 
396 {
397  protected:
398  cModule *mod;
399  public:
400  ModNameParamResolver(cModule *mod) {this->mod = mod;}
401  virtual bool resolve(const char *paramname, std::string& value) override;
402 };
403 
411 {
412  public:
413  typedef std::map<std::string,std::string> StringMap;
414  protected:
415  StringMap params;
416  public:
417  StringMapParamResolver(const StringMap& m) {params = m;}
418  virtual bool resolve(const char *paramname, std::string& value) override;
419 };
420 
421 } // namespace omnetpp
422 
423 
424 #endif
425 
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:395
virtual const char * getName() const override
Definition: cxmlelement.h:176
virtual int getSourceLineNumber() const
Definition: cxmlelement.h:215
virtual cObject * getOwner() const override
Definition: cxmlelement.h:186
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
virtual const char * getSourceFileName() const
Definition: cxmlelement.h:210
A parameter resolver class for cXMLElement::getElementByPath().
Definition: cxmlelement.h:410
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