XMLUtils.cc

Go to the documentation of this file.
00001 
00002 #include "XMLUtils.h"
00003 #include "IPAddressResolver.h"
00004 
00005 const cXMLElement* getUniqueChild(const cXMLElement *node, const char *name)
00006 {
00007     const cXMLElement *child = getUniqueChildIfExists(node, name);
00008     if(!child)
00009         throw cRuntimeError("xml error: exactly one %s element expected", name);
00010 
00011     return child;
00012 }
00013 
00014 const cXMLElement* getUniqueChildIfExists(const cXMLElement *node, const char *name)
00015 {
00016     cXMLElementList list = node->getChildrenByTagName(name);
00017     if(list.size() > 1)
00018         throw cRuntimeError("xml error: at most one %s element expected", name);
00019     else if(list.size() == 1)
00020         return (*list.begin());
00021     else
00022         return NULL;
00023 }
00024 
00025 bool parseBool(const char *text)
00026 {
00027     if(!strcasecmp(text, "down"))
00028         return false;
00029     else if(!strcasecmp(text, "off"))
00030         return false;
00031     else if(!strcasecmp(text, "false"))
00032         return false;
00033     else if(!strcasecmp(text, "no"))
00034         return false;
00035     else if(!strcasecmp(text, "0"))
00036         return false;
00037     else if(!strcasecmp(text, "up"))
00038         return true;
00039     else if(!strcasecmp(text, "on"))
00040         return true;
00041     else if(!strcasecmp(text, "true"))
00042         return true;
00043     else if(!strcasecmp(text, "yes"))
00044         return true;
00045     else if(!strcasecmp(text, "1"))
00046         return true;
00047     else
00048         throw cRuntimeError("unknown bool constant: %s", text);
00049 }
00050 
00051 void checkTags(const cXMLElement *node, const char *allowed)
00052 {
00053     std::vector<const char *> tags;
00054 
00055     cStringTokenizer st(allowed, " ");
00056     const char *nt;
00057     while((nt = st.nextToken())!=NULL)
00058         tags.push_back(nt);
00059 
00060     for(cXMLElement *child=node->getFirstChild(); child; child=child->getNextSibling())
00061     {
00062         unsigned int i;
00063         for(i = 0; i < tags.size(); i++)
00064             if(!strcmp(child->getTagName(), tags[i]))
00065                 break;
00066         if(i == tags.size())
00067             opp_error("subtag <%s> not expected in <%s>", child->getTagName(), node->getTagName());
00068     }
00069 }
00070 
00071 const char* getParameterStrValue(const cXMLElement *ptr, const char *name, const char *def)
00072 {
00073     const cXMLElement *xvalue = getUniqueChildIfExists(ptr, name);
00074     if(xvalue)
00075         return xvalue->getNodeValue();
00076     else
00077         return def;
00078 }
00079 
00080 bool getParameterBoolValue(const cXMLElement *ptr, const char *name, bool def)
00081 {
00082     const cXMLElement *xvalue = getUniqueChildIfExists(ptr, name);
00083     if(xvalue)
00084         return parseBool(xvalue->getNodeValue());
00085     else
00086         return def;
00087 }
00088 
00089 bool getParameterBoolValue(const cXMLElement *ptr, const char *name)
00090 {
00091     const cXMLElement *xvalue = getUniqueChild(ptr, name);
00092     return parseBool(xvalue->getNodeValue());
00093 }
00094 
00095 const char* getParameterStrValue(const cXMLElement *ptr, const char *name)
00096 {
00097     const cXMLElement *xvalue = getUniqueChild(ptr, name);
00098     return xvalue->getNodeValue();
00099 }
00100 
00101 int getParameterIntValue(const cXMLElement *ptr, const char *name, int def)
00102 {
00103     const cXMLElement *xvalue = getUniqueChildIfExists(ptr, name);
00104     if(xvalue)
00105         return atoi(xvalue->getNodeValue());
00106     else
00107         return def;
00108 }
00109 
00110 int getParameterIntValue(const cXMLElement *ptr, const char *name)
00111 {
00112     const cXMLElement *xvalue = getUniqueChild(ptr, name);
00113     return atoi(xvalue->getNodeValue());
00114 }
00115 
00116 IPAddress getParameterIPAddressValue(const cXMLElement *ptr, const char *name, IPAddress def)
00117 {
00118     const cXMLElement *xvalue = getUniqueChildIfExists(ptr, name);
00119     if(xvalue)
00120         return IPAddress(xvalue->getNodeValue());
00121     else
00122         return def;
00123 }
00124 
00125 IPAddress getParameterIPAddressValue(const cXMLElement *ptr, const char *name)
00126 {
00127     const cXMLElement *xvalue = getUniqueChild(ptr, name);
00128     return IPAddressResolver().resolve(xvalue->getNodeValue()).get4();
00129 }
00130 
00131 double getParameterDoubleValue(const cXMLElement *ptr, const char *name, double def)
00132 {
00133     const cXMLElement *xvalue = getUniqueChildIfExists(ptr, name);
00134     if(xvalue)
00135         return strtod(xvalue->getNodeValue(), NULL);
00136     else
00137         return def;
00138 }
00139 
00140 double getParameterDoubleValue(const cXMLElement *ptr, const char *name)
00141 {
00142     const cXMLElement *xvalue = getUniqueChild(ptr, name);
00143     return strtod(xvalue->getNodeValue(), NULL);
00144 }