📄 soapser.cpp
字号:
HRESULT CSoapSerializer::PushElement(WCHAR * pPrefix, WCHAR * pName, WCHAR * pnsURI)
{
HRESULT hr = S_OK;
CAutoP<CElementStackEntry> pele(NULL);
#ifndef UNDER_CE
CElementStackEntry * pCurrentHead(NULL);
#else
CElementStackEntry * pCurrentHead = NULL;
#endif
pele = new CElementStackEntry();
CHK_BOOL (pele != NULL, E_OUTOFMEMORY);
CHK (pele->Init());
CHK (pele->setName(pName)) ;
CHK (pele->setPrefix(pPrefix)) ;
CHK (pele->setURI(pnsURI));
pCurrentHead = PeekElement();
if (pCurrentHead)
{
pele->setDefaultNamespace(pCurrentHead->getDefaultNamespace());
}
else
{
pele->setDefaultNamespace(g_pwstrEmpty);
}
m_ElementStack.Push(pele.PvReturn());
Cleanup:
ASSERT(hr==S_OK);
return (hr);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// function: CSoapSerializer::PopElement()
//
// parameters:
//
// description:
//
// returns:
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////
CElementStackEntry * CSoapSerializer::PopElement(void)
{
return m_ElementStack.Pop();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// function: CSoapSerializer::PeekElement()
//
// parameters:
//
// description:
//
// returns:
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////
CElementStackEntry * CSoapSerializer::PeekElement(void)
{
return m_ElementStack.Peek();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// function: CSoapSerializer::FlushElement()
//
// parameters:
//
// description:
//
// returns:
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT CSoapSerializer::FlushElement(void)
{
HRESULT hr = S_OK;
CElementStackEntry * pEle = NULL;
pEle = PeekElement();
if ( (!pEle) || (pEle->getIsSerialized()) )
return (S_OK);
CHK (pEle->FixNamespace(&m_nsHelper));
CHK (pEle->FlushElement(this));
Cleanup:
ASSERT(hr==S_OK);
return (hr);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// function: CSoapSerializer::NamespaceHandler(WCHAR * prefix, WCHAR* ns_uri)
//
// parameters:
//
// description:
// this function takes a prefix and a namespace URI
// it returns the prefix matching the namespace URI or the input parameter
// prefix if the function did not pass in a uri
//
// after the function the namespace is registered
// returns:
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////
WCHAR * CSoapSerializer::NamespaceHandler(WCHAR * prefix, WCHAR* ns_uri)
{
HRESULT hr = S_OK;
WCHAR * pPrefix = NULL;
if ((!ns_uri) || (wcslen(ns_uri) == 0) )
{
// no namespace uri, whatever is in prefix will do
return prefix;
}
// there is a uri on the request. first lets see if this URI is
// already a URI of a given namespace
CNamespaceListEntry * pEntry = NULL;
while (pEntry = m_nsHelper.FindNamespace(ns_uri, pEntry))
{
// a namespace with this URI is already defined
if ( (prefix) && (wcslen(prefix) ) )
{
// there is also a prefix, is this prefix for the current
// call identical to the one defined in the namespace ?
if (wcscmp(prefix, pEntry->getPrefix()) == NULL)
{
// namespace and URI are already defined!!
return pEntry->getPrefix();
}
// the prefix(es) don't match
// perhaps there is another matching one ?
continue;
}
// we have a matching entry, but we do not have a prefix defined
// the prefix in the matching entry is the one we care about
return pEntry->getPrefix();
}
CHK ( m_nsHelper.AddNamespace(prefix, ns_uri) );
pPrefix = NamespaceHandler(prefix, ns_uri);
CHK (m_ElementStack.AddAttribute((WCHAR *)g_pwstrXmlns, (WCHAR*)g_pwstrEmpty, pPrefix, ns_uri) );
return pPrefix;
Cleanup:
ASSERT(hr==S_OK);
return (prefix);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// function: HRESULT CSoapSerializer::_WriterStartDocument(TCHAR *pchEncoding)
//
// parameters:
//
// description:
//
// returns:
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT CSoapSerializer::_WriterStartDocument(BSTR pchEncoding)
{
HRESULT hr = E_FAIL;
WCHAR *pchXMLStart1 = L"<?xml version=\"1.0\" encoding=\"";
WCHAR *pchXMLStart2 = L"\" standalone=\"no\"?>";
CHK(m_pEncodingStream->setEncoding(pchEncoding));
CHK (m_pEncodingStream->write(pchXMLStart1));
CHK (m_pEncodingStream->write(pchEncoding));
CHK (m_pEncodingStream->write(pchXMLStart2));
Cleanup:
ASSERT(hr==S_OK);
return (hr);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// function: HRESULT CSoapSerializer::_WriterEndDocument(void)
//
// parameters:
//
// description:
//
// returns:
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT CSoapSerializer::_WriterEndDocument(void)
{
HRESULT hr = S_OK;
// flush everything
m_pEncodingStream->flush();
if (m_bFaultOccured)
{
// in an error case set the error code
CHK (m_pEncodingStream->setFaultCode());
// for good measurement flush again
m_pEncodingStream->flush();
}
Cleanup:
return(hr);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// function: HRESULT CSoapSerializer::_WriterStartElement(....
//
// parameters:
//
// description:
//
// returns:
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT CSoapSerializer::_WriterStartElement(const wchar_t *pwchNamespaceUri,
int cchNamespaceUri,
const wchar_t * pwchLocalName,
int cchLocalName,
const wchar_t * pwchQName,
int cchQName)
{
HRESULT hr = E_FAIL;
WCHAR *pchXMLStart = L"<";
if (m_bElementOpen)
{
// end whatever was there before...
CHK (m_pEncodingStream->write((BSTR)g_pwstrClosing));
m_bElementOpen = false;
}
CHK (m_pEncodingStream->write(pchXMLStart));
CHK (m_pEncodingStream->write((BSTR)pwchQName));
m_bElementOpen = true;
Cleanup:
ASSERT(hr==S_OK);
return (hr);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// function: HRESULT CSoapSerializer::_WriterEndElement(const wchar_t *pwchNamespaceUri,
// int cchNamespaceUri, const wchar_t * pwchLocalName, int cchLocalName,
// const wchar_t * pwchQName, int cchQName)
//
//
// parameters:
//
// description:
//
// returns:
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT CSoapSerializer::_WriterEndElement(const wchar_t *pwchNamespaceUri,
int cchNamespaceUri,
const wchar_t * pwchLocalName,
int cchLocalName,
const wchar_t * pwchQName,
int cchQName)
{
HRESULT hr = E_FAIL;
WCHAR *pchXMLFullEnd = L"/>";
WCHAR *pchXMLStart = L"</";
if (!m_bElementOpen)
{
// need full element
CHK (m_pEncodingStream->write(pchXMLStart));
CHK (m_pEncodingStream->write((BSTR)pwchQName));
CHK (m_pEncodingStream->write((BSTR)g_pwstrClosing));
m_bElementOpen = true;
}
else
{
CHK (m_pEncodingStream->write(pchXMLFullEnd));
}
m_bElementOpen = false;
Cleanup:
ASSERT(hr==S_OK);
return (hr);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// function: HRESULT CSoapSerializer::_WriterCharacters(const WCHAR* pchChars, int cbChars)
//
// parameters:
//
// description:
//
// returns:
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT CSoapSerializer::_WriterCharacters(const WCHAR* pchChars, int cbChars)
{
HRESULT hr(S_OK);
#ifndef UNDER_CE
UINT cbIn, cbOut;
#endif
ULONG cTextGood = 0;
ULONG cEntityLen = 0;
const WCHAR * pwchTextWalk = pchChars;
WCHAR * pwchEntity = NULL;
WCHAR * pwchEntities = L"&<>'"";
if (m_bElementOpen)
{
// first end the element
CHK (m_pEncodingStream->write((BSTR)g_pwstrClosing));
m_bElementOpen = false;
}
while (cbChars--)
{
switch (*pwchTextWalk)
{
case '&':
pwchEntity = pwchEntities;
cEntityLen = 5;
break;
case '<':
pwchEntity = pwchEntities + 5;
cEntityLen = 4;
break;
case '>':
pwchEntity = pwchEntities + 9;
cEntityLen = 4;
break;
case '\'':
pwchEntity = pwchEntities + 13;
cEntityLen = 6;
break;
case '\"':
pwchEntity = pwchEntities + 19;
cEntityLen = 6;
break;
default:
cTextGood++;
}
if (pwchEntity)
{
CHK(m_pEncodingStream->write((BSTR)pchChars, cTextGood));
pchChars += cTextGood + 1;
cTextGood = 0;
CHK(m_pEncodingStream->write(pwchEntity, cEntityLen));
pwchEntity = NULL;
}
pwchTextWalk++;
}
CHK(m_pEncodingStream->write((BSTR)pchChars));
Cleanup:
ASSERT(hr==S_OK);
return (hr);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// function: HRESULT CSoapSerializer:: _WriterAddAttribute(WCHAR *pchURI,WCHAR *pchLocalName,
// WCHAR *pchQName, WCHAR *pchType, WCHAR *pchValue)
//
// parameters:
//
// description:
//
// returns:
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT CSoapSerializer:: _WriterAddAttribute(WCHAR *pchURI,WCHAR *pchLocalName, WCHAR *pchQName, WCHAR *pchType, WCHAR *pchValue)
{
HRESULT hr;
WCHAR *pchSpace=L" ";
WCHAR *pchEqual=L"=\"";
WCHAR *pchQuote=L"\"";
#ifndef UNDER_CE
int cbChars;
#endif
ASSERT(m_bElementOpen);
CHK (m_pEncodingStream->write(pchSpace));
CHK (m_pEncodingStream->write(pchQName));
CHK (m_pEncodingStream->write(pchEqual));
CHK (m_pEncodingStream->write(pchValue));
CHK (m_pEncodingStream->write(pchQuote));
Cleanup:
ASSERT(hr==S_OK);
return (hr);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -