208 lines
4.6 KiB
C++

// Copyright (C) 2004 Intel Corporation
// defines an XML utility interface
#ifndef __CIMXMLUTILS_H
#define __CIMXMLUTILS_H
#include <string>
#include <map>
#include <vector>
#include "XMLTypes.h"
#include "Exception.h"
using namespace std;
using namespace CimXMLTypesNamespace;
using namespace WsmanExceptionNamespace;
namespace CimXMLUtilsNamespace
{
// function declarations
// this function should be called once at the beginning of the program
// enableSchemaValidation will enable schema validation. To enable
// validation from xsd file, set validateFromFile. To set the path where
// search for xsd files that are provided (in the XMLDoc constructor)
// set xsdPath.
// (The xsd file name will be appended to the provided xsdPath, as is.)
// *** Schema Validation has not been implemented
void InitXMLLibrary(const bool enableSchemaValidation = false,
const bool validateFromFile = false, const string& xsdPath = "");
// this function should be called once at the end of the program
void TerminateXMLLibrary();
// Exception class, encapsulates exceptions thrown by the XML lib
// used to implement the XML utility interface
class CIMXMLException : public GeneralWsmanException
{
public:
CIMXMLException(const char* message)
:GeneralWsmanException(message, WSMAN_XML_ERROR){}
virtual ~CIMXMLException() throw() {}
};
// forward class declaration
class XMLElementImpl;
// XMLElement represents an XML element node
class XMLElement
{
friend class XMLDocImpl;
friend class XMLElementImpl;
private:
XMLElementImpl* impl;
XMLElement(XMLElementImpl* i);
public:
XMLElement(const XMLElement& other);
~XMLElement();
XMLElement& operator=(const XMLElement &other );
XMLElement CreateChildNode(const string& nodeName,
const string& ns,
const string& prefix,
const string* text = NULL);
void CreateLeafNode(const string& nodeName,
const string& ns,
const string& prefix,
const string& nodeValue);
void AddText(const string& nodeValue);
bool HasNextSibling() const;
XMLElement GetNextSibling() const;
bool HasChildren() const;
bool IsLeafNode() const;
XMLElement GetFirstChild() const;
string GetNodeName() const;
string GetTextValue() const;
string ToString(bool incRoot = false) const;
string GetNSUri() const;
void GetAttributes(map<string, string>& attribs) const;
string GetAttribValue(const string& name) const;
void AddAttribValue(const string& name, const string& value);
void AddNSDefinition(const string& ns, const string* prefix = NULL);
template <class T>
void AppendLeaf(const string& name,
const T& t,
const string& ns = "",
const string& prefix = "")
{
CreateLeafNode(name, ns, prefix,
CimXMLTypesNamespace::XMLTypeConverter::TypeToString(t));
}
template <class T>
void AppendLeafArray(const string& name,
const vector<T>& vec,
const string& ns = "",
const string& prefix = "")
{
for(unsigned int i = 0 ; i < vec.size(); i++)
{
AppendLeaf(name, vec[i], ns, prefix);
}
}
template <class T>
void AppendNode(const string& name,
const T& t,
const string& ns = "",
const string& prefix = "")
{
XMLElement e = CreateChildNode(name, ns, prefix);
t.SerializeMembers(e);
}
template <class T>
void AppendNodeArray(const string& name,
const vector<T>& vec,
const string& ns = "",
const string& prefix = "")
{
for(unsigned int i = 0 ; i < vec.size(); i++)
{
AppendNode(name, vec[i], ns, prefix);
}
}
template <class T>
void SetValue(T& t) const
{
CimXMLTypesNamespace::
XMLTypeConverter::
StringToType(GetTextValue(),t);
}
template <class T>
void AddValue(vector<T>& vec) const
{
T t;
CimXMLTypesNamespace::
XMLTypeConverter::
StringToType(GetTextValue(),t);
vec.push_back(t);
}
template <class T>
void SetComplexType(T& t) const
{
t.Deserialize(*this);
}
template <class T>
void AddComplexType(vector<T>& vec) const
{
T t;
t.Deserialize(*this);
vec.push_back(t);
}
};
class XMLDocImpl;
// XML Doc represents an XML document
class XMLDoc
{
private:
XMLDocImpl* impl;
XMLDoc(const XMLDoc& xml);
XMLDoc& operator=(const XMLDoc &other );
public:
// parse an XML doc, perform schema validation using xsdFile, if provided
XMLDoc(const string& xml, const char* xsdFile = NULL);
// create an xml doc with the given root element
XMLDoc(const char* rootName = "root",
const char* uri = NULL,
const char* prefix = NULL);
~XMLDoc();
XMLElement GetRootNode();
string ToString(bool incVersionStr = false);
};
} //CimXMLUtilsNamespace
#endif