30 private bool capturePhoneticCharacters;
31 private readonly List<PhoneticInfo> phoneticsInfo;
32 private MemoryStream stream;
65 phoneticsInfo =
new List<PhoneticInfo>();
78 public void Init(MemoryStream stream,
Workbook workbook, IOptions readerOptions, Action<MemoryStream, Workbook, string, IOptions, int?> inlinePluginHandler)
81 this.Workbook = workbook;
82 this.Options = readerOptions;
83 this.InlinePluginHandler = inlinePluginHandler;
84 if (readerOptions is ReaderOptions options)
86 this.capturePhoneticCharacters = options.EnforcePhoneticCharacterImport;
100 XmlDocument xr =
new XmlDocument
104 using (XmlReader reader = XmlReader.Create(stream,
new XmlReaderSettings() { XmlResolver =
null }))
107 StringBuilder sb =
new StringBuilder();
108 foreach (XmlNode node
in xr.DocumentElement.ChildNodes)
110 if (node.LocalName.Equals(
"si", StringComparison.OrdinalIgnoreCase))
113 GetTextToken(node, ref sb);
114 if (capturePhoneticCharacters)
130 throw new IOException(
"The XML entry could not be read from the " + nameof(stream) +
". Please see the inner exception:", ex);
139 private void GetTextToken(XmlNode node, ref StringBuilder sb)
141 if (node.LocalName.Equals(
"rPh", StringComparison.OrdinalIgnoreCase))
143 if (capturePhoneticCharacters && !
string.IsNullOrEmpty(node.InnerText))
145 string start = node.Attributes.GetNamedItem(
"sb").InnerText;
146 string end = node.Attributes.GetNamedItem(
"eb").InnerText;
147 phoneticsInfo.Add(
new PhoneticInfo(node.InnerText, start, end));
152 if (node.LocalName.Equals(
"t", StringComparison.OrdinalIgnoreCase) && !
string.IsNullOrEmpty(node.InnerText))
154 sb.Append(node.InnerText);
156 if (node.HasChildNodes)
158 foreach (XmlNode childNode
in node.ChildNodes)
160 GetTextToken(childNode, ref sb);
170 private string ProcessPhoneticCharacters(StringBuilder sb)
172 if (phoneticsInfo.Count == 0)
174 return sb.ToString();
176 string text = sb.ToString();
177 StringBuilder sb2 =
new StringBuilder();
178 int currentTextIndex = 0;
179 foreach (PhoneticInfo info
in phoneticsInfo)
181 sb2.Append(text.Substring(currentTextIndex, info.StartIndex + info.Length - currentTextIndex));
182 sb2.Append(
'(').Append(info.Value).Append(
')');
183 currentTextIndex = info.StartIndex + info.Length;
185 sb2.Append(text.Substring(currentTextIndex));
187 phoneticsInfo.Clear();
188 return sb2.ToString();
199 sealed class PhoneticInfo
204 public string Value {
get;
private set; }
208 public int StartIndex {
get;
private set; }
212 public int Length {
get;
private set; }
220 public PhoneticInfo(
string value,
string start,
string end)
223 StartIndex = ParserUtils.ParseInt(start);
224 Length = ParserUtils.ParseInt(end) - StartIndex;
void Init(MemoryStream stream, Workbook workbook, IOptions readerOptions, Action< MemoryStream, Workbook, string, IOptions, int?> inlinePluginHandler)
Initialization method (interface implementation).
Action< MemoryStream, Workbook, string, IOptions, int?> InlinePluginHandler
Reference to the ReaderPlugInHandler, to be used for post operations in the Execute method.