示例#1
0
 private void HandleConstant(string name, ConstantAttribute value)
 {
     if (value == null)
     {
         return;
     }
     With(name, value.ConstantValue?.ToString(), value.Context);
 }
        private void AddAttributes(ITagAttributeSetter tagReflection, ITag tag)
        {
            while (!_helper.IsAhead(CLOSING_TOKENS))
            {
//                _helper.Read(TokenType.Seperator);
                _helper.Next();
                ReadWhiteSpace(_helper);
                if (_helper.IsAhead(CLOSING_TOKENS))
                {
                    return;
                }
                var keyToken = _helper.Read(TokenType.Regular);
                var key      = tagReflection.SupportNaturalLanguage?LanguageHelper.CamelCaseAttribute(keyToken.Contents): keyToken.Contents;
                _helper.Read(TagLibConstants.FIELD_ASSIGNMENT);
                var value    = _helper.Read(TokenType.Literal).Contents;
                var existing = tagReflection[key];
                if (!existing?.AllowOverWrite ?? false)
                {
                    throw TagException.PropertyAlReadySet(key).Decorate(keyToken.Context);
                }
                if (string.IsNullOrEmpty(value))
                {
                    tagReflection[key] = new ConstantAttribute("", tag)
                    {
                        AttributeName = key, Context = keyToken.Context
                    };
                    continue;
                }
                if (!value.Contains("${"))
                {
                    tagReflection[key] = new ConstantAttribute(value, tag)
                    {
                        AttributeName = key, Context = keyToken.Context, ResourceLocator = _locator
                    };
                    continue;
                }
                var offSet = _helper.Current.Context;
                var attr   = new TemplateAttribute(new InternalFormatter(new TagLibParserFactoryAdapter(this), _expressionLib, value, false, _locator, offSet).Parse())
                {
                    AttributeName = key
                };
                tagReflection[key] = attr;
            }
        }
        /// <summary>
        /// Applies attribute configurations to the map.
        /// </summary>
        /// <param name="memberMap">The member map.</param>
        protected virtual void ApplyAttributes(MemberMap memberMap)
        {
            var member = memberMap.Data.Member;

            //if( member.GetCustomAttribute( typeof( IndexAttribute ) ) is IndexAttribute indexAttribute )
            IndexAttribute indexAttribute = member.GetCustomAttributes(typeof(IndexAttribute), false).FirstOrDefault() as IndexAttribute;

            if (indexAttribute != null)
            {
                memberMap.Data.Index      = indexAttribute.Index;
                memberMap.Data.IndexEnd   = indexAttribute.IndexEnd;
                memberMap.Data.IsIndexSet = true;
            }

            //if( member.GetCustomAttribute( typeof( NameAttribute ) ) is NameAttribute nameAttribute )
            NameAttribute nameAttribute = member.GetCustomAttributes(typeof(NameAttribute), false).FirstOrDefault() as NameAttribute;

            if (nameAttribute != null)
            {
                memberMap.Data.Names.Clear();
                memberMap.Data.Names.AddRange(nameAttribute.Names);
                memberMap.Data.IsNameSet = true;
            }

            //if( member.GetCustomAttribute( typeof( NameIndexAttribute ) ) is NameIndexAttribute nameIndexAttribute )
            NameIndexAttribute nameIndexAttribute = member.GetCustomAttributes(typeof(NameIndexAttribute), false).FirstOrDefault() as NameIndexAttribute;

            if (nameIndexAttribute != null)
            {
                memberMap.Data.NameIndex = nameIndexAttribute.NameIndex;
            }


            //if( member.GetCustomAttribute( typeof( IgnoreAttribute ) ) is IgnoreAttribute ignoreAttribute )
            IgnoreAttribute ignoreAttribute = member.GetCustomAttributes(typeof(IgnoreAttribute), false).FirstOrDefault() as IgnoreAttribute;

            if (ignoreAttribute != null)
            {
                memberMap.Data.Ignore = true;
            }

            //if( member.GetCustomAttribute( typeof( DefaultAttribute ) ) is DefaultAttribute defaultAttribute )
            DefaultAttribute defaultAttribute = member.GetCustomAttributes(typeof(DefaultAttribute), false).FirstOrDefault() as DefaultAttribute;

            if (defaultAttribute != null)
            {
                memberMap.Data.Default      = defaultAttribute.Default;
                memberMap.Data.IsDefaultSet = true;
            }

            //if( member.GetCustomAttribute( typeof( ConstantAttribute ) ) is ConstantAttribute constantAttribute )
            ConstantAttribute constantAttribute = member.GetCustomAttributes(typeof(ConstantAttribute), false).FirstOrDefault() as ConstantAttribute;

            if (constantAttribute != null)
            {
                memberMap.Data.Constant      = constantAttribute.Constant;
                memberMap.Data.IsConstantSet = true;
            }

            //if( member.GetCustomAttribute( typeof( TypeConverterAttribute ) ) is TypeConverterAttribute typeConverterAttribute )
            TypeConverterAttribute typeConverterAttribute = member.GetCustomAttributes(typeof(TypeConverterAttribute), false).FirstOrDefault() as TypeConverterAttribute;

            if (typeConverterAttribute != null)
            {
                memberMap.Data.TypeConverter = typeConverterAttribute.TypeConverter;
            }

            //if( member.GetCustomAttribute( typeof( CultureInfoAttribute ) ) is CultureInfoAttribute cultureInfoAttribute )
            CultureInfoAttribute cultureInfoAttribute = member.GetCustomAttributes(typeof(CultureInfoAttribute), false).FirstOrDefault() as CultureInfoAttribute;

            if (cultureInfoAttribute != null)
            {
                memberMap.Data.TypeConverterOptions.CultureInfo = cultureInfoAttribute.CultureInfo;
            }

            //if( member.GetCustomAttribute( typeof( DateTimeStylesAttribute ) ) is DateTimeStylesAttribute dateTimeStylesAttribute )
            DateTimeStylesAttribute dateTimeStylesAttribute = member.GetCustomAttributes(typeof(DateTimeStylesAttribute), false).FirstOrDefault() as DateTimeStylesAttribute;

            if (dateTimeStylesAttribute != null)
            {
                memberMap.Data.TypeConverterOptions.DateTimeStyle = dateTimeStylesAttribute.DateTimeStyles;
            }

            //if( member.GetCustomAttribute( typeof( NumberStylesAttribute ) ) is NumberStylesAttribute numberStylesAttribute )
            NumberStylesAttribute numberStylesAttribute = member.GetCustomAttributes(typeof(NumberStylesAttribute), false).FirstOrDefault() as NumberStylesAttribute;

            if (numberStylesAttribute != null)
            {
                memberMap.Data.TypeConverterOptions.NumberStyle = numberStylesAttribute.NumberStyles;
            }

            FormatAttribute formatAttribute = member.GetCustomAttributes(typeof(FormatAttribute), false).FirstOrDefault() as FormatAttribute;

            if (formatAttribute != null)
            {
                memberMap.Data.TypeConverterOptions.Formats = formatAttribute.Formats;
            }

            BooleanTrueValuesAttribute booleanTrueValuesAttribute = member.GetCustomAttributes(typeof(BooleanTrueValuesAttribute), false).FirstOrDefault() as BooleanTrueValuesAttribute;

            if (booleanTrueValuesAttribute != null)
            {
                memberMap.Data.TypeConverterOptions.BooleanTrueValues.Clear();
                memberMap.Data.TypeConverterOptions.BooleanTrueValues.AddRange(booleanTrueValuesAttribute.TrueValues);
            }

            BooleanFalseValuesAttribute booleanFalseValuesAttribute = member.GetCustomAttributes(typeof(BooleanFalseValuesAttribute), false).FirstOrDefault() as BooleanFalseValuesAttribute;

            if (booleanFalseValuesAttribute != null)
            {
                memberMap.Data.TypeConverterOptions.BooleanFalseValues.Clear();
                memberMap.Data.TypeConverterOptions.BooleanFalseValues.AddRange(booleanFalseValuesAttribute.FalseValues);
            }

            NullValuesAttribute nullValuesAttribute = member.GetCustomAttributes(typeof(NullValuesAttribute), false).FirstOrDefault()  as NullValuesAttribute;

            if (nullValuesAttribute != null)
            {
                memberMap.Data.TypeConverterOptions.NullValues.Clear();
                memberMap.Data.TypeConverterOptions.NullValues.AddRange(nullValuesAttribute.NullValues);
            }
        }