VXIrec_utils.h
上传用户:xqtpzdz
上传日期:2022-05-21
资源大小:1764k
文件大小:8k
源码类别:

xml/soap/webservice

开发平台:

Visual C++

  1. /****************License************************************************
  2.  * Vocalocity OpenVXI
  3.  * Copyright (C) 2004-2005 by Vocalocity, Inc. All Rights Reserved.
  4.  * This program is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU General Public License
  6.  * as published by the Free Software Foundation; either version 2
  7.  * of the License, or (at your option) any later version.
  8.  *  
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  17.  * Vocalocity, the Vocalocity logo, and VocalOS are trademarks or 
  18.  * registered trademarks of Vocalocity, Inc. 
  19.  * OpenVXI is a trademark of Scansoft, Inc. and used under license 
  20.  * by Vocalocity.
  21.  ***********************************************************************/
  22. #ifndef _VXIREC_UTILS
  23. #define _VXIREC_UTILS
  24. #include <util/PlatformUtils.hpp>       // Xerces headers
  25. #include <util/TransService.hpp>
  26. #include <parsers/SAXParser.hpp>
  27. #include <framework/MemBufInputSource.hpp>
  28. #include <sax2/XMLReaderFactory.hpp>
  29. #include <sax/HandlerBase.hpp>
  30. #include <sax/SAXParseException.hpp> // by DOMTreeErrorReporter
  31. #include <sax/EntityResolver.hpp>    // by DTDResolver
  32. #include <sax/ErrorHandler.hpp>      // by DOMTreeErrorReporter
  33. using namespace xercesc;
  34. #include <SWIprintf.h>
  35. #include <VXIrecAPI.h>
  36. #include <VXIvalue.h>
  37. #include <list>
  38. #include <string>
  39. typedef std::basic_string<VXIchar> vxistring;
  40. #ifdef __cplusplus
  41. extern "C" {
  42. #endif
  43. // Constants for diagnostic logging tags
  44. //
  45. static const VXIunsigned DIAG_TAG_API         = 0;
  46. static const VXIunsigned DIAG_TAG_RECOGNITION = 1;
  47. static const VXIunsigned DIAG_TAG_GRAMMARS    = 2;
  48. static const VXIunsigned DIAG_TAG_PARSE       = 4;
  49. static const VXIunsigned DIAG_TAG_PROPERTY    = 5;
  50. #define VXIREC_MODULE L"VXIrec"
  51. class VXIrecWordList;
  52. class GrammarSaxHandler;
  53. class GrammarWordList;
  54. typedef std::list<vxistring> STRINGLIST;
  55. typedef struct VXIrecGrammarInfo {
  56.   vxistring word;
  57.   vxistring tag; 
  58.   STRINGLIST taglist;   
  59.   vxistring semantic;
  60. } VXIrecGrammarInfo;
  61. // Grammar info list
  62. typedef std::list<VXIrecGrammarInfo> GRAMMARINFOLIST;
  63. // Information after cracking JSGF grammar, may be more???
  64. typedef struct JSGFInfo {
  65.   vxistring versionStr;
  66.   vxistring nameStr;
  67.   vxistring publicStr;
  68.   vxistring contentStr;
  69. } JSGFInfo;
  70. class VXIrecGrammar {
  71. public:
  72.   VXIrecGrammar() { }
  73.   virtual ~VXIrecGrammar() { }
  74.   virtual void SetEnabled(bool) = 0;
  75.   // Sets the flag indicating whether or not the grammar is enabled for the
  76.   // next recognition.
  77.   virtual bool IsEnabled() const = 0;
  78.   // Returns current enabled state of this grammar.
  79.   
  80.   virtual GRAMMARINFOLIST * GetGrammarInfoList() const = 0;
  81.   
  82.   virtual bool GetGrammarInfo(const VXIchar* input, 
  83.                               VXIrecGrammarInfo ** gramInfo) const = 0;
  84.   virtual bool IsDtmf() const = 0;
  85. };
  86. class VXIrecData {
  87. public:
  88.   VXIrecData(VXIlogInterface *log, VXIinetInterface *inet);
  89.   virtual ~VXIrecData();
  90.   static VXIunsigned diagLogBase;
  91.   static int Initialize(VXIlogInterface* log, VXIunsigned diagBase);
  92.   static int ShutDown();
  93.   // Release all existing grammars.
  94.   void Clear();
  95.   // Add a new grammar.  The VXIrecData assumes ownership of the grammar.
  96.   void AddGrammar(VXIrecGrammar *g);
  97.   // Removed an exising grammar.  The corresponding grammar is destroyed.
  98.   void FreeGrammar(VXIrecGrammar *g);
  99.   void ActivateGrammar(VXIrecGrammar *g);
  100.   void DeactivateGrammar(VXIrecGrammar *g);
  101.   int GetActiveCount() const;
  102.   
  103.   // Parse SRGS grammar: very simple srgs grammar processing: i.e: no ref rule, etc.
  104.   VXIrecGrammar * ParseSRGSGrammar(const vxistring & srgsgram, 
  105.                                    const VXIMap    * properties,
  106.                                    bool isdtmf = false);
  107.   // Return true if grammar found for the input and the nlsml is constructed
  108.   bool ConstructNLSMLForInput(const VXIchar* input, vxistring & nlsmlresult);
  109.   
  110.   // Conversion functions
  111.   bool JSGFToSRGS(const vxistring & incoming,
  112.                   vxistring & result, 
  113.                   const VXIMap* props);
  114.   void ConvertJSGFType(const vxistring & dataIn, JSGFInfo & info);
  115.   bool OptionToSRGS(const VXIMap    * properties,
  116.                     const VXIVector * gramChoice,
  117.                     const VXIVector * gramValue,
  118.                     const VXIVector * gramAcceptance,
  119.                     const VXIbool     isDTMF,
  120.                     vxistring & srgs);
  121.   
  122.   // Process semantic interpretation based on specific implementation, i.e: SSFT
  123.   bool ProcessSemanticInterp(vxistring & result, const VXIrecGrammarInfo *ginfo); 
  124.   // Logging functions
  125.   VXIlogResult LogError(VXIunsigned errorID, const VXIchar *format, ...) const;
  126.   VXIlogResult LogDiag(VXIunsigned tag, const VXIchar *subtag, const VXIchar *format, ...) const;
  127.   VXIlogInterface * GetLog() const { return log; }
  128.   void ShowPropertyValue(const VXIMap *properties);
  129.   void ShowPropertyValue(const VXIchar *key,
  130.                          const VXIValue *value, VXIunsigned PROP_TAG);     
  131.   bool FetchContent(
  132.     const VXIchar *  name, 
  133.     const VXIMap  *  properties,
  134.     VXIbyte       ** result,
  135.     VXIulong      &  read );
  136. private:
  137.   VXIlogInterface *log;
  138.   VXIinetInterface *inet;
  139.   typedef std::list<VXIrecGrammar *> GRAMMARS;
  140.   GRAMMARS grammars;
  141.   GRAMMARS activeGrammars;
  142.   
  143.   GrammarSaxHandler *xmlHandler;
  144.   SAXParser         *parser;  
  145. };
  146. typedef enum NODE_TYPE {
  147.   GRAMMAR_NODE,
  148.   META_NODE,
  149.   ITEM_NODE,
  150.   UNKNOWN_NODE      /* always last */   
  151. } NODE_TYPE;
  152. #define GRAMMAR       L"grammar"
  153. #define META          L"meta"
  154. #define ITEM          L"item"
  155. #define TAG           L"tag"
  156. #define NAME          L"name"
  157. #define CONTENT       L"content"
  158. class GrammarSaxHandler : public HandlerBase 
  159. {
  160. public:
  161.   GrammarSaxHandler(VXIlogInterface *l);
  162.   virtual ~GrammarSaxHandler();
  163.   void startElement(const XMLCh* const, AttributeList&);
  164.   void characters(const XMLCh* const chars, const unsigned int length);
  165.   void endElement(const XMLCh* const name);
  166.   void warning(const SAXParseException& exception) 
  167.   { processError(exception, L"Warning"); }
  168.   void error(const SAXParseException& exception) 
  169.   { processError(exception, L"Error"); }
  170.   void fatalError(const SAXParseException& exception) 
  171.   { processError(exception, L"Fatal Error"); }
  172.   // Create a new grammar info list
  173.   void CreateGrammarInfoList()
  174.   {
  175.     isDTMFGram = false;
  176.     grammarInfo.word = L"";
  177.     grammarInfo.tag = L"";
  178.     grammarInfo.semantic = L"";  
  179.     grammarInfoList = new GRAMMARINFOLIST();      
  180.   }
  181.   
  182.   // Destroy grammar info list
  183.   void DestroyGrammarInfoList()
  184.   {
  185.     if( grammarInfoList ) {
  186.       delete grammarInfoList;
  187.       grammarInfoList = NULL;
  188.     }
  189.   }
  190.   
  191.   // Release the ownership of grammar info list
  192.   GRAMMARINFOLIST * AcquireGrammarInfoList()
  193.   {
  194.     GRAMMARINFOLIST * ret = grammarInfoList;
  195.     grammarInfoList = NULL;
  196.     return ret;
  197.   }
  198.   
  199.   bool isDTMFGrammar() const { return isDTMFGram; }
  200. private:
  201.   void processError(const SAXParseException& exception, 
  202.                     const VXIchar* errType);
  203.   
  204.   // Logging functions
  205.   VXIlogResult LogError(VXIunsigned errorID, const VXIchar *format, ...) const;
  206.   VXIlogResult LogDiag(VXIunsigned offset, const VXIchar *subtag, 
  207.                        const VXIchar *format, ...) const;
  208.   
  209. private:
  210.   GRAMMARINFOLIST * grammarInfoList;
  211.   VXIrecGrammarInfo       grammarInfo;
  212.   bool processTag;
  213.   bool isDTMFGram;
  214.   NODE_TYPE nodeType;
  215.   VXIlogInterface* log;
  216. };  
  217. #ifdef __cplusplus
  218. }
  219. #endif
  220. #endif /* include guard */