Functions

XMLUtils.cc File Reference

#include "XMLUtils.h"
#include "IPAddressResolver.h"

Go to the source code of this file.

Functions

const cXMLElement * getUniqueChild (const cXMLElement *node, const char *name)
const cXMLElement * getUniqueChildIfExists (const cXMLElement *node, const char *name)
bool parseBool (const char *text)
void checkTags (const cXMLElement *node, const char *allowed)
const char * getParameterStrValue (const cXMLElement *ptr, const char *name, const char *def)
bool getParameterBoolValue (const cXMLElement *ptr, const char *name, bool def)
bool getParameterBoolValue (const cXMLElement *ptr, const char *name)
const char * getParameterStrValue (const cXMLElement *ptr, const char *name)
int getParameterIntValue (const cXMLElement *ptr, const char *name, int def)
int getParameterIntValue (const cXMLElement *ptr, const char *name)
IPAddress getParameterIPAddressValue (const cXMLElement *ptr, const char *name, IPAddress def)
IPAddress getParameterIPAddressValue (const cXMLElement *ptr, const char *name)
double getParameterDoubleValue (const cXMLElement *ptr, const char *name, double def)
double getParameterDoubleValue (const cXMLElement *ptr, const char *name)

Function Documentation

void checkTags ( const cXMLElement *  node,
const char *  allowed 
)

Definition at line 51 of file XMLUtils.cc.

Referenced by RSVP::delSession(), SimpleClassifier::readItemFromXML(), SimpleClassifier::readTableFromXML(), LIBTable::readTableFromXML(), RSVP::readTrafficFromXML(), RSVP::readTrafficRouteFromXML(), and RSVP::readTrafficSessionFromXML().

{
    std::vector<const char *> tags;

    cStringTokenizer st(allowed, " ");
    const char *nt;
    while((nt = st.nextToken())!=NULL)
        tags.push_back(nt);

    for(cXMLElement *child=node->getFirstChild(); child; child=child->getNextSibling())
    {
        unsigned int i;
        for(i = 0; i < tags.size(); i++)
            if(!strcmp(child->getTagName(), tags[i]))
                break;
        if(i == tags.size())
            opp_error("subtag <%s> not expected in <%s>", child->getTagName(), node->getTagName());
    }
}

bool getParameterBoolValue ( const cXMLElement *  ptr,
const char *  name,
bool  def 
)

Definition at line 80 of file XMLUtils.cc.

Referenced by RSVP::readTrafficSessionFromXML().

{
    const cXMLElement *xvalue = getUniqueChildIfExists(ptr, name);
    if(xvalue)
        return parseBool(xvalue->getNodeValue());
    else
        return def;
}

bool getParameterBoolValue ( const cXMLElement *  ptr,
const char *  name 
)

Definition at line 89 of file XMLUtils.cc.

{
    const cXMLElement *xvalue = getUniqueChild(ptr, name);
    return parseBool(xvalue->getNodeValue());
}

double getParameterDoubleValue ( const cXMLElement *  ptr,
const char *  name,
double  def 
)

Definition at line 131 of file XMLUtils.cc.

Referenced by RSVP::readTrafficSessionFromXML().

{
    const cXMLElement *xvalue = getUniqueChildIfExists(ptr, name);
    if(xvalue)
        return strtod(xvalue->getNodeValue(), NULL);
    else
        return def;
}

double getParameterDoubleValue ( const cXMLElement *  ptr,
const char *  name 
)

Definition at line 140 of file XMLUtils.cc.

{
    const cXMLElement *xvalue = getUniqueChild(ptr, name);
    return strtod(xvalue->getNodeValue(), NULL);
}

int getParameterIntValue ( const cXMLElement *  ptr,
const char *  name,
int  def 
)

Definition at line 101 of file XMLUtils.cc.

Referenced by RSVP::delSession(), SimpleClassifier::readItemFromXML(), LIBTable::readTableFromXML(), and RSVP::readTrafficSessionFromXML().

