public void TestInitialize()
        {
            XliffReaderSettings settings;

            this._document = new XliffDocument();

            settings = new XliffReaderSettings();
            settings.Validators.Clear();
            this._reader = new XliffReader(settings);
        }
예제 #2
0
        public void TestInitialize()
        {
            XliffWriterSettings settings;

            settings = new XliffWriterSettings();
            settings.Indent = true;
            settings.IndentChars = "    ";
            settings.Validators.Clear();

            this._document = new XliffDocument();
            this._stream = new System.IO.MemoryStream();
            this._writer = new XliffWriter(settings);
        }
예제 #3
0
        /// <summary>
        /// Demonstrates how to disable validation when writing an XLIFF document.
        /// </summary>
        /// <param name="document">The document to write.</param>
        /// <param name="file">The path to the document to write.</param>
        public static void DisableValidationOnWrite(XliffDocument document, string file)
        {
            using (IO.FileStream stream = new IO.FileStream(file, IO.FileMode.Create, IO.FileAccess.Write))
            {
                XliffWriter writer;
                XliffWriterSettings settings;

                settings = new XliffWriterSettings();
                settings.Validators.Clear();

                writer = new XliffWriter(settings);
                writer.Serialize(stream, document);
            }
        }
        /// <summary>
        /// Stores the <see cref="XliffElement"/> as a child of this <see cref="XliffElement"/>.
        /// </summary>
        /// <param name="child">The child to add.</param>
        /// <returns>True if the child was stored, otherwise false.</returns>
        protected override bool StoreChild(ElementInfo child)
        {
            bool result;

            result = true;
            if (child.Element is XliffDocument)
            {
                Utilities.ThrowIfPropertyNotNull(this, PropertyNames.Document, this.Document);
                this.Document = (XliffDocument)child.Element;
            }
            else
            {
                result = base.StoreChild(child);
            }

            return result;
        }
        public void ParentAttachedList_Add()
        {
            XliffDocument item;

            //
            // Test with null.
            //

            try
            {
                this._list.Add(null);
                Assert.Fail("Expected ArgumentNullException to be thrown.");
            }
            catch (ArgumentNullException)
            {
            }

            //
            // Test with valid item1.
            //

            item = new XliffDocument();
            this._list.Add(item);
            Assert.IsNotNull(item.Parent, "Parent was not set.");
            Assert.AreEqual(ParentAttachedListTests._parent, item.Parent, "Parent was not set correctly.");

            //
            // Test with parent already set.
            //

            Assert.IsNotNull(item.Parent, "Parent should be set.");

            try
            {
                this._list.Add(item);
                Assert.Fail("Expected ElementReuseException to be thrown.");
            }
            catch (ElementReuseException)
            {
            }
        }
예제 #6
0
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        /// <param name="args">Command line arguments passed to the application.</param>
        public static void Main(string[] args)
        {
            string path;

            path = IO.Path.GetTempFileName();
            try
            {
                XliffDocument document;
                Segment segment;
                Unit unit;

                document = new XliffDocument("en-us");
                document.Files.Add(new File("f1"));
                unit = new Unit("u1");
                document.Files[0].Containers.Add(unit);
                segment = new Segment("s1");
                segment.Source = new Source();
                segment.Source.Text.Add(new PlainText("text"));
                unit.Resources.Add(segment);

                SampleCode.BlankDocument();
                SampleCode.DisableValidationOnWrite(document, path);
                SampleCode.ReadDocument(path);
                SampleCode.StoreCustomExtension();
                SampleCode.StoreGenericExtension();
                SampleCode.StoreGlossary();
                SampleCode.StoreMatches();
                SampleCode.StoreMetadata();
                SampleCode.ViewValidations(new XliffDocument("en-us"), path);
                SampleCode.WriteDocument(document, path);
                SampleCode.WhiteSpaces();
            }
            finally
            {
                IO.File.Delete(path);
            }
        }
예제 #7
0
        /// <summary>
        /// Serializes an <see cref="XliffDocument"/> to a stream.
        /// </summary>
        /// <param name="stream">The stream to write to.</param>
        /// <param name="document">The document to write.</param>
        public void Serialize(Stream stream, XliffDocument document)
        {
            XmlWriterSettings settings;

            ArgValidator.Create(stream, "stream").IsNotNull();
            ArgValidator.Create(document, "document").IsNotNull();

            settings = new XmlWriterSettings();
            settings.Indent = this.settings.Indent;
            if (settings.Indent && (this.settings.IndentChars != null))
            {
                settings.IndentChars = this.settings.IndentChars;
            }

            using (XmlWriter writer = XmlWriter.Create(stream, settings))
            {
                this.Serialize(writer, document);
            }
        }
예제 #8
0
        /// <summary>
        /// Demonstrates how to create a blank XLIFF document.
        /// </summary>
        public static void BlankDocument()
        {
            XliffDocument document;

            document = new XliffDocument("en-us");
        }
        public void ParentAttachedList_Clear()
        {
            XliffDocument item;

            //
            // Test with empty list.
            //

            Assert.AreEqual(0, this._list.Count, "List is not empty.");
            this._list.Clear();
            Assert.AreEqual(0, this._list.Count, "List is not empty.");

            //
            // Test with valid item1.
            //

            item = new XliffDocument();
            this._list.Add(item);
            Assert.IsNotNull(item.Parent, "Parent was not set.");
            this._list.Clear();
            Assert.AreEqual(0, this._list.Count, "List is not empty.");
            Assert.IsNull(item.Parent, "Parent was not reset.");
        }
        public void ParentAttachedList_Remove()
        {
            XliffDocument item1;
            XliffDocument item2;

            //
            // Test with null.
            //

            try
            {
                this._list.Remove(null);
                Assert.Fail("Expected ArgumentNullException to be thrown.");
            }
            catch (ArgumentNullException)
            {
            }

            //
            // Test with empty list.
            //

            Assert.IsFalse(this._list.Remove(new XliffDocument()), "Remove returned incorrect result.");

            //
            // Test with invalid item1.
            //

            item1 = new XliffDocument();
            this._list.Add(item1);
            Assert.IsFalse(this._list.Remove(new XliffDocument()), "Remove returned incorrect result.");
            Assert.AreEqual(1, this._list.Count, "Count is incorrect.");

            //
            // Test with valid item1.
            //

            item2 = new XliffDocument();
            this._list.Add(item2);
            Assert.IsTrue(this._list.Remove(item1), "Remove returned incorrect result.");
            Assert.AreEqual(1, this._list.Count, "Count is incorrect.");
            Assert.AreSame(this._list[0], item2, "List was incorrectly modified.");
            Assert.IsNull(item1.Parent, "Parent was not reset.");
            Assert.IsNotNull(item2.Parent, "Parent was reset incorrectly.");
        }
 public void TestInitialize()
 {
     this._document = new XliffDocument();
 }
예제 #12
0
        public void Extensibility_WithInvalidElementPrefix()
        {
            XliffDocument document;
            IExtensible extensible;
            IExtension extension;
            Segment segment;
            Unit unit;

            document = new XliffDocument("en-us");

            document.Files.Add(new File("f1"));
            extensible = document.Files[0];

            // Unit information.
            unit = new Unit("u1");
            document.Files[0].Containers.Add(unit);
            extensible = unit;

            // Segment information.
            segment = new Segment("s1");
            segment.Source = new Source();
            segment.State = TranslationState.Initial;
            unit.Resources.Add(segment);

            Console.WriteLine("Test with null prefix.");
            try
            {
                extensible.Extensions.Clear();
                extension = new GenericExtension("extension");
                extensible.Extensions.Add(extension);
                extension.AddAttribute(new TestAttribute(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, "attribute1", "attribute 1"));
                extension.AddChild(new ElementInfo(new XmlNameInfo(ExtensibilityTests.Namespace1, "name"), new Source()));

                TestUtilities.GetDocumentContents(document, "    ");
                Assert.Fail("Expected InvalidXmlSpecifierException to be thrown.");
            }
            catch (InvalidXmlSpecifierException e)
            {
                Assert.IsInstanceOfType(e.InnerException, typeof(ArgumentNullException), "Exception is incorrect.");
            }

            Console.WriteLine("Test with invalid prefix.");
            try
            {
                extensible.Extensions.Clear();
                extension = new GenericExtension("extension");
                extensible.Extensions.Add(extension);
                extension.AddAttribute(new TestAttribute(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, "attribute1", "attribute 1"));
                extension.AddChild(new ElementInfo(new XmlNameInfo("a:b", ExtensibilityTests.Namespace1, "name"), new Source()));

                TestUtilities.GetDocumentContents(document, "    ");
                Assert.Fail("Expected InvalidXmlSpecifierException to be thrown.");
            }
            catch (InvalidXmlSpecifierException e)
            {
                Assert.IsInstanceOfType(e.InnerException, typeof(XmlException), "Exception is incorrect.");
            }

            Console.WriteLine("Test with null namespace.");
            try
            {
                extensible.Extensions.Clear();
                extension = new GenericExtension("extension");
                extensible.Extensions.Add(extension);
                extension.AddAttribute(new TestAttribute(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, "attribute1", "attribute 1"));
                extension.AddChild(new ElementInfo(new XmlNameInfo(ExtensibilityTests.Prefix1, null, "name"), new Source()));

                TestUtilities.GetDocumentContents(document, "    ");
                Assert.Fail("Expected InvalidXmlSpecifierException to be thrown.");
            }
            catch (InvalidXmlSpecifierException e)
            {
                Assert.IsInstanceOfType(e.InnerException, typeof(ArgumentNullException), "Exception is incorrect.");
            }

            Console.WriteLine("Test with null local name.");
            try
            {
                extensible.Extensions.Clear();
                extension = new GenericExtension("extension");
                extensible.Extensions.Add(extension);
                extension.AddAttribute(new TestAttribute(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, "attribute1", "attribute 1"));
                extension.AddChild(new ElementInfo(new XmlNameInfo(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, null), new Source()));

                TestUtilities.GetDocumentContents(document, "    ");
                Assert.Fail("Expected InvalidXmlSpecifierException to be thrown.");
            }
            catch (InvalidXmlSpecifierException e)
            {
                Assert.IsInstanceOfType(e.InnerException, typeof(ArgumentNullException), "Exception is incorrect.");
            }

            Console.WriteLine("Test with differing namespace.");
            try
            {
                extensible.Extensions.Clear();
                extension = new GenericExtension("extension");
                extensible.Extensions.Add(extension);
                extension.AddAttribute(new TestAttribute(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, "attribute1", "attribute 1"));
                extension.AddChild(new ElementInfo(new XmlNameInfo(ExtensibilityTests.Prefix1, "namespace", "name"), new Source()));

                TestUtilities.GetDocumentContents(document, "    ");
                Assert.Fail("Expected InvalidXmlSpecifierException to be thrown.");
            }
            catch (InvalidOperationException)
            {
            }
        }
예제 #13
0
        public void Extensibility_SerializeWithRegisteredExtensions()
        {
            GenericElement child1;
            GenericElement child2;
            XliffDocument document;
            IExtensible extensible;
            IExtension extension;
            Segment segment;
            Source source;
            Unit unit;

            // Document extensions.
            document = new XliffDocument("en-us");
            extensible = document;
            extension = new GenericExtension("extension");
            extensible.Extensions.Add(extension);
            extension.AddAttribute(new GenericExtensionAttribute(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, "attribute1", "attribute 1"));
            extension.AddAttribute(new GenericExtensionAttribute(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, "attribute2", "attribute 2"));
            extension = new GenericExtension("extension");
            extensible.Extensions.Add(extension);
            extension.AddAttribute(new GenericExtensionAttribute(ExtensibilityTests.Prefix2, ExtensibilityTests.Namespace2, "attribute1", "attribute 3"));
            extension.AddAttribute(new GenericExtensionAttribute(ExtensibilityTests.Prefix2, ExtensibilityTests.Namespace2, "attribute2", "attribute 4"));

            // File extensions.
            document.Files.Add(new File("f1"));
            extensible = document.Files[0];
            extension = new GenericExtension("extension");
            extensible.Extensions.Add(extension);
            extension.AddAttribute(new GenericExtensionAttribute(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, "attribute1", "attribute 5"));
            extension.AddAttribute(new GenericExtensionAttribute(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, "attribute2", "attribute 6"));
            extension = new GenericExtension("extension");
            extensible.Extensions.Add(extension);
            extension.AddAttribute(new GenericExtensionAttribute(ExtensibilityTests.Prefix2, ExtensibilityTests.Namespace2, "attribute1", "attribute 7"));
            extension.AddAttribute(new GenericExtensionAttribute(ExtensibilityTests.Prefix2, ExtensibilityTests.Namespace2, "attribute2", "attribute 8"));

            // Child 0.
            child1 = new GenericElement();
            child1.SetAttribute(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, "attribute1", "attribute 9");
            child1.SetAttribute(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, "attribute2", "attribute 10");
            extension.AddChild(new ElementInfo(new XmlNameInfo(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, "element1"), child1));
            child2 = new GenericElement();
            child2.SetAttribute(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, "attribute3", "attribute 11");
            child1.AddChild(new ElementInfo(new XmlNameInfo(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, "element2"), child2));
            source = new Source();
            source.Language = "de-de";
            source.Space = Preservation.Preserve;
            child2.AddChild(new ElementInfo(new XmlNameInfo("source"), source));

            // Child 1.
            child1 = new GenericElement();
            child1.SetAttribute(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, "attribute1", "attribute 12");
            child1.SetAttribute(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, "attribute2", "attribute 13");
            extension.AddChild(new ElementInfo(new XmlNameInfo(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, "element1"), child1));
            child2 = new GenericElement();
            child2.SetAttribute(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, "attribute3", "attribute 14");
            child1.AddChild(new ElementInfo(new XmlNameInfo(ExtensibilityTests.Namespace1, "element2"), child2));
            child2.AddChild(new ElementInfo(new XmlNameInfo((string)null), new PlainText("text 1")));

            // Child 2.
            child1 = new GenericElement();
            child1.SetAttribute(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, "attribute1", "attribute 15");
            child1.SetAttribute(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, "attribute2", "attribute 16");
            child1.SetAttribute(ExtensibilityTests.Prefix2, ExtensibilityTests.Namespace2, "attribute1", "attribute 17");
            extension.AddChild(new ElementInfo(new XmlNameInfo(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, "element1"), child1));
            child2 = new GenericElement();
            child2.SetAttribute(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, "attribute2", "attribute 18");
            child1.AddChild(new ElementInfo(new XmlNameInfo(ExtensibilityTests.Namespace2, "element1"), child2));
            child2.AddChild(new ElementInfo(new XmlNameInfo((string)null), new PlainText("text 3")));

            // Child 3.
            child1 = new GenericElement();
            child1.SetAttribute(ExtensibilityTests.Prefix2, ExtensibilityTests.Namespace2, "attribute1", "attribute 19");
            child1.SetAttribute(ExtensibilityTests.Prefix2, ExtensibilityTests.Namespace2, "attribute2", "attribute 20");
            extension.AddChild(new ElementInfo(new XmlNameInfo(ExtensibilityTests.Prefix2, ExtensibilityTests.Namespace2, "element1"), child1));
            child2 = new GenericElement();
            child2.SetAttribute(ExtensibilityTests.Prefix2, ExtensibilityTests.Namespace2, "attribute3", "attribute 21");
            child1.AddChild(new ElementInfo(new XmlNameInfo(ExtensibilityTests.Namespace2, "element2"), child2));
            child2.AddChild(new ElementInfo(new XmlNameInfo("target"), new Target()));

            // Child 4.
            child1 = new GenericElement();
            child1.SetAttribute(ExtensibilityTests.Prefix2, ExtensibilityTests.Namespace2, "attribute1", "attribute 22");
            child1.SetAttribute(ExtensibilityTests.Prefix2, ExtensibilityTests.Namespace2, "attribute2", "attribute 23");
            extension.AddChild(new ElementInfo(new XmlNameInfo(ExtensibilityTests.Prefix2, ExtensibilityTests.Namespace2, "element1"), child1));
            child2 = new GenericElement();
            child2.SetAttribute(ExtensibilityTests.Prefix2, ExtensibilityTests.Namespace2, "attribute3", "attribute 24");
            child1.AddChild(new ElementInfo(new XmlNameInfo(ExtensibilityTests.Namespace2, "element2"), child2));
            child2.AddChild(new ElementInfo(new XmlNameInfo((string)null), new PlainText("text 2")));

            // Unit information.
            unit = new Unit("u1");
            document.Files[0].Containers.Add(unit);
            extensible = unit;

            extension = new GenericExtension("extension");
            extensible.Extensions.Add(extension);

            // Segment information.
            segment = new Segment("s1");
            segment.Source = new Source();
            segment.State = TranslationState.Initial;
            unit.Resources.Add(segment);

            Assert.AreEqual(
                            TestUtilities.GetFileContents(TestData.DocumentWithExtensions),
                            TestUtilities.GetDocumentContents(document, "    "),
                            "Document contents are incorrect.");
        }
예제 #14
0
        /// <summary>
        /// Creates a document with every element and attribute.
        /// </summary>
        /// <returns>The created document.</returns>
        private XliffDocument CreateFullyLoadedDocument()
        {
            XliffDocument result;

            result = new XliffDocument("en-us");
            result.TargetLanguage = "de-de";
            result.Space = Preservation.Default;
            // version is automatic

            result.Files.Add(this.CreateFile());

            return result;
        }
        /// <summary>
        /// Deserializes a file and stores the resulting <see cref="XliffDocument"/> internally.
        /// </summary>
        /// <param name="data">The identifier of the document to deserialize.</param>
        private void Deserialize(TestData data)
        {
            string path;

            path = System.IO.Path.Combine(Environment.CurrentDirectory, TestUtilities.TestDataDirectory, data.ToString() + ".xlf");
            using (System.IO.FileStream stream = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read))
            {
                this._document = this._reader.Deserialize(stream);
            }

            Assert.IsNotNull(this._document, "Failed to deserialize.");
        }
        public void ParentAttachedList_IndexOf()
        {
            XliffDocument item;

            //
            // Test with null.
            //

            try
            {
                this._list.IndexOf(null);
                Assert.Fail("Expected ArgumentNullException to be thrown.");
            }
            catch (ArgumentNullException)
            {
            }

            //
            // Test with empty list.
            //

            Assert.AreEqual(-1, this._list.IndexOf(new XliffDocument()), "IndexOf returned invalid index.");

            //
            // Test with invalid item1.
            //

            item = new XliffDocument();
            this._list.Add(item);
            Assert.AreEqual(-1, this._list.IndexOf(new XliffDocument()), "IndexOf returned invalid index.");

            //
            // Test with valid item1.
            //

            item = new XliffDocument();
            this._list.Add(item);
            Assert.AreEqual(1, this._list.IndexOf(item), "IndexOf returned invalid index.");
        }
        public void ParentAttachedList_Insert()
        {
            XliffDocument item1;
            XliffDocument item2;

            //
            // Test with null.
            //

            try
            {
                this._list.Insert(0, null);
                Assert.Fail("Expected ArgumentNullException to be thrown.");
            }
            catch (ArgumentNullException)
            {
            }

            //
            // Test with invalid index.
            //

            try
            {
                this._list.Insert(10, new XliffDocument());
                Assert.Fail("Expected ArgumentOutOfRangeException to be thrown.");
            }
            catch (ArgumentOutOfRangeException)
            {
            }

            //
            // Test with parent already set.
            //

            item1 = new XliffDocument();
            item1.Parent = ParentAttachedListTests._parent;
            try
            {
                this._list.Insert(0, item1);
                Assert.Fail("Expected ElementReuseException to be thrown.");
            }
            catch (ElementReuseException)
            {
            }

            //
            // Test with valid item1.
            //

            item1 = new XliffDocument();
            this._list.Insert(0, item1);
            Assert.AreEqual(1, this._list.Count, "List wasn't updated.");
            Assert.AreSame(item1, this._list[0], "Item wasn't added to list.");
            Assert.IsNotNull(item1.Parent, "Parent wasn't set.");

            //
            // Overwrite an item1.
            //

            item2 = new XliffDocument();
            this._list.Insert(0, item2);
            Assert.AreEqual(2, this._list.Count, "List wasn't updated.");
            Assert.AreSame(item2, this._list[0], "Item wasn't added to list.");
            Assert.IsNotNull(item2.Parent, "Parent wasn't set.");
            Assert.IsNotNull(item1.Parent, "Parent was modified.");
        }
 /// <summary>
 /// Deserializes the document stored in the internal stream and stores the result in the internal document.
 /// </summary>
 private void DeserializeDocument()
 {
     StandardValidatorTests._stream.Seek(0, SeekOrigin.Begin);
     this._document = StandardValidatorTests._reader.Deserialize(StandardValidatorTests._stream);
 }
        public void ParentAttachedList_RemoveAt()
        {
            XliffDocument item1;
            XliffDocument item2;

            //
            // Test with empty list.
            //

            try
            {
                this._list.RemoveAt(0);
                Assert.Fail("Expected ArgumentOutOfRangeException to be thrown.");
            }
            catch (ArgumentOutOfRangeException)
            {
            }

            //
            // Test with negative value.
            //

            item1 = new XliffDocument();
            this._list.Add(item1);

            try
            {
                this._list.RemoveAt(-1);
                Assert.Fail("Expected ArgumentOutOfRangeException to be thrown.");
            }
            catch (ArgumentOutOfRangeException)
            {
            }

            //
            // Test with invalid index.
            //

            try
            {
                this._list.RemoveAt(this._list.Count);
                Assert.Fail("Expected ArgumentOutOfRangeException to be thrown.");
            }
            catch (ArgumentOutOfRangeException)
            {
            }

            //
            // Test with valid index.
            //

            item2 = new XliffDocument();
            this._list.Add(item2);
            this._list.RemoveAt(0);
            Assert.AreEqual(1, this._list.Count, "Count is incorrect.");
            Assert.AreSame(this._list[0], item2, "List was incorrectly modified.");
            Assert.IsNull(item1.Parent, "Parent was not reset.");
            Assert.IsNotNull(item2.Parent, "Parent was reset incorrectly.");
        }
        public void ParentAttachedList_Contains()
        {
            XliffDocument item;
            bool actualValue;

            //
            // Test with empty list.
            //

            actualValue = this._list.Contains(new XliffDocument());
            Assert.IsFalse(actualValue, "Contains returned wrong result.");

            //
            // Test with valid item1.
            //

            item = new XliffDocument();
            this._list.Add(item);
            actualValue = this._list.Contains(new XliffDocument());
            Assert.IsFalse(actualValue, "Contains returned wrong result.");
            actualValue = this._list.Contains(item);
            Assert.IsTrue(actualValue, "Contains didn't find the item.");
        }
예제 #21
0
        /// <summary>
        /// Demonstrates how to store custom attributes and elements on a <see cref="File"/> element using a custom
        /// extension and element types.
        /// </summary>
        public static void StoreCustomExtension()
        {
            TestExtension extension;
            IExtensible extensible;
            Segment segment;
            XliffDocument document;
            XliffReader reader;
            Unit unit;
            string path;

            // This namespace will be stored on the document element like: <xliff xmlns:pre1="urn:custom:extension:1.0"
            const string customNamespace = "urn:custom:extension:1.0";
            const string customPrefix = "customPrefix";

            extension = new TestExtension();

            document = new XliffDocument("en-us");
            document.Files.Add(new File("f1"));

            unit = new Unit("u1");
            document.Files[0].Containers.Add(unit);

            segment = new Segment("s1");
            unit.Resources.Add(segment);

            segment.Source = new Source();
            segment.Source.Text.Add(new PlainText("text"));

            extensible = document.Files[0];

            // Create custom attributes that look like: <file id="f1" pre1:testattr1="testvalue1" pre1:testattr2="testvalue2">
            if (extensible.SupportsAttributeExtensions)
            {
                extension.AddAttribute(new TestAttribute(customPrefix, customNamespace, "testattr1", "testvalue1"));
                extension.AddAttribute(new TestAttribute(customPrefix, customNamespace, "testattr2", "testvalue2"));
                extensible.Extensions.Add(extension);
            }

            // Create a custom element that looks like: <pre1:testelement1 pre1:testattr1="testvalue1" />
            if (extensible.SupportsElementExtensions)
            {
                ElementInfo info;
                TestElement element;

                element = new TestElement();
                element.SetAttribute(customPrefix, customNamespace, "testattr1", "testvalue1");
                info = new ElementInfo(new XmlNameInfo(customPrefix, customNamespace, "testelement1"), element);
                extension.AddChild(info);
            }

            // Write the file just like any other file.
            path = IO.Path.GetTempFileName();
            SampleCode.WriteDocument(document, path);

            // Read the file using an custom extension handler so the custom types are loaded. The loaded File will
            // have the custom extension and attributes and elements on it just like it was created above.
            reader = new XliffReader();
            reader.RegisterExtensionHandler(customNamespace, new TestExtensionHandler());
            using (IO.FileStream stream = new IO.FileStream(path, IO.FileMode.Open, IO.FileAccess.Read))
            {
                document = reader.Deserialize(stream);
            }
        }
        public void ParentAttachedList_CopyTo()
        {
            XliffDocument item;
            XliffDocument[] array;

            array = new XliffDocument[3];

            //
            // Test with null array.
            //

            try
            {
                this._list.CopyTo(null, 0);
                Assert.Fail("Expected ArgumentNullException to be thrown.");
            }
            catch (ArgumentNullException)
            {
            }

            //
            // Test with invalid index.
            //

            try
            {
                this._list.CopyTo(array, array.Length + 1);
                Assert.Fail("Expected ArgumentException to be thrown.");
            }
            catch (ArgumentException)
            {
            }

            //
            // Test with empty list.
            //

            array[0] = null;
            this._list.CopyTo(array, 0);
            Assert.IsNull(array[0], "Array was updated.");

            //
            // Test with valid item1.
            //

            item = new XliffDocument();
            this._list.Add(item);
            this._list.CopyTo(array, 0);
            Assert.IsNotNull(array[0], "Array was not updated.");
            Assert.AreSame(item, array[0], "List was not copied.");

            //
            // Test with valid items.
            //

            item = new XliffDocument();
            this._list.Add(item);
            this._list.CopyTo(array, 1);
            Assert.IsNotNull(array[0], "Array[0] was not updated.");
            Assert.IsNotNull(array[1], "Array[1] was not updated.");
            Assert.IsNotNull(array[2], "Array[2] was not updated.");
            Assert.AreSame(array[0], array[1], "List was not copied at index 1.");
            Assert.AreSame(item, array[2], "Full list was not copied.");
        }
예제 #23
0
        /// <summary>
        /// Demonstrates how to validate a document on write and display the data where the validation error occurred.
        /// </summary>
        /// <param name="document">The document to validate.</param>
        /// <param name="file">The path to write the document to.</param>
        public static void ViewValidations(XliffDocument document, string file)
        {
            using (IO.FileStream stream = new IO.FileStream(file, IO.FileMode.Create, IO.FileAccess.Write))
            {
                XliffWriter writer;

                writer = new XliffWriter();

                try
                {
                    writer.Serialize(stream, document);
                }
                catch (ValidationException e)
                {
                    Console.WriteLine("ValidationException Details:");
                    if (e.Data != null)
                    {
                        foreach (object key in e.Data.Keys)
                        {
                            Console.WriteLine("  '{0}': '{1}'", key, e.Data[key]);
                        }
                    }
                }
            }
        }
        public void ParentAttachedList_Count()
        {
            XliffDocument item;

            //
            // Test with empty list.
            //

            Assert.AreEqual(0, this._list.Count);

            //
            // Test with valid items.
            //

            item = new XliffDocument();
            this._list.Add(item);
            Assert.AreEqual(1, this._list.Count);
            item = new XliffDocument();
            this._list.Add(item);
            item = new XliffDocument();
            this._list.Add(item);
            Assert.AreEqual(3, this._list.Count);
        }
예제 #25
0
        /// <summary>
        /// Demonstrates how to write an XLIFF document to a file.
        /// </summary>
        /// <param name="document">The document to write.</param>
        /// <param name="file">The path to write the document to.</param>
        public static void WriteDocument(XliffDocument document, string file)
        {
            using (IO.FileStream stream = new IO.FileStream(file, IO.FileMode.Create, IO.FileAccess.Write))
            {
                XliffWriter writer;

                writer = new XliffWriter();
                writer.Serialize(stream, document);
            }
        }
        public void ParentAttachedList_GetEnumerator()
        {
            IEnumerator<XliffDocument> enumerator;
            XliffDocument item;

            //
            // Test with empty list.
            //

            enumerator = this._list.GetEnumerator();
            Assert.IsNotNull(enumerator, "Enumerator is null.");
            Assert.IsFalse(enumerator.MoveNext(), "Enumerator should be empty.");

            //
            // Test with multiple items.
            //

            item = new XliffDocument();
            this._list.Add(item);
            item = new XliffDocument();
            this._list.Add(item);
            enumerator = this._list.GetEnumerator();
            Assert.IsNotNull(enumerator, "Enumerator is null.");
            Assert.IsTrue(enumerator.MoveNext(), "Enumerator should not be empty.");
            Assert.IsTrue(enumerator.MoveNext(), "Enumerator should not be empty.");
            Assert.AreSame(item, enumerator.Current, "Enumerator item is incorrect.");
            Assert.IsFalse(enumerator.MoveNext(), "Enumerator should be empty.");
        }
예제 #27
0
        /// <summary>
        /// Serializes an <see cref="XliffDocument"/> to an Xml stream.
        /// </summary>
        /// <param name="writer">The Xml stream to write to.</param>
        /// <param name="document">The document to write.</param>
        private void Serialize(XmlWriter writer, XliffDocument document)
        {
            // Validate the document before serializing.
            foreach (IXliffValidator validator in this.settings.Validators)
            {
                validator.Validate(document);
            }

            this.document = document;
            this.writer = writer;

            try
            {
                this.SerializeImpl();
            }
            finally
            {
                this.document = null;
                this.writer = null;
            }
        }
        public void ParentAttachedList_Indexer()
        {
            XliffDocument item1;
            XliffDocument item2;

            //
            // Test get with invalid index.
            //

            try
            {
                item1 = this._list[0];
                Assert.Fail("Expected ArgumentOutOfRangeException to be thrown.");
            }
            catch (ArgumentOutOfRangeException)
            {
            }

            //
            // Test set with invalid index.
            //

            item1 = new XliffDocument();

            try
            {
                this._list[0] = item1;
                Assert.Fail("Expected ArgumentOutOfRangeException to be thrown.");
            }
            catch (ArgumentOutOfRangeException)
            {
            }

            this._list.Add(item1);

            //
            // Test set with null.
            //

            try
            {
                this._list[0] = null;
                Assert.Fail("Expected ArgumentNullException to be thrown.");
            }
            catch (ArgumentNullException)
            {
            }

            //
            // Test set with parent already set.
            //

            Assert.IsNotNull(item1.Parent, "Parent should be set.");

            try
            {
                this._list[0] = item1;
                Assert.Fail("Expected ElementReuseException to be thrown.");
            }
            catch (ElementReuseException)
            {
            }

            //
            // Test set by overwriting item1.
            //

            item2 = new XliffDocument();
            this._list[0] = item2;
            Assert.IsNull(item1.Parent, "Parent should be reset.");
            Assert.IsNotNull(item2.Parent, "Parent should be set.");

            //
            // Test that get works.
            //

            Assert.AreSame(item2, this._list[0], "Get didn't return the correct item.");
        }