public void Test_ReplaceVariableWithValue_Field() { string sTest = "I drive a ^car1, but I'm saving up for a ^car2."; string sRes = ""; DField fieldTmp = null; DForm formTmp = null; formTmp = new DForm(null); fieldTmp = new DField(formTmp); fieldTmp.Variables.Add(new DVariable("car1", "Yugo")); fieldTmp.Variables.Add(new DVariable("car2", "Kia")); sRes = m_oDE.ReplaceVariablenameWithValue(fieldTmp, sTest); Assert.That(sRes == "Kia"); } // Test_ReplaceVariableWithValue_Field
public void Test_ReplaceAllVarsInPlace() { string sTest = "I drive a ^car1, but I'm saving up for a ^car2."; string sRes = ""; DSession sesTmp = null; DDocument docTmp = null; DForm formTmp = null; DField fieldTmp = null; sesTmp = new DSession(0); docTmp = new DDocument(sesTmp); formTmp = new DForm(docTmp); fieldTmp = new DField(formTmp); formTmp.Variables.Add(new DVariable("car1", "Yugo")); fieldTmp.Variables.Add(new DVariable("car2", "Kia")); sRes = m_oDE.ReplaceAllVarsInPlace(fieldTmp, sTest); Assert.That(sRes == "I drive a Yugo, but I'm saving up for a Kia."); } //
/// <summary> /// Note: Until javascript in Mono is complete and stable, this will interpret the script code. /// For simplicity's sake, the only supported format is: /// variable = FullQualifiedObject(param0, param1, ...); /// The variable is expected to be in field scope, and is optional. Parameters can only be of /// type string, and are always passed by value. /// </summary> /// <param name="i_dfActive"></param> /// <param name="i_iOptionIndex"></param> /// <returns></returns> // public static bool ScriptExecute(ILegacyLogger i_Logger, string i_sVmcId, DField i_dfField, DAction i_daAction) public static bool ScriptExecute(ILegacyLogger i_Logger, string i_sVmcId, ISubdocContext i_SubdocContext, DAction i_daAction) { bool bRet = true, bRes = true; int ii = 0, iIndexStart = 0; System.Int32 iTmp = 0; // I know, same as 'int', but... DScript dsCode; string sStatement, sVarName, sVarNameShort, sRes = ""; DVariable dvTmp = null; try { // FIX - The parsing code is currently a hack to get a demo working. // FIX - The variables we look for in the javascript may be VoiceXML variables. // Right now we only handle two types of statements: variable declarations (with or // without assignment) and function calls (w/ or w/out assignment.) All parameters and // return values from function calls are of type string. Any text results to be TTSed // need to be done through the reserved statement 'document.writeln(svar);'. // dsCode = (DScript)i_daAction.m_oValue; for (ii = 0; ii < dsCode.Code.Count; ii++) { dvTmp = null; sStatement = dsCode.Code[ii]; // Check if there is a variable declared if (sStatement.IndexOf("var ") >= 0) { sVarName = ScriptExtractVariableName(sStatement.Substring(4)); iTmp = sVarName.IndexOf('.'); if (iTmp < 0) { sVarNameShort = sVarName; } else { sVarNameShort = sVarName.Substring(iTmp + 1); } dvTmp = DialogEngine.DialogEngine.FindVariableByName(i_SubdocContext, sVarName); // Reuse variable if it exists, even though script has (erroneously) declared a new one. if (dvTmp == null) { // Allocate variable and put it in proper scope. dvTmp = new DVariable(); dvTmp.Name = sVarNameShort; iIndexStart = sStatement.IndexOf("var ") + 4; if (i_SubdocContext.GetType() == typeof(DField)) { DField dfField = (DField)i_SubdocContext; if (sVarName.IndexOf("session.") >= 0) { dfField.m_dfParentForm.m_ddParentDocument.m_dsParentSession.m_DVariables.Add(dvTmp); } else if (sVarName.IndexOf("application.") >= 0) { // FIX - Keep it in session since we don't currently have an "application" in USCs? dfField.m_dfParentForm.m_ddParentDocument.m_dsParentSession.m_DVariables.Add(dvTmp); } else if (sVarName.IndexOf("document.") >= 0) { dfField.m_dfParentForm.m_ddParentDocument.m_DVariables.Add(dvTmp); } else if (sVarName.IndexOf("dialog.") >= 0) { dfField.m_dfParentForm.Variables.Add(dvTmp); } else { i_SubdocContext.Variables.Add(dvTmp); } } else if (i_SubdocContext.GetType() == typeof(DForm)) { DForm dfForm = (DForm)i_SubdocContext; if (sVarName.IndexOf("session.") >= 0) { dfForm.m_ddParentDocument.m_dsParentSession.m_DVariables.Add(dvTmp); } else if (sVarName.IndexOf("application.") >= 0) { // FIX - Keep it in session since we don't currently have an "application" in USCs? dfForm.m_ddParentDocument.m_dsParentSession.m_DVariables.Add(dvTmp); } else if (sVarName.IndexOf("document.") >= 0) { dfForm.m_ddParentDocument.m_DVariables.Add(dvTmp); } else if (sVarName.IndexOf("dialog.") >= 0) { dfForm.Variables.Add(dvTmp); } else { i_SubdocContext.Variables.Add(dvTmp); } } else { i_Logger.Log(Level.Exception, string.Format("[{0}]DialogEngine.ScriptExecute - invalid type '{1}'.", i_sVmcId, i_SubdocContext.GetType().ToString())); } } } else if (sStatement.IndexOf(m_csBoolEqual) >= 0) { i_Logger.Log(Level.Warning, "[" + i_sVmcId + "]" + "DialogEngine.ScriptExecute - Boolean conditions not yet supported."); } else if (sStatement.IndexOf('=') >= 0) { sVarName = sStatement.Substring(0, sStatement.IndexOfAny(DialogEngine.VoiceXmlParser.s_acBreakNonVar)); dvTmp = DialogEngine.DialogEngine.FindVariableByName(i_SubdocContext, sVarName); if (dvTmp == null) { i_Logger.Log(Level.Exception, "[" + i_sVmcId + "]" + "DialogEngine.ScriptExecute - Variable named '" + sVarName + "' was not found, line " + ii + " '" + sStatement + "'."); // Create a new temporary variable so we can keep going // FIX - probably ought to return a failure if we get here... dvTmp = new DVariable(); dvTmp.Name = sVarName; } } else { // No variable assigned to the return value, so create a temporary variable dvTmp = new DVariable("TEMPORARY", ""); } // if 'var' // Check if there is an assignment iIndexStart = sStatement.IndexOf('='); if (iIndexStart >= 0) { iIndexStart++; bRes = ScriptAssignVariable(i_Logger, i_sVmcId, i_SubdocContext, dvTmp, sStatement.Substring(iIndexStart).Trim()); } else { // Check for a function call that doesn't assign a return value to a variable if (sStatement.IndexOf('(') >= 0) { sRes = ScriptCallFunction(i_Logger, i_sVmcId, i_SubdocContext, sStatement.TrimEnd(DialogEngine.VoiceXmlParser.s_acTerminator)); } else { i_Logger.Log(Level.Info, "[" + i_sVmcId + "]" + "DialogEngine.ExecuteScript - Seem to have found a no-op on line " + ii + ": '" + sStatement + "'."); } } } // for } catch (Exception e) { bRet = false; i_Logger.Log(Level.Exception, "[" + i_sVmcId + "]" + "DialogEngine.ScriptExecute: " + e.ToString()); } return(bRet); }