NanoXLSX.Core 3.0.0-rc.3
Loading...
Searching...
No Matches
AuxiliaryData.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 © 2025
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.Collections.Generic;
9using System.Linq;
10using NanoXLSX.Utils;
11
12namespace NanoXLSX
13{
17 internal class AuxiliaryData
18 {
22 private const string DEFAULT_ENTITY_ID = "";
23
24 // Structure: PlugInId => EntityId => SubEntityId => value
25 private readonly Dictionary<string, Dictionary<string, Dictionary<string, DataEntry>>> data
26 = new Dictionary<string, Dictionary<string, Dictionary<string, DataEntry>>>();
27
36 public void SetData(string plugInId, int valueId, object value, bool persistent = false)
37 {
38 string id = ParserUtils.ToString(valueId);
39 SetData(plugInId, id, value, persistent);
40 }
41
50 public void SetData(string plugInId, int entityId, string valueId, object value, bool persistent = false)
51 {
52 string id = ParserUtils.ToString(entityId);
53 SetData(plugInId, id, valueId, value, persistent);
54 }
55
63 public void SetData(string plugInId, string valueId, object value, bool persistent = false)
64 {
65 SetData(plugInId, DEFAULT_ENTITY_ID, valueId, value, persistent);
66 }
67
76 public void SetData(string plugInId, string entityId, int valueId, object value, bool persistent = false)
77 {
78 string id = ParserUtils.ToString(valueId);
79 SetData(plugInId, entityId, id, value, persistent);
80 }
81
90 public void SetData(string plugInId, string entityId, string valueId, object value, bool persistent = false)
91 {
92 if (!data.TryGetValue(plugInId, out Dictionary<string, Dictionary<string, DataEntry>> pluginData))
93 {
94 pluginData = new Dictionary<string, Dictionary<string, DataEntry>>();
95 data[plugInId] = pluginData;
96 }
97
98 if (!pluginData.TryGetValue(entityId, out Dictionary<string, DataEntry> entityData))
99 {
100 entityData = new Dictionary<string, DataEntry>();
101 pluginData[entityId] = entityData;
102 }
103
104 entityData[valueId] = new DataEntry(value, persistent);
105 }
106
113 public object GetData(string plugInId, int valueId)
114 {
115 string id = ParserUtils.ToString(valueId);
116 return GetData(plugInId, id);
117 }
118
125 public object GetData(string plugInId, string valueId)
126 {
127 return GetData(plugInId, DEFAULT_ENTITY_ID, valueId);
128 }
129
137 public object GetData(string plugInId, int entityId, string valueId)
138 {
139 string id = ParserUtils.ToString(entityId);
140 return GetData(plugInId, id, valueId);
141 }
142
150 public object GetData(string plugInId, string entityId, string valueId)
151 {
152 if (data.TryGetValue(plugInId, out Dictionary<string, Dictionary<string, DataEntry>> pluginData) &&
153 pluginData.TryGetValue(entityId, out Dictionary<string, DataEntry> entityData) &&
154 entityData.TryGetValue(valueId, out DataEntry entry))
155 {
156 return entry.Value;
157 }
158 return null;
159 }
160
168 public T GetData<T>(string plugInId, int valueId)
169 {
170 string id = ParserUtils.ToString(valueId);
171 return GetData<T>(plugInId, id);
172 }
173
181 public T GetData<T>(string plugInId, string valueId)
182 {
183 return GetData<T>(plugInId, DEFAULT_ENTITY_ID, valueId);
184 }
185
194 public T GetData<T>(string plugInId, string entityId, int valueId)
195 {
196 string id = ParserUtils.ToString(valueId);
197 return GetData<T>(plugInId, entityId, id);
198 }
199
208 public T GetData<T>(string plugInId, string entityId, string valueId)
209 {
210 object value = GetData(plugInId, entityId, valueId);
211 return value is T ? (T)value : default;
212 }
213
214 public List<T> GetDataList<T>(string plugInId)
215 {
216 return GetDataList<T>(plugInId, DEFAULT_ENTITY_ID);
217 }
218
219 public List<T> GetDataList<T>(string plugInId, string entityId)
220 {
221 List<T> result = new List<T>();
222 if (data.TryGetValue(plugInId, out Dictionary<string, Dictionary<string, DataEntry>> pluginData) &&
223 pluginData.TryGetValue(entityId, out Dictionary<string, DataEntry> entityData))
224 {
225 foreach (KeyValuePair<string, DataEntry> entry in entityData)
226 {
227 if (entry.Value.Value is T value)
228 {
229 result.Add(value);
230 }
231 }
232 }
233 return result;
234 }
235
239 public void ClearTemporaryData()
240 {
241 foreach (KeyValuePair<string, Dictionary<string, Dictionary<string, DataEntry>>> pluginPair in data.ToList())
242 {
243 Dictionary<string, Dictionary<string, DataEntry>> pluginData = pluginPair.Value;
244 foreach (KeyValuePair<string, Dictionary<string, DataEntry>> entityPair in pluginData.ToList())
245 {
246 Dictionary<string, DataEntry> entityData = entityPair.Value;
247 List<string> keysToRemove = new List<string>();
248 foreach (KeyValuePair<string, DataEntry> kvp in entityData)
249 {
250 if (!kvp.Value.Persistent)
251 {
252 keysToRemove.Add(kvp.Key);
253 }
254 }
255 foreach (string key in keysToRemove)
256 {
257 entityData.Remove(key);
258 }
259 if (entityData.Count == 0)
260 {
261 pluginData.Remove(entityPair.Key);
262 }
263 }
264 if (pluginData.Count == 0)
265 {
266 data.Remove(pluginPair.Key);
267 }
268 }
269 }
270
274 public void ClearData()
275 {
276 data.Clear();
277 }
278
282 private class DataEntry
283 {
287 public object Value { get; }
291 public bool Persistent { get; }
292
298 public DataEntry(object value, bool persistent)
299 {
300 Value = value;
301 Persistent = persistent;
302 }
303 }
304
305 }
306}