From 0a514755f3a1fa1eac7d01130b4fe779d8dcb9e8 Mon Sep 17 00:00:00 2001 From: Nathan Cooke Date: Mon, 23 Jul 2018 14:43:04 -0500 Subject: [PATCH 1/8] Consolidating duplicate code --- .../Documents/JsonDocumentTests.cs | 4 +- .../Documents/XmlDocumentTests.cs | 4 +- .../Documents/YamlDocumentTests.cs | 4 +- src/MagicChunks/Documents/JsonDocument.cs | 37 ++++++------------- src/MagicChunks/Documents/XmlDocument.cs | 20 ++-------- src/MagicChunks/Documents/YamlDocument.cs | 20 ++-------- 6 files changed, 26 insertions(+), 63 deletions(-) diff --git a/src/MagicChunks.Tests/Documents/JsonDocumentTests.cs b/src/MagicChunks.Tests/Documents/JsonDocumentTests.cs index 1c5dceb..334187f 100644 --- a/src/MagicChunks.Tests/Documents/JsonDocumentTests.cs +++ b/src/MagicChunks.Tests/Documents/JsonDocumentTests.cs @@ -498,7 +498,7 @@ public void ValidateEmptyPath() ArgumentException result = Assert.Throws(() => document.ReplaceKey(new[] { "a", "", "b" }, "")); // Arrange - Assert.True(result.Message?.StartsWith("There is empty items in the path.")); + Assert.True(result.Message?.StartsWith("There is at least one empty segment in the path.")); } [Fact] @@ -511,7 +511,7 @@ public void ValidateWithespacePath() ArgumentException result = Assert.Throws(() => document.ReplaceKey(new[] { "a", " ", "b" }, "")); // Arrange - Assert.True(result.Message?.StartsWith("There is empty items in the path.")); + Assert.True(result.Message?.StartsWith("There is at least one empty segment in the path.")); } } } \ No newline at end of file diff --git a/src/MagicChunks.Tests/Documents/XmlDocumentTests.cs b/src/MagicChunks.Tests/Documents/XmlDocumentTests.cs index 860a3b0..4ff51f3 100644 --- a/src/MagicChunks.Tests/Documents/XmlDocumentTests.cs +++ b/src/MagicChunks.Tests/Documents/XmlDocumentTests.cs @@ -616,7 +616,7 @@ public void ValidateEmptyPath() ArgumentException result = Assert.Throws(() => document.ReplaceKey(new[] { "a", "", "b" }, "")); // Assert - Assert.True(result.Message?.StartsWith("There is empty items in the path.")); + Assert.True(result.Message?.StartsWith("There is at least one empty segment in the path.")); } [Fact] @@ -629,7 +629,7 @@ public void ValidateWhiteSpacePath() ArgumentException result = Assert.Throws(() => document.ReplaceKey(new[] { "a", " ", "b" }, "")); // Assert - Assert.True(result.Message?.StartsWith("There is empty items in the path.")); + Assert.True(result.Message?.StartsWith("There is at least one empty segment in the path.")); } [Fact] diff --git a/src/MagicChunks.Tests/Documents/YamlDocumentTests.cs b/src/MagicChunks.Tests/Documents/YamlDocumentTests.cs index 79c776c..3232c03 100644 --- a/src/MagicChunks.Tests/Documents/YamlDocumentTests.cs +++ b/src/MagicChunks.Tests/Documents/YamlDocumentTests.cs @@ -117,7 +117,7 @@ public void ValidateEmptyPath() ArgumentException result = Assert.Throws(() => document.ReplaceKey(new[] { "a", "", "b" }, "")); // Arrange - Assert.True(result.Message?.StartsWith("There is empty items in the path.")); + Assert.True(result.Message?.StartsWith("There is at least one empty segment in the path.")); } [Fact] @@ -130,7 +130,7 @@ public void ValidateWithespacePath() ArgumentException result = Assert.Throws(() => document.ReplaceKey(new[] { "a", " ", "b" }, "")); // Arrange - Assert.True(result.Message?.StartsWith("There is empty items in the path.")); + Assert.True(result.Message?.StartsWith("There is at least one empty segment in the path.")); } } } \ No newline at end of file diff --git a/src/MagicChunks/Documents/JsonDocument.cs b/src/MagicChunks/Documents/JsonDocument.cs index ea3815d..b498fa4 100644 --- a/src/MagicChunks/Documents/JsonDocument.cs +++ b/src/MagicChunks/Documents/JsonDocument.cs @@ -9,7 +9,7 @@ namespace MagicChunks.Documents { - public class JsonDocument : IDocument + public class JsonDocument : Document, IDocument { private static readonly Regex JsonObjectRegex = new Regex(@"^{.+}$$", RegexOptions.CultureInvariant | RegexOptions.Compiled); private static readonly Regex NodeIndexEndingRegex = new Regex(@"\[\d+\]$", RegexOptions.CultureInvariant | RegexOptions.Compiled); @@ -30,14 +30,7 @@ public JsonDocument(string source) public void AddElementToArray(string[] path, string value) { - if ((path == null) || (path.Any() == false)) - throw new ArgumentException("Path is not speicified.", nameof(path)); - - if (path.Any(String.IsNullOrWhiteSpace)) - throw new ArgumentException("There is empty items in the path.", nameof(path)); - - if (Document.Root == null) - throw new ArgumentException("Root element is not present.", nameof(path)); + ValidatePath(path); var targets = FindPath(path.Take(path.Length - 1), (JObject)Document.Root); var pathEnding = path.Last(); @@ -50,14 +43,7 @@ public void AddElementToArray(string[] path, string value) public void ReplaceKey(string[] path, string value) { - if ((path == null) || (path.Any() == false)) - throw new ArgumentException("Path is not speicified.", nameof(path)); - - if (path.Any(String.IsNullOrWhiteSpace)) - throw new ArgumentException("There is empty items in the path.", nameof(path)); - - if (Document.Root == null) - throw new ArgumentException("Root element is not present.", nameof(path)); + ValidatePath(path); var targets = FindPath(path.Take(path.Length - 1), (JObject)Document.Root); var pathEnding = path.Last(); @@ -70,14 +56,7 @@ public void ReplaceKey(string[] path, string value) public void RemoveKey(string[] path) { - if ((path == null) || (path.Any() == false)) - throw new ArgumentException("Path is not speicified.", nameof(path)); - - if (path.Any(String.IsNullOrWhiteSpace)) - throw new ArgumentException("There is empty items in the path.", nameof(path)); - - if (Document.Root == null) - throw new ArgumentException("Root element is not present.", nameof(path)); + ValidatePath(path); var targets = FindPath(path.Take(path.Length - 1), (JObject)Document.Root); var pathEnding = path.Last(); @@ -185,5 +164,13 @@ public override string ToString() public void Dispose() { } + + protected override void ValidatePath(string[] path) + { + base.ValidatePath(path); + + if (Document.Root == null) + throw new ArgumentException("Root element is not present.", nameof(path)); + } } } \ No newline at end of file diff --git a/src/MagicChunks/Documents/XmlDocument.cs b/src/MagicChunks/Documents/XmlDocument.cs index 705d057..e147044 100644 --- a/src/MagicChunks/Documents/XmlDocument.cs +++ b/src/MagicChunks/Documents/XmlDocument.cs @@ -9,7 +9,7 @@ namespace MagicChunks.Documents { - public class XmlDocument : IDocument + public class XmlDocument : Document, IDocument { private static readonly Regex AttributeFilterRegex = new Regex(@"(?.+?)\[\s*\@(?[\w\:]+)\s*\=\s*[\'\""]?(?.+?)[\'\""]?\s*\]$", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.Singleline); private static readonly Regex ProcessingInstructionsPathElementRegex = new Regex(@"^\?.+", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.Singleline); @@ -32,11 +32,7 @@ public XmlDocument(string source) public void AddElementToArray(string[] path, string value) { - if ((path == null) || (path.Any() == false)) - throw new ArgumentException("Path is not speicified.", nameof(path)); - - if (path.Any(String.IsNullOrWhiteSpace)) - throw new ArgumentException("There is empty items in the path.", nameof(path)); + ValidatePath(path); XElement current = Document.Root; string documentNamespace = Document.Root?.Name.NamespaceName ?? String.Empty; @@ -60,11 +56,7 @@ public void AddElementToArray(string[] path, string value) public void ReplaceKey(string[] path, string value) { - if ((path == null) || (path.Any() == false)) - throw new ArgumentException("Path is not speicified.", nameof(path)); - - if (path.Any(String.IsNullOrWhiteSpace)) - throw new ArgumentException("There is empty items in the path.", nameof(path)); + ValidatePath(path); XElement current = Document.Root; string documentNamespace = Document.Root?.Name.NamespaceName ?? String.Empty; @@ -104,11 +96,7 @@ public void ReplaceKey(string[] path, string value) public void RemoveKey(string[] path) { - if ((path == null) || (path.Any() == false)) - throw new ArgumentException("Path is not specified.", nameof(path)); - - if (path.Any(String.IsNullOrWhiteSpace)) - throw new ArgumentException("There is empty items in the path.", nameof(path)); + ValidatePath(path); XElement current = Document.Root; string documentNamespace = Document.Root?.Name.NamespaceName ?? String.Empty; diff --git a/src/MagicChunks/Documents/YamlDocument.cs b/src/MagicChunks/Documents/YamlDocument.cs index e46172b..7cbea2e 100644 --- a/src/MagicChunks/Documents/YamlDocument.cs +++ b/src/MagicChunks/Documents/YamlDocument.cs @@ -9,7 +9,7 @@ namespace MagicChunks.Documents { - public class YamlDocument : IDocument + public class YamlDocument : Document, IDocument { protected readonly Dictionary Document; @@ -35,11 +35,7 @@ public YamlDocument(string source) public void AddElementToArray(string[] path, string value) { - if ((path == null) || (path.Any() == false)) - throw new ArgumentException("Path is not speicified.", nameof(path)); - - if (path.Any(String.IsNullOrWhiteSpace)) - throw new ArgumentException("There is empty items in the path.", nameof(path)); + ValidatePath(path); Dictionary current = Document; @@ -53,11 +49,7 @@ public void AddElementToArray(string[] path, string value) public void ReplaceKey(string[] path, string value) { - if ((path == null) || (path.Any() == false)) - throw new ArgumentException("Path is not speicified.", nameof(path)); - - if (path.Any(String.IsNullOrWhiteSpace)) - throw new ArgumentException("There is empty items in the path.", nameof(path)); + ValidatePath(path); Dictionary current = Document; @@ -71,11 +63,7 @@ public void ReplaceKey(string[] path, string value) public void RemoveKey(string[] path) { - if ((path == null) || (path.Any() == false)) - throw new ArgumentException("Path is not speicified.", nameof(path)); - - if (path.Any(String.IsNullOrWhiteSpace)) - throw new ArgumentException("There is empty items in the path.", nameof(path)); + ValidatePath(path); Dictionary current = Document; From 4c666b43674f7320f279e152b9801791af8f3c54 Mon Sep 17 00:00:00 2001 From: Nathan Cooke Date: Mon, 23 Jul 2018 14:47:23 -0500 Subject: [PATCH 2/8] Adding missing file --- src/MagicChunks/Core/Document.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/MagicChunks/Core/Document.cs diff --git a/src/MagicChunks/Core/Document.cs b/src/MagicChunks/Core/Document.cs new file mode 100644 index 0000000..3b871ff --- /dev/null +++ b/src/MagicChunks/Core/Document.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace MagicChunks.Core +{ + public class Document + { + protected virtual void ValidatePath(string[] path) + { + if ((path == null) || (path.Any() == false)) + throw new ArgumentException("Path is not speicified.", nameof(path)); + + if (path.Any(String.IsNullOrWhiteSpace)) + throw new ArgumentException("There is at least one empty segment in the path.", nameof(path)); + } + } +} From 63f4b98fa770d6416d8e84f5e15dc3994836fdce Mon Sep 17 00:00:00 2001 From: Nathan Cooke Date: Mon, 23 Jul 2018 14:48:36 -0500 Subject: [PATCH 3/8] simplifying logic --- src/MagicChunks/Core/Document.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MagicChunks/Core/Document.cs b/src/MagicChunks/Core/Document.cs index 3b871ff..a16f91a 100644 --- a/src/MagicChunks/Core/Document.cs +++ b/src/MagicChunks/Core/Document.cs @@ -9,7 +9,7 @@ public class Document { protected virtual void ValidatePath(string[] path) { - if ((path == null) || (path.Any() == false)) + if (path == null || !path.Any()) throw new ArgumentException("Path is not speicified.", nameof(path)); if (path.Any(String.IsNullOrWhiteSpace)) From e67934cdf87ff77909643b4ee48941cf3390c8c0 Mon Sep 17 00:00:00 2001 From: Nathan Cooke Date: Mon, 23 Jul 2018 15:34:23 -0500 Subject: [PATCH 4/8] removing more duplicated code --- src/MagicChunks/Core/Document.cs | 2 +- src/MagicChunks/Documents/JsonDocument.cs | 35 +++++++++++------------ 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/src/MagicChunks/Core/Document.cs b/src/MagicChunks/Core/Document.cs index a16f91a..2baff8c 100644 --- a/src/MagicChunks/Core/Document.cs +++ b/src/MagicChunks/Core/Document.cs @@ -7,7 +7,7 @@ namespace MagicChunks.Core { public class Document { - protected virtual void ValidatePath(string[] path) + protected static void ValidatePath(string[] path) { if (path == null || !path.Any()) throw new ArgumentException("Path is not speicified.", nameof(path)); diff --git a/src/MagicChunks/Documents/JsonDocument.cs b/src/MagicChunks/Documents/JsonDocument.cs index b498fa4..2974207 100644 --- a/src/MagicChunks/Documents/JsonDocument.cs +++ b/src/MagicChunks/Documents/JsonDocument.cs @@ -21,6 +21,9 @@ public JsonDocument(string source) try { Document = (JObject)JsonConvert.DeserializeObject(source); + + if (Document.Root == null) + throw new ArgumentException("Root element is not present.", nameof(source)); } catch (JsonReaderException ex) { @@ -30,10 +33,7 @@ public JsonDocument(string source) public void AddElementToArray(string[] path, string value) { - ValidatePath(path); - - var targets = FindPath(path.Take(path.Length - 1), (JObject)Document.Root); - var pathEnding = path.Last(); + (var targets, var pathEnding) = Process(path); foreach (var target in targets) { @@ -43,10 +43,7 @@ public void AddElementToArray(string[] path, string value) public void ReplaceKey(string[] path, string value) { - ValidatePath(path); - - var targets = FindPath(path.Take(path.Length - 1), (JObject)Document.Root); - var pathEnding = path.Last(); + (var targets, var pathEnding) = Process(path); foreach (var target in targets) { @@ -56,10 +53,7 @@ public void ReplaceKey(string[] path, string value) public void RemoveKey(string[] path) { - ValidatePath(path); - - var targets = FindPath(path.Take(path.Length - 1), (JObject)Document.Root); - var pathEnding = path.Last(); + (var targets, var pathEnding) = Process(path); if (NodeIndexEndingRegex.IsMatch(pathEnding) || NodeValueEndingRegex.IsMatch(pathEnding)) { @@ -82,6 +76,16 @@ public void RemoveKey(string[] path) } } + private (IEnumerable, string) Process(string[] path) + { + ValidatePath(path); + + var targets = FindPath(path.Take(path.Length - 1), (JObject)Document.Root); + var pathEnding = path.Last(); + + return (targets, pathEnding); + } + private static IEnumerable FindPath(IEnumerable path, JObject current) { var pathElements = new Queue(path); @@ -165,12 +169,5 @@ public void Dispose() { } - protected override void ValidatePath(string[] path) - { - base.ValidatePath(path); - - if (Document.Root == null) - throw new ArgumentException("Root element is not present.", nameof(path)); - } } } \ No newline at end of file From 12723c8c024107431085e9df6e93e882cb9b598d Mon Sep 17 00:00:00 2001 From: Nathan Cooke Date: Mon, 23 Jul 2018 15:56:07 -0500 Subject: [PATCH 5/8] moved root element error to constructor --- .../Documents/YamlDocumentTests.cs | 18 ++++++++++++++++-- src/MagicChunks/Documents/YamlDocument.cs | 12 ++++-------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/MagicChunks.Tests/Documents/YamlDocumentTests.cs b/src/MagicChunks.Tests/Documents/YamlDocumentTests.cs index 3232c03..5fd73fb 100644 --- a/src/MagicChunks.Tests/Documents/YamlDocumentTests.cs +++ b/src/MagicChunks.Tests/Documents/YamlDocumentTests.cs @@ -111,7 +111,10 @@ public void Remove() public void ValidateEmptyPath() { // Assert - YamlDocument document = new YamlDocument(""); + var document = new YamlDocument(@"a: + x: 1 +b: 2 +c: 3"); // Act ArgumentException result = Assert.Throws(() => document.ReplaceKey(new[] { "a", "", "b" }, "")); @@ -124,7 +127,10 @@ public void ValidateEmptyPath() public void ValidateWithespacePath() { // Assert - YamlDocument document = new YamlDocument(""); + var document = new YamlDocument(@"a: + x: 1 +b: 2 +c: 3"); // Act ArgumentException result = Assert.Throws(() => document.ReplaceKey(new[] { "a", " ", "b" }, "")); @@ -132,5 +138,13 @@ public void ValidateWithespacePath() // Arrange Assert.True(result.Message?.StartsWith("There is at least one empty segment in the path.")); } + + [Fact] + public void ConstructorThrowsErrorIfDocumentHasNoRoot() + { + ArgumentException result = Assert.Throws(() => new YamlDocument("")); + + Assert.True(result.Message?.StartsWith("Root element is not present.")); + } } } \ No newline at end of file diff --git a/src/MagicChunks/Documents/YamlDocument.cs b/src/MagicChunks/Documents/YamlDocument.cs index 7cbea2e..c48c1cb 100644 --- a/src/MagicChunks/Documents/YamlDocument.cs +++ b/src/MagicChunks/Documents/YamlDocument.cs @@ -25,6 +25,10 @@ public YamlDocument(string source) try { Document = (Dictionary)deserializer.Deserialize(reader); + + if (Document == null) + throw new ArgumentException("Root element is not present.", nameof(source)); + } catch (YamlException ex) { @@ -39,9 +43,6 @@ public void AddElementToArray(string[] path, string value) Dictionary current = Document; - if (current == null) - throw new ArgumentException("Root element is not present.", nameof(path)); - current = FindPath(path.Take(path.Length - 1), current); UpdateTargetArrayElement(current, path.Last(), value); @@ -53,8 +54,6 @@ public void ReplaceKey(string[] path, string value) Dictionary current = Document; - if (current == null) - throw new ArgumentException("Root element is not present.", nameof(path)); current = FindPath(path.Take(path.Length - 1), current); @@ -67,9 +66,6 @@ public void RemoveKey(string[] path) Dictionary current = Document; - if (current == null) - throw new ArgumentException("Root element is not present.", nameof(path)); - current = FindPath(path.Take(path.Length - 1), current); current.Remove(path.Last()); } From 522b83f29925e621f499aaae69cd848c1c6538b6 Mon Sep 17 00:00:00 2001 From: Nathan Cooke Date: Mon, 23 Jul 2018 16:11:00 -0500 Subject: [PATCH 6/8] Consolodating duplicated code in YamlDocument --- src/MagicChunks/Documents/YamlDocument.cs | 29 +++++++---------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/src/MagicChunks/Documents/YamlDocument.cs b/src/MagicChunks/Documents/YamlDocument.cs index c48c1cb..6d24513 100644 --- a/src/MagicChunks/Documents/YamlDocument.cs +++ b/src/MagicChunks/Documents/YamlDocument.cs @@ -39,35 +39,24 @@ public YamlDocument(string source) public void AddElementToArray(string[] path, string value) { - ValidatePath(path); - - Dictionary current = Document; - - current = FindPath(path.Take(path.Length - 1), current); - - UpdateTargetArrayElement(current, path.Last(), value); + UpdateTargetArrayElement(Process(path), path.Last(), value); } public void ReplaceKey(string[] path, string value) { - ValidatePath(path); - - Dictionary current = Document; - - - current = FindPath(path.Take(path.Length - 1), current); - - UpdateTargetElement(current, path.Last(), value); + UpdateTargetElement(Process(path), path.Last(), value); } public void RemoveKey(string[] path) { - ValidatePath(path); - - Dictionary current = Document; + Process(path).Remove(path.Last()); + } - current = FindPath(path.Take(path.Length - 1), current); - current.Remove(path.Last()); + private Dictionary Process(string[] path) + { + ValidatePath(path); + var current = FindPath(path.Take(path.Length - 1), Document); + return current; } private static Dictionary FindPath(IEnumerable path, Dictionary current) From 7ba0c1d6d905939736e5026a553eae1c8d382728 Mon Sep 17 00:00:00 2001 From: Nathan Cooke Date: Mon, 23 Jul 2018 16:50:27 -0500 Subject: [PATCH 7/8] Removing duplicated code in XmlDocument. --- src/MagicChunks/Documents/XmlDocument.cs | 68 ++++++++++-------------- 1 file changed, 28 insertions(+), 40 deletions(-) diff --git a/src/MagicChunks/Documents/XmlDocument.cs b/src/MagicChunks/Documents/XmlDocument.cs index e147044..5ac158b 100644 --- a/src/MagicChunks/Documents/XmlDocument.cs +++ b/src/MagicChunks/Documents/XmlDocument.cs @@ -32,20 +32,10 @@ public XmlDocument(string source) public void AddElementToArray(string[] path, string value) { - ValidatePath(path); - - XElement current = Document.Root; - string documentNamespace = Document.Root?.Name.NamespaceName ?? String.Empty; - - if (current == null) - throw new ArgumentException("Root element is not present.", nameof(path)); - - if (String.Compare(current.Name.LocalName, path.First(), StringComparison.OrdinalIgnoreCase) != 0) - throw new ArgumentException("Root element name does not match path.", nameof(path)); - - if (!path.Any(p => ProcessingInstructionsPathElementRegex.IsMatch(p))) + (var match, var documentNamespace) = ProcessAndMatch(path); + if (!match) { - current = FindPath(path.Skip(1).Take(path.Length - 2), current, documentNamespace) as XElement; + var current = FindPath(path.Skip(1).Take(path.Length - 2), Document.Root, documentNamespace) as XElement; UpdateTargetArrayElement(value, path.Last(), current, documentNamespace); } else @@ -56,20 +46,11 @@ public void AddElementToArray(string[] path, string value) public void ReplaceKey(string[] path, string value) { - ValidatePath(path); + (var match, var documentNamespace) = ProcessAndMatch(path); - XElement current = Document.Root; - string documentNamespace = Document.Root?.Name.NamespaceName ?? String.Empty; - - if (current == null) - throw new ArgumentException("Root element is not present.", nameof(path)); - - if (String.Compare(current.Name.LocalName, path.First(), StringComparison.OrdinalIgnoreCase) != 0) - throw new ArgumentException("Root element name does not match path.", nameof(path)); - - if (!path.Any(p => ProcessingInstructionsPathElementRegex.IsMatch(p))) + if (!match) { - current = FindPath(path.Skip(1).Take(path.Length - 2), current, documentNamespace) as XElement; + var current = FindPath(path.Skip(1).Take(path.Length - 2), Document.Root, documentNamespace) as XElement; UpdateTargetElement(value, path.Last(), current, documentNamespace); } else @@ -89,39 +70,46 @@ public void ReplaceKey(string[] path, string value) throw new ArgumentException("To update processing instruction you should point attribute name.", nameof(path)); } - var processingInstruction = FindPath(path.Skip(1).Take(path.Length - 2), current, documentNamespace) as XProcessingInstruction; + var processingInstruction = FindPath(path.Skip(1).Take(path.Length - 2), Document.Root, documentNamespace) as XProcessingInstruction; UpdateProcessingInstruction(value, path.Last().TrimStart('@'), processingInstruction, documentNamespace); } } public void RemoveKey(string[] path) { - ValidatePath(path); + (var match, var documentNamespace) = ProcessAndMatch(path); - XElement current = Document.Root; - string documentNamespace = Document.Root?.Name.NamespaceName ?? String.Empty; - - if (current == null) - throw new ArgumentException("Root element is not present.", nameof(path)); - - if (String.Compare(current.Name.LocalName, path.First(), StringComparison.OrdinalIgnoreCase) != 0) - throw new ArgumentException("Root element name does not match path.", nameof(path)); - - if (!path.Any(p => ProcessingInstructionsPathElementRegex.IsMatch(p))) + if (!match) { - current = FindPath(path.Skip(1).Take(path.Length - 2), current, documentNamespace) as XElement; + var current = FindPath(path.Skip(1).Take(path.Length - 2), Document.Root, documentNamespace) as XElement; RemoveTargetElement(path.Last(), current, documentNamespace); } else { - var processingInstruction = FindPath(path.Skip(1).Take(path.Length - 2), current, documentNamespace) as XProcessingInstruction; + var processingInstruction = FindPath(path.Skip(1).Take(path.Length - 2), Document.Root, documentNamespace) as XProcessingInstruction; // Remove whole processing instruction (not just single attribute) if (processingInstruction == null) - processingInstruction = FindPath(path.Skip(1).Take(path.Length - 1), current, documentNamespace) as XProcessingInstruction; + processingInstruction = FindPath(path.Skip(1).Take(path.Length - 1), Document.Root, documentNamespace) as XProcessingInstruction; RemoveProcessingInstruction(path.Last(), processingInstruction, documentNamespace); } + + } + private (bool, string) ProcessAndMatch(string[] path) + { + ValidatePath(path); + + XElement current = Document.Root; + string documentNamespace = Document.Root?.Name.NamespaceName ?? String.Empty; + + if (current == null) + throw new ArgumentException("Root element is not present.", nameof(path)); + + if (String.Compare(current.Name.LocalName, path.First(), StringComparison.OrdinalIgnoreCase) != 0) + throw new ArgumentException("Root element name does not match path.", nameof(path)); + + return (path.Any(p => ProcessingInstructionsPathElementRegex.IsMatch(p)), documentNamespace); } private static XNode FindPath(IEnumerable path, XElement current, string documentNamespace) From c589a97ed137e3e1215903adc0d778b1c31db182 Mon Sep 17 00:00:00 2001 From: Nathan Cooke Date: Tue, 24 Jul 2018 08:37:52 -0500 Subject: [PATCH 8/8] resolving parameter order issue --- src/MagicChunks/Core/Transformer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MagicChunks/Core/Transformer.cs b/src/MagicChunks/Core/Transformer.cs index b9d6db8..49462e1 100644 --- a/src/MagicChunks/Core/Transformer.cs +++ b/src/MagicChunks/Core/Transformer.cs @@ -79,7 +79,7 @@ public string Transform(IDocument source, TransformationCollection transformatio foreach (var key in transformations.KeysToRemove) { if (String.IsNullOrWhiteSpace(key)) - throw new ArgumentNullException("Transformation key is empty.", nameof(transformations)); + throw new ArgumentNullException(nameof(transformations), "Transformation key is empty."); source.RemoveKey(SplitKey(key)); }