Example #1
0
 private void TryExtractAttribute(AttributeType propertyAttributes, PropertyInfo propInfo)
 {
     if (propertyAttributes == AttributeType.None)
     {
         return;
     }
     if (propertyAttributes.HasFlag(AttributeType.ColumnMap))
     {
         TryAddAttribute(propInfo, ColumnMapAttributes, (cm, prop) => cm.PropertyName = prop.Name);
     }
     if (propertyAttributes.HasFlag(AttributeType.RenameColumn))
     {
         TryAddAttribute(propInfo, RenameColumnAttributes, (cm, prop) => cm.CurrentName = prop.Name);
     }
     if (propertyAttributes.HasFlag(AttributeType.DistinctColumn))
     {
         TryAddAttribute(propInfo, DistinctColumnAttributes, (cm, prop) => cm.DistinctPropertyName = prop.Name);
     }
     if (propertyAttributes.HasFlag(AttributeType.KeyColumn))
     {
         TryAddAttribute(propInfo, KeyColumnAttributes, (cm, prop) => cm.KeyPropertyName = prop.Name);
     }
     if (propertyAttributes.HasFlag(AttributeType.AggregateColumn))
     {
         TryAddAttribute(propInfo, AggregateColumnAttributes, (cm, prop) => cm.AggregatedValuePropName = prop.Name);
     }
     if (propertyAttributes.HasFlag(AttributeType.GroupColumn))
     {
         TryAddAttribute(propInfo, GroupColumnAttributes, (cm, prop) => cm.GroupPropNameInOutput = prop.Name);
     }
     if (propertyAttributes.HasFlag(AttributeType.MatchColumn))
     {
         TryAddAttribute(propInfo, MatchColumnAttributes, (cm, prop) => cm.LookupSourcePropertyName = prop.Name);
     }
     if (propertyAttributes.HasFlag(AttributeType.RetrieveColumn))
     {
         TryAddAttribute(propInfo, RetrieveColumnAttributes, (cm, prop) => cm.LookupSourcePropertyName = prop.Name);
     }
     if (propertyAttributes.HasFlag(AttributeType.IdColumn))
     {
         TryAddAttribute(propInfo, IdColumnAttributes, (cm, prop) => cm.IdPropertyName = prop.Name);
     }
     if (propertyAttributes.HasFlag(AttributeType.UpdateColumn))
     {
         TryAddAttribute(propInfo, UpdateColumnAttributes, (cm, prop) => cm.UpdatePropertyName = prop.Name);
     }
     if (propertyAttributes.HasFlag(AttributeType.CompareColumn))
     {
         TryAddAttribute(propInfo, CompareColumnAttributes, (cm, prop) => cm.ComparePropertyName = prop.Name);
     }
     if (propertyAttributes.HasFlag(AttributeType.DeleteColumn))
     {
         TryAddAttribute(propInfo, DeleteColumnAttributes, (cm, prop) => cm.DeletePropertyName = prop.Name);
     }
 }
Example #2
0
        public bool TryGetAttribute(string xaml, string attributeName, AttributeType attributeTypesToCheck, out AttributeType attributeType, out int index, out int length, out string value)
        {
            try
            {
                var xamlSpan = xaml.AsSpan();

                if (attributeTypesToCheck.HasFlag(AttributeType.Inline))
                {
                    if (string.IsNullOrWhiteSpace(xaml))
                    {
                        System.Diagnostics.Debugger.Break();
                        this.Logger.RecordError($"xaml not passed to `TryGetAttribute({xaml}, {attributeName}, {attributeTypesToCheck})`");

                        attributeType = AttributeType.None;
                        index         = -1;
                        length        = 0;
                        value         = string.Empty;
                        return(false);
                    }

                    var searchText = $"{attributeName}=\"";

                    var tbIndex = xaml.IndexOf(searchText, StringComparison.Ordinal);

                    if (tbIndex >= 0 && char.IsWhiteSpace(xaml[tbIndex - 1]))
                    {
                        var tbEnd = xaml.IndexOf("\"", tbIndex + searchText.Length, StringComparison.Ordinal);

                        attributeType = AttributeType.Inline;
                        index         = tbIndex;
                        length        = tbEnd - tbIndex + 1;
                        value         = xaml.Substring(tbIndex + searchText.Length, tbEnd - tbIndex - searchText.Length);
                        return(true);
                    }
                }

                var elementName = GetElementName(xamlSpan);

                if (attributeTypesToCheck.HasFlag(AttributeType.Element))
                {
                    var searchText = $"<{elementName}.{attributeName}>";

                    var startIndex = xamlSpan.IndexOf(searchText.AsSpan(), StringComparison.Ordinal);

                    if (startIndex > -1)
                    {
                        var closingElement = $"</{elementName}.{attributeName}>";
                        var endPos         = xaml.IndexOf(closingElement, startIndex, StringComparison.Ordinal);

                        if (endPos > -1)
                        {
                            attributeType = AttributeType.Element;
                            index         = startIndex;
                            length        = endPos - startIndex + closingElement.Length;
                            value         = xaml.Substring(startIndex + searchText.Length, endPos - startIndex - searchText.Length);
                            return(true);
                        }
                    }
                }

                if (attributeTypesToCheck.HasFlag(AttributeType.DefaultValue))
                {
                    var endOfOpening   = xamlSpan.IndexOf(">".AsSpan(), StringComparison.Ordinal);
                    var startOfClosing = xamlSpan.IndexOf($"</{elementName}>".AsSpan(), StringComparison.Ordinal);

                    if (startOfClosing > 0 && startOfClosing > endOfOpening)
                    {
                        var defaultValue = xaml.Substring(endOfOpening + 1, startOfClosing - endOfOpening - 1);

                        if (!string.IsNullOrWhiteSpace(defaultValue) && !defaultValue.TrimStart().StartsWith("<"))
                        {
                            attributeType = AttributeType.DefaultValue;
                            index         = 0;
                            length        = xaml.Length;
                            value         = defaultValue;
                            return(true);
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                this?.Logger?.RecordException(exc);
            }

            attributeType = AttributeType.None;
            index         = -1;
            length        = 0;
            value         = string.Empty;
            return(false);
        }
        public bool TryGetAttribute(string xaml, string attributeName, AttributeType attributeTypesToCheck, out AttributeType attributeType, out int index, out int length, out string value)
        {
            if (attributeTypesToCheck.HasFlag(AttributeType.Inline))
            {
                var searchText = $"{attributeName}=\"";

                var tbIndex = xaml.IndexOf(searchText, StringComparison.Ordinal);

                if (tbIndex >= 0)
                {
                    var tbEnd = xaml.IndexOf("\"", tbIndex + searchText.Length, StringComparison.Ordinal);

                    attributeType = AttributeType.Inline;
                    index         = tbIndex;
                    length        = tbEnd - tbIndex + 1;
                    value         = xaml.Substring(tbIndex + searchText.Length, tbEnd - tbIndex - searchText.Length);
                    return(true);
                }
            }

            var elementName = xaml.Substring(1, xaml.IndexOfAny(new[] { ' ', '>' }) - 1);

            if (attributeTypesToCheck.HasFlag(AttributeType.Element))
            {
                var searchText = $"<{elementName}.{attributeName}>";

                var startIndex = xaml.IndexOf(searchText, StringComparison.Ordinal);

                if (startIndex > -1)
                {
                    var closingElement = $"</{elementName}.{attributeName}>";
                    var endPos         = xaml.IndexOf(closingElement, startIndex, StringComparison.Ordinal);

                    if (endPos > -1)
                    {
                        attributeType = AttributeType.Element;
                        index         = startIndex;
                        length        = endPos - startIndex + closingElement.Length;
                        value         = xaml.Substring(startIndex + searchText.Length, endPos - startIndex - searchText.Length);
                        return(true);
                    }
                }
            }

            if (attributeTypesToCheck.HasFlag(AttributeType.DefaultValue))
            {
                var endOfOpening   = xaml.IndexOf(">");
                var closingTag     = $"</{elementName}>";
                var startOfClosing = xaml.IndexOf(closingTag, StringComparison.Ordinal);

                if (startOfClosing > 0 && startOfClosing > endOfOpening)
                {
                    var defaultValue = xaml.Substring(endOfOpening + 1, startOfClosing - endOfOpening - 1);

                    if (!string.IsNullOrWhiteSpace(defaultValue) && !defaultValue.TrimStart().StartsWith("<"))
                    {
                        attributeType = AttributeType.DefaultValue;
                        index         = 0;
                        length        = xaml.Length;
                        value         = defaultValue;
                        return(true);
                    }
                }
            }

            attributeType = AttributeType.None;
            index         = -1;
            length        = 0;
            value         = string.Empty;
            return(false);
        }