Ejemplo n.º 1
0
        /// <summary>
        /// Clones any XObject into another one.
        /// </summary>
        /// <param name="this">This XObject to clone.</param>
        /// <param name="setLineColumnInfo">False to not propagate any associated <see cref="IXmlLineInfo"/> to the cloned object.</param>
        /// <returns>A clone of this object.</returns>
        public static T Clone <T>(this T @this, bool setLineColumnInfo = true) where T : XObject
        {
            XObject o = null;

            switch (@this)
            {
            case null: return(null);

            case XAttribute a: o = new XAttribute(a); break;

            case XElement e: o = new XElement(e.Name, e.Attributes().Select(a => a.Clone()), e.Nodes().Select(n => n.Clone())); break;

            case XComment c: o = new XComment(c); break;

            case XCData d: o = new XCData(d); break;

            case XText t: o = new XText(t); break;

            case XProcessingInstruction p: o = new XProcessingInstruction(p); break;

            case XDocument d: o = new XDocument(new XDeclaration(d.Declaration), d.Nodes().Select(n => n.Clone())); break;

            case XDocumentType t: o = new XDocumentType(t); break;

            default: throw new NotSupportedException(@this.GetType().AssemblyQualifiedName);
            }
            return(setLineColumnInfo ? (T)o.SetLineColumnInfo(@this) : (T)o);
        }
Ejemplo n.º 2
0
 private string GetLocalName()
 {
     if (this.IsInteractive)
     {
         XElement source = this.source as XElement;
         if (source != null)
         {
             return(source.Name.LocalName);
         }
         XAttribute attribute = this.source as XAttribute;
         if (attribute != null)
         {
             return(attribute.Name.LocalName);
         }
         XProcessingInstruction instruction = this.source as XProcessingInstruction;
         if (instruction != null)
         {
             return(instruction.Target);
         }
         XDocumentType type = this.source as XDocumentType;
         if (type != null)
         {
             return(type.Name);
         }
     }
     return(string.Empty);
 }
Ejemplo n.º 3
0
        string GetLocalName()
        {
            if (!IsInteractive)
            {
                return(string.Empty);
            }
            XElement e = source as XElement;

            if (e != null)
            {
                return(e.Name.LocalName);
            }
            XAttribute a = source as XAttribute;

            if (a != null)
            {
                return(a.Name.LocalName);
            }
            XProcessingInstruction p = source as XProcessingInstruction;

            if (p != null)
            {
                return(p.Target);
            }
            XDocumentType n = source as XDocumentType;

            if (n != null)
            {
                return(n.Name);
            }
            return(string.Empty);
        }
Ejemplo n.º 4
0
        internal override bool DeepEquals(XNode node)
        {
            XDocumentType other = node as XDocumentType;

            return(other != null && _name == other._name && _publicId == other._publicId &&
                   _systemId == other.SystemId && _internalSubset == other._internalSubset);
        }
Ejemplo n.º 5
0
        internal static XElement GetPlistRootNode(TextReader readerAtStartOfDocument)
        {
            Validator.IsNotNull(readerAtStartOfDocument, "readerAtStartOfDocument");

            var doc = XDocument.Load(readerAtStartOfDocument);

            var documentType = doc.DocumentType;
            if (documentType != null)
            {
                var requiredDocumentType = new XDocumentType("plist", "-//Apple Computer//DTD PLIST 1.0//EN",
                                                             "http://www.apple.com/DTDs/PropertyList-1.0.dtd", "");
                if (!XNode.DeepEquals(documentType, requiredDocumentType))
                {
                    throw new ArgumentOutOfRangeException("readerAtStartOfDocument",
                                                          "The provided TextReader's document does not contain the required DOCTYPE of the form " +
                                                          @"<!DOCTYPE plist PUBLIC ""-//Apple Computer//DTD PLIST 1.0//EN"" ""http://www.apple.com/DTDs/PropertyList-1.0.dtd"">");
                }
            }
            var root = doc.Element("plist");
            if (root == null)
            {
                throw new ArgumentOutOfRangeException("readerAtStartOfDocument",
                                                      "The provided TextReader's document does not contain the required plist root node.");
            }

            var version = root.Attribute("version");
            if (version == null || version.Value != "1.0")
            {
                throw new ArgumentOutOfRangeException("readerAtStartOfDocument",
                                                      "The provided TextReader's document does not contain the required plist root node version of \"1.0\".");
            }

            return root;
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Initializes an instance of the XDocumentType class
        /// from another XDocumentType object.
        /// </summary>
        /// <param name="other"><see cref="XDocumentType"/> object to copy from.</param>
        public XDocumentType(XDocumentType other)
        {
            ArgumentNullException.ThrowIfNull(other);

            _name           = other._name;
            _publicId       = other._publicId;
            _systemId       = other._systemId;
            _internalSubset = other._internalSubset;
        }
Ejemplo n.º 7
0
 public XDocumentType(XDocumentType other)
 {
     if (other == null)
         throw new ArgumentNullException ("other");
     name = other.name;
     pubid = other.pubid;
     sysid = other.sysid;
     intSubset = other.intSubset;
 }
Ejemplo n.º 8
0
        public int GetHashCode(XNode obj)
        {
            if (obj == null)
            {
                return(0);
            }
            int h = ((int)obj.NodeType << 6);

            switch (obj.NodeType)
            {
            case XmlNodeType.Document:
                XDocument doc = (XDocument)obj;
                h = h ^ GetHashCode(doc.Declaration);
                foreach (XNode n in doc.Nodes())
                {
                    h = h ^ (n.GetHashCode() << 5);
                }
                break;

            case XmlNodeType.Element:
                XElement el = (XElement)obj;
                h = h ^ (el.Name.GetHashCode() << 3);
                foreach (XAttribute a in el.Attributes())
                {
                    h = h ^ (a.GetHashCode() << 7);
                }
                foreach (XNode n in el.Nodes())
                {
                    h = h ^ (n.GetHashCode() << 6);
                }
                break;

            case XmlNodeType.Comment:
                h = h ^ ((XComment)obj).Value.GetHashCode();
                break;

            case XmlNodeType.ProcessingInstruction:
                XPI pi = (XPI)obj;
                h = h ^ ((pi.Target.GetHashCode() << 6) + pi.Data.GetHashCode());
                break;

            case XmlNodeType.DocumentType:
                XDocumentType dtd = (XDocumentType)obj;
                h = h ^ (dtd.Name.GetHashCode() << 7) ^
                    (dtd.PublicId.GetHashCode() << 6) ^
                    (dtd.SystemId.GetHashCode() << 5) ^
                    (dtd.InternalSubset.GetHashCode() << 4);
                break;

            case XmlNodeType.Text:
                h = h ^ (((XText)obj).GetHashCode());
                break;
            }
            return(h);
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Initializes an instance of the XDocumentType class
 /// from another XDocumentType object.
 /// </summary>
 /// <param name="other"><see cref="XDocumentType"/> object to copy from.</param>
 public XDocumentType(XDocumentType other)
 {
     if (other == null)
     {
         throw new ArgumentNullException("other");
     }
     this.name           = other.name;
     this.publicId       = other.publicId;
     this.systemId       = other.systemId;
     this.internalSubset = other.internalSubset;
 }
Ejemplo n.º 10
0
 public XDocumentType(XDocumentType other)
 {
     if (other == null)
     {
         throw new ArgumentNullException("other");
     }
     name      = other.name;
     pubid     = other.pubid;
     sysid     = other.sysid;
     intSubset = other.intSubset;
 }
Ejemplo n.º 11
0
        public void WriteToFile ()
        {
            // Corrected header of the plist
            string publicId = "-//Apple//DTD PLIST 1.0//EN";
            string stringId = "http://www.apple.com/DTDs/PropertyList-1.0.dtd";
            string internalSubset = null;
            XDeclaration declaration = new XDeclaration ("1.0", Encoding.UTF8.EncodingName, null);
            XDocumentType docType = new XDocumentType ("plist", publicId, stringId, internalSubset);

            this.XMLDict.Save (this.filePath, declaration, docType);
        }
Ejemplo n.º 12
0
 public void XDocumentTypeDocTypeVariation()
 {
     XDocumentType toChange = new XDocumentType("root", "", "", "");
     XDocumentType original = new XDocumentType(toChange);
     using (EventsHelper eHelper = new EventsHelper(toChange))
     {
         toChange.Name = "newName";
         Assert.True(toChange.Name.Equals("newName"), "Name did not change");
         eHelper.Verify(XObjectChange.Name, toChange);
     }
 }
Ejemplo n.º 13
0
 /// <summary>
 /// Initializes an instance of the XDocumentType class
 /// from another XDocumentType object.
 /// </summary>
 /// <param name="other"><see cref="XDocumentType"/> object to copy from.</param>
 public XDocumentType(XDocumentType other)
 {
     if (other == null)
     {
         throw new ArgumentNullException(nameof(other));
     }
     _name           = other._name;
     _publicId       = other._publicId;
     _systemId       = other._systemId;
     _internalSubset = other._internalSubset;
 }
Ejemplo n.º 14
0
        private static async Task <XNode> ReadFromAsyncInternal(XmlReader reader, CancellationToken cancellationToken)
        {
            if (reader.ReadState != ReadState.Interactive)
            {
                throw new InvalidOperationException(SR.InvalidOperation_ExpectedInteractive);
            }

            XNode ret;

            switch (reader.NodeType)
            {
            case XmlNodeType.Text:
            case XmlNodeType.SignificantWhitespace:
            case XmlNodeType.Whitespace:
                ret = new XText(reader.Value);
                break;

            case XmlNodeType.CDATA:
                ret = new XCData(reader.Value);
                break;

            case XmlNodeType.Comment:
                ret = new XComment(reader.Value);
                break;

            case XmlNodeType.DocumentType:
                var name           = reader.Name;
                var publicId       = reader.GetAttribute("PUBLIC");
                var systemId       = reader.GetAttribute("SYSTEM");
                var internalSubset = reader.Value;

                ret = new XDocumentType(name, publicId, systemId, internalSubset);
                break;

            case XmlNodeType.Element:
                return(await XElement.CreateAsync(reader, cancellationToken).ConfigureAwait(false));

            case XmlNodeType.ProcessingInstruction:
                var target = reader.Name;
                var data   = reader.Value;

                ret = new XProcessingInstruction(target, data);
                break;

            default:
                throw new InvalidOperationException(SR.Format(SR.InvalidOperation_UnexpectedNodeType, reader.NodeType));
            }

            cancellationToken.ThrowIfCancellationRequested();
            await reader.ReadAsync().ConfigureAwait(false);

            return(ret);
        }
 public XDocumentType(XDocumentType other)
 {
     if (other == null)
     {
         throw new ArgumentNullException("other");
     }
     this.name = other.name;
     this.publicId = other.publicId;
     this.systemId = other.systemId;
     this.internalSubset = other.internalSubset;
     this.dtdInfo = other.dtdInfo;
 }
Ejemplo n.º 16
0
        public override string GetAttribute(string name)
        {
            if (!IsInteractive)
            {
                return(null);
            }
            XElement e = GetElementInAttributeScope();

            if (e != null)
            {
                string localName, namespaceName;
                GetNameInAttributeScope(name, e, out localName, out namespaceName);
                XAttribute a = e.lastAttr;
                if (a != null)
                {
                    do
                    {
                        a = a.next;
                        if (a.Name.LocalName == localName && a.Name.NamespaceName == namespaceName)
                        {
                            if (omitDuplicateNamespaces && IsDuplicateNamespaceAttribute(a))
                            {
                                return(null);
                            }
                            else
                            {
                                return(a.Value);
                            }
                        }
                    } while (a != e.lastAttr);
                }
                return(null);
            }
            XDocumentType n = source as XDocumentType;

            if (n != null)
            {
                switch (name)
                {
                case "PUBLIC":
                    return(n.PublicId);

                case "SYSTEM":
                    return(n.SystemId);
                }
            }
            return(null);
        }
Ejemplo n.º 17
0
 public void WritePropertyList(string fileName, IEnumerable<Piece> pieces, IEnumerable<IEnumerable<int>> solutions)
 {
     var doc = new XDocument();
     var documentType = new XDocumentType("plist", "-//Apple//DTD PLIST 1.0//EN", "http://www.apple.com/DTDs/PropertyList-1.0.dtd", null);
     doc.Add(documentType);
     var plist = new XElement("plist", new XAttribute("version", "1.0"));
     plist.Add(CreateDict(
         new[] {"Pieces", "Solutions"},
         new[] {CreatePieces(pieces), CreateSolutions(solutions)}));
     doc.Add(plist);
     //var stringBuilder = new StringBuilder();
     //var stringWriter = new StringWriter(stringBuilder);
     //doc.Save(stringWriter);
     //var text = stringBuilder.ToString();
     doc.Save(fileName);
 }
Ejemplo n.º 18
0
        public string ToPapXml()
        {
            var doc = new XDocument ();

            var docType = new XDocumentType("pap", "-//WAPFORUM//DTD PAP 2.1//EN", "http://www.openmobilealliance.org/tech/DTD/pap_2.1.dtd", "<?wap-pap-ver supported-versions=\"2.0\"?>");

            doc.AddFirst (docType);

            var pap = new XElement ("pap");

            var pushMsg = new XElement ("push-message");

            pushMsg.Add (new XAttribute ("push-id", this.PushId));
            pushMsg.Add(new XAttribute("source-reference", this.SourceReference));

            if (!string.IsNullOrEmpty (this.PpgNotifyRequestedTo))
                pushMsg.Add(new XAttribute("ppg-notify-requested-to", this.PpgNotifyRequestedTo));

            if (this.DeliverAfterTimestamp.HasValue)
                pushMsg.Add (new XAttribute ("deliver-after-timestamp", this.DeliverAfterTimestamp.Value.ToUniversalTime ().ToString("s", CultureInfo.InvariantCulture) + "Z"));
            if (this.DeliverBeforeTimestamp.HasValue)
                pushMsg.Add (new XAttribute ("deliver-before-timestamp", this.DeliverBeforeTimestamp.Value.ToUniversalTime ().ToString("s", CultureInfo.InvariantCulture) + "Z"));

            //Add all the recipients
            foreach (var r in Recipients)
            {
                var address = new XElement("address");

                var addrValue = r.Recipient;

                if (!string.IsNullOrEmpty(r.RecipientType))
                {
                    addrValue = string.Format("WAPPUSH={0}%3A{1}/TYPE={2}", System.Web.HttpUtility.UrlEncode(r.Recipient),
                        r.Port, r.RecipientType);
                }

                address.Add(new XAttribute("address-value", addrValue));
                pushMsg.Add (address);
            }

            pushMsg.Add (new XElement ("quality-of-service", new XAttribute ("delivery-method", this.QualityOfService.ToString ().ToLowerInvariant ())));

            pap.Add(pushMsg);
            doc.Add (pap);

            return "<?xml version=\"1.0\"?>" + Environment.NewLine + doc.ToString (SaveOptions.None);
        }
Ejemplo n.º 19
0
        internal static XNode ReadFrom(XmlReader r, LoadOptions options)
        {
            switch (r.NodeType)
            {
            case XmlNodeType.Element:
                return(XElement.LoadCore(r, options));

            case XmlNodeType.Whitespace:
            case XmlNodeType.SignificantWhitespace:
            case XmlNodeType.Text:
                XText t = new XText(r.Value);
                t.FillLineInfoAndBaseUri(r, options);
                r.Read();
                return(t);

            case XmlNodeType.CDATA:
                XCData c = new XCData(r.Value);
                c.FillLineInfoAndBaseUri(r, options);
                r.Read();
                return(c);

            case XmlNodeType.ProcessingInstruction:
                XPI pi = new XPI(r.Name, r.Value);
                pi.FillLineInfoAndBaseUri(r, options);
                r.Read();
                return(pi);

            case XmlNodeType.Comment:
                XComment cm = new XComment(r.Value);
                cm.FillLineInfoAndBaseUri(r, options);
                r.Read();
                return(cm);

            case XmlNodeType.DocumentType:
                XDocumentType d = new XDocumentType(r.Name,
                                                    r.GetAttribute("PUBLIC"),
                                                    r.GetAttribute("SYSTEM"),
                                                    r.Value);
                d.FillLineInfoAndBaseUri(r, options);
                r.Read();
                return(d);

            default:
                throw new InvalidOperationException(String.Format("Node type {0} is not supported", r.NodeType));
            }
        }
Ejemplo n.º 20
0
 public override string GetAttribute(string name)
 {
     if (this.IsInteractive)
     {
         string   str3;
         XElement elementInAttributeScope = this.GetElementInAttributeScope();
         if (elementInAttributeScope != null)
         {
             string str;
             string str2;
             GetNameInAttributeScope(name, elementInAttributeScope, out str, out str2);
             XAttribute lastAttr = elementInAttributeScope.lastAttr;
             if (lastAttr != null)
             {
                 do
                 {
                     lastAttr = lastAttr.next;
                     if ((lastAttr.Name.LocalName == str) && (lastAttr.Name.NamespaceName == str2))
                     {
                         if (this.omitDuplicateNamespaces && this.IsDuplicateNamespaceAttribute(lastAttr))
                         {
                             return(null);
                         }
                         return(lastAttr.Value);
                     }
                 }while (lastAttr != elementInAttributeScope.lastAttr);
             }
             return(null);
         }
         XDocumentType source = this.source as XDocumentType;
         if ((source != null) && ((str3 = name) != null))
         {
             if (str3 == "PUBLIC")
             {
                 return(source.PublicId);
             }
             if (str3 == "SYSTEM")
             {
                 return(source.SystemId);
             }
         }
     }
     return(null);
 }
 public void InvalidAddIntoXDocument1()
 {
     _runWithEvents = (bool)Params[0];
     try
     {
         var doc = new XDocument(new XDocumentType("root", null, null, null), new XElement("A"));
         var o = new XDocumentType("D", null, null, null);
         if (_runWithEvents)
         {
             _eHelper = new EventsHelper(doc);
         }
         doc.AddFirst(o);
         if (_runWithEvents)
         {
             _eHelper.Verify(XObjectChange.Add, o);
         }
         TestLog.Compare(false, "Exception expected");
     }
     catch (InvalidOperationException)
     {
     }
 }
Ejemplo n.º 22
0
        /// <summary>
        /// Invoked for each <see cref="XDocumentType"/>
        /// </summary>
        /// <param name="documentType"></param>
        /// <returns></returns>
        public virtual XDocumentType Visit(XDocumentType documentType)
        {
            Contract.Requires<ArgumentNullException>(documentType != null);

            return documentType;
        }
        public void TestActualGenerationDocxWithHtmlLatTags()
        {
            //var data = new XDocument();
            var xDoc = new XDocument();
            var xElement = new XElement("test", "value = &raquo");
            xDoc.Add(xElement);
            XDocumentType docType = new XDocumentType("names", null, null, @"<!ENTITY laquo ""&#171;""><!ENTITY raquo ""&#187;"">");
            xDoc.AddFirst(docType);
            

            
         
            Console.WriteLine(xDoc);
            /*string internalSubset = string.Empty;
            //var docType = new XDocumentType("names", null, null,)
            var htmlLatEntitiesMap = new Dictionary<string, int>();
            htmlLatEntitiesMap.Add("raquo", );*/


            //Console.WriteLine(x);

        }
Ejemplo n.º 24
0
 //[Variation(Priority = 1, Desc = "XDocument invalid add - DTD after element")]
 public void InvalidAddIntoXDocument2()
 {
     runWithEvents = (bool)Params[0];
     try
     {
         var doc = new XDocument(new XElement("A"), new XComment("comm"));
         var o = new XDocumentType("D", null, null, null);
         if (runWithEvents)
         {
             eHelper = new EventsHelper(doc);
         }
         doc.LastNode.AddBeforeSelf(o);
         if (runWithEvents)
         {
             eHelper.Verify(XObjectChange.Add, o);
         }
         TestLog.Compare(false, "Exception expected");
     }
     catch (InvalidOperationException)
     {
     }
 }
Ejemplo n.º 25
0
        // DTD
        //[Variation(Priority = 0, Desc = "DTD - normal", Params = new object[] { new string[] { "root", "a1", "a2", "a3", }, "<!DOCTYPE root PUBLIC \"a1\" \"a2\"[a3]>" })]
        //[Variation(Priority = 1, Desc = "DTD - no systemId", Params = new object[] { new string[] { "root", "a1", null, "a3" }, "<!DOCTYPE root PUBLIC \"a1\" \"\"[a3]>" })]
        //[Variation(Priority = 1, Desc = "DTD - no publicId", Params = new object[] { new string[] { "root", null, "a2", "a3" }, "<!DOCTYPE root SYSTEM \"a2\"[a3]>" })]
        //[Variation(Priority = 1, Desc = "DTD - no publicId, no systemId", Params = new object[] { new string[] { "root", null, null, "a3" }, "<!DOCTYPE root [a3]>" })]
        //[Variation(Priority = 1, Desc = "DTD - all nulls", Params = new object[] { new string[] { "root", null, null, null }, "<!DOCTYPE root >" })]
        public void DTDConstruct()
        {
            var data = Variation.Params[0] as string[];
            var serial = Variation.Params[1] as string;

            var dtd = new XDocumentType(data[0], data[1], data[2], data[3]);
            TestLog.Compare(dtd.Name, data[0], "dtd.Name, data[0]");
            TestLog.Compare(dtd.PublicId, data[1], "dtd.SystemId, data[1]");
            TestLog.Compare(dtd.SystemId, data[2], "dtd.PublicId, datat[2]");
            TestLog.Compare(dtd.InternalSubset, data[3], "dtd.InternalSubset, data[3]");
            TestLog.Compare(dtd.NodeType, XmlNodeType.DocumentType, "nodetype");
            TestLog.Compare(dtd.ToString(), serial, "DTD construction");
        }
Ejemplo n.º 26
0
		internal static XNode ReadFrom (XmlReader r, LoadOptions options)
		{
			switch (r.NodeType) {
			case XmlNodeType.Element:
				return XElement.LoadCore (r, options);
			case XmlNodeType.Whitespace:
			case XmlNodeType.SignificantWhitespace:
			case XmlNodeType.Text:
				XText t = new XText (r.Value);
				t.FillLineInfoAndBaseUri (r, options);
				r.Read ();
				return t;
			case XmlNodeType.CDATA:
				XCData c = new XCData (r.Value);
				c.FillLineInfoAndBaseUri (r, options);
				r.Read ();
				return c;
			case XmlNodeType.ProcessingInstruction:
				XPI pi = new XPI (r.Name, r.Value);
				pi.FillLineInfoAndBaseUri (r, options);
				r.Read ();
				return pi;
			case XmlNodeType.Comment:
				XComment cm = new XComment (r.Value);
				cm.FillLineInfoAndBaseUri (r, options);
				r.Read ();
				return cm;
			case XmlNodeType.DocumentType:
				XDocumentType d = new XDocumentType (r.Name,
					r.GetAttribute ("PUBLIC"),
					r.GetAttribute ("SYSTEM"),
					r.Value);
				d.FillLineInfoAndBaseUri (r, options);
				r.Read ();
				return d;
			default:
				throw new InvalidOperationException (String.Format ("Node type {0} is not supported", r.NodeType));
			}
		}
Ejemplo n.º 27
0
        public void XDocTypeInXStreamingElement()
        {
            XDocumentType node = new XDocumentType(
                "DOCTYPE",
                "note",
                "SYSTEM",
                "<!ELEMENT note (to,from,heading,body)><!ELEMENT to (#PCDATA)><!ELEMENT from (#PCDATA)><!ELEMENT heading (#PCDATA)><!ELEMENT body (#PCDATA)>");

            XStreamingElement streamElement = new XStreamingElement("Root", node);
            Assert.Throws<InvalidOperationException>(() => streamElement.Save(new MemoryStream()));
        }
Ejemplo n.º 28
0
 //[Variation(Priority = 0, Desc = "XDocumentType - change system Id")]
 public void XDocTypeSystemID()
 {
     XDocumentType toChange = new XDocumentType("root", "", "", "");
     XDocument xDoc = new XDocument(toChange);
     XDocument xDocOriginal = new XDocument(xDoc);
     using (EventsHelper docHelper = new EventsHelper(xDoc))
     {
         using (EventsHelper typeHelper = new EventsHelper(toChange))
         {
             toChange.SystemId = "newValue";
             TestLog.Compare(toChange.SystemId.Equals("newValue"), "SystemID did not change");
             typeHelper.Verify(XObjectChange.Value, toChange);
         }
         docHelper.Verify(XObjectChange.Value, toChange);
     }
 }
Ejemplo n.º 29
0
        /// <summary>
        /// Read an XNode from the given XmlReader and LineInfo object. If available, line info will be added to XElement.
        /// This technique is adapted from here: http://blogs.msdn.com/b/mikechampion/archive/2006/09/10/748408.aspx
        /// </summary>
        /// <param name="reader">The XmlReader to read from</param>
        /// <param name="lineInfo">This should be <paramref name="reader"/> cast as an <see cref="IXmlLineInfo"/></param>
        /// <returns>an XNode with line information if present</returns>
        /// <seealso cref="XNode.ReadFrom">This function replaces XNode.ReadFrom</seealso>
        public static XNode ReadWithLineInfo(XmlReader reader, IXmlLineInfo lineInfo)
        {
            XNode node = null;
            XElement parent = null;

            do
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        // create a new element with the given name
                        XElement element = new XElement(XName.Get(reader.LocalName, reader.NamespaceURI));

                        // add attributes to the element
                        if (reader.MoveToFirstAttribute())
                        {
                            do
                            {
                                // Compound documents created with older versions of ABB.SrcML left out some namespaces
                                // this causes an "xmlns" attribute to be added to any names not in the default (SRC) namespace
                                // to avoid an ArgumentException thrown by element.Add in this case, just don't add these
                                if (!(reader.LocalName == "xmlns" && reader.NamespaceURI == "http://www.w3.org/2000/xmlns/"))
                                    element.Add(new XAttribute(XName.Get(reader.LocalName, reader.NamespaceURI), reader.Value));
                            } while (reader.MoveToNextAttribute());
                            reader.MoveToElement();
                        }
                        // add a ABB.SrcML.LineInfo annotation to the element if line information is present.
                        if (lineInfo.HasLineInfo())
                        {
                            element.SetLineInfo(new LineInfo(lineInfo.LineNumber, lineInfo.LinePosition));
                        }

                        // if the reader is not empty, we have to go and get all of the children and add them.
                        // otherwise, we can jsut set this to node.
                        if (!reader.IsEmptyElement)
                        {
                            if (null != parent)
                            {
                                parent.Add(element);
                            }
                            parent = element;
                            continue;
                        }
                        else
                        {
                            node = element;
                        }
                        break;
                    case XmlNodeType.EndElement:
                        // process the EndElement
                        if (null == parent)
                            return null;
                        if (parent.IsEmpty)
                        {
                            parent.Add(string.Empty);
                        }
                        if (parent.Parent == null)
                            return parent;
                        parent = parent.Parent;
                        continue;
                    case XmlNodeType.Text:
                    case XmlNodeType.SignificantWhitespace:
                    case XmlNodeType.Whitespace:
                        node = new XText(reader.Value);
                        break;
                    case XmlNodeType.CDATA:
                        node = new XCData(reader.Value);
                        break;
                    case XmlNodeType.Comment:
                        node = new XComment(reader.Value);
                        break;
                    case XmlNodeType.ProcessingInstruction:
                        node = new XProcessingInstruction(reader.Name, reader.Value);
                        break;
                    case XmlNodeType.DocumentType:
                        node = new XDocumentType(reader.LocalName, reader.GetAttribute("PUBLIC"), reader.GetAttribute("SYSTEM"), reader.Value);
                        break;
                    case XmlNodeType.EntityReference:
                        reader.ResolveEntity();
                        continue;
                    case XmlNodeType.XmlDeclaration:
                    case XmlNodeType.EndEntity:
                        continue;
                    default:
                        throw new InvalidOperationException();
                }

                if (null == parent)
                    return node;
                parent.Add(node);
            } while (reader.Read());
            return null;
        }
