Skip to content

Commit

Permalink
add getValue() overloads that return true/false rather than throwing
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Smith committed Dec 22, 2020
1 parent faa6d30 commit 1aa458e
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 16 deletions.
32 changes: 28 additions & 4 deletions modules/c++/xml.lite/include/xml/lite/Attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#ifndef __XML_LITE_ATTRIBUTES_H__
#define __XML_LITE_ATTRIBUTES_H__
#pragma once

#include "sys/Conf.h"
#include "except/Exception.h"
Expand Down Expand Up @@ -172,7 +173,7 @@ class Attributes
* Adds an attribute to the list of attributes.
* \param attribute the attribute to add
*/
void add(const AttributeNode & attribute);
AttributeNode& add(const AttributeNode& attribute);

/*!
* Look up the index of an attribute by XML 1.0 qualified name.
Expand Down Expand Up @@ -223,6 +224,13 @@ class Attributes
* \throw IndexOutOfRangeException if the index is out of range
*/
std::string getValue(int i) const;
/*!
* Look up an attribute's value by index.
* \param i The index for the attribute we want
* \param result The value, if found
* \return If the index is out of range or not
*/
bool getValue(int i, std::string& result) const;

/*!
* Look up an attribute's value by XML 1.0 qualified name.
Expand All @@ -231,16 +239,32 @@ class Attributes
* \throw NoSuchKeyException If the qname is not found
*/
std::string getValue(const std::string & qname) const;
/*!
* Look up an attribute's value by XML 1.0 qualified name.
* \param qname The qualified name
* \param result The value, if found
* \return If the qname is not found or not
*/
bool getValue(const std::string& qname, std::string& result) const;

/*!
* Look up an attribute's value by Namespace name.
* \param uri The uri association
* \param localName the local name of the object
* \return The value
* \throw NoSuchKeyException If the qname is not found
* \throw NoSuchKeyException If the uri/localName is not found
*/
std::string getValue(const std::string & uri,
const std::string & localName) const;
std::string getValue(const std::string & uri, const std::string & localName) const;
/*!
* Look up an attribute's value by Namespace name.
* \param uri The uri association
* \param localName the local name of the object
* \param result The value, if found
* \return If the uri/localName is not found or not
*/
bool getValue(const std::string& uri, const std::string& localName,
std::string& result) const;

/*!
* Get an attribute note based on the index as a const ref
* \param i The node index
Expand Down
56 changes: 44 additions & 12 deletions modules/c++/xml.lite/source/Attributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ std::string xml::lite::Attributes::getValue(int i) const
{
return mAttributes.at(i).getValue();
}
bool xml::lite::Attributes::getValue(int i, std::string& result) const
{
if ((i >= 0) && (i < mAttributes.size()))
{
result = mAttributes[i].getValue();
return true; // index in range
}
return false; // index out-of-range
}

std::string xml::lite::Attributes::getUri(int i) const
{
Expand All @@ -82,39 +91,62 @@ std::string xml::lite::Attributes::getQName(int i) const
}

std::string xml::lite::Attributes::getValue(const std::string& qname) const
{
std::string retval;
if (!getValue(qname, retval))
{
throw except::NoSuchKeyException(Ctxt(FmtX("QName '%s' could not be found",
qname.c_str())));
}

return retval;
}
{
for (size_t i = 0; i < mAttributes.size(); i++)
{
if (qname == mAttributes[i].getQName())
return mAttributes[i].getValue();
{
result = mAttributes[i].getValue();
return true; // found
}
}
throw except::NoSuchKeyException(Ctxt(FmtX("QName '%s' could not be found",
qname.c_str())));

// We don't ever reach this but it keeps the compiler from complaining...
return mAttributes[mAttributes.size() - 1].getValue();
return false; // not found
}

std::string xml::lite::Attributes::getValue(
const std::string& uri,
const std::string& localName) const
{
std::string retval;
if (!getValue(uri, localName, retval))
{
throw except::NoSuchKeyException(Ctxt(FmtX("(uri: %s, localName: %s",
uri.c_str(), localName.c_str())));
}
return retval;
}
bool xml::lite::Attributes::getValue(
const std::string& uri,
const std::string& localName,
std::string& result) const
{
for (size_t i = 0; i < mAttributes.size(); i++)
{
if ((uri == mAttributes[i].getUri()) && (localName
== mAttributes[i].getLocalName()))
return mAttributes[i].getValue();
{
result = mAttributes[i].getValue();
return true; // found
}
}
throw except::NoSuchKeyException(Ctxt(FmtX("(uri: %s, localName: %s",
uri.c_str(), localName.c_str())));

// We don't ever reach this but it keeps the compiler from complaining...
return mAttributes[mAttributes.size() - 1].getValue();
return false; // not found
}

void xml::lite::Attributes::add(const AttributeNode& attribute)
xml::lite::AttributeNode& xml::lite::Attributes::add(const AttributeNode& attribute)
{
mAttributes.push_back(attribute);
return mAttributes.back();
}

void xml::lite::AttributeNode::setQName(const std::string& qname)
Expand Down

0 comments on commit 1aa458e

Please sign in to comment.