/// <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;
        }
Пример #2
0
        /// <summary>
        /// Returns the set of attributes by looking for <see cref="SchemaEntityAttribute"/> attributes and
        /// returning the CSharp property Name and attribute data.
        /// </summary>
        /// <param name="type">The type to reflect on.</param>
        /// <param name="provider">A provider that can return custom values when inheriting values.</param>
        /// <returns>A dictionary whose type is the CSharp property Name, and the value is an object that
        /// stores the attribute data.</returns>
        /// <param name="outputResolver">The resolver to use when the output of properties is determined at runtime.
        /// </param>
        public static IDictionary<string, AttributeData> GetSchemaAttributes(
            Type type,
            IInheritanceInfoProvider provider,
            IOutputResolver outputResolver)
        {
            Dictionary<string, AttributeData> result;
            Dictionary<string, IEnumerable<ExplicitOutputDependencyAttribute>> outputDependencyMap;
            IEnumerable<SchemaAttributes> attributes;

            result = new Dictionary<string, AttributeData>();
            outputDependencyMap = new Dictionary<string, IEnumerable<ExplicitOutputDependencyAttribute>>();

            attributes = ReflectorCache.Instance.GetSchemaAttributes(type);
            foreach (SchemaAttributes entry in attributes)
            {
                if (entry.Schema != null)
                {
                    AttributeData data;

                    if (entry.DefaultValue != null)
                    {
                        data = new AttributeData(
                                                 type.Name,
                                                 entry.Schema.Name,
                                                 entry.Schema.Requirement != Requirement.Required,
                                                 entry.DefaultValue.Value);
                    }
                    else
                    {
                        data = new AttributeData(
                                                 type.Name,
                                                 entry.Schema.Name,
                                                 entry.Schema.Requirement != Requirement.Required);
                    }

                    data.IgnoreNamespace = true;
                    result.Add(entry.Name, data);

                    data.ConverterTypeName = (entry.Converter != null) ? entry.Converter.TypeName : null;
                    if (entry.HasValueIndicator != null)
                    {
                        data.HasValueIndicator = Reflector.GetSingletonViaActivator(entry.HasValueIndicator.TypeName) as IHasValueIndicator;
                    }

                    foreach (InheritValueAttribute inheritance in entry.InheritanceList)
                    {
                        InheritanceInfo inheritanceInfo;

                        data.InheritValue = true;

                        if (inheritance.Inheritance == Inheritance.Callback)
                        {
                            Debug.Assert(provider != null, "IInheritanceProvider is null.");
                            inheritanceInfo = provider.GetInheritanceInfo(entry.Name);
                            Debug.Assert(inheritanceInfo != null, "inheritanceInfo is null.");
                        }
                        else
                        {
                            inheritanceInfo = new InheritanceInfo(
                                                                  inheritance.AncestorType,
                                                                  inheritance.AncestorPropertyName);
                        }

                        data.InheritanceList.Add(inheritanceInfo);
                    }

                    // Store the mapping of attribute name to the names of other attributes it depends on for output.
                    if (entry.ExplicitOutputDependencies != null)
                    {
                        outputDependencyMap[entry.Name] = entry.ExplicitOutputDependencies;
                    }
                }
            }

            // Now that all attributes have been stored, add the explicit output dependencies referencing the
            // attributes directly.
            foreach (KeyValuePair<string, IEnumerable<ExplicitOutputDependencyAttribute>> pair in outputDependencyMap)
            {
                foreach (ExplicitOutputDependencyAttribute attribute in pair.Value)
                {
                    if (attribute.Property == null)
                    {
                        result[pair.Key].OutputResolver = outputResolver;
                    }
                    else
                    {
                        result[pair.Key].ExplicitOutputDependencies.Add(attribute.Property, result[attribute.Property]);
                    }
                }
            }

            return result;
        }