NanoXLSX.Core 3.2.1
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 © 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.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
243 public bool RemoveEntityData(string plugInId, string entityId, string valueId)
244 {
245 if (!data.TryGetValue(plugInId, out Dictionary<string, Dictionary<string, DataEntry>> pluginData) ||
246 !pluginData.TryGetValue(entityId, out Dictionary<string, DataEntry> entityData) ||
247 !entityData.Remove(valueId))
248 {
249 return false;
250 }
251
252 if (entityData.Count == 0)
253 {
254 pluginData.Remove(entityId);
255 }
256 if (pluginData.Count == 0)
257 {
258 data.Remove(plugInId);
259 }
260 return true;
261 }
262
270 public bool RemoveEntityData(string plugInId, string entityId, int valueId)
271 {
272 string id = ParserUtils.ToString(valueId);
273 return RemoveEntityData(plugInId, entityId, id);
274 }
275
283 public bool RemoveEntityData(string plugInId, string entityId, object obj)
284 {
285 if (data.TryGetValue(plugInId, out Dictionary<string, Dictionary<string, DataEntry>> pluginData) &&
286 pluginData.TryGetValue(entityId, out Dictionary<string, DataEntry> entityData))
287 {
288 foreach (KeyValuePair<string, DataEntry> entry in entityData)
289 {
290 if (ReferenceEquals(entry.Value.Value, obj))
291 {
292 return RemoveEntityData(plugInId, entityId, entry.Key);
293 }
294 }
295 }
296 return false;
297 }
298
304 public void ClearEntityData(string plugInId, string entityId)
305 {
306 if (data.TryGetValue(plugInId, out Dictionary<string, Dictionary<string, DataEntry>> pluginData))
307 {
308 pluginData.Remove(entityId);
309 if (pluginData.Count == 0)
310 {
311 data.Remove(plugInId);
312 }
313 }
314 }
315
319 public void ClearTemporaryData()
320 {
321 foreach (KeyValuePair<string, Dictionary<string, Dictionary<string, DataEntry>>> pluginPair in data.ToList())
322 {
323 Dictionary<string, Dictionary<string, DataEntry>> pluginData = pluginPair.Value;
324 foreach (KeyValuePair<string, Dictionary<string, DataEntry>> entityPair in pluginData.ToList())
325 {
326 Dictionary<string, DataEntry> entityData = entityPair.Value;
327 List<string> keysToRemove = new List<string>();
328 foreach (KeyValuePair<string, DataEntry> kvp in entityData)
329 {
330 if (!kvp.Value.Persistent)
331 {
332 keysToRemove.Add(kvp.Key);
333 }
334 }
335 foreach (string key in keysToRemove)
336 {
337 entityData.Remove(key);
338 }
339 if (entityData.Count == 0)
340 {
341 pluginData.Remove(entityPair.Key);
342 }
343 }
344 if (pluginData.Count == 0)
345 {
346 data.Remove(pluginPair.Key);
347 }
348 }
349 }
350
354 public void ClearData()
355 {
356 data.Clear();
357 }
358
362 private class DataEntry
363 {
367 public object Value { get; }
371 public bool Persistent { get; }
372
378 public DataEntry(object value, bool persistent)
379 {
380 Value = value;
381 Persistent = persistent;
382 }
383 }
384
385 }
386}