static IAttributesProvider InvokeBuildMetadataMethodFromMetadataProvider(MethodInfo method, Type metadataClassType, Type componentType, bool forFiltering) { IAttributesProvider result = forFiltering ? CreateFilteringMetadataBuilder(componentType) : CreateMetadataBuilder(componentType); method.Invoke(Activator.CreateInstance(metadataClassType), new[] { result }); return(result); }
static IAttributesProvider InvokeBuildMetadataStaticMethod(MethodInfo method, Type componentType, bool forFiltering) { IAttributesProvider result = forFiltering ? CreateFilteringMetadataBuilder(componentType) : CreateMetadataBuilder(componentType); method.Invoke(null, new[] { result }); return(result); }
static IEnumerable <Attribute> GetExternalAndFluentAPIAttributes(IAttributesProvider attributesProvider, string propertyName) { lock (attributesProvider) { var attributes = attributesProvider.GetAttributes(propertyName); var groupedAttributes = attributes.SelectMany(attr => GetAttributeTypes(attr).Select(x => new { type = x, value = attr })).GroupBy(x => x.type); var attributeCategories = groupedAttributes.GroupBy(g => g.Key.GetCustomAttributes(typeof(AttributeUsageAttribute), true).Cast <AttributeUsageAttribute>().Single().AllowMultiple).ToList(); var simpleAttributes = attributeCategories.Where(x => x.Key).SelectMany(x => x).SelectMany(x => x).Select(x => x.value); var uniqueAttributes = attributeCategories.Where(x => !x.Key).SelectMany(x => x).Select(x => x.Last()).Select(x => x.value); return(simpleAttributes.Concat(uniqueAttributes).Select(x => new AttributeReference(x)).GroupBy(x => x).Select(x => x.Key.Attribute).ToList()); } }
static IAttributesProvider GetExternalAndFluentAPIAttrbutesCore(Type componentType) { IEnumerable <IAttributesProvider> result = new IAttributesProvider[0]; foreach (var type in GetHierarchy(componentType)) { IEnumerable <Type> metadataClassType = DataAnnotationsAttributeHelper.GetMetadataClassType(type) .Return(x => new[] { x }, () => Enumerable.Empty <Type>()); IEnumerable <Type> externalMetadataClassTypes = GetMetadataTypes(MetadataLocator.Default, type); result = result.Concat(metadataClassType.Concat(externalMetadataClassTypes).GetProviders(type)); GetFluentAPIAttributesFromStaticMethod(type, type).Do(x => result = result.Concat(new[] { x })); } return(CompositeMetadataAttributesProvider.Create(result)); }
public static string GetVertexAttributes(object vertex, IAttributesProvider provider) { var attributes = provider.GetVertexAttributes(vertex); if (vertex is IAttributed) { foreach (var attribute in ((IAttributed)vertex).Attributes) { attributes.Add(attribute); } } return(attributes.GetAttributes()); }
static IAttributesProvider GetFluentAPIAttributesFromStaticMethod(Type metadataClassType, Type componentType) { MethodInfo buildMetadataMethod = GetBuildMetadataMethods(metadataClassType) .Where(x => GetBuildMetadataMethodEntityType(x) == componentType) .SingleOrDefault(); if (buildMetadataMethod == null) { return(null); } var key = new Tuple <Type, Type>(componentType, metadataClassType); IAttributesProvider result = CreateBuilder(componentType); buildMetadataMethod.Invoke(null, new[] { result }); return(result); }
static IAttributesProvider GetExternalAndFluentAPIAttributesCore(Type componentType, bool forFiltering) { IEnumerable <Type> hierarchy = componentType.Yield().Flatten(x => x.BaseType.YieldIfNotNull()).Reverse(); IEnumerable <IAttributesProvider> result = new IAttributesProvider[0]; foreach (var type in hierarchy) { IEnumerable <Type> metadataClassType = (forFiltering ? GetFilteringMetadataClassType(type) : GetMetadataClassType(type)) .Return(x => new[] { x }, () => Enumerable.Empty <Type>()); IEnumerable <Type> externalMetadataClassTypes = GetMetadataTypes(MetadataLocator.Default, type); result = result.Concat(metadataClassType.Concat(externalMetadataClassTypes).GetProviders(type, forFiltering)); (forFiltering ? GetFluentAPIFilteringAttributesFromStaticMethod(type, type) : GetFluentAPIAttributesFromStaticMethod(type, type)) .Do(x => result = result.Concat(new[] { x })); } return(CompositeMetadataAttributesProvider.Create(result)); }
static IAttributesProvider GetFluentAPIAttributes(Type metadataClassType, Type componentType) { bool isPublic = metadataClassType.IsPublic || metadataClassType.IsNestedPublic; if (metadataClassType.IsAbstract || !isPublic || metadataClassType.GetConstructor(new Type[0]) == null) { return(null); } Type metadatProviderInterfaceType = metadataClassType.GetInterfaces().SingleOrDefault(x => { Type expectedProviderType = componentType.IsEnum ? typeof(IEnumMetadataProvider <>) : typeof(IMetadataProvider <>); return(x.GetGenericTypeDefinition() == expectedProviderType && x.GetGenericArguments().Single() == componentType); }); if (metadatProviderInterfaceType == null) { return(null); } IAttributesProvider result = CreateBuilder(componentType); object provider = Activator.CreateInstance(metadataClassType); metadatProviderInterfaceType.GetMethod(BuildMetadataMethodName, BindingFlags.Instance | BindingFlags.Public).Invoke(provider, new[] { result }); return(result); }
static IAttributesProvider GetExtenalAndFluentAPIAttrbutesCore(Type componentType) { IEnumerable <IAttributesProvider> result = new IAttributesProvider[0]; while (componentType != null) { Type metadataClassType = DataAnnotationsAttributeHelper.GetMetadataClassType(componentType); if (metadataClassType != null) { result = result.Concat(GetAllMetadataAttributes(metadataClassType, componentType)); } Type[] externalMetadataClassTypes = MetadataLocator.Default.With(x => x.GetMetadataTypes(GetTypeOrGenericTypeDefinition(componentType))); if (externalMetadataClassTypes != null) { foreach (var externalMetadataClassType in externalMetadataClassTypes) { result = result.Concat(GetAllMetadataAttributes(externalMetadataClassType, componentType)); } } GetFluentAPIAttributesFromStaticMethod(componentType, componentType).Do(x => result = result.Concat(new[] { x })); componentType = componentType.BaseType; } return(new CompositeMetadataAttributesProvider(result.Where(x => x != null).ToArray())); }
/// <summary> /// Returns an array of all attributes defined on this member of the given attribute type. /// Returns an empty array if no attributes are defined on this member. /// </summary> /// <param name="inherit">If true, look in base classes for inherited custom attributes.[note: this parameter is ignored]</param> public static object[] GetCustomAttributes(IAttributesProvider member, Type attributeType, bool inherit) { var attributes = member.Attributes(); return GetCustomAttributes(attributeType, attributes); }
/// <summary> /// Returns an array of all attributes defined on this member. /// Returns an empty array if no attributes are defined on this member. /// </summary> /// <param name="inherit">If true, look in base classes for inherited custom attributes.</param> public static object[] GetCustomAttributes(IAttributesProvider member, bool inherit) { return GetCustomAttributes(member.Attributes()); }
/// <summary> /// are one or more attributes of the given type defined on this member. /// </summary> /// <param name="attributeType">The type of the custom attribute</param> /// <param name="inherit">If true, look in base classes for inherited custom attributes.</param> public static bool IsDefined(IAttributesProvider member, Type attributeType, bool inherit) { var attributes = member.Attributes(); return IsDefined(attributeType, attributes); }
/// <summary> /// Returns an array of all attributes defined on this member. /// Returns an empty array if no attributes are defined on this member. /// </summary> /// <param name="inherit">If true, look in base classes for inherited custom attributes.</param> public static object[] GetCustomAttributes(IAttributesProvider member, bool inherit) { return(GetCustomAttributes(member.Attributes())); }
/// <summary> /// Converts given graph into the dot language. /// </summary> /// <returns>During the conversion each node is assigned an id. Map of these ids is returned.</returns> public object[] Convert( TextWriter originalWriter, IGraph graph, IAttributesProvider additionalAttributesProvider) { var nodesMap = new List<object>(); var idsMap = new Dictionary<object, int>(); var writer = new IndentedTextWriter(originalWriter); writer.WriteLine("digraph g { "); var graphAttributes = new Dictionary<string, string>(); if (graph is IAttributed) { graphAttributes = new Dictionary<string,string>(((IAttributed) graph).Attributes); } // we don't need compound attribute if there are no sub-graphs if (graph.SubGraphs.Any()) { graphAttributes.Add("compound", "true"); } writer.WriteLine("graph [{0}];", graphAttributes.GetAttributes()); writer.Indent++; foreach (var vertex in graph.Vertices) { writer.WriteLine("{0} [ {1} ];", nodesMap.Count, GetVertexAttributes(vertex, additionalAttributesProvider)); idsMap.Add(vertex, nodesMap.Count); nodesMap.Add(vertex); } var subGraphs = graph.SubGraphs.ToArray(); for (int i = 0; i < subGraphs.Length; i++) { var subGraph = subGraphs[i]; writer.WriteLine("subgraph cluster{0} {{ ", nodesMap.Count); idsMap.Add(subGraph, nodesMap.Count); nodesMap.Add(subGraph); writer.Indent++; // In order to have clusters labels always on the top: // when the direction is from bottom to top, change label location to bottom, // which in this direction means top of the image string rankdir; if (graph is IAttributed && ((IAttributed)graph).Attributes.TryGetValue("rankdir", out rankdir) && rankdir == RankDirection.BottomToTop) { writer.WriteLine("graph [labelloc=b];"); } if (subGraph is IAttributed) { writer.WriteLine("graph [{0}];", ((IAttributed)subGraph).GetAttributes()); } foreach (var vertex in subGraph.Vertices) { writer.WriteLine("{0} [ {1} ];", nodesMap.Count, GetVertexAttributes(vertex, additionalAttributesProvider)); idsMap.Add(vertex, nodesMap.Count); nodesMap.Add(vertex); } writer.Indent--; writer.WriteLine("}; "); } foreach (var edge in graph.Edges) { IDictionary<string, string> attributes = new Dictionary<string, string>(); if (edge is IAttributed) { attributes = ((IAttributed)edge).Attributes; } if (edge.SourceArrow != null) { attributes["dir"] = "both"; } object edgeSource = edge.Source; object edgeDestination = edge.Destination; if (edgeSource is ISubGraph) { attributes["ltail"] = "cluster" + idsMap[edgeSource]; edgeSource = ((ISubGraph) edgeSource).Vertices.First(); } if (edgeDestination is ISubGraph) { attributes["lhead"] = "cluster" + idsMap[edgeDestination]; var subGraph = ((ISubGraph) edgeDestination); edgeDestination = subGraph.Vertices.FirstOrDefault(); if (edgeDestination == null) { throw new InvalidOperationException( "SubGraph must have at least one vertex. " + string.Format("This does not hold for {0} subgraph. ", subGraph.Label) + "Graphviz4Net cannot render empty subgraphs. "); } } attributes["comment"] = nodesMap.Count.ToString(); nodesMap.Add(edge); writer.WriteLine( "{0}{1} -> {2}{3} [ {4} ];", idsMap[edgeSource], edge.SourcePort == null ? string.Empty : ":\"" + edge.SourcePort + "\"", idsMap[edgeDestination], edge.DestinationPort == null ? string.Empty : ":\"" + edge.DestinationPort + "\"", attributes.GetAttributes()); } writer.Indent--; writer.WriteLine("}"); // end of the diagraph return nodesMap.ToArray(); }
public static string GetVertexAttributes(object vertex, IAttributesProvider provider) { var attributes = provider.GetVertexAttributes(vertex); if (vertex is IAttributed) { foreach (var attribute in ((IAttributed)vertex).Attributes) { attributes.Add(attribute); } } return attributes.GetAttributes(); }
/// <summary> /// Returns an array of all attributes defined on this member of the given attribute type. /// Returns an empty array if no attributes are defined on this member. /// </summary> /// <param name="inherit">If true, look in base classes for inherited custom attributes.[note: this parameter is ignored]</param> public static object[] GetCustomAttributes(IAttributesProvider member, Type attributeType, bool inherit) { var attributes = member.Attributes(); return(GetCustomAttributes(attributeType, attributes)); }
/// <summary> /// Converts given graph into the dot language. /// </summary> /// <returns>During the conversion each node is assigned an id. Map of these ids is returned.</returns> public object[] Convert( TextWriter originalWriter, IGraph graph, IAttributesProvider additionalAttributesProvider) { var nodesMap = new List <object>(); var idsMap = new Dictionary <object, int>(); var writer = new IndentedTextWriter(originalWriter); writer.WriteLine("digraph g { "); var graphAttributes = new Dictionary <string, string>(); if (graph is IAttributed) { graphAttributes = new Dictionary <string, string>(((IAttributed)graph).Attributes); } // we don't need compound attribute if there are no sub-graphs if (graph.SubGraphs.Any()) { graphAttributes.Add("compound", "true"); } writer.WriteLine("graph [{0}];", graphAttributes.GetAttributes()); writer.Indent++; foreach (var vertex in graph.Vertices) { writer.WriteLine("{0} [ {1} ];", nodesMap.Count, GetVertexAttributes(vertex, additionalAttributesProvider)); idsMap.Add(vertex, nodesMap.Count); nodesMap.Add(vertex); } var subGraphs = graph.SubGraphs.ToArray(); for (int i = 0; i < subGraphs.Length; i++) { var subGraph = subGraphs[i]; writer.WriteLine("subgraph cluster{0} {{ ", nodesMap.Count); idsMap.Add(subGraph, nodesMap.Count); nodesMap.Add(subGraph); writer.Indent++; // In order to have clusters labels always on the top: // when the direction is from bottom to top, change label location to bottom, // which in this direction means top of the image string rankdir; if (graph is IAttributed && ((IAttributed)graph).Attributes.TryGetValue("rankdir", out rankdir) && rankdir == RankDirection.BottomToTop) { writer.WriteLine("graph [labelloc=b];"); } if (subGraph is IAttributed) { writer.WriteLine("graph [{0}];", ((IAttributed)subGraph).GetAttributes()); } foreach (var vertex in subGraph.Vertices) { writer.WriteLine("{0} [ {1} ];", nodesMap.Count, GetVertexAttributes(vertex, additionalAttributesProvider)); idsMap.Add(vertex, nodesMap.Count); nodesMap.Add(vertex); } writer.Indent--; writer.WriteLine("}; "); } foreach (var edge in graph.Edges) { IDictionary <string, string> attributes = new Dictionary <string, string>(); if (edge is IAttributed) { attributes = ((IAttributed)edge).Attributes; } if (edge.SourceArrow != null) { attributes["dir"] = "both"; } object edgeSource = edge.Source; object edgeDestination = edge.Destination; if (edgeSource is ISubGraph) { attributes["ltail"] = "cluster" + idsMap[edgeSource]; edgeSource = ((ISubGraph)edgeSource).Vertices.First(); } if (edgeDestination is ISubGraph) { attributes["lhead"] = "cluster" + idsMap[edgeDestination]; var subGraph = ((ISubGraph)edgeDestination); edgeDestination = subGraph.Vertices.FirstOrDefault(); if (edgeDestination == null) { throw new InvalidOperationException( "SubGraph must have at least one vertex. " + string.Format("This does not hold for {0} subgraph. ", subGraph.Label) + "Graphviz4Net cannot render empty subgraphs. "); } } attributes["comment"] = nodesMap.Count.ToString(); nodesMap.Add(edge); writer.WriteLine( "{0}{1} -> {2}{3} [ {4} ];", idsMap[edgeSource], edge.SourcePort == null ? string.Empty : ":\"" + edge.SourcePort + "\"", idsMap[edgeDestination], edge.DestinationPort == null ? string.Empty : ":\"" + edge.DestinationPort + "\"", attributes.GetAttributes()); } writer.Indent--; writer.WriteLine("}"); // end of the diagraph return(nodesMap.ToArray()); }
/// <summary> /// are one or more attributes of the given type defined on this member. /// </summary> /// <param name="attributeType">The type of the custom attribute</param> /// <param name="inherit">If true, look in base classes for inherited custom attributes.</param> public static bool IsDefined(IAttributesProvider member, Type attributeType, bool inherit) { var attributes = member.Attributes(); return(IsDefined(attributeType, attributes)); }