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

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 _EXE_CONT
  23. #define _EXE_CONT
  24. #include "VXIvalue.h"
  25. #include <vector>
  26. extern "C" struct VXIjsiInterface;
  27. extern "C" struct VXIjsiContext;
  28. #include <xercesc/dom/DOMDocument.hpp>
  29. /**
  30.  * ECMA Script evaluation engine.
  31.  *
  32.  * Using the VXIjsi interface, this layer is employed by the VoiceXML 
  33.  * Interpreter to set and retrieve script variables and evaluate expressions.<p>
  34.  *
  35.  * All functions, except the destructor, may throw exceptions.  These fall
  36.  * into two classes.   The interpreter events represent semantic errors
  37.  * which could in principal be handled by the application.  And the severe
  38.  * JavaScriptErrors indicate serious errors with the VXIjsi layer.<p>
  39.  * 
  40.  *   VXIException::InterpreterEvent(EV_ERROR_ECMASCRIPT)<br>
  41.  *   VXIException::JavaScriptError();<br>
  42.  */
  43. class Scripter {
  44. public:
  45.   Scripter(VXIjsiInterface* jsiapi);
  46.   ~Scripter();
  47.   /** 
  48.    * Determines whether or not the named variable is defined.
  49.    * 
  50.    * @return true if variable has been defined
  51.    *         false otherwise
  52.    */
  53.   bool IsVarDefined(const vxistring & name);
  54.   /** 
  55.    * Determines whether or not the named variable is declared.
  56.    * 
  57.    * @return true if variable has been declared
  58.    *         false otherwise
  59.    */
  60.   bool IsVarDeclared(const vxistring & name);
  61.   /** 
  62.    * Creates a new variablevariable to the indicated expression.
  63.    */
  64.   void MakeVar(const vxistring & name, const vxistring & expr);
  65.   /** 
  66.    * Creates a new variablevariable to the indicated expression.
  67.    */
  68.   void MakeVar(const vxistring & name, const VXIValue * value);
  69.   /**
  70.    * Sets an existing variable to the indicated DOMDocument.
  71.    */
  72.   void MakeVar(const vxistring & name, xercesc::DOMDocument *doc);
  73.   /** 
  74.    * Sets an existing variable to the indicated expression.
  75.    */
  76.   void SetVar(const vxistring & name, const vxistring & expr);
  77.   /**
  78.    * Sets a variable to read-only.
  79.    */
  80.   void SetVarReadOnly(const vxistring & name);
  81.   /** 
  82.    * Sets a variable to 'undefined'.
  83.    */
  84.   void ClearVar(const vxistring & name);
  85.   /** 
  86.    * Creates a new variable or sets an existing variable to the indicated
  87.    * string.
  88.    */
  89.   void SetString(const vxistring & var, const vxistring & val);
  90.   /** 
  91.    * Creates a new variable or sets an existing variable to the indicated
  92.    * value type.  This may be used for complex types such as Object.
  93.    */
  94.   void SetValue(const vxistring & var, const VXIValue * value);
  95.   /** 
  96.    * Retrieves a variable from ECMAScript.  
  97.    * 
  98.    * The conversion is as follows:<br>
  99.    *   Undefined, Null       --> NULL pointer<br>
  100.    *   String                --> VXIString<br>
  101.    *   Boolean               --> VXIFloat or VXIInteger {true, false} -> {1, 0}<br>
  102.    *   Number                --> VXIFloat or VXIDouble or VXIInteger<br>
  103.    *   Object                --> VXIMap<br><br>
  104.    * 
  105.    * NOTE: The caller must free the resulting VXIValue.
  106.    */
  107.   VXIValue* GetValue(const vxistring & name) const;
  108.   /** 
  109.    * Pushes a new variable scope onto the stack with the given name.  New
  110.    * variables will be defined in this scope, unless they are explicitly
  111.    * referenced using <scope>.<variable_name> as in application.lastresult$.
  112.    */
  113.   void PushScope(const vxistring & name, bool alias = false);
  114.   /** 
  115.    * Pops the current scope, destroying it and any associated variables.
  116.    */
  117.   void PopScope();
  118.   /** 
  119.    * Checks the name of the current scope against the argument.
  120.    * 
  121.    * @return true if the current scope matches the requested name.
  122.    *         false otherwise
  123.    */
  124.   bool CurrentScope(const vxistring & name) const;
  125.   /** 
  126.    * Evaluates an ECMAScript script.
  127.    */
  128.   void EvalScript(const vxistring & script);
  129.   /** 
  130.    * Evaluates an ECMAScript script, and converts the result to an integer.
  131.    */
  132.   VXIint EvalScriptToInt(const vxistring & script);
  133.   /** 
  134.    * Evaluates an ECMAScript script, and converts the result to a string.
  135.    */
  136.   void EvalScriptToString(const vxistring & script, vxistring & result);
  137.   /** 
  138.    * Evaluates an ECMAScript script, and converts the result to a string.
  139.    * NOTE: The caller must free the resulting VXIValue.
  140.    */
  141.   VXIValue * EvalScriptToValue(const vxistring & script);
  142.   /** 
  143.    * Evaluates an expression as a boolean.
  144.    */
  145.   bool TestCondition(const vxistring & script);
  146. private:
  147.   void maybe_throw_js_error(int err, const VXIchar *script = NULL) const;
  148.   Scripter(const Scripter &);                 // no implementation
  149.   Scripter & operator=(const Scripter &);     // no implementation
  150. private:
  151.   typedef std::vector<vxistring> SCOPESTACK;
  152.   SCOPESTACK scopeStack;
  153.   VXIjsiInterface * jsi_api;
  154.   VXIjsiContext * jsi_context;
  155. };
  156. #endif