示例#1
0
        private string FormatValue(Entity valueProvider, formatTemplate template)
        {
            Entity original = CheckTarget();
            IEnumerable <(string, Entity)> values          = GetValues();
            IEnumerable <string>           formattedValues = values.Select(Format).ToArray();
            string formattedValue = string.Join(template.seperator, formattedValues);

            return(ApplyPrefixSuffix(formattedValue, true));

            Entity CheckTarget()
            {
                Entity result = valueProvider;

                while (result.Owner != null && result.Owner.Type == EntityKeys.FormatKey)
                {
                    result = result.Owner.Owner;
                }

                if (!string.IsNullOrEmpty(template.target) &&
                    !template.target.Equals(result.Type, StringComparison.OrdinalIgnoreCase))
                {
                    throw new FormatTargetMismatchException(template, result.Type);
                }

                return(result);
            }

            IEnumerable <(string, Entity)> GetValues()
            {
                return(template.multiplicity == multiplicity.OneOrMore
                           ? (template.Split?.Any() == true
                                  ? valueProvider.Value <string>().Split(template.Split.Select(Resolve).ToArray(), StringSplitOptions.None).Select(s => (s, (Entity)null))
                                  : valueProvider.Select(e => (e.Value <string>(), e)))
                           : new[] { (valueProvider.Value <string>(), (Entity)null) });
        public override Entity Resolve(Entity owner, string key, bool fallback = false)
        {
            if (key == EntityKeys.FormatKey)
            {
                return(owner.Create(key, templateRepository.FormatTemplates.Cast <object>().ToArray()));
            }

            if (owner.Value <Match>() != null)
            {
                Match match = owner.Value <Match>();
                if (int.TryParse(key, out int index))
                {
                    if (match.Groups.Count > index)
                    {
                        return(owner.Create(key, match.Groups[index].Value));
                    }
                }
                else if (owner.Value <Regex>()?.GetGroupNames()
                         .Contains(key, StringComparer.OrdinalIgnoreCase)
                         == true)
                {
                    string realKey = owner.Value <Regex>()?.GetGroupNames()
                                     .First(n => n.Equals(key, StringComparison.OrdinalIgnoreCase));
                    return(owner.Create(key, match.Groups[realKey].Value));
                }
                throw new ContentProviderException(key, owner);
            }

            if (key == EntityKeys.ValueKey)
            {
                return(owner.Create(key, owner.Values <FormatValueAccess>()
                                    .OrderByDescending(a => a.Created)
                                    .First().Value));
            }

            IEnumerable <formatTemplate> templates = owner.Values <formatTemplate>();
            formatTemplate template = templates.FirstOrDefault(t => t.name.Equals(key, StringComparison.OrdinalIgnoreCase));

            if (template != null)
            {
                return(owner.Create(key, new Func <string>(() => FormatValue(owner.Owner, template))));
            }
            throw new ContentProviderException(key, owner);
        }
示例#3
0
 public FormatTargetMismatchException(formatTemplate template, string entityType) : base(string.Format(CultureInfo.InvariantCulture, ExceptionTexts.FormatTargetMismatch, template?.name, entityType, template.target))
 {
 }
        private string FormatValue(Entity valueProvider, formatTemplate template)
        {
            Entity original                      = CheckTarget();
            IEnumerable <string> values          = GetValues();
            IEnumerable <string> formattedValues = values.Select(Format);
            string formattedValue                = string.Join(template.seperator, formattedValues);

            return(ApplyPrefixSuffix(formattedValue, true));

            Entity CheckTarget()
            {
                Entity result = valueProvider;

                while (result.Owner != null && result.Owner.Type == EntityKeys.FormatKey)
                {
                    result = result.Owner.Owner;
                }

                if (!string.IsNullOrEmpty(template.target) &&
                    !template.target.Equals(result.Type, StringComparison.OrdinalIgnoreCase))
                {
                    throw new FormatTargetMismatchException(template, result.Type);
                }

                return(result);
            }

            IEnumerable <string> GetValues()
            {
                return(template.multiplicity == multiplicity.OneOrMore
                           ? (template.Split?.Any() == true
                                  ? valueProvider.Value <string>().Split(template.Split.Select(Resolve).ToArray(), StringSplitOptions.None)
                                  : valueProvider.Select(e => e.Value <string>()))
                           : new[] { valueProvider.Value <string>() });

                string Resolve(string split)
                {
                    return(resolver.Resolve(split, original));
                }
            }

            string ApplyPrefixSuffix(string mainString, bool onlyAggregated)
            {
                prefixSuffixFormat perValuePrefix = template.Prefix?.FirstOrDefault(p => p.onlyaggregated == onlyAggregated);
                string             prefix         = perValuePrefix != null
                                    ? resolver.Resolve(perValuePrefix.value, valueProvider.Empty)
                                    : string.Empty;

                prefixSuffixFormat perValueSuffix = template.Suffix?.FirstOrDefault(p => p.onlyaggregated == onlyAggregated);
                string             suffix         = perValueSuffix != null
                                    ? resolver.Resolve(perValueSuffix.value, valueProvider.Empty)
                                    : string.Empty;

                return(prefix + mainString + suffix);
            }

            string Format(string value)
            {
                string result = ApplyPatterns();

                return(ApplyPrefixSuffix(result, false));

                string ApplyPatterns()
                {
                    foreach (conditionalConversionFormat format in template.ConditionalConversion ?? Enumerable.Empty <conditionalConversionFormat>())
                    {
                        if (bool.TryParse(resolver.Resolve(format.condition, original), out bool applies) &&
                            applies)
                        {
                            using (original.AddTemporaryDataSource(new FormatValueAccess(value)))
                            {
                                return(resolver.Resolve(format.template, original));
                            }
                        }
                    }

                    foreach (regexConversionFormat format in template.RegexConversion ?? Enumerable.Empty <regexConversionFormat>())
                    {
                        Match match = CreateMatch(format.pattern, format.ignorecase, out Regex regex);
                        if (match.Success)
                        {
                            return(resolver.Resolve(format.converted, valueProvider.Create("regex", match, regex)));
                        }
                    }

                    foreach (templateConversionFormat format in template.TemplateConversion ?? Enumerable.Empty <templateConversionFormat>())
                    {
                        Match match = CreateMatch(format.pattern, format.ignorecase, out Regex _);
                        if (match.Success)
                        {
                            using (original.AddTemporaryDataSource(new FormatValueAccess(value)))
                            {
                                return(resolver.Resolve(format.template, original));
                            }
                        }
                    }

                    return(value);

                    Match CreateMatch(string pattern, bool ignorecase, out Regex regex)
                    {
                        if (!pattern.StartsWith("^", StringComparison.Ordinal))
                        {
                            pattern = $"^{pattern}";
                        }

                        if (!pattern.EndsWith("$", StringComparison.Ordinal))
                        {
                            pattern += "$";
                        }
                        RegexOptions options = ignorecase ? RegexOptions.IgnoreCase : RegexOptions.None;

                        regex = new Regex(pattern, options);
                        return(regex.Match(value));
                    }
                }
            }
        }