📄 rssreaderxml.cpp
字号:
// RSSReaderXML.cpp: implementation of the CRSSReaderXML class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "RSSReaderXML.h"
#include "CRobotInternet.h"
#include <stdlib.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CRSSReaderXML::CRSSReaderXML()
{
m_bLoadLocal = FALSE;
}
CRSSReaderXML::~CRSSReaderXML()
{
}
//从本地缓冲中加载旧数据。
BOOL CRSSReaderXML::LoadLocal( CRSSChannel* &pChannel,CString szName,CString szGUID )
{
BOOL bCreateChannel = FALSE;
if( !pChannel )
{
pChannel = new CRSSChannel();
bCreateChannel = TRUE;
}
pChannel->SetCacheGUID( szGUID );
/// SendStatusMessage( pChannel,SM_LOADLOCAL_START);
m_bLoadLocal = TRUE;
if( !markup.Load( szGUID ) )
{
// SendStatusMessage( pChannel,SM_LOADXML_FAILED);
if( bCreateChannel ) delete( pChannel );
return FALSE;
}
//Step 2: Prase RSS Feed XML and Create All Element Objects.
BOOL bPraseOK = FALSE;
// SendStatusMessage( pChannel,SM_PRASEXML_START );
bPraseOK = Prase(pChannel);
if( bPraseOK )
{
// SendStatusMessage( pChannel,SM_PRASEXML_END );
/// SendStatusMessage( pChannel,SM_LOADLOCAL_END );
}
else
// SendStatusMessage( pChannel,SM_PRASEXML_FAILED);
if( !bPraseOK && !bCreateChannel ) delete( pChannel );
return bPraseOK;
}
//从频道服务器上下载频道Feed并解释数据。
BOOL CRSSReaderXML::Read(CRSSChannel* &pChannel, CString szURL )
{
BOOL bCreateChannel = FALSE;
if( !pChannel )
{
pChannel = new CRSSChannel();
pChannel->SetURL( szURL );
bCreateChannel = TRUE;
}
SendStatusMessage( pChannel,SM_REFRESH_START);
m_bLoadLocal = FALSE;
//开始从RSS Feeds源站点下载Feed.
SendStatusMessage( pChannel,SM_DOWNLOAD_START);
/*
MSXML2::IServerXMLHTTPRequestPtr pIXMLHTTPRequest ;
HRESULT hr;
try {
hr=pIXMLHTTPRequest.CreateInstance("Msxml2.ServerXMLHTTP.3.0");
SUCCEEDED(hr) ? 0 : throw hr;
pIXMLHTTPRequest->setTimeouts(10000,10000,120000,60000);
hr=pIXMLHTTPRequest->open("GET",_bstr_t(szURL), false);
pIXMLHTTPRequest->setRequestHeader("Content-Type", "text/xml");
SUCCEEDED(hr) ? 0 : throw hr;
hr=pIXMLHTTPRequest->send();
SUCCEEDED(hr) ? 0 : throw hr;
SendStatusMessage( pChannel,SM_DOWNLOAD_END); //下载成功.
}
catch (...)
{
SendStatusMessage( pChannel,SM_DOWNLOAD_FAILED,0); //下载失败.
if( bCreateChannel ) delete( pChannel );
return FALSE;
}
try{
markup.m_pDOMDoc = pIXMLHTTPRequest->responseXML;
markup.ResetPos();
}
catch(...)
{
SendStatusMessage( pChannel,SM_LOADXML_FAILED);
if( bCreateChannel ) delete( pChannel );
return FALSE;
}
*/
CRobotInternet internet;
CString szXMLBuffer,sErrMsg;
int nResult;
if( !pChannel->GetCallbackHwnd() )
szXMLBuffer = CRSSReaderXML::GetTempFile()+_T("tempfeed.xml");
else
szXMLBuffer = CRSSReaderXML::GetTempFile() +_T("tempfeed1.xml");
if( internet.httpGetFile( szURL,szXMLBuffer,nResult,sErrMsg) )
{
SendStatusMessage( pChannel,SM_DOWNLOAD_END); //下载成功.
}
else
{
SendStatusMessage( pChannel,SM_DOWNLOAD_FAILED,nResult); //下载失败.
if( bCreateChannel ) delete( pChannel );
return FALSE;
}
if( !markup.Load( szXMLBuffer ) )
{
SendStatusMessage( pChannel,SM_LOADXML_FAILED);
// SendStatusMessage( pChannel,SM_PRASEXML_FAILED); //测试BUG。
if( bCreateChannel ) delete( pChannel );
return FALSE;
}
BOOL bPraseOK = FALSE;
SendStatusMessage( pChannel,SM_PRASEXML_START );
bPraseOK = Prase(pChannel);
markup.Close();
if( bPraseOK )
{
pChannel->SetLastUpdateTime();
SendStatusMessage( pChannel,SM_PRASEXML_END );
SendStatusMessage( pChannel,SM_REFRESH_END );
}
else
{
SendStatusMessage( pChannel,SM_PRASEXML_FAILED);
}
if( !bPraseOK && !bCreateChannel ) delete( pChannel );
return bPraseOK;
}
BOOL CRSSReaderXML::Prase( CRSSChannel* pChannel)
{
if( !pChannel || markup.IsNull() ) return FALSE;
CString strOutput;
if( markup.FindElem( _T("rss") ))
{
pChannel->SetVersion( markup.GetAttrib(_T("version")) );
}
else if( markup.FindElem( _T("rdf:RDF")) )
{
pChannel->SetVersion( _T("1.0") );//markup.GetAttrib(_T("version")) );
}
else
{
//SendStatusMessage(pChannel,
return FALSE;
}
markup.IntoElem();
if(!markup.FindElem( _T("channel")) )
{
//SendStatusMessage(pChannel,
return FALSE;
}
markup.IntoElem();
while( markup.NextSlibingElem() )
{
// Element
strOutput = markup.GetTagName();
strOutput.MakeLower();
if ( strOutput == _T("title") )
{
pChannel->SetTitle(markup.GetData());
}
else if ( strOutput == _T("link") )
{
pChannel->SetLink(markup.GetData());
}
else if ( strOutput == _T("description") )
{
pChannel->SetDescription (markup.GetData());
}
else if ( strOutput == _T("copyright") )
{
pChannel->SetCopyright(markup.GetData());
}
else if ( strOutput == _T("language") )
{
pChannel->SetLanguage (markup.GetData());
}
else if( strOutput == _T("managingEditor") )
{
pChannel->SetManagingEditor(markup.GetData());
}
else if ( strOutput == _T("webmaster") )
{
pChannel->SetWebMaster(markup.GetData());
}
else if( strOutput == _T("rating") )
{
pChannel->SetRating( markup.GetData());
}
else if( strOutput == _T("docs") )
{
pChannel->SetDocs(markup.GetData());
}
else if ( strOutput == _T("lastbuilddate") )
{
CStdDate* pDate = new CStdDate();
pDate->SetDateTime(markup.GetData());
pChannel->SetLastBuildDate( pDate );
}
else if ( strOutput == _T("pubdate") )
{
CStdDate* pDate = new CStdDate();
pDate->SetDateTime(markup.GetData());
pChannel->SetPubDate( pDate );
}
else if ( strOutput == _T("ttl") )
{
CString szTemp = markup.GetData();
pChannel->SetTTL( atoi( (LPTSTR)(LPCTSTR)szTemp ) );
}
else if ( strOutput == _T("generator") )
{
pChannel->SetGenerator(markup.GetData());
}
else if ( strOutput == _T("image") )
{
pChannel->SetImage( PraseImage() );
}
else if ( strOutput == _T("item") )
{
pChannel->AddItem( PraseItem(pChannel) );
}
else if( strOutput == _T("cloud") )
{
pChannel->SetCloud( PraseCloud() );
}
else if( strOutput == _T("textinput" ) )
{
pChannel->SetTextInput( PraseTextInput() );
}
else if( strOutput == _T("Category") )
{
pChannel->AddCategory( PraseCategory() );
}
else if( strOutput == _T("skiphours") )
{
PraseSkipHours(pChannel);
}
else if( strOutput == _T("skipdays"))
{
PraseSkipDays( pChannel);
}
}
markup.OutOfElem();
while( markup.NextSlibingElem() )
{
strOutput = markup.GetTagName();
strOutput.MakeLower();
strOutput.MakeLower();
if( strOutput == _T("item") && pChannel)
{
pChannel->AddItem( PraseItem(pChannel) );
}
}
return TRUE;
}
CRSSItem* CRSSReaderXML::PraseItem( CRSSChannel* pChannel )
{
markup.ResetChildPos();
markup.IntoElem();
CString strOutput;
CRSSItem* pItem = new CRSSItem(pChannel);
ASSERT(pItem);
while( markup.NextSlibingElem() )
{
strOutput = markup.GetTagName();
strOutput.MakeLower();
if ( strOutput == _T("title") )
{
pItem->SetTitle(markup.GetData());
}
else if ( strOutput == _T("description") )
{
pItem->SetDescription(markup.GetData());
}
else if ( strOutput == _T("link") )
{
pItem->SetLink( markup.GetData());
}
else if ( strOutput == _T("author") )
{
pItem->SetAuthor(markup.GetData());
}
else if ( strOutput == _T("category") )
{
pItem->AddCategory( PraseCategory() );
}
else if ( strOutput == _T("pubdate") )
{
CStdDate* pDate = new CStdDate();
ASSERT(pDate);
pDate->SetDateTime(markup.GetData());
pItem->SetPubDate( pDate );
}
else if ( strOutput == _T("comments") )
{
pItem->SetComments(markup.GetData());
}
else if( strOutput == _T("enclosure"))
{
pItem->SetEnclosure( PraseEnclosure() );
}
else if( strOutput == _T("guid") )
{
pItem->SetGuid( PraseGuid());
}
else if( strOutput == _T("source"))
{
pItem->SetSource( PraseSource() );
}
else if( strOutput == _T("readstatus") )
{
CString szTemp = markup.GetData();
if( szTemp.CompareNoCase(_T("true"))==0)
pItem->SetReadStatus( TRUE );
else
pItem->SetReadStatus( FALSE );
}
else if( strOutput == _T("redflag"))
{
CString szTemp = markup.GetData();
if( szTemp.CompareNoCase(_T("true"))==0)
pItem->SetRedFlag( TRUE );
else
pItem->SetRedFlag( FALSE );
}
else if( strOutput == _T("retakedate") )
{
CStdDate* pDate = new CStdDate();
ASSERT(pDate);
pDate->SetDateTime(markup.GetData());
pItem->SetRetakeDate( pDate );
}
}
markup.OutOfElem();
return pItem;
}
CRSSImage* CRSSReaderXML::PraseImage()
{
CString strOutput;
markup.ResetChildPos();
markup.IntoElem();
CRSSImage* pImage = new CRSSImage();
ASSERT(pImage);
while( markup.NextSlibingElem() )
{
strOutput = markup.GetTagName();
strOutput.MakeLower();
if ( strOutput == _T("title") )
{
pImage->SetTitle(markup.GetData());
}
else if ( strOutput == _T("url") )
{
pImage->SetURL(markup.GetData());
}
else if ( strOutput == _T("link") )
{
pImage->SetLink(markup.GetData());
}
else if ( strOutput == _T("width") )
{
CString szTemp = markup.GetData();
pImage->SetWidth( atoi( (LPTSTR)(LPCTSTR)szTemp) );
}
else if ( strOutput == _T("height") )
{
CString szTemp = markup.GetData();
pImage->SetHeight( atoi( (LPTSTR)(LPCTSTR)szTemp) );
}
else if ( strOutput == _T("description") )
{
pImage->SetDescription(markup.GetData());
}
}
markup.OutOfElem();
return pImage;
}
CRSSCloud* CRSSReaderXML::PraseCloud()
{
CRSSCloud* pCloud = new CRSSCloud();
ASSERT(pCloud);
pCloud->SetDomain( markup.GetAttrib(_T("domain") ));
pCloud->SetPath( markup.GetAttrib(_T("path")) );
CString szValue = markup.GetAttrib(_T("port"));
if( !szValue.IsEmpty() )
pCloud->SetPort( atoi( (LPTSTR)(LPCTSTR)szValue ) );
pCloud->SetProtocal( markup.GetAttrib(_T("protocal") ));
pCloud->SetRegisterProcedure( markup.GetAttrib(_T("registerprocedure")) );
return pCloud;
}
CRSSEnclosure* CRSSReaderXML::PraseEnclosure()
{
CRSSEnclosure* pEnclosure = new CRSSEnclosure();
ASSERT(pEnclosure);
pEnclosure->SetURL( markup.GetAttrib(_T("url")) );
pEnclosure->SetType( markup.GetAttrib(_T("type")));
CString szValue = markup.GetAttrib(_T("length"));
if( !szValue.IsEmpty() )
pEnclosure->SetLength( atoi( (LPTSTR)(LPCTSTR)szValue ) );
return pEnclosure;
}
CRSSGuid* CRSSReaderXML::PraseGuid()
{
CRSSGuid* pGuid = new CRSSGuid();
ASSERT(pGuid);
pGuid->SetText(markup.GetData());
pGuid->SetIsPermaLink( markup.GetAttrib(_T("IsPermaLink")) );
return pGuid;
}
CRSSSource* CRSSReaderXML::PraseSource()
{
CRSSSource* pSource = new CRSSSource();
ASSERT(pSource);
pSource->SetText(markup.GetData());
pSource->SetURL( markup.GetAttrib(_T("url")) );
return pSource;
}
CRSSTextInput* CRSSReaderXML::PraseTextInput()
{
markup.ResetChildPos();
CString strOutput;
CRSSTextInput* pInput = new CRSSTextInput();
ASSERT(pInput);
markup.IntoElem();
while( markup.NextSlibingElem() )
{
strOutput = markup.GetTagName();
strOutput.MakeLower();
if ( strOutput == _T("title") )
{
pInput->SetTitle(markup.GetData() );
}
else if ( strOutput == _T("description") )
{
pInput->SetDescription(markup.GetData());
}
else if ( strOutput == _T("name") )
{
pInput->SetText(markup.GetData());
}
else if ( strOutput == _T("link") )
{
pInput->SetURL(markup.GetData());
}
}
markup.OutOfElem();
return pInput;
}
CRSSCategory* CRSSReaderXML::PraseCategory()
{
CRSSCategory* pcategory = new CRSSCategory();
ASSERT(pcategory);
pcategory->SetText( markup.GetData() );
pcategory->SetDomain( markup.GetAttrib(_T("domain")) );
return pcategory;
}
void CRSSReaderXML::PraseSkipHours( CRSSChannel* pChannel)
{
if( !pChannel ) return;
pChannel->SetSkipHours(markup.GetData());
}
void CRSSReaderXML::PraseSkipDays( CRSSChannel* pChannel)
{
if( !pChannel ) return;
pChannel->SetSkipDays( markup.GetData());
}
CString CRSSReaderXML::GetTempFile()
{
TCHAR szTempPath [MAX_PATH];
DWORD dwResult=:: GetTempPath (MAX_PATH, szTempPath);
ASSERT (dwResult);
//Create a unique temporary file.
TCHAR szTempFile [MAX_PATH];
UINT nResult=GetTempFileName (szTempPath, _T ("~ex"),0,szTempFile);
ASSERT (nResult);
return (LPCTSTR)szTempPath;
}
void CRSSReaderXML::SendStatusMessage(CRSSChannel *pChannel, int iCode, int iSubCode)
{
HWND hwndCall;
if( pChannel->GetCallbackHwnd() )
hwndCall = pChannel->GetCallbackHwnd();
else
hwndCall = m_hCallbackWnd;
if( hwndCall )
{
WPARAM wParam = MAKEWPARAM((WORD)iCode,(WORD)iSubCode);
::SendMessage( hwndCall,WM_RSS_READ_STATUS,wParam,(LPARAM)pChannel);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -