From a4672fb1d24a8054603e98d22f4dcdbf9de0e2bd Mon Sep 17 00:00:00 2001 From: Arnold Beiland Date: Mon, 27 Jan 2020 12:25:06 +0200 Subject: [PATCH] Allow attribute with customized namespace prefix to be set on XliffElement --- Xliff.OM.Tests/Core/XliffElementTests.cs | 40 +++++++++++++++++++ Xliff.OM.Tests/Serialization/TestData.cs | 2 + .../Serialization/XliffReaderTests.cs | 30 +++++++++++++- .../DocumentWithTwoNamespaces.xlf | 5 +++ .../UnitWithAttributeHavingCustomNsPrefix.xlf | 6 +++ Xliff.OM.Tests/Xliff.OM.Tests.csproj | 6 +++ Xliff.OM/Core/XliffElement.cs | 9 +++-- 7 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 Xliff.OM.Tests/TestData.Xliff.OM.Tests/DocumentWithTwoNamespaces.xlf create mode 100644 Xliff.OM.Tests/TestData.Xliff.OM.Tests/UnitWithAttributeHavingCustomNsPrefix.xlf diff --git a/Xliff.OM.Tests/Core/XliffElementTests.cs b/Xliff.OM.Tests/Core/XliffElementTests.cs index 76bfa19..fa8957a 100644 --- a/Xliff.OM.Tests/Core/XliffElementTests.cs +++ b/Xliff.OM.Tests/Core/XliffElementTests.cs @@ -142,6 +142,19 @@ public int Property get { return (int)this.GetPropertyValue("Property"); } set { this.SetPropertyValue(value, "Property"); } } + + /// + /// Gets or sets a value that is used to verify attribute matching with namespaces + /// + [Converter(typeof(HexConverter))] + [DefaultValue(100)] + [SchemaEntity("ns1", "namespace1", "PropertyFromNamespace", Requirement.Optional)] + public int PropertyFromNamespace + { + get { return (int)this.GetPropertyValue("PropertyFromNamespace"); } + set { this.SetPropertyValue(value, "PropertyFromNamespace"); } + } + #endregion Properties /// @@ -830,6 +843,33 @@ public void XliffElement_SelectorPath_Target() Assert.AreEqual("#/f=f1/g=g1/u=u1", span.SelectorPath, "SelectorPath is incorrect."); } + /// + /// Tests wether attribute value can be set regardless of differences in namespace prefix naming + /// + [TestMethod()] + [TestCategory(TestUtilities.UnitTestCategory)] + public void XliffElement_TrySetAttributeValue() + { + TestXliffElement element = new TestXliffElement(); + const string propertyName = "PropertyFromNamespace"; + + Console.WriteLine("Test with default namespace prefix."); + var defaultNameInfo = new XmlNameInfo("ns1", "namespace1", "PropertyFromNamespace"); + var result1 = ((IXliffDataConsumer) element).TrySetAttributeValue(defaultNameInfo, "A"); + Assert.AreEqual(SetAttributeResult.Success, result1); + Assert.AreEqual(10, element.PropertyFromNamespace); + + Console.WriteLine("Test with customized namespace prefix."); + var customizedXmlNameInfo = new XmlNameInfo("ns2", "namespace1", "PropertyFromNamespace"); + var result2 = ((IXliffDataConsumer)element).TrySetAttributeValue(customizedXmlNameInfo, "B"); + Assert.AreEqual(SetAttributeResult.Success, result2); + Assert.AreEqual(11, element.PropertyFromNamespace); + + Console.WriteLine("Test with another namespace"); + var anotherXmlNameInfo = new XmlNameInfo("ns3", "namespace3", "PropertyFromNamespace"); + var result3 = ((IXliffDataConsumer)element).TrySetAttributeValue(anotherXmlNameInfo, "C"); + Assert.AreEqual(SetAttributeResult.PossibleExtension, result3); + } #endregion Test Methods } diff --git a/Xliff.OM.Tests/Serialization/TestData.cs b/Xliff.OM.Tests/Serialization/TestData.cs index 5b137c4..031ddc6 100644 --- a/Xliff.OM.Tests/Serialization/TestData.cs +++ b/Xliff.OM.Tests/Serialization/TestData.cs @@ -14,6 +14,7 @@ public enum TestData DocumentWithValidValues, DocumentWithExtensions, DocumentWithXmlPrefix, + DocumentWithTwoNamespaces, EmptyFile, FileNoteWithDefaultValues, FileNoteWithEmptyValues, @@ -68,6 +69,7 @@ public enum TestData UnitWithEmptyValues, UnitWithMetadata, UnitWithValidValues, + UnitWithAttributeHavingCustomNsPrefix, ValidDocument } } diff --git a/Xliff.OM.Tests/Serialization/XliffReaderTests.cs b/Xliff.OM.Tests/Serialization/XliffReaderTests.cs index 60d9070..3aac663 100644 --- a/Xliff.OM.Tests/Serialization/XliffReaderTests.cs +++ b/Xliff.OM.Tests/Serialization/XliffReaderTests.cs @@ -1,4 +1,7 @@ -namespace Localization.Xliff.OM.Serialization.Tests +using System.Linq; +using Localization.Xliff.OM.Extensibility; + +namespace Localization.Xliff.OM.Serialization.Tests { using System; using System.Collections.Generic; @@ -173,6 +176,24 @@ public void XliffReader_DocumentWithXmlPrefix() Assert.AreEqual("text", ((PlainText)segment.Source.Text[0]).Text, "Text[0].Text is incorrect."); } + /// + /// Tests a document can have attributes from different namespaces on the same element. + /// + [TestMethod()] + [TestCategory(TestUtilities.UnitTestCategory)] + public void XliffReader_DocumentWithTwoNamespaces() + { + Deserialize(TestData.DocumentWithTwoNamespaces); + + Assert.AreEqual("2.0", _document.Version); + + var itsVersionAttribute = ((IExtensible) _document).Extensions.Single().GetAttributes().Single(); + Assert.AreEqual("version", itsVersionAttribute.LocalName); + Assert.AreEqual("its", itsVersionAttribute.Prefix); + Assert.AreEqual("http://www.w3.org/2005/11/its", itsVersionAttribute.Namespace); + Assert.AreEqual("3.0", itsVersionAttribute.Value); + } + /// /// Tests that a deserializes correctly. /// @@ -1020,6 +1041,13 @@ public void XliffReader_Unit() "TargetDirectionality is incorrect."); Assert.IsTrue(unit.Translate, "Translate is incorrect."); Assert.AreEqual("type", unit.Type, "Type is incorrect."); + + Console.WriteLine("Test for attribute with custom namespace prefix"); + this.Deserialize(TestData.UnitWithAttributeHavingCustomNsPrefix); + unit = this._document.Files[0].Containers[0] as Unit; + Assert.IsNotNull(unit, "Unit is null."); + Assert.AreEqual(String.Empty, unit.Id, "Id is incorrect."); + Assert.AreEqual("100", unit.StorageRestriction, "StorageRestriction is incorrect"); } /// diff --git a/Xliff.OM.Tests/TestData.Xliff.OM.Tests/DocumentWithTwoNamespaces.xlf b/Xliff.OM.Tests/TestData.Xliff.OM.Tests/DocumentWithTwoNamespaces.xlf new file mode 100644 index 0000000..0d999e2 --- /dev/null +++ b/Xliff.OM.Tests/TestData.Xliff.OM.Tests/DocumentWithTwoNamespaces.xlf @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/Xliff.OM.Tests/TestData.Xliff.OM.Tests/UnitWithAttributeHavingCustomNsPrefix.xlf b/Xliff.OM.Tests/TestData.Xliff.OM.Tests/UnitWithAttributeHavingCustomNsPrefix.xlf new file mode 100644 index 0000000..b4cfe49 --- /dev/null +++ b/Xliff.OM.Tests/TestData.Xliff.OM.Tests/UnitWithAttributeHavingCustomNsPrefix.xlf @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Xliff.OM.Tests/Xliff.OM.Tests.csproj b/Xliff.OM.Tests/Xliff.OM.Tests.csproj index 38f13e8..a29991c 100644 --- a/Xliff.OM.Tests/Xliff.OM.Tests.csproj +++ b/Xliff.OM.Tests/Xliff.OM.Tests.csproj @@ -978,6 +978,9 @@ PreserveNewest + + PreserveNewest + PreserveNewest @@ -1046,6 +1049,9 @@ PreserveNewest + + PreserveNewest + PreserveNewest diff --git a/Xliff.OM/Core/XliffElement.cs b/Xliff.OM/Core/XliffElement.cs index 6e81f68..90ee9f2 100644 --- a/Xliff.OM/Core/XliffElement.cs +++ b/Xliff.OM/Core/XliffElement.cs @@ -637,7 +637,7 @@ internal bool GetPropertyValue(string property, bool asString, bool inheritedVal if (key.IsInstanceOfType(ancestor)) { string newProperty; - + // Get a different property Name if specified. newProperty = inheritanceMap[key] ?? property; @@ -958,9 +958,12 @@ protected virtual bool TrySetPropertyValue(XmlNameInfo name, string value) foreach (AttributeData attribute in this.attributes.Values) { if (attribute.IsSupported && - (name.Prefix == attribute.Prefix) && + (name.Prefix != NamespacePrefixes.XmlNamespace) && (name.LocalName == attribute.LocalName) && - (attribute.IgnoreNamespace || (name.Namespace == attribute.Namespace))) + ( + name.Namespace == attribute.Namespace || + attribute.IgnoreNamespace && name.Prefix == NamespacePrefixes.Xml + )) { attribute.SetValue(value); result = true;