Exemplo n.º 1
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);
            }
        }
Exemplo n.º 2
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);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Serializes an <see cref="XliffElement"/> to the stream using the specified Xml element Name and namespace.
        /// The method creates an element and writes attributes if necessary, then writes out text followed by notes
        /// if the element supports notes. It then uses recursion to do the same for children.
        /// </summary>
        /// <param name="name">The name of the XmlElement to create.</param>
        /// <param name="namespaceName">The namespace of the XmlElement to create.</param>
        /// <param name="prefix">The prefix Name of the XmlElement to create.</param>
        /// <param name="provider">The content to serialize.</param>
        /// <param name="extraNamespace">A set of namespaces to configure on the element. The key is the name
        /// of the namespace and the value is the URI of the namespace.</param>
        /// <param name="serializeInnerContent">True to serialize inner content. If false only the opening and closing
        /// tags will be serialized along with attributes.</param>
        private void SerializeImpl(
            string name,
            string namespaceName,
            string prefix,
            IXliffDataProvider provider,
            Dictionary <string, string> extraNamespace,
            bool serializeInnerContent)
        {
            // The name may be null for elements that are just inline elements for others. For example, the
            // PlainText class represents text within a Source node, but doesn't actually require an Xml element
            // so it won't have a Name.
            if (name != null)
            {
                this.writer.WriteStartElement(prefix, name, namespaceName);

                if (extraNamespace != null)
                {
                    foreach (string key in extraNamespace.Keys)
                    {
                        this.writer.WriteAttributeString(
                            NamespacePrefixes.XmlNamespace,
                            key,
                            string.Empty,
                            extraNamespace[key]);
                    }
                }

                this.SerializeAttributesImpl(provider);
                this.SerializeExtensions(provider as IExtensible, true, false);
            }

            if (serializeInnerContent)
            {
                Comparison <OutputItem> comparer;
                List <OutputItem>       order;
                Dictionary <Type, List <ElementInfo> > children;
                List <ElementInfo> unorderedChildren;
                bool serializedChildAsObject;
                bool serializedText;
                bool serializedExtensions;

                serializedChildAsObject = false;
                serializedText          = false;
                serializedExtensions    = false;

                children = XliffWriter.GetProviderChildrenGroupedByType(provider);

                comparer = delegate(OutputItem item1, OutputItem item2)
                {
                    int result;

                    if (item1.GroupOrdinal == item2.GroupOrdinal)
                    {
                        result = 0;
                    }
                    else if (item1.GroupOrdinal < item2.GroupOrdinal)
                    {
                        result = -1;
                    }
                    else
                    {
                        result = 1;
                    }

                    return(result);
                };

                order = new List <OutputItem>(provider.XliffOutputOrder);
                order.Sort(comparer);

                foreach (OutputItem item in order)
                {
                    switch (item.ItemType)
                    {
                    case OutputItemType.Child:
                        this.SerializeChildImpl(children[item.ChildType]);

                        // Object was specified by the provider so don't treat it as the unordered list.
                        serializedChildAsObject |= item.ChildType == typeof(object);
                        break;

                    case OutputItemType.Extension:
                        this.SerializeExtensions(provider as IExtensible, false, true);
                        serializedExtensions = true;
                        break;

                    case OutputItemType.Text:
                        this.SerializeTextImpl(provider);
                        serializedText = true;
                        break;

                    default:
                        Debug.Assert(false, item.ItemType.ToString() + " is not handled.");
                        break;
                    }
                }

                // Serialize everything that hasn't been serialized yet. Order doesn't matter at this point.
                if (!serializedText)
                {
                    this.SerializeTextImpl(provider);
                }

                if (!serializedChildAsObject && children.TryGetValue(typeof(object), out unorderedChildren))
                {
                    this.SerializeChildImpl(unorderedChildren);
                }

                if (!serializedExtensions)
                {
                    this.SerializeExtensions(provider as IExtensible, false, true);
                }
            }

            if (name != null)
            {
                if (provider.HasXliffChildren || provider.HasXliffText)
                {
                    this.writer.WriteFullEndElement();
                }
                else
                {
                    this.writer.WriteEndElement();
                }
            }
        }
Exemplo n.º 4
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]);
                        }
                    }
                }
            }
        }
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.");
            }
        }