26 public class ThemeReader : IPluginBaseReader
29 private Stream stream;
63 public void Init(Stream stream,
Workbook workbook, IOptions readerOptions, Action<Stream, Workbook, string, IOptions, int?> inlinePluginHandler)
66 this.Workbook = workbook;
67 this.Options = readerOptions;
68 this.InlinePluginHandler = inlinePluginHandler;
81 ColorScheme colorScheme =
null;
82 using (XmlReader reader = XmlReader.Create(stream, XmlStreamUtils.CreateSettings()))
86 if (reader.NodeType != XmlNodeType.Element)
90 if (XmlStreamUtils.IsElement(reader,
"theme") && colorScheme ==
null)
92 string themeName = reader.GetAttribute(
"name");
93 Workbook.WorkbookTheme =
new Theme(themeName);
94 colorScheme =
new ColorScheme();
95 Workbook.WorkbookTheme.Colors = colorScheme;
97 else if (XmlStreamUtils.IsElement(reader,
"clrScheme") && colorScheme !=
null)
99 ReadClrScheme(reader, colorScheme);
108 throw new IOException(
"The XML entry could not be read from the input stream. Please see the inner exception:", ex);
116 private static void ReadClrScheme(XmlReader reader, ColorScheme colorScheme)
118 colorScheme.Name = reader.GetAttribute(
"name") ??
string.Empty;
119 using (XmlReader subtree = reader.ReadSubtree())
122 while (subtree.Read())
124 if (subtree.NodeType != XmlNodeType.Element)
128 string slot = subtree.LocalName;
129 IColor color = ReadColorEntry(subtree);
133 colorScheme.Dark1 = color;
136 colorScheme.Light1 = color;
139 colorScheme.Dark2 = color;
142 colorScheme.Light2 = color;
145 colorScheme.Accent1 = color;
148 colorScheme.Accent2 = color;
151 colorScheme.Accent3 = color;
154 colorScheme.Accent4 = color;
157 colorScheme.Accent5 = color;
160 colorScheme.Accent6 = color;
163 colorScheme.Hyperlink = color;
166 colorScheme.FollowedHyperlink = color;
178 private static IColor ReadColorEntry(XmlReader reader)
180 if (reader.IsEmptyElement)
184 int slotDepth = reader.Depth;
185 while (reader.Read())
187 if (reader.Depth == slotDepth)
191 if (reader.NodeType == XmlNodeType.Element)
193 if (reader.LocalName.Equals(
"sysClr", StringComparison.OrdinalIgnoreCase))
195 string val = reader.GetAttribute(
"val");
196 SystemColor systemColor =
new SystemColor
198 ColorValue = ParseSystemColor(val)
200 string lastColor = reader.GetAttribute(
"lastClr");
201 if (lastColor !=
null)
203 systemColor.LastColor = lastColor;
208 else if (reader.LocalName.Equals(
"srgbClr", StringComparison.OrdinalIgnoreCase))
210 SrgbColor color =
new SrgbColor
212 ColorValue = reader.GetAttribute(
"val")
228 private static SystemColor.Value ParseSystemColor(
string value)
230 if (
string.IsNullOrEmpty(value))
232 throw new IOException(
"The system color entry was null or empty");
236 return SystemColor.MapStringToValue(value);
240 throw new IOException(
"The system color entry '" + value +
"' could not be parsed", ex);
void Init(Stream stream, Workbook workbook, IOptions readerOptions, Action< Stream, Workbook, string, IOptions, int?> inlinePluginHandler)
Initialization method (interface implementation).
Action< Stream, Workbook, string, IOptions, int?> InlinePluginHandler
Reference to the ReaderPlugInHandler, to be used for post operations in the Execute method.