NanoXLSX.Core 3.2.1
Loading...
Searching...
No Matches
RelationshipCatalog.cs
1/*
2 * NanoXLSX is a small .NET library to generate and read XLSX (Microsoft Excel 2007 or newer) files in an easy and native way
3 * Copyright Raphael Stoeckli © 2026
4 * This library is licensed under the MIT License.
5 * You find a copy of the license in project folder or on: http://opensource.org/licenses/MIT
6 */
7
8using System;
9using System.Collections.Generic;
10using System.Collections.ObjectModel;
11
12namespace NanoXLSX.Internal
13{
17 internal sealed class RelationshipCatalog
18 {
19 private readonly List<RelationshipInfo> relationships = new List<RelationshipInfo>();
20 private readonly List<RelationshipDiscoveryIssue> issues = new List<RelationshipDiscoveryIssue>();
21 private readonly Dictionary<string, Dictionary<string, RelationshipInfo>> relationshipsBySource
22 = new Dictionary<string, Dictionary<string, RelationshipInfo>>(StringComparer.Ordinal);
23 private readonly ReadOnlyCollection<RelationshipInfo> readOnlyRelationships;
24 private readonly ReadOnlyCollection<RelationshipDiscoveryIssue> readOnlyIssues;
25
29 public IReadOnlyList<RelationshipInfo> Relationships { get { return readOnlyRelationships; } }
30
34 public IReadOnlyList<RelationshipDiscoveryIssue> Issues { get { return readOnlyIssues; } }
35
39 public bool IsComplete { get { return issues.Count == 0; } }
40
44 public RelationshipCatalog()
45 {
46 readOnlyRelationships = relationships.AsReadOnly();
47 readOnlyIssues = issues.AsReadOnly();
48 }
49
55 public bool TryAdd(RelationshipInfo relationship)
56 {
57 string sourcePartPath = relationship.SourcePartPath ?? string.Empty;
58 if (!relationshipsBySource.TryGetValue(sourcePartPath, out Dictionary<string, RelationshipInfo> sourceRelationships))
59 {
60 sourceRelationships = new Dictionary<string, RelationshipInfo>(StringComparer.Ordinal);
61 relationshipsBySource.Add(sourcePartPath, sourceRelationships);
62 }
63 if (sourceRelationships.ContainsKey(relationship.Id))
64 {
65 return false;
66 }
67 sourceRelationships.Add(relationship.Id, relationship);
68 relationships.Add(relationship);
69 return true;
70 }
71
76 public void AddIssue(RelationshipDiscoveryIssue issue)
77 {
78 issues.Add(issue);
79 }
80
87 public RelationshipInfo GetBySourceAndId(string sourcePartPath, string relationshipId)
88 {
89 string normalizedSourcePartPath = sourcePartPath ?? string.Empty;
90 if (relationshipsBySource.TryGetValue(normalizedSourcePartPath, out Dictionary<string, RelationshipInfo> sourceRelationships)
91 && relationshipId != null
92 && sourceRelationships.TryGetValue(relationshipId, out RelationshipInfo relationship))
93 {
94 return relationship;
95 }
96 return null;
97 }
98
103 public IReadOnlyList<RelationshipInfo> GetByType(string documentType)
104 {
105 List<RelationshipInfo> matches = new List<RelationshipInfo>();
106 foreach (RelationshipInfo relationship in relationships)
107 {
108 if (string.Equals(relationship.Type, documentType, StringComparison.Ordinal))
109 {
110 matches.Add(relationship);
111 }
112 }
113 return matches.AsReadOnly();
114 }
115 }
116}