Ejemplo n.º 30
0
 //[Variation(Priority = 0, Desc = "XDocumentType - Valid Name")]
 public void ValidDocTypeVariation()
 {
     _runWithEvents = (bool)Params[0];
     XDocumentType toChange = new XDocumentType("root", "", "", "");
     if (_runWithEvents) _eHelper = new EventsHelper(toChange);
     toChange.Name = "newName";
     if (_runWithEvents) _eHelper.Verify(XObjectChange.Name);
     TestLog.Compare(toChange.Name.Equals("newName"), "Name did not change");
 }
Ejemplo n.º 31
0
        internal void ReadContentFrom(XmlReader r, LoadOptions o)
        {
            if ((o & (LoadOptions.SetBaseUri | LoadOptions.SetLineInfo)) == 0)
            {
                ReadContentFrom(r);
                return;
            }
            if (r.ReadState != ReadState.Interactive)
            {
                throw new InvalidOperationException(SR.InvalidOperation_ExpectedInteractive);
            }
            XContainer     c       = this;
            XNode          n       = null;
            NamespaceCache eCache  = new NamespaceCache();
            NamespaceCache aCache  = new NamespaceCache();
            string         baseUri = (o & LoadOptions.SetBaseUri) != 0 ? r.BaseURI : null;
            IXmlLineInfo   li      = (o & LoadOptions.SetLineInfo) != 0 ? r as IXmlLineInfo : null;

            do
            {
                string uri = r.BaseURI;
                switch (r.NodeType)
                {
                case XmlNodeType.Element:
                {
                    XElement e = new XElement(eCache.Get(r.NamespaceURI).GetName(r.LocalName));
                    if (baseUri != null && baseUri != uri)
                    {
                        e.SetBaseUri(uri);
                    }
                    if (li != null && li.HasLineInfo())
                    {
                        e.SetLineInfo(li.LineNumber, li.LinePosition);
                    }
                    if (r.MoveToFirstAttribute())
                    {
                        do
                        {
                            XAttribute a = new XAttribute(aCache.Get(r.Prefix.Length == 0 ? string.Empty : r.NamespaceURI).GetName(r.LocalName), r.Value);
                            if (li != null && li.HasLineInfo())
                            {
                                a.SetLineInfo(li.LineNumber, li.LinePosition);
                            }
                            e.AppendAttributeSkipNotify(a);
                        } while (r.MoveToNextAttribute());
                        r.MoveToElement();
                    }
                    c.AddNodeSkipNotify(e);
                    if (!r.IsEmptyElement)
                    {
                        c = e;
                        if (baseUri != null)
                        {
                            baseUri = uri;
                        }
                    }
                    break;
                }

                case XmlNodeType.EndElement:
                {
                    if (c.content == null)
                    {
                        c.content = string.Empty;
                    }
                    // Store the line info of the end element tag.
                    // Note that since we've got EndElement the current container must be an XElement
                    XElement e = c as XElement;
                    Debug.Assert(e != null, "EndElement received but the current container is not an element.");
                    if (e != null && li != null && li.HasLineInfo())
                    {
                        e.SetEndElementLineInfo(li.LineNumber, li.LinePosition);
                    }
                    if (c == this)
                    {
                        return;
                    }
                    if (baseUri != null && c.HasBaseUri)
                    {
                        baseUri = c.parent.BaseUri;
                    }
                    c = c.parent;
                    break;
                }

                case XmlNodeType.Text:
                case XmlNodeType.SignificantWhitespace:
                case XmlNodeType.Whitespace:
                    if ((baseUri != null && baseUri != uri) ||
                        (li != null && li.HasLineInfo()))
                    {
                        n = new XText(r.Value);
                    }
                    else
                    {
                        c.AddStringSkipNotify(r.Value);
                    }
                    break;

                case XmlNodeType.CDATA:
                    n = new XCData(r.Value);
                    break;

                case XmlNodeType.Comment:
                    n = new XComment(r.Value);
                    break;

                case XmlNodeType.ProcessingInstruction:
                    n = new XProcessingInstruction(r.Name, r.Value);
                    break;

                case XmlNodeType.DocumentType:
                    n = new XDocumentType(r.LocalName, r.GetAttribute("PUBLIC"), r.GetAttribute("SYSTEM"), r.Value);
                    break;

                case XmlNodeType.EntityReference:
                    if (!r.CanResolveEntity)
                    {
                        throw new InvalidOperationException(SR.InvalidOperation_UnresolvedEntityReference);
                    }
                    r.ResolveEntity();
                    break;

                case XmlNodeType.EndEntity:
                    break;

                default:
                    throw new InvalidOperationException(SR.Format(SR.InvalidOperation_UnexpectedNodeType, r.NodeType));
                }
                if (n != null)
                {
                    if (baseUri != null && baseUri != uri)
                    {
                        n.SetBaseUri(uri);
                    }
                    if (li != null && li.HasLineInfo())
                    {
                        n.SetLineInfo(li.LineNumber, li.LinePosition);
                    }
                    c.AddNodeSkipNotify(n);
                    n = null;
                }
            } while (r.Read());
        }
Ejemplo n.º 32
0
 public void XDocumentTypeChangeInternalSubset()
 {
     XDocumentType toChange = new XDocumentType("root", "", "", "");
     XDocument xDoc = new XDocument(toChange);
     XDocument xDocOriginal = new XDocument(xDoc);
     using (EventsHelper docHelper = new EventsHelper(xDoc))
     {
         using (EventsHelper typeHelper = new EventsHelper(toChange))
         {
             toChange.InternalSubset = "newValue";
             Assert.True(toChange.InternalSubset.Equals("newValue"), "Internal Subset did not change");
             typeHelper.Verify(XObjectChange.Value, toChange);
         }
         docHelper.Verify(XObjectChange.Value, toChange);
     }
 }
Ejemplo n.º 33
0
        internal override bool DeepEquals(XNode node)
        {
            XDocumentType type = node as XDocumentType;

            return((((type != null) && (this.name == type.name)) && ((this.publicId == type.publicId) && (this.systemId == type.SystemId))) && (this.internalSubset == type.internalSubset));
        }
Ejemplo n.º 34
0
        internal void ReadContentFrom(XmlReader r, LoadOptions o)
        {
            if ((o & (LoadOptions.SetLineInfo | LoadOptions.SetBaseUri)) == LoadOptions.None)
            {
                this.ReadContentFrom(r);
            }
            else
            {
                if (r.ReadState != System.Xml.ReadState.Interactive)
                {
                    throw new InvalidOperationException(System.Xml.Linq.Res.GetString("InvalidOperation_ExpectedInteractive"));
                }
                XContainer     parent  = this;
                XNode          n       = null;
                NamespaceCache cache   = new NamespaceCache();
                NamespaceCache cache2  = new NamespaceCache();
                string         baseUri = ((o & LoadOptions.SetBaseUri) != LoadOptions.None) ? r.BaseURI : null;
                IXmlLineInfo   info    = ((o & LoadOptions.SetLineInfo) != LoadOptions.None) ? (r as IXmlLineInfo) : null;
                do
                {
                    string baseURI = r.BaseURI;
                    switch (r.NodeType)
                    {
                    case XmlNodeType.Element:
                    {
                        XElement element = new XElement(cache.Get(r.NamespaceURI).GetName(r.LocalName));
                        if ((baseUri != null) && (baseUri != baseURI))
                        {
                            element.SetBaseUri(baseURI);
                        }
                        if ((info != null) && info.HasLineInfo())
                        {
                            element.SetLineInfo(info.LineNumber, info.LinePosition);
                        }
                        if (r.MoveToFirstAttribute())
                        {
                            do
                            {
                                XAttribute a = new XAttribute(cache2.Get((r.Prefix.Length == 0) ? string.Empty : r.NamespaceURI).GetName(r.LocalName), r.Value);
                                if ((info != null) && info.HasLineInfo())
                                {
                                    a.SetLineInfo(info.LineNumber, info.LinePosition);
                                }
                                element.AppendAttributeSkipNotify(a);
                            }while (r.MoveToNextAttribute());
                            r.MoveToElement();
                        }
                        parent.AddNodeSkipNotify(element);
                        if (!r.IsEmptyElement)
                        {
                            parent = element;
                            if (baseUri != null)
                            {
                                baseUri = baseURI;
                            }
                        }
                        break;
                    }

                    case XmlNodeType.Text:
                    case XmlNodeType.Whitespace:
                    case XmlNodeType.SignificantWhitespace:
                        if (((baseUri == null) || (baseUri == baseURI)) && ((info == null) || !info.HasLineInfo()))
                        {
                            parent.AddStringSkipNotify(r.Value);
                        }
                        else
                        {
                            n = new XText(r.Value);
                        }
                        break;

                    case XmlNodeType.CDATA:
                        n = new XCData(r.Value);
                        break;

                    case XmlNodeType.EntityReference:
                        if (!r.CanResolveEntity)
                        {
                            throw new InvalidOperationException(System.Xml.Linq.Res.GetString("InvalidOperation_UnresolvedEntityReference"));
                        }
                        r.ResolveEntity();
                        break;

                    case XmlNodeType.ProcessingInstruction:
                        n = new XProcessingInstruction(r.Name, r.Value);
                        break;

                    case XmlNodeType.Comment:
                        n = new XComment(r.Value);
                        break;

                    case XmlNodeType.DocumentType:
                        n = new XDocumentType(r.LocalName, r.GetAttribute("PUBLIC"), r.GetAttribute("SYSTEM"), r.Value, r.DtdInfo);
                        break;

                    case XmlNodeType.EndElement:
                    {
                        if (parent.content == null)
                        {
                            parent.content = string.Empty;
                        }
                        XElement element2 = parent as XElement;
                        if (((element2 != null) && (info != null)) && info.HasLineInfo())
                        {
                            element2.SetEndElementLineInfo(info.LineNumber, info.LinePosition);
                        }
                        if (parent == this)
                        {
                            return;
                        }
                        if ((baseUri != null) && parent.HasBaseUri)
                        {
                            baseUri = parent.parent.BaseUri;
                        }
                        parent = parent.parent;
                        break;
                    }

                    case XmlNodeType.EndEntity:
                        break;

                    default:
                        throw new InvalidOperationException(System.Xml.Linq.Res.GetString("InvalidOperation_UnexpectedNodeType", new object[] { r.NodeType }));
                    }
                    if (n != null)
                    {
                        if ((baseUri != null) && (baseUri != baseURI))
                        {
                            n.SetBaseUri(baseURI);
                        }
                        if ((info != null) && info.HasLineInfo())
                        {
                            n.SetLineInfo(info.LineNumber, info.LinePosition);
                        }
                        parent.AddNodeSkipNotify(n);
                        n = null;
                    }
                }while (r.Read());
            }
        }
