/// <summary>
 /// This constructor is internal so that external users cannot construct it (and therefore we do not have to test it separately).
 /// Initialize output state to accept Rtf content (top-level sequences are therefore prohibited).
 /// </summary>
 internal XmlQueryOutput(XmlQueryRuntime runtime, XmlEventCache xwrt) {
     this.runtime = runtime;
     this.xwrt = xwrt;
     this.xstate = XmlState.WithinContent;
     this.depth = 1;
     this.rootType = XPathNodeType.Root;
 }
Esempio n. 2
0
 /// <summary>
 /// This constructor is internal so that external users cannot construct it (and therefore we do not have to test it separately).
 /// Initialize output state to accept Rtf content (top-level sequences are therefore prohibited).
 /// </summary>
 internal XmlQueryOutput(XmlQueryRuntime runtime, XmlEventCache xwrt)
 {
     _runtime = runtime;
     _xwrt = xwrt;
     _xstate = XmlState.WithinContent;
     _depth = 1;
     _rootType = XPathNodeType.Root;
 }
Esempio n. 3
0
 public override void AcceptName(RCToken token)
 {
     if (_state == XmlState.OpenTag)
     {
         _tags.Push(token.Text);
         _state = XmlState.Attributes;
     }
     else if (_state == XmlState.Attributes)
     {
         _attribute = token.Text;
     }
 }
Esempio n. 4
0
        private void RunForFile(string byteFilename, string xmlFilename)
        {
            XmlState xmlSpec = XmlStatePersistor.LoadState(xmlFilename);

            Assert.NotNull(xmlSpec);

            bool failed = false;

            using (StreamReader byteFile = new StreamReader(byteFilename))
            {
                while (!byteFile.EndOfStream)
                {
                    string[] parts = byteFile.ReadLine().Split(": ");
                    Assert.Equal(2, parts.Length);

                    int index = int.Parse(parts[0]);
                    int count = int.Parse(parts[1]);

                    Macro macroXml = xmlSpec.MacroPool.FirstOrDefault(m => m.Index == index);
                    Assert.NotNull(macroXml);

                    List <byte[]> data = Enumerable.Range(0, count).Select(x => byteFile.ReadLine().HexToByteArray()).ToList();

                    Assert.Equal(macroXml.Operations.Count, data.Count);

                    for (var i = 0; i < count; i++)
                    {
                        try
                        {
                            MacroOpBase converted = MacroOpManager.CreateFromData(data[i], false);
                            MacroOpBase op        = macroXml.Operations[i].ToMacroOp();
                            if (!Equals(converted, op))
                            {
                                output.WriteLine("Line {2}\nGot:\n {0}Expected:\n {1}", ToString(converted), ToString(op), i);
                                failed = true;
                            }
                        }
                        catch (Exception e)
                        {
                            output.WriteLine(e.Message + "\n");
                            failed = true;
                        }
                    }
                }
            }

            Assert.False(failed);
        }
        private void RunForFile(string filename)
        {
            string tmpFile  = Path.GetTempFileName();
            string fullPath = Path.Combine(AppContext.BaseDirectory, filename);

            XmlState profile = XmlStatePersistor.LoadState(fullPath);

            Assert.NotNull(profile);
            Assert.True(XmlStatePersistor.SaveState(tmpFile, profile));

            List <string> changes = CompileXmlChanges(CalculateFileVersion(profile.MajorVersion, profile.MinorVersion), fullPath, tmpFile);

            File.Delete(tmpFile);

            string res = String.Join(Environment.NewLine, changes);

            output.WriteLine(res);
            Assert.Equal("", res);
        }
