diff --git a/XmlAbstraction.Test/XmlObjectUnitTest.cs b/XmlAbstraction.Test/XmlObjectUnitTest.cs index 164ba85..149fb3b 100644 --- a/XmlAbstraction.Test/XmlObjectUnitTest.cs +++ b/XmlAbstraction.Test/XmlObjectUnitTest.cs @@ -31,50 +31,14 @@ public void TestClassReopenFile() $"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}test.xml"); fstrm.Write(Encoding.UTF8.GetBytes(testXml), 0, testXml.Length); fstrm.Dispose(); - xmlObj.Dispose(); xmlObj = new XmlObject( $"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}test.xml", testXml); NoThrows(() => xmlObj.ReopenFile()); - xmlObj.Dispose(); File.Delete( $"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}test.xml"); } - [Fact] - public void TestClassDoubleDispose() - { - var testXml = @" - -"; - var xmlObj = new XmlObject(testXml); - xmlObj.Dispose(); - xmlObj.Dispose(); - } - - [Fact] - public void TestClassDisposedExceptions() - { - var testXml = @" - -"; - var xmlObj = new XmlObject(testXml); - xmlObj.Dispose(); - - // test to make sure that ObjectDisposedException is thrown. - Assert.ThrowsAny(() => xmlObj.AddAttribute("test4", "test", "test")); - Assert.ThrowsAny(() => xmlObj.Write("test", "test")); - Assert.ThrowsAny(() => xmlObj.Write("test2", "test", "test")); - Assert.ThrowsAny(() => xmlObj.Write("test3", "test31", new string[] { "test1", "test2", "test3" })); - Assert.ThrowsAny(() => xmlObj.Read("test")); - Assert.ThrowsAny(() => xmlObj.Read("test2", "test")); - Assert.ThrowsAny(() => xmlObj.Read("test3", "test31", null)); - Assert.ThrowsAny(() => xmlObj.Delete("test")); - Assert.ThrowsAny(() => xmlObj.Delete("test2", "test")); - Assert.ThrowsAny(() => xmlObj.ReopenFile()); - Assert.ThrowsAny(() => xmlObj.Save()); - } - [Fact] public void TestClassEdits() { @@ -96,15 +60,12 @@ public void TestClassEdits() Assert.ThrowsAny(() => xmlObj.Delete("test")); Assert.ThrowsAny(() => xmlObj.Delete("test2", "test")); Assert.ThrowsAny(() => xmlObj.ReopenFile()); - xmlObj.Dispose(); xmlObj = new XmlObject(testXmlNoRoot); // reopen data from a file. - xmlObj.Dispose(); var fstrm = File.Create( $"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}test.xml"); fstrm.Write(Encoding.UTF8.GetBytes(testXml), 0, testXml.Length); fstrm.Dispose(); - xmlObj.Dispose(); xmlObj = new XmlObject( $"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}test.xml", testXml); @@ -124,15 +85,11 @@ public void TestClassEdits() NoThrows(() => xmlObj.Delete("test")); Assert.ThrowsAny(() => xmlObj.Delete("test2", "test")); NoThrows(() => xmlObj.Save()); - xmlObj.Dispose(); File.Delete( $"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}test.xml"); xmlObj = new XmlObject($"{Path.DirectorySeparatorChar}test.xml", testXml, true); - xmlObj.Dispose(); xmlObj = new XmlObject($"{Path.DirectorySeparatorChar}test.xml", testXml, true); - xmlObj.Dispose(); xmlObj = new XmlObject($"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}test.xml", testXml, true); - xmlObj.Dispose(); } } } diff --git a/XmlAbstraction/XmlAbstraction.csproj b/XmlAbstraction/XmlAbstraction.csproj index 2f31472..f41c1e2 100644 --- a/XmlAbstraction/XmlAbstraction.csproj +++ b/XmlAbstraction/XmlAbstraction.csproj @@ -8,7 +8,7 @@ true AraHaan - 1.0.1 + 1.1.0 A library that contains a System.Xml and System.Xml.Linq abstraction class. Copyright 2018 XML @@ -16,7 +16,7 @@ https://github.com/AraHaan/XmlAbstraction/ https://github.com/AraHaan/XmlAbstraction/ https://github.com/AraHaan/XmlAbstraction/blob/master/LICENSE - A bit of changes and implemented TODOs and some fixes as well. + A breaking change removing IDisposable from XmlObject. true diff --git a/XmlAbstraction/XmlObject.cs b/XmlAbstraction/XmlObject.cs index 2913f5f..2500b31 100644 --- a/XmlAbstraction/XmlObject.cs +++ b/XmlAbstraction/XmlObject.cs @@ -17,7 +17,7 @@ namespace XmlAbstraction // Only the Save() method should do direct edits to the XDocument object of the class named "Doc". // The rest should just use the dictionaries for the changes to be applied to the xml in the Save() // method if the xml is not read-only. I did this to support read only memory access of xml. - public class XmlObject : IDisposable + public class XmlObject { /// /// Initializes a new instance of the class @@ -113,11 +113,6 @@ public XmlObject(string xmlfilename, string fallbackxmlcontent, bool saveToCurre this.Doc = (fileSize > 0) ? XDocument.Load(xmlfilename) : XDocument.Parse(fallbackxmlcontent); } - /// - /// Gets a value indicating whether the is disposed. - /// - public bool IsDisposed { get; private set; } = false; - private object ObjLock { get; set; } private XDocument Doc { get; set; } @@ -181,15 +176,9 @@ private bool HasChangedExternally /// but only if it has changed. If the file was not saved it /// will be saved first. /// - /// is disposed. /// Cannot reopen on read-only instances. public void ReopenFile() { - if (this.IsDisposed) - { - throw new ObjectDisposedException(nameof(XmlObject)); - } - if (!this.CachedXmlfilename.Equals(":memory")) { this.Save(); @@ -202,7 +191,7 @@ public void ReopenFile() } /// - /// Adds or edits an attribute in an Element and sets it's value in the XMLObject. + /// Adds or edits an attribute in an Element and sets it's value in the . /// /// This method can also remove the attribute by setting the value to null. /// @@ -210,7 +199,6 @@ public void ReopenFile() /// empty value as well as making the attribute as if the Element was /// pre-added before calling this function. /// - /// is disposed. /// Attribute already exists in the xml file. /// When called from a read-only instance. /// The name of the element to add a attribute to. @@ -218,11 +206,6 @@ public void ReopenFile() /// The value of the attribute. public void AddAttribute(string elementname, string attributename, object attributevalue) { - if (this.IsDisposed) - { - throw new ObjectDisposedException(nameof(XmlObject)); - } - if (!this.CachedXmlfilename.Equals(":memory")) { var elem = this.Doc.Root.Element(elementname); @@ -331,17 +314,11 @@ public void AddAttribute(string elementname, string attributename, object attrib /// /// If Element does not exist yet it will be created automatically. /// - /// is disposed. /// When called from a read-only instance. /// The name of the element to write to or create. /// The value for the element. public void Write(string elementname, string value) { - if (this.IsDisposed) - { - throw new ObjectDisposedException(nameof(XmlObject)); - } - if (!this.CachedXmlfilename.Equals(":memory")) { var elem = this.Doc.Root.Element(elementname); @@ -396,7 +373,6 @@ public void Write(string elementname, string value) /// /// If Element does not exist yet it will be created automatically. /// - /// is disposed. /// Attribute already exists in the xml file. /// When called from a read-only instance. /// @@ -407,11 +383,6 @@ public void Write(string elementname, string value) /// The value of the attribute to use. public void Write(string elementname, string attributename, string attributevalue) { - if (this.IsDisposed) - { - throw new ObjectDisposedException(nameof(XmlObject)); - } - if (!this.CachedXmlfilename.Equals(":memory")) { this.AddAttribute(elementname, attributename, attributevalue); @@ -428,18 +399,12 @@ public void Write(string elementname, string attributename, string attributevalu /// /// If Elements do not exist yet they will be created automatically. /// - /// is disposed. /// When called from a read-only instance. /// parrent element name of the subelement. /// The name to use when writing subelement(s). /// The array of values to use for the subelement(s). public void Write(string parentelementname, string elementname, string[] values) { - if (this.IsDisposed) - { - throw new ObjectDisposedException(nameof(XmlObject)); - } - if (!this.CachedXmlfilename.Equals(":memory")) { var elem = this.Doc.Root.Element(parentelementname); @@ -490,17 +455,11 @@ public void Write(string parentelementname, string elementname, string[] values) /// /// If Element does not exist yet it will be created automatically with an empty value. /// - /// is disposed. /// When the Element does not exist in a read-only instance. /// The element name to read the value from. /// The value of the input element or . public string Read(string elementname) { - if (this.IsDisposed) - { - throw new ObjectDisposedException(nameof(XmlObject)); - } - var elem = this.Doc.Root.Element(elementname); if (elem != null || this.ElementsAdded.ContainsKey(elementname) @@ -528,18 +487,12 @@ public string Read(string elementname) /// If Element and the attribute does not exist yet it will be created automatically /// with an empty value. /// - /// is disposed. /// When the Element does not exist in a read-only instance. /// The element name to get the value of a attribute. /// The name of the attribute to get the value of. /// The value of the input element or . public string Read(string elementname, string attributename) { - if (this.IsDisposed) - { - throw new ObjectDisposedException(nameof(XmlObject)); - } - var elem = this.Doc.Root.Element(elementname); if (elem == null) { @@ -583,7 +536,6 @@ public string Read(string elementname, string attributename) /// If Parent Element does not exist yet it will be created automatically /// with an empty value. In that case an empty string array is returned. /// - /// is disposed. /// When the Element does not exist in a read-only instance. /// The name of the parrent element of the subelement(s). /// The name of the subelements to get their values. @@ -596,11 +548,6 @@ public string Read(string elementname, string attributename) /// public string[] Read(string parentelementname, string elementname, object unused = null) { - if (this.IsDisposed) - { - throw new ObjectDisposedException(nameof(XmlObject)); - } - var elem = this.Doc.Descendants(parentelementname); var strarray = new string[] { }; foreach (var element in elem) @@ -627,17 +574,11 @@ public string[] Read(string parentelementname, string elementname, object unused /// Deletes an xml element using the element name. /// Can also delete not only the parrent element but also subelements with it. /// - /// is disposed. /// elementname does not exist in the xml or in pending edits. /// When the object is a read-only instance. /// The element name of the element to delete. public void Delete(string elementname) { - if (this.IsDisposed) - { - throw new ObjectDisposedException(nameof(XmlObject)); - } - if (!this.CachedXmlfilename.Equals(":memory")) { var elem = this.Doc.Root.Element(elementname); @@ -667,18 +608,12 @@ public void Delete(string elementname) /// /// Removes an xml attribute using the element name and the name of the attribute. /// - /// is disposed. /// elementname or attributename does not exist in the xml or in pending edits. /// When the object is a read-only instance. /// The element name that has the attribute to delete. /// The name of the attribute to delete. public void Delete(string elementname, string attributename) { - if (this.IsDisposed) - { - throw new ObjectDisposedException(nameof(XmlObject)); - } - if (!this.CachedXmlfilename.Equals(":memory")) { var elem = this.Doc.Root.Element(elementname); @@ -731,7 +666,6 @@ public void Delete(string elementname, string attributename) /// /// Saves the underlying XML file if it changed. /// - /// is disposed. public void Save() { // do not save in memory xml. It should be read only. @@ -739,11 +673,6 @@ public void Save() { lock (this.ObjLock) { - if (this.IsDisposed) - { - throw new ObjectDisposedException(nameof(XmlObject)); - } - if (this.HasChangedExternally && this.Exists) { // reopen file to apply changes at runtime to it. @@ -831,11 +760,6 @@ public void Save() } } - /// - /// Disposes of the and saves the underlying XML file if it changed. - /// - public void Dispose() => this.Dispose(true); - // Summary: // Adds an Element to the XmlObject but verifies it does not exist first. // @@ -898,25 +822,6 @@ private void SaveAddedSubelements(XElement xElement, XmlElementData elemdata) } } - private void Dispose(bool disposing) - { - if (!this.IsDisposed) - { - this.Save(); - this.Exists = false; - this.HasChanged = false; - - // remove everything from the Lists/Dictonaries then destroy them. - this.ElementsAdded = null; - this.ElementsEdits = null; - this.ElementAttributesDeleted = null; - this.ElementsDeleted = null; - this.Doc = null; - this.CachedXmlfilename = string.Empty; - this.IsDisposed = true; - } - } - private class XmlAttributeData { internal string AttributeName { get; set; } = string.Empty; diff --git a/appveyor.yml b/appveyor.yml index b23f941..cc1fcb6 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,4 +1,4 @@ -version: 1.0.1.0-alpha{build} +version: 1.1.0.0-alpha{build} branches: only: - master