// XmlDocFragment.h
//
//////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2002 Robert Cunnings. All rights reserved.
//
// You may copy, modify, distribute or publish this code free of charge.
//
// NO WARRANTY: This software is provided AS IS, without warranty of any kind.
//
//////////////////////////////////////////////////////////////////////
//
// WMXMLDocFragment class - a class representing an XML fragment.
// Contains methods to serialize and deserialize element content
// to native types. Allows parsed XML to be queried using XPATH like syntax.
// 
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_XMLDOCFRAGMENT_H__CE664AFE_9841_4FCF_9368_37A155603AB9__INCLUDED_)
#define AFX_XMLDOCFRAGMENT_H__CE664AFE_9841_4FCF_9368_37A155603AB9__INCLUDED_

#include "ATLComTime.h"
#include "comdef.h"
#include <string>
#include <list>
#include <map>
using namespace std;

class WMXMLElement;

class WMXMLDocFragment
{
public:
    WMXMLDocFragment(void);
    WMXMLDocFragment(WMXMLElement& Root);
    ~WMXMLDocFragment(void);

    bool SerializeXML(string& strXML, bool bPrettyPrint = false);
    bool SerializeXML(wstring& wstrXML, bool bPrettyPrint = false);
    bool SerializeXML(BSTR *pbstrXML, bool bPrettyPrint = false);
    bool DeserializeXML(const char *pszXML, unsigned long ulLength);
    bool DeserializeXML(const string& strXML);
    bool DeserializeXML(const wstring& wstrXML);
    bool DeserializeXML(const BSTR bstrXML);


    WMXMLElement *CreateRootElem(   const wstring& wstrNamespace,
                                    const wstring& wstrPrefix,
                                    const wstring& wstrLocalName);
    WMXMLElement *CreateChildElem(  WMXMLElement *pParent,
                                    const wstring& wstrNamespace,
                                    const wstring& wstrPrefix,
                                    const wstring& wstrLocalName);

    // helpers to read element values using xpath notation
    bool SelectElems(const wstring& wstrPath,
                map<wstring, wstring>& mapNamespaces);

    bool SelectElems(WMXMLElement *pContext,
                const wstring& wstrPath,
                map<wstring, wstring>& mapNamespaces);

    WMXMLElement *NextElem();
    WMXMLElement *GetRootElem() { return m_pRoot; }
    void SetRootElem(WMXMLElement *pElem);

    static bool IsNilElement(WMXMLElement *pElem);
    static bool IsEmptyElement(WMXMLElement *pElem);

    static bool SerializeString(const wstring& wstrValue, WMXMLElement *pElem);
    static bool SerializeString(const string& strValue, WMXMLElement *pElem);
    static bool DeserializeString(wstring& wstrValue, WMXMLElement *pElem);
    static bool DeserializeString(string& strValue, WMXMLElement *pElem);
    static bool SerializeInt(int nValue, WMXMLElement *pElem);
    static bool DeserializeInt(int& nValue, WMXMLElement *pElem);
    static bool SerializeUInt(unsigned int uValue, WMXMLElement *pElem);
    static bool DeserializeUInt(unsigned int& uValue, WMXMLElement *pElem);
    static bool SerializeFloat(double fltValue, WMXMLElement *pElem);
    static bool DeserializeFloat(double& fltValue, WMXMLElement *pElem);
    static bool SerializeTime(COleDateTime& dt, WMXMLElement *pElem);
    static bool DeserializeTime(COleDateTime& dt, WMXMLElement *pElem);
    static bool SerializeDateTime(COleDateTime& dt, WMXMLElement *pElem);
    static bool DeserializeDateTime(COleDateTime& dt, WMXMLElement *pElem);
    static bool SerializeDecimal(_variant_t& decValue, WMXMLElement *pElem);
    static bool DeserializeDecimal(_variant_t& decValue, WMXMLElement *pElem);
    static bool SerializeBoolean(bool bValue, WMXMLElement *pElem);
    static bool DeserializeBoolean(bool& bValue, WMXMLElement *pElem);
    static bool SerializeBase64(const unsigned char *pValue, int nValueLength, WMXMLElement *pElem);
    static bool DeserializeBase64(unsigned char *pValue, int& nValueLength, WMXMLElement *pElem);
    static bool SerializeHexBinary(const unsigned char *pValue, int nValueLength, WMXMLElement *pElem);
    static bool DeserializeHexBinary(unsigned char *pValue, int& nValueLength, WMXMLElement *pElem);

protected:

    bool SelectElements(const wstring& wstrPath,
                        list<WMXMLElement *>& listElements,
                        map<wstring, wstring>& mapNamespaces);

    bool SelectElements(WMXMLElement *pContext,
                        const wstring& wstrPath,
                        list<WMXMLElement *>& listElements,
                        map<wstring, wstring>& mapNamespaces);
    // msg root
    WMXMLElement                        *m_pRoot;
    // select list
    list<WMXMLElement *>                m_listSelection;
    list<WMXMLElement *>::iterator      m_iterSelect;
};

#endif // !defined(AFX_XMLDOCFRAGMENT_H__CE664AFE_9841_4FCF_9368_37A155603AB9__INCLUDED_)