Ejemplo n.º 35
0
        //[Variation(Priority = 0, Desc = "Write and valIdate XDocumentType")]
        public void writer_5()
        {
            string expectedXml = "<!DOCTYPE root PUBLIC \"\" \"\"[<!ELEMENT root ANY>]>";
            var doc = new XDocumentType("root", "", "", "<!ELEMENT root ANY>");

            TestToStringAndXml(doc, expectedXml);

            var ws = new XmlWriterSettings();
            ws.ConformanceLevel = ConformanceLevel.Document;
            ws.OmitXmlDeclaration = true;
            // Set to true when FileIO is ok
            ws.CloseOutput = false;

            // Use a file to replace this when FileIO is ok
            var m = new MemoryStream();
            using (XmlWriter wr = XmlWriter.Create(m, ws))
            {
                doc.WriteTo(wr);
            }
            m.Position = 0;
            using (TextReader r = new StreamReader(m))
            {
                string actualXml = r.ReadToEnd();
                TestLog.Compare(expectedXml, actualXml, "XDocumentType writeTo method failed");
            }
        }
 // Summary:
 //     Initializes an instance of the System.Xml.Linq.XDocumentType class from another
 //     System.Xml.Linq.XDocumentType object.
 //
 // Parameters:
 //   other:
 //     An System.Xml.Linq.XDocumentType object to copy from.
 public XDocumentType(XDocumentType other)
 {
   Contract.Requires(other != null);
 }
 // Summary:
 //     Initializes an instance of the System.Xml.Linq.XDocumentType class from another
 //     System.Xml.Linq.XDocumentType object.
 //
 // Parameters:
 //   other:
 //     An System.Xml.Linq.XDocumentType object to copy from.
 public XDocumentType(XDocumentType other)
 {
     Contract.Requires(other != null);
 }
Ejemplo n.º 38
0
 //[Variation(Priority = 0, Desc = "DTD : all field", Params = new object[] { new string[] { "root", "a", "b", "c" }, new string[] { "root", "a", "b", "c" }, true })]
 //[Variation(Priority = 1, Desc = "DTD : all nulls", Params = new object[] { new string[] { "root", null, null, null }, new string[] { "root", null, null, null }, true })]
 //[Variation(Priority = 2, Desc = "DTD : internal subset only", Params = new object[] { new string[] { "root", null, null, "data" }, new string[] { "root", null, null, "data" }, true })]
 //[Variation(Priority = 0, Desc = "DTD (negative) : name diff", Params = new object[] { new string[] { "A", "", "", "" }, new string[] { "B", "", "", "" }, false })]
 //[Variation(Priority = 1, Desc = "DTD (negative) : subset diff", Params = new object[] { new string[] { "A", null, null, "aa" }, new string[] { "A", null, null, "bb" }, false })]
 //[Variation(Priority = 2, Desc = "DTD (negative) : null vs. \"\"", Params = new object[] { new string[] { "A", "", "", "" }, new string[] { "A", null, null, null }, false })]
 public void DTD()
 {
     bool expected = (bool)Variation.Params[2];
     var data0 = Variation.Params[0] as string[];
     var data1 = Variation.Params[1] as string[];
     XDocumentType dtd1 = new XDocumentType(data0[0], data0[1], data0[2], data0[3]);
     XDocumentType dtd2 = new XDocumentType(data1[0], data1[1], data1[2], data1[3]);
     VerifyComparison(expected, dtd1, dtd2);
 }
Ejemplo n.º 39
0
 internal void ReadContentFrom(XmlReader r, LoadOptions o)
 {
     if ((o & (LoadOptions.SetBaseUri | LoadOptions.SetLineInfo)) == 0)
     {
         ReadContentFrom(r);
         return;
     }
     if (r.ReadState != ReadState.Interactive) throw new InvalidOperationException(SR.InvalidOperation_ExpectedInteractive);
     XContainer c = this;
     XNode n = null;
     NamespaceCache eCache = new NamespaceCache();
     NamespaceCache aCache = new NamespaceCache();
     string baseUri = (o & LoadOptions.SetBaseUri) != 0 ? r.BaseURI : null;
     IXmlLineInfo li = (o & LoadOptions.SetLineInfo) != 0 ? r as IXmlLineInfo : null;
     do
     {
         string uri = r.BaseURI;
         switch (r.NodeType)
         {
             case XmlNodeType.Element:
                 {
                     XElement e = new XElement(eCache.Get(r.NamespaceURI).GetName(r.LocalName));
                     if (baseUri != null && baseUri != uri)
                     {
                         e.SetBaseUri(uri);
                     }
                     if (li != null && li.HasLineInfo())
                     {
                         e.SetLineInfo(li.LineNumber, li.LinePosition);
                     }
                     if (r.MoveToFirstAttribute())
                     {
                         do
                         {
                             XAttribute a = new XAttribute(aCache.Get(r.Prefix.Length == 0 ? string.Empty : r.NamespaceURI).GetName(r.LocalName), r.Value);
                             if (li != null && li.HasLineInfo())
                             {
                                 a.SetLineInfo(li.LineNumber, li.LinePosition);
                             }
                             e.AppendAttributeSkipNotify(a);
                         } while (r.MoveToNextAttribute());
                         r.MoveToElement();
                     }
                     c.AddNodeSkipNotify(e);
                     if (!r.IsEmptyElement)
                     {
                         c = e;
                         if (baseUri != null)
                         {
                             baseUri = uri;
                         }
                     }
                     break;
                 }
             case XmlNodeType.EndElement:
                 {
                     if (c.content == null)
                     {
                         c.content = string.Empty;
                     }
                     // Store the line info of the end element tag.
                     // Note that since we've got EndElement the current container must be an XElement
                     XElement e = c as XElement;
                     Debug.Assert(e != null, "EndElement received but the current container is not an element.");
                     if (e != null && li != null && li.HasLineInfo())
                     {
                         e.SetEndElementLineInfo(li.LineNumber, li.LinePosition);
                     }
                     if (c == this) return;
                     if (baseUri != null && c.HasBaseUri)
                     {
                         baseUri = c.parent.BaseUri;
                     }
                     c = c.parent;
                     break;
                 }
             case XmlNodeType.Text:
             case XmlNodeType.SignificantWhitespace:
             case XmlNodeType.Whitespace:
                 if ((baseUri != null && baseUri != uri) ||
                     (li != null && li.HasLineInfo()))
                 {
                     n = new XText(r.Value);
                 }
                 else
                 {
                     c.AddStringSkipNotify(r.Value);
                 }
                 break;
             case XmlNodeType.CDATA:
                 n = new XCData(r.Value);
                 break;
             case XmlNodeType.Comment:
                 n = new XComment(r.Value);
                 break;
             case XmlNodeType.ProcessingInstruction:
                 n = new XProcessingInstruction(r.Name, r.Value);
                 break;
             case XmlNodeType.DocumentType:
                 n = new XDocumentType(r.LocalName, r.GetAttribute("PUBLIC"), r.GetAttribute("SYSTEM"), r.Value);
                 break;
             case XmlNodeType.EntityReference:
                 if (!r.CanResolveEntity) throw new InvalidOperationException(SR.InvalidOperation_UnresolvedEntityReference);
                 r.ResolveEntity();
                 break;
             case XmlNodeType.EndEntity:
                 break;
             default:
                 throw new InvalidOperationException(SR.Format(SR.InvalidOperation_UnexpectedNodeType, r.NodeType));
         }
         if (n != null)
         {
             if (baseUri != null && baseUri != uri)
             {
                 n.SetBaseUri(uri);
             }
             if (li != null && li.HasLineInfo())
             {
                 n.SetLineInfo(li.LineNumber, li.LinePosition);
             }
             c.AddNodeSkipNotify(n);
             n = null;
         }
     } while (r.Read());
 }
