📄 aspparser.cpp
字号:
/********************************************************************/
/* */
/* AspParser.cpp */
/* */
/* Implementation of the CAspParser class. */
/* */
/* Programmed by Pablo van der Meer */
/* http://www.pablovandermeer.nl */
/* */
/* Last updated: 22 february 2003 */
/* */
/********************************************************************/
#include "stdafx.h"
#include "demo.h"
#include "AspParser.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// Script Engine CLSIDs...
#include <initguid.h>
DEFINE_GUID(CLSID_VBScript, 0xb54f3741, 0x5b07, 0x11cf, 0xa4, 0xb0, 0x0,
0xaa, 0x0, 0x4a, 0x55, 0xe8);
DEFINE_GUID(CLSID_JScript, 0xf414c260, 0x6ac0, 0x11cf, 0xb6, 0xd1, 0x00,
0xaa, 0x00, 0xbb, 0xbb, 0x58);
IMPLEMENT_DYNCREATE(CAspParser, CScriptEngine)
CAspParser::CAspParser()
{
m_pRequestObject = NULL;
m_pResponseObject = NULL;
}
CAspParser::~CAspParser()
{
}
/********************************************************************/
/* */
/* Function name : OnGetItemInfo */
/* Description : Allows the scripting engine to obtain */
/* information about an item added with the */
/* IActiveScript::AddNamedItem method. */
/* */
/********************************************************************/
HRESULT CAspParser::OnGetItemInfo(LPCOLESTR pstrName, DWORD dwReturnMask, IUnknown** ppUnknownItem, ITypeInfo** ppTypeInfo)
{
HRESULT hResult = S_OK;
CString strName(pstrName);
// valid ppUnknownItem pointer?
if (ppUnknownItem != NULL)
{
*ppUnknownItem = NULL;
}
// valid ppTypeInfo pointer ?
if (ppTypeInfo != NULL)
{
*ppTypeInfo = NULL;
}
// Check for Response object
if (wcsicmp(pstrName, L"Response") == 0)
{
// get the document's dispatch interface
IDispatch* pDispatch = m_pResponseObject->GetIDispatch(FALSE);
// is script engine looking for an IUnknown for our object?
if (dwReturnMask & SCRIPTINFO_IUNKNOWN)
{
// Provide 'Response' object.
*ppUnknownItem = (IUnknown*)m_pResponseObject->GetIDispatch(TRUE);
}
// is script engine expecting an ITypeInfo?
if (dwReturnMask & SCRIPTINFO_ITYPEINFO)
{
// default to NULL
*ppTypeInfo = NULL;
}
}
else
// Check for Request object
if (wcsicmp(pstrName, L"Request") == 0)
{
// get the document's dispatch interface
IDispatch* pDispatch = m_pRequestObject->GetIDispatch(FALSE);
// is script engine looking for an IUnknown for our object?
if (dwReturnMask & SCRIPTINFO_IUNKNOWN)
{
// Provide 'Request' object.
*ppUnknownItem = (IUnknown*)m_pRequestObject->GetIDispatch(TRUE);
}
// ss script engine expecting an ITypeInfo?
if (dwReturnMask & SCRIPTINFO_ITYPEINFO)
{
// default to NULL
*ppTypeInfo = NULL;
}
}
else
{
CString strError;
strError.Format("The script engine asked for information\nfor an unknown object named \"%s\".", (LPCSTR)strName);
AfxMessageBox(strError, MB_ICONSTOP | MB_OK);
hResult = E_UNEXPECTED;
}
return hResult;
}
/********************************************************************/
/* */
/* Function name : Initialize */
/* Description : Initialize ASP parser */
/* */
/********************************************************************/
BOOL CAspParser::Initialize()
{
if (!Create(CLSID_VBScript))
return FALSE;
m_pRequestObject = new CRequestObject;
m_pResponseObject = new CResponseObject;
// Add an IResponse item to the engine's name space...
if (!AddNamedItem(L"Response", SCRIPTITEM_ISVISIBLE | SCRIPTITEM_ISPERSISTENT))
return FALSE;
// Add an IRequest item to the engine's name space...
if (!AddNamedItem(L"Request", SCRIPTITEM_ISVISIBLE | SCRIPTITEM_ISPERSISTENT))
return FALSE;
return TRUE;
}
/********************************************************************/
/* */
/* Function name : CleanUp */
/* Description : Clean up */
/* */
/********************************************************************/
void CAspParser::CleanUp()
{
if (m_pRequestObject)
{
delete m_pRequestObject;
}
m_pRequestObject = NULL;
if (m_pResponseObject)
{
delete m_pResponseObject;
}
m_pResponseObject = NULL;
CScriptEngine::CleanUp();
}
/********************************************************************/
/* */
/* Function name : Execute */
/* Description : Execute provided script */
/* */
/********************************************************************/
BOOL CAspParser::Execute(CString &strScript)
{
CString strResult;
if (!ParseInputText(strScript, strResult))
return FALSE;
// clear the response buffer
m_pResponseObject->Clear();
// Parse the code script
EXCEPINFO ei;
BSTR pParseText = strResult.AllocSysString();
if (ParseScriptText(pParseText, NULL, NULL, NULL, 0, 0, 0L, NULL, &ei))
{
// Set the engine state. This line actually triggers the execution of the script.
Connect();
// close the script engine
Close();
return TRUE;
}
return FALSE;
}
/********************************************************************/
/* */
/* Function name : ParseInputText */
/* Description : Parse input text -> output ASP script */
/* */
/********************************************************************/
BOOL CAspParser::ParseInputText(CString &strText, CString &strResult)
{
while(!strText.IsEmpty())
{
// search for begin tag "<%"
int nStartPos = strText.Find("<%");
if (nStartPos == -1)
{
// if tag is not found, output remaining text
strResult += ConvertToCodeBlock(strText);
break;
}
// if "<%" has been found, output the preceeding text
strResult += ConvertToCodeBlock(strText.Left(nStartPos));
// skip tag
nStartPos += 2;
// search for the end tag "%>"
int nEndPos = strText.Find("%>", nStartPos);
if (nEndPos == -1)
{
// end tag is not found!
return FALSE;
}
// get script text
CString strScript = strText.Mid(nStartPos, nEndPos - nStartPos);
strScript.TrimLeft();
// if the string begins with = it is translated to a Response.Write
if (strScript.Left(1) == "=")
{
// Replace <%=x%> with 'Response.Write x'
strResult += "Response.Write ";
strResult += strScript.Mid(2);
}
else
{
strResult += strScript;
}
strResult += "\r\n";
// get next code segment
strText = strText.Mid(nEndPos+2);
}
return TRUE;
}
/********************************************************************/
/* */
/* Function name : ConvertToCodeBlock */
/* Description : Convert HTML to ASP code block */
/* */
/********************************************************************/
CString CAspParser::ConvertToCodeBlock(LPCTSTR lpszHTML)
{
CString strHTML = lpszHTML;
CString strResult;
while(!strHTML.IsEmpty())
{
int nIndex = strHTML.Find("\r\n", 0);
if (nIndex != -1)
{
CString strTemp = strHTML.Left(nIndex);
strTemp.Replace("\"", "\"\"");
if (!strTemp.IsEmpty())
{
strResult += "Response.Write \"";
strResult += strTemp;
strResult += "\" & vbCrLf\r\n";
}
else
{
strResult += "Response.Write \"\" & vbCrLf\r\n";
}
// strip previous line
strHTML = strHTML.Mid(nIndex + 2);
}
else
{
CString strTemp = strHTML;
strTemp.Replace("\"", "\"\"");
if (!strTemp.IsEmpty())
{
strResult += "Response.Write \"";
strResult += strTemp;
strResult += "\"\r\n";
}
else
{
strResult += "\r\n";
}
// we're done!
break;
}
}
return strResult;
}
/********************************************************************/
/* */
/* Function name : GetResponseBuffer */
/* Description : Return content of response buffer */
/* */
/********************************************************************/
BOOL CAspParser::GetResponseBuffer(CString &strBuffer)
{
strBuffer = m_pResponseObject->GetResponseBuffer();
return !strBuffer.IsEmpty();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -