Skip to content

Commit

Permalink
Writable comments table (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjfanning authored Jan 14, 2022
1 parent 75e3119 commit f293a5e
Show file tree
Hide file tree
Showing 7 changed files with 551 additions and 199 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group = 'com.github.pjfanning'
version = '2.3.5-SNAPSHOT'
version = '2.4.0-SNAPSHOT'

sonarqube {
properties {
Expand All @@ -27,8 +27,8 @@ repositories {
dependencies {
implementation 'org.slf4j:slf4j-api:1.7.33'
implementation 'com.h2database:h2:2.0.206'
implementation 'org.apache.poi:poi-ooxml:5.1.0'
implementation 'org.apache.poi:poi:5.1.0'
implementation 'org.apache.poi:poi-ooxml:5.2.0'
implementation 'org.apache.poi:poi:5.2.0'
implementation 'org.apache.xmlbeans:xmlbeans:5.0.3'
implementation 'org.apache.commons:commons-text:1.9'
testImplementation 'junit:junit:4.13.2'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package com.github.pjfanning.poi.xssf.streaming;

import com.microsoft.schemas.vml.CTShape;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.xssf.model.Comments;
import org.apache.poi.xssf.usermodel.XSSFComment;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst;

public class DelegatingXSSFComment extends XSSFComment {
private final SerializableComment delegate;
private final Comments comments;
private final CTShape ctShape;

DelegatingXSSFComment(Comments comments, SerializableComment delegate, CTShape ctShape) {
super(comments, null, ctShape);
this.comments = comments;
this.delegate = delegate;
this.ctShape = ctShape;
}

@Override
public String getAuthor() {
return delegate.getAuthor();
}

@Override
public int getColumn() {
return delegate.getColumn();
}

@Override
public int getRow() {
return delegate.getRow();
}

@Override
public boolean isVisible() {
return delegate.isVisible();
}

@Override
public CellAddress getAddress() {
return delegate.getAddress();
}

@Override
public XSSFRichTextString getString() {
return delegate.getString();
}

/**
* If you are only interested in the text of the comment and don't need the
* full {@link XSSFRichTextString}, then this method is more efficient.
* @return the text of the comment
*/
public String getCommentText() {
return delegate.getCommentText();
}

@Override
public CTComment getCTComment() {
CTRst rst = delegate.getString().getCTRst();
CTComment ctComment = CTComment.Factory.newInstance();
ctComment.setText(rst);
return ctComment;
}

@Override
public void setAddress(int row, int col) {
CellAddress oldAddress = delegate.getAddress();
delegate.setAddress(row, col);
comments.referenceUpdated(oldAddress, this);
}

@Override
public void setAddress(CellAddress address) {
CellAddress oldAddress = delegate.getAddress();
delegate.setAddress(address);
comments.referenceUpdated(oldAddress, this);
}

@Override
public void setRow(int row) {
CellAddress oldAddress = delegate.getAddress();
delegate.setRow(row);
comments.referenceUpdated(oldAddress, this);
}

@Override
public void setColumn(int col) {
CellAddress oldAddress = delegate.getAddress();
delegate.setColumn(col);
comments.referenceUpdated(oldAddress, this);
}

@Override
public void setString(RichTextString string) {
delegate.setString(string);
comments.commentUpdated(this);
}

@Override
public void setString(String string) {
delegate.setString(string);
comments.commentUpdated(this);
}

@Override
public void setAuthor(String author) {
delegate.setAuthor(author);
comments.commentUpdated(this);
}

@Override
public void setVisible(boolean visible) {
delegate.setVisible(visible);
comments.commentUpdated(this);
}

@Override
protected CTShape getCTShape() {
return ctShape;
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class SXSSFFactory extends XSSFFactory {

private boolean encryptTempFiles = false;
private boolean enableTempFileSharedStrings = true;
private boolean enableTempFileComments = false;

public SXSSFFactory() {}

Expand Down Expand Up @@ -41,6 +42,16 @@ public SXSSFFactory enableTempFileSharedStrings(boolean enableTempFileSharedStri
return this;
}

/**
* @param enableTempFileComments whether to enable temp file comments table (default is false)
* @return this factory instance
* @since v2.3.0
*/
public SXSSFFactory enableTempFileComments(boolean enableTempFileComments) {
this.enableTempFileComments = enableTempFileComments;
return this;
}

@Override
public POIXMLDocumentPart newDocumentPart(POIXMLRelation descriptor) {
if (XSSFRelation.SHARED_STRINGS.getRelation().equals(descriptor.getRelation()) && enableTempFileSharedStrings) {
Expand All @@ -51,6 +62,14 @@ public POIXMLDocumentPart newDocumentPart(POIXMLRelation descriptor) {
"required for this feature and is not included as a core dependency of poi-ooxml");
}
}
if (XSSFRelation.SHEET_COMMENTS.getRelation().equals(descriptor.getRelation()) && enableTempFileComments) {
try {
return new TempFileCommentsTable(encryptTempFiles);
} catch (Error|RuntimeException e) {
throw new RuntimeException("Exception creating TempFileCommentsTable; com.h2database h2 jar is " +
"required for this feature and is not included as a core dependency of poi-ooxml");
}
}
return super.newDocumentPart(descriptor);
}
}
Loading

0 comments on commit f293a5e

Please sign in to comment.