284 lines
8.3 KiB
C++
284 lines
8.3 KiB
C++
//****************************************************************************
|
|
// Filename: SimpleXMLParser.h
|
|
// Copyright 1999 Daniel X. Pape. All rights reserved.
|
|
//
|
|
// Description: implementation of the Element classes.
|
|
//
|
|
//****************************************************************************
|
|
// Revision History:
|
|
// Thursday, July 08, 1999 - Original. Heavily based on "A Simple XML
|
|
// Parser" by Sebastien Andrivet. See Documentation.
|
|
//****************************************************************************
|
|
|
|
#include "XmlElements.h"
|
|
|
|
|
|
|
|
// Our namespace
|
|
namespace SimpleXMLParser
|
|
{
|
|
|
|
// Null element
|
|
static ElementNull s_nullElem;
|
|
ElementNull& Element::nullElem = s_nullElem;
|
|
|
|
// ***************************************************************************
|
|
// Function: Constructor
|
|
// Desc:
|
|
// ***************************************************************************
|
|
Attribute::Attribute(const std::string& name, const std::string& value)
|
|
: name_(name), value_(value)
|
|
{
|
|
}
|
|
|
|
// ***************************************************************************
|
|
// Function: Add
|
|
// Desc: Adds the string to the value
|
|
// ***************************************************************************
|
|
void
|
|
Value::Add(const std::string& strText)
|
|
{
|
|
value_ += strText;
|
|
}
|
|
|
|
// ***************************************************************************
|
|
// Function: Add
|
|
// Desc: Adds the character to the value
|
|
// ***************************************************************************
|
|
void
|
|
Value::Add(char c)
|
|
{
|
|
value_ += c;
|
|
}
|
|
|
|
// ***************************************************************************
|
|
// Function: string conversion operator
|
|
// Desc:
|
|
// ***************************************************************************
|
|
Value::operator const std::string&() const
|
|
{
|
|
return(value_);
|
|
}
|
|
|
|
// ***************************************************************************
|
|
// Function: Constructor
|
|
// Desc:
|
|
// ***************************************************************************
|
|
Element::Element(const std::string& strName)
|
|
: name_(strName)
|
|
{
|
|
}
|
|
|
|
// ***************************************************************************
|
|
// Function: IsNull
|
|
// Desc: All elements are not null except Element::nullElem
|
|
// ***************************************************************************
|
|
bool
|
|
Element::IsNull() const
|
|
{
|
|
return(false);
|
|
}
|
|
|
|
// ***************************************************************************
|
|
// Function: Constructor
|
|
// Desc:
|
|
// ***************************************************************************
|
|
ElementTag::ElementTag(const std::string& strName)
|
|
: Element(strName)
|
|
{
|
|
}
|
|
|
|
// ***************************************************************************
|
|
// Function: Destructor
|
|
// Desc:
|
|
// ***************************************************************************
|
|
ElementTag::~ElementTag()
|
|
{
|
|
// Destroy attributes
|
|
// for(Attributes::iterator itAttrib = attributes_.begin();
|
|
// itAttrib != attributes_.end(); ++itAttrib)
|
|
// delete *itAttrib;
|
|
|
|
// Destroy childs
|
|
for(Elements::iterator itElem = children_.begin();
|
|
itElem != children_.end(); ++itElem)
|
|
delete *itElem;
|
|
}
|
|
|
|
// ***************************************************************************
|
|
// Function: AddChild
|
|
// Desc:
|
|
// ***************************************************************************
|
|
bool
|
|
ElementTag::AddChild(Element* pChild)
|
|
{
|
|
children_.push_back(pChild);
|
|
return(true);
|
|
}
|
|
|
|
// ***************************************************************************
|
|
// Function: GetChildren
|
|
// Desc:
|
|
// ***************************************************************************
|
|
const Elements*
|
|
ElementTag::GetChildren() const
|
|
{
|
|
return(children_.empty() ? 0 : &children_);
|
|
}
|
|
|
|
// ***************************************************************************
|
|
// Function: FindChild
|
|
// Desc: Find a child called szName starting at it
|
|
// ***************************************************************************
|
|
bool
|
|
ElementTag::FindChild(const char * szName,
|
|
Elements::const_iterator& it) const
|
|
{
|
|
for(; it != children_.end(); ++it)
|
|
{
|
|
if((*it)->GetName() == szName)
|
|
return(true);
|
|
}
|
|
|
|
return(false);
|
|
}
|
|
|
|
// ***************************************************************************
|
|
// Function: GetChild
|
|
// Desc: Get the nth element called szName
|
|
// ***************************************************************************
|
|
const Element&
|
|
ElementTag::GetChild(const char * szName, int nIndex) const
|
|
{
|
|
Elements::const_iterator it = children_.begin();
|
|
|
|
// Count the number of match to skip (less 1)
|
|
// Bug#0001: was ++nIndex instead of ++nCount
|
|
for(int nCount = 0; nCount < nIndex; ++nCount)
|
|
{
|
|
if(!FindChild(szName, it))
|
|
return nullElem;
|
|
++it;
|
|
}
|
|
|
|
// Last search (we skip nIndex - 1)
|
|
if(!FindChild(szName, it))
|
|
return nullElem;
|
|
|
|
return(**it);
|
|
|
|
}
|
|
|
|
// ***************************************************************************
|
|
// Function: GetAttributes
|
|
// Desc: return 0 if no attributes
|
|
// ***************************************************************************
|
|
const Attributes*
|
|
ElementTag::GetAttributes() const
|
|
{
|
|
return(attributes_.empty() ? 0 : &attributes_);
|
|
}
|
|
|
|
// ***************************************************************************
|
|
// Function: GetAttributeValue
|
|
// Desc: return "" if attribute doesn't exist
|
|
// ***************************************************************************
|
|
const std::string
|
|
ElementTag::GetAttributeValue(const std::string& attr) const
|
|
{
|
|
Attributes::const_iterator a_it = attributes_.find(attr);
|
|
if(a_it != attributes_.end())
|
|
return (*a_it).second;
|
|
else
|
|
return "";
|
|
}
|
|
|
|
// ***************************************************************************
|
|
// Function: Constructor
|
|
// Desc:
|
|
// ***************************************************************************
|
|
ElementSimple::ElementSimple(const std::string& strName)
|
|
: Element(strName)
|
|
{
|
|
}
|
|
|
|
// ***************************************************************************
|
|
// Function: AddChild
|
|
// Desc: Do nothing for ElementSimple ... no children.
|
|
// ***************************************************************************
|
|
bool
|
|
ElementSimple::AddChild(Element* /*pChild*/)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// ***************************************************************************
|
|
// Function: Destructor
|
|
// Desc: No children.
|
|
// ***************************************************************************
|
|
const Elements*
|
|
ElementSimple::GetChildren() const
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
// ***************************************************************************
|
|
// Function: GetChild
|
|
// Desc: Get the nth element called szName - return nullElem.
|
|
// ***************************************************************************
|
|
const Element&
|
|
ElementSimple::GetChild(const char * /*szName*/, int /*nIndex*/) const
|
|
{
|
|
return nullElem;
|
|
}
|
|
|
|
// ***************************************************************************
|
|
// Function: GetAttributes
|
|
// Desc: No attributes - return 0
|
|
// ***************************************************************************
|
|
const Attributes*
|
|
ElementSimple::GetAttributes() const
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
// ***************************************************************************
|
|
// Function: GetAttributeValue
|
|
// Desc: No attributes - return ""
|
|
// ***************************************************************************
|
|
const std::string ElementSimple::GetAttributeValue(const std::string&) const
|
|
{
|
|
return "";
|
|
}
|
|
|
|
// ***************************************************************************
|
|
// Function: Constructor
|
|
// Desc:
|
|
// ***************************************************************************
|
|
ElementComment::ElementComment(const std::string& strComment)
|
|
: ElementSimple("!")
|
|
{
|
|
AddValue(strComment);
|
|
}
|
|
|
|
// ***************************************************************************
|
|
// Function: Constructor
|
|
// Desc:
|
|
// ***************************************************************************
|
|
ElementNull::ElementNull()
|
|
: ElementSimple("")
|
|
{
|
|
}
|
|
|
|
// ***************************************************************************
|
|
// Function: IsNull
|
|
// Desc:
|
|
// ***************************************************************************
|
|
bool ElementNull::IsNull() const
|
|
{
|
|
return true;
|
|
}
|
|
|
|
} // end namespace
|
|
|