/// <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);
            }
        }
        public void XliffWriter_Segment()
        {
            File file;
            Segment segment;
            Unit unit;
            string actualValue;

            file = new File();
            this._document.Files.Add(file);

            unit = new Unit();
            file.Containers.Add(unit);

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

            Console.WriteLine("Test with default values. Also tests that State is not output because SubState is not output.");
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.SegmentWithDefaultValues), actualValue);

            Console.WriteLine("Test with empty values. Also tests that State is output because SubState is output.");
            segment.Id = String.Empty;
            segment.SubState = String.Empty;
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.SegmentWithEmptyValues), actualValue);

            Console.WriteLine("Test with valid values.");
            segment.CanResegment = true;
            segment.Id = "id";
            segment.State = TranslationState.Reviewed;
            segment.SubState = "substate";
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.SegmentWithValidValues), actualValue);
        }
        public void XliffWriter_OriginalData()
        {
            Unit unit;
            string actualValue;

            unit = new Unit();
            unit.OriginalData = new OriginalData();
            this._document.Files.Add(new File());
            this._document.Files[0].Containers.Add(unit);

            Console.WriteLine("Test with default values.");
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.OriginalDataWithDefaultValues), actualValue);

            Console.WriteLine("Test with valid values.");
            unit.OriginalData.AddData("id", "text");
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.OriginalDataWithValidValues), actualValue);
        }
        public void XliffWriter_Metadata()
        {
            MetaGroup group;
            Unit unit;
            string actualValue;

            group = new MetaGroup();
            group.AppliesTo = MetaGroupSubject.Source;
            group.Category = "category";
            group.Containers.Add(new Meta("type", "text"));

            unit = new Unit("u1");
            unit.Metadata = new MetadataContainer();
            unit.Metadata.Groups.Add(group);
            this._document.Files.Add(new File("f1"));
            this._document.Files[0].Containers.Add(unit);

            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.UnitWithMetadata), actualValue);
        }
        public void XliffWriter_MarkedSpanStart()
        {
            MarkedSpanStart span;
            Segment segment;
            Unit unit;
            string actualValue;

            unit = new Unit("u1");
            this._document.SourceLanguage = "en-us";
            this._document.Files.Add(new File("f1"));
            this._document.Files[0].Containers.Add(unit);

            segment = new Segment("s1");
            unit.Resources.Add(segment);
            segment.Source = new Source();
            span = new MarkedSpanStart("mrk1");
            segment.Source.Text.Add(span);

            actualValue = this.Serialize();
            // Translate value will be automatically written because type is generic (default).
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.MarkedSpanStartWithValidValues), actualValue);

            span.Type = MarkedSpanTypes.Term;
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.MarkedSpanStartWithTerm), actualValue);

            span.Type = "my:type";
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.MarkedSpanStartWithCustomType), actualValue);
        }
        /// <summary>
        /// Creates a unit with every element and attribute.
        /// </summary>
        /// <returns>The created unit.</returns>
        private Unit CreateUnit()
        {
            Unit result;
            Ignorable ignorable;
            Note note;
            Segment segment;

            this.ids[typeof(Data)] = 0;

            result = new Unit("u" + this.GetNextId(typeof(Unit)));
            result.CanResegment = true;
            result.FormatStyle = FormatStyleValue.Anchor;
            result.Name = "unit";
            result.SizeInfoReference = "sizeref";
            result.SizeRestriction = "restriction";
            result.SourceDirectionality = ContentDirectionality.RTL;
            result.Space = Preservation.Preserve;
            result.StorageRestriction = "restriction";
            result.SubFormatStyle.Add("key1", "value1");
            result.SubFormatStyle.Add("key2", "value2");
            result.TargetDirectionality = ContentDirectionality.LTR;
            result.Translate = false;
            result.Type = "pre:type";

            this.AddExtension(result);
            note = this.AddNote(result);
            result.OriginalData = this.CreateOriginalData();
            segment = this.CreateSegment(note.Id);
            result.Resources.Add(segment);
            ignorable = this.CreateIgnorable(note.Id);
            ignorable.Target.Order = 2;
            result.Resources.Add(ignorable);

            result.Changes = this.CreateChangeTrackingModule_ChangeTrack();
            result.Glossary = this.CreateGlossary(segment.Id);
            result.Matches.Add(this.CreateMatch(note.Id));
            result.Matches[0].SourceReference = Utilities.MakeIri(segment.Id);
            result.Metadata = this.CreateMetadata();
            result.ProfileData = this.CreateSizeRestrictionModule_ProfileData();
            result.ResourceData = this.CreateResourceDataModule_ResourceData();
            result.ValidationRules = this.CreateValidationModule_Validation();

            return result;
        }
        /// <summary>
        /// Demonstrates how to store <see cref="Glossary"/> data in a <see cref="Unit"/> element.
        /// </summary>
        public static void StoreGlossary()
        {
            GlossaryEntry entry;
            MarkedSpan span;
            Segment segment;
            Translation translation;
            Unit unit;

            // Create a unit to hold the glossary.
            unit = new Unit("id");
            unit.Glossary = new Glossary();
            entry = new GlossaryEntry();
            entry.Reference = "#m1";
            unit.Glossary.Entries.Add(entry);

            // Create a term that looks like: <gls:term source="publicTermbase">TAB key</gls:term>
            entry.Term.Source = "publicTermbase";
            entry.Term.Text = "TAB key";

            // Create a translation that looks like: <gls:translation id="1" source="myTermbase">Tabstopptaste</gls:translation>
            translation = new Translation("1");
            translation.Source = "myTermbase";
            translation.Text = "Tabstopptaste";
            entry.Translations.Add(translation);

            // Create a translation that looks like: <gls:translation ref="#m2" source="myTermbase">TAB-TASTE</gls:translation>
            translation = new Translation();
            translation.Reference = "#m1";
            translation.Source = "myTermbase";
            translation.Text = "TAB-TASTE";
            entry.Translations.Add(translation);

            // Create a definition that looks like:
            //  <gls:definition source="publicTermbase">A keyboard key that is traditionally used to insert tab"
            //      characters into a document.</gls:definition>
            entry.Definition = new Definition();
            entry.Definition.Source = "publicTermbase";
            entry.Definition.Text = "A keyboard key that is traditionally used to insert tab characters into a document.";

            // Create a segment to which the glossary refers.
            segment = new Segment();
            segment.Source = new Source();
            segment.Source.Text.Add(new PlainText("Press the "));
            span = new MarkedSpan("m1");
            span.Type = "term";
            span.Text.Add(new PlainText("TAB key"));
            segment.Source.Text.Add(span);

            segment.Target = new Target();
            segment.Target.Text.Add(new PlainText("Drücken Sie die "));
            span = new MarkedSpan("m1");
            span.Type = "term";
            span.Text.Add(new PlainText("TAB-TASTE"));
            segment.Target.Text.Add(span);

            unit.Resources.Add(segment);
        }
        public void StandardValidator_FormatStyle_Attributes()
        {
            Core.File file;
            Segment segment;
            SpanningCodeEnd end;
            SpanningCodeStart start;
            Unit unit;

            segment = new Segment("sx");
            segment.Source = new Source();
            unit = new Unit("ux");
            unit.Resources.Add(segment);

            file = new Core.File("fx");
            file.Containers.Add(unit);

            this.DeserializeDocument();
            this._document.Files.Add(file);

            start = new SpanningCodeStart();
            start.Id = "sc1";
            segment.Source.Text.Add(start);

            end = new SpanningCodeEnd();
            end.Isolated = false;
            end.FormatStyle = FormatStyleValue.Anchor;
            end.StartReference = start.Id;
            segment.Source.Text.Add(end);

            Console.WriteLine("Test fs with ec not isolated.");
            this.VerifyValidationException(ValidationError.FormatStyleWithSpanEndNotIsolated);
            end.Isolated = true;
            end.StartReference = null;
            start.Isolated = true;

            Console.WriteLine("Test subfs without fs.");
            end.FormatStyle = null;
            end.SubFormatStyle.Add("key", "value");
            this.VerifyValidationException(ValidationError.FormatStyleSubFormatWithoutFormat);

            Console.WriteLine("Test with valid data.");
            end.FormatStyle = FormatStyleValue.Anchor;
            StandardValidatorTests._validator.Validate(this._document);
        }
        public void XliffWriter_Glossary()
        {
            MarkedSpan span;
            Segment segment;
            Unit unit;

            unit = new Unit("u1");
            unit.Glossary = new Glossary();
            unit.Glossary.Entries.Add(new GlossaryEntry());
            unit.Glossary.Entries[0].Id = "entry1";
            unit.Glossary.Entries[0].Reference = Utilities.MakeIri("m1");
            unit.Glossary.Entries[0].Definition = new Definition();
            unit.Glossary.Entries[0].Definition.Source = "definitionSource";
            unit.Glossary.Entries[0].Definition.Text = "definition text";
            unit.Glossary.Entries[0].Term.Source = "termSource";
            unit.Glossary.Entries[0].Term.Text = "term text";
            unit.Glossary.Entries[0].Translations.Add(new Translation());
            unit.Glossary.Entries[0].Translations[0].Id = "trans1";
            unit.Glossary.Entries[0].Translations[0].Reference = Utilities.MakeIri("m1");
            unit.Glossary.Entries[0].Translations[0].Source = "transSource";
            unit.Glossary.Entries[0].Translations[0].Text = "translation text";

            span = new MarkedSpan("m1");
            span.Type = "term";
            span.Text.Add(new PlainText("text"));

            segment = new Segment();
            segment.Source = new Source();
            segment.Source.Text.Add(span);
            segment.State = TranslationState.Initial;

            this._document.SourceLanguage = "en-us";
            this._document.Files.Add(new File("f1"));
            this._document.Files[0].Containers.Add(unit);
            unit.Resources.Add(segment);

            Assert.AreEqual(
                            TestUtilities.GetFileContents(TestData.GlossaryWithValidValues),
                            this.Serialize(),
                            "Serialized document is incorrect.");
        }
示例#10
0
        public void XliffWriter_FullDocument()
        {
            File file;
            Group group1;
            Group group2;
            Ignorable ignorable;
            Note note;
            Segment segment;
            Skeleton skeleton;
            Unit unit;
            string actualValue;

            this._document.SourceLanguage = "en-us";
            this._document.Space = Preservation.Default;
            this._document.TargetLanguage = "de-de";
            this._document.Version = "version";

            file = new File();
            file.CanResegment = true;
            file.Id = "id";
            file.Original = "original";
            file.SourceDirectionality = ContentDirectionality.LTR;
            file.Space = Preservation.Preserve;
            file.TargetDirectionality = ContentDirectionality.RTL;
            file.Translate = true;
            this._document.Files.Add(file);

            group1 = new Group();
            group1.CanResegment = true;
            group1.Id = "id";
            group1.Name = "name";
            group1.SourceDirectionality = ContentDirectionality.RTL;
            group1.Space = Preservation.Preserve;
            group1.TargetDirectionality = ContentDirectionality.Auto;
            group1.Translate = true;
            group1.Type = "type";
            file.Containers.Add(group1);

            group2 = new Group();
            group2.CanResegment = false;
            group2.Id = "id2";
            group2.Name = "name2";
            group2.SourceDirectionality = ContentDirectionality.LTR;
            group2.Space = Preservation.Default;
            group2.TargetDirectionality = ContentDirectionality.RTL;
            group2.Translate = false;
            group2.Type = "type2";
            file.Containers.Add(group2);

            unit = new Unit();
            unit.CanResegment = false;
            unit.Id = "id";
            unit.Name = "name";
            unit.Notes.Add(new Note("text"));
            unit.SourceDirectionality = ContentDirectionality.LTR;
            unit.Space = Preservation.Default;
            unit.TargetDirectionality = ContentDirectionality.Auto;
            unit.Translate = true;
            unit.Type = "type";
            group2.Containers.Add(unit);

            unit.OriginalData = new OriginalData();
            unit.OriginalData.AddData("id", "data1");
            unit.OriginalData.DataElements[0].Directionality = ContentDirectionality.RTL;
            unit.OriginalData.DataElements[0].Space = Preservation.Preserve;
            unit.OriginalData.DataElements[0].Text.Add(new CodePoint(1));

            unit = new Unit();
            unit.CanResegment = false;
            unit.Id = "id2";
            unit.Name = "name2";
            unit.Notes.Add(new Note("text2"));
            unit.SourceDirectionality = ContentDirectionality.RTL;
            unit.Space = Preservation.Preserve;
            unit.TargetDirectionality = ContentDirectionality.LTR;
            unit.Translate = false;
            unit.Type = "type2";
            group2.Containers.Add(unit);

            unit.OriginalData = new OriginalData();
            unit.OriginalData.AddData("id1", "data1");
            unit.OriginalData.DataElements[0].Directionality = ContentDirectionality.LTR;
            unit.OriginalData.DataElements[0].Space = Preservation.Preserve;
            unit.OriginalData.DataElements[0].Text.Add(new CodePoint(2));
            unit.OriginalData.AddData("id2", "data2");
            unit.OriginalData.DataElements[1].Directionality = ContentDirectionality.RTL;
            unit.OriginalData.DataElements[1].Space = Preservation.Preserve;
            unit.OriginalData.DataElements[1].Text.Add(new CodePoint(1));

            ignorable = new Ignorable();
            ignorable.Id = "id";
            ignorable.Source = new Source("text");
            ignorable.Target = new Target("text");
            unit.Resources.Add(ignorable);

            segment = new Segment();
            segment.CanResegment = true;
            segment.Id = "id";
            segment.Source = new Source();
            segment.State = TranslationState.Reviewed;
            segment.SubState = "substate";
            segment.Target = new Target();
            segment.Source.Language = "en-us";
            segment.Source.Space = Preservation.Default;
            segment.Source.Text.Add(new CodePoint(1));
            segment.Source.Text.Add(new PlainText("text"));
            segment.Target.Language = "en-us";
            segment.Target.Order = 100;
            segment.Target.Space = Preservation.Default;
            segment.Target.Text.Add(new CodePoint(12));
            segment.Target.Text.Add(new PlainText("text2"));
            unit.Resources.Add(segment);

            note = new Note();
            note.AppliesTo = TranslationSubject.Source;
            note.Category = "category";
            note.Id = "id";
            note.Priority = 2;
            note.Text = "text";
            file.Notes.Add(note);

            file.Notes.Add(new Note("text2"));

            skeleton = new Skeleton();
            skeleton.HRef = "href";
            skeleton.NonTranslatableText = "text";
            file.Skeleton = skeleton;

            file = new File();
            file.CanResegment = false;
            file.Id = "id2";
            file.Notes.Add(new Note("text"));
            file.Original = "original2";
            file.SourceDirectionality = ContentDirectionality.Auto;
            file.Space = Preservation.Default;
            file.TargetDirectionality = ContentDirectionality.LTR;
            file.Translate = false;
            this._document.Files.Add(file);

            skeleton = new Skeleton();
            skeleton.HRef = "href";
            skeleton.NonTranslatableText = "text";
            file.Skeleton = skeleton;

            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.FullDocument), actualValue);
        }
示例#11
0
        public void XliffWriter_Data()
        {
            Data data;
            Unit unit;
            string actualValue;

            data = new Data();
            unit = new Unit();
            unit.OriginalData = new OriginalData();
            unit.OriginalData.DataElements.Add(data);
            this._document.Files.Add(new File());
            this._document.Files[0].Containers.Add(unit);

            Console.WriteLine("Test with default values.");
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.DataWithDefaultValues), actualValue);

            Console.WriteLine("Test with empty values.");
            data.Id = String.Empty;
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.DataWithEmptyValues), actualValue);

            Console.WriteLine("Test with valid values.");
            data.Directionality = ContentDirectionality.RTL;
            data.Id = "id";
            data.Space = Preservation.Preserve;
            data.Text.Add(new CodePoint(3));
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.DataWithValidValues), actualValue);
        }
示例#12
0
        public void XliffWriter_UnitNotes()
        {
            File file;
            Unit unit;
            string actualValue;

            file = new File();
            this._document.Files.Add(file);

            unit = new Unit();
            file.Containers.Add(unit);

            Console.WriteLine("Test with default values.");
            unit.Notes.Add(new Note());
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.UnitNoteWithDefaultValues), actualValue);

            Console.WriteLine("Test with empty values.");
            unit.Notes[0].Category = String.Empty;
            unit.Notes[0].Id = String.Empty;
            unit.Notes[0].Text = String.Empty;
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.UnitNoteWithEmptyValues), actualValue);

            Console.WriteLine("Test with valid values.");
            unit.Notes[0].AppliesTo = TranslationSubject.Target;
            unit.Notes[0].Category = "category";
            unit.Notes[0].Id = "id";
            unit.Notes[0].Priority = 100;
            unit.Notes[0].Text = "text";
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.UnitNoteWithValidValues), actualValue);
        }
示例#13
0
        public void XliffWriter_Unit()
        {
            File file;
            Unit unit;
            string actualValue;

            file = new File();
            this._document.Files.Add(file);

            unit = new Unit();
            file.Containers.Add(unit);

            Console.WriteLine("Test with default values.");
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.UnitWithDefaultValues), actualValue);

            Console.WriteLine("Test with empty values.");
            unit.Id = String.Empty;
            unit.Name = String.Empty;
            unit.Type = String.Empty;
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.UnitWithEmptyValues), actualValue);

            Console.WriteLine("Test with valid values.");
            unit.CanResegment = false;
            unit.Id = "id";
            unit.Name = "name";
            unit.SourceDirectionality = ContentDirectionality.Auto;
            unit.Space = Preservation.Default;
            unit.TargetDirectionality = ContentDirectionality.LTR;
            unit.Translate = true;
            unit.Type = "type";
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.UnitWithValidValues), actualValue);
        }
        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)
            {
            }
        }
        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.");
        }
示例#16
0
        public void XliffWriter_SpanningCodeEnd()
        {
            File file;
            Segment segment;
            SpanningCodeEnd span;
            Unit unit;
            string actualValue;

            file = new File();
            this._document.Files.Add(file);

            unit = new Unit("unit1");
            file.Containers.Add(unit);

            segment = new Segment("seg1");
            segment.Source = new Source();
            span = new SpanningCodeEnd();
            segment.Source.Text.Add(span);
            unit.Resources.Add(segment);

            Console.WriteLine("Test with null Id.");
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.SpanningCodeEndWithNullId), actualValue);

            Console.WriteLine("Test with empty Id.");
            span.Id = string.Empty;
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.SpanningCodeEndWithNullId), actualValue);

            Console.WriteLine("Test with valid Id.");
            span.Id = "span1";
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.SpanningCodeEndWithValidId), actualValue);
        }
示例#17
0
        public void XliffWriter_Target()
        {
            Target target;
            Unit unit;
            string actualValue;

            this._document.Files.Add(new File());
            unit = new Unit();
            this._document.Files[0].Containers.Add(unit);
            unit.Resources.Add(new Segment());
            target = new Target();
            unit.Resources[0].Target = target;

            Console.WriteLine("Test with default values.");
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.TargetWithDefaultValues), actualValue);

            Console.WriteLine("Test with empty values.");
            target.Language = String.Empty;
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.TargetWithEmptyValues), actualValue);

            Console.WriteLine("Test with valid values.");
            target.Language = "en-us";
            target.Order = 100;
            target.Space = Preservation.Preserve;
            target.Text.Add(new CodePoint(1));
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.TargetWithValidValues), actualValue);
        }
示例#18
0
        public void XliffWriter_Ignorable()
        {
            File file;
            Ignorable ignorable;
            Unit unit;
            string actualValue;

            file = new File();
            this._document.Files.Add(file);

            unit = new Unit();
            file.Containers.Add(unit);

            ignorable = new Ignorable();
            unit.Resources.Add(ignorable);

            Console.WriteLine("Test with default values.");
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.IgnorableWithDefaultValues), actualValue);

            Console.WriteLine("Test with empty values.");
            ignorable.Id = String.Empty;
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.IgnorableWithEmptyValues), actualValue);

            Console.WriteLine("Test with valid values.");
            ignorable.Id = "id";
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.IgnorableWithValidValues), actualValue);
        }
        /// <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);
            }
        }