Esempio n. 6
0
 public override void AcceptXmlBracket(RCToken token)
 {
     if (token.Text.Equals("<"))
     {
         _contents.Push(RCBlock.Empty);
         _attributes.Push(RCBlock.Empty);
         _state = XmlState.OpenTag;
     }
     else if (token.Text.Equals(">"))
     {
         if (_state == XmlState.Attributes)
         {
             _state = XmlState.Content;
         }
     }
     else if (token.Text.Equals("</") || token.Text.Equals("/>"))
     {
         // The current child is already at the top of the stack see "<"
         RCBlock child      = _contents.Pop();
         RCBlock attributes = _attributes.Pop();
         // null to be replaced with block of attributes
         if (child.Count == 0)
         {
             child = new RCBlock(attributes, "", ":", _text);
         }
         else
         {
             child = new RCBlock(attributes, "", ":", child);
         }
         RCBlock next = new RCBlock(_contents.Pop(), _tags.Pop(), ":", child);
         _contents.Push(next);
         _text  = _default;
         _state = XmlState.CloseTag;
     }
     else if (token.Text.Equals("="))
     {
     }
 }
 /// <summary>
 /// XmlRawWriter.WriteStartAttribute() with prefix, local-name, ns, and schema type.
 /// </summary>
 public void WriteStartAttributeUnchecked(string prefix, string localName, string ns) {
     Debug.Assert(this.xstate == XmlState.EnumAttrs, "WriteStartAttribute cannot be called in the " + this.xstate + " state.");
     Writer.WriteStartAttribute(prefix, localName, ns);
     this.xstate = XmlState.WithinAttr;
     this.depth++;
 }
 /// <summary>
 /// Call XmlRawWriter.WriteEndElement() with prefix, local-name, and ns.
 /// </summary>
 public void WriteEndElementUnchecked(string prefix, string localName, string ns) {
     Debug.Assert(this.xstate == XmlState.EnumAttrs || this.xstate == XmlState.WithinContent, "WriteEndElement cannot be called in the " + this.xstate + " state.");
     Writer.WriteEndElement(prefix, localName, ns);
     this.xstate = XmlState.WithinContent;
     this.depth--;
     if (this.nsmgr != null) this.nsmgr.PopScope();
 }
        /// <summary>
        /// Call XmlRawWriter.StartElementContent().
        /// </summary>
        public void StartElementContentUnchecked() {
            Debug.Assert(this.xstate == XmlState.EnumAttrs, "StartElementContent cannot be called in the " + this.xstate + " state.");

            // Output any cached namespaces
            if (this.cntNmsp != 0)
                WriteCachedNamespaces();

            Writer.StartElementContent();
            this.xstate = XmlState.WithinContent;
        }
        //-----------------------------------------------
        // XmlQueryOutput methods (XmlRawWriter)
        //-----------------------------------------------

        /// <summary>
        /// Call XmlRawWriter.WriteStartElement() with prefix, local-name, ns, and schema type.
        /// </summary>
        public void WriteStartElementUnchecked(string prefix, string localName, string ns) {
            Debug.Assert(this.xstate == XmlState.WithinContent, "WriteStartElement cannot be called in the " + this.xstate + " state.");
            if (this.nsmgr != null) this.nsmgr.PushScope();
            Writer.WriteStartElement(prefix, localName, ns);
            this.xstate = XmlState.EnumAttrs;
            this.depth++;
            this.useDefNmsp = ns.Length == 0;
        }
