📄 resorgsymbolsdoc.cpp
字号:
{
CString sLine;
if (!f.ReadString(sLine) )
{
break;
}
sStyleSheetText += sLine + _T("\r\n");
}
f.Close();
}
catch (CFileException* pError)
{
LPTSTR pszErrorMsg = sErrorMsg.GetBuffer(_MAX_PATH);
::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
pError->m_lOsError,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
pszErrorMsg,
_MAX_PATH,
NULL);
sErrorMsg.ReleaseBuffer();
pError->Delete();
}
}
ASSERT(!sStyleSheetText.IsEmpty() );
if (!sStyleSheetText.IsEmpty() )
{
if (m_sTemporaryReportPathName.IsEmpty() )
{
m_sTemporaryReportPathName = GetTemporaryReportName();
}
ASSERT(!m_sTemporaryReportPathName.IsEmpty() );
if (!m_sTemporaryReportPathName.IsEmpty() )
{
CString sErrorMsg;
if (CreateReport(sStyleSheetText, m_sTemporaryReportPathName, &sErrorMsg) )
{
//static_cast<CResOrgCoreApp*>( ::AfxGetApp() )->OpenHtmlView( GetTitle() + _T(" - Report"), _T("file://") + m_sTemporaryReportPathName);
static_cast<CResOrgCoreApp*>( ::AfxGetApp() )->OpenHtmlView( GetTitle() + _T(" - Report"), m_sTemporaryReportPathName);
}
else
{
::AfxMessageBox(sErrorMsg, MB_OK | MB_ICONSTOP);
}
}
}
}
}
bool CResOrgSymbolsDoc::CreateReport(const CString& sStyleSheetText,
const CString& sPathName,
CString* psMsg /*= NULL*/)
{
bool bResult = false;
CString sErrorMsg;
CString sReportText = GenerateReportText(sStyleSheetText, psMsg);
if (!sReportText.IsEmpty() )
{
try
{
CStdioFile f(sPathName, CFile::modeCreate | CFile::modeWrite);
f.WriteString(sReportText);
f.Close();
bResult = true;
}
catch (CFileException* pError)
{
LPTSTR pszErrorMsg = sErrorMsg.GetBuffer(_MAX_PATH);
::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
pError->m_lOsError,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
pszErrorMsg,
_MAX_PATH,
NULL);
sErrorMsg.ReleaseBuffer();
pError->Delete();
}
}
return bResult;
}
CString CResOrgSymbolsDoc::GenerateReportText(const CString& sStyleSheetText, CString* psMsg /*= NULL*/) const
{
CString sReportText, sErrorMsg;
try
{
MSXML2::IXMLDOMDocumentPtr ptrDOMDoc;
MSXML2::IXMLDOMDocumentPtr ptrXSLTDOMDoc;
// Create empty DOM
::CoInitialize(NULL);
HRESULT hr = ptrDOMDoc.CreateInstance("Msxml2.DOMDocument.4.0");
if (FAILED(hr) )
{
sErrorMsg.LoadString(IDP_REPORT_ERR_XML_DOM_CREATE);
throw new CUserException();
}
hr = ptrXSLTDOMDoc.CreateInstance("Msxml2.DOMDocument.4.0");
if (FAILED(hr) )
{
sErrorMsg.LoadString(IDP_REPORT_ERR_XSLT_DOM_CREATE);
throw new CUserException();
}
ptrXSLTDOMDoc->async = false;
ptrXSLTDOMDoc->loadXML( (LPCTSTR)sStyleSheetText);
sReportText = GenerateReport( (IXMLDOMDocument*)ptrDOMDoc.GetInterfacePtr(),
(IXMLDOMDocument*)ptrXSLTDOMDoc.GetInterfacePtr(),
psMsg);
}
catch(_com_error& e)
{
HRESULT hr = e.Error();
TRACE( _T("Unable to display report. Error code 0x%08x"), hr);
if (NULL != psMsg)
{
psMsg->Format( _T("CResOrgSymbolsDoc::GenerateReportText()\nException 0x%08x"), hr);
}
}
catch(CUserException* e)
{
if (NULL != psMsg)
{
psMsg->Format( _T("CResOrgSymbolsDoc::GenerateReportText()\n%s"), sErrorMsg);
}
e->Delete();
}
return sReportText;
}
CString CResOrgSymbolsDoc::GenerateReport( IXMLDOMDocument* pXmlDoc,
IXMLDOMDocument* pStyleSheetDoc,
CString* psMsg /*= NULL*/) const
{
MSXML2::IXMLDOMDocumentPtr ptrXmlDoc(pXmlDoc);
MSXML2::IXMLDOMDocumentPtr ptrStyleSheetDoc(pStyleSheetDoc);
CString sErrorMsg;
CString sReportText;
try
{
// tell our shape to save its state as XML (into ptrDOMDoc)
HRESULT hr = SaveXml( (IXMLDOMDocument*)ptrXmlDoc.GetInterfacePtr(), &sErrorMsg);
if (FAILED(hr) )
{
sErrorMsg = _T("Failed to generate XML stream - ") + sErrorMsg;
throw new CUserException();
}
ptrStyleSheetDoc->async = false;
// Perform the transformation
sReportText = (LPCTSTR)ptrXmlDoc->transformNode(ptrStyleSheetDoc);
}
catch (CUserException* pError)
{
// Nothing to do here - we already know what's wrong
pError->Delete();
}
if (sReportText.IsEmpty())
{
if ( (NULL != psMsg) && !sErrorMsg.IsEmpty() )
{
*psMsg = sErrorMsg;
}
TRACE("ERROR in CResOrgSymbolsDoc::GenerateReport(): %s\n", sErrorMsg);
}
return sReportText;
}
bool CResOrgSymbolsDoc::WriteXml(const CString& sPathName, CString* psMsg /*= NULL*/) const
{
ASSERT(!sPathName.IsEmpty() );
bool bResult = false;
CString sErrorMsg;
ASSERT(NULL != m_pSymbolManager);
if (!sPathName.IsEmpty() && (NULL != m_pSymbolManager) )
{
try
{
MSXML2::IXMLDOMDocumentPtr ptrDOMDoc;
// Create empty DOM
::CoInitialize(NULL);
HRESULT hr = ptrDOMDoc.CreateInstance("Msxml2.DOMDocument.4.0");
if (FAILED(hr) )
{
//sErrorMsg.LoadString(IDP_RESORG_ERR_XML_DOM_CREATE);
sErrorMsg = _T("Failed to create XML DOM");
throw new CUserException();
}
hr = SaveXml( (IXMLDOMDocument*)ptrDOMDoc.GetInterfacePtr(), &sErrorMsg);
if (FAILED(hr) )
{
sErrorMsg = _T("Failed to save XML stream - ") + sErrorMsg;
throw new CUserException();
}
try
{
_variant_t varPathName = (LPCTSTR)sPathName;
ptrDOMDoc->save(varPathName);
bResult = true;
}
catch(_com_error& e)
{
sErrorMsg = (LPCTSTR)e.Description();
}
}
catch (CUserException* pError)
{
// Nothing to do here - we already know what's wrong
pError->Delete();
}
}
if (!bResult)
{
if ( (NULL != psMsg) && !sErrorMsg.IsEmpty() )
{
*psMsg = sErrorMsg;
}
TRACE( _T("ERROR in CResOrgSymbolsDoc::WriteXml(): %s\n"), sErrorMsg);
}
return bResult;
}
HRESULT CResOrgSymbolsDoc::SaveXml( IXMLDOMDocument* pDOMDoc,
CString* psMsg /*= NULL*/) const
{
HRESULT hr = E_FAIL;
CString sErrorMsg;
ASSERT(NULL != m_pSymbolManager);
if (NULL != m_pSymbolManager)
{
try
{
hr = S_OK;
MSXML2::IXMLDOMDocumentPtr ptrDOMDoc(pDOMDoc);
// Add xml version node
CString sAttribs = _T("version=\"1.0\" encoding=\"iso-8859-1\"");
MSXML2::IXMLDOMProcessingInstructionPtr ptrIns;
ptrIns = ptrDOMDoc->createProcessingInstruction(_T("xml"), (LPCTSTR)sAttribs);
ptrDOMDoc->appendChild(ptrIns);
// Create root element
MSXML2::IXMLDOMElementPtr ptrRoot = ptrDOMDoc->createElement(_T("ResOrgExportFile") );
ptrDOMDoc->appendChild(ptrRoot);
CResOrgXmlWriter writer;
hr = writer.SaveXml( m_pSymbolManager,
(IXMLDOMDocument*)ptrDOMDoc.GetInterfacePtr(),
(IXMLDOMElement*)ptrRoot.GetInterfacePtr(),
&sErrorMsg);
MSXML2::IXMLDOMElementPtr ptrApplication = ptrDOMDoc->createElement( _T("Application") );
MSXML2::IXMLDOMAttributePtr ptrAppNameAttr = ptrDOMDoc->createAttribute(_T("name"));
ptrAppNameAttr->value = _T("ResOrg");
ptrApplication->setAttributeNode(ptrAppNameAttr);
ptrRoot->appendChild(ptrApplication);
CNGModuleVersion ver;
ver.GetFileVersionInfo();
MSXML2::IXMLDOMElementPtr ptrName = ptrDOMDoc->createElement( _T("Name") );
ptrName->text = (LPCTSTR)ver.GetValue( _T("ProductName") );
ptrApplication->appendChild(ptrName);
MSXML2::IXMLDOMElementPtr ptrVersion = ptrDOMDoc->createElement( _T("Version") );
ptrVersion->text = (LPCTSTR)ver.GetValue( _T("FileVersion") );
ptrApplication->appendChild(ptrVersion);
MSXML2::IXMLDOMElementPtr ptrWebUrl = ptrDOMDoc->createElement( _T("WebUrl") );
ptrWebUrl->text = (LPCTSTR)Options.GetWebURL();
ptrApplication->appendChild(ptrWebUrl);
MSXML2::IXMLDOMElementPtr ptrTimestamp = ptrDOMDoc->createElement( _T("Timestamp") );
CTime t = CTime::GetCurrentTime();
CString sTimestamp = t.Format( _T("%A, %B %d, %Y, %H:%M") );
ptrTimestamp->text = (LPCTSTR)sTimestamp;
ptrRoot->appendChild(ptrTimestamp);
if (FAILED(hr) )
{
sErrorMsg = _T("Failed to save XML stream - ") + sErrorMsg;
throw new CUserException();
}
}
catch(_com_error& e)
{
hr = e.Error();
}
}
if (FAILED(hr) )
{
if ( (NULL != psMsg) && !sErrorMsg.IsEmpty() )
{
*psMsg = sErrorMsg;
}
TRACE( _T("ERROR in CResOrgSymbolsDoc::SaveXml(): %s\n"), sErrorMsg);
}
return hr;
}
CString CResOrgSymbolsDoc::LoadXsl(UINT uResourceID) const
{
CString sTemplate;
CString sResType = _T("XSL");
HINSTANCE hInst = ::AfxFindResourceHandle(MAKEINTRESOURCE(uResourceID), sResType);
if (NULL != hInst)
{
HRSRC hRsrc = ::FindResource(hInst, MAKEINTRESOURCE(uResourceID), sResType);
if (NULL != hRsrc)
{
HGLOBAL hGlobal = ::LoadResource(hInst, hRsrc);
LPBYTE pBuffer = (LPBYTE)::LockResource(hGlobal);
sTemplate = (LPCTSTR)pBuffer;
}
}
return sTemplate;
}
CString CResOrgSymbolsDoc::GetTemporaryReportName(void) const
{
CString sPathName;
TCHAR szFolderPath[_MAX_PATH + 1];
::GetTempPath(_MAX_PATH, szFolderPath);
LPTSTR pszPathName = sPathName.GetBuffer(_MAX_PATH);
if (0 != ::GetTempFileName( szFolderPath,
_T("HTM"),
0,
pszPathName) )
{
sPathName.ReleaseBuffer();
PathRemoveExtension(sPathName);
PathAddExtension(sPathName, _T(".html") );
}
return sPathName;
}
void CResOrgSymbolsDoc::DeleteTemporaryReport(void)
{
if (!m_sTemporaryReportPathName.IsEmpty() )
{
// Delete temporary file if it exists
::_tremove(m_sTemporaryReportPathName);
m_sTemporaryReportPathName = _T("");
}
}
BOOL CResOrgSymbolsDoc::PathAddExtension(CString& rsPathName, const CString& sExtension) const
{
LPTSTR pszPathName = rsPathName.GetBuffer(_MAX_PATH);
BOOL bResult = ::PathAddExtension(pszPathName, sExtension);
rsPathName.ReleaseBuffer();
return bResult;
}
void CResOrgSymbolsDoc::PathRemoveExtension(CString& rsPathName) const
{
LPTSTR pszPathName = rsPathName.GetBuffer(_MAX_PATH);
::PathRemoveExtension(pszPathName);
rsPathName.ReleaseBuffer();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -