mirror of https://github.com/apache/poi.git
Fix bug #49386 - avoid NPE when extracting OOXML file properties which are dates
Also tidy up POIXMLPropertiesTextExtractor to exclude properties which are missing git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@951384 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
bf86a2f7bf
commit
c1c954d14e
|
@ -34,6 +34,7 @@
|
||||||
|
|
||||||
<changes>
|
<changes>
|
||||||
<release version="3.7-SNAPSHOT" date="2010-??-??">
|
<release version="3.7-SNAPSHOT" date="2010-??-??">
|
||||||
|
<action dev="POI-DEVELOPERS" type="fix">49386 - avoid NPE when extracting OOXML file properties which are dates</action>
|
||||||
<action dev="POI-DEVELOPERS" type="fix">49377 - only call DecimalFormat.setRoundingMode on Java 1.6 - it's needed to match excel's rendering of numbers</action>
|
<action dev="POI-DEVELOPERS" type="fix">49377 - only call DecimalFormat.setRoundingMode on Java 1.6 - it's needed to match excel's rendering of numbers</action>
|
||||||
<action dev="POI-DEVELOPERS" type="fix">49378 - correct 1.6ism</action>
|
<action dev="POI-DEVELOPERS" type="fix">49378 - correct 1.6ism</action>
|
||||||
<action dev="POI-DEVELOPERS" type="add">Parse the HSMF headers chunk if present, and use it to find Dates in text extraction if needed</action>
|
<action dev="POI-DEVELOPERS" type="add">Parse the HSMF headers chunk if present, and use it to find Dates in text extraction if needed</action>
|
||||||
|
|
|
@ -20,6 +20,8 @@ package org.apache.poi;
|
||||||
import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart;
|
import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart;
|
||||||
import org.openxmlformats.schemas.officeDocument.x2006.customProperties.CTProperty;
|
import org.openxmlformats.schemas.officeDocument.x2006.customProperties.CTProperty;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A {@link POITextExtractor} for returning the textual
|
* A {@link POITextExtractor} for returning the textual
|
||||||
* content of the OOXML file properties, eg author
|
* content of the OOXML file properties, eg author
|
||||||
|
@ -41,6 +43,24 @@ public class POIXMLPropertiesTextExtractor extends POIXMLTextExtractor {
|
||||||
public POIXMLPropertiesTextExtractor(POIXMLTextExtractor otherExtractor) {
|
public POIXMLPropertiesTextExtractor(POIXMLTextExtractor otherExtractor) {
|
||||||
super(otherExtractor.getDocument());
|
super(otherExtractor.getDocument());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void appendIfPresent(StringBuffer text, String thing, boolean value) {
|
||||||
|
appendIfPresent(text, thing, Boolean.toString(value));
|
||||||
|
}
|
||||||
|
private void appendIfPresent(StringBuffer text, String thing, int value) {
|
||||||
|
appendIfPresent(text, thing, Integer.toString(value));
|
||||||
|
}
|
||||||
|
private void appendIfPresent(StringBuffer text, String thing, Date value) {
|
||||||
|
if(value == null) { return; }
|
||||||
|
appendIfPresent(text, thing, value.toString());
|
||||||
|
}
|
||||||
|
private void appendIfPresent(StringBuffer text, String thing, String value) {
|
||||||
|
if(value == null) { return; }
|
||||||
|
text.append(thing);
|
||||||
|
text.append(" = ");
|
||||||
|
text.append(value);
|
||||||
|
text.append("\n");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the core document properties, eg author
|
* Returns the core document properties, eg author
|
||||||
|
@ -50,25 +70,26 @@ public class POIXMLPropertiesTextExtractor extends POIXMLTextExtractor {
|
||||||
PackagePropertiesPart props =
|
PackagePropertiesPart props =
|
||||||
getDocument().getProperties().getCoreProperties().getUnderlyingProperties();
|
getDocument().getProperties().getCoreProperties().getUnderlyingProperties();
|
||||||
|
|
||||||
text.append("Category = " + props.getCategoryProperty().getValue() + "\n");
|
appendIfPresent(text, "Category", props.getCategoryProperty().getValue());
|
||||||
text.append("ContentStatus = " + props.getContentStatusProperty().getValue() + "\n");
|
appendIfPresent(text, "Category", props.getCategoryProperty().getValue());
|
||||||
text.append("ContentType = " + props.getContentTypeProperty().getValue() + "\n");
|
appendIfPresent(text, "ContentStatus", props.getContentStatusProperty().getValue());
|
||||||
text.append("Created = " + props.getCreatedProperty().getValue() + "\n");
|
appendIfPresent(text, "ContentType", props.getContentTypeProperty().getValue());
|
||||||
text.append("CreatedString = " + props.getCreatedPropertyString() + "\n");
|
appendIfPresent(text, "Created", props.getCreatedProperty().getValue());
|
||||||
text.append("Creator = " + props.getCreatorProperty().getValue() + "\n");
|
appendIfPresent(text, "CreatedString", props.getCreatedPropertyString());
|
||||||
text.append("Description = " + props.getDescriptionProperty().getValue() + "\n");
|
appendIfPresent(text, "Creator", props.getCreatorProperty().getValue());
|
||||||
text.append("Identifier = " + props.getIdentifierProperty().getValue() + "\n");
|
appendIfPresent(text, "Description", props.getDescriptionProperty().getValue());
|
||||||
text.append("Keywords = " + props.getKeywordsProperty().getValue() + "\n");
|
appendIfPresent(text, "Identifier", props.getIdentifierProperty().getValue());
|
||||||
text.append("Language = " + props.getLanguageProperty().getValue() + "\n");
|
appendIfPresent(text, "Keywords", props.getKeywordsProperty().getValue());
|
||||||
text.append("LastModifiedBy = " + props.getLastModifiedByProperty().getValue() + "\n");
|
appendIfPresent(text, "Language", props.getLanguageProperty().getValue());
|
||||||
text.append("LastPrinted = " + props.getLastPrintedProperty().getValue() + "\n");
|
appendIfPresent(text, "LastModifiedBy", props.getLastModifiedByProperty().getValue());
|
||||||
text.append("LastPrintedString = " + props.getLastPrintedPropertyString() + "\n");
|
appendIfPresent(text, "LastPrinted", props.getLastPrintedProperty().getValue());
|
||||||
text.append("Modified = " + props.getModifiedProperty().getValue() + "\n");
|
appendIfPresent(text, "LastPrintedString", props.getLastPrintedPropertyString());
|
||||||
text.append("ModifiedString = " + props.getModifiedPropertyString() + "\n");
|
appendIfPresent(text, "Modified", props.getModifiedProperty().getValue());
|
||||||
text.append("Revision = " + props.getRevisionProperty().getValue() + "\n");
|
appendIfPresent(text, "ModifiedString", props.getModifiedPropertyString());
|
||||||
text.append("Subject = " + props.getSubjectProperty().getValue() + "\n");
|
appendIfPresent(text, "Revision", props.getRevisionProperty().getValue());
|
||||||
text.append("Title = " + props.getTitleProperty().getValue() + "\n");
|
appendIfPresent(text, "Subject", props.getSubjectProperty().getValue());
|
||||||
text.append("Version = " + props.getVersionProperty().getValue() + "\n");
|
appendIfPresent(text, "Title", props.getTitleProperty().getValue());
|
||||||
|
appendIfPresent(text, "Version", props.getVersionProperty().getValue());
|
||||||
|
|
||||||
return text.toString();
|
return text.toString();
|
||||||
}
|
}
|
||||||
|
@ -81,21 +102,21 @@ public class POIXMLPropertiesTextExtractor extends POIXMLTextExtractor {
|
||||||
org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.CTProperties
|
org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.CTProperties
|
||||||
props = getDocument().getProperties().getExtendedProperties().getUnderlyingProperties();
|
props = getDocument().getProperties().getExtendedProperties().getUnderlyingProperties();
|
||||||
|
|
||||||
text.append("Application = " + props.getApplication() + "\n");
|
appendIfPresent(text, "Application", props.getApplication());
|
||||||
text.append("AppVersion = " + props.getAppVersion() + "\n");
|
appendIfPresent(text, "AppVersion", props.getAppVersion());
|
||||||
text.append("Characters = " + props.getCharacters() + "\n");
|
appendIfPresent(text, "Characters", props.getCharacters());
|
||||||
text.append("CharactersWithSpaces = " + props.getCharactersWithSpaces() + "\n");
|
appendIfPresent(text, "CharactersWithSpaces", props.getCharactersWithSpaces());
|
||||||
text.append("Company = " + props.getCompany() + "\n");
|
appendIfPresent(text, "Company", props.getCompany());
|
||||||
text.append("HyperlinkBase = " + props.getHyperlinkBase() + "\n");
|
appendIfPresent(text, "HyperlinkBase", props.getHyperlinkBase());
|
||||||
text.append("HyperlinksChanged = " + props.getHyperlinksChanged() + "\n");
|
appendIfPresent(text, "HyperlinksChanged", props.getHyperlinksChanged());
|
||||||
text.append("Lines = " + props.getLines() + "\n");
|
appendIfPresent(text, "Lines", props.getLines());
|
||||||
text.append("LinksUpToDate = " + props.getLinksUpToDate() + "\n");
|
appendIfPresent(text, "LinksUpToDate", props.getLinksUpToDate());
|
||||||
text.append("Manager = " + props.getManager() + "\n");
|
appendIfPresent(text, "Manager", props.getManager());
|
||||||
text.append("Pages = " + props.getPages() + "\n");
|
appendIfPresent(text, "Pages", props.getPages());
|
||||||
text.append("Paragraphs = " + props.getParagraphs() + "\n");
|
appendIfPresent(text, "Paragraphs", props.getParagraphs());
|
||||||
text.append("PresentationFormat = " + props.getPresentationFormat() + "\n");
|
appendIfPresent(text, "PresentationFormat", props.getPresentationFormat());
|
||||||
text.append("Template = " + props.getTemplate() + "\n");
|
appendIfPresent(text, "Template", props.getTemplate());
|
||||||
text.append("TotalTime = " + props.getTotalTime() + "\n");
|
appendIfPresent(text, "TotalTime", props.getTotalTime());
|
||||||
|
|
||||||
return text.toString();
|
return text.toString();
|
||||||
}
|
}
|
||||||
|
|
|
@ -583,10 +583,15 @@ public final class PackagePropertiesPart extends PackagePart implements
|
||||||
if (d == null) {
|
if (d == null) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
Date date = d.getValue();
|
||||||
|
if (date == null) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
SimpleDateFormat df = new SimpleDateFormat(
|
SimpleDateFormat df = new SimpleDateFormat(
|
||||||
"yyyy-MM-dd'T'HH:mm:ss'Z'");
|
"yyyy-MM-dd'T'HH:mm:ss'Z'");
|
||||||
df.setTimeZone(TimeZone.getTimeZone("UTC"));
|
df.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||||
return df.format(d.getValue());
|
return df.format(date);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -16,17 +16,17 @@
|
||||||
==================================================================== */
|
==================================================================== */
|
||||||
package org.apache.poi;
|
package org.apache.poi;
|
||||||
|
|
||||||
import java.io.File;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
import org.apache.poi.xssf.extractor.XSSFExcelExtractor;
|
|
||||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
||||||
import org.apache.poi.openxml4j.opc.OPCPackage;
|
import org.apache.poi.openxml4j.opc.OPCPackage;
|
||||||
import org.apache.poi.util.PackageHelper;
|
import org.apache.poi.util.PackageHelper;
|
||||||
|
import org.apache.poi.xslf.XSLFSlideShow;
|
||||||
import junit.framework.TestCase;
|
import org.apache.poi.xssf.extractor.XSSFExcelExtractor;
|
||||||
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||||
|
|
||||||
public final class TestXMLPropertiesTextExtractor extends TestCase {
|
public final class TestXMLPropertiesTextExtractor extends TestCase {
|
||||||
private static final POIDataSamples _ssSamples = POIDataSamples.getSpreadSheetInstance();
|
private static final POIDataSamples _ssSamples = POIDataSamples.getSpreadSheetInstance();
|
||||||
|
private static final POIDataSamples _slSamples = POIDataSamples.getSlideShowInstance();
|
||||||
|
|
||||||
public void testGetFromMainExtractor() throws Exception {
|
public void testGetFromMainExtractor() throws Exception {
|
||||||
OPCPackage pkg = PackageHelper.open(_ssSamples.openResourceAsStream("ExcelWithAttachments.xlsm"));
|
OPCPackage pkg = PackageHelper.open(_ssSamples.openResourceAsStream("ExcelWithAttachments.xlsm"));
|
||||||
|
@ -87,4 +87,23 @@ public final class TestXMLPropertiesTextExtractor extends TestCase {
|
||||||
public void testCustom() {
|
public void testCustom() {
|
||||||
// TODO!
|
// TODO!
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bug #49386 - some properties, especially
|
||||||
|
* dates can be null
|
||||||
|
*/
|
||||||
|
public void testWithSomeNulls() throws Exception {
|
||||||
|
OPCPackage pkg = OPCPackage.open(
|
||||||
|
_slSamples.openResourceAsStream("49386-null_dates.pptx")
|
||||||
|
);
|
||||||
|
XSLFSlideShow sl = new XSLFSlideShow(pkg);
|
||||||
|
|
||||||
|
POIXMLPropertiesTextExtractor ext = new POIXMLPropertiesTextExtractor(sl);
|
||||||
|
ext.getText();
|
||||||
|
|
||||||
|
String text = ext.getText();
|
||||||
|
assertFalse(text.contains("Created =")); // With date is null
|
||||||
|
assertTrue(text.contains("CreatedString = ")); // Via string is blank
|
||||||
|
assertTrue(text.contains("LastModifiedBy = IT Client Services"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Binary file not shown.
Loading…
Reference in New Issue