Esempio n. 11
0
 protected override void When()
 {
     _state = new XmlState();
 }
        /// <summary>
        /// Before writing a comment, perform various checks to ensure well-formedness.
        /// </summary>
        public void WriteStartComment() {
            // Xml state transitions
            ConstructWithinContent(XPathNodeType.Comment);

            this.nodeText.Clear();
            this.xstate = XmlState.WithinComment;
            this.depth++;
        }
        /// <summary>
        /// Before writing a namespace, perform various checks to ensure well-formedness.
        /// </summary>
        public void WriteStartNamespace(string prefix) {
            Debug.Assert(prefix != null, "Invalid argument");

            // Handle namespace attributes that are not sent directly to WriteNamespaceDeclaration
            ConstructInEnumAttrs(XPathNodeType.Namespace);
            this.piTarget/*nmspPrefix*/ = prefix;
            this.nodeText.Clear();

            this.xstate = XmlState.WithinNmsp;
            this.depth++;
        }
        /// <summary>
        /// Ensure that state transitions to EnumAttrs.
        /// </summary>
        private void ConstructInEnumAttrs(XPathNodeType rootType) {
            Debug.Assert(rootType == XPathNodeType.Attribute || rootType == XPathNodeType.Namespace);

            switch (this.xstate) {
                case XmlState.WithinSequence:
                    StartTree(rootType);
                    this.xstate = XmlState.EnumAttrs;
                    break;

                case XmlState.EnumAttrs:
                    // Already in EnumAttrs state
                    break;

                default:
                    // Construction is not allowed in this state
                    ThrowInvalidStateError(rootType);
                    break;
            }
        }
        /// <summary>
        /// Ensure that state transitions to WithinContent.
        /// </summary>
        private void ConstructWithinContent(XPathNodeType rootType) {
            Debug.Assert(rootType == XPathNodeType.Element || rootType == XPathNodeType.Comment || rootType == XPathNodeType.ProcessingInstruction);

            switch (this.xstate) {
                case XmlState.WithinSequence:
                    // If state is WithinSequence, call XmlSequenceWriter.StartTree
                    StartTree(rootType);
                    this.xstate = XmlState.WithinContent;
                    break;

                case XmlState.WithinContent:
                    // Already within element content
                    break;

                case XmlState.EnumAttrs:
                    // Start element content
                    StartElementContentUnchecked();
                    break;

                default:
                    // Construction is not allowed in this state
                    ThrowInvalidStateError(rootType);
                    break;
            }
        }
Esempio n. 16
0
        private void Process()
        {
            var state = XmlState.Other;
            var line  = 1;

            var ch = ReadText();

            _i = 0;
            while (ch > 0)
            {
                switch (ch)
                {
                case '\r':
                    ReadText('\n');
                    line++;
                    _lineOffsets.Add(_i + 1);
                    if (state == XmlState.Tag)
                    {
                        state = XmlState.Attribute;
                    }
                    break;

                case '\n':
                    line++;
                    _lineOffsets.Add(_i + 1);
                    if (state == XmlState.Tag)
                    {
                        state = XmlState.Attribute;
                    }
                    break;

                default:
                    switch (state)
                    {
                    case XmlState.Attribute:
                        if (ch == '=')
                        {
                            if (ReadText('"') || ReadText('\''))
                            {
                                state = XmlState.AttributeValue;
                            }
                        }
                        else if (ch == '>')
                        {
                            state = XmlState.Other;
                        }
                        break;

                    case XmlState.AttributeValue:
                        if (ch == '"' || ch == '\'')
                        {
                            state = XmlState.Tag;
                        }
                        break;

                    case XmlState.CData:
                        if (ch == ']' && ReadText("]>"))
                        {
                            state = XmlState.Other;
                        }
                        break;

                    case XmlState.Comment:
                        if (ch == '-' && ReadText("->"))
                        {
                            state = XmlState.Other;
                        }
                        break;

                    case XmlState.Tag:
                        if (char.IsWhiteSpace((char)ch))
                        {
                            state = XmlState.Attribute;
                        }
                        else if (ch == '>')
                        {
                            state = XmlState.Other;
                        }
                        break;

                    case XmlState.Other:
                        if (ch == '<')
                        {
                            if (_reader.Peek() == '!')
                            {
                                ReadText();
                                if (ReadText("--"))
                                {
                                    state = XmlState.Comment;
                                }
                                else if (ReadText("[CDATA["))
                                {
                                    state = XmlState.CData;
                                }
                            }
                            else
                            {
                                state = XmlState.Tag;
                            }
                        }
                        break;
                    }
                    break;
                }
                ch = ReadText();
            }

            switch (state)
            {
            case XmlState.Attribute:
                if (char.IsWhiteSpace(_builder[_builder.Length - 1]))
                {
                    state = XmlState.AttributeStart;
                    _builder.Append(">");
                }
                else
                {
                    _builder.Append("=\"\">");
                }
                break;

            case XmlState.AttributeValue:
                if (_builder[_builder.Length - 1] == '=')
                {
                    _builder.Append('"');
                }
                else
                {
                    if (_builder[LastIndexOf('=') + 1] == '\'')
                    {
                        _builder.Append("'>");
                    }
                    else
                    {
                        _builder.Append("\">");
                    }
                }
                break;

            case XmlState.CData:
                _builder.Append("]]>");
                break;

            case XmlState.Comment:
                _builder.Append("-->");
                break;

            case XmlState.Tag:
                if (_builder[_builder.Length - 1] == '<')
                {
                    _builder.Append(__noName);
                }
                _builder.Append(">");
                break;
            }
            _builder.Append("<!--").Append(__eof).Append("-->");

            _reader = null;
            var settings = new XmlReaderSettings()
            {
                ConformanceLevel = ConformanceLevel.Fragment
            };
            var textReader = new System.IO.StringReader(_builder.ToString());

            _builder.Length = 0;
            _xml            = XmlReader.Create(textReader, settings);
            _endState       = state;
        }
Esempio n. 17
0
    private void Process()
    {
      var state = XmlState.Other;
      var line = 1;

      var ch = ReadText();
      _i = 0;
      while (ch > 0)
      {
        switch (ch)
        {
          case '\r':
            ReadText('\n');
            line++;
            _lineOffsets.Add(_i + 1);
            if (state == XmlState.Tag) state = XmlState.Attribute;
            break;
          case '\n':
            line++;
            _lineOffsets.Add(_i + 1);
            if (state == XmlState.Tag) state = XmlState.Attribute;
            break;
          default:
            switch (state)
            {
              case XmlState.Attribute:
                if (ch == '=')
                {
                  if (ReadText('"') || ReadText('\''))
                  {
                    state = XmlState.AttributeValue;
                  }
                }
                else if (ch == '>')
                {
                  state = XmlState.Other;
                }
                break;
              case XmlState.AttributeValue:
                if (ch == '"' || ch == '\'')
                {
                  state = XmlState.Tag;
                }
                break;
              case XmlState.CData:
                if (ch == ']' && ReadText("]>"))
                {
                  state = XmlState.Other;
                }
                break;
              case XmlState.Comment:
                if (ch == '-' && ReadText("->"))
                {
                  state = XmlState.Other;
                }
                break;
              case XmlState.Tag:
                if (char.IsWhiteSpace((char)ch))
                {
                  state = XmlState.Attribute;
                }
                else if (ch == '>')
                {
                  state = XmlState.Other;
                }
                break;
              case XmlState.Other:
                if (ch == '<')
                {
                  if (_reader.Peek() == '!')
                  {
                    ReadText();
                    if (ReadText("--"))
                    {
                      state = XmlState.Comment;
                    }
                    else if (ReadText("[CDATA["))
                    {
                      state = XmlState.CData;
                    }
                  }
                  else
                  {
                    state = XmlState.Tag;
                  }
                }
                break;
            }
            break;
        }
        ch = ReadText();
      }

      switch (state)
      {
        case XmlState.Attribute:
          if (char.IsWhiteSpace(_builder[_builder.Length - 1]))
          {
            state = XmlState.AttributeStart;
            _builder.Append(">");
          }
          else
          {
            _builder.Append("=\"\">");
          }
          break;
        case XmlState.AttributeValue:
          if (_builder[_builder.Length - 1] == '=')
          {
            _builder.Append('"');
          }
          else
          {
            if (_builder[LastIndexOf('=') + 1] == '\'')
            {
              _builder.Append("'>");
            }
            else
            {
              _builder.Append("\">");
            }
          }
          break;
        case XmlState.CData:
          _builder.Append("]]>");
          break;
        case XmlState.Comment:
          _builder.Append("-->");
          break;
        case XmlState.Tag:
          if (_builder[_builder.Length - 1] == '<') _builder.Append(__noName);
          _builder.Append(">");
          break;
      }
      _builder.Append("<!--").Append(__eof).Append("-->");

      _reader = null;
      var settings = new XmlReaderSettings() { ConformanceLevel = ConformanceLevel.Fragment };
      var textReader = new System.IO.StringReader(_builder.ToString());
      _builder.Length = 0;
      _xml = XmlReader.Create(textReader, settings);
      _endState = state;
    }
        /// <summary>
        /// Before writing a namespace, perform various checks to ensure well-formedness.
        /// </summary>
        public void WriteEndNamespace() {
            Debug.Assert(this.xstate == XmlState.WithinNmsp, "WriteEndNamespace cannot be called in the " + this.xstate + " state.");

            this.xstate = XmlState.EnumAttrs;
            this.depth--;

            // Write cached namespace attribute
            WriteNamespaceDeclaration(this.nmspPrefix, this.nmspText);
            this.nmspPrefix = null;

            if (this.depth == 0)
                EndTree();
        }
 /// <summary>
 /// XmlRawWriter.WriteEndAttribute().
 /// </summary>
 public void WriteEndAttributeUnchecked() {
     Debug.Assert(this.xstate == XmlState.WithinAttr, "WriteEndAttribute cannot be called in the " + this.xstate + " state.");
     Writer.WriteEndAttribute();
     this.xstate = XmlState.EnumAttrs;
     this.depth--;
 }
        /// <summary>
        /// Before writing a comment, perform various checks to ensure well-formedness.
        /// </summary>
        public void WriteEndComment() {
            Debug.Assert(this.xstate == XmlState.WithinComment, "WriteEndComment cannot be called in the " + this.xstate + " state.");

            Writer.WriteComment(this.nodeText.GetResult());

            this.xstate = XmlState.WithinContent;
            this.depth--;

            if (this.depth == 0)
                EndTree();
        }
        private Dictionary<string, string> usedPrefixes = new Dictionary<string, string>(); //The prefies that used in the current scope

        /// <summary>
        /// This constructor is internal so that external users cannot construct it (and therefore we do not have to test it separately).
        /// Initialize output state to accept top-level sequences.
        /// </summary>
        internal XmlQueryOutput(XmlQueryRuntime runtime, XmlSequenceWriter seqwrt) {
            this.runtime = runtime;
            this.seqwrt = seqwrt;
            this.xstate = XmlState.WithinSequence;
        }
        /// <summary>
        /// Before writing a processing instruction, perform various checks to ensure well-formedness.
        /// </summary>
        public void WriteEndProcessingInstruction() {
            Debug.Assert(this.xstate == XmlState.WithinPI, "WriteEndProcessingInstruction cannot be called in the " + this.xstate + " state.");

            Writer.WriteProcessingInstruction(this.piTarget, this.nodeText.GetResult());

            this.xstate = XmlState.WithinContent;
            this.depth--;

            // Xml state transitions
            if (this.depth == 0)
                EndTree();
        }
        /// <summary>
        /// Before writing a namespace, perform various checks to ensure well-formedness.
        /// </summary>
        public void WriteEndNamespace() {
            Debug.Assert(this.xstate == XmlState.WithinNmsp, "WriteEndNamespace cannot be called in the " + this.xstate + " state.");

            this.xstate = XmlState.EnumAttrs;
            this.depth--;

            // Write cached namespace attribute
            WriteNamespaceDeclaration(this.piTarget/*nmspPrefix*/, this.nodeText.GetResult());

            if (this.depth == 0)
                EndTree();
        }
 protected override void GivenThat()
 {
     base.GivenThat();
     _state = new XmlState(_testXmlDocument);
 }
        /// <summary>
        /// Return the type of node that is under construction given the specified XmlState.
        /// </summary>
        private XPathNodeType XmlStateToNodeType(XmlState xstate) {
            switch (xstate) {
                case XmlState.EnumAttrs: return XPathNodeType.Element;
                case XmlState.WithinContent: return XPathNodeType.Element;
                case XmlState.WithinAttr: return XPathNodeType.Attribute;
                case XmlState.WithinComment: return XPathNodeType.Comment;
                case XmlState.WithinPI: return XPathNodeType.ProcessingInstruction;
            }

            Debug.Assert(false, xstate.ToString() + " is not a valid XmlState.");
            return XPathNodeType.Element;
        }
        /// <summary>
        /// Before writing a comment, perform various checks to ensure well-formedness.
        /// </summary>
        public void WriteStartComment() {
            // Xml state transitions
            ConstructWithinContent(XPathNodeType.Comment);

            this.commentText = string.Empty;
            this.xstate = XmlState.WithinComment;
            this.depth++;
        }
        /// <summary>
        /// Before writing a processing instruction, perform various checks to ensure well-formedness.
        /// </summary>
        public void WriteStartProcessingInstruction(string target) {
            // Xml state transitions
            ConstructWithinContent(XPathNodeType.ProcessingInstruction);

            // Verify PI name
            ValidateNames.ValidateNameThrow("", target, "", XPathNodeType.ProcessingInstruction, ValidateNames.Flags.AllExceptPrefixMapping);

            this.piTarget = target;
            this.nodeText.Clear();

            this.xstate = XmlState.WithinPI;
            this.depth++;
        }
        //-----------------------------------------------
        // XmlQueryOutput methods (XmlSequenceWriter)
        //-----------------------------------------------

        /// <summary>
        /// Call XmlSequenceWriter.StartTree() in order to start construction of a new tree.
        /// </summary>
        public void StartTree(XPathNodeType rootType) {
            Debug.Assert(this.xstate == XmlState.WithinSequence, "StartTree cannot be called in the " + this.xstate + " state.");
            Writer = this.seqwrt.StartTree(rootType, this.nsmgr, this.runtime.NameTable);
            this.rootType = rootType;
            this.xstate = (rootType == XPathNodeType.Attribute || rootType == XPathNodeType.Namespace) ? XmlState.EnumAttrs : XmlState.WithinContent;
        }
 protected override void When()
 {
     _state = new XmlState(_testXmlDocument);
 }
 /// <summary>
 /// Call XmlSequenceWriter.EndTree().
 /// </summary>
 public void EndTree() {
     Debug.Assert(this.xstate == XmlState.EnumAttrs || this.xstate == XmlState.WithinContent, "EndTree cannot be called in the " + this.xstate + " state.");
     this.seqwrt.EndTree();
     this.xstate = XmlState.WithinSequence;
     Writer = null;
 }
        //-----------------------------------------------
        // XmlQueryOutput methods (XmlRawWriter)
        //-----------------------------------------------

        /// <summary>
        /// Call XmlRawWriter.WriteStartElement() with prefix, local-name, ns, and schema type.
        /// </summary>
        public void WriteStartElementUnchecked(string prefix, string localName, string ns) {
            Debug.Assert(this.xstate == XmlState.WithinContent, "WriteStartElement cannot be called in the " + this.xstate + " state.");
            if (this.nsmgr != null) this.nsmgr.PushScope();
            Writer.WriteStartElement(prefix, localName, ns);
            //reset when enter element
            usedPrefixes.Clear();
            usedPrefixes[prefix] = ns;
            this.xstate = XmlState.EnumAttrs;
            this.depth++;
        }
Esempio n. 32
0
        private Dictionary<string, string> _usedPrefixes = new Dictionary<string, string>(); //The prefies that used in the current scope

        /// <summary>
        /// This constructor is internal so that external users cannot construct it (and therefore we do not have to test it separately).
        /// Initialize output state to accept top-level sequences.
        /// </summary>
        internal XmlQueryOutput(XmlQueryRuntime runtime, XmlSequenceWriter seqwrt)
        {
            _runtime = runtime;
            _seqwrt = seqwrt;
            _xstate = XmlState.WithinSequence;
        }