?? xmlbase.cpp
字號:
// ================================================================================
//
// author: Rainer Schuster
//
// created: 09.03.2005 10:20:45
//
// filename: xmlbase.cpp IMPLEMENTATION
//
// This code is as it is. You are allowed to use, modify and/or redistribute it freely.
// I'm not responsible for any errors or damage. Use it at your own risk.
//
// ================================================================================
#include "stdafx.h"
#include "xmlbase.h"
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/parsers/AbstractDOMParser.hpp>
#include <xercesc/dom/DOMImplementation.hpp>
#include <xercesc/dom/DOMImplementationLS.hpp>
#include <xercesc/dom/DOMImplementationRegistry.hpp>
#include <xercesc/dom/DOMBuilder.hpp>
#include <xercesc/dom/DOMException.hpp>
#include <xercesc/dom/DOMDocument.hpp>
#include <xercesc/dom/DOMNodeList.hpp>
#include <xercesc/dom/DOMError.hpp>
#include <xercesc/dom/DOMLocator.hpp>
#include <xercesc/dom/DOMNamedNodeMap.hpp>
#include <xercesc/dom/DOMAttr.hpp>
#include <string>
XMLBaseErrorHandler ::XMLBaseErrorHandler () :
fSawErrors(false)
{
}
XMLBaseErrorHandler ::~XMLBaseErrorHandler ()
{
}
// ---------------------------------------------------------------------------
// XMLBaseErrorHandler: Overrides of the DOM ErrorHandler interface
// ---------------------------------------------------------------------------
bool XMLBaseErrorHandler ::handleError(const DOMError& domError)
{
fSawErrors = true;
if (domError.getSeverity() == DOMError::DOM_SEVERITY_WARNING)
AfxMessageBox( "Warning at file ");
else if (domError.getSeverity() == DOMError::DOM_SEVERITY_ERROR)
AfxMessageBox( "Error at file ");
else
AfxMessageBox( "Fatal Error at file ");
CString strErr;
strErr.Format( "%s, line %i, char %i\n Message: %s",
XC(domError.getLocation()->getURI()),
domError.getLocation()->getLineNumber(),
domError.getLocation()->getColumnNumber(),
XC(domError.getMessage()) );
return true;
}
void XMLBaseErrorHandler ::resetErrors()
{
fSawErrors = false;
}
void CXMLGrid::Parse( DOMDocument *doc, const char* lpcszID)
{
DOMNode *row;
int count = 0;
if (doc)
{
if (doc->getNodeType() == DOMNode::DOCUMENT_NODE)
{
DOMNode *node = doc->getFirstChild();
while( node )
{
if( strcmp( XC(node->getNodeName()), "XMLGRID") == 0)
{
ParseAttributes( node);
if( strcmp(lpcszID, m_strID) == 0)
{
row = node->getFirstChild();
while( row )
{
if( strcmp( XC(row->getNodeName()), "row") == 0 )
{
ParseRow( row);
}
else
if( strcmp( XC(row->getNodeName()), "header") == 0 )
{
ParseHeader( row);
}
else
{
OnUnknownAttribute( row);
}
row = row->getNextSibling();
}
}
}
node = node->getNextSibling();
}
}
}
}
void CXMLGrid::ParseAttributes( DOMNode *node)
{
static char cAttrib[32768]; //32K
if( GetAttributeValue( node, "id", cAttrib, sizeof(cAttrib)) )
m_strID = cAttrib;
if( GetAttributeValue( node, "label", cAttrib, sizeof(cAttrib)) )
m_strLabel = cAttrib;
if( GetAttributeValue( node, "icons", cAttrib, sizeof(cAttrib)) )
m_strIcons = cAttrib;
}
void CXMLGrid::OnUnknownAttribute( DOMNode *row)
{
}
void CXMLGrid::ParseHeader( DOMNode *row)
{
long lId;
DOMNode *col_definition = row->getFirstChild();
m_header.m_lIndex = 0;
while( col_definition )
{
if( GetAttributeValue( col_definition, "id", lId) )
{
m_header.Insert( lId, XC( col_definition->getTextContent()) );
}
col_definition = col_definition->getNextSibling();
}
}
void CXMLGrid::ParseRow( DOMNode *row)
{
CXMLGridRow rowdata;
GetAttributeValue( row, "index", rowdata.m_lIndex );
// Spalten Parsen!!
DOMNode *col = row->getFirstChild();
while( col )
{
if( strcmp( XC(col->getNodeName()), "col") == 0 )
{
ParseCol( col, rowdata);
}
col = col->getNextSibling();
}
if( rowdata.m_pCols->size() )
{
m_rows.insert( CXMLGridDataPair(m_lRowCount++, rowdata) );
}
}
void CXMLGrid::ParseCol( DOMNode *col, CXMLGridRow &rowdata)
{
long lId;
if( GetAttributeValue( col, "id", lId) )
rowdata.Insert( lId, XC(col->getTextContent()) );
// if( GetAttributeValue( col, "width", lId) )
//TODO:
}
bool CXMLBase::Init(const char* lpcszXMLFile, const char *lpcszID)
{
const char* xmlFile = lpcszXMLFile;
AbstractDOMParser::ValSchemes valScheme = AbstractDOMParser::Val_Auto;
bool doNamespaces = false;
bool doSchema = false;
bool schemaFullChecking = false;
bool doList = false;
bool errorOccurred = false;
bool recognizeNEL = false;
bool printOutEncounteredEles = false;
char localeStr[64];
XMLBaseErrorHandler errorHandler;
memset(localeStr, 0, sizeof localeStr);
// Initialize the XML4C system
try
{
if (strlen(localeStr))
{
XMLPlatformUtils::Initialize(localeStr);
}
else
{
XMLPlatformUtils::Initialize();
}
if (recognizeNEL)
{
XMLPlatformUtils::recognizeNEL(recognizeNEL);
}
}
catch (const XMLException& toCatch)
{
AfxMessageBox( XC(toCatch.getMessage()) );
return false;
}
// Instantiate the DOM parser.
static const XMLCh gLS[] = { chLatin_L, chLatin_S, chNull };
DOMImplementation *impl = DOMImplementationRegistry::getDOMImplementation(gLS);
DOMBuilder *parser = ((DOMImplementationLS*)impl)->createDOMBuilder(DOMImplementationLS::MODE_SYNCHRONOUS, 0);
parser->setFeature(XMLUni::fgDOMNamespaces, doNamespaces);
parser->setFeature(XMLUni::fgXercesSchema, doSchema);
parser->setFeature(XMLUni::fgXercesSchemaFullChecking, schemaFullChecking);
XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *doc = 0;
unsigned long duration;
try
{
// reset document pool
parser->resetDocumentPool();
const unsigned long startMillis = XMLPlatformUtils::getCurrentMillis();
doc = parser->parseURI(xmlFile);
const unsigned long endMillis = XMLPlatformUtils::getCurrentMillis();
duration = endMillis - startMillis;
}
catch (const XMLException& toCatch)
{
AfxMessageBox( XC(toCatch.getMessage()) );
errorOccurred = true;
return false;
}
catch (const DOMException& toCatch)
{
const unsigned int maxChars = 2047;
XMLCh errText[maxChars + 1];
CString strErr;
strErr.Format( "DOM Error during parsing: '%s'; DOMException code is: %i", xmlFile, toCatch.code);
if (DOMImplementation::loadDOMExceptionMsg(toCatch.code, errText, maxChars))
{
strErr += "Message is: ";
strErr += XC(errText);
}
errorOccurred = true;
}
catch (...)
{
AfxMessageBox ("Unerwartete Ausnahme beim Parsen");
errorOccurred = true;
return false;
}
if (errorHandler.getSawErrors())
{
AfxMessageBox( "Errors occurred, no output available");
errorOccurred = true;
return false;
}
else
{
//jetzt k鰊nen wir parsen!!
if (doc)
{
Parse( doc, lpcszID);
}
}
parser->release();
// And call the termination method
XMLPlatformUtils::Terminate();
return true;
}
BOOL LoadIcon( CString strIcon2Load, CString strPath, CPictureMap *map)
{
BOOL bReturn = FALSE;
if( !strPath.IsEmpty() )
{
TCHAR cLast = strPath.GetAt( strPath.GetLength()-1);
if( cLast != '\\'
|| cLast != '/'
)
{
strPath += "\\";
}
LPPICTURE pPict=NULL;
CSize size;
bReturn = LoadPictureFile( strPath + strIcon2Load, &pPict);
map->insert( CPicturePair( strIcon2Load, pPict ));
}
return bReturn;
}
BOOL LoadPictureFile(LPCTSTR szFile, LPPICTURE* pgpPicture)
{
// open file
HANDLE hFile = CreateFile(szFile,
GENERIC_READ,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
AfxMessageBox ("Could not read file");
return FALSE;
}
// get file size
DWORD dwFileSize = GetFileSize(hFile, NULL);
if (dwFileSize == (DWORD)-1)
{
CloseHandle(hFile);
AfxMessageBox ("File seems to be empty");
return FALSE;
}
LPVOID pvData = NULL;
// alloc memory based on file size
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
if (hGlobal == NULL)
{
CloseHandle(hFile);
AfxMessageBox ("Could not allocate memory for image");
return FALSE;
}
pvData = GlobalLock(hGlobal);
if (pvData == NULL)
{
GlobalUnlock(hGlobal);
CloseHandle(hFile);
AfxMessageBox ("Could not lock memory for image");
return FALSE;
}
DWORD dwBytesRead = 0;
// read file and store in global memory
BOOL bRead = ReadFile(hFile,
pvData,
dwFileSize,
&dwBytesRead,
NULL);
GlobalUnlock(hGlobal);
CloseHandle(hFile);
if (!bRead)
{
AfxMessageBox ("Could not read file");
return FALSE;
}
LPSTREAM pstm = NULL;
// create IStream* from global memory
HRESULT hr = CreateStreamOnHGlobal(hGlobal,
TRUE,
&pstm);
if (!(SUCCEEDED(hr)))
{
AfxMessageBox ("CreateStreamOnHGlobal() failed");
if (pstm != NULL)
pstm->Release();
return FALSE;
}
else if (pstm == NULL)
{
AfxMessageBox ("CreateStreamOnHGlobal() failed");
return FALSE;
}
// Create IPicture from image file
if (*pgpPicture)
(*pgpPicture)->Release();
hr = ::OleLoadPicture(pstm,
dwFileSize,
FALSE,
IID_IPicture,
(LPVOID *)&(*pgpPicture));
if (!(SUCCEEDED(hr)))
{
pstm->Release();
AfxMessageBox("Could not load image (hr failure)");
return FALSE;
}
else if (*pgpPicture == NULL)
{
pstm->Release();
AfxMessageBox("Could not load image (pgpPicture failure)");
return FALSE;
}
pstm->Release();
return TRUE; //Made it ...!
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -