NanoXLSX.Reader 3.0.0-rc.2
Loading...
Searching...
No Matches
ThemeReader.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;
9using System.IO;
10using System.Xml;
12using NanoXLSX.Interfaces.Plugin;
14using NanoXLSX.Registry;
15using NanoXLSX.Registry.Attributes;
16using NanoXLSX.Themes;
17using IOException = NanoXLSX.Exceptions.IOException;
18
20{
24 [NanoXlsxPlugIn(PlugInUUID = PlugInUUID.ThemeReader)]
25 public class ThemeReader : IPlugInReader
26 {
27
28 private MemoryStream stream;
29
30 #region properties
34 public Workbook Workbook { get; set; }
35 #endregion
36
37 #region constructors
41 internal ThemeReader()
42 {
43 }
44 #endregion
45
46 #region methods
53 public void Init(MemoryStream stream, Workbook workbook, IOptions readerOptions)
54 {
55 this.stream = stream;
56 this.Workbook = workbook;
57 }
58
63 public void Execute()
64 {
65 try
66 {
67 using (stream) // Close after processing
68 {
69 XmlDocument xr = new XmlDocument() { XmlResolver = null };
70 using (XmlReader reader = XmlReader.Create(stream, new XmlReaderSettings() { XmlResolver = null }))
71 {
72 xr.Load(reader);
73 string prefix = ReaderUtils.DiscoverPrefix(xr, "theme");
74 XmlNodeList themes = ReaderUtils.GetElementsByTagName(xr, "theme", prefix);
75 string themeName = ReaderUtils.GetAttribute(themes[0], "name"); // If this fails, something is completely wrong
76 Workbook.WorkbookTheme = new Theme(themeName);
77 ColorScheme colorScheme = new ColorScheme();
78 Workbook.WorkbookTheme.Colors = colorScheme;
79 XmlNodeList colors = ReaderUtils.GetElementsByTagName(xr, "clrScheme", prefix);
80
81 foreach (XmlNode color in colors)
82 {
83 string colorSchemeName = ReaderUtils.GetAttribute(color, "name", "");
84 Workbook.WorkbookTheme.Colors.Name = colorSchemeName;
85 XmlNodeList colorNodes = color.ChildNodes;
86 foreach (XmlNode colorNode in colorNodes)
87 {
88 string name = colorNode.LocalName;
89 switch (name)
90 {
91 case "dk1":
92 colorScheme.Dark1 = ParseColor(colorNode.ChildNodes);
93 break;
94 case "lt1":
95 colorScheme.Light1 = ParseColor(colorNode.ChildNodes);
96 break;
97 case "dk2":
98 colorScheme.Dark2 = ParseColor(colorNode.ChildNodes);
99 break;
100 case "lt2":
101 colorScheme.Light2 = ParseColor(colorNode.ChildNodes);
102 break;
103 case "accent1":
104 colorScheme.Accent1 = ParseColor(colorNode.ChildNodes);
105 break;
106 case "accent2":
107 colorScheme.Accent2 = ParseColor(colorNode.ChildNodes);
108 break;
109 case "accent3":
110 colorScheme.Accent3 = ParseColor(colorNode.ChildNodes);
111 break;
112 case "accent4":
113 colorScheme.Accent4 = ParseColor(colorNode.ChildNodes);
114 break;
115 case "accent5":
116 colorScheme.Accent5 = ParseColor(colorNode.ChildNodes);
117 break;
118 case "accent6":
119 colorScheme.Accent6 = ParseColor(colorNode.ChildNodes);
120 break;
121 case "hlink":
122 colorScheme.Hyperlink = ParseColor(colorNode.ChildNodes);
123 break;
124 case "folHlink":
125 colorScheme.FollowedHyperlink = ParseColor(colorNode.ChildNodes);
126 break;
127 }
128
129 }
130 }
131 RederPlugInHandler.HandleInlineQueuePlugins(ref stream, Workbook, PlugInUUID.ThemeInlineReader);
132 }
133 }
134 }
135 catch (Exception ex)
136 {
137 throw new IOException("The XML entry could not be read from the input stream. Please see the inner exception:", ex);
138 }
139 }
140
146 private static IColor ParseColor(XmlNodeList childNodes)
147 {
148 foreach (XmlNode node in childNodes)
149 {
150 if (node.LocalName == "sysClr")
151 {
152 SystemColor.Value value = ParseSystemColor(node);
153 SystemColor systemColor = new SystemColor
154 {
155 ColorValue = value
156 };
157 string lastColor = ReaderUtils.GetAttribute(node, "lastClr");
158 if (lastColor != null)
159 {
160 systemColor.LastColor = lastColor;
161 }
162 return systemColor;
163 }
164 else if (node.LocalName == "srgbClr")
165 {
166 SrgbColor srgbColor = new SrgbColor
167 {
168 ColorValue = ReaderUtils.GetAttribute(node, "val")
169 };
170 return srgbColor;
171 }
172 }
173 return null;
174 }
175
182 private static SystemColor.Value ParseSystemColor(XmlNode innerNode)
183 {
184 string value = ReaderUtils.GetAttribute(innerNode, "val");
185 if (string.IsNullOrEmpty(value))
186 {
187 throw new IOException("The system color entry was null or empty");
188 }
189 try
190 {
191 return SystemColor.MapStringToValue(value);
192 }
193 catch (Exception ex)
194 {
195 throw new IOException("The system color entry '" + value + "' could not be parsed", ex);
196 }
197 }
198 #endregion
199 }
200}
Static class with common util methods, used during reading XLSX files.
static string GetAttribute(XmlNode node, string targetName, string fallbackValue=null)
Gets the XML attribute of the passed XML node by its name.
Class representing a reader for theme definitions of XLSX files.
void Execute()
Method to execute the main logic of the plug-in (interface implementation).
Workbook Workbook
Workbook reference where read data is stored (should not be null).
void Init(MemoryStream stream, Workbook workbook, IOptions readerOptions)
Initialization method (interface implementation).
Exceptions.IOException IOException