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

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. // -----1=0-------2=0-------3=0-------4=0-------5=0-------6=0-------7=0-------8
  23. #ifdef OPENVXI
  24. #include "OSBinet.h"
  25. #define SBinetInit OSBinetInit
  26. #define SBinetShutDown OSBinetShutDown
  27. #define SBinetCreateResource OSBinetCreateResource
  28. #define SBinetDestroyResource OSBinetDestroyResource
  29. #else
  30. #include "SBinet.h"
  31. #endif
  32. #include "SBlog.h"
  33. #include "VXIinterpreter.h"
  34. #include "VXIvalue.h"
  35. #include <iostream>
  36. #include <string>
  37. #include <sstream>
  38. #if defined(__GNUC__) && (! defined(_GLIBCPP_USE_WCHAR_T))
  39. namespace std
  40. {
  41.   typedef basic_string<wchar_t> wstring;
  42. }
  43. #define wcout cout
  44. using std::cout;
  45. typedef char myCharType;
  46. #define MY_EMPTY_STR ""
  47. static std::ostream & operator<< (std::ostream & s, const wchar_t *str) {
  48.   if ( str == NULL ) {
  49.     s << "<null>";
  50.   } else {
  51.     unsigned int i, len = wcslen (str);
  52.     for (i = 0; i < len; i++) 
  53.       if ( str[i] & 0xFF00 )
  54. s << "<%" << (int) str[i] << ">";
  55.       else
  56. s << (char) str[i];
  57.   }
  58.   return s;
  59. }
  60. static std::ostream & operator<< (std::ostream & s, const std::wstring &str) {
  61.   s << str.c_str();
  62.   return s;
  63. }
  64. #else  /* NOT defined(__GNUC__) && (! defined(_GLIBCPP_USE_WCHAR_T)) */
  65. using std::wcout;
  66. typedef VXIchar myCharType;
  67. #define MY_EMPTY_STR L""
  68. #endif /* defined(__GNUC__) && (! defined(_GLIBCPP_USE_WCHAR_T)) */
  69. using std::endl;
  70. const int ID_INTERPRETER = 8000;
  71. const int ID_INET        = 2000;
  72. static std::basic_ostringstream<myCharType> gblOs;
  73. // ------*---------*---------*---------*---------*---------*---------*---------
  74. static std::basic_ostream<myCharType>& 
  75. operator<<(std::basic_ostream<myCharType>& os,
  76.    const VXIValue * & val)
  77. {
  78.   if (val == NULL)
  79.     return os << L"NULL";
  80.   VXIchar tmp[256];
  81.   
  82.   switch (VXIValueGetType(val)) {
  83.   case VALUE_INTEGER:
  84.     return os << VXIIntegerValue(reinterpret_cast<const VXIInteger*>(val));
  85.   case VALUE_FLOAT: {
  86. #ifdef WIN32
  87.     _snwprintf(tmp, 256, L"%f", VXIFloatValue(reinterpret_cast<const VXIFloat*>(val)));
  88. #else
  89.     swprintf(tmp, 256, L"%f", VXIFloatValue(reinterpret_cast<const VXIFloat*>(val)));
  90. #endif
  91.     return os << tmp;
  92.   }
  93. #if (VXI_CURRENT_VERSION >= 0x00030000)
  94.   case VALUE_DOUBLE:
  95. #ifdef WIN32
  96.     _snwprintf(tmp, 256, L"%f", VXIDoubleValue(reinterpret_cast<const VXIDouble*>(val)));
  97. #else
  98.     swprintf(tmp, 256, L"%f", VXIDoubleValue(reinterpret_cast<const VXIDouble*>(val)));
  99. #endif
  100.     return os << tmp;
  101.   case VALUE_ULONG:
  102.     return os << VXIULongValue(reinterpret_cast<const VXIULong*>(val));
  103. #endif
  104.   case VALUE_STRING:
  105.     return os << VXIStringCStr(reinterpret_cast<const VXIString *>(val));
  106.   case VALUE_VECTOR:
  107.     {
  108.       const VXIVector * v = reinterpret_cast<const VXIVector *>(val);
  109.       const VXIunsigned len = VXIVectorLength(v);
  110.       os << L"{ ";
  111.       for (VXIunsigned i = 0; i < len; ++i) {
  112.         if (i != 0) os << L", ";
  113.         os << VXIVectorGetElement(v, i);
  114.       }
  115.       return os << L" }";
  116.     }
  117.   case VALUE_MAP:
  118.     {
  119.       const VXIMap * m = reinterpret_cast<const VXIMap *>(val);
  120.       const VXIchar * key;
  121.       const VXIValue * value;
  122.       os << L"{ ";
  123.       if (VXIMapNumProperties(m) != 0) {
  124.         VXIMapIterator * i = VXIMapGetFirstProperty(m, &key, &value);
  125.         os << L"(" << key << L", " << value << L")";
  126.         
  127.         while (VXIMapGetNextProperty(i, &key, &value)
  128.                == VXIvalue_RESULT_SUCCESS)
  129.           os << L", (" << key << L", " << value << L")";
  130.         VXIMapIteratorDestroy(&i);
  131.       }
  132.       return os << L" }";
  133.     }
  134.   default:
  135.     break;
  136.   }
  137.   return os << L"(unprintable result)";
  138. }
  139. void LoggerClear()
  140. {
  141.   gblOs.str(MY_EMPTY_STR);
  142. }
  143. void LoggerPrint()
  144. {
  145.   wcout << gblOs.str();
  146.   wcout.flush();
  147. }
  148. void LoggerErrorListener(SBlogInterface *,
  149.                          const VXIchar *moduleName,
  150.                          VXIunsigned errorID,
  151.                          time_t,
  152.  VXIunsigned,
  153.                          const VXIVector *keys,
  154.                          const VXIVector *values,
  155.                          void *)
  156. {
  157.   if (!gblOs.good()) return;
  158.   gblOs << L"Error (module='" << moduleName << L"' id='" << errorID;
  159.   VXIunsigned len = VXIVectorLength(keys);
  160.   if (len != VXIVectorLength(values) || len == 0) {
  161.     gblOs << L"')" << endl;
  162.   }
  163.   else {
  164.     gblOs << L"') : ";
  165.     for (VXIunsigned i = 0; i < len; ++i) {
  166.       const VXIValue * key = VXIVectorGetElement(keys, i);
  167.       const VXIValue * val = VXIVectorGetElement(values, i);
  168.       gblOs << key << L"='" << val << L"' ";
  169.     }
  170.     gblOs << endl;
  171.   }
  172. }
  173. void LoggerDiagnosticListener(SBlogInterface *,
  174.                              VXIunsigned,
  175.                              const VXIchar *,
  176.                              time_t,
  177.      VXIunsigned,
  178.                              const VXIchar* printmsg,
  179.                              void *)
  180. {
  181.   if (!gblOs.good()) return;
  182.   if (printmsg == NULL) printmsg = L"";
  183.   gblOs << L"Info: " << printmsg << endl;
  184. }
  185. // ------*---------*---------*---------*---------*---------*---------*---------
  186. void Expand(std::wstring & output, const char * const input)
  187. {
  188.   output.erase();
  189.   if (input == NULL || *input == '') return;
  190.   for (const char * c = input; *c != ''; ++c)
  191.     output += *c;
  192. }
  193. #ifdef WIN32
  194. #include "Windows.h"
  195. #else
  196.   typedef unsigned long DWORD;
  197.   #define GetTickCount clock
  198. #endif
  199. class ElapsedTime
  200. {
  201. public:
  202. ElapsedTime()
  203. {
  204.     start = GetTickCount();
  205. }
  206. ~ElapsedTime()
  207. {
  208.     printf( "ElapsedTime = %d", GetTickCount() - start );
  209. }
  210. DWORD start;
  211. };
  212. int main(int argc, char *argv[])
  213. {
  214.     ElapsedTime elapsed;
  215.     DWORD start = GetTickCount();
  216.   // (0) Get program name and provide usage info.
  217.   std::wstring PROGRAM;
  218.   Expand(PROGRAM, argv[0]);
  219.   if (argc < 2) {
  220.     wcout << PROGRAM << 
  221.       L": Usage - this program will attempt to open and validate "
  222.       L"VXML documents.  You may specifiy one or more URIs on the command line"
  223.       << endl;
  224.     return 1;
  225.   }
  226.   VXIlogInterface  *log = NULL;
  227.   // (1) Initialize a log object.
  228.   if (SBlogInit() != VXIlog_RESULT_SUCCESS) {
  229.     wcout << PROGRAM << L": Unable to initialize SBlog." << endl;
  230.     return -1;
  231.   }
  232.   if (SBlogCreateResource(&log) != VXIlog_RESULT_SUCCESS || log == NULL) {
  233.     wcout << PROGRAM << L": Unable to create SBlog instance." << endl;
  234.     return -1;
  235.   }
  236.   SBlogInterface * temp = reinterpret_cast<SBlogInterface *>(log);
  237.   temp->RegisterErrorListener(temp, LoggerErrorListener, NULL);
  238.   temp->RegisterDiagnosticListener(temp, LoggerDiagnosticListener, NULL);
  239.   temp->ControlDiagnosticTag(temp, ID_INTERPRETER, TRUE);
  240.   // (2) Do other one time initialization:
  241.   VXIMapHolder extensionRules;
  242.   if (SBinetInit(log, ID_INET, NULL, 100, 2, 3600, NULL, 0, NULL,
  243.                  extensionRules.GetValue(), NULL) != VXIinet_RESULT_SUCCESS)
  244.   {
  245.     wcout << PROGRAM << L": Unable to initialize SBinet." << endl;
  246.     return -1;
  247.   }
  248.   if (VXIinterpreterInit(log, ID_INTERPRETER, NULL) != VXIinterp_RESULT_SUCCESS)
  249.   {
  250.     wcout << PROGRAM << L": Unable to initialize VXIinterpreter." << endl;
  251.     return -1;
  252.   }
  253.   // (3) Create resources.
  254.   VXIinetInterface * inet = NULL;
  255.   if (SBinetCreateResource(log, NULL, &inet) != VXIinet_RESULT_SUCCESS ||
  256.       inet == NULL)
  257.   {
  258.     wcout << PROGRAM << L": Unable to create SBinet instance." << endl;
  259.     return -1;
  260.   }
  261.   VXIresources resources;
  262.   resources.cache  = NULL;
  263.   resources.inet   = inet;
  264.   resources.jsi    = NULL;
  265.   resources.log    = log;
  266.   resources.object = NULL;
  267.   resources.prompt = NULL;
  268.   resources.rec    = NULL;
  269.   resources.tel    = NULL;
  270.   VXIinterpreterInterface * interpreter = NULL;
  271.   if (VXIinterpreterCreateResource(&resources, &interpreter)
  272.       != VXIinterp_RESULT_SUCCESS || interpreter == NULL)
  273.   {
  274.     wcout << PROGRAM << L": Unable to create VXIinterpreter instance." << endl;
  275.     return -1;
  276.   }
  277.   // (4) Validate document.
  278.   bool failure = false;
  279.   std::wstring filename;
  280.   for (int i = 1; i < argc; ++i) {
  281.     wcout << L"-----------------------------------------------------------"
  282.              L"-------------------" << endl;
  283.     Expand(filename, argv[i]);
  284.     VXIinterpreterResult result
  285.       = interpreter->Validate(interpreter, filename.c_str());
  286.     if (result == VXIinterp_RESULT_SUCCESS) {
  287.       wcout << L"VALID: " << filename << endl;
  288.       LoggerClear();
  289.       continue;
  290.     }
  291.     switch (result) {
  292.     case VXIinterp_RESULT_FETCH_TIMEOUT:   // document not retrieved in time
  293.       wcout << L"FAILED: Retrieval failed due to timeout - "
  294.             << filename << endl;
  295.       break;
  296.     case VXIinterp_RESULT_FETCH_ERROR: // document retrieval failed
  297.       wcout << L"FAILED: Retrieval failed - " << filename << endl;
  298.       break;
  299.     case VXIinterp_RESULT_FAILURE:     // document is invalid VXML
  300.       wcout << L"INVALID: " << filename << endl;
  301.       break;
  302.     default:
  303.       wcout << L"FAILED: Unexpected error while processing " << filename<<endl;
  304.       break;
  305.     }
  306.     LoggerPrint();
  307.     LoggerClear();
  308.     failure = true;
  309.   }
  310.   // (5) Destroy resources.
  311.   VXIinterpreterDestroyResource(&interpreter);
  312.   SBinetDestroyResource(&inet);
  313.   
  314.   // (6) Final shutdown.
  315.   VXIinterpreterShutDown(log);
  316.   SBinetShutDown(log);
  317.   // (7) Clean up log.
  318.   SBlogDestroyResource(&log);
  319.   SBlogShutDown();
  320.   // (8) 
  321.   if (failure) return 1;
  322.   return 0;
  323. }