29 private bool capturePhoneticCharacters;
30 private readonly List<PhoneticInfo> phoneticsInfo;
31 private MemoryStream stream;
57 phoneticsInfo =
new List<PhoneticInfo>();
69 public void Init(MemoryStream stream,
Workbook workbook, IOptions readerOptions)
72 this.workbook = workbook;
75 this.capturePhoneticCharacters = options.EnforcePhoneticCharacterImport;
89 XmlDocument xr =
new XmlDocument
93 using (XmlReader reader = XmlReader.Create(stream,
new XmlReaderSettings() { XmlResolver =
null }))
96 StringBuilder sb =
new StringBuilder();
97 foreach (XmlNode node
in xr.DocumentElement.ChildNodes)
99 if (node.LocalName.Equals(
"si", StringComparison.OrdinalIgnoreCase))
102 GetTextToken(node, ref sb);
103 if (capturePhoneticCharacters)
113 RederPlugInHandler.HandleInlineQueuePlugins(ref stream,
Workbook, PlugInUUID.SharedStringsInlineReader);
119 throw new IOException(
"The XML entry could not be read from the " + nameof(stream) +
". Please see the inner exception:", ex);
128 private void GetTextToken(XmlNode node, ref StringBuilder sb)
130 if (node.LocalName.Equals(
"rPh", StringComparison.OrdinalIgnoreCase))
132 if (capturePhoneticCharacters && !
string.IsNullOrEmpty(node.InnerText))
134 string start = node.Attributes.GetNamedItem(
"sb").InnerText;
135 string end = node.Attributes.GetNamedItem(
"eb").InnerText;
136 phoneticsInfo.Add(
new PhoneticInfo(node.InnerText, start, end));
141 if (node.LocalName.Equals(
"t", StringComparison.OrdinalIgnoreCase) && !
string.IsNullOrEmpty(node.InnerText))
143 sb.Append(node.InnerText);
145 if (node.HasChildNodes)
147 foreach (XmlNode childNode
in node.ChildNodes)
149 GetTextToken(childNode, ref sb);
159 private string ProcessPhoneticCharacters(StringBuilder sb)
161 if (phoneticsInfo.Count == 0)
163 return sb.ToString();
165 string text = sb.ToString();
166 StringBuilder sb2 =
new StringBuilder();
167 int currentTextIndex = 0;
168 foreach (PhoneticInfo info
in phoneticsInfo)
170 sb2.Append(text.Substring(currentTextIndex, info.StartIndex + info.Length - currentTextIndex));
171 sb2.Append(
'(').Append(info.Value).Append(
')');
172 currentTextIndex = info.StartIndex + info.Length;
174 sb2.Append(text.Substring(currentTextIndex));
176 phoneticsInfo.Clear();
177 return sb2.ToString();
188 sealed class PhoneticInfo
193 public string Value {
get;
private set; }
197 public int StartIndex {
get;
private set; }
201 public int Length {
get;
private set; }
209 public PhoneticInfo(
string value,
string start,
string end)
212 StartIndex = ParserUtils.ParseInt(start);
213 Length = ParserUtils.ParseInt(end) - StartIndex;