Ejemplo n.º 40
0
                //[Variation(Priority = 0, Desc = "XDocumentType - Invalid Name")]
                public void InvalidDocTypeVariation()
                {
                    _runWithEvents = (bool)Params[0];
                    XDocumentType toChange = new XDocumentType("root", "", "", "");
                    if (_runWithEvents) _eHelper = new EventsHelper(toChange);

                    try
                    {
                        toChange.Name = null;
                    }
                    catch (Exception)
                    {
                        if (_runWithEvents) _eHelper.Verify(0);
                        return;
                    }

                    try
                    {
                        toChange.Name = " ";
                    }
                    catch (Exception)
                    {
                        if (_runWithEvents) _eHelper.Verify(0);
                        return;
                    }

                    try
                    {
                        toChange.Name = "";
                    }
                    catch (Exception)
                    {
                        if (_runWithEvents) _eHelper.Verify(0);
                        return;
                    }
                }
Ejemplo n.º 41
0
 //[Variation(Priority = 0, Desc = "Constructor - XStreamingElement(XName, XDocumentType)")]
 public void XDocTypeInXStreamingElement()
 {
     XDocumentType node = new XDocumentType("DOCTYPE", "note", "SYSTEM",
         "<!ELEMENT note (to,from,heading,body)><!ELEMENT to (#PCDATA)><!ELEMENT from (#PCDATA)><!ELEMENT heading (#PCDATA)><!ELEMENT body (#PCDATA)>");
     try
     {
         XStreamingElement streamElement = new XStreamingElement("Root", node);
         streamElement.Save(new MemoryStream());
     }
     catch (System.InvalidOperationException)
     {
         return;
     }
     throw new TestFailedException("");
 }
Ejemplo n.º 42
0
        public bool Equals(XNode x, XNode y)
        {
            if (x == null)
            {
                return(y == null);
            }
            else if (y == null)
            {
                return(false);
            }
            //throw new NotImplementedException ();
            if (x.NodeType != y.NodeType)
            {
                return(false);
            }
            switch (x.NodeType)
            {
            case XmlNodeType.Document:
                XDocument doc1 = (XDocument)x;
                XDocument doc2 = (XDocument)y;
                if (!Equals(doc1.Declaration, doc2.Declaration))
                {
                    return(false);
                }
                IEnumerator <XNode> id2 = doc2.Nodes().GetEnumerator();
                foreach (XNode n in doc1.Nodes())
                {
                    if (!id2.MoveNext())
                    {
                        return(false);
                    }
                    if (!Equals(n, id2.Current))
                    {
                        return(false);
                    }
                }
                return(!id2.MoveNext());

            case XmlNodeType.Element:
                XElement e1 = (XElement)x;
                XElement e2 = (XElement)y;
                if (e1.Name != e2.Name)
                {
                    return(false);
                }
                IEnumerator <XAttribute> ia2 = e2.Attributes().GetEnumerator();
                foreach (XAttribute n in e1.Attributes())
                {
                    if (!ia2.MoveNext())
                    {
                        return(false);
                    }
                    if (!Equals(n, ia2.Current))
                    {
                        return(false);
                    }
                }
                if (ia2.MoveNext())
                {
                    return(false);
                }
                IEnumerator <XNode> ie2 = e2.Nodes().GetEnumerator();
                foreach (XNode n in e1.Nodes())
                {
                    if (!ie2.MoveNext())
                    {
                        return(false);
                    }
                    if (!Equals(n, ie2.Current))
                    {
                        return(false);
                    }
                }
                return(!ie2.MoveNext());

            case XmlNodeType.Comment:
                XComment c1 = (XComment)x;
                XComment c2 = (XComment)y;
                return(c1.Value == c2.Value);

            case XmlNodeType.ProcessingInstruction:
                XPI p1 = (XPI)x;
                XPI p2 = (XPI)y;
                return(p1.Target == p2.Target && p1.Data == p2.Data);

            case XmlNodeType.DocumentType:
                XDocumentType d1 = (XDocumentType)x;
                XDocumentType d2 = (XDocumentType)y;
                return(d1.Name == d2.Name &&
                       d1.PublicId == d2.PublicId &&
                       d1.SystemId == d2.SystemId &&
                       d1.InternalSubset == d2.InternalSubset);

            case XmlNodeType.Text:
                return(((XText)x).Value == ((XText)y).Value);
            }
            throw new Exception("INTERNAL ERROR: should not happen");
        }
