Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
<PackageVersion Include="Microsoft.IO.RecyclableMemoryStream" Version="3.0.1" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
<PackageVersion Include="NSax" Version="1.0.2" />
<PackageVersion Include="NSubstitute" Version="5.1.0" />
<PackageVersion Include="NuGetizer" Version="1.1.0" />
<PackageVersion Include="NUnit" Version="4.3.2" />
Expand Down
1 change: 0 additions & 1 deletion ooxml/NPOI.OOXML.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="NSax" />
<PackageReference Include="System.Security.Cryptography.Xml" />
</ItemGroup>

Expand Down
119 changes: 59 additions & 60 deletions ooxml/XSSF/EventUserModel/ReadOnlySharedStringsTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ namespace NPOI.XSSF.EventUserModel
using System.Collections.Generic;
using System.IO;
using System.Text;
using NSAX;
using NSAX.Helpers;
using System.Xml;

/// <summary>
/// <para>
Expand Down Expand Up @@ -72,7 +71,7 @@ namespace NPOI.XSSF.EventUserModel
/// </code>
/// </para>
/// </summary>
public class ReadOnlySharedStringsTable : DefaultHandler
public class ReadOnlySharedStringsTable
{

private bool includePhoneticRuns;
Expand Down Expand Up @@ -106,7 +105,6 @@ public class ReadOnlySharedStringsTable : DefaultHandler
/// </summary>
/// <param name="pkg">The <see cref="OPCPackage"/> to use as basis for the shared-strings table.</param>
/// <exception cref="IOException"> If reading the data from the package fails.</exception>
/// <exception cref="SAXException"> if parsing the XML data fails.</exception>
public ReadOnlySharedStringsTable(OPCPackage pkg)
: this(pkg, true)
{
Expand All @@ -117,8 +115,6 @@ public ReadOnlySharedStringsTable(OPCPackage pkg)
/// <param name="pkg">The <see cref="OPCPackage"/> to use as basis for the shared-strings table.</param>
/// <param name="includePhoneticRuns">whether or not to concatenate phoneticRuns onto the shared string</param>
/// <exception cref="IOException">IOException If reading the data from the package fails.</exception>
/// <exception cref="SAXException">SAXException if parsing the XML data fails.</exception>
/// @since POI 3.14-Beta3
public ReadOnlySharedStringsTable(OPCPackage pkg, bool includePhoneticRuns)
{
this.includePhoneticRuns = includePhoneticRuns;
Expand Down Expand Up @@ -153,9 +149,7 @@ public ReadOnlySharedStringsTable(PackagePart part)
/// </summary>
/// @since POI 3.14-Beta3
public ReadOnlySharedStringsTable(PackagePart part, bool includePhoneticRuns)

{

this.includePhoneticRuns = includePhoneticRuns;
ReadFrom(part.GetInputStream());
}
Expand All @@ -165,27 +159,32 @@ public ReadOnlySharedStringsTable(PackagePart part, bool includePhoneticRuns)
/// </summary>
/// <param name="is1">The input stream containing the XML document.</param>
/// <exception cref="IOException"> if an error occurs while reading.</exception>
/// <exception cref="SAXException"> if parsing the XML data fails.</exception>
public void ReadFrom(Stream is1)
{
// test if the file is empty, otherwise parse it
//PushbackInputStream pis = new PushbackInputStream(is1, 1);
//int emptyTest = pis.Read();
//if (emptyTest > -1)
if(is1.Length > 0)
{
//pis.Unread(emptyTest);
InputSource sheetSource = new InputSource(is1);
//try
XmlReaderSettings settings = new XmlReaderSettings();
settings.DtdProcessing = DtdProcessing.Ignore;
var reader = XmlReader.Create(is1, settings);
while(reader.Read())
{
NSAX.AElfred.SAXDriver sheetParser = new NSAX.AElfred.SAXDriver();
sheetParser.ContentHandler = (this);
sheetParser.Parse(sheetSource);
if(reader.NodeType == XmlNodeType.Element)
{
//begin element
StartElement(reader);
}
else if(reader.NodeType == XmlNodeType.EndElement)
{
EndElement(reader);
}
else if(reader.NodeType == XmlNodeType.Text ||
reader.NodeType == XmlNodeType.SignificantWhitespace ||
reader.NodeType == XmlNodeType.Whitespace)
{
TextNode(reader);
}
}
//catch (ParserConfigurationException e)
//{
// throw new RuntimeException("SAX parser appears to be broken - " + e.GetMessage());
//}
}
}

Expand Down Expand Up @@ -224,85 +223,85 @@ public String GetEntryAt(int idx)
private bool tIsOpen;
private bool inRPh;

public override void StartElement(String uri, String localName, String name,
IAttributes attributes)
public void TextNode(XmlReader reader)
{
if(tIsOpen)
{
if(inRPh && includePhoneticRuns)
{
characters.Append(reader.Value);
}
else if(!inRPh)
{
characters.Append(reader.Value);
}
}
}

if (uri != null && !uri.Equals(XSSFRelation.NS_SPREADSHEETML))
public void StartElement(XmlReader reader)
{
string uri = reader.NamespaceURI;
string localName = reader.LocalName;
//string name = reader.Name;
if(uri != null && !uri.Equals(XSSFRelation.NS_SPREADSHEETML))
{
return;
}

if ("sst".Equals(localName))
if("sst".Equals(localName))
{
String count = attributes.GetValue("count");
if (count != null) this.count = Int32.Parse(count);
String uniqueCount = attributes.GetValue("uniqueCount");
if (uniqueCount != null) this.uniqueCount = Int32.Parse(uniqueCount);
String count = reader.GetAttribute("count");
if(count != null)
this.count = Int32.Parse(count);
String uniqueCount = reader.GetAttribute("uniqueCount");
if(uniqueCount != null)
this.uniqueCount = Int32.Parse(uniqueCount);

this.strings = new List<String>(this.uniqueCount);
this.phoneticStrings = new Dictionary<int, String>();
characters = new StringBuilder();
}
else if ("si".Equals(localName))
else if("si".Equals(localName))
{
characters.Length = 0;
}
else if ("t".Equals(localName))
else if("t".Equals(localName))
{
tIsOpen = true;
}
else if ("rPh".Equals(localName))
else if("rPh".Equals(localName))
{
inRPh = true;
//append space...this assumes that rPh always comes After regular <t>
if (includePhoneticRuns && characters.Length > 0)
if(includePhoneticRuns && characters.Length > 0)
{
characters.Append(" ");
}
}
}

public override void EndElement(String uri, String localName, String name)

public void EndElement(XmlReader reader)
{

if (uri != null && !uri.Equals(XSSFRelation.NS_SPREADSHEETML))
string uri = reader.NamespaceURI;
string localName = reader.LocalName;
//string name = reader.Name;
if(uri != null && !uri.Equals(XSSFRelation.NS_SPREADSHEETML))
{
return;
}

if ("si".Equals(localName))
if("si".Equals(localName))
{
strings.Add(characters.ToString());
}
else if ("t".Equals(localName))
else if("t".Equals(localName))
{
tIsOpen = false;
}
else if ("rPh".Equals(localName))
else if("rPh".Equals(localName))
{
inRPh = false;
}
}

/// <summary>
/// Captures characters only if a t(ext) element is open.
/// </summary>
public override void Characters(char[] ch, int start, int length)
{
if (tIsOpen)
{
if (inRPh && includePhoneticRuns)
{
characters.Append(ch, start, length);
}
else if (!inRPh)
{
characters.Append(ch, start, length);
}
}
}
}
}

63 changes: 23 additions & 40 deletions ooxml/XSSF/EventUserModel/XSSFReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,15 @@ limitations under the License.
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace NPOI.XSSF.EventUserModel
{

using NPOI;
using NPOI.OpenXml4Net.Exceptions;
using NPOI.OpenXml4Net.OPC;
using NPOI.Util;
using NPOI.XSSF.Model;
using NPOI.XSSF.UserModel;
using NSAX;
using NSAX.Helpers;
using System.Xml;

/// <summary>
Expand Down Expand Up @@ -247,29 +243,27 @@ internal SheetIterator(PackagePart wb)
public virtual List<XSSFSheetRef> CreateSheetIteratorFromWB(PackagePart wb)
{
XMLSheetRefReader xmlSheetRefReader = new XMLSheetRefReader();
NSAX.AElfred.SAXDriver xmlReader;
try
{
xmlReader = new NSAX.AElfred.SAXDriver();// SAXHelper.newXMLReader();
XmlReaderSettings settings = new XmlReaderSettings();
settings.DtdProcessing = DtdProcessing.Ignore;
var reader = XmlReader.Create(wb.GetInputStream(), settings);
while(reader.Read())
{
if(reader.NodeType == XmlNodeType.Element)
{
xmlSheetRefReader.StartElement(reader);
}
}
}
//catch (ParserConfigurationException e)
//{
// throw new POIXMLException(e);
//}
catch (SAXException e)
catch(XmlException e)
{
throw new POIXMLException(e);
}
xmlReader.ContentHandler = (xmlSheetRefReader);
try
{
xmlReader.Parse(new InputSource(wb.GetInputStream()));
}
catch (SAXException e)
catch(InvalidOperationException e)
{
throw new POIXMLException(e);
}

List<XSSFSheetRef> validSheets = new List<XSSFSheetRef>();
foreach (XSSFSheetRef xssfSheetRef in xmlSheetRefReader.GetSheetRefs())
{
Expand Down Expand Up @@ -456,39 +450,28 @@ public XSSFSheetRef(String id, String name)
}

//scrapes sheet reference info and order from workbook.xml
private class XMLSheetRefReader : DefaultHandler
private class XMLSheetRefReader
{
private static String SHEET = "sheet";
private static String ID = "id";
private static String NAME = "name";

private List<XSSFSheetRef> sheetRefs = new List<XSSFSheetRef>();

// read <sheet name="Sheet6" sheetId="4" r:id="rId6"/>
// and add XSSFSheetRef(id="rId6", name="Sheet6") to sheetRefs
public override void StartElement(String uri, String localName, String qName, IAttributes attrs)
public void StartElement(XmlReader reader)
{

if (localName.Equals(SHEET, StringComparison.OrdinalIgnoreCase))
string uri = reader.NamespaceURI;
string localName = reader.LocalName;
if(localName.Equals(SHEET, StringComparison.OrdinalIgnoreCase))
{
String name = null;
String id = null;
for (int i = 0; i < attrs.Length; i++)
String name = reader.GetAttribute(NAME);
String id = reader.GetAttribute(ID, PackageNamespaces.SCHEMA_RELATIONSHIPS);

if(name != null && id != null)
{
String attrName = attrs.GetLocalName(i);
if (attrName.Equals(NAME, StringComparison.OrdinalIgnoreCase))
{
name = attrs.GetValue(i);
}
else if (attrName.Equals(ID, StringComparison.OrdinalIgnoreCase))
{
id = attrs.GetValue(i);
}
if (name != null && id != null)
{
sheetRefs.Add(new XSSFSheetRef(id, name));
break;
}
sheetRefs.Add(new XSSFSheetRef(id, name));
}
}
}
Expand Down
Loading
Loading