示例#20
0
        public void XliffWriter_Inheritance()
        {
            File file;
            Segment segment;
            Unit unit;
            string actualValue;

            file = new File("newfile");
            file.Translate = false;
            this._document.SourceLanguage = "en-us";
            this._document.Files.Add(file);

            unit = new Unit("newunit");
            // Inherit the value of Translate.
            file.Containers.Add(unit);

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

            segment.Source = new Source();
            segment.Source.Text.Add(new MarkedSpan("newmrk") { Translate = true });

            actualValue = this.Serialize(OutputDetail.Minimal);
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.InheritanceWithMinimalOutput), actualValue);
        }
        /// <summary>
        /// Demonstrates how to store <see cref="Match"/> data in a <see cref="Unit"/> element.
        /// </summary>
        public static void StoreMatches()
        {
            Match match;
            Segment segment;
            Unit unit;

            unit = new Unit("id");

            segment = new Segment("seg1");
            segment.Source = new Source("text");
            segment.Target = new Target("best translation");
            unit.Resources.Add(segment);

            match = new Match("#seg1");
            match.MatchQuality = 90.0f;
            match.MatchSuitability = 87.0f;
            match.Origin = "custom tool";
            match.Similarity = 10.0f;
            match.Source = new Source("text");
            match.SubType = "ms:type";
            match.Target = new Target("translation 1");
            match.Type = MatchType.TranslationMemory;
            unit.Matches.Add(match);

            match = new Match("#seg1");
            match.MatchQuality = 80.0f;
            match.MatchSuitability = 75.0f;
            match.Origin = "custom tool";
            match.Similarity = 8.0f;
            match.Source = new Source("text");
            match.SubType = "ms:type";
            match.Target = new Target("translation 2");
            match.Type = MatchType.TranslationMemory;
            unit.Matches.Add(match);

            match = new Match("#seg1");
            match.MatchQuality = 60.0f;
            match.MatchSuitability = 57.0f;
            match.Origin = "custom tool";
            match.Similarity = 7.0f;
            match.Source = new Source("text");
            match.SubType = "ms:type";
            match.Target = new Target("translation 3");
            match.Type = MatchType.TranslationMemory;
            unit.Matches.Add(match);
        }
示例#22
0
        public void XliffWriter_MarkedSpan()
        {
            MarkedSpan span1;
            MarkedSpan span2;
            Segment segment;
            Unit unit;
            string actualValue;

            unit = new Unit("u1");
            this._document.SourceLanguage = "en-us";
            this._document.Files.Add(new File("f1"));
            this._document.Files[0].Containers.Add(unit);

            segment = new Segment("s1");
            unit.Resources.Add(segment);
            segment.Source = new Source();
            span1 = new MarkedSpan("mrk1");
            segment.Source.Text.Add(span1);

            Console.WriteLine("Test with valid values.");
            actualValue = this.Serialize();
            // Translate value will be automatically written because type is generic (default).
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.MarkedSpanWithValidValues), actualValue);

            Console.WriteLine("Test with term.");
            span1.Type = MarkedSpanTypes.Term;
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.MarkedSpanWithTerm), actualValue);

            Console.WriteLine("Test with custom type.");
            span1.Type = "my:type";
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.MarkedSpanWithCustomType), actualValue);

            Console.WriteLine("Test with nested spans.");
            span1.Type = MarkedSpanTypes.Comment;
            span1.Value = "this is a comment";
            span2 = new MarkedSpan("mrk2");
            span2.Translate = true;
            span1.Text.Add(span2);
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.MarkedSpanWithNestedMarkedSpan), actualValue);
        }