Пример #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ElementInfo"/> class.
        /// </summary>
        /// <param name="name">The Xml name of the element.</param>
        /// <param name="element">The element this class describes.</param>
        public ElementInfo(XmlNameInfo name, XliffElement element)
            : base(name)
        {
            ArgValidator.Create(element, "element").IsNotNull();

            this.Element = element;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ElementInfo"/> class.
        /// </summary>
        /// <param name="name">The Xml name of the element.</param>
        /// <param name="element">The element this class describes.</param>
        public ElementInfo(XmlNameInfo name, XliffElement element)
            : base(name)
        {
            ArgValidator.Create(element, "element").IsNotNull();

            this.Element = element;
        }
        /// <summary>
        /// Creates an instance of this class and populates its members using reflection on the specified
        /// <see cref="XliffElement"/>
        /// </summary>
        /// <param name="element">The element to reflect upon.</param>
        /// <returns>An instance of this class with information about the elements children and attributes.</returns>
        public static IElementInformation Create(XliffElement element)
        {
            ElementInformationFromReflection result;

            result = new ElementInformationFromReflection();
            result.ChildMap = Reflector.GetSchemaChildren(element.GetType());
            result.AttributeMap = Reflector.GetSchemaAttributes(element.GetType(), element as IInheritanceInfoProvider, element as IOutputResolver);

            return result;
        }
        /// <summary>
        /// Validates that the <see cref="XliffElement"/> is not already used as a child for another
        /// <see cref="XliffElement"/>. If the element is a child, then an <see cref="ElementReuseException"/>
        /// is thrown.
        /// </summary>
        /// <param name="element">The element to check.</param>
        public static void ParentIsNull(XliffElement element)
        {
            if ((element != null) && (element.Parent != null))
            {
                string message;

                message = string.Format(Properties.Resources.ArgValidator_ElementReused_Format, element.GetType().Name);
                throw new ElementReuseException(message);
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="AttributeDataProvider"/> class.
        /// </summary>
        /// <param name="host">The element that hosts the attribute.</param>
        /// <param name="property">The name of the property associated with the attribute.</param>
        /// <param name="data">The raw attribute data being referenced by this class.</param>
        internal AttributeDataProvider(XliffElement host, string property, AttributeData data)
            : base(data)
        {
            Debug.Assert(host != null, "Host cannot be null.");
            Debug.Assert(data != null, "Data cannot be null.");
            Debug.Assert(!string.IsNullOrEmpty(property), "Property cannot be null.");

            this.data = data;
            this.host = host;
            this.property = property;
        }
Пример #6
0
        /// <summary>
        /// Handler called when getting the custom Directionality value from a spanning code element. The value
        /// depends on the hierarchy of the ancestry.
        /// </summary>
        /// <param name="element">The element that the property is for.</param>
        /// <param name="property">The name of the property that is to be returned.</param>
        /// <returns>The custom value.</returns>
        private object LanguageInheritanceHandler(XliffElement element, string property)
        {
            object result;
            XliffDocument document;

            Debug.Assert(property == "Language", "Property name is not supported.");

            result = null;
            document = element.FindAncestor<XliffDocument>();
            if (document != null)
            {
                result = document.SourceLanguage;
            }

            return result;
        }
Пример #7
0
        /// <summary>
        /// Handler called when getting the custom Directionality value from a spanning code element. The value
        /// depends on the hierarchy of the ancestry.
        /// </summary>
        /// <param name="element">The element that the property is for.</param>
        /// <param name="property">The name of the property that is to be returned.</param>
        /// <returns>The custom value.</returns>
        private object LanguageInheritanceHandler(XliffElement element, string property)
        {
            object        result;
            XliffDocument document;

            Debug.Assert(property == "Language", "Property name is not supported.");

            result   = null;
            document = element.FindAncestor <XliffDocument>();
            if (document != null)
            {
                result = document.TargetLanguage;
            }

            return(result);
        }
Пример #8
0
        /// <summary>
        /// Deserializes text into XLIFF elements under the specified source element.
        /// </summary>
        /// <param name="source">The element to deserialize content to.</param>
        /// <param name="text">The text to deserialize.</param>
        public static void DeserializeText(XliffElement source, string text)
        {
            XliffReader reader;

            reader = new XliffReader();
            reader.currentElementState = new ElementState(source);

            using (MemoryStream stream = new MemoryStream())
            {
                // Don't wrap this in using because it may dispose of the stream which is one too many.
                TextWriter writer;

                writer = new StreamWriter(stream);
                writer.Write(text);

                writer.Flush();
                stream.Seek(0, SeekOrigin.Begin);
                reader.reader = XmlReader.Create(stream);
                reader.DeserializeXmlContent(source);
            }
        }
Пример #9
0
 /// <summary>
 /// Serializes an <see cref="XliffElement"/> to the stream using the specified Xml element Name.
 /// </summary>
 /// <param name="name">The name of the XmlElement to create.</param>
 /// <param name="element">The content to serialize.</param>
 private void SerializeImpl(string name, XliffElement element)
 {
     this.SerializeImpl(name, null, null, element, null);
 }
Пример #10
0
 /// <summary>
 /// Serializes an <see cref="XliffElement"/> to the stream using the specified Xml element Name.
 /// </summary>
 /// <param name="name">The name of the XmlElement to create.</param>
 /// <param name="element">The content to serialize.</param>
 private void SerializeImpl(string name, XliffElement element)
 {
     this.SerializeImpl(name, null, null, element, null);
 }
Пример #11
0
        /// <summary>
        /// Deserializes text into XLIFF elements under the specified source element.
        /// </summary>
        /// <param name="source">The element to deserialize content to.</param>
        /// <param name="text">The text to deserialize.</param>
        public static void DeserializeText(XliffElement source, string text)
        {
            XliffReader reader;

            reader = new XliffReader();
            reader.currentElementState = new ElementState(source);

            using (MemoryStream stream = new MemoryStream())
            {
                // Don't wrap this in using because it may dispose of the stream which is one too many.
                TextWriter writer;

                writer = new StreamWriter(stream);
                writer.Write(text);

                writer.Flush();
                stream.Seek(0, SeekOrigin.Begin);
                reader.reader = XmlReader.Create(stream);
                reader.DeserializeXmlContent(source);
            }
        }
Пример #12
0
        /// <summary>
        /// Deserializes the Xml content in the reader to the specified element.
        /// </summary>
        /// <param name="root">The element to insert deserialized XLIFF elements to.</param>
        private void DeserializeXmlContent(XliffElement root)
        {
            this.currentElementState = new ElementState(root);

            this.reader.MoveToContent();

            do
            {
                switch (this.reader.NodeType)
                {
                    case XmlNodeType.CDATA:
                        if (this.currentElementState.Consumer is IResourceStringContentContainer)
                        {
                            this.currentElementState.Consumer.AddXliffChild(
                                                              new XmlNameInfo((string)null),
                                                              new CDataTag(this.reader.Value));
                        }

                        break;

                    case XmlNodeType.Comment:
                        if (this.currentElementState.Consumer is IResourceStringContentContainer)
                        {
                            this.currentElementState.Consumer.AddXliffChild(
                                                              new XmlNameInfo((string)null),
                                                              new CommentTag(this.reader.Value));
                        }

                        break;

                    case XmlNodeType.Element:
                        {
                            ElementState currentState;

                            currentState = this.currentElementState;
                            this.DeserializeElement();

                            if (object.ReferenceEquals(this.currentElementState, currentState))
                            {
                                // The element won't have any children so validate it.
                                this.ValidateElementState(this.currentElementState);
                            }
                        }

                        break;

                    case XmlNodeType.EndElement:
                        this.ValidateElementState(this.currentElementState);
                        this.currentElementState = this.elementStack.Pop();
                        break;

                    case XmlNodeType.Text:
                        {
                            int ordinal;

                            ordinal = this.currentElementState.GetOrdinal(OutputItemType.Text, typeof(string));
                            if (ordinal < this.currentElementState.LastOrdinalRead)
                            {
                                string message;

                                message = string.Format(
                                                        Properties.Resources.XliffElement_ElementOutOfOrder_Format,
                                                        this.currentElementState.Consumer.GetType().Name,
                                                        typeof(string).Name,
                                                        this.reader.NamespaceURI,
                                                        this.reader.LocalName);
                                throw new FormatException(message);
                            }

                            this.currentElementState.Consumer.SetXliffText(this.reader.Value);
                        }

                        break;

                    case XmlNodeType.ProcessingInstruction:
                        if (this.currentElementState.Consumer is IResourceStringContentContainer)
                        {
                            this.currentElementState.Consumer.AddXliffChild(
                                                              new XmlNameInfo((string)null),
                                                              new ProcessingInstructionTag(this.reader.Name, this.reader.Value));
                        }

                        break;

                    case XmlNodeType.SignificantWhitespace:
                        // SignificantWhitespace is used when xml:space=preserve is added to an XmlElement and the
                        // whitespace wasn't added by the Xml writer.
                        if (this.currentElementState.Consumer is IResourceStringContentContainer)
                        {
                            IResourceStringContentContainer container;

                            container = (IResourceStringContentContainer)this.currentElementState.Consumer;
                            container.Text.Add(new PlainText(this.reader.Value));
                        }

                        break;

                    case XmlNodeType.Whitespace:
                        // Whitespace was added by the Xml writer and is used between XmlElements so it should be ignored.
                        break;
                }
            }
            while (this.reader.Read());
        }
Пример #13
0
 /// <summary>
 /// Verifies that the expected item is present and matches a set of items obtained from
 /// another target. The <typeparamref name="TItem"/> represents the type of items to compare from
 /// the enumerations. Only items of these types are compared. All other types are ignored.
 /// </summary>
 /// <typeparam name="TItem">The type of items to extract from <paramref name="expectedItem"/>
 /// and <paramref name="actualItems"/>.</typeparam>
 /// <param name="expectedItem">The expected item.</param>
 /// <param name="actualItems">The items to compare against the expected.</param>
 /// <param name="name">The XLIFF name the keys of the <paramref name="actualItems"/> should match.</param>
 public static void VerifyItems <TItem>(XliffElement expectedItem,
                                        IEnumerable <ElementInfo> actualItems,
                                        string name) where TItem : XliffElement
 {
     TestUtilities.VerifyItems <TItem>(new XliffElement[] { expectedItem }, actualItems, name);
 }
Пример #14
0
        /// <summary>
        /// Deserializes the Xml content in the reader to the specified element.
        /// </summary>
        /// <param name="root">The element to insert deserialized XLIFF elements to.</param>
        private void DeserializeXmlContent(XliffElement root)
        {
            this.currentElementState = new ElementState(root);

            this.reader.MoveToContent();

            do
            {
                switch (this.reader.NodeType)
                {
                case XmlNodeType.CDATA:
                    if (this.currentElementState.Consumer is IResourceStringContentContainer)
                    {
                        this.currentElementState.Consumer.AddXliffChild(
                            new XmlNameInfo((string)null),
                            new CDataTag(this.reader.Value));
                    }

                    break;

                case XmlNodeType.Comment:
                    if (this.currentElementState.Consumer is IResourceStringContentContainer)
                    {
                        this.currentElementState.Consumer.AddXliffChild(
                            new XmlNameInfo((string)null),
                            new CommentTag(this.reader.Value));
                    }

                    break;

                case XmlNodeType.Element:
                {
                    ElementState currentState;

                    currentState = this.currentElementState;
                    this.DeserializeElement();

                    if (object.ReferenceEquals(this.currentElementState, currentState))
                    {
                        // The element won't have any children so validate it.
                        this.ValidateElementState(this.currentElementState);
                    }
                }

                break;

                case XmlNodeType.EndElement:
                    this.ValidateElementState(this.currentElementState);
                    this.currentElementState = this.elementStack.Pop();
                    break;

                case XmlNodeType.Text:
                {
                    int ordinal;

                    ordinal = this.currentElementState.GetOrdinal(OutputItemType.Text, typeof(string));
                    if (ordinal < this.currentElementState.LastOrdinalRead)
                    {
                        string message;

                        message = string.Format(
                            Properties.Resources.XliffElement_ElementOutOfOrder_Format,
                            this.currentElementState.Consumer.GetType().Name,
                            typeof(string).Name,
                            this.reader.NamespaceURI,
                            this.reader.LocalName);
                        throw new FormatException(message);
                    }

                    this.currentElementState.Consumer.SetXliffText(this.reader.Value);
                }

                break;

                case XmlNodeType.ProcessingInstruction:
                    if (this.currentElementState.Consumer is IResourceStringContentContainer)
                    {
                        this.currentElementState.Consumer.AddXliffChild(
                            new XmlNameInfo((string)null),
                            new ProcessingInstructionTag(this.reader.Name, this.reader.Value));
                    }

                    break;

                case XmlNodeType.SignificantWhitespace:
                    // SignificantWhitespace is used when xml:space=preserve is added to an XmlElement and the
                    // whitespace wasn't added by the Xml writer.
                    if (this.currentElementState.Consumer is IResourceStringContentContainer)
                    {
                        IResourceStringContentContainer container;

                        container = (IResourceStringContentContainer)this.currentElementState.Consumer;
                        container.Text.Add(new PlainText(this.reader.Value));
                    }

                    break;

                case XmlNodeType.Whitespace:
                    // Whitespace was added by the Xml writer and is used between XmlElements so it should be ignored.
                    break;
                }
            }while (this.reader.Read());
        }