Back to index

lightning-sunbird  0.9+nobinonly
XMLElement.cpp
Go to the documentation of this file.
00001 // XMLElement.cpp : Implementation of CXMLElement
00002 #include "stdafx.h"
00003 //#include "Activexml.h"
00004 #include "XMLElement.h"
00005 
00006 
00007 CXMLElement::CXMLElement()
00008 {
00009        m_nType = 0;
00010        m_pParent = NULL;
00011 }
00012 
00013 
00014 CXMLElement::~CXMLElement()
00015 {
00016 }
00017 
00018 
00019 HRESULT CXMLElement::SetParent(IXMLElement *pParent)
00020 {
00021        // Note: parent is not refcounted
00022        m_pParent = pParent;
00023        return S_OK;
00024 }
00025 
00026 
00027 HRESULT CXMLElement::PutType(long nType)
00028 {
00029        m_nType = nType;
00030        return S_OK;
00031 }
00032 
00033 
00034 HRESULT CXMLElement::ReleaseAll()
00035 {
00036        // Release all children
00037        m_cChildren.clear();
00038        return S_OK;
00039 }
00040 
00041 
00043 // CXMLElement
00044 
00045 
00046 // Return the element's tag name
00047 HRESULT STDMETHODCALLTYPE CXMLElement::get_tagName(/* [out][retval] */ BSTR __RPC_FAR *p)
00048 {
00049        if (p == NULL)
00050        {
00051               return E_INVALIDARG;
00052        }
00053        USES_CONVERSION;
00054        *p = SysAllocString(A2OLE(m_szTagName.c_str()));
00055        return S_OK;
00056 }
00057 
00058 
00059 // Store the tag name
00060 HRESULT STDMETHODCALLTYPE CXMLElement::put_tagName(/* [in] */ BSTR p)
00061 {
00062        if (p == NULL)
00063        {
00064               return E_INVALIDARG;
00065        }
00066        USES_CONVERSION;
00067        m_szTagName = OLE2A(p);
00068        return S_OK;
00069 }
00070 
00071 
00072 // Returns the parent element
00073 HRESULT STDMETHODCALLTYPE CXMLElement::get_parent(/* [out][retval] */ IXMLElement __RPC_FAR *__RPC_FAR *ppParent)
00074 {
00075        if (ppParent == NULL)
00076        {
00077               return E_INVALIDARG;
00078        }
00079 
00080        *ppParent = NULL;
00081        if (m_pParent)
00082        {
00083               return m_pParent->QueryInterface(IID_IXMLElement, (void **) ppParent);
00084        }
00085 
00086        return S_OK;
00087 }
00088 
00089 
00090 // Set the specified attribute value
00091 HRESULT STDMETHODCALLTYPE CXMLElement::setAttribute(/* [in] */ BSTR strPropertyName, /* [in] */ VARIANT PropertyValue)
00092 {
00093        if (strPropertyName == NULL || PropertyValue.vt != VT_BSTR)
00094        {
00095               return E_INVALIDARG;
00096        }
00097 
00098        USES_CONVERSION;
00099        std::string szPropertyName = OLE2A(strPropertyName);
00100        std::string szPropertyValue = OLE2A(PropertyValue.bstrVal);
00101        m_cAttributes[szPropertyName] = szPropertyValue;
00102 
00103        return S_OK;
00104 }
00105 
00106 
00107 // Return the requested attribute
00108 HRESULT STDMETHODCALLTYPE CXMLElement::getAttribute(/* [in] */ BSTR strPropertyName, /* [out][retval] */ VARIANT __RPC_FAR *PropertyValue)
00109 {
00110        if (strPropertyName == NULL || PropertyValue == NULL)
00111        {
00112               return E_INVALIDARG;
00113        }
00114 
00115        USES_CONVERSION;
00116        std::string szPropertyName = OLE2A(strPropertyName);
00117        StringMap::iterator i = m_cAttributes.find(szPropertyName);
00118        if (i == m_cAttributes.end())
00119        {
00120               return S_FALSE;
00121        }
00122 
00123        PropertyValue->vt = VT_BSTR;
00124        PropertyValue->bstrVal = SysAllocString(A2OLE((*i).second.c_str()));
00125        return S_OK;
00126 }
00127 
00128 
00129 // Find and remove the specified attribute
00130 HRESULT STDMETHODCALLTYPE CXMLElement::removeAttribute(/* [in] */ BSTR strPropertyName)
00131 {
00132        if (strPropertyName == NULL)
00133        {
00134               return E_INVALIDARG;
00135        }
00136 
00137        USES_CONVERSION;
00138        std::string szPropertyName = OLE2A(strPropertyName);
00139        StringMap::iterator i = m_cAttributes.find(szPropertyName);
00140        if (i == m_cAttributes.end())
00141        {
00142               return E_INVALIDARG;
00143        }
00144        
00145        m_cAttributes.erase(i);
00146        
00147        return S_OK;
00148 }
00149 
00150 
00151 // Return the child collection for this element
00152 HRESULT STDMETHODCALLTYPE CXMLElement::get_children(/* [out][retval] */ IXMLElementCollection __RPC_FAR *__RPC_FAR *pp)
00153 {
00154        CXMLElementCollectionInstance *pCollection = NULL;
00155        CXMLElementCollectionInstance::CreateInstance(&pCollection);
00156        if (pCollection == NULL)
00157        {
00158               return E_OUTOFMEMORY;
00159        }
00160 
00161        // Add children to the collection
00162        for (ElementList::iterator i = m_cChildren.begin(); i != m_cChildren.end(); i++)
00163        {
00164               pCollection->Add(*i);
00165        }
00166 
00167        pCollection->QueryInterface(IID_IXMLElementCollection, (void **) pp);
00168 
00169        return S_OK;
00170 }
00171 
00172 
00173 HRESULT STDMETHODCALLTYPE CXMLElement::get_type(/* [out][retval] */ long __RPC_FAR *plType)
00174 {
00175        if (plType == NULL)
00176        {
00177               return E_INVALIDARG;
00178        }
00179        *plType = m_nType;
00180        return S_OK;
00181 }
00182 
00183 
00184 HRESULT STDMETHODCALLTYPE CXMLElement::get_text(/* [out][retval] */ BSTR __RPC_FAR *p)
00185 {
00186        return E_NOTIMPL;
00187 }
00188 
00189 
00190 HRESULT STDMETHODCALLTYPE CXMLElement::put_text(/* [in] */ BSTR p)
00191 {
00192        return E_NOTIMPL;
00193 }
00194 
00195 
00196 HRESULT STDMETHODCALLTYPE CXMLElement::addChild(/* [in] */ IXMLElement __RPC_FAR *pChildElem, long lIndex, long lReserved)
00197 {
00198        if (pChildElem == NULL)
00199        {
00200               return E_INVALIDARG;
00201        }
00202 
00203        // Set the child's parent to be this element
00204        ((CXMLElement *) pChildElem)->SetParent(this);
00205 
00206        if (lIndex < 0 || lIndex >= m_cChildren.size())
00207        {
00208               // Append to end
00209               m_cChildren.push_back(pChildElem);
00210        }
00211        else
00212        {
00213 // TODO              m_cChildren.insert(&m_cChildren[lIndex]);
00214               m_cChildren.push_back(pChildElem);
00215        }
00216 
00217        return S_OK;
00218 }
00219 
00220 
00221 HRESULT STDMETHODCALLTYPE CXMLElement::removeChild(/* [in] */ IXMLElement __RPC_FAR *pChildElem)
00222 {
00223        // TODO
00224        return E_NOTIMPL;
00225 }
00226