{
    const cXMLElement *xvalue = getUniqueChildIfExists(ptr, name);
    if(xvalue)
        return atoi(xvalue->getNodeValue());
    else
        return def;
}

int getParameterIntValue ( const cXMLElement *  ptr,
const char *  name 
)

Definition at line 110 of file XMLUtils.cc.

{
    const cXMLElement *xvalue = getUniqueChild(ptr, name);
    return atoi(xvalue->getNodeValue());
}

IPAddress getParameterIPAddressValue ( const cXMLElement *  ptr,
const char *  name,
IPAddress  def 
)

Definition at line 116 of file XMLUtils.cc.

Referenced by RSVP::delSession(), SimpleClassifier::readItemFromXML(), and RSVP::readTrafficSessionFromXML().

{
    const cXMLElement *xvalue = getUniqueChildIfExists(ptr, name);
    if(xvalue)
        return IPAddress(xvalue->getNodeValue());
    else
        return def;
}

IPAddress getParameterIPAddressValue ( const cXMLElement *  ptr,
const char *  name 
)

Definition at line 125 of file XMLUtils.cc.

{
    const cXMLElement *xvalue = getUniqueChild(ptr, name);
    return IPAddressResolver().resolve(xvalue->getNodeValue()).get4();
}

const char* getParameterStrValue ( const cXMLElement *  ptr,
const char *  name 
)

Definition at line 95 of file XMLUtils.cc.

{
    const cXMLElement *xvalue = getUniqueChild(ptr, name);
    return xvalue->getNodeValue();
}

const char* getParameterStrValue ( const cXMLElement *  ptr,
const char *  name,
const char *  def 
)

Definition at line 71 of file XMLUtils.cc.

Referenced by LIBTable::readTableFromXML(), and RSVP::readTrafficSessionFromXML().

{
    const cXMLElement *xvalue = getUniqueChildIfExists(ptr, name);
    if(xvalue)
        return xvalue->getNodeValue();
    else
        return def;
}

const cXMLElement* getUniqueChild ( const cXMLElement *  node,
const char *  name 
)

Definition at line 5 of file XMLUtils.cc.

Referenced by getParameterBoolValue(), getParameterDoubleValue(), getParameterIntValue(), getParameterIPAddressValue(), getParameterStrValue(), LIBTable::readTableFromXML(), and RSVP::readTrafficSessionFromXML().

{
    const cXMLElement *child = getUniqueChildIfExists(node, name);
    if(!child)
        throw cRuntimeError("xml error: exactly one %s element expected", name);

    return child;
}

const cXMLElement* getUniqueChildIfExists ( const cXMLElement *  node,
const char *  name 
)

Definition at line 14 of file XMLUtils.cc.

Referenced by RSVP::delSession(), getParameterBoolValue(), getParameterDoubleValue(), getParameterIntValue(), getParameterIPAddressValue(), getParameterStrValue(), getUniqueChild(), SimpleClassifier::readItemFromXML(), and RSVP::readTrafficSessionFromXML().

{
    cXMLElementList list = node->getChildrenByTagName(name);
    if(list.size() > 1)
        throw cRuntimeError("xml error: at most one %s element expected", name);
    else if(list.size() == 1)
        return (*list.begin());
    else
        return NULL;
}

bool parseBool ( const char *  text  ) 

Definition at line 25 of file XMLUtils.cc.

Referenced by getParameterBoolValue().

{
    if(!strcasecmp(text, "down"))
        return false;
    else if(!strcasecmp(text, "off"))
        return false;
    else if(!strcasecmp(text, "false"))
        return false;
    else if(!strcasecmp(text, "no"))
        return false;
    else if(!strcasecmp(text, "0"))
        return false;
    else if(!strcasecmp(text, "up"))
        return true;
    else if(!strcasecmp(text, "on"))
        return true;
    else if(!strcasecmp(text, "true"))
        return true;
    else if(!strcasecmp(text, "yes"))
        return true;
    else if(!strcasecmp(text, "1"))
        return true;
    else
        throw cRuntimeError("unknown bool constant: %s", text);
}