diff options
Diffstat (limited to 'src/ooxml')
12 files changed, 107 insertions, 86 deletions
diff --git a/src/ooxml/java/org/apache/poi/ooxml/POIXMLDocumentPart.java b/src/ooxml/java/org/apache/poi/ooxml/POIXMLDocumentPart.java index 54fa790eca..c8e1a277a4 100644 --- a/src/ooxml/java/org/apache/poi/ooxml/POIXMLDocumentPart.java +++ b/src/ooxml/java/org/apache/poi/ooxml/POIXMLDocumentPart.java @@ -615,7 +615,7 @@ public class POIXMLDocumentPart { protected void read(POIXMLFactory factory, Map<PackagePart, POIXMLDocumentPart> context) throws OpenXML4JException { PackagePart pp = getPackagePart(); - if (pp.getContentType().equals(XWPFRelation.TEMPLATE.getContentType())) { + if (pp.getContentType().equals(XWPFRelation.GLOSSARY_DOCUMENT.getContentType())) { logger.log(POILogger.WARN, "POI does not currently support template.main+xml (glossary) parts. " + "Skipping this part for now."); diff --git a/src/ooxml/java/org/apache/poi/ooxml/extractor/CommandLineTextExtractor.java b/src/ooxml/java/org/apache/poi/ooxml/extractor/CommandLineTextExtractor.java index 999abd46ee..c3d429b3c7 100644 --- a/src/ooxml/java/org/apache/poi/ooxml/extractor/CommandLineTextExtractor.java +++ b/src/ooxml/java/org/apache/poi/ooxml/extractor/CommandLineTextExtractor.java @@ -22,41 +22,37 @@ import org.apache.poi.extractor.POITextExtractor; /** * A command line wrapper around {@link ExtractorFactory}, useful - * for when debugging. + * for when debugging. */ public class CommandLineTextExtractor { - public static final String DIVIDER = "======================="; - - public static void main(String[] args) throws Exception { - if(args.length < 1) { - System.err.println("Use:"); - System.err.println(" CommandLineTextExtractor <filename> [filename] [filename]"); - System.exit(1); - } - - for (String arg : args) { - System.out.println(DIVIDER); - - File f = new File(arg); - System.out.println(f); - - POITextExtractor extractor = - ExtractorFactory.createExtractor(f); - try { - POITextExtractor metadataExtractor = - extractor.getMetadataTextExtractor(); - - System.out.println(" " + DIVIDER); - String metaData = metadataExtractor.getText(); - System.out.println(metaData); - System.out.println(" " + DIVIDER); - String text = extractor.getText(); - System.out.println(text); - System.out.println(DIVIDER); - System.out.println("Had " + metaData.length() + " characters of metadata and " + text.length() + " characters of text"); - } finally { - extractor.close(); - } - } - } + public static final String DIVIDER = "======================="; + + public static void main(String[] args) throws Exception { + if (args.length < 1) { + System.err.println("Use:"); + System.err.println(" CommandLineTextExtractor <filename> [filename] [filename]"); + System.exit(1); + } + + for (String arg : args) { + System.out.println(DIVIDER); + + File f = new File(arg); + System.out.println(f); + + try (POITextExtractor extractor = ExtractorFactory.createExtractor(f)) { + POITextExtractor metadataExtractor = + extractor.getMetadataTextExtractor(); + + System.out.println(" " + DIVIDER); + String metaData = metadataExtractor.getText(); + System.out.println(metaData); + System.out.println(" " + DIVIDER); + String text = extractor.getText(); + System.out.println(text); + System.out.println(DIVIDER); + System.out.println("Had " + metaData.length() + " characters of metadata and " + text.length() + " characters of text"); + } + } + } } diff --git a/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/ZipHelper.java b/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/ZipHelper.java index 094e89c6d7..0169909c9b 100644 --- a/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/ZipHelper.java +++ b/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/ZipHelper.java @@ -154,8 +154,6 @@ public final class ZipHelper { "The supplied data appears to be a raw XML file. " + "Formats such as Office 2003 XML are not supported"); default: - case OOXML: - case UNKNOWN: // Don't check for a Zip header, as to maintain backwards // compatibility we need to let them seek over junk at the // start before beginning processing. diff --git a/src/ooxml/java/org/apache/poi/openxml4j/util/ZipInputStreamZipEntrySource.java b/src/ooxml/java/org/apache/poi/openxml4j/util/ZipInputStreamZipEntrySource.java index e185239ff1..0a56966d4f 100644 --- a/src/ooxml/java/org/apache/poi/openxml4j/util/ZipInputStreamZipEntrySource.java +++ b/src/ooxml/java/org/apache/poi/openxml4j/util/ZipInputStreamZipEntrySource.java @@ -38,7 +38,7 @@ public class ZipInputStreamZipEntrySource implements ZipEntrySource { /** * Reads all the entries from the ZipInputStream - * into memory, and closes the source stream. + * into memory, and don't close (since POI 4.0.1) the source stream. * We'll then eat lots of memory, but be able to * work with the entries at-will. */ @@ -50,7 +50,6 @@ public class ZipInputStreamZipEntrySource implements ZipEntrySource { } zipEntries.put(zipEntry.getName(), new ZipArchiveFakeEntry(zipEntry, inp)); } - inp.close(); } @Override diff --git a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFPictureShape.java b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFPictureShape.java index 97b4ab8cb0..9f07b18bac 100644 --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFPictureShape.java +++ b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFPictureShape.java @@ -170,16 +170,24 @@ public class XSLFPictureShape extends XSLFSimpleShape @SuppressWarnings("WeakerAccess") protected String getBlipLink(){ - String link = getBlip().getLink(); - if (link.isEmpty()) return null; - return link; + CTBlip blip = getBlip(); + if (blip != null) { + String link = blip.getLink(); + return (link.isEmpty()) ? null : link; + } else { + return null; + } } @SuppressWarnings("WeakerAccess") protected String getBlipId(){ - String id = getBlip().getEmbed(); - if (id.isEmpty()) return null; - return id; + CTBlip blip = getBlip(); + if (blip != null) { + String id = blip.getEmbed(); + return (id.isEmpty()) ? null : id; + } else { + return null; + } } @Override diff --git a/src/ooxml/testcases/org/apache/poi/ooxml/TestPOIXMLProperties.java b/src/ooxml/testcases/org/apache/poi/ooxml/TestPOIXMLProperties.java index d91b8191eb..1ad2a633f9 100644 --- a/src/ooxml/testcases/org/apache/poi/ooxml/TestPOIXMLProperties.java +++ b/src/ooxml/testcases/org/apache/poi/ooxml/TestPOIXMLProperties.java @@ -19,6 +19,7 @@ package org.apache.poi.ooxml; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -89,7 +90,7 @@ public final class TestPOIXMLProperties { XSSFWorkbook newWorkbook = XSSFTestDataSamples.writeOutAndReadBack(workbook); workbook.close(); - assertTrue(workbook != newWorkbook); + assertNotSame(workbook, newWorkbook); POIXMLProperties newProps = newWorkbook.getProperties(); @@ -158,7 +159,7 @@ public final class TestPOIXMLProperties { p = ctProps.getPropertyArray(3); assertEquals("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}", p.getFmtid()); assertEquals("test-4", p.getName()); - assertEquals(true, p.getBool()); + assertTrue(p.getBool()); assertEquals(5, p.getPid()); wb2.close(); diff --git a/src/ooxml/testcases/org/apache/poi/xddf/usermodel/TestNecessaryOOXMLClasses.java b/src/ooxml/testcases/org/apache/poi/xddf/usermodel/TestNecessaryOOXMLClasses.java index 74ec1bb6cf..6cbf24ab6f 100644 --- a/src/ooxml/testcases/org/apache/poi/xddf/usermodel/TestNecessaryOOXMLClasses.java +++ b/src/ooxml/testcases/org/apache/poi/xddf/usermodel/TestNecessaryOOXMLClasses.java @@ -87,6 +87,10 @@ public class TestNecessaryOOXMLClasses { Assert.assertNotNull(ctLblAlgn); CTDashStopList ctDashStopList = CTDashStopList.Factory.newInstance(); Assert.assertNotNull(ctDashStopList); + STDispBlanksAs stDashBlanksAs = STDispBlanksAs.Factory.newInstance(); + Assert.assertNotNull(stDashBlanksAs); + CTDispBlanksAs ctDashBlanksAs = CTDispBlanksAs.Factory.newInstance(); + Assert.assertNotNull(ctDashBlanksAs); STLblAlgn.Enum e1 = STLblAlgn.Enum.forString("ctr"); Assert.assertNotNull(e1); @@ -100,6 +104,8 @@ public class TestNecessaryOOXMLClasses { Assert.assertNotNull(e5); STMarkerStyle.Enum e6 = STMarkerStyle.Enum.forString("circle"); Assert.assertNotNull(e6); + STDispBlanksAs.Enum e7 = STDispBlanksAs.Enum.forString("span"); + Assert.assertNotNull(e7); CTTextBulletTypefaceFollowText ctTextBulletTypefaceFollowText = CTTextBulletTypefaceFollowText.Factory.newInstance(); Assert.assertNotNull(ctTextBulletTypefaceFollowText); diff --git a/src/ooxml/testcases/org/apache/poi/xdgf/extractor/TestXDGFVisioExtractor.java b/src/ooxml/testcases/org/apache/poi/xdgf/extractor/TestXDGFVisioExtractor.java index 6a3369ed90..c261e2c91b 100644 --- a/src/ooxml/testcases/org/apache/poi/xdgf/extractor/TestXDGFVisioExtractor.java +++ b/src/ooxml/testcases/org/apache/poi/xdgf/extractor/TestXDGFVisioExtractor.java @@ -42,7 +42,7 @@ public class TestXDGFVisioExtractor { } @After - public void closeResoures() throws IOException { + public void closeResources() throws IOException { if(xml != null) { xml.close(); } diff --git a/src/ooxml/testcases/org/apache/poi/xslf/TestXSLFBugs.java b/src/ooxml/testcases/org/apache/poi/xslf/TestXSLFBugs.java index 48b0816993..8dbc8ca23e 100644 --- a/src/ooxml/testcases/org/apache/poi/xslf/TestXSLFBugs.java +++ b/src/ooxml/testcases/org/apache/poi/xslf/TestXSLFBugs.java @@ -17,12 +17,7 @@ package org.apache.poi.xslf; import static org.apache.poi.POITestCase.assertContains; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.Assert.*; import java.awt.Color; import java.awt.Dimension; @@ -94,6 +89,25 @@ public class TestXSLFBugs { private static final POIDataSamples slTests = POIDataSamples.getSlideShowInstance(); @Test + public void bug62929() throws Exception { + try(XMLSlideShow ss1 = XSLFTestDataSamples.openSampleDocument("missing-blip-fill.pptx")) { + assertEquals(1, ss1.getSlides().size()); + + XSLFSlide slide = ss1.getSlides().get(0); + + assertEquals(slide.getShapes().size(), 1); + + XSLFPictureShape picture = (XSLFPictureShape)slide.getShapes().get(0); + + assertEquals(picture.getShapeId(), 662); + assertFalse(picture.isExternalLinkedPicture()); + assertNull(picture.getPictureData()); + assertNull(picture.getPictureLink()); + assertNull(picture.getClipping()); + } + } + + @Test public void bug62736() throws Exception { XMLSlideShow ss1 = XSLFTestDataSamples.openSampleDocument("bug62736.pptx"); diff --git a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestPPTX2PNG.java b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestPPTX2PNG.java index 94205da144..199a9f6286 100644 --- a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestPPTX2PNG.java +++ b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestPPTX2PNG.java @@ -47,7 +47,7 @@ public class TestPPTX2PNG { private static final POIDataSamples samples = POIDataSamples.getSlideShowInstance(); private static final File basedir = null; private static final String files = - "53446.ppt, alterman_security.ppt, alterman_security.pptx, KEY02.pptx, themes.pptx, backgrounds.pptx, layouts.pptx, sample.pptx, shapes.pptx, 54880_chinese.ppt"; + "53446.ppt, alterman_security.ppt, alterman_security.pptx, KEY02.pptx, themes.pptx, backgrounds.pptx, layouts.pptx, sample.pptx, shapes.pptx, 54880_chinese.ppt, keyframes.pptx"; diff --git a/src/ooxml/testcases/org/apache/poi/xssf/XSSFTestDataSamples.java b/src/ooxml/testcases/org/apache/poi/xssf/XSSFTestDataSamples.java index 0f03af7453..b981b9994e 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/XSSFTestDataSamples.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/XSSFTestDataSamples.java @@ -70,7 +70,7 @@ public class XSSFTestDataSamples { * @param wb the workbook to write * @param testName a fragment of the filename * @return the location where the workbook was saved - * @throws IOException + * @throws IOException If writing the file fails */ public static <R extends Workbook> File writeOut(R wb, String testName) throws IOException { final File file = getOutputFile(testName); @@ -104,7 +104,9 @@ public class XSSFTestDataSamples { file = TempFile.createTempFile(testName, ".xlsx"); } if (file.exists()) { - file.delete(); + if(!file.delete()) { + throw new IOException("Could not delete file " + file); + } } return file; } @@ -114,7 +116,7 @@ public class XSSFTestDataSamples { * * @param wb the workbook to write * @return the memory buffer - * @throws IOException + * @throws IOException If writing the file fails */ public static <R extends Workbook> ByteArrayOutputStream writeOut(R wb) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(8192); @@ -137,7 +139,7 @@ public class XSSFTestDataSamples { * to avoid creating a temporary file. However, this may complicate the calling * code to avoid having the workbook, BAOS, and BAIS open at the same time. * - * @param wb + * @param wb The workbook to write out, it is closed after the call. * @param testName file name to be used to write to a file. This file will be cleaned up by a call to readBack(String) * @return workbook location * @throws RuntimeException if {@link #TEST_OUTPUT_DIR} System property is not set @@ -161,18 +163,13 @@ public class XSSFTestDataSamples { * * @param wb the workbook to write * @return the memory buffer - * @throws IOException + * @throws RuntimeException If writing the file fails */ - public static <R extends Workbook> ByteArrayOutputStream writeOutAndClose(R wb) { - try { - ByteArrayOutputStream out = writeOut(wb); - // Do not close the workbook if there was a problem writing the workbook - wb.close(); - return out; - } - catch (final IOException e) { - throw new RuntimeException(e); - } + public static <R extends Workbook> ByteArrayOutputStream writeOutAndClose(R wb) throws IOException { + ByteArrayOutputStream out = writeOut(wb); + // Do not close the workbook if there was a problem writing the workbook + wb.close(); + return out; } /** @@ -183,12 +180,14 @@ public class XSSFTestDataSamples { * * @param file the workbook file to read and delete * @return the read back workbook - * @throws IOException + * @throws IOException If reading or deleting the file fails */ public static XSSFWorkbook readBackAndDelete(File file) throws IOException { XSSFWorkbook wb = readBack(file); // do not delete the file if there's an error--might be helpful for debugging - file.delete(); + if(!file.delete()) { + throw new IOException("Could not delete file " + file + " after reading"); + } return wb; } @@ -198,16 +197,12 @@ public class XSSFTestDataSamples { * * @param file the workbook file to read * @return the read back workbook - * @throws IOException + * @throws IOException If reading the file fails */ public static XSSFWorkbook readBack(File file) throws IOException { - InputStream in = new FileInputStream(file); - try { + try (InputStream in = new FileInputStream(file)) { return new XSSFWorkbook(in); } - finally { - in.close(); - } } /** @@ -216,17 +211,13 @@ public class XSSFTestDataSamples { * * @param out the output stream to read back from * @return the read back workbook - * @throws IOException + * @throws IOException If reading the file fails */ public static XSSFWorkbook readBack(ByteArrayOutputStream out) throws IOException { - InputStream is = new ByteArrayInputStream(out.toByteArray()); - out.close(); - try { + try (InputStream is = new ByteArrayInputStream(out.toByteArray())) { + out.close(); return new XSSFWorkbook(is); } - finally { - is.close(); - } } /** diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/extractor/TestXWPFWordExtractor.java b/src/ooxml/testcases/org/apache/poi/xwpf/extractor/TestXWPFWordExtractor.java index 0fcccd1838..30d943051f 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/extractor/TestXWPFWordExtractor.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/extractor/TestXWPFWordExtractor.java @@ -452,4 +452,12 @@ public class TestXWPFWordExtractor extends TestCase { //once we add processing for this, we can change this to contains assertNotContained(txt, "table rows"); } + + public void testPartsInTemplate() throws IOException { + XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("60316b.dotx"); + XWPFWordExtractor extractor = new XWPFWordExtractor(doc); + String txt = extractor.getText(); + assertContains(txt, "header 2"); + assertContains(txt, "footer 1"); + } } |