public XDDFValueAxis createValueAxis(AxisPosition pos) {
XDDFValueAxis valueAxis = new XDDFValueAxis(getCTPlotArea(), pos);
- if (axes.size() == 1) {
- XDDFChartAxis axis = axes.get(0);
- axis.crossAxis(valueAxis);
- valueAxis.crossAxis(axis);
- axis.setCrosses(AxisCrosses.AUTO_ZERO);
- valueAxis.setCrosses(AxisCrosses.AUTO_ZERO);
- }
- axes.add(valueAxis);
+ addAxis(valueAxis);
return valueAxis;
}
*/
public XDDFSeriesAxis createSeriesAxis(AxisPosition pos) {
XDDFSeriesAxis seriesAxis = new XDDFSeriesAxis(getCTPlotArea(), pos);
- if (axes.size() == 1) {
- XDDFChartAxis axis = axes.get(0);
- axis.crossAxis(seriesAxis);
- seriesAxis.crossAxis(axis);
- axis.setCrosses(AxisCrosses.AUTO_ZERO);
- seriesAxis.setCrosses(AxisCrosses.AUTO_ZERO);
- }
- axes.add(seriesAxis);
+ addAxis(seriesAxis);
return seriesAxis;
}
public XDDFCategoryAxis createCategoryAxis(AxisPosition pos) {
XDDFCategoryAxis categoryAxis = new XDDFCategoryAxis(getCTPlotArea(), pos);
- if (axes.size() == 1) {
- XDDFChartAxis axis = axes.get(0);
- axis.crossAxis(categoryAxis);
- categoryAxis.crossAxis(axis);
- axis.setCrosses(AxisCrosses.AUTO_ZERO);
- categoryAxis.setCrosses(AxisCrosses.AUTO_ZERO);
- }
- axes.add(categoryAxis);
+ addAxis(categoryAxis);
return categoryAxis;
}
public XDDFDateAxis createDateAxis(AxisPosition pos) {
XDDFDateAxis dateAxis = new XDDFDateAxis(getCTPlotArea(), pos);
+ addAxis(dateAxis);
+ return dateAxis;
+ }
+
+ private void addAxis(XDDFChartAxis newAxis) {
if (axes.size() == 1) {
XDDFChartAxis axis = axes.get(0);
- axis.crossAxis(dateAxis);
- dateAxis.crossAxis(axis);
+ axis.crossAxis(newAxis);
+ newAxis.crossAxis(axis);
axis.setCrosses(AxisCrosses.AUTO_ZERO);
- dateAxis.setCrosses(AxisCrosses.AUTO_ZERO);
+ newAxis.setCrosses(AxisCrosses.AUTO_ZERO);
}
- axes.add(dateAxis);
- return dateAxis;
+ axes.add(newAxis);
}
/**
public PackageRelationship createRelationshipInChart(POIXMLRelation chartRelation, POIXMLFactory chartFactory,
int chartIndex) {
documentPart = createRelationship(chartRelation, chartFactory, chartIndex, true).getDocumentPart();
- return this.addRelation(null, chartRelation, documentPart).getRelationship();
+ return addRelation(null, chartRelation, documentPart).getRelationship();
}
/**
private PackagePart createWorksheetPart(POIXMLRelation chartRelation, POIXMLRelation chartWorkbookRelation,
POIXMLFactory chartFactory) throws InvalidFormatException {
PackageRelationship xlsx = createRelationshipInChart(chartWorkbookRelation, chartFactory, chartIndex);
- this.setExternalId(xlsx.getId());
+ setExternalId(xlsx.getId());
return getTargetPart(xlsx);
}
protected void fillSheet(XSSFSheet sheet, XDDFDataSource<?> categoryData, XDDFNumericalDataSource<?> valuesData) {
int numOfPoints = categoryData.getPointCount();
for (int i = 0; i < numOfPoints; i++) {
- XSSFRow row = this.getRow(sheet, i + 1); // first row is for title
+ XSSFRow row = getRow(sheet, i + 1); // first row is for title
Object category = categoryData.getPointAt(i);
if (category != null) {
- this.getCell(row, categoryData.getColIndex()).setCellValue(category.toString());
+ getCell(row, categoryData.getColIndex()).setCellValue(category.toString());
}
Number value = valuesData.getPointAt(i);
if (value != null) {
- this.getCell(row, valuesData.getColIndex()).setCellValue(value.doubleValue());
+ getCell(row, valuesData.getColIndex()).setCellValue(value.doubleValue());
}
}
}
*/
public CellReference setSheetTitle(String title, int column) {
XSSFSheet sheet = getSheet();
- XSSFRow row = this.getRow(sheet, 0);
- XSSFCell cell = this.getCell(row, column);
+ XSSFRow row = getRow(sheet, 0);
+ XSSFCell cell = getCell(row, column);
cell.setCellValue(title);
return new CellReference(sheet.getSheetName(), 0, column, true, true);
setFontName(DEFAULT_FONT_NAME);
setFontHeight((double)DEFAULT_FONT_SIZE);
}
-
+
/**
* get the underlying CTFont font
*/
public short getFontHeightInPoints() {
return (short)getFontHeightRaw();
}
-
+
/**
* Return the raw font height, in points, but also
* including fractions.
/**
- * Perform a registration of ourselves
+ * Perform a registration of ourselves
* to the style table
*/
public long registerTo(StylesTable styles) {
&& Objects.equals(this.getBold(), cf.getBold())
&& Objects.equals(this.getStrikeout(), cf.getStrikeout())
&& Objects.equals(this.getCharSet(), cf.getCharSet())
- && Objects.equals(this.getBold(), cf.getBold())
+ && Objects.equals(this.getItalic(), cf.getItalic())
&& Objects.equals(this.getColor(), cf.getColor())
&& Objects.equals(this.getFamily(), cf.getFamily())
&& Objects.equals(this.getFontHeight(), cf.getFontHeight())
import java.io.PushbackInputStream;
import java.net.URI;
import java.net.URISyntaxException;
+import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.TreeMap;
import java.util.function.BiConsumer;
import java.util.regex.Pattern;
-import java.util.zip.ZipException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
OPCPackage p = OPCPackage.open(originalFile, PackageAccess.READ_WRITE);
try {
p.save(targetFile.getAbsoluteFile());
-
+
// Compare the original and newly saved document
assertTrue(targetFile.exists());
ZipFileAssert.assertEquals(new File(originalFile), targetFile);
PackagingURIHelper.createPartName("/foo.txt")
)
);
-
+
pkg.revert();
}
PackagePart corePart = pkg.createPart(corePartName, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml");
// Put in some dummy content
OutputStream coreOut = corePart.getOutputStream();
- coreOut.write("<dummy-xml />".getBytes("UTF-8"));
+ coreOut.write("<dummy-xml />".getBytes(StandardCharsets.UTF_8));
coreOut.close();
// And another bit
// Dummy content again
coreOut = corePart.getOutputStream();
- coreOut.write("<dummy-xml2 />".getBytes("UTF-8"));
+ coreOut.write("<dummy-xml2 />".getBytes(StandardCharsets.UTF_8));
coreOut.close();
//add a relationship with internal target: "#Sheet1!A1"
assertEquals("/xl/workbook.xml", coreRel.getTargetURI().toString());
corePart = pkg.getPart(coreRel);
assertNotNull(corePart);
-
+
PackageRelationshipCollection rels = corePart.getRelationshipsByType("http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink");
assertEquals(1, rels.size());
rel = rels.getRelationship(0);
assertNotNull(rel);
assertEquals("Sheet1!A1", rel.getTargetURI().getRawFragment());
-
+
assertMSCompatibility(pkg);
} finally {
pkg.close();
try (FileOutputStream fout = new FileOutputStream(targetFile)) {
p.save(fout);
}
-
+
// Compare the original and newly saved document
assertTrue(targetFile.exists());
ZipFileAssert.assertEquals(new File(originalFile), targetFile);
public void openFromInputStream() throws IOException, InvalidFormatException {
String originalFile = OpenXML4JTestDataSamples.getSampleFileName("TestPackageCommon.docx");
- FileInputStream finp = new FileInputStream(originalFile);
+ try (FileInputStream finp = new FileInputStream(originalFile)) {
+ @SuppressWarnings("resource")
+ OPCPackage p = OPCPackage.open(finp);
- @SuppressWarnings("resource")
- OPCPackage p = OPCPackage.open(finp);
-
- assertNotNull(p);
- assertNotNull(p.getRelationships());
- assertEquals(12, p.getParts().size());
+ try {
+ assertNotNull(p);
+ assertNotNull(p.getRelationships());
+ assertEquals(12, p.getParts().size());
- // Check it has the usual bits
- assertTrue(p.hasRelationships());
- assertTrue(p.containPart(PackagingURIHelper.createPartName("/_rels/.rels")));
-
- p.revert();
- finp.close();
+ // Check it has the usual bits
+ assertTrue(p.hasRelationships());
+ assertTrue(p.containPart(PackagingURIHelper.createPartName("/_rels/.rels")));
+ } finally {
+ p.revert();
+ }
+ }
}
/**
* TODO: fix and enable
*/
@Test
- @Ignore
+ @Ignore
public void removePartRecursive() throws IOException, InvalidFormatException, URISyntaxException {
String originalFile = OpenXML4JTestDataSamples.getSampleFileName("TestPackageCommon.docx");
File targetFile = OpenXML4JTestDataSamples.getOutputFile("TestPackageRemovePartRecursiveOUTPUT.docx");
@SuppressWarnings("resource")
OPCPackage p = OPCPackage.open(originalFile, PackageAccess.READ_WRITE);
- p.removePartRecursive(PackagingURIHelper.createPartName(new URI(
- "/word/document.xml")));
- p.save(tempFile.getAbsoluteFile());
+ try {
+ p.removePartRecursive(PackagingURIHelper.createPartName(new URI(
+ "/word/document.xml")));
+ p.save(tempFile.getAbsoluteFile());
- // Compare the original and newly saved document
- assertTrue(targetFile.exists());
- ZipFileAssert.assertEquals(targetFile, tempFile);
- assertTrue(targetFile.delete());
-
- p.revert();
+ // Compare the original and newly saved document
+ assertTrue(targetFile.exists());
+ ZipFileAssert.assertEquals(targetFile, tempFile);
+ assertTrue(targetFile.delete());
+ } finally {
+ p.revert();
+ }
}
@Test
// Don't save modifications
p.revert();
}
-
+
/**
* Test that we can open a file by path, and then
* write changes to it.
File tempFile = TempFile.createTempFile("poiTesting","tmp");
File origFile = OpenXML4JTestDataSamples.getSampleFile("TestPackageCommon.docx");
FileHelper.copyFile(origFile, tempFile);
-
+
// Open the temp file
OPCPackage p = OPCPackage.open(tempFile.toString(), PackageAccess.READ_WRITE);
// Close it
p.close();
// Delete it
assertTrue(tempFile.delete());
-
+
// Reset
FileHelper.copyFile(origFile, tempFile);
p = OPCPackage.open(tempFile.toString(), PackageAccess.READ_WRITE);
-
+
// Save it to the same file - not allowed
try {
p.save(tempFile);
p.close();
// Delete it
assertTrue(tempFile.delete());
-
-
+
+
// Open it read only, then close and delete - allowed
FileHelper.copyFile(origFile, tempFile);
p = OPCPackage.open(tempFile.toString(), PackageAccess.READ);
p.close();
assertTrue(tempFile.delete());
}
+
/**
* Test that we can open a file by path, save it
* to another file, then delete both
File tempFile2 = TempFile.createTempFile("poiTesting","tmp");
File origFile = OpenXML4JTestDataSamples.getSampleFile("TestPackageCommon.docx");
FileHelper.copyFile(origFile, tempFile);
-
+
// Open the temp file
- OPCPackage p = OPCPackage.open(tempFile.toString(), PackageAccess.READ_WRITE);
+ try (OPCPackage p = OPCPackage.open(tempFile.toString(), PackageAccess.READ_WRITE)) {
+ // Save it to a different file
+ p.save(tempFile2);
+ }
- // Save it to a different file
- p.save(tempFile2);
- p.close();
-
// Delete both the files
assertTrue(tempFile.delete());
assertTrue(tempFile2.delete());
try {
List<PackagePart> rs = pkg.getPartsByName(Pattern.compile("/word/.*?\\.xml"));
HashMap<String, PackagePart> selected = new HashMap<>();
-
+
for(PackagePart p : rs)
selected.put(p.getPartName().getName(), p);
-
+
assertEquals(6, selected.size());
assertTrue(selected.containsKey("/word/document.xml"));
assertTrue(selected.containsKey("/word/fontTable.xml"));
pkg.revert();
}
}
-
+
@Test
public void getPartSize() throws IOException, InvalidFormatException {
String filepath = OpenXML4JTestDataSamples.getSampleFileName("sample.docx");
p.revert();
is.close();
}
-
+
/**
* Verify we give helpful exceptions (or as best we can) when
* supplied with non-OOXML file types (eg OLE2, ODF)
public void NonOOXMLFileTypes() throws Exception {
// Spreadsheet has a good mix of alternate file types
POIDataSamples files = POIDataSamples.getSpreadSheetInstance();
-
+
// OLE2 - Stream
try {
try (InputStream stream = files.openResourceAsStream("SampleSS.xls")) {
assertTrue(e.getMessage().contains("The supplied data appears to be in the OLE2 Format"));
assertTrue(e.getMessage().contains("You are calling the part of POI that deals with OOXML"));
}
-
+
// Raw XML - Stream
try {
try (InputStream stream = files.openResourceAsStream("SampleSS.xml")) {
assertTrue(e.getMessage().contains("The supplied data appears to be a raw XML file"));
assertTrue(e.getMessage().contains("Formats such as Office 2003 XML"));
}
-
+
// ODF / ODS - Stream
try {
try (InputStream stream = files.openResourceAsStream("SampleSS.ods")) {
assertTrue(e.toString().contains("The supplied data appears to be in ODF"));
assertTrue(e.toString().contains("Formats like these (eg ODS"));
}
-
+
// Plain Text - Stream
try {
try (InputStream stream = files.openResourceAsStream("SampleSS.txt")) {
append.write(spam);
size += spam.length;
}
- append.write("</Types>".getBytes("UTF-8"));
+ append.write("</Types>".getBytes(StandardCharsets.UTF_8));
size += 8;
eOut.setSize(size);
} else {
ZipSecureFile.setMaxTextSize(before);
}
}
-
+
// bug 60128
@Test(expected=NotOfficeXmlFileException.class)
public void testCorruptFile() throws InvalidFormatException {
new PushbackInputStream(new FileInputStream(hssf), 2),
new BufferedInputStream(new FileInputStream(hssf), 2),
};
-
+
try {
for (InputStream is : isList) {
WorkbookFactory.create(is).close();
public void testBug62592SequentialCallsToGetParts() throws Exception {
//make absolutely certain that sequential calls don't throw InvalidFormatExceptions
String originalFile = OpenXML4JTestDataSamples.getSampleFileName("TestPackageCommon.docx");
- OPCPackage p2 = OPCPackage.open(originalFile, PackageAccess.READ);
- p2.getParts();
- p2.getParts();
+ try (OPCPackage p2 = OPCPackage.open(originalFile, PackageAccess.READ)) {
+ p2.getParts();
+ p2.getParts();
+ }
}
@Test
}
}
+ @SuppressWarnings("UnstableApiUsage")
@Test
public void testBug63029() throws Exception {
File testFile = OpenXML4JTestDataSamples.getSampleFile("sample.docx");
ex = e;
}
// verify there was an exception while closing the file
+ assertNotNull("Fail to save: an error occurs while saving the package : Bugzilla 63029", ex);
assertEquals("Fail to save: an error occurs while saving the package : Bugzilla 63029", ex.getMessage());
// assert that md5 after closing is the same, i.e. the source is left intact
"backgrounds.pptx, layouts.pptx, sample.pptx, shapes.pptx, 54880_chinese.ppt, keyframes.pptx," +
"customGeo.pptx, customGeo.ppt, wrench.emf, santa.wmf";
-
-
@BeforeClass
public static void checkHslf() {
try {
}
// use filename instead of File object to omit full pathname in test name
- @Parameter(value = 0)
+ @Parameter
public String pptFile;
@SuppressWarnings("ConstantConditions")
Path path = XSSFTestDataSamples.getSampleFile("48936-strings.txt").toPath();
- Stream<String> lines = Files.lines(path, StandardCharsets.UTF_8);
- List<String> lst = lines
- .map(String::trim)
- .filter(((Predicate<String>)String::isEmpty).negate())
- .collect(Collectors.toList());
- lines.close();
+ final List<String> lst;
+ try (Stream<String> lines = Files.lines(path, StandardCharsets.UTF_8)) {
+ lst = lines
+ .map(String::trim)
+ .filter(((Predicate<String>) String::isEmpty).negate())
+ .collect(Collectors.toList());
+ }
for (String str : lst) {
s.createRow(i++).createCell(0).setCellValue(str);
@Test
public void test54764WithSAXHelper() throws Exception {
File testFile = XSSFTestDataSamples.getSampleFile("54764.xlsx");
- ZipFile zip = new ZipFile(testFile);
- ZipArchiveEntry ze = zip.getEntry("xl/sharedStrings.xml");
- XMLReader reader = XMLHelper.newXMLReader();
- try {
- reader.parse(new InputSource(zip.getInputStream(ze)));
- fail("should have thrown SAXParseException");
- } catch (SAXParseException e) {
- assertNotNull(e.getMessage());
- assertTrue(e.getMessage().contains("more than \"1\" entity"));
+ try (ZipFile zip = new ZipFile(testFile)) {
+ ZipArchiveEntry ze = zip.getEntry("xl/sharedStrings.xml");
+ XMLReader reader = XMLHelper.newXMLReader();
+ try {
+ reader.parse(new InputSource(zip.getInputStream(ze)));
+ fail("should have thrown SAXParseException");
+ } catch (SAXParseException e) {
+ assertNotNull(e.getMessage());
+ assertTrue(e.getMessage().contains("more than \"1\" entity"));
+ }
}
}
@Test
public void test54764WithDocumentHelper() throws Exception {
File testFile = XSSFTestDataSamples.getSampleFile("54764.xlsx");
- ZipFile zip = new ZipFile(testFile);
- ZipArchiveEntry ze = zip.getEntry("xl/sharedStrings.xml");
- try {
- DocumentHelper.readDocument(zip.getInputStream(ze));
- fail("should have thrown SAXParseException");
- } catch (SAXParseException e) {
- assertNotNull(e.getMessage());
- assertNotEquals(isOldXercesActive(), e.getMessage().contains("DOCTYPE is disallowed when the feature"));
+ try (ZipFile zip = new ZipFile(testFile)) {
+ ZipArchiveEntry ze = zip.getEntry("xl/sharedStrings.xml");
+ try {
+ DocumentHelper.readDocument(zip.getInputStream(ze));
+ fail("should have thrown SAXParseException");
+ } catch (SAXParseException e) {
+ assertNotNull(e.getMessage());
+ assertNotEquals(isOldXercesActive(), e.getMessage().contains("DOCTYPE is disallowed when the feature"));
+ }
}
}
assertEquals(FontCharset.ARABIC.getValue(), xssfFont.getCharSet());
xssfFont.setCharSet((byte)(FontCharset.ARABIC.getValue()));
assertEquals(FontCharset.ARABIC.getValue(), xssfFont.getCharSet());
-
+
// This one isn't allowed
assertNull(FontCharset.valueOf(9999));
try {
} catch(POIXMLException e) {
// expected here
}
-
-
+
+
// Now try with a few sample files
-
+
// Normal charset
XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("Formatting.xlsx");
- assertEquals(0,
+ assertEquals(0,
wb1.getSheetAt(0).getRow(0).getCell(0).getCellStyle().getFont().getCharSet()
);
wb1.close();
-
+
// GB2312 charset
XSSFWorkbook wb2 = XSSFTestDataSamples.openSampleWorkbook("49273.xlsx");
- assertEquals(134,
+ assertEquals(134,
wb2.getSheetAt(0).getRow(0).getCell(0).getCellStyle().getFont().getCharSet()
);
wb2.close();
assertEquals(ctFont.getColorArray(0).getRgb()[3],xssfFont.getXSSFColor().getRGB()[3]);
xssfFont.setColor((short)23);
-
+
byte[] bytes = Integer.toHexString(0xF1F1F1).getBytes(LocaleUtil.CHARSET_1252);
color.setRgb(bytes);
XSSFColor newColor=XSSFColor.from(color, null);
xssfFont.setColor(newColor);
assertEquals(ctFont.getColorArray(0).getRgb()[2],newColor.getRGB()[2]);
-
+
assertArrayEquals(bytes, xssfFont.getXSSFColor().getRGB());
assertEquals(0, xssfFont.getColor());
}
// cannot check on result because on some machines we get back false here!
SheetUtil.canComputeColumnWidth(wb.getFontAt(0));
- wb.close();
+ wb.close();
}
// store test from TestSheetUtil here as it uses XSSF
@Test
- public void testCanComputeWidthInvalidFont() throws IOException {
+ public void testCanComputeWidthInvalidFont() {
Font font = new XSSFFont(CTFont.Factory.newInstance());
font.setFontName("some non existing font name");
-
- // Even with invalid fonts we still get back useful data most of the time...
+
+ // Even with invalid fonts we still get back useful data most of the time...
SheetUtil.canComputeColumnWidth(font);
}
wb.close();
}
+
+ @Test
+ public void testEquals() {
+ XSSFFont font = new XSSFFont();
+ XSSFFont equ = new XSSFFont();
+ XSSFFont notequ = new XSSFFont();
+ notequ.setItalic(true);
+
+ assertEquals(equ, font);
+ assertNotEquals(font, notequ);
+
+ notequ.setItalic(false);
+ notequ.setThemeColor((short)123);
+ assertNotEquals(font, notequ);
+ }
}
import java.awt.geom.AffineTransform;
import java.awt.geom.Dimension2D;
import java.awt.geom.Rectangle2D;
-import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
private final List<HemfRecord> records = new ArrayList<>();
private boolean isParsed = false;
- public HemfPicture(InputStream is) throws IOException {
+ public HemfPicture(InputStream is) {
this(new LittleEndianInputStream(is));
}
- public HemfPicture(LittleEndianInputStream is) throws IOException {
+ public HemfPicture(LittleEndianInputStream is) {
stream = is;
}
prop.setViewportOrg(emfBounds.getX(), emfBounds.getY());
prop.setViewportExt(emfBounds.getWidth(), emfBounds.getHeight());
- int idx = 0;
for (HemfRecord r : getRecords()) {
try {
g.draw(r);
} catch (RuntimeException ignored) {
}
- idx++;
}
} finally {
ctx.setTransform(at);
}
public static void main(String[] args) throws Exception {
+ if(args.length == 0) {
+ System.err.println("Usage: OutlookTextExtractor <file> [<file> ...]");
+ System.exit(1);
+ }
for (String filename : args) {
try (POIFSFileSystem poifs = new POIFSFileSystem(new File(filename));
OutlookTextExtractor extractor = new OutlookTextExtractor(poifs)) {
size += SprmUtils.addSprm((short)0x6a09, 0, varParam, sprmList);
}
- if (newCHP.isFOle2() != newCHP.isFOle2())
+ if (newCHP.isFOle2() != oldCHP.isFOle2())
{
int value = 0;
if (newCHP.isFOle2())