📄 sblogmapper.cpp
字号:
/****************License************************************************
*
* Copyright 2000-2003. ScanSoft, Inc.
*
* Use of this software is subject to notices and obligations set forth
* in the SpeechWorks Public License - Software Version 1.2 which is
* included with this software.
*
* ScanSoft is a registered trademark of ScanSoft, Inc., and OpenSpeech,
* SpeechWorks and the SpeechWorks logo are registered trademarks or
* trademarks of SpeechWorks International, Inc. in the United States
* and other countries.
*
***********************************************************************/
#include <vxibuildopts.h>
#if P_VXI
#define SBLOGMAPPER_EXPORTS
#include "SBlogMapper.h"
#include <map> // For STL map template class
#include <string>
#ifdef WIN32
#include <windows.h>
#endif
#include "vxibuildopts.h"
#if defined(__GNUC__) && (! defined(_GLIBCPP_USE_WCHAR_T))
namespace std
{
typedef basic_string<wchar_t> wstring;
}
#endif
#ifndef MODULE_PREFIX
#define MODULE_PREFIX COMPANY_DOMAIN L"."
#endif
#define MODULE_NAME MODULE_PREFIX L"SBlog"
#ifdef HAVE_XERCES
#include <xercesc/util/PlatformUtils.hpp> // Xerces headers
#include <xercesc/parsers/SAXParser.hpp>
#include <xercesc/sax/HandlerBase.hpp>
using namespace xercesc;
#else
#pragma message("WARNING: HAVE_XERCES not defined, need Apache Xerces to enable the XML error text lookup feature")
#endif
#include "SBlogOSUtils.h"
static const VXIchar *EMPTY_STRING = L"";
// platform independent util functions convert a wide char to int
static int wtoint(const VXIchar *strval)
{
char mystr[128];
if( strval == NULL ) return 0;
SBlogWchar2Latin1(strval, mystr, 127);
return atoi(mystr);
}
//=================================================================
//
// ONLY COMPILE IF HAVE_XERCES IS DEFINED
//
//=================================================================
#ifdef HAVE_XERCES
// Error mapper
class SBlogErrorMapper {
public:
SBlogErrorMapper() : xmlHandler(NULL), initialized(0),
parser(NULL), errorLookUpTable(NULL) { }
virtual ~SBlogErrorMapper();
VXIlogResult ParseErrorMappingFiles(const VXIVector *ErrorMapFiles);
const VXIchar *GetErrorText(VXIunsigned errorID, const VXIchar *moduleName,
int *severity);
static void showExceptionMsg(const XMLException& exception);
static VXIlogResult Error(VXIunsigned errorID, const VXIchar *errorIDText,
const VXIchar *format, ...);
private:
VXIlogResult InitializeXMLParser(void);
int moduleNameCmp(const VXIchar *modName, const VXIchar *pattern) const;
private:
// Internal string type
typedef std::wstring MyString;
// Data structure for error table entries
typedef struct ErrorEntry {
int severity;
MyString message;
} ErrorEntry;
// Map of error strings
typedef std::map<VXIunsigned, ErrorEntry> ErrorEntryMap;
// Data structure for the module mapping table (linked list)
typedef struct ModuleMappingTable {
MyString moduleName;
MyString errFileName;
ErrorEntryMap errTable;
struct ModuleMappingTable *next;
} ModuleMappingTable;
private:
class MySAXHandler;
MySAXHandler *xmlHandler;
VXIbool initialized;
SAXParser *parser;
ModuleMappingTable *errorLookUpTable;
};
// convert XMLCh char to wide char
static void XMLCh2VXIchar(const XMLCh * x, VXIchar *output,
unsigned int maxlen)
{
if (x == NULL) return;
unsigned int len = XMLString::stringLen(x);
if (output == NULL) return;
for (unsigned int i = 0; i < len + 1 && i < maxlen; ++i)
// We throw out anything above 0xFFFF
output[i] = (x[i] != 0 && (x[i] & ~XMLCh(0xFFFF))) ? VXIchar(0xBE)
: VXIchar(x[i]);
}
class SBlogErrorMapper::MySAXHandler : public HandlerBase {
public:
MySAXHandler();
virtual ~MySAXHandler();
void startElement(const XMLCh* const, AttributeList&);
void characters(const XMLCh* const chars, const unsigned int length);
void endElement(const XMLCh* const name);
void warning(const SAXParseException& exception)
{ processError(exception, L"Warning"); }
void error(const SAXParseException& exception)
{ processError(exception, L"Error"); }
void fatalError(const SAXParseException& exception)
{ processError(exception, L"Fatal Error"); }
void resetData(void)
{
errorTable.erase(errorTable.begin(), errorTable.end());
}
const ErrorEntryMap &getErrorTable(void) const { return errorTable; }
const MyString &getModuleName(void) const { return moduleName; }
private:
void processError(const SAXParseException& exception,
const VXIchar* errType);
private:
ErrorEntryMap errorTable;
VXIbool isProcessing;
VXIunsigned errorNumber;
int errorSeverity;
MyString moduleName;
MyString errorMessage;
VXIchar tempString[4096];
};
SBlogErrorMapper::MySAXHandler::MySAXHandler() :
HandlerBase(), errorTable(), moduleName(), errorMessage()
{
isProcessing = 0;
errorNumber = 0;
errorSeverity = -1;
}
SBlogErrorMapper::MySAXHandler::~MySAXHandler()
{
}
void
SBlogErrorMapper::MySAXHandler::startElement(const XMLCh* const name,
AttributeList& attributes)
{
// 1. parse module name from the root
XMLCh2VXIchar(name, tempString, 4096);
if(wcscmp(tempString, L"ErrorMessages") == 0)
{
for(unsigned int i = 0; i < attributes.getLength(); i++)
{
XMLCh2VXIchar(attributes.getName(i), tempString, 4096);
if(wcscmp(tempString, L"moduleName") == 0)
{
XMLCh2VXIchar(attributes.getValue(i), tempString, 4096);
moduleName = tempString;
}
}
}
// 2. only care about <error> elements
else if(wcscmp(tempString, L"error") == 0) {
isProcessing = 1;
errorMessage = L"";
// Find the error number and severity (tag attributes)
for (unsigned int i = 0; i < attributes.getLength(); i++) {
XMLCh2VXIchar(attributes.getName(i), tempString, 4096);
if(wcscmp(tempString, L"num") == 0) {
XMLCh2VXIchar(attributes.getValue(i), tempString, 4096);
errorNumber = wtoint(tempString);
}
else if(wcscmp(tempString, L"severity") == 0) {
XMLCh2VXIchar(attributes.getValue(i), tempString, 4096);
errorSeverity = wtoint(tempString);
}
}
}
else if(isProcessing)
errorMessage += L"???"; // Runtime-determined value, unknown
}
void
SBlogErrorMapper::MySAXHandler::characters(const XMLCh* const chars,
const unsigned int length)
{
// This is the real error message text
if(isProcessing)
{
XMLCh2VXIchar(chars, tempString, 4096);
errorMessage += tempString;
}
}
void SBlogErrorMapper::MySAXHandler::endElement(const XMLCh* const name)
{
// We only care about <error> elements
XMLCh2VXIchar(name, tempString, 4096);
if(wcscmp(tempString, L"error") == 0) {
isProcessing = 0;
// Add error entry to error mapping table (linked list)
ErrorEntry newEntry;
newEntry.severity = errorSeverity;
const VXIchar *sptr = errorMessage.c_str();
// cutting extra white spaces inside the text
bool extraSpace = false;
while( *sptr != L'\0' )
{
if( SBlogIsSpace(*sptr) )
{
if( !extraSpace )
{
newEntry.message += L' ';
extraSpace = true;
}
}
else
{
newEntry.message += *sptr;
extraSpace = false;
}
sptr++;
}
// Add to linked list
errorTable.insert(ErrorEntryMap::value_type(errorNumber, newEntry));
}
}
void
SBlogErrorMapper::MySAXHandler::processError(
const SAXParseException& exception,
const VXIchar* errType)
{
VXIchar ws1[4096];
XMLCh2VXIchar(exception.getMessage(), ws1, 4096);
Error(352, L"SBlogListeners: XML error lookup file parse failed",
L"%s%s%s%s%s%s%s%u%s%u", L"errType", errType, L"errMsg", ws1,
L"file", exception.getSystemId(),
L"line", exception.getLineNumber(),
L"column", exception.getColumnNumber());
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -