Commit fffcfd2e80ea5c0e2ee350737968747f5e251c4c

Authored by Jürgen Knödlseder
1 parent b08b52b8

Add xml_get_attr() function to GTools.hpp to access directly the attribute of a …

…parameter in an XML file.
include/GTools.hpp
1 1 /***************************************************************************
2 2 * GTools.hpp - GammaLib tools *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2008-2015 by Juergen Knoedlseder *
  4 + * copyright (C) 2008-2016 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ... @@ -115,6 +115,10 @@ namespace gammalib {
115 115 const GXmlElement* xml_get_par(const std::string& origin,
116 116 const GXmlElement& xml,
117 117 const std::string& name);
  118 + std::string xml_get_attr(const std::string& origin,
  119 + const GXmlElement& xml,
  120 + const std::string& name,
  121 + const std::string& attribute);
118 122 void xml_check_par(const std::string& origin,
119 123 const std::string& name,
120 124 const int& number);
... ...
pyext/GTools.i
1 1 /***************************************************************************
2 2 * GTools.i - GammaLib tools *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2008-2015 by Juergen Knoedlseder *
  4 + * copyright (C) 2008-2016 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ... @@ -82,6 +82,10 @@ namespace gammalib {
82 82 const GXmlElement* xml_get_par(const std::string& origin,
83 83 const GXmlElement& xml,
84 84 const std::string& name);
  85 + std::string xml_get_attr(const std::string& origin,
  86 + const GXmlElement& xml,
  87 + const std::string& name,
  88 + const std::string& attribute);
85 89 void xml_check_par(const std::string& origin,
86 90 const std::string& name,
87 91 const int& number);
... ...
src/support/GTools.cpp
... ... @@ -1392,10 +1392,10 @@ bool gammalib::xml_has_par(const GXmlElement& xml, const std::string& name)
1392 1392 *
1393 1393 * Returns pointer to parameter with given @p name in XML element. If the
1394 1394 * @p name is not found, a parameter with the given @p name is added. In
1395   - * that respect the method differs from xml_getpar which does not add a
  1395 + * that respect the function differs from xml_get_par which does not add a
1396 1396 * parameter element.
1397 1397 *
1398   - * The method checks for multiple occurences of a parameter and throws an
  1398 + * The function checks for multiple occurences of a parameter and throws an
1399 1399 * exception in case that more than one parameter with a given name is found.
1400 1400 ***************************************************************************/
1401 1401 GXmlElement* gammalib::xml_need_par(const std::string& origin,
... ... @@ -1442,10 +1442,10 @@ GXmlElement* gammalib::xml_need_par(const std::string& origin,
1442 1442 * @exception GException::invalid_value
1443 1443 * Invalid XML format encountered.
1444 1444 *
1445   - * Returns pointer to parameter with given @p name in XML element. The method
1446   - * checks whether the parameter has been found and throws an exception if
1447   - * no parameter or multiple occurences of a parameter with given @p name
1448   - * are found.
  1445 + * Returns pointer to parameter with given @p name in XML element. The
  1446 + * function checks whether the parameter has been found and throws an
  1447 + * exception if no parameter or multiple occurences of a parameter with given
  1448 + * @p name are found.
1449 1449 ***************************************************************************/
1450 1450 const GXmlElement* gammalib::xml_get_par(const std::string& origin,
1451 1451 const GXmlElement& xml,
... ... @@ -1476,6 +1476,51 @@ const GXmlElement* gammalib::xml_get_par(const std::string& origin,
1476 1476  
1477 1477  
1478 1478 /***********************************************************************//**
  1479 + * @brief Return attribute value for a given parameter in XML element
  1480 + *
  1481 + * @param[in] origin Method requesting parameter.
  1482 + * @param[in] xml XML element.
  1483 + * @param[in] name Parameter name.
  1484 + * @param[in] attribute Attribute name.
  1485 + * @return Value of attribute.
  1486 + *
  1487 + * @exception GException::invalid_value
  1488 + * Attribute not found.
  1489 + *
  1490 + * Returns the value of @p attribute of parameter @p name in XML element.
  1491 + * The function checks whether the parameter has been found and throws an
  1492 + * exception if no parameter or multiple occurences of a parameter with given
  1493 + * @p name are found. The function furthermore checks whether the attribute
  1494 + * exists.
  1495 + ***************************************************************************/
  1496 +std::string gammalib::xml_get_attr(const std::string& origin,
  1497 + const GXmlElement& xml,
  1498 + const std::string& name,
  1499 + const std::string& attribute)
  1500 +{
  1501 + // Initialise attribute value
  1502 + std::string value = "";
  1503 +
  1504 + // Get parameter
  1505 + const GXmlElement* par = gammalib::xml_get_par(origin, xml, name);
  1506 +
  1507 + // Throw an exception if a parameter has not the requested attribute
  1508 + if (!par->has_attribute(attribute)) {
  1509 + std::string msg = "Attribute \""+attribute+"\" not found in XML "
  1510 + "parameter \""+name+"\". Please verify the XML "
  1511 + "format.";
  1512 + throw GException::invalid_value(origin, msg);
  1513 + }
  1514 +
  1515 + // Extract attribute
  1516 + value = par->attribute(attribute);
  1517 +
  1518 + // Return attribute value
  1519 + return value;
  1520 +}
  1521 +
  1522 +
  1523 +/***********************************************************************//**
1479 1524 * @brief Checks whether a parameter has occured once
1480 1525 *
1481 1526 * @param[in] origin Method performing the check.
... ...