Exemplo n.º 1
0
    public static void Main()
    {
        //Create the XmlDocument.
        XmlDocument doc = new XmlDocument();

        doc.LoadXml("<!DOCTYPE book [<!ENTITY h 'hardcover'>]>" +
                    "<book genre='novel' ISBN='1-861001-57-5'>" +
                    "<title>Pride And Prejudice</title>" +
                    "<misc/>" +
                    "</book>");

        //Create an entity reference node. The child count should be 0
        //since the node has not been expanded.
        XmlEntityReference entityref = doc.CreateEntityReference("h");

        Console.WriteLine(entityref.ChildNodes.Count);

        //After the node has been added to the document, its parent node
        //is set and the entity reference node is expanded.  It now has a child
        //node containing the entity replacement text.
        doc.DocumentElement.LastChild.AppendChild(entityref);
        Console.WriteLine(entityref.FirstChild.InnerText);

        //Create and insert an undefined entity reference node.  When the entity
        //reference node is expanded, because the entity reference is undefined
        //the child is an empty text node.
        XmlEntityReference entityref2 = doc.CreateEntityReference("p");

        doc.DocumentElement.LastChild.AppendChild(entityref2);
        Console.WriteLine(entityref2.FirstChild.InnerText);
    }
Exemplo n.º 2
0
        public void DuplicateIdInEntityAttached()
        {
            v.Detach();

            XmlElement a1 = doc.CreateElement("anchor");

            a1.SetAttribute("id", "id1");

            XmlEntityReference xer = doc.CreateEntityReference("test");

            // no errors at this point (not added to doc yet)
            Assert.AreEqual(0, v.InvalidNodes.AllErrors.Length);

            doc.DocumentElement.AppendChild(a1);
            doc.DocumentElement.AppendChild(xer);

            v = new ValidationManager();
            v.Attach(doc, null);

            Assert.AreEqual(2, v.InvalidNodes.AllErrors.Length, "Expected duplicate ids");

            doc.DocumentElement.RemoveChild(a1);

            Assert.AreEqual(0, v.InvalidNodes.AllErrors.Length, "Errors remain after removing duplicate");
        }
Exemplo n.º 3
0
        public void ChildNodes()
        {
            XmlTextReader xtr = new XmlTextReader("<!DOCTYPE root [<!ENTITY ent 'ent-value'><!ENTITY el '<foo>hoge</foo><bar/>'>]><root/>",
                                                  XmlNodeType.Document, null);
            XmlDocument doc = new XmlDocument();

            doc.Load(xtr);
            XmlEntityReference ent = doc.CreateEntityReference("ent");

            // ChildNodes are not added yet.
            Assert.IsNull(ent.FirstChild);
            doc.DocumentElement.AppendChild(ent);
            // ChildNodes are added here.
            Assert.IsNotNull(ent.FirstChild);

            ent = doc.CreateEntityReference("foo");
            Assert.IsNull(ent.FirstChild);
            // Entity value is empty when the matching DTD entity
            // node does not exist.
            doc.DocumentElement.AppendChild(ent);
            Assert.IsNotNull(ent.FirstChild);

            Assert.AreEqual(String.Empty, ent.FirstChild.Value);

            ent = doc.CreateEntityReference("el");
            Assert.AreEqual("", ent.InnerText);
            doc.DocumentElement.AppendChild(ent);
            Assert.AreEqual("<foo>hoge</foo><bar />", ent.InnerXml);
            Assert.AreEqual("hoge", ent.InnerText);
            Assert.AreEqual(XmlNodeType.Element, ent.FirstChild.NodeType);
        }
Exemplo n.º 4
0
        private XmlEntityReference LoadEntityReferenceInAttribute()
        {
            XmlEntityReference reference = this.dummyDocument.CreateEntityReference(this.reader.LocalName);

            if (this.reader.CanResolveEntity)
            {
                this.reader.ResolveEntity();
                while (this.reader.ReadAttributeValue())
                {
                    switch (this.reader.NodeType)
                    {
                    case XmlNodeType.Text:
                    {
                        reference.AppendChild(this.dummyDocument.CreateTextNode(this.reader.Value));
                        continue;
                    }

                    case XmlNodeType.EntityReference:
                    {
                        reference.AppendChild(this.LoadEntityReferenceInAttribute());
                        continue;
                    }

                    case XmlNodeType.EndEntity:
                        if (reference.ChildNodes.Count == 0)
                        {
                            reference.AppendChild(this.dummyDocument.CreateTextNode(string.Empty));
                        }
                        return(reference);
                    }
                    throw XmlLoader.UnexpectedNodeType(this.reader.NodeType);
                }
            }
            return(reference);
        }
Exemplo n.º 5
0
        static void ChangeXMl(string file)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(file);
                XmlNode root = doc.DocumentElement;
                // root.RemoveChild(root.FirstChild);
                XmlElement stud = doc.CreateElement("student");
                {
                    XmlComment comment = doc.CreateComment("coment");
                    stud.AppendChild(comment);
                    XmlElement fn = doc.CreateElement("fname");
                    {
                        XmlEntityReference hl     = doc.CreateEntityReference("hello");
                        XmlText            fntext = doc.CreateTextNode("Anna");
                        fn.AppendChild(hl);
                        fn.AppendChild(fntext);
                    }
                    stud.AppendChild(fn);
                    XmlElement ln = doc.CreateElement("lname");
                    ln.InnerText = "Drozd";
                    stud.AppendChild(ln);

                    XmlElement sum = doc.CreateElement("suma");
                    sum.InnerText = "12.63";
                    stud.AppendChild(sum);

                    // <address country="Україна" region="Житомирська область">Житомир</address>
                    XmlElement address = doc.CreateElement("address");
                    address.InnerText = "Бердичів";
                    {
                        XmlAttribute country = doc.CreateAttribute("country");
                        country.Value = "Україна";
                        address.Attributes.Append(country);

                        XmlAttribute region = doc.CreateAttribute("region");
                        region.Value = "Житомирська область";
                        address.Attributes.Append(region);
                    }
                    stud.AppendChild(address);


                    //    <birthday>15.05.1978</birthday>
                    XmlElement birthday = doc.CreateElement("birthday");
                    birthday.InnerText = "25.07.1996";
                    stud.AppendChild(birthday);
                }
                root.AppendChild(stud);
                doc.Save(file);
                Console.WriteLine(file + "is changed");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Exemplo n.º 6
0
        public void WriteTo()
        {
            XmlDocument doc = new XmlDocument();

            doc.LoadXml("<root/>");
            XmlEntityReference er = doc.CreateEntityReference("foo");

            doc.DocumentElement.AppendChild(er);
            Assert.AreEqual("foo", er.Name, "Name");
            Assert.AreEqual("<root>&foo;</root>", doc.DocumentElement.OuterXml, "WriteTo");
        }
Exemplo n.º 7
0
        static void AddNodeDOM()
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load("Students.xml");
                XmlNode root = doc.DocumentElement;
                root.RemoveChild(root.FirstChild);
                XmlElement stud = doc.CreateElement("student");
                {
                    XmlElement pib = doc.CreateElement("pib");
                    {
                        XmlEntityReference xmlEntity = doc.CreateEntityReference("hello");
                        XmlText            name      = doc.CreateTextNode("Федоренко Федір Федорович");
                        pib.AppendChild(xmlEntity);
                        pib.AppendChild(name);
                    }
                    stud.AppendChild(pib);

                    XmlElement bday = doc.CreateElement("bday");
                    bday.InnerText = "25.05.2013";
                    stud.AppendChild(bday);

                    XmlElement avg = doc.CreateElement("avg");
                    avg.InnerText = "25,98";
                    stud.AppendChild(avg);

                    XmlElement address = doc.CreateElement("address");
                    {
                        XmlAttribute region = doc.CreateAttribute("region");
                        region.Value = "Житомирська обл.";
                        address.Attributes.Append(region);

                        XmlAttribute country = doc.CreateAttribute("country");
                        country.Value = "Україна";
                        address.Attributes.Append(country);


                        XmlText city = doc.CreateTextNode("Бердичів");
                        address.AppendChild(city);
                    }
                    stud.AppendChild(address);
                }
                root.AppendChild(stud);
                doc.Save("StudentsNew.xml");
                Console.WriteLine("Add node in tree");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    public static void Main()
    {
        //Create the XmlDocument.
        XmlDocument doc = new XmlDocument();

        doc.Load("http://localhost/uri.xml");

        //Display information on the entity reference node.
        XmlEntityReference entref = (XmlEntityReference)doc.DocumentElement.LastChild.FirstChild;

        Console.WriteLine("Name of the entity reference:  {0}", entref.Name);
        Console.WriteLine("Base URI of the entity reference:  {0}", entref.BaseURI);
        Console.WriteLine("The entity replacement text:  {0}", entref.InnerText);
    }
