# cxmlelement.h

    00001 //==========================================================================
    00002 //  CXMLELEMENT.H - part of
    00003 //                     OMNeT++/OMNEST
    00004 //            Discrete System Simulation in C++
    00005 //
    00006 // Contents:
    00007 //   class cXMLElement
    00008 //
    00009 //==========================================================================
    00010
    00011 /*--------------------------------------------------------------*
    00012   Copyright (C) 2002-2005 Andras Varga
    00013
    00014   This file is distributed WITHOUT ANY WARRANTY. See the file
    00015   `license' for details on this and other legal matters.
    00016 *--------------------------------------------------------------*/
    00017
    00018 #ifndef __CXMLELEMENT_H
    00019 #define __CXMLELEMENT_H
    00020
    00021 #include <string>
    00022 #include <map>
    00023 #include <vector>
    00024 #include "defs.h"
    00025
    00026 class cXMLElement;
    00027 class cModule;
    00028
    00032 typedef std::vector<cXMLElement*> cXMLElementList;
    00033
    00037 typedef std::map<std::string,std::string> cXMLAttributeMap;
    00038
    00039
    00071 // TBD if namespaces are supported by Expat & libxml in an easy way, maybe do it here
    00072 class SIM_API cXMLElement
    00073 {
    00074   public:
    00079     class SIM_API ParamResolver
    00080     {
    00081       public:
    00087         virtual bool resolve(const char *paramname, std::string& value) = 0;
    00088         virtual ~ParamResolver() {}
    00089     };
    00090
    00091   private:
    00092     std::string ename;
    00093     std::string srcloc;
    00094     std::string value;
    00095     cXMLAttributeMap attrs;
    00096     cXMLElement *parent;
    00097     cXMLElement *firstchild;
    00098     cXMLElement *lastchild;
    00099     cXMLElement *prevsibling;
    00100     cXMLElement *nextsibling;
    00101
    00102   private:
    00103      void doGetElementsByTagName(const char *tagname, cXMLElementList& list) const;
    00104
    00105   public:
    00106     // internal: Constructor
    00107     cXMLElement(const char *tagname, const char *srcloc, cXMLElement *parent);
    00108
    00109     // internal: sets text node within element
    00110     virtual void setNodeValue(const char *s, int len);
    00111
    00112     // internal: appends to text node within element
    00113     virtual void appendNodeValue(const char *s, int len);
    00114
    00115     // internal: Destructor. Destroys children too.
    00116     virtual ~cXMLElement();
    00117
    00118     // internal: Sets the value of the attribute with the given name.
    00119     virtual void setAttribute(const char *attr, const char *value);
    00120
    00121     // internal: Appends the given element at the end of the child element list.
    00122     virtual void appendChild(cXMLElement *node);
    00123
    00124     // internal: Inserts the given element just before the specified child element
    00125     // in the child element list. The where element must be a child of this element.
    00126     virtual void insertChildBefore(cXMLElement *where, cXMLElement *newnode);
    00127
    00128     // internal: Removes the given element from the child element list.
    00129     // The pointer passed should be a child of this element.
    00130     virtual cXMLElement *removeChild(cXMLElement *node);
    00131
    00132     // internal: matches from root element
    00133     static cXMLElement *getDocumentElementByPath(cXMLElement *documentnode, const char *pathexpr, ParamResolver *resolver=NULL);
    00134
    00137
    00141     virtual const char *getTagName() const;
    00142
    00147     virtual const char *getSourceLocation() const;
    00148
    00153     virtual const char *getNodeValue() const;
    00154
    00159     virtual const char *getAttribute(const char *attr) const;
    00160
    00164     virtual bool hasAttributes() const;
    00165
    00169     virtual const cXMLAttributeMap& getAttributes() const;
    00171
    00177     virtual cXMLElement *getParentNode() const;
    00178
    00182     virtual bool hasChildren() const;
    00183
    00188     virtual cXMLElement *getFirstChild() const;
    00189
    00194     virtual cXMLElement *getLastChild() const;
    00195
    00211     virtual cXMLElement *getNextSibling() const;
    00212
    00218     virtual cXMLElement *getPreviousSibling() const;
    00219
    00224     virtual cXMLElement *getFirstChildWithTag(const char *tagname) const;
    00225
    00240     virtual cXMLElement *getNextSiblingWithTag(const char *tagname) const;
    00241
    00245     virtual cXMLElementList getChildren() const;
    00246
    00250     virtual cXMLElementList getChildrenByTagName(const char *tagname) const;
    00251
    00256     virtual cXMLElementList getElementsByTagName(const char *tagname) const;
    00258
    00267     cXMLElement *getFirstChildWithAttribute(const char *tagname, const char *attr, const char *attrvalue=NULL) const;
    00268
    00274     cXMLElement *getElementById(const char *idattrvalue) const;
    00275
    00305     cXMLElement *getElementByPath(const char *pathexpression, cXMLElement *root=NULL, ParamResolver *resolver=NULL) const;
    00306
    00313     void debugDump(int depth=0) const;
    00315 };
    00316
    00324 class SIM_API ModNameParamResolver : public cXMLElement::ParamResolver
    00325 {
    00326   protected:
    00327     cModule *mod;
    00328   public:
    00329     ModNameParamResolver(cModule *mod)  {this->mod = mod;}
    00330     virtual bool resolve(const char *paramname, std::string& value);
    00331 };
    00332
    00338 class SIM_API StringMapParamResolver : public cXMLElement::ParamResolver
    00339 {
    00340   public:
    00341     typedef std::map<std::string,std::string> StringMap;
    00342   protected:
    00343     StringMap params;
    00344   public:
    00345     StringMapParamResolver(const StringMap& m)  {params = m;}
    00346     virtual bool resolve(const char *paramname, std::string& value);
    00347 };
    00348
    00349 #endif
    00350