private string NormalizeActivityText(string textFormat, string text) { if (string.IsNullOrWhiteSpace(text)) { return(string.Empty); } // Default to markdown if it isn't specified. if (textFormat == null) { textFormat = TextFormatTypes.Markdown; } string plainText; if (textFormat.Equals(TextFormatTypes.Plain, StringComparison.Ordinal)) { plainText = text; } else if (textFormat.Equals(TextFormatTypes.Markdown, StringComparison.Ordinal)) { plainText = AlexaMarkdownToPlaintextRenderer.Render(text); } else // xml format or other unknown and unsupported format. { plainText = string.Empty; } if (!SecurityElement.IsValidText(plainText)) { plainText = SecurityElement.Escape(plainText); } return(plainText); }
/// <summary> /// Verilen metin içindeki XML'e özel karakterleri ', /// > gibi eşleniğine çevirir. /// </summary> /// <param name="s"></param> /// <returns></returns> public string EscPeakML(string s) { if (string.IsNullOrEmpty(s)) { return(s); } return(!SecurityElement.IsValidText(s) ? SecurityElement.Escape(s) : s); }
public static string EscapeXML(this string s) { //if (string.IsNullOrEmpty(s) && (!s.Contains("&"))) // return s; return(SecurityElement.Escape(s)); return(!SecurityElement.IsValidText(s) ? SecurityElement.Escape(s) : s); }
//public static string XmlEscape(string unescaped) //{ // XmlDocument xmlDoc = new XmlDocument(); // XmlNode node = xmlDoc.CreateElement("root"); // node.InnerText = unescaped; // return node.InnerXml; //} //public static string XmlUnescape(string escaped) //{ // XmlDocument xmlDoc = new XmlDocument(); // XmlNode node = xmlDoc.CreateElement("root"); // node.InnerXml = escaped; // return node.InnerText; //} // public static string XmlEncode(this string value) // { // var output = new StringBuilder(); // var text = new XText(value); // using (var writer = XmlWriter.Create(output, new XmlWriterSettings { ConformanceLevel = ConformanceLevel.Fragment })) // { // text.WriteTo(writer); // writer.Flush(); // return output.ToString(); // } // } // public static string XmlDecode(this string value) // { // if (value.Length == 0) // return value; // var output = new StringBuilder(); // var reader = XmlReader.Create(new StringReader(value), new XmlReaderSettings { ConformanceLevel = ConformanceLevel.Fragment }); // reader.MoveToContent(); // var text = (XText)XText.ReadFrom(reader); // return text.Value; // } public static string EscapeXml(string xml) { if (string.IsNullOrEmpty(xml)) { return(xml); } return(!SecurityElement.IsValidText(xml) ? SecurityElement.Escape(xml) : xml); }
public void IsValidText() { Assert.True(SecurityElement.IsValidText("x x")); Assert.False(SecurityElement.IsValidText("x<x")); Assert.False(SecurityElement.IsValidText("x>x")); Assert.True(SecurityElement.IsValidText("x\"x")); Assert.True(SecurityElement.IsValidText("x'x")); Assert.True(SecurityElement.IsValidText("x&x")); Assert.False(SecurityElement.IsValidText(null)); Assert.True(SecurityElement.IsValidText(string.Empty)); }
public void IsValidText() { Assert.IsTrue(SecurityElement.IsValidText("x x"), "#1"); Assert.IsFalse(SecurityElement.IsValidText("x<x"), "#2"); Assert.IsFalse(SecurityElement.IsValidText("x>x"), "#3"); Assert.IsTrue(SecurityElement.IsValidText("x\"x"), "#4"); Assert.IsTrue(SecurityElement.IsValidText("x'x"), "#5"); Assert.IsTrue(SecurityElement.IsValidText("x&x"), "#6"); Assert.IsFalse(SecurityElement.IsValidText(null), "#7"); Assert.IsTrue(SecurityElement.IsValidText(string.Empty), "#8"); }
// Add a child element to the specified security element. private static SecurityElement AddChildElement( SecurityElement parentElement, string tagName, string tagText) { if (parentElement != null) { // Ensure that the tag text is in valid XML format. //<Snippet9> if (!SecurityElement.IsValidText(tagText)) //</Snippet9> { // Replace invalid text with valid XML text // to enforce proper XML formatting. //<Snippet19> tagText = SecurityElement.Escape(tagText); //</Snippet19> } // Determine whether the tag is in valid XML format. //<Snippet10> if (SecurityElement.IsValidTag(tagName)) //</Snippet10> { //<Snippet24> SecurityElement childElement; childElement = parentElement.SearchForChildByTag(tagName); //</Snippet24> if (childElement != null) { //<Snippet25> String elementText; elementText = parentElement.SearchForTextOfTag(tagName); //</Snippet25> if (!elementText.Equals(tagText)) { // Add child element to the parent security element. parentElement.AddChild( new SecurityElement(tagName, tagText)); } } else { // Add child element to the parent security element. parentElement.AddChild( new SecurityElement(tagName, tagText)); } } } return(parentElement); }
public void WriteText(string text) { if (!SecurityElement.IsValidText(text)) { throw new ArgumentException(); } if (_needCloseElementStart) { this.Write(">"); } this.Write(SecurityElement.Escape(text)); _needCloseElementStart = false; }
private ConversationResponseBody CreateConversationResponseFromLastActivity(IEnumerable <Activity> activities, ITurnContext context) { var activity = activities != null && activities.Any() ? activities.Last() : null; if (!SecurityElement.IsValidText(activity.Text)) { activity.Text = SecurityElement.Escape(activity.Text); } var response = new ConversationResponseBody(); var userStorage = new JObject { { "UserId", context.TurnState["GoogleUserId"].ToString() } }; response.UserStorage = userStorage.ToString(); if (activity?.Attachments != null && activity.Attachments.FirstOrDefault(a => a.ContentType == SigninCard.ContentType) != null) { response.ExpectUserResponse = true; response.ResetUserStorage = null; response.ExpectedInputs = new ExpectedInput[] { new ExpectedInput() { PossibleIntents = new PossibleIntent[] { new PossibleIntent() { Intent = "actions.intent.SIGN_IN", InputValueData = new InputValueData() { type = "type.googleapis.com/google.actions.v2.SignInValueSpec" } }, }, InputPrompt = new InputPrompt() { RichInitialPrompt = new RichResponse() { Items = new Item[] { new SimpleResponse() { Content = new SimpleResponseContent() { TextToSpeech = "PLACEHOLDER" } } } } } } }; return(response); } if (!string.IsNullOrEmpty(activity?.Text)) { var simpleResponse = new SimpleResponse { Content = new SimpleResponseContent { DisplayText = activity.Text, Ssml = activity.Speak, TextToSpeech = activity.Text } }; var responseItems = new List <Item> { simpleResponse }; // Add Google card to response if set AddCardToResponse(context, ref responseItems, activity); // Add Media response to response if set AddMediaResponseToResponse(context, ref responseItems, activity); if (activity.InputHint == null || activity.InputHint == InputHints.AcceptingInput) { activity.InputHint = ShouldEndSessionByDefault ? InputHints.IgnoringInput : InputHints.ExpectingInput; } // check if we should be listening for more input from the user switch (activity.InputHint) { case InputHints.IgnoringInput: response.ExpectUserResponse = false; response.FinalResponse = new FinalResponse() { RichResponse = new RichResponse() { Items = responseItems.ToArray() } }; break; case InputHints.ExpectingInput: response.ExpectUserResponse = true; response.ExpectedInputs = new ExpectedInput[] { new ExpectedInput() { PossibleIntents = new PossibleIntent[] { new PossibleIntent() { Intent = "actions.intent.TEXT" }, }, InputPrompt = new InputPrompt() { RichInitialPrompt = new RichResponse() { Items = responseItems.ToArray() } } } }; var suggestionChips = AddSuggestionChipsToResponse(context, activity); if (suggestionChips.Any()) { response.ExpectedInputs.First().InputPrompt.RichInitialPrompt.Suggestions = suggestionChips.ToArray(); } break; default: break; } } else { response.ExpectUserResponse = false; } return(response); }
private static string EscapeXML(string nodeText) { return((SecurityElement.IsValidText(nodeText))? nodeText : SecurityElement.Escape(nodeText)); }
// Test the valid string testing operators. public void TestSecurityElementValidStrings() { Assert("ValidAttrName (1)", !SecurityElement.IsValidAttributeName(null)); Assert("ValidAttrName (2)", !SecurityElement.IsValidAttributeName("")); Assert("ValidAttrName (3)", !SecurityElement.IsValidAttributeName("a<b")); Assert("ValidAttrName (4)", !SecurityElement.IsValidAttributeName("a>b")); Assert("ValidAttrName (5)", !SecurityElement.IsValidAttributeName("&")); Assert("ValidAttrName (6)", !SecurityElement.IsValidAttributeName(" ")); Assert("ValidAttrName (7)", SecurityElement.IsValidAttributeName("fooBar")); Assert("ValidAttrName (8)", SecurityElement.IsValidAttributeName("123")); Assert("ValidAttrValue (1)", !SecurityElement.IsValidAttributeValue(null)); Assert("ValidAttrValue (2)", SecurityElement.IsValidAttributeValue("")); Assert("ValidAttrValue (3)", !SecurityElement.IsValidAttributeValue("a<b")); Assert("ValidAttrValue (4)", !SecurityElement.IsValidAttributeValue("a>b")); Assert("ValidAttrValue (5)", SecurityElement.IsValidAttributeValue("&")); Assert("ValidAttrValue (6)", SecurityElement.IsValidAttributeValue(" ")); Assert("ValidAttrValue (7)", SecurityElement.IsValidAttributeValue("fooBar")); Assert("ValidAttrValue (8)", SecurityElement.IsValidAttributeValue("123")); Assert("ValidAttrValue (9)", !SecurityElement.IsValidAttributeValue("\"")); Assert("ValidTag (1)", !SecurityElement.IsValidTag(null)); Assert("ValidTag (2)", !SecurityElement.IsValidTag("")); Assert("ValidTag (3)", !SecurityElement.IsValidTag("a<b")); Assert("ValidTag (4)", !SecurityElement.IsValidTag("a>b")); Assert("ValidTag (5)", !SecurityElement.IsValidTag("&")); Assert("ValidTag (6)", !SecurityElement.IsValidTag(" ")); Assert("ValidTag (7)", SecurityElement.IsValidTag("fooBar")); Assert("ValidTag (8)", SecurityElement.IsValidTag("123")); Assert("ValidText (1)", !SecurityElement.IsValidText(null)); Assert("ValidText (2)", SecurityElement.IsValidText("")); Assert("ValidText (3)", !SecurityElement.IsValidText("a<b")); Assert("ValidText (4)", !SecurityElement.IsValidText("a>b")); Assert("ValidText (5)", SecurityElement.IsValidText("&")); Assert("ValidText (6)", SecurityElement.IsValidText(" ")); Assert("ValidText (7)", SecurityElement.IsValidText("fooBar")); Assert("ValidText (8)", SecurityElement.IsValidText("123")); Assert("ValidText (9)", SecurityElement.IsValidText("\"")); }
/// <summary> /// Escapes this string, so it may be stored inside an XML format /// </summary> public static string EscapeXML(this string s) { return(!SecurityElement.IsValidText(s) ? SecurityElement.Escape(s) : s); }
private AlexaResponseBody CreateResponseFromLastActivity(IEnumerable <Activity> activities, ITurnContext context) { var response = new AlexaResponseBody() { Version = "1.0", Response = new AlexaResponse() { ShouldEndSession = context.GetAlexaRequestBody().Request.Type == AlexaRequestTypes.SessionEndedRequest || ShouldEndSessionByDefault } }; if (context.GetAlexaRequestBody().Request.Type == AlexaRequestTypes.SessionEndedRequest && (activities == null || !activities.Any())) { response.Response.OutputSpeech = new AlexaOutputSpeech() { Type = AlexaOutputSpeechType.PlainText, Text = string.Empty }; return(response); } var activity = activities.First(); // https://github.com/alexa/alexa-skills-kit-sdk-for-nodejs/issues/25 // https://stackoverflow.com/questions/53019696/special-characters-not-supported-by-aws-polly/53020501#53020501 // Fixed the above issues if (!SecurityElement.IsValidText(activity.Text)) { activity.Text = SecurityElement.Escape(activity.Text); } if (activity.Type == ActivityTypes.EndOfConversation) { response.Response.ShouldEndSession = true; } if (!string.IsNullOrEmpty(activity.Speak)) { response.Response.OutputSpeech = new AlexaOutputSpeech() { Type = AlexaOutputSpeechType.SSML, Ssml = activity.Speak.Contains("<speak>") ? activity.Speak : $"<speak>{activity.Speak}</speak>", }; if (!string.IsNullOrEmpty(activity.Text)) { response.Response.OutputSpeech.Text = $"{activity.Text} "; } } else if (!string.IsNullOrEmpty(activity.Text)) { if (response.Response.OutputSpeech == null) { response.Response.OutputSpeech = new AlexaOutputSpeech() { Type = AlexaOutputSpeechType.PlainText, Text = activity.Text }; } } if (context.TurnState.ContainsKey("AlexaReprompt")) { var repromptSpeech = context.TurnState.Get <string>("AlexaReprompt"); response.Response.Reprompt = new Reprompt() { OutputSpeech = new AlexaOutputSpeech() { Type = AlexaOutputSpeechType.SSML, Ssml = repromptSpeech.Contains("<speak>") ? repromptSpeech : $"<speak>{repromptSpeech}</speak>" } }; } AddDirectivesToResponse(context, response); AddCardToResponse(context, response, activity); switch (activity.InputHint) { case InputHints.IgnoringInput: response.Response.ShouldEndSession = true; break; case InputHints.ExpectingInput: response.Response.ShouldEndSession = false; break; case InputHints.AcceptingInput: default: break; } return(response); }