Ejemplo n.º 43
0
            public bool ReadContentFrom(XContainer rootContainer, XmlReader r, LoadOptions o)
            {
                XNode  newNode = null;
                string baseUri = r.BaseURI;

                switch (r.NodeType)
                {
                case XmlNodeType.Element:
                {
                    XElement e = new XElement(_eCache.Get(r.NamespaceURI).GetName(r.LocalName));
                    if (_baseUri != null && _baseUri != baseUri)
                    {
                        e.SetBaseUri(baseUri);
                    }
                    if (_lineInfo != null && _lineInfo.HasLineInfo())
                    {
                        e.SetLineInfo(_lineInfo.LineNumber, _lineInfo.LinePosition);
                    }
                    if (r.MoveToFirstAttribute())
                    {
                        do
                        {
                            XAttribute a = new XAttribute(_aCache.Get(r.Prefix.Length == 0 ? string.Empty : r.NamespaceURI).GetName(r.LocalName), r.Value);
                            if (_lineInfo != null && _lineInfo.HasLineInfo())
                            {
                                a.SetLineInfo(_lineInfo.LineNumber, _lineInfo.LinePosition);
                            }
                            e.AppendAttributeSkipNotify(a);
                        } while (r.MoveToNextAttribute());
                        r.MoveToElement();
                    }
                    _currentContainer.AddNodeSkipNotify(e);
                    if (!r.IsEmptyElement)
                    {
                        _currentContainer = e;
                        if (_baseUri != null)
                        {
                            _baseUri = baseUri;
                        }
                    }
                    break;
                }

                case XmlNodeType.EndElement:
                {
                    if (_currentContainer.content == null)
                    {
                        _currentContainer.content = string.Empty;
                    }
                    // Store the line info of the end element tag.
                    // Note that since we've got EndElement the current container must be an XElement
                    XElement e = _currentContainer as XElement;
                    Debug.Assert(e != null, "EndElement received but the current container is not an element.");
                    if (e != null && _lineInfo != null && _lineInfo.HasLineInfo())
                    {
                        e.SetEndElementLineInfo(_lineInfo.LineNumber, _lineInfo.LinePosition);
                    }
                    if (_currentContainer == rootContainer)
                    {
                        return(false);
                    }
                    if (_baseUri != null && _currentContainer.HasBaseUri)
                    {
                        _baseUri = _currentContainer.parent.BaseUri;
                    }
                    _currentContainer = _currentContainer.parent;
                    break;
                }

                case XmlNodeType.Text:
                case XmlNodeType.SignificantWhitespace:
                case XmlNodeType.Whitespace:
                    if ((_baseUri != null && _baseUri != baseUri) ||
                        (_lineInfo != null && _lineInfo.HasLineInfo()))
                    {
                        newNode = new XText(r.Value);
                    }
                    else
                    {
                        _currentContainer.AddStringSkipNotify(r.Value);
                    }
                    break;

                case XmlNodeType.CDATA:
                    newNode = new XCData(r.Value);
                    break;

                case XmlNodeType.Comment:
                    newNode = new XComment(r.Value);
                    break;

                case XmlNodeType.ProcessingInstruction:
                    newNode = new XProcessingInstruction(r.Name, r.Value);
                    break;

                case XmlNodeType.DocumentType:
                    newNode = new XDocumentType(r.LocalName, r.GetAttribute("PUBLIC"), r.GetAttribute("SYSTEM"), r.Value);
                    break;

                case XmlNodeType.EntityReference:
                    if (!r.CanResolveEntity)
                    {
                        throw new InvalidOperationException(SR.InvalidOperation_UnresolvedEntityReference);
                    }
                    r.ResolveEntity();
                    break;

                case XmlNodeType.EndEntity:
                    break;

                default:
                    throw new InvalidOperationException(SR.Format(SR.InvalidOperation_UnexpectedNodeType, r.NodeType));
                }

                if (newNode != null)
                {
                    if (_baseUri != null && _baseUri != baseUri)
                    {
                        newNode.SetBaseUri(baseUri);
                    }

                    if (_lineInfo != null && _lineInfo.HasLineInfo())
                    {
                        newNode.SetLineInfo(_lineInfo.LineNumber, _lineInfo.LinePosition);
                    }

                    _currentContainer.AddNodeSkipNotify(newNode);
                    newNode = null;
                }

                return(true);
            }
Ejemplo n.º 44
0
 /// <summary>
 /// Invoked for each <see cref="XDocumentType"/>
 /// </summary>
 /// <param name="documentType"></param>
 public virtual void Visit(XDocumentType documentType)
 {
     Contract.Requires<ArgumentNullException>(documentType != null);
 }
Ejemplo n.º 45
0
 /// <summary>
 /// Initializes an instance of the XDocumentType class
 /// from another XDocumentType object.
 /// </summary>
 /// <param name="other"><see cref="XDocumentType"/> object to copy from.</param>
 public XDocumentType(XDocumentType other !!)
 {
		public XDocumentTypeWrapper(XDocumentType documentType) : base(documentType)
		{
			_documentType = documentType;
		}
        internal void ReadContentFrom(XmlReader r, LoadOptions o)
        {
            if ((o & (LoadOptions.SetLineInfo | LoadOptions.SetBaseUri)) == LoadOptions.None)
            {
                this.ReadContentFrom(r);
            }
            else
            {
                if (r.ReadState != System.Xml.ReadState.Interactive)
                {
                    throw new InvalidOperationException(System.Xml.Linq.Res.GetString("InvalidOperation_ExpectedInteractive"));
                }
                XContainer parent = this;
                XNode n = null;
                NamespaceCache cache = new NamespaceCache();
                NamespaceCache cache2 = new NamespaceCache();
                string baseUri = ((o & LoadOptions.SetBaseUri) != LoadOptions.None) ? r.BaseURI : null;
                IXmlLineInfo info = ((o & LoadOptions.SetLineInfo) != LoadOptions.None) ? (r as IXmlLineInfo) : null;
                do
                {
                    string baseURI = r.BaseURI;
                    switch (r.NodeType)
                    {
                        case XmlNodeType.Element:
                        {
                            XElement element = new XElement(cache.Get(r.NamespaceURI).GetName(r.LocalName));
                            if ((baseUri != null) && (baseUri != baseURI))
                            {
                                element.SetBaseUri(baseURI);
                            }
                            if ((info != null) && info.HasLineInfo())
                            {
                                element.SetLineInfo(info.LineNumber, info.LinePosition);
                            }
                            if (r.MoveToFirstAttribute())
                            {
                                do
                                {
                                    XAttribute a = new XAttribute(cache2.Get((r.Prefix.Length == 0) ? string.Empty : r.NamespaceURI).GetName(r.LocalName), r.Value);
                                    if ((info != null) && info.HasLineInfo())
                                    {
                                        a.SetLineInfo(info.LineNumber, info.LinePosition);
                                    }
                                    element.AppendAttributeSkipNotify(a);
                                }
                                while (r.MoveToNextAttribute());
                                r.MoveToElement();
                            }
                            parent.AddNodeSkipNotify(element);
                            if (!r.IsEmptyElement)
                            {
                                parent = element;
                                if (baseUri != null)
                                {
                                    baseUri = baseURI;
                                }
                            }
                            break;
                        }
                        case XmlNodeType.Text:
                        case XmlNodeType.Whitespace:
                        case XmlNodeType.SignificantWhitespace:
                            if (((baseUri == null) || (baseUri == baseURI)) && ((info == null) || !info.HasLineInfo()))
                            {
                                parent.AddStringSkipNotify(r.Value);
                            }
                            else
                            {
                                n = new XText(r.Value);
                            }
                            break;

                        case XmlNodeType.CDATA:
                            n = new XCData(r.Value);
                            break;

                        case XmlNodeType.EntityReference:
                            if (!r.CanResolveEntity)
                            {
                                throw new InvalidOperationException(System.Xml.Linq.Res.GetString("InvalidOperation_UnresolvedEntityReference"));
                            }
                            r.ResolveEntity();
                            break;

                        case XmlNodeType.ProcessingInstruction:
                            n = new XProcessingInstruction(r.Name, r.Value);
                            break;

                        case XmlNodeType.Comment:
                            n = new XComment(r.Value);
                            break;

                        case XmlNodeType.DocumentType:
                            n = new XDocumentType(r.LocalName, r.GetAttribute("PUBLIC"), r.GetAttribute("SYSTEM"), r.Value, r.DtdInfo);
                            break;

                        case XmlNodeType.EndElement:
                        {
                            if (parent.content == null)
                            {
                                parent.content = string.Empty;
                            }
                            XElement element2 = parent as XElement;
                            if (((element2 != null) && (info != null)) && info.HasLineInfo())
                            {
                                element2.SetEndElementLineInfo(info.LineNumber, info.LinePosition);
                            }
                            if (parent == this)
                            {
                                return;
                            }
                            if ((baseUri != null) && parent.HasBaseUri)
                            {
                                baseUri = parent.parent.BaseUri;
                            }
                            parent = parent.parent;
                            break;
                        }
                        case XmlNodeType.EndEntity:
                            break;

                        default:
                            throw new InvalidOperationException(System.Xml.Linq.Res.GetString("InvalidOperation_UnexpectedNodeType", new object[] { r.NodeType }));
                    }
                    if (n != null)
                    {
                        if ((baseUri != null) && (baseUri != baseURI))
                        {
                            n.SetBaseUri(baseURI);
                        }
                        if ((info != null) && info.HasLineInfo())
                        {
                            n.SetLineInfo(info.LineNumber, info.LinePosition);
                        }
                        parent.AddNodeSkipNotify(n);
                        n = null;
                    }
                }
                while (r.Read());
            }
        }