private void SetXml(string s) { if (String.IsNullOrEmpty(s)) { return; } XDocument xdoc = XDocument.Parse(s); string formattedText = xdoc.ToString().Trim(); if (String.IsNullOrEmpty(formattedText)) { return; } XmlStateMachine machine = new XmlStateMachine(); if (s.StartsWith("<?")) { string xmlDeclaration = machine.GetXmlDeclaration(s); if (xmlDeclaration != String.Empty) { formattedText = xmlDeclaration + Environment.NewLine + formattedText; } } int location = 0; int failCount = 0; int tokenTryCount = 0; XmlTokenType ttype = XmlTokenType.Unknown; while (location < formattedText.Length) { string token = machine.GetNextToken(formattedText, location, out ttype); Color color = machine.GetTokenColor(ttype); this.AppendText(token, color); location += token.Length; tokenTryCount++; // Check for ongoing failure if (token.Length == 0) { failCount++; } if (failCount > 10 || tokenTryCount > formattedText.Length) { string theRestOfIt = formattedText.Substring(location, formattedText.Length - location); //this.AppendText(Environment.NewLine + Environment.NewLine + theRestOfIt); // DEBUG this.AppendText(theRestOfIt); break; } } }
public Color GetTokenColor(XmlTokenType ttype) { Color brown = Color.FromArgb(238, 149, 68); switch (ttype) { case XmlTokenType.NodeValue: case XmlTokenType.EqualSignStart: case XmlTokenType.EqualSignEnd: case XmlTokenType.DoubleQuotationMarkStart: case XmlTokenType.DoubleQuotationMarkEnd: case XmlTokenType.SingleQuotationMarkStart: case XmlTokenType.SingleQuotationMarkEnd: return(Color.Black); case XmlTokenType.XmlDeclarationStart: case XmlTokenType.XmlDeclarationEnd: case XmlTokenType.NodeStart: case XmlTokenType.NodeEnd: case XmlTokenType.NodeEndValueStart: case XmlTokenType.CDataStart: case XmlTokenType.CDataEnd: case XmlTokenType.CommentStart: case XmlTokenType.CommentEnd: case XmlTokenType.AttributeValue: case XmlTokenType.DocTypeStart: case XmlTokenType.DocTypeEnd: case XmlTokenType.DocTypeDefStart: case XmlTokenType.DocTypeDefEnd: return(Color.Blue); case XmlTokenType.CDataValue: case XmlTokenType.DocTypeDefValue: return(Color.Gray); case XmlTokenType.CommentValue: return(Color.Green); case XmlTokenType.DocTypeName: case XmlTokenType.NodeName: return(Color.Brown); case XmlTokenType.AttributeName: case XmlTokenType.DocTypeDeclaration: return(Color.Red); default: return(Color.Orange); } }
public Color GetTokenColor(XmlTokenType ttype) { switch (ttype) { case XmlTokenType.NodeValue: case XmlTokenType.EqualSignStart: case XmlTokenType.EqualSignEnd: case XmlTokenType.DoubleQuotationMarkStart: case XmlTokenType.DoubleQuotationMarkEnd: case XmlTokenType.SingleQuotationMarkStart: case XmlTokenType.SingleQuotationMarkEnd: return(Color.DimGray); case XmlTokenType.XmlDeclarationStart: case XmlTokenType.XmlDeclarationEnd: case XmlTokenType.NodeStart: case XmlTokenType.NodeEnd: case XmlTokenType.NodeEndValueStart: case XmlTokenType.CDataStart: case XmlTokenType.CDataEnd: case XmlTokenType.CommentStart: case XmlTokenType.CommentEnd: case XmlTokenType.AttributeValue: case XmlTokenType.DocTypeStart: case XmlTokenType.DocTypeEnd: case XmlTokenType.DocTypeDefStart: case XmlTokenType.DocTypeDefEnd: return(Color.DimGray); case XmlTokenType.CDataValue: case XmlTokenType.DocTypeDefValue: return(Color.SkyBlue); case XmlTokenType.CommentValue: return(Color.SkyBlue); case XmlTokenType.DocTypeName: case XmlTokenType.NodeName: return(Color.DarkCyan); case XmlTokenType.AttributeName: case XmlTokenType.DocTypeDeclaration: return(Color.DodgerBlue); default: return(Color.Orange); } }
private void HandleAttributeEnd() { if (subString.StartsWith(">")) { HandleReservedXmlToken(); } else if (subString.StartsWith("/>")) { HandleReservedXmlToken(); } else if (subString.StartsWith("?>")) { HandleReservedXmlToken(); } else { CurrentState = XmlTokenType.AttributeName; token = ReadAttributeName(subString); } }
public XmlToken(XmlTokenType type, TextPosition position, Boolean ignorable = false) { _type = type; _position = position; _ignorable = ignorable; }
private void HandleReservedXmlToken() { // check if state changer // <, >, =, </, />, <![CDATA[, <!--, --> if (subString.StartsWith("<![CDATA[")) { CurrentState = XmlTokenType.CDataStart; token = "<![CDATA["; } else if (subString.StartsWith("<!DOCTYPE")) { CurrentState = XmlTokenType.DocTypeStart; token = "<!"; } else if (subString.StartsWith("</")) { CurrentState = XmlTokenType.NodeStart; token = "</"; } else if (subString.StartsWith("<!--")) { CurrentState = XmlTokenType.CommentStart; token = "<!--"; } else if (subString.StartsWith("<?")) { CurrentState = XmlTokenType.XmlDeclarationStart; token = "<?"; } else if (subString.StartsWith("<")) { CurrentState = XmlTokenType.NodeStart; token = "<"; } else if (subString.StartsWith("=")) { CurrentState = XmlTokenType.EqualSignStart; if (CurrentState == XmlTokenType.AttributeValue) { CurrentState = XmlTokenType.EqualSignEnd; } token = "="; } else if (subString.StartsWith("?>")) { CurrentState = XmlTokenType.XmlDeclarationEnd; token = "?>"; } else if (subString.StartsWith(">")) { CurrentState = XmlTokenType.NodeEndValueStart; token = ">"; } else if (subString.StartsWith("-->")) { CurrentState = XmlTokenType.CommentEnd; token = "-->"; } else if (subString.StartsWith("]>")) { CurrentState = XmlTokenType.DocTypeEnd; token = "]>"; } else if (subString.StartsWith("]]>")) { CurrentState = XmlTokenType.CDataEnd; token = "]]>"; } else if (subString.StartsWith("/>")) { CurrentState = XmlTokenType.NodeEnd; token = "/>"; } else if (subString.StartsWith("\"")) { if (CurrentState == XmlTokenType.AttributeValue) { CurrentState = XmlTokenType.DoubleQuotationMarkEnd; } else { CurrentState = XmlTokenType.DoubleQuotationMarkStart; } token = "\""; } else if (subString.StartsWith("'")) { CurrentState = XmlTokenType.SingleQuotationMarkStart; if (CurrentState == XmlTokenType.AttributeValue) { CurrentState = XmlTokenType.SingleQuotationMarkEnd; } token = "'"; } }
public string GetNextToken(string s, out XmlTokenType ttype) { ttype = XmlTokenType.Unknown; // skip past any whitespace (token added to it at the end of method) string whitespace = GetWhitespace(s); subString = s.TrimStart(); token = string.Empty; if (CurrentState == XmlTokenType.CDataStart) { // check for empty CDATA if (subString.StartsWith("]]>")) { CurrentState = XmlTokenType.CDataEnd; token = "]]>"; } else { CurrentState = XmlTokenType.CDataValue; int n = subString.IndexOf("]]>"); token = subString.Substring(0, n); } } else if (CurrentState == XmlTokenType.DocTypeStart) { CurrentState = XmlTokenType.DocTypeName; token = "DOCTYPE"; } else if (CurrentState == XmlTokenType.DocTypeName) { CurrentState = XmlTokenType.DocTypeDeclaration; int n = subString.IndexOf("["); token = subString.Substring(0, n); } else if (CurrentState == XmlTokenType.DocTypeDeclaration) { CurrentState = XmlTokenType.DocTypeDefStart; token = "["; } else if (CurrentState == XmlTokenType.DocTypeDefStart) { if (subString.StartsWith("]>")) { CurrentState = XmlTokenType.DocTypeDefEnd; token = "]>"; } else { CurrentState = XmlTokenType.DocTypeDefValue; int n = subString.IndexOf("]>"); token = subString.Substring(0, n); } } else if (CurrentState == XmlTokenType.DocTypeDefValue) { CurrentState = XmlTokenType.DocTypeDefEnd; token = "]>"; } else if (CurrentState == XmlTokenType.DoubleQuotationMarkStart) { // check for empty attribute value if (subString[0] == '\"') { CurrentState = XmlTokenType.DoubleQuotationMarkEnd; token = "\""; } else { CurrentState = XmlTokenType.AttributeValue; int n = subString.IndexOf("\""); token = subString.Substring(0, n); } } else if (CurrentState == XmlTokenType.SingleQuotationMarkStart) { // check for empty attribute value if (subString[0] == '\'') { CurrentState = XmlTokenType.SingleQuotationMarkEnd; token = "\'"; } else { CurrentState = XmlTokenType.AttributeValue; int n = subString.IndexOf("'"); token = subString.Substring(0, n); } } else if (CurrentState == XmlTokenType.CommentStart) { // check for empty comment if (subString.StartsWith("-->")) { CurrentState = XmlTokenType.CommentEnd; token = "-->"; } else { CurrentState = XmlTokenType.CommentValue; token = ReadCommentValue(subString); } } else if (CurrentState == XmlTokenType.NodeStart) { CurrentState = XmlTokenType.NodeName; token = ReadNodeName(subString); } else if (CurrentState == XmlTokenType.XmlDeclarationStart) { CurrentState = XmlTokenType.NodeName; token = ReadNodeName(subString); } else if (CurrentState == XmlTokenType.NodeName) { if (subString[0] != '/' && subString[0] != '>') { CurrentState = XmlTokenType.AttributeName; token = ReadAttributeName(subString); } else { HandleReservedXmlToken(); } } else if (CurrentState == XmlTokenType.NodeEndValueStart) { if (subString[0] == '<') { HandleReservedXmlToken(); } else { CurrentState = XmlTokenType.NodeValue; token = ReadNodeValue(subString); } } else if (CurrentState == XmlTokenType.DoubleQuotationMarkEnd) { HandleAttributeEnd(); } else if (CurrentState == XmlTokenType.SingleQuotationMarkEnd) { HandleAttributeEnd(); } else { HandleReservedXmlToken(); } if (token != string.Empty) { ttype = CurrentState; return(whitespace + token); } return(string.Empty); }
/// <summary> /// Constructor /// </summary> /// <param name="text"></param> /// <param name="index"></param> /// <param name="type"></param> public XmlToken(string text, int index, XmlTokenType type) { this.text = text; this.index = index; this.type = type; }
public static void ReadXmlToken_Returns_Correct_XmlToken(string xml, int expectedTokenSize, XmlTokenType tokenType, string expectedXml = null) { var xmlBytes = Encoding.UTF8.GetBytes(xml); var expectedTokenBytes = xmlBytes; if (expectedXml != null) { expectedTokenBytes = Encoding.UTF8.GetBytes(expectedXml); } var buffer = new ReadOnlySequence <byte>(xmlBytes); var expectedToken = new XmlToken(new ReadOnlySequence <byte>(expectedTokenBytes), tokenType); (int actualTokenSize, var actualToken) = XmlTokenDeserializer.Deserialize(buffer); Assert.Equal(expectedTokenSize, actualTokenSize); Assert.Equal(expectedToken.Type, actualToken.Type); Assert.Equal(expectedToken.BufferSegments.ToArray(), actualToken.BufferSegments.ToArray()); }
public XmlToken(ReadOnlySequence <byte> bufferSegments, XmlTokenType type) { BufferSegments = bufferSegments; Type = type; }
/// <summary> /// Constructor /// </summary> /// <param name="text"></param> /// <param name="index"></param> /// <param name="type"></param> public XmlToken(string text, int index, XmlTokenType type) { this.text = text; this.index = index; this.type = type; }
public XmlToken(XmlTokenType type, TextPosition position, Boolean ignorable = false) { _type = type; _position = position; _ignorable = ignorable; }
/// <summary> /// Sets the default values. /// </summary> public XmlTagToken(XmlTokenType type, TextPosition position) : base(type, position) { _name = String.Empty; _attributes = new List <KeyValuePair <String, String> >(); }
/// <summary> /// Sets the default values. /// </summary> public XmlTagToken(XmlTokenType type, TextPosition position) : base(type, position) { _name = String.Empty; _attributes = new List<KeyValuePair<String, String>>(); }
public XmlToken(XmlTokenType type, TextPosition position) { _type = type; _position = position; }
public XmlToken(XmlTokenType type, TextPosition position) { _type = type; _position = position; }