예제 #1
0
        /// <summary>
        /// Stores an attribute in an extension. If no appropriate extension handler was found, a general purpose
        /// one will be created.
        /// </summary>
        /// <param name="extensible">The object that contains the extensions.</param>
        /// <returns>A result indicating whether the attribute was stored.</returns>
        private SetAttributeResult StoreAttributeExtension(IExtensible extensible)
        {
            SetAttributeResult result;

            result = SetAttributeResult.InvalidAttribute;
            if (this.settings.IncludeExtensions)
            {
                if (this.reader.Prefix == NamespacePrefixes.XmlNamespace)
                {
                    result = SetAttributeResult.ReservedName;
                }
                else if ((extensible != null) && extensible.SupportsAttributeExtensions)
                {
                    ExtensionNameInfo info;
                    IExtensionHandler handler;

                    info = new ExtensionNameInfo(this.reader.Prefix, this.reader.NamespaceURI, this.reader.LocalName);
                    if (this.handlers.Value.TryGetValue(this.reader.NamespaceURI, out handler) ||
                        this.handlers.Value.TryGetValue(XliffReader.DefaultHandlerKey, out handler))
                    {
                        IExtensionAttribute attribute;

                        attribute = handler.CreateAttribute(info, this.reader.Value);
                        if (handler.StoreAttribute(extensible, attribute))
                        {
                            result = SetAttributeResult.Success;
                        }
                    }
                    else
                    {
                        Debug.Assert(false, "Default handler was not found.");
                    }
                }
            }

            return(result);
        }
예제 #2
0
        /// <summary>
        /// Deserializes the element at the reader's current position as a new <see cref="XliffElement"/>.
        /// </summary>
        private void DeserializeElement()
        {
            IXliffDataConsumer newElement;
            XmlNameInfo        name;
            bool handlerCreated;
            bool isEmpty;

            handlerCreated = false;

            // Create a new element based on the XLIFF Name. Some elements don't necessarily have XliffElements
            // (ex. Notes) so false is returned.
            name       = new XmlNameInfo(this.reader.Prefix, this.reader.NamespaceURI, this.reader.LocalName);
            newElement = this.currentElementState.Consumer.CreateXliffElement(name);
            if (newElement == null)
            {
                // Don't store native XLIFF elements as extensions.
                if (this.settings.IncludeExtensions &&
                    !Utilities.IsCoreNamespace(this.reader.NamespaceURI) &&
                    !Utilities.IsModuleNamespace(this.reader.NamespaceURI))
                {
                    IExtensible extensible;

                    extensible = this.currentElementState.Consumer as IExtensible;
                    if ((extensible != null) && extensible.SupportsElementExtensions && this.handlers.IsValueCreated)
                    {
                        IExtensionHandler handler;

                        if (this.handlers.Value.TryGetValue(this.reader.NamespaceURI, out handler) ||
                            this.handlers.Value.TryGetValue(XliffReader.DefaultHandlerKey, out handler))
                        {
                            ExtensionNameInfo extensionName;
                            XliffElement      createdElement;

                            extensionName  = new ExtensionNameInfo(name.Prefix, name.Namespace, name.LocalName);
                            createdElement = handler.CreateElement(extensionName);
                            handler.StoreElement(extensible, new ElementInfo(name, createdElement));

                            newElement     = createdElement;
                            handlerCreated = true;
                        }
                        else
                        {
                            Debug.Assert(false, "Default handler was not found.");
                        }
                    }
                }

                if (newElement == null)
                {
                    string message;

                    message = string.Format(
                        Properties.Resources.XliffElement_UnknownElement_Format,
                        this.reader.Name,
                        this.currentElementState.Consumer.GetType().Name);
                    throw new FormatException(message);
                }
                else
                {
                    int ordinal;

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

                        message = string.Format(
                            Properties.Resources.XliffElement_ElementOutOfOrder_Format,
                            this.currentElementState.Consumer.GetType().Name,
                            newElement.GetType().Name,
                            name.Namespace,
                            name.LocalName);
                        throw new FormatException(message);
                    }

                    this.currentElementState.LastOrdinalRead = ordinal;
                }
            }
            else
            {
                int ordinal;

                ordinal = this.currentElementState.GetOrdinal(OutputItemType.Child, newElement.GetType());
                if (ordinal < this.currentElementState.LastOrdinalRead)
                {
                    string message;

                    message = string.Format(
                        Properties.Resources.XliffElement_ElementOutOfOrder_Format,
                        this.currentElementState.Consumer.GetType().Name,
                        newElement.GetType().Name,
                        name.Namespace,
                        name.LocalName);
                    throw new FormatException(message);
                }

                this.currentElementState.LastOrdinalRead = ordinal;
            }

            // Check if empty element before reading attributes because reading attributes moves the reader.
            isEmpty = this.reader.IsEmptyElement;

            Debug.Assert(newElement != null, "newElement is null.");
            this.DeserializeAttributes(newElement);

            // Extensions are not stored directly as children.
            if (!handlerCreated)
            {
                this.currentElementState.Consumer.AddXliffChild(name, newElement);
            }

            if (!isEmpty)
            {
                this.elementStack.Push(this.currentElementState);
                this.currentElementState = new ElementState(newElement);
            }
            else
            {
                this.ValidateElementState(new ElementState(newElement));
            }
        }