/// <summary> /// Validates the behavior of the Equals overload on XProcessingInstruction. /// </summary> /// <returns>true if pass, false if fail</returns> //[Variation(Desc = "ProcessingInstructionEquals")] public void ProcessingInstructionEquals() { XProcessingInstruction c1 = new XProcessingInstruction("targetx", "datax"); XProcessingInstruction c2 = new XProcessingInstruction("targetx", "datay"); XProcessingInstruction c3 = new XProcessingInstruction("targety", "datax"); XProcessingInstruction c4 = new XProcessingInstruction("targety", "datay"); XProcessingInstruction c5 = new XProcessingInstruction("targetx", "datax"); bool b1 = XNode.DeepEquals(c1, (XProcessingInstruction)null); bool b3 = XNode.DeepEquals(c1, c1); bool b4 = XNode.DeepEquals(c1, c2); bool b5 = XNode.DeepEquals(c1, c3); bool b6 = XNode.DeepEquals(c1, c4); bool b7 = XNode.DeepEquals(c1, c5); Validate.IsEqual(b1, false); Validate.IsEqual(b3, true); Validate.IsEqual(b4, false); Validate.IsEqual(b5, false); Validate.IsEqual(b6, false); Validate.IsEqual(b7, true); b1 = XNode.EqualityComparer.GetHashCode(c1) == XNode.EqualityComparer.GetHashCode(c5); Validate.IsEqual(b1, true); }
/// <summary> /// Initializes a new XML processing instruction by copying its target and data /// from another XML processing instruction. /// </summary> /// <param name="other">XML processing instruction to copy from.</param> public XProcessingInstruction(XProcessingInstruction other) { ArgumentNullException.ThrowIfNull(other); this.target = other.target; this.data = other.data; }
public XProcessingInstruction(XProcessingInstruction other) { if (other == null) throw new ArgumentNullException ("other"); this.name = other.name; this.data = other.data; }
/// <summary> /// Tests the ProcessingInstruction constructor that takes a value. /// </summary> /// <param name="contextValue"></param> /// <returns></returns> //[Variation(Desc = "CreateProcessingInstructionSimple")] public void CreateProcessingInstructionSimple() { try { new XProcessingInstruction(null, "abcd"); Validate.ExpectedThrow(typeof(ArgumentNullException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentNullException)); } try { new XProcessingInstruction("abcd", null); Validate.ExpectedThrow(typeof(ArgumentNullException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentNullException)); } XProcessingInstruction c = new XProcessingInstruction("foo", "bar"); Validate.IsEqual(c.Target, "foo"); Validate.IsEqual(c.Data, "bar"); Validate.IsNull(c.Parent); }
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); }
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); }
public void Data() { XPI pi = new XPI("mytarget", String.Empty); Assert.AreEqual("mytarget", pi.Target, "#1"); Assert.AreEqual(String.Empty, pi.Data, "#2"); }
/// <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); }
//[Variation(Priority = 0, Desc = "XProcessingInstruction - Valid Name")] public void ValidPIVariation() { _runWithEvents = (bool)Params[0]; XProcessingInstruction toChange = new XProcessingInstruction("target", "data"); if (_runWithEvents) _eHelper = new EventsHelper(toChange); toChange.Target = "newTarget"; if (_runWithEvents) _eHelper.Verify(XObjectChange.Name); TestLog.Compare(toChange.Target.Equals("newTarget"), "Name did not change"); }
public XProcessingInstruction(XProcessingInstruction other) { if (other == null) { throw new ArgumentNullException("other"); } this.name = other.name; this.data = other.data; }
public XProcessingInstruction(XProcessingInstruction other) { if (other == null) { throw new ArgumentNullException("other"); } this.target = other.target; this.data = other.data; }
/// <summary> /// Initializes a new XML processing instruction by copying its target and data /// from another XML processing instruction. /// </summary> /// <param name="other">XML processing instruction to copy from.</param> public XProcessingInstruction(XProcessingInstruction other) { if (other == null) { throw new ArgumentNullException(nameof(other)); } this.target = other.target; this.data = other.data; }
public void CreateProcessingInstructionSimple() { Assert.Throws<ArgumentNullException>(() => new XProcessingInstruction(null, "abcd")); Assert.Throws<ArgumentNullException>(() => new XProcessingInstruction("abcd", null)); XProcessingInstruction c = new XProcessingInstruction("foo", "bar"); Assert.Equal("foo", c.Target); Assert.Equal("bar", c.Data); Assert.Null(c.Parent); }
// <?xml version="1.0"?> // <?order alpha ascending?> // <art xmlns='urn:art-org:art'> // <period name='Renaissance' xmlns:a='urn:art-org:artists'> // <a:artist>Leonardo da Vinci</a:artist> // <a:artist>Michelangelo</a:artist> // <a:artist><![CDATA[Donatello]]></a:artist> // </period> // <!-- insert period here --> // </art> public static XDocument CreateDocumentVerbose() { XNamespace nsArt = "urn:art-org:art"; XNamespace nsArtists = "urn:art-org:artists"; // create the document XDocument document = new XDocument(); // create the xml declaration and // set on the document document.Declaration = new XDeclaration("1.0", null, null); // create the art element and // add to the document XElement art = new XElement(nsArt + "art"); document.Add(art); // create the order processing instruction and // add before the art element XProcessingInstruction pi = new XProcessingInstruction("order", "alpha ascending"); art.AddBeforeSelf(pi); // create the period element and // add to the art element XElement period = new XElement(nsArt + "period"); art.Add(period); // add the name attribute to the period element period.SetAttributeValue("name", "Renaissance"); // create the namespace declaration xmlns:a and // add to the period element XAttribute nsdecl = new XAttribute(XNamespace.Xmlns + "a", nsArtists); period.Add(nsdecl); // create the artists elements and // the underlying text nodes period.SetElementValue(nsArtists + "artist", "Michelangelo"); XElement artist = new XElement(nsArtists + "artist", "Leonardo ", "da ", "Vinci"); period.AddFirst(artist); artist = new XElement(nsArtists + "artist"); period.Add(artist); XText cdata = new XText("Donatello"); artist.Add(cdata); // create the comment and // add to the art element XComment comment = new XComment("insert period here"); art.Add(comment); return document; }
public override IEnumerable< XNode > Process(XNode node) { XElement element = _AssumeElement( node ); Validation.RequireAttributes( element, AttrName.Name ); // PI name and value can have symbolic expansions or expressions string pi_name = _ProcessText( ( string ) element.Attribute( AttrName.Name ) ).GetTextValue(); string pi_val = _ProcessText( element.Value ).GetTextValue(); var pi = new XProcessingInstruction( pi_name, pi_val ); return new[] {pi}; }
public void CreateDocumentWithContent() { XDeclaration declaration = new XDeclaration("1.0", "utf-8", "yes"); XComment comment = new XComment("This is a document"); XProcessingInstruction instruction = new XProcessingInstruction("doc-target", "doc-data"); XElement element = new XElement("RootElement"); XDocument doc = new XDocument(declaration, comment, instruction, element); Assert.Equal(new XNode[] { comment, instruction, element }, doc.Nodes()); }
// <?xml version="1.0"?> // <?order alpha ascending?> // <art xmlns='urn:art-org:art'> // <period name='Renaissance' xmlns:a='urn:art-org:artists'> // <a:artist>Leonardo da Vinci</a:artist> // <a:artist>Michelangelo</a:artist> // <a:artist><![CDATA[Donatello]]></a:artist> // </period> // <!-- 在此处插入 period --> // </art> public static XDocument CreateDocumentVerbose() { XNamespace nsArt = "urn:art-org:art"; XNamespace nsArtists = "urn:art-org:artists"; // 创建文档 XDocument document = new XDocument(); // 创建 xml 声明,并在 // 文档中对其进行设置 document.Declaration = new XDeclaration("1.0", null, null); // 创建 art 元素,并将其 // 添加到文档中 XElement art = new XElement(nsArt + "art"); document.Add(art); // 创建顺序处理指令,并将其 // 添加到 art 元素之前 XProcessingInstruction pi = new XProcessingInstruction("order", "alpha ascending"); art.AddBeforeSelf(pi); // 创建 period 元素,并将其 // 添加到 art 元素中 XElement period = new XElement(nsArt + "period"); art.Add(period); // 向 period 元素中添加 name 特性 period.SetAttributeValue("name", "Renaissance"); // 创建命名空间声明 xmlns:a,并将其 // 添加到 period 元素中 XAttribute nsdecl = new XAttribute(XNamespace.Xmlns + "a", nsArtists); period.Add(nsdecl); // 创建 artist 元素和 // 基础文本节点 period.SetElementValue(nsArtists + "artist", "Michelangelo"); XElement artist = new XElement(nsArtists + "artist", "Leonardo ", "da ", "Vinci"); period.AddFirst(artist); artist = new XElement(nsArtists + "artist"); period.Add(artist); XText cdata = new XText("Donatello"); artist.Add(cdata); // 创建注释,并将其 // 添加到 art 元素中 XComment comment = new XComment("insert period here"); art.Add(comment); return document; }
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 static XDocument set_ProcessingInstruction(this XDocument xDocument, string target, string data) { var processingInstruntion = xDocument.processingInstruction(target); if (processingInstruntion.notNull()) processingInstruntion.Data = data; else { var newProcessingInstruction = new XProcessingInstruction(target, data);//"xsl-stylesheet", "type=\"text/xsl\" href=\"LogStyle.xsl\""); xDocument.AddFirst(newProcessingInstruction); } return xDocument; }
/// <summary> /// Validate behavior of the XDocument copy/clone constructor. /// </summary> /// <returns>true if pass, false if fail</returns> //[Variation(Desc = "CreateDocumentCopy")] public void CreateDocumentCopy() { try { new XDocument((XDocument)null); Validate.ExpectedThrow(typeof(ArgumentNullException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentNullException)); } XDeclaration declaration = new XDeclaration("1.0", "utf-8", "yes"); XComment comment = new XComment("This is a document"); XProcessingInstruction instruction = new XProcessingInstruction("doc-target", "doc-data"); XElement element = new XElement("RootElement"); XDocument doc = new XDocument(declaration, comment, instruction, element); XDocument doc2 = new XDocument(doc); IEnumerator e = doc2.Nodes().GetEnumerator(); // First node: declaration Validate.IsEqual(doc.Declaration.ToString(), doc2.Declaration.ToString()); // Next node: comment Validate.IsEqual(e.MoveNext(), true); Validate.Type(e.Current, typeof(XComment)); Validate.IsNotReferenceEqual(e.Current, comment); XComment comment2 = (XComment)e.Current; Validate.IsEqual(comment2.Value, comment.Value); // Next node: processing instruction Validate.IsEqual(e.MoveNext(), true); Validate.Type(e.Current, typeof(XProcessingInstruction)); Validate.IsNotReferenceEqual(e.Current, instruction); XProcessingInstruction instruction2 = (XProcessingInstruction)e.Current; Validate.String(instruction2.Target, instruction.Target); Validate.String(instruction2.Data, instruction.Data); // Next node: element. Validate.IsEqual(e.MoveNext(), true); Validate.Type(e.Current, typeof(XElement)); Validate.IsNotReferenceEqual(e.Current, element); XElement element2 = (XElement)e.Current; Validate.ElementName(element2, element.Name.ToString()); Validate.Count(element2.Nodes(), 0); // Should be end. Validate.IsEqual(e.MoveNext(), false); }
// equals => hashcode should be the same // - all "simple" node types // - text vs. CDATA // XDocument: // - Normal mode // - Concatenated text (Whitespace) nodes // - Diffs in XDecl // XElement: // - Normal mode // - same nodes, different order // - comments inside the texts // - same nodes, same order (positive) // // - Concatenated text nodes // - string content vs. text node/s // - empty string vs. empty text node // - Multiple text nodes but the same value // - adjacent text & CData // // - IsEmpty // - Attribute order // - Namespace declarations // - local vs. in-scope // - default redef. //[Variation(Priority = 0, Desc = "PI normal", Params = new object[] { "PI", "click", "PI", "click", true })] //[Variation(Priority = 0, Desc = "PI target=data", Params = new object[] { "PI", "PI", "PI", "PI", true })] //[Variation(Priority = 0, Desc = "PI data = ''", Params = new object[] { "PI", "", "PI", "", true })] //[Variation(Priority = 1, Desc = "PI data1!=data2", Params = new object[] { "PI", "click", "PI", "", false })] //[Variation(Priority = 2, Desc = "PI target1!=target2!", Params = new object[] { "AAAAP", "click", "AAAAQ", "click", false })] //[Variation(Priority = 2, Desc = "PI hashconflict I.", Params = new object[] { "AAAAP", "AAAAQ", "AAAAP", "AAAAQ", true })] //[Variation(Priority = 2, Desc = "PI data=target, not the same", Params = new object[] { "PA", "PA", "PI", "PI", false })] //[Variation(Priority = 2, Desc = "PI hashconflict II.", Params = new object[] { "AAAAP", "AAAAQ", "AAAAQ", "AAAAP", false })] public void PI() { bool expected = (bool)Variation.Params[4]; XProcessingInstruction p1 = new XProcessingInstruction(Variation.Params[0] as string, Variation.Params[1] as string); XProcessingInstruction p2 = new XProcessingInstruction(Variation.Params[2] as string, Variation.Params[3] as string); VerifyComparison(expected, p1, p2); XDocument doc = new XDocument(p1); XElement e2 = new XElement("p2p", p2); VerifyComparison(expected, p1, p2); }
//[Variation(Desc = "pi.Target = '' should not be allowed")] public void XPIEmptyStringShouldNotBeAllowed() { XProcessingInstruction pi = new XProcessingInstruction("PI", "data"); try { pi.Target = ""; } catch (ArgumentException) { return; } throw new TestException(TestResult.Failed, ""); }
//[Variation(Desc = "NodeTypes")] public void NodeTypes() { XDocument document = new XDocument(); XElement element = new XElement("x"); XText text = new XText("text-value"); XComment comment = new XComment("comment"); XProcessingInstruction processingInstruction = new XProcessingInstruction("target", "data"); Validate.IsEqual(document.NodeType, XmlNodeType.Document); Validate.IsEqual(element.NodeType, XmlNodeType.Element); Validate.IsEqual(text.NodeType, XmlNodeType.Text); Validate.IsEqual(comment.NodeType, XmlNodeType.Comment); Validate.IsEqual(processingInstruction.NodeType, XmlNodeType.ProcessingInstruction); }
public void NodeTypes() { XDocument document = new XDocument(); XElement element = new XElement("x"); XText text = new XText("text-value"); XComment comment = new XComment("comment"); XProcessingInstruction processingInstruction = new XProcessingInstruction("target", "data"); Assert.Equal(XmlNodeType.Document, document.NodeType); Assert.Equal(XmlNodeType.Element, element.NodeType); Assert.Equal(XmlNodeType.Text, text.NodeType); Assert.Equal(XmlNodeType.Comment, comment.NodeType); Assert.Equal(XmlNodeType.ProcessingInstruction, processingInstruction.NodeType); }
public void CreateWriter1() { string xml = "<root><foo/><bar></bar><baz a='v' xmlns='urn:foo' xmlns:x='urn:x'><x:ext xmlns=''>test</x:ext><!-- comment --> <?some-pi some-data?></baz></root>"; XDocument doc = new XDocument(); XmlWriter xw = doc.CreateWriter(); XmlReader xr = XmlReader.Create(new StringReader(xml)); while (!xr.EOF) { xw.WriteNode(xr, false); } xw.Close(); Assert.AreEqual("root", doc.Root.Name.LocalName, "#1"); XElement el = doc.Root.FirstNode as XElement; Assert.AreEqual("foo", el.Name.LocalName, "#2-1"); Assert.IsTrue(el.IsEmpty, "#2-2"); Assert.IsFalse(el.HasAttributes, "#2-3"); el = el.NextNode as XElement; Assert.IsFalse(el.IsEmpty, "#3"); el = el.NextNode as XElement; Assert.AreEqual("a", el.FirstAttribute.Name.LocalName, "#4-1"); Assert.AreEqual("xmlns", el.FirstAttribute.NextAttribute.Name.LocalName, "#4-2"); Assert.AreEqual("x", el.LastAttribute.Name.LocalName, "#4-3"); Assert.AreEqual(XNamespace.Xmlns, el.LastAttribute.Name.Namespace, "#4-4"); el = el.FirstNode as XElement; // <x:ext Assert.AreEqual("ext", el.Name.LocalName, "#5-1"); Assert.AreEqual(XNamespace.Get("urn:x"), el.Name.Namespace, "#5-2"); // xmlns='' Assert.AreEqual("xmlns", el.FirstAttribute.Name.LocalName, "#5-3"); Assert.AreEqual(XNamespace.Get(String.Empty), el.FirstAttribute.Name.Namespace, "#5-4"); XText t = el.FirstNode as XText; Assert.AreEqual("test", t.Value, "#6"); XComment c = el.NextNode as XComment; Assert.AreEqual(" comment ", c.Value, "#7"); t = c.NextNode as XText; Assert.AreEqual(" ", t.Value, "#8"); XPI pi = t.NextNode as XPI; Assert.AreEqual("some-pi", pi.Target, "#9-1"); Assert.AreEqual("some-data", pi.Data, "#9-2"); Assert.IsNull(el.Parent.NextNode, "#10"); Assert.IsNull(el.Parent.Parent.NextNode, "#11"); }
public void CreateDocumentCopy() { Assert.Throws<ArgumentNullException>(() => new XDocument((XDocument)null)); XDeclaration declaration = new XDeclaration("1.0", "utf-8", "yes"); XComment comment = new XComment("This is a document"); XProcessingInstruction instruction = new XProcessingInstruction("doc-target", "doc-data"); XElement element = new XElement("RootElement"); XDocument doc = new XDocument(declaration, comment, instruction, element); XDocument doc2 = new XDocument(doc); IEnumerator e = doc2.Nodes().GetEnumerator(); // First node: declaration Assert.Equal(doc.Declaration.ToString(), doc2.Declaration.ToString()); // Next node: comment Assert.True(e.MoveNext()); Assert.IsType<XComment>(e.Current); Assert.NotSame(comment, e.Current); XComment comment2 = (XComment)e.Current; Assert.Equal(comment.Value, comment2.Value); // Next node: processing instruction Assert.True(e.MoveNext()); Assert.IsType<XProcessingInstruction>(e.Current); Assert.NotSame(instruction, e.Current); XProcessingInstruction instruction2 = (XProcessingInstruction)e.Current; Assert.Equal(instruction.Target, instruction2.Target); Assert.Equal(instruction.Data, instruction2.Data); // Next node: element. Assert.True(e.MoveNext()); Assert.IsType<XElement>(e.Current); Assert.NotSame(element, e.Current); XElement element2 = (XElement)e.Current; Assert.Equal(element.Name.ToString(), element2.Name.ToString()); Assert.Empty(element2.Nodes()); // Should be end. Assert.False(e.MoveNext()); }
public void XProcessingInstructionPIVariation() { XProcessingInstruction toChange = new XProcessingInstruction("target", "data"); XProcessingInstruction original = new XProcessingInstruction(toChange); using (UndoManager undo = new UndoManager(toChange)) { undo.Group(); using (EventsHelper eHelper = new EventsHelper(toChange)) { toChange.Target = "newTarget"; Assert.True(toChange.Target.Equals("newTarget"), "Name did not change"); eHelper.Verify(XObjectChange.Name, toChange); } undo.Undo(); Assert.True(XNode.DeepEquals(toChange, original), "Undo did not work"); } }
public void ProcessingInstructionEquals() { XProcessingInstruction c1 = new XProcessingInstruction("targetx", "datax"); XProcessingInstruction c2 = new XProcessingInstruction("targetx", "datay"); XProcessingInstruction c3 = new XProcessingInstruction("targety", "datax"); XProcessingInstruction c4 = new XProcessingInstruction("targety", "datay"); XProcessingInstruction c5 = new XProcessingInstruction("targetx", "datax"); Assert.False(XNode.DeepEquals(c1, (XProcessingInstruction)null)); Assert.True(XNode.DeepEquals(c1, c1)); Assert.False(XNode.DeepEquals(c1, c2)); Assert.False(XNode.DeepEquals(c1, c3)); Assert.False(XNode.DeepEquals(c1, c4)); Assert.True(XNode.DeepEquals(c1, c5)); Assert.Equal(XNode.EqualityComparer.GetHashCode(c1), XNode.EqualityComparer.GetHashCode(c5)); }
public void ProcessingInstructionValues() { XProcessingInstruction c = new XProcessingInstruction("xxx", "yyy"); Assert.Equal("xxx", c.Target); Assert.Equal("yyy", c.Data); // Null values not allowed. Assert.Throws<ArgumentNullException>(() => c.Target = null); Assert.Throws<ArgumentNullException>(() => c.Data = null); // Try setting values. c.Target = "abcd"; Assert.Equal("abcd", c.Target); c.Data = "efgh"; Assert.Equal("efgh", c.Data); Assert.Equal("abcd", c.Target); }
//[Variation(Priority = 0, Desc = "XProcessingInstruction - Invalid Name")] public void InvalidPIVariation() { _runWithEvents = (bool)Params[0]; XProcessingInstruction toChange = new XProcessingInstruction("target", "data"); if (_runWithEvents) _eHelper = new EventsHelper(toChange); try { toChange.Target = null; } catch (Exception) { if (_runWithEvents) _eHelper.Verify(0); return; } try { toChange.Target = " "; } catch (Exception) { if (_runWithEvents) _eHelper.Verify(0); return; } try { toChange.Target = ""; } catch (Exception) { if (_runWithEvents) _eHelper.Verify(0); return; } throw new TestException(TestResult.Failed, ""); }
internal override bool DeepEquals(XNode node) { XProcessingInstruction instruction = node as XProcessingInstruction; return(((instruction != null) && (this.target == instruction.target)) && (this.data == instruction.data)); }
void ExpandPlugins(ViewContext viewContext, XProcessingInstruction[] pis, XElement head, List<XElement> scripts) { var typesUsed = new HashSet<Type>(); foreach (var pi in pis) { var plugin = getPlugin(pi.Target); plugin.Initialize(viewContext); var isFirstUse = !typesUsed.Contains(plugin.GetType()); var headContents = plugin.GetHeadContents(isFirstUse); typesUsed.Add(plugin.GetType()); head.Add(headContents); var content = plugin.Render(pi.Data).ToArray(); ExpandPlugins(viewContext, content.OfType<XProcessingInstruction>().ToArray(), head, scripts); ExpandPlugins(viewContext, content.OfType<XContainer>().DescendantNodes().OfType<XProcessingInstruction>().ToArray(), head, scripts); pi.ReplaceWith(content); scripts.AddRange(plugin.GetScripts(isFirstUse)); } }
//[Variation(Priority = 1, Desc = "XDocument - the same node instance, connected - sanity", Param = true)] //[Variation(Priority = 1, Desc = "XDocument - the same node instance - sanity", Param = false)] public void XDocumentTheSameReferenceSanity() { object[] paras = null; var connected = (bool)Variation.Param; XDocument doc1 = null; if (connected) { doc1 = new XDocument(new XElement("root", new XElement("A"), new XProcessingInstruction("PI", "data"))); paras = new object[] { doc1.Root.LastNode, doc1.Root.LastNode, doc1.Root.Element("A") }; } else { var e = new XElement("A"); var pi = new XProcessingInstruction("PI", "data"); paras = new object[] { pi, pi, e }; } var doc = new XDocument(paras); XNode firstPI = doc.FirstNode; XNode secondPI = firstPI.NextNode; XNode rootElem = secondPI.NextNode; TestLog.Compare(firstPI != null, "firstPI != null"); TestLog.Compare(firstPI.NodeType, XmlNodeType.ProcessingInstruction, "firstPI nodetype"); TestLog.Compare(firstPI is XProcessingInstruction, "firstPI is XPI"); TestLog.Compare(secondPI != null, "secondPI != null"); TestLog.Compare(secondPI.NodeType, XmlNodeType.ProcessingInstruction, "secondPI nodetype"); TestLog.Compare(secondPI is XProcessingInstruction, "secondPI is XPI"); TestLog.Compare(rootElem != null, "rootElem != null"); TestLog.Compare(rootElem.NodeType, XmlNodeType.Element, "rootElem nodetype"); TestLog.Compare(rootElem is XElement, "rootElem is XElement"); TestLog.Compare(rootElem.NextNode == null, "rootElem NextNode"); TestLog.Compare(firstPI != secondPI, "firstPI != secondPI"); TestLog.Compare(XNode.DeepEquals(firstPI, secondPI), "XNode.DeepEquals(firstPI,secondPI)"); foreach (object o in paras) { var e = o as XNode; if (connected) { TestLog.Compare(e.Parent, doc1.Root, "Orig Parent"); TestLog.Compare(e.Document, doc1, "Orig Document"); } else { TestLog.Compare(e.Parent == null, "Orig Parent not connected"); TestLog.Compare(e.Document, doc, "Orig Document not connected"); } } }
//[Variation(Priority = 0, Desc = "XDocument - adding element cloned", Param = "XElement")] //[Variation(Priority = 1, Desc = "XDocument - adding PI cloned", Param = "XPI")] //[Variation(Priority = 1, Desc = "XDocument - adding XmlDecl cloned", Param = "XmlDecl")] //[Variation (Priority=1, Desc="XDocument - adding dot type")] //[Variation(Priority = 1, Desc = "XDocument - adding Comment cloned", Param = "XComment")] //[Variation(Priority = 1, Desc = "XDocument - combination off allowed types in correct order, cloned", Param = "Mix1")] //[Variation(Priority = 1, Desc = "XDocument - combination off allowed types in correct order, without decl; cloned", Param = "Mix2")] //[Variation(Priority = 2, Desc = "XDocument - adding string/whitespace", Param = "Whitespace")] public void XDocumentAddParamsCloning() { object[] paras = null; var paramType = Variation.Param as string; var doc1 = new XDocument(); switch (paramType) { case "XElement": var xe = new XElement("root"); doc1.Add(xe); paras = new object[] { xe }; break; case "XPI": var pi = new XProcessingInstruction("Click", "data"); doc1.Add(pi); paras = new object[] { pi }; break; case "XComment": var comm = new XComment("comment"); doc1.Add(comm); paras = new object[] { comm }; break; case "Whitespace": var txt = new XText(" "); doc1.Add(txt); paras = new object[] { txt }; break; case "Mix1": { var a2 = new XComment("comment"); doc1.Add(a2); var a3 = new XProcessingInstruction("Click", "data"); doc1.Add(a3); var a4 = new XElement("root"); doc1.Add(a4); var a5 = new XProcessingInstruction("Click2", "data2"); doc1.Add(a5); paras = new object[] { a2, a3, a4, a5 }; } break; case "Mix2": { var a2 = new XComment("comment"); doc1.Add(a2); var a3 = new XProcessingInstruction("Click", "data"); doc1.Add(a3); var a4 = new XElement("root"); doc1.Add(a4); var a5 = new XProcessingInstruction("Click2", "data2"); doc1.Add(a5); paras = new object[] { a2, a3, a4, a5 }; } break; default: TestLog.Compare(false, "Test case: Wrong param"); break; } var doc = new XDocument(paras); TestLog.Compare(doc != null, "doc!=null"); TestLog.Compare(doc.Document == doc, "doc.Document property"); int counter = 0; for (XNode node = doc.FirstNode; node.NextNode != null; node = node.NextNode) { TestLog.Compare(node != null, "node != null"); var orig = paras[counter] as XNode; TestLog.Compare(node != orig, "Not the same instance, cloned"); TestLog.Compare(orig.Document, doc1, "Orig Document"); TestLog.Compare(XNode.DeepEquals(node, orig), "node equals param"); TestLog.Compare(node.Document, doc, "Document property"); counter++; } }
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()); } }
/// <summary> /// Compares two processing instructions using the indicated comparison options. /// </summary> /// <param name="p1">The first processing instruction to compare.</param> /// <param name="p2">The second processing instruction to compare.</param> /// <param name="options">The options to use in the comparison.</param> /// <returns>true if the processing instructions are equal, false otherwise.</returns> public static bool DeepEquals(this XProcessingInstruction p1, XProcessingInstruction p2, ComparisonOptions options) { if ((p1 ?? p2) == null) return true; if ((p1 == null) || (p2 == null)) return false; // They are not both null, so if either is, then the other isn't return ((p1.Target == p2.Target) && (p1.Data == p1.Data)); }
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); }
public XProcessingInstructionWrapper(XProcessingInstruction processingInstruction) : base(processingInstruction) { }
/// <summary> /// Invoked for each <see cref="XProcessingInstruction"/> /// </summary> /// <param name="processingInstruction"></param> public virtual void Visit(XProcessingInstruction processingInstruction) { Contract.Requires<ArgumentNullException>(processingInstruction != null); }
public void NameNull() { XPI pi = new XPI(null, String.Empty); }
public void NodeNoParentAddRemove() { // Not allowed if parent is null. int i = 0; while (true) { XNode node = null; switch (i++) { case 0: node = new XElement("x"); break; case 1: node = new XComment("c"); break; case 2: node = new XText("abc"); break; case 3: node = new XProcessingInstruction("target", "data"); break; default: i = -1; break; } if (i < 0) { break; } Assert.Throws<InvalidOperationException>(() => node.AddBeforeSelf("foo")); Assert.Throws<InvalidOperationException>(() => node.AddAfterSelf("foo")); Assert.Throws<InvalidOperationException>(() => node.Remove()); } }
public void XProcessingInstructionChangeValue() { XProcessingInstruction toChange = new XProcessingInstruction("target", "Original Value"); String newValue = "New Value"; XElement xElem = new XElement("root", toChange); XElement xElemOriginal = new XElement(xElem); using (UndoManager undo = new UndoManager(xElem)) { undo.Group(); using (EventsHelper eHelper = new EventsHelper(xElem)) { using (EventsHelper piHelper = new EventsHelper(toChange)) { toChange.Data = newValue; Assert.True(toChange.Data.Equals(newValue), "Value did not change"); xElem.Verify(); piHelper.Verify(XObjectChange.Value, toChange); } eHelper.Verify(XObjectChange.Value, toChange); } undo.Undo(); Assert.True(XNode.DeepEquals(xElem, xElemOriginal), "Undo did not work!"); } }
/// <summary> /// Tests the AddAfterSelf/AddBeforeSelf/Remove method on Node, /// when there's no parent. /// </summary> /// <param name="context"></param> /// <returns></returns> //[Variation(Desc = "NodeNoParentAddRemove")] public void NodeNoParentAddRemove() { // Not allowed if parent is null. int i = 0; while (true) { XNode node = null; switch (i++) { case 0: node = new XElement("x"); break; case 1: node = new XComment("c"); break; case 2: node = new XText("abc"); break; case 3: node = new XProcessingInstruction("target", "data"); break; default: i = -1; break; } if (i < 0) { break; } try { node.AddBeforeSelf("foo"); Validate.ExpectedThrow(typeof(InvalidOperationException)); } catch (Exception ex) { Validate.Catch(ex, typeof(InvalidOperationException)); } try { node.AddAfterSelf("foo"); Validate.ExpectedThrow(typeof(InvalidOperationException)); } catch (Exception ex) { Validate.Catch(ex, typeof(InvalidOperationException)); } try { node.Remove(); Validate.ExpectedThrow(typeof(InvalidOperationException)); } catch (Exception ex) { Validate.Catch(ex, typeof(InvalidOperationException)); } } }
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()); }
internal override bool DeepEquals(XNode node) { XProcessingInstruction other = node as XProcessingInstruction; return(other != null && target == other.target && data == other.data); }
public XProcessingInstruction(XProcessingInstruction other) { Contract.Requires(other != null); }
public void DataNull() { XPI pi = new XPI("mytarget", null); }