Skip to content

Commit

Permalink
create PoiSharedStringsSupport (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjfanning authored Jul 11, 2024
1 parent c759036 commit 986882b
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.github.pjfanning.xlsx.impl;

import com.github.pjfanning.poi.xssf.streaming.CommentsTableBase;
import com.github.pjfanning.poi.xssf.streaming.MapBackedCommentsTable;
import com.github.pjfanning.poi.xssf.streaming.MapBackedSharedStringsTable;
import com.github.pjfanning.poi.xssf.streaming.TempFileCommentsTable;
import com.github.pjfanning.poi.xssf.streaming.TempFileSharedStringsTable;
import com.github.pjfanning.xlsx.StreamingReader;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xssf.model.Comments;
import org.apache.poi.xssf.model.SharedStringsTable;

import java.io.IOException;
import java.io.InputStream;

/**
* Keeps code that relies on poi-shared-strings out of the main codebase.
*/
public final class PoiSharedStringsSupport {
public static Comments createTempFileCommentsTable(final StreamingReader.Builder builder) throws IOException {
return new TempFileCommentsTable(
builder.encryptCommentsTempFile(),
builder.fullFormatRichText());
}

public static Comments createMapBackedCommentsTable(final StreamingReader.Builder builder) {
return new MapBackedCommentsTable(builder.fullFormatRichText());
}

public static SharedStringsTable createTempFileSharedStringsTable(
final StreamingReader.Builder builder) throws IOException {
return new TempFileSharedStringsTable(
builder.encryptSstTempFile(),
builder.fullFormatRichText());
}

public static SharedStringsTable createTempFileSharedStringsTable(
final OPCPackage pkg, final StreamingReader.Builder builder) throws IOException {
return new TempFileSharedStringsTable(
pkg,
builder.encryptSstTempFile(),
builder.fullFormatRichText());
}

public static SharedStringsTable createMapBackedSharedStringsTable(
final StreamingReader.Builder builder) {
return new MapBackedSharedStringsTable(builder.fullFormatRichText());
}

public static SharedStringsTable createMapBackedSharedStringsTable(
final OPCPackage pkg, final StreamingReader.Builder builder) throws IOException {
return new MapBackedSharedStringsTable(
pkg,
builder.fullFormatRichText());
}

public static void readComments(final Comments comments, final InputStream inputStream) throws IOException {
if (comments instanceof CommentsTableBase) {
((CommentsTableBase) comments).readFrom(inputStream);
}
}

private PoiSharedStringsSupport() {
// no-op
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ private Supplier getFormatterForType(String type) {
case "e": //error type
return new StringSupplier("ERROR: " + lastContents);
case "n": //numeric type
if(currentCell.getNumericFormat() != null && lastContents.length() > 0) {
if(currentCell.getNumericFormat() != null && !lastContents.isEmpty()) {
// the formatRawCellContents operation incurs a significant overhead on large sheets,
// and we want to defer the execution of this method until the value is actually needed.
// it is not needed in all cases..
Expand All @@ -538,7 +538,7 @@ private Supplier getFormatterForType(String type) {
return new StringSupplier(lastContents);
}
case "d": //date type (Strict OOXML format)
if(currentCell.getNumericFormat() != null && lastContents.length() > 0) {
if(currentCell.getNumericFormat() != null && !lastContents.isEmpty()) {
// the formatRawCellContents operation incurs a significant overhead on large sheets,
// and we want to defer the execution of this method until the value is actually needed.
// it is not needed in all cases..
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.github.pjfanning.xlsx.impl;

import com.github.pjfanning.poi.xssf.streaming.MapBackedSharedStringsTable;
import com.github.pjfanning.poi.xssf.streaming.TempFileSharedStringsTable;
import com.github.pjfanning.xlsx.SharedStringsImplementationType;
import com.github.pjfanning.xlsx.StreamingReader.Builder;
import com.github.pjfanning.xlsx.exceptions.ExcelRuntimeException;
Expand Down Expand Up @@ -186,9 +184,9 @@ private void loadPackage(OPCPackage pkg) throws IOException, OpenXML4JException,

if (builder.getSharedStringsImplementationType() == SharedStringsImplementationType.TEMP_FILE_BACKED) {
log.info("Created sst cache file");
sst = new TempFileSharedStringsTable(pkg, builder.encryptSstTempFile(), builder.fullFormatRichText());
sst = PoiSharedStringsSupport.createTempFileSharedStringsTable(pkg, builder);
} else if (builder.getSharedStringsImplementationType() == SharedStringsImplementationType.CUSTOM_MAP_BACKED) {
sst = new MapBackedSharedStringsTable(pkg, builder.fullFormatRichText());
sst = PoiSharedStringsSupport.createMapBackedSharedStringsTable(pkg, builder);
} else if (strictFormat) {
sst = OoxmlStrictHelper.getSharedStringsTable(builder, pkg);
} else {
Expand Down
36 changes: 21 additions & 15 deletions src/main/java/com/github/pjfanning/xlsx/impl/ooxml/OoxmlReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@ Licensed to the Apache Software Foundation (ASF) under one or more
==================================================================== */
package com.github.pjfanning.xlsx.impl.ooxml;

import com.github.pjfanning.poi.xssf.streaming.MapBackedCommentsTable;
import com.github.pjfanning.poi.xssf.streaming.MapBackedSharedStringsTable;
import com.github.pjfanning.poi.xssf.streaming.TempFileCommentsTable;
import com.github.pjfanning.poi.xssf.streaming.TempFileSharedStringsTable;
import com.github.pjfanning.xlsx.CommentsImplementationType;
import com.github.pjfanning.xlsx.StreamingReader;
import com.github.pjfanning.xlsx.exceptions.MissingSheetException;
import com.github.pjfanning.xlsx.exceptions.OpenException;
import com.github.pjfanning.xlsx.impl.PoiSharedStringsSupport;
import org.apache.poi.ooxml.POIXMLException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
Expand Down Expand Up @@ -133,9 +130,9 @@ public SharedStrings getSharedStrings(StreamingReader.Builder builder) throws IO
case POI_DEFAULT:
return new SharedStringsTable(parts.get(0));
case CUSTOM_MAP_BACKED:
return new MapBackedSharedStringsTable(parts.get(0).getPackage());
return PoiSharedStringsSupport.createMapBackedSharedStringsTable(parts.get(0).getPackage(), builder);
case TEMP_FILE_BACKED:
return new TempFileSharedStringsTable(parts.get(0).getPackage(), builder.encryptSstTempFile());
return PoiSharedStringsSupport.createTempFileSharedStringsTable(parts.get(0).getPackage(), builder);
default:
return new ReadOnlySharedStringsTable(parts.get(0));
}
Expand Down Expand Up @@ -378,25 +375,34 @@ private static Comments parseComments(final StreamingReader.Builder builder,
throws IOException, XMLStreamException {
if (builder.getCommentsImplementationType() == CommentsImplementationType.TEMP_FILE_BACKED) {
try (InputStream is = commentsPart.getInputStream()) {
TempFileCommentsTable ct = new TempFileCommentsTable(
builder.encryptCommentsTempFile(),
builder.fullFormatRichText());
Comments ct = PoiSharedStringsSupport.createTempFileCommentsTable(builder);
try {
ct.readFrom(is);
PoiSharedStringsSupport.readComments(ct, is);
} catch (IOException | RuntimeException e) {
ct.close();
if (ct instanceof AutoCloseable) {
try {
((AutoCloseable) ct).close();
} catch (Exception e2) {
e.addSuppressed(e2);
}
}
throw e;
}
return ct;
}
} else if (builder.getCommentsImplementationType() == CommentsImplementationType.CUSTOM_MAP_BACKED) {
try (InputStream is = commentsPart.getInputStream()) {
MapBackedCommentsTable ct = new MapBackedCommentsTable(
builder.fullFormatRichText());
Comments ct = PoiSharedStringsSupport.createMapBackedCommentsTable(builder);
try {
ct.readFrom(is);
PoiSharedStringsSupport.readComments(ct, is);
} catch (IOException | RuntimeException e) {
ct.close();
if (ct instanceof AutoCloseable) {
try {
((AutoCloseable) ct).close();
} catch (Exception e2) {
e.addSuppressed(e2);
}
}
throw e;
}
return ct;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.github.pjfanning.xlsx.impl.ooxml;

import com.github.pjfanning.poi.xssf.streaming.MapBackedCommentsTable;
import com.github.pjfanning.poi.xssf.streaming.MapBackedSharedStringsTable;
import com.github.pjfanning.poi.xssf.streaming.TempFileCommentsTable;
import com.github.pjfanning.poi.xssf.streaming.TempFileSharedStringsTable;
import com.github.pjfanning.xlsx.StreamingReader;
import com.github.pjfanning.xlsx.impl.PoiSharedStringsSupport;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable;
Expand Down Expand Up @@ -97,21 +94,28 @@ public static SharedStrings getSharedStringsTable(StreamingReader.Builder builde
}
return sst;
case TEMP_FILE_BACKED:
TempFileSharedStringsTable tfst = new TempFileSharedStringsTable(
builder.encryptSstTempFile(), builder.fullFormatRichText());
SharedStringsTable tfst = PoiSharedStringsSupport.createTempFileSharedStringsTable(builder);
try {
tfst.readFrom(is);
} catch (IOException|RuntimeException e) {
tfst.close();
try {
tfst.close();
} catch (IOException e2) {
e.addSuppressed(e2);
}
throw e;
}
return tfst;
case CUSTOM_MAP_BACKED:
MapBackedSharedStringsTable mbst = new MapBackedSharedStringsTable(builder.fullFormatRichText());
SharedStringsTable mbst = PoiSharedStringsSupport.createMapBackedSharedStringsTable(builder);
try {
mbst.readFrom(is);
} catch (IOException|RuntimeException e) {
mbst.close();
try {
mbst.close();
} catch (IOException e2) {
e.addSuppressed(e2);
}
throw e;
}
return mbst;
Expand All @@ -138,22 +142,32 @@ public static Comments getCommentsTable(StreamingReader.Builder builder, Package
try(InputStream is = tempData.getInputStream()) {
switch (builder.getCommentsImplementationType()) {
case TEMP_FILE_BACKED:
TempFileCommentsTable tfct = new TempFileCommentsTable(
builder.encryptCommentsTempFile(),
builder.fullFormatRichText());
Comments tfct = PoiSharedStringsSupport.createTempFileCommentsTable(builder);
try {
tfct.readFrom(is);
PoiSharedStringsSupport.readComments(tfct, is);
} catch (IOException|RuntimeException e) {
tfct.close();
if (tfct instanceof AutoCloseable) {
try {
((AutoCloseable) tfct).close();
} catch (Exception e2) {
e.addSuppressed(e2);
}
}
throw e;
}
return tfct;
case CUSTOM_MAP_BACKED:
MapBackedCommentsTable mbct = new MapBackedCommentsTable(builder.fullFormatRichText());
Comments mbct = PoiSharedStringsSupport.createMapBackedCommentsTable(builder);
try {
mbct.readFrom(is);
PoiSharedStringsSupport.readComments(mbct, is);
} catch (IOException|RuntimeException e) {
mbct.close();
if (mbct instanceof AutoCloseable) {
try {
((AutoCloseable) mbct).close();
} catch (Exception e2) {
e.addSuppressed(e2);
}
}
throw e;
}
return mbct;
Expand Down

0 comments on commit 986882b

Please sign in to comment.