📄 xmlesc.cpp
字号:
/* ***** BEGIN LICENSE BLOCK ***** * Source last modified: $Id: xmlesc.cpp,v 1.8.2.1 2004/07/19 21:04:07 hubbe Exp $ * * Portions Copyright (c) 1995-2004 RealNetworks, Inc. All Rights Reserved. * * The contents of this file, and the files included with this file, * are subject to the current version of the RealNetworks Public * Source License (the "RPSL") available at * http://www.helixcommunity.org/content/rpsl unless you have licensed * the file under the current version of the RealNetworks Community * Source License (the "RCSL") available at * http://www.helixcommunity.org/content/rcsl, in which case the RCSL * will apply. You may also obtain the license terms directly from * RealNetworks. You may not use this file except in compliance with * the RPSL or, if you have a valid RCSL with RealNetworks applicable * to this file, the RCSL. Please see the applicable RPSL or RCSL for * the rights, obligations and limitations governing use of the * contents of the file. * * Alternatively, the contents of this file may be used under the * terms of the GNU General Public License Version 2 or later (the * "GPL") in which case the provisions of the GPL are applicable * instead of those above. If you wish to allow use of your version of * this file only under the terms of the GPL, and not to allow others * to use your version of this file under the terms of either the RPSL * or RCSL, indicate your decision by deleting the provisions above * and replace them with the notice and other provisions required by * the GPL. If you do not delete the provisions above, a recipient may * use your version of this file under the terms of any one of the * RPSL, the RCSL or the GPL. * * This file is part of the Helix DNA Technology. RealNetworks is the * developer of the Original Code and owns the copyrights in the * portions it created. * * This file, and the files included with this file, is distributed * and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY * KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS * ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET * ENJOYMENT OR NON-INFRINGEMENT. * * Technology Compatibility Kit Test Suite(s) Location: * http://www.helixcommunity.org/content/tck * * Contributor(s): * * ***** END LICENSE BLOCK ***** */// $Id: xmlesc.cpp,v 1.8.2.1 2004/07/19 21:04:07 hubbe Exp $/******************************************************************* * * NAME: xmlesc.cpp * * CLASS * CEscapeXMLtoHTML * * DESCRIPTION: * The implmentation for a class that escapes all the XML characters * that are not printable in HTML. A IHXBuffer is passed into the * class on throgh the Convert Method. This is a pure virtual class. * The CheckTag, and OnTag functions must be implemented in a child * class. * * NOTES: * Started with Mozilla's ColorHTML function * *******************************************************************/#include "hxcom.h" /* IUnknown */#include "hxtypes.h" #include "hxbuffer.h"#include "hxiids.h"#include "hxstrutl.h"#include "hxstring.h"#include "perplex.h"#include "rtsputil.h"#include "growingq.h"#include "xmlesc.h"#include "vsrcinfo.h"#include "hlxclib/string.h"/*___________________________________________________________________________ *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Static variable declarations *___________________________________________________________________________ *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/const char* constCEscapeXMLtoHTML::m_pDefaultTags[20] = { tag_BEGIN_TAG ,tag_END_TAG ,tag_BEGIN_TAG_NAME ,tag_END_TAG_NAME ,tag_BEGIN_ATTRIBUTE ,tag_END_ATTRIBUTE ,tag_BEGIN_BROKEN_ATT ,tag_END_BROKEN_ATT ,tag_BEGIN_COMMENT ,tag_END_COMMENT ,tag_BEGIN_AMPERSAND ,tag_END_AMPERSAND ,tag_BEGIN_HREF ,tag_END_HREF ,tag_PROCESSING_INSTRUCTIONS ,tag_END_PI ,tag_BEGIN_CDATA ,tag_END_CDATA ,tag_BEGIN_DTD ,tag_END_DTD};const char* constCEscapeXMLtoHTML::m_pStyleTags[20] ={ style_BEGIN_TAG ,style_END_TAG ,style_BEGIN_TAG_NAME ,style_END_TAG_NAME ,style_BEGIN_ATTRIBUTE ,style_END_ATTRIBUTE ,style_BEGIN_BROKEN_ATT ,style_END_BROKEN_ATT ,style_BEGIN_COMMENT ,style_END_COMMENT ,style_BEGIN_AMPERSAND ,style_END_AMPERSAND ,style_BEGIN_HREF ,style_END_HREF ,style_PROCESSING_INSTRUCTIONS ,style_END_PI ,style_BEGIN_CDATA ,style_END_CDATA ,style_BEGIN_DTD ,style_END_DTD};/*___________________________________________________________________________ *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Public Methods *___________________________________________________________________________ *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*//*___________________________________________________________________________ *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * CEscapeXMLtoHTML(pServerUrl, pOurPath) * * PARAMETERS: * pServerUrl Full url to the server including the template file. * pOurPath The Relative path of the file currently being parsed * no trailing "/" * bMangleLinks - to mangle or not * bUseStyles - output in styles or regular HTML * pHotTags - The tags that we will call OnTag For. * pDefaultView - The default viewsource to use for remote rstp * links ":port/viewmount/viewfile" * DESCRIPTION: * Constructor. * This is the primary means of creating an instance of CEscapeXMLtoHTML. *___________________________________________________________________________ *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/CEscapeXMLtoHTML::CEscapeXMLtoHTML(IHXValues* pOptions, const char** pHotTags) : m_pEscapeStrings(NULL), m_pServerUrl(NULL), m_pOurPath(NULL), m_pHotTags(NULL), m_pDefaultView(NULL), m_pFileName(NULL), m_pRamGen(NULL), m_ulModDate(0), m_ulFileSize(0){ m_pHotTags = pHotTags; IHXBuffer* pViewURL = NULL; IHXBuffer* pCurrentPath = NULL; IHXBuffer* pRemoteView = NULL; IHXBuffer* pFileName = NULL; UINT32 ulMangle = 0; UINT32 ulStyles = 0; if ( FAILED(pOptions->GetPropertyCString("ViewSourceURL", pViewURL)) || FAILED(pOptions->GetPropertyCString("CurrentPath", pCurrentPath)) || FAILED(pOptions->GetPropertyULONG32("HidePaths", ulMangle)) || FAILED(pOptions->GetPropertyULONG32("UseStyles", ulStyles)) || FAILED(pOptions->GetPropertyCString("RemoteViewSourceURL", pRemoteView)) || FAILED(pOptions->GetPropertyCString("FileName", pFileName)) || FAILED(pOptions->GetPropertyULONG32("ModificationTime", m_ulModDate)) || FAILED(pOptions->GetPropertyULONG32("FileSize", m_ulFileSize)) ) { HX_ASSERT(FALSE); // return HXR_INVALID_PARAMETER; } m_bMangleLinks = ulMangle ? TRUE : FALSE; BOOL bUseStyles = ulStyles ? TRUE : FALSE; m_pServerUrl = new char[pViewURL->GetSize()+1]; strcpy(m_pServerUrl, (char*)pViewURL->GetBuffer()); /* Flawfinder: ignore */ m_pOurPath = new char[pCurrentPath->GetSize()+1]; strcpy(m_pOurPath, (char*)pCurrentPath->GetBuffer()); /* Flawfinder: ignore */ m_pDefaultView = new char[pRemoteView->GetSize()+1]; strcpy(m_pDefaultView, (char*)pRemoteView->GetBuffer()); /* Flawfinder: ignore */ m_pFileName = new char[pFileName->GetSize()+1]; strcpy(m_pFileName, (char*)pFileName->GetBuffer()); /* Flawfinder: ignore */ IHXBuffer* pRamGen = NULL; if ( SUCCEEDED(pOptions->GetPropertyCString("RamGenURL", pRamGen)) ) { m_pRamGen = new char[pRamGen->GetSize()+1]; strcpy(m_pRamGen, (char*)pRamGen->GetBuffer()); /* Flawfinder: ignore */ } HX_RELEASE(pRamGen); HX_RELEASE(pRemoteView); HX_RELEASE(pViewURL); HX_RELEASE(pCurrentPath); HX_RELEASE(pFileName); if ( bUseStyles ) { m_pEscapeStrings = (const char**) m_pStyleTags; } else { m_pEscapeStrings = (const char**) m_pDefaultTags; }}/*___________________________________________________________________________ *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * ~CEscapeXMLtoHTML() * * PARAMETERS: * none * * DESCRIPTION: * Destructor. * *___________________________________________________________________________ *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/CEscapeXMLtoHTML::~CEscapeXMLtoHTML(){ HX_VECTOR_DELETE(m_pRamGen); HX_VECTOR_DELETE(m_pServerUrl); HX_VECTOR_DELETE(m_pOurPath); HX_VECTOR_DELETE(m_pDefaultView); HX_VECTOR_DELETE(m_pFileName);}/*___________________________________________________________________________ *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Convert(pXMLsrc, pOutBuffer) * * PARAMETERS: * pXMLsrc Pointer to a IHXBuffer containing the src of xml to * be converted * pOutBuffer A new IHXBuffer containing the parsed XML data ready * to be displayed in an HTML browser. * * DESCRIPTION: * Creates a growing queue, copies the buffers to a cstring, then calls * the Parse(...) function to parse the string. Next the outputed is * dequeued Off the growing queue and put in a new IHXBuffer and * returned * * RETURNS * *___________________________________________________________________________ *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/STDMETHODIMP CEscapeXMLtoHTML::Convert(IHXBuffer* pXMLsrc, REF(IHXBuffer*) pOutBuffer){ // TODO: UTF-16 UCHAR* pIn = NULL; UINT32 ulLen; HX_ASSERT(pXMLsrc != NULL); pXMLsrc->Get(pIn, ulLen); CBigByteGrowingQueue* pQueue = NULL; if ( ulLen ) { pQueue = new CBigByteGrowingQueue(ulLen*4); if ( !pQueue ) { // panic HX_RELEASE(pXMLsrc); return HXR_OUTOFMEMORY; } DataObject StateObj; StateObj.state = IN_CONTENT; StateObj.bPushChar = TRUE; PushHeader(pQueue); pQueue->EnQueue("<pre><!-- Begin Source -->\n"); Parse(pIn, ulLen, pQueue, &StateObj); pQueue->EnQueue("\n<!-- End Source --></pre>\n"); UINT32 ulOutLen; ulOutLen = pQueue->GetQueuedItemCount(); HX_RELEASE(pOutBuffer); pOutBuffer = new CHXBuffer(); pOutBuffer->AddRef(); if ( pOutBuffer->SetSize(ulOutLen) != HXR_OK ) { HX_RELEASE(pXMLsrc); return HXR_OUTOFMEMORY; } UCHAR* pOut = pOutBuffer->GetBuffer(); pQueue->DeQueue(pOut, ulOutLen); } else { // just return an empty buffer. HX_RELEASE(pOutBuffer); pOutBuffer = new CHXBuffer(); pOutBuffer->AddRef(); } HX_DELETE(pQueue); return HXR_OK;}/*___________________________________________________________________________ *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * PushCommonHeader(queue) * * PARAMETERS: * * queue The queue to push the output to. * * DESCRIPTION: * * Pushes the common information onto the queue. * * RETURNS * *___________________________________________________________________________ *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/void CEscapeXMLtoHTML::PushCommonHeader(CBigByteGrowingQueue* queue){ queue->EnQueue(z_pFileName); queue->EnQueue(m_pFileName); queue->EnQueue(z_pEndLine); QueueModificationTime(queue, m_ulModDate); QueueFileSize(queue, m_ulFileSize); if ( m_pRamGen ) { queue->EnQueue(z_pRamLink); queue->EnQueue("<a href=\""); queue->EnQueue(m_pRamGen); const char* p = m_pOurPath; if ( *p == '/' ) { p++; } queue->EnQueue(p); queue->EnQueue("/"); queue->EnQueue(m_pFileName); queue->EnQueue("\">"); queue->EnQueue(m_pRamGen); queue->EnQueue(p); queue->EnQueue("/"); queue->EnQueue(m_pFileName); queue->EnQueue("</a>"); queue->EnQueue(z_pEndLine); }}/*___________________________________________________________________________ *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Protected Methods *___________________________________________________________________________ *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*//*___________________________________________________________________________ *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * WrapAttributeWithHREF (pPositionPointer, ulLen, pObj, pQueue, pAttribute) * * PARAMETERS: * * pPositionPointer Pointer to a character string. It points to the first * whitespace after the tagname * ulLen Length to end of pPositionPointer so we don't overrun the buffer * pObj The current state of the Parse Function, this can be used * to continue parsing. * queue The queue to push the output to. * pAttribute The attribute we want to wrap. * * DESCRIPTION:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -