This class reads XLIFF 2.0 documents.
        public static void ClassInitialize(TestContext context)
        {
            string path;

            StandardValidatorTests._stream = new MemoryStream();

            path = Path.Combine(Environment.CurrentDirectory, TestUtilities.TestDataDirectory, "ValidDocument.xlf");
            using (FileStream stream = new FileStream(path, FileMode.Open))
            {
                stream.CopyTo(StandardValidatorTests._stream);
            }

            StandardValidatorTests._reader = new XliffReader();
            StandardValidatorTests._validator = new StandardValidator();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Demonstrates how to read an XLIFF document from a file.
        /// </summary>
        /// <param name="file">The path to the document to read.</param>
        public static void ReadDocument(string file)
        {
            using (IO.FileStream stream = new IO.FileStream(file, IO.FileMode.Open, IO.FileAccess.Read))
            {
                XliffDocument document;
                XliffReader reader;

                reader = new XliffReader();
                document = reader.Deserialize(stream);
            }
        }
Exemplo n.º 3
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 Xliff_DataDriven()
        {
            string path;
            ExpectedResult expectedResult;
            int errorNumber;

            errorNumber = -1;
            path = (string)this.TestContext.DataRow[FileBasedTests.TestEntryPathAttribute];
            expectedResult = (ExpectedResult)Enum.Parse(
                                                        typeof(ExpectedResult),
                                                        (string)this.TestContext.DataRow[FileBasedTests.TestEntryResultAttribute]);
            if (expectedResult == ExpectedResult.ValidationError)
            {
                errorNumber = int.Parse((string)this.TestContext.DataRow[FileBasedTests.TestEntryErrorAttribute]);
            }

            Trace.WriteLine("Path: " + path);
            Trace.WriteLine("ExpectedResult: " + expectedResult);
            Trace.WriteLine("ErrorNumber: " + errorNumber);

            try
            {
                Assert.IsTrue(System.IO.File.Exists(path), "File '{0}' doesn't exist.", path);
                using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
                {
                    XliffDocument document;
                    XliffReader reader;

                    reader = new XliffReader();
                    document = reader.Deserialize(stream);
                }

                Assert.AreEqual(expectedResult, ExpectedResult.Success, "Expected result is incorrect.");
            }
            catch (FormatException)
            {
                if (expectedResult != ExpectedResult.BadFormat)
                {
                    throw;
                }
            }
            catch (InvalidOperationException)
            {
                if (expectedResult != ExpectedResult.InvalidOperation)
                {
                    throw;
                }
            }
            catch (NotSupportedException)
            {
                if (expectedResult != ExpectedResult.NotSupported)
                {
                    throw;
                }
            }
            catch (ValidationException e)
            {
                if (expectedResult != ExpectedResult.ValidationError)
                {
                    throw;
                }

                Assert.AreEqual(errorNumber, e.ErrorNumber, "ErrorNumber is incorrect.");
            }
            catch (XmlException)
            {
                if (expectedResult != ExpectedResult.ReadError)
                {
                    throw;
                }
            }
        }
Exemplo n.º 5
0
        public void Extensibility_FullDocumentRoundtrip()
        {
            XliffDocument document;
            string documentContents;
            string fileContents;
            int mismatchIndex;

            this.ids.Clear();

            document = this.CreateFullyLoadedDocument();
            documentContents = TestUtilities.GetDocumentContents(document, string.Empty);
            fileContents = TestUtilities.GetFileContents(TestData.DocumentWithEverything);
            mismatchIndex = -1;
            for (int i = 0; i < fileContents.Length; i++)
            {
                if ((mismatchIndex < 0) && (fileContents[i] != documentContents[i]))
                {
                    mismatchIndex = i;
                    break;
                }
            }

            if (mismatchIndex >= 0)
            {
                Console.WriteLine("Expected Output:");
                Console.WriteLine(fileContents);
                Console.WriteLine();
                Console.WriteLine("Actual Output:");
                Console.WriteLine(documentContents);
                Console.WriteLine();
            }

            Assert.IsTrue(fileContents == documentContents,
                          "Document contents are incorrect starting at index {0} (neighbor chars are '{1}' vs. '{2}').",
                          mismatchIndex,
                          (mismatchIndex >= 10) ? fileContents.Substring(mismatchIndex - 10, 20) : "[see output]",
                          (mismatchIndex >= 10) ? documentContents.Substring(mismatchIndex - 10, 20) : "[see output]");

            Console.WriteLine("Serializing and deserializing document");
            using (IO.MemoryStream stream = new IO.MemoryStream())
            {
                XliffReader reader;
                XliffWriter writer;

                writer = new XliffWriter();
                writer.Serialize(stream, document);

                stream.Seek(0, IO.SeekOrigin.Begin);
                reader = new XliffReader();
                document = reader.Deserialize(stream);

                Assert.AreEqual(
                              TestUtilities.GetFileContents(TestData.DocumentWithEverything),
                              TestUtilities.GetDocumentContents(document, string.Empty),
                              "Document contents are incorrect.");
            }
        }
Exemplo n.º 6
0
 public static void WhiteSpaces()
 {
     string data = "<xliff srcLang='en' version='2.0' xmlns='urn:oasis:names:tc:xliff:document:2.0'>"
     + "<file id='f1'><unit id='u1'>"
     + "<segment><source>Sentence 1.</source></segment>"
     + "<ignorable><source> </source></ignorable>"
     + "<segment><source>Sentence 2.</source></segment>"
     + "</unit></file></xliff>";
     using (IO.MemoryStream ms = new IO.MemoryStream(Encoding.UTF8.GetBytes(data)))
     {
         XliffReader reader = new XliffReader();
         XliffDocument doc = reader.Deserialize(ms);
         foreach (XliffElement e in doc.CollapseChildren<XliffElement>())
         {
             Console.WriteLine("Type: " + e.GetType().ToString());
             if (e is PlainText)
             {
                 PlainText pt = (PlainText)e;
                 Console.WriteLine("Content: '" + pt.Text + "'");
             }
         }
     }
     Console.ReadKey();
 }