Exemplo n.º 9
0
    // Test setting attributes via direct text node inserts.
    public void TestXmlAttributeInsert()
    {
        XmlAttribute attr;

        attr = doc.CreateAttribute("prefix", "foo", "uri");

        XmlText text1 = doc.CreateTextNode("hello");
        XmlText text2 = doc.CreateTextNode(" and goodbye");

        attr.AppendChild(text1);
        AssertEquals("Insert (1)", "hello", attr.Value);
        AssertEquals("Insert (2)", text1, attr.FirstChild);
        AssertEquals("Insert (3)", text1, attr.LastChild);

        attr.AppendChild(text2);
        AssertEquals("Insert (4)", "hello and goodbye", attr.Value);
        AssertEquals("Insert (5)", text1, attr.FirstChild);
        AssertEquals("Insert (6)", text2, attr.LastChild);

        // Entity references do not affect the combined value,
        // but they do affect the XML.
        XmlEntityReference entity = doc.CreateEntityReference("foo");

        attr.AppendChild(entity);
        AssertEquals("Insert (7)", "hello and goodbye", attr.Value);
        AssertEquals("Insert (8)",
                     "hello and goodbye&foo;", attr.InnerXml);

        // Cannot insert whitespace into attributes.
        try
        {
            attr.AppendChild(doc.CreateWhitespace("   "));
            Fail("Insert (9)");
        }
        catch (InvalidOperationException)
        {
            // Success
        }
        try
        {
            attr.AppendChild(doc.CreateSignificantWhitespace("   "));
            Fail("Insert (9)");
        }
        catch (InvalidOperationException)
        {
            // Success
        }
    }
Exemplo n.º 10
0
    public static void Main()
    {
        // Create the XmlDocument.
        XmlDocument doc = new XmlDocument();

        doc.LoadXml("<!DOCTYPE book [<!ENTITY h 'hardcover'>]>" +
                    "<book genre='novel' ISBN='1-861001-57-5'>" +
                    "<title>Pride And Prejudice</title>" +
                    "<style>&h;</style>" +
                    "</book>");

        // Display information on the entity reference node.
        XmlEntityReference entref = (XmlEntityReference)doc.DocumentElement.LastChild.FirstChild;

        Console.WriteLine("Name of the entity reference:  {0}", entref.Name);
        Console.WriteLine("The entity replacement text:  {0}", entref.InnerText);
    }
Exemplo n.º 11
0
    public static void Main()
    {
        //Create the XmlDocument.
        XmlDocument doc = new XmlDocument();

        doc.LoadXml("<!DOCTYPE book [<!ENTITY h 'hardcover'>]>" +
                    "<book genre='novel' ISBN='1-861001-57-5'>" +
                    "<title>Pride And Prejudice</title>" +
                    "<style>&h;</style>" +
                    "</book>");

        // Determine whether the node is read-only.
        XmlEntityReference entref = (XmlEntityReference)doc.DocumentElement.LastChild.FirstChild;

        if (entref.IsReadOnly)
        {
            Console.WriteLine("Entity reference nodes are always read-only");
        }
    }
Exemplo n.º 12
0
        public void UnknownElementInEntityAttached()
        {
            v.Detach();

            XmlEntityReference xer = doc.CreateEntityReference("unk");

            // no errors at this point (not added to doc yet)
            Assert.AreEqual(0, v.InvalidNodes.AllErrors.Length);

            doc.DocumentElement.AppendChild(xer);

            v = new ValidationManager();
            v.Attach(doc, null);
            v.ValidateAll();

            Assert.AreEqual(2, v.InvalidNodes.AllErrors.Length, "Expected some errors");

            doc.DocumentElement.RemoveChild(xer);

            Assert.AreEqual(0, v.InvalidNodes.AllErrors.Length, "Errors remain after removing duplicate");
        }
Exemplo n.º 13
0
        private XmlEntityReference LoadEntityReferenceInAttribute()
        {
            Debug.Assert(reader.NodeType == XmlNodeType.EntityReference);

            XmlEntityReference eref = dummyDocument.CreateEntityReference(reader.LocalName);

            if (!reader.CanResolveEntity)
            {
                return(eref);
            }
            reader.ResolveEntity();

            while (reader.ReadAttributeValue())
            {
                switch (reader.NodeType)
                {
                case XmlNodeType.Text:
                    eref.AppendChild(dummyDocument.CreateTextNode(reader.Value));
                    continue;

                case XmlNodeType.EndEntity:
                    if (eref.ChildNodes.Count == 0)
                    {
                        eref.AppendChild(dummyDocument.CreateTextNode(String.Empty));
                    }
                    return(eref);

                case XmlNodeType.EntityReference:
                    eref.AppendChild(LoadEntityReferenceInAttribute());
                    break;

                default:
                    throw XmlLoader.UnexpectedNodeType(reader.NodeType);
                }
            }

            return(eref);
        }
Exemplo n.º 14
0
        /// <summary>
        /// Creates an entity reference with the specified name.
        /// </summary>
        /// <param name="name">The name of the entity reference.</param>
        /// <returns>A new <see cref="DOMEntityReference"/>.</returns>
        public DOMEntityReference createEntityReference(string name)
        {
            XmlEntityReference entref = XmlDocument.CreateEntityReference(name);

            return(new DOMEntityReference(entref));
        }
Exemplo n.º 15
0
        private ulong ComputeHashXmlNode(XmlNode node)
        {
            switch (node.NodeType)
            {
            case XmlNodeType.Element:
            {
                XmlElement    el = (XmlElement)node;
                HashAlgorithm ha = new HashAlgorithm();

                HashElement(ha, el.LocalName, el.Prefix, el.NamespaceURI);
                ComputeHashXmlChildren(ha, el);

                return(ha.Hash);
            }

            case XmlNodeType.Attribute:
                // attributes are hashed in ComputeHashXmlChildren;
                Debug.Assert(false);
                return(0);

            case XmlNodeType.Whitespace:
                return(0);

            case XmlNodeType.SignificantWhitespace:
                if (!_bIgnoreWhitespace)
                {
                    goto case XmlNodeType.Text;
                }
                return(0);

            case XmlNodeType.Comment:
                if (!_bIgnoreComments)
                {
                    return(HashCharacterNode(XmlNodeType.Comment, ((XmlCharacterData)node).Value));
                }
                return(0);

            case XmlNodeType.Text:
            {
                XmlCharacterData cd = (XmlCharacterData)node;
                if (_bIgnoreWhitespace)
                {
                    return(HashCharacterNode(cd.NodeType, XmlDiff.NormalizeText(cd.Value)));
                }
                else
                {
                    return(HashCharacterNode(cd.NodeType, cd.Value));
                }
            }

            case XmlNodeType.CDATA:
            {
                XmlCharacterData cd = (XmlCharacterData)node;
                return(HashCharacterNode(cd.NodeType, cd.Value));
            }

            case XmlNodeType.ProcessingInstruction:
            {
                if (_bIgnorePI)
                {
                    return(0);
                }

                XmlProcessingInstruction pi = (XmlProcessingInstruction)node;
                return(HashPI(pi.Target, pi.Value));
            }

            case XmlNodeType.EntityReference:
            {
                XmlEntityReference er = (XmlEntityReference)node;
                return(HashER(er.Name));
            }

            case XmlNodeType.XmlDeclaration:
            {
                if (_bIgnoreXmlDecl)
                {
                    return(0);
                }
                XmlDeclaration decl = (XmlDeclaration)node;
                return(HashXmlDeclaration(XmlDiff.NormalizeXmlDeclaration(decl.Value)));
            }

            case XmlNodeType.DocumentType:
            {
                if (_bIgnoreDtd)
                {
                    return(0);
                }
                XmlDocumentType docType = (XmlDocumentType)node;
                return(HashDocumentType(docType.Name, docType.PublicId, docType.SystemId, docType.InternalSubset));
            }

            case XmlNodeType.DocumentFragment:
                return(0);

            default:
                Debug.Assert(false);
                return(0);
            }
        }
Exemplo n.º 16
0
 private void Stream(XmlEntityReference entityReference)
 {
     Data.Add(new ClassSeparatorData(typeof(XmlEntityReference)));
 }
Exemplo n.º 17
0
        private void Stream(XmlEntityReference entRef)
        {
            MDataObjs.Add(new ClassSeparator(typeof(XmlEntityReference)));

            // no data to display at this level
        }
 internal DOMEntityReference(XmlEntityReference/*!*/ xmlEntityReference)
 {
     this.XmlEntityReference = xmlEntityReference;
 }
Exemplo n.º 19
0
        // Methods
        internal override void Apply(XmlNode parent, ref XmlNode currentPosition)
        {
            Debug.Assert(_matchNode.ParentNode == parent ||
                         (_matchNode.ParentNode == null && _matchNode.NodeType == XmlNodeType.Attribute) ||
                         _matchNode.NodeType == XmlNodeType.XmlDeclaration ||
                         _matchNode.NodeType == XmlNodeType.DocumentType);

            switch (_matchNode.NodeType)
            {
            case XmlNodeType.Element:
            {
                Debug.Assert(_value == null);

                if (_name == null)
                {
                    _name = ((XmlElement)_matchNode).LocalName;
                }
                if (_ns == null)
                {
                    _ns = ((XmlElement)_matchNode).NamespaceURI;
                }
                if (_prefix == null)
                {
                    _prefix = ((XmlElement)_matchNode).Prefix;
                }

                XmlElement newEl = parent.OwnerDocument.CreateElement(_prefix, _name, _ns);

                // move attributes
                XmlAttributeCollection attrs = _matchNode.Attributes;
                while (attrs.Count > 0)
                {
                    XmlAttribute attr = (XmlAttribute)attrs.Item(0);
                    attrs.RemoveAt(0);
                    newEl.Attributes.Append(attr);
                }

                // move children
                XmlNode curChild = _matchNode.FirstChild;
                while (curChild != null)
                {
                    XmlNode nextSibling = curChild.NextSibling;
                    _matchNode.RemoveChild(curChild);
                    newEl.AppendChild(curChild);
                    curChild = nextSibling;
                }

                parent.ReplaceChild(newEl, _matchNode);
                currentPosition = newEl;

                ApplyChildren(newEl);

                break;
            }

            case XmlNodeType.Attribute:
            {
                if (_name == null)
                {
                    _name = ((XmlAttribute)_matchNode).LocalName;
                }
                if (_ns == null)
                {
                    _ns = ((XmlAttribute)_matchNode).NamespaceURI;
                }
                if (_prefix == null)
                {
                    _prefix = ((XmlAttribute)_matchNode).Prefix;
                }
                if (_value == null)
                {
                    _value = ((XmlAttribute)_matchNode).Value;
                }

                XmlAttribute newAttr = parent.OwnerDocument.CreateAttribute(_prefix, _name, _ns);
                newAttr.Value = _value;

                parent.Attributes.Remove((XmlAttribute)_matchNode);
                parent.Attributes.Append(newAttr);
                break;
            }

            case XmlNodeType.Text:
            case XmlNodeType.CDATA:
            case XmlNodeType.Comment:
                Debug.Assert(_value != null);
                ((XmlCharacterData)_matchNode).Data = _value;
                currentPosition = _matchNode;
                break;

            case XmlNodeType.ProcessingInstruction:
            {
                if (_name != null)
                {
                    if (_value == null)
                    {
                        _value = ((XmlProcessingInstruction)_matchNode).Data;
                    }
                    XmlProcessingInstruction newPi = parent.OwnerDocument.CreateProcessingInstruction(_name, _value);

                    parent.ReplaceChild(newPi, _matchNode);
                    currentPosition = newPi;
                }
                else
                {
                    ((XmlProcessingInstruction)_matchNode).Data = _value;
                    currentPosition = _matchNode;
                }
                break;
            }

            case XmlNodeType.EntityReference:
            {
#if NETCORE
                throw new NotSupportedException("XmlNodeType.EntityReference is not supported");
#else
                Debug.Assert(_name != null);

                XmlEntityReference newEr = parent.OwnerDocument.CreateEntityReference(_name);

                parent.ReplaceChild(newEr, _matchNode);
                currentPosition = newEr;
                break;
#endif
            }

            case XmlNodeType.XmlDeclaration:
            {
                Debug.Assert(_value != null && _value != string.Empty);
                XmlDeclaration xmlDecl = (XmlDeclaration)_matchNode;
                xmlDecl.Encoding   = null;
                xmlDecl.Standalone = null;
                xmlDecl.InnerText  = _value;
                break;
            }

            case XmlNodeType.DocumentType:
            {
#if NETCORE
                throw new NotSupportedException("XmlNodeType.DocumentType is not supported");
#else
                if (_name == null)
                {
                    _name = ((XmlDocumentType)_matchNode).LocalName;
                }

                if (_ns == null)
                {
                    _ns = ((XmlDocumentType)_matchNode).SystemId;
                }
                else if (_ns == string.Empty)
                {
                    _ns = null;
                }

                if (_prefix == null)
                {
                    _prefix = ((XmlDocumentType)_matchNode).PublicId;
                }
                else if (_prefix == string.Empty)
                {
                    _prefix = null;
                }

                if (_value == null)
                {
                    _value = ((XmlDocumentType)_matchNode).InternalSubset;
                }

                XmlDocumentType docType = _matchNode.OwnerDocument.CreateDocumentType(_name, _prefix, _ns, _value);
                _matchNode.ParentNode.ReplaceChild(docType, _matchNode);
                break;
#endif
            }

            default:
                Debug.Assert(false);
                break;
            }
        }
Exemplo n.º 20
0
        internal XmlElement ParseElement(XmlDocument owner, XmlElement parent)
        {
            XmlElement xmlElement = new XmlElement(this.root);

            if (this.ParseStartTag(owner, parent, xmlElement))
            {
                return(xmlElement);
            }
            while (this.currentToken != Token.EndOfFile)
            {
                switch (this.currentToken)
                {
                case Token.StartCharacterData:
                    this.ParseCData((XmlNode)xmlElement);
                    continue;

                case Token.StartProcessingInstruction:
                    this.ParseProcessingInstruction((XmlNode)xmlElement);
                    continue;

                case Token.StartOfClosingTag:
                    this.endTagContext = this.scanner.CurrentSourceContext;
                    int num1 = (int)this.GetNextToken();
                    if (this.currentToken != Token.Identifier)
                    {
                        xmlElement.endTagContext = this.endTagContext;
                        if (this.currentToken == Token.Whitespace)
                        {
                            this.errorHandler.HandleError((Node)this.scanner.GetStringLiteral(), SR.IllegalWhitespace);
                        }
                        else
                        {
                            this.errorHandler.HandleError((Node)this.scanner.GetStringLiteral(), SR.UnexpectedToken, this.scanner.GetString());
                        }
                        xmlElement.SourceContext.EndCol = this.scanner.CurrentSourceContext.EndCol;
                        goto label_28;
                    }
                    else
                    {
                        this.endTag = this.ParseQualifiedName();
                        break;
                    }

                case Token.StartOfTag:
                    this.ParseElement(owner, xmlElement);
                    if (this.endTag == null)
                    {
                        continue;
                    }
                    break;

                case Token.StartLiteralComment:
                    this.ParseComment((XmlNode)xmlElement);
                    continue;

                case Token.LiteralContentString:
                    Literal stringLiteral1 = this.scanner.GetStringLiteral();
                    xmlElement.AddChild((Node)stringLiteral1);
                    int num2 = (int)this.GetNextToken();
                    continue;

                case Token.CharacterEntity:
                    XmlEntityReference xmlEntityReference1 = new XmlEntityReference(owner, this.scanner.GetCharEntity());
                    xmlEntityReference1.SourceContext = this.scanner.CurrentSourceContext;
                    xmlElement.AddChild((Node)xmlEntityReference1);
                    int num3 = (int)this.GetNextToken();
                    continue;

                case Token.GeneralEntityReference:
                    XmlEntityReference xmlEntityReference2 = new XmlEntityReference(owner, this.scanner.GetEntityName());
                    xmlEntityReference2.SourceContext = this.scanner.CurrentSourceContext;
                    xmlElement.AddChild((Node)xmlEntityReference2);
                    int num4 = (int)this.GetNextToken();
                    continue;

                default:
                    int num5 = (int)this.GetNextToken();
                    continue;
                }
                XmlNode xmlNode = (XmlNode)xmlElement;
                while (xmlNode != null && !XmlNode.QualifiedNameMatches(xmlNode.Name, this.endTag))
                {
                    xmlNode = xmlNode.Parent;
                }
                if (xmlNode == null)
                {
                    this.errorHandler.HandleError((Node)this.endTag, SR.ClosingTagMismatch, xmlElement.Name.ToString());
                }
                else if (xmlNode != xmlElement)
                {
                    this.errorHandler.HandleError((Node)this.endTag, SR.ClosingTagMismatch, xmlElement.Name.ToString());
                    return(xmlElement);
                }
                xmlElement.endTagContext = this.endTagContext;
                xmlElement.endTag        = this.endTag;
                this.endTag = (Identifier)null;
                Literal stringLiteral2 = this.scanner.GetStringLiteral();
                this.SkipWhitespace();
                if (this.currentToken != Token.EndOfEndTag)
                {
                    this.errorHandler.HandleError((Node)stringLiteral2, SR.ExpectingToken, ">");
                    xmlElement.SourceContext.EndCol = this.scanner.CurrentSourceContext.EndCol;
                    xmlElement.endTagContext.EndCol = this.scanner.CurrentSourceContext.EndCol;
                    break;
                }
                xmlElement.SourceContext.EndCol = this.scanner.CurrentSourceContext.EndCol;
                xmlElement.endTagContext.EndCol = this.scanner.CurrentSourceContext.EndCol;
                int num6 = (int)this.GetNextToken();
                return(xmlElement);
            }
label_28:
            if (this.currentToken == Token.EndOfFile)
            {
                this.errorHandler.HandleError((Node)xmlElement.Name, SR.TagNotClosed, xmlElement.Name != null ? xmlElement.Name.ToString() : "");
            }
            xmlElement.SourceContext.EndCol = this.scanner.CurrentSourceContext.EndCol;
            return(xmlElement);
        }
Exemplo n.º 21
0
 internal DOMEntityReference(XmlEntityReference /*!*/ xmlEntityReference)
     : base(ScriptContext.CurrentContext, true)
 {
     this.XmlEntityReference = xmlEntityReference;
 }
Exemplo n.º 22
0
        private byte[] EncodeNode(XmlNode node)
        {
            List <byte> bytesList = new List <byte>();

            switch (node.NodeType)
            {
            case XmlNodeType.Element:
                //bool hasAttributes = node.Attributes.Count > 0;

                bool hasAttributes = false;

                foreach (XmlAttribute att in node.Attributes)
                {
                    hasAttributes |= !att.Name.StartsWith("xmlns");
                }

                bool hasContent = node.HasChildNodes;
                int  codePage   = tagCodeSpace.ContainsTag(currentTagCodePage, node.Name);
                if (codePage >= 0)
                {
                    if (currentTagCodePage != codePage)
                    {
                        bytesList.Add((byte)GlobalTokens.Names.SWITCH_PAGE);
                        bytesList.Add((byte)codePage);
                        currentTagCodePage = codePage;
                    }

                    byte keyValue = tagCodeSpace.GetCodePage(currentTagCodePage).GetTag(node.Name).Token;
                    if (hasAttributes)
                    {
                        keyValue |= 128;
                    }
                    if (hasContent)
                    {
                        keyValue |= 64;
                    }
                    bytesList.Add(keyValue);
                }
                else
                {
                    //TODO: unkown tag
                }

                if (hasAttributes)
                {
                    foreach (XmlAttribute attribute in node.Attributes)
                    {
                        bytesList.AddRange(EncodeNode(attribute));
                    }
                    bytesList.Add((byte)GlobalTokens.Names.END);
                }

                if (hasContent)
                {
                    bytesList.AddRange(EncodeNodes(node.ChildNodes));
                    bytesList.Add((byte)GlobalTokens.Names.END);
                }
                break;

            case XmlNodeType.Text:
                bool isOpaqueData = false;

                if (opaqueDataExpressions.Count > 0)
                {
                    foreach (OpaqueDataExpression expression in OpaqueDataExpressions)
                    {
                        if (expression.TagName.Equals(node.ParentNode.Name))
                        {
                            if (node.ParentNode.SelectSingleNode(expression.Expression) != null)
                            {
                                isOpaqueData = true;
                                break;
                            }
                        }
                    }
                }

                if (isOpaqueData)
                {
                    byte[] opaqueDataBytes = GetBytes(node.Value);
                    bytesList.Add((byte)GlobalTokens.Names.OPAQUE);
                    bytesList.AddRange(GetMultiByte(opaqueDataBytes.Length));
                    bytesList.AddRange(opaqueDataBytes);
                }
                else
                {
                    string textValue = node.Value;

                    while (textValue.Length > 0)
                    {
                        int stringTableIndex = textValue.Length;

                        if (stringTable.ContainsString(textValue))
                        {
                            StringTableItem stringTableItem = stringTable.GetString(textValue);
                            stringTableIndex = textValue.IndexOf(stringTableItem.Value);

                            if (stringTableIndex == 0)
                            {
                                bytesList.Add((byte)GlobalTokens.Names.STR_T);
                                bytesList.AddRange(GetMultiByte(stringTableItem.Index));
                                textValue = textValue.Substring(stringTableItem.Value.Length);
                                continue;
                            }
                        }

                        bytesList.Add((byte)GlobalTokens.Names.STR_I);
                        bytesList.AddRange(textEncoding.GetBytes(textValue.Substring(0, stringTableIndex)));
                        bytesList.Add(0);

                        textValue = textValue.Substring(stringTableIndex);
                    }
                }
                break;

            case XmlNodeType.EntityReference:
                bytesList.Add((byte)GlobalTokens.Names.ENTITY);
                XmlEntityReference reference = (XmlEntityReference)node;
                foreach (int stringItem in reference.InnerText.ToCharArray())
                {
                    bytesList.AddRange(GetMultiByte(stringItem));
                }
                break;

            case XmlNodeType.Attribute:
                var tmpCodePage = attributeCodeSpace.GetCodePage(currentAttributeCodePage);
                var tmpOk       = tmpCodePage.ContainsAttributeStart(node.Name, node.Value);

                if (tmpOk)
                {
                    AttributeStart attributeStart = attributeCodeSpace.GetCodePage(currentAttributeCodePage).GetAttributeStart(node.Name, node.Value);
                    bytesList.Add(attributeStart.Token);

                    string postAttributeValue = node.Value.Substring(attributeStart.Prefix.Length);
                    while (postAttributeValue.Length > 0)
                    {
                        int attrValueIndex = postAttributeValue.Length;

                        if (attributeCodeSpace.GetCodePage(currentAttributeCodePage).ContainsAttributeValue(postAttributeValue))
                        {
                            AttributeValue attrValue = attributeCodeSpace.GetCodePage(currentAttributeCodePage).GetAttributeValue(postAttributeValue);
                            attrValueIndex = postAttributeValue.IndexOf(attrValue.Value);

                            if (attrValueIndex == 0)
                            {
                                bytesList.Add(attrValue.Token);
                                postAttributeValue = postAttributeValue.Substring(attrValue.Value.Length);
                                continue;
                            }
                        }

                        int stringTableIndex = postAttributeValue.Length;

                        if (stringTable.ContainsString(postAttributeValue))
                        {
                            StringTableItem stringTableItem = stringTable.GetString(postAttributeValue);
                            stringTableIndex = postAttributeValue.IndexOf(stringTableItem.Value);

                            if (stringTableIndex == 0)
                            {
                                bytesList.Add((byte)GlobalTokens.Names.STR_T);
                                bytesList.AddRange(GetMultiByte(stringTableItem.Index));

                                postAttributeValue = postAttributeValue.Substring(stringTableItem.Value.Length);
                                continue;
                            }
                        }

                        int firstReferenceIndex = Math.Min(attrValueIndex, stringTableIndex);
                        bytesList.Add((byte)GlobalTokens.Names.STR_I);
                        bytesList.AddRange(textEncoding.GetBytes(postAttributeValue.Substring(0, firstReferenceIndex)));
                        bytesList.Add(0);

                        postAttributeValue = postAttributeValue.Substring(firstReferenceIndex);
                    }
                }
                break;
            }

            return(bytesList.ToArray());
        }