Skip to content

Commit

Permalink
Deprecate methods with typos and provide new ones.
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1819776 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
centic9 committed Jan 1, 2018
1 parent 0fd44af commit 859ce54
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 59 deletions.
87 changes: 38 additions & 49 deletions src/ooxml/java/org/apache/poi/extractor/ExtractorFactory.java
Expand Up @@ -55,6 +55,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
import org.apache.poi.util.NotImplemented;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
import org.apache.poi.util.Removal;
import org.apache.poi.xdgf.extractor.XDGFVisioExtractor;
import org.apache.poi.xslf.extractor.XSLFPowerPointExtractor;
import org.apache.poi.xslf.usermodel.XSLFRelation;
Expand Down Expand Up @@ -136,42 +137,20 @@ public static POITextExtractor createExtractor(File f) throws IOException, OpenX
POIOLE2TextExtractor extractor = createExtractor(fs);
extractor.setFilesystem(fs);
return extractor;

} catch (OfficeXmlFileException e) {
// ensure file-handle release
IOUtils.closeQuietly(fs);
return createExtractor(OPCPackage.open(f.toString(), PackageAccess.READ));

} catch (NotOLE2FileException ne) {
// ensure file-handle release
IOUtils.closeQuietly(fs);
throw new IllegalArgumentException("Your File was neither an OLE2 file, nor an OOXML file");

} catch (OpenXML4JException e) {
// ensure file-handle release
IOUtils.closeQuietly(fs);
throw e;

} catch (XmlException e) {
// ensure file-handle release
IOUtils.closeQuietly(fs);
throw e;

} catch (IOException e) {
// ensure file-handle release
IOUtils.closeQuietly(fs);
throw e;

} catch (RuntimeException e) {
// ensure file-handle release
IOUtils.closeQuietly(fs);
throw e;
} catch (Error e) {
} catch (OpenXML4JException | Error | RuntimeException | IOException | XmlException e) {
// ensure file-handle release
IOUtils.closeQuietly(fs);
throw e;
}
}
}

public static POITextExtractor createExtractor(InputStream inp) throws IOException, OpenXML4JException, XmlException {
InputStream is = FileMagic.prepareToCheckMagic(inp);
Expand Down Expand Up @@ -265,27 +244,7 @@ public static POIXMLTextExtractor createExtractor(OPCPackage pkg) throws IOExcep

throw new IllegalArgumentException("No supported documents found in the OOXML package (found "+contentType+")");

} catch (IOException e) {
// ensure that we close the package again if there is an error opening it, however
// we need to revert the package to not re-write the file via close(), which is very likely not wanted for a TextExtractor!
pkg.revert();
throw e;
} catch (OpenXML4JException e) {
// ensure that we close the package again if there is an error opening it, however
// we need to revert the package to not re-write the file via close(), which is very likely not wanted for a TextExtractor!
pkg.revert();
throw e;
} catch (XmlException e) {
// ensure that we close the package again if there is an error opening it, however
// we need to revert the package to not re-write the file via close(), which is very likely not wanted for a TextExtractor!
pkg.revert();
throw e;
} catch (RuntimeException e) {
// ensure that we close the package again if there is an error opening it, however
// we need to revert the package to not re-write the file via close(), which is very likely not wanted for a TextExtractor!
pkg.revert();
throw e;
} catch (Error e) {
} catch (IOException | Error | RuntimeException | XmlException | OpenXML4JException e) {
// ensure that we close the package again if there is an error opening it, however
// we need to revert the package to not re-write the file via close(), which is very likely not wanted for a TextExtractor!
pkg.revert();
Expand Down Expand Up @@ -323,8 +282,23 @@ public static POITextExtractor createExtractor(DirectoryNode poifsDir) throws IO
* If there are no embedded documents, you'll get back an
* empty array. Otherwise, you'll get one open
* {@link POITextExtractor} for each embedded file.
*
* @deprecated Use the method with correct "embedded"
*/
@Deprecated
@Removal(version="4.2")
public static POITextExtractor[] getEmbededDocsTextExtractors(POIOLE2TextExtractor ext) throws IOException, OpenXML4JException, XmlException {
return getEmbeddedDocsTextExtractors(ext);
}

/**
* Returns an array of text extractors, one for each of
* the embedded documents in the file (if there are any).
* If there are no embedded documents, you'll get back an
* empty array. Otherwise, you'll get one open
* {@link POITextExtractor} for each embedded file.
*/
public static POITextExtractor[] getEmbeddedDocsTextExtractors(POIOLE2TextExtractor ext) throws IOException, OpenXML4JException, XmlException {
// All the embedded directories we spotted
ArrayList<Entry> dirs = new ArrayList<>();
// For anything else not directly held in as a POIFS directory
Expand Down Expand Up @@ -392,9 +366,7 @@ public static POITextExtractor[] getEmbededDocsTextExtractors(POIOLE2TextExtract
// Ignore, just means it didn't contain
// a format we support as yet
logger.log(POILogger.INFO, "Format not supported yet", e.getLocalizedMessage());
} catch (XmlException e) {
throw new IOException(e.getMessage(), e);
} catch (OpenXML4JException e) {
} catch (XmlException | OpenXML4JException e) {
throw new IOException(e.getMessage(), e);
}
}
Expand All @@ -407,10 +379,27 @@ public static POITextExtractor[] getEmbededDocsTextExtractors(POIOLE2TextExtract
* If there are no embedded documents, you'll get back an
* empty array. Otherwise, you'll get one open
* {@link POITextExtractor} for each embedded file.
*
* @deprecated Use the method with correct "embedded"
*/
@Deprecated
@Removal(version="4.2")
@NotImplemented
@SuppressWarnings("UnusedParameters")
@SuppressWarnings({"UnusedParameters", "UnusedReturnValue"})
public static POITextExtractor[] getEmbededDocsTextExtractors(POIXMLTextExtractor ext) {
return getEmbeddedDocsTextExtractors(ext);
}

/**
* Returns an array of text extractors, one for each of
* the embedded documents in the file (if there are any).
* If there are no embedded documents, you'll get back an
* empty array. Otherwise, you'll get one open
* {@link POITextExtractor} for each embedded file.
*/
@NotImplemented
@SuppressWarnings({"UnusedParameters", "UnusedReturnValue"})
public static POITextExtractor[] getEmbeddedDocsTextExtractors(POIXMLTextExtractor ext) {
throw new IllegalStateException("Not yet supported");
}

Expand Down
Expand Up @@ -48,14 +48,12 @@ Licensed to the Apache Software Foundation (ASF) under one or more
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.poifs.filesystem.OPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
import org.apache.poi.xdgf.extractor.XDGFVisioExtractor;
import org.apache.poi.xslf.extractor.XSLFPowerPointExtractor;
import org.apache.poi.xssf.extractor.XSSFEventBasedExcelExtractor;
import org.apache.poi.xssf.extractor.XSSFExcelExtractor;
import org.apache.poi.xssf.usermodel.TestMatrixFormulasFromXMLSpreadsheet;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.junit.BeforeClass;
import org.junit.Test;
Expand Down Expand Up @@ -779,26 +777,40 @@ public void testPreferEventBased() throws Exception {
}

/**
* Test embeded docs text extraction. For now, only
* does poifs embeded, but will do ooxml ones
* Test embedded docs text extraction. For now, only
* does poifs embedded, but will do ooxml ones
* at some point.
*/
@SuppressWarnings("deprecation")
@Test
public void testEmbeded() throws Exception {
public void testEmbedded() throws Exception {
POIOLE2TextExtractor ext;
POITextExtractor[] embeds;

// No embedings
// No embeddings
ext = (POIOLE2TextExtractor)
ExtractorFactory.createExtractor(xls);
embeds = ExtractorFactory.getEmbededDocsTextExtractors(ext);
assertEquals(0, embeds.length);
ext.close();

// No embeddings
ext = (POIOLE2TextExtractor)
ExtractorFactory.createExtractor(xls);
embeds = ExtractorFactory.getEmbeddedDocsTextExtractors(ext);
assertEquals(0, embeds.length);
ext.close();

// Excel
ext = (POIOLE2TextExtractor)
ExtractorFactory.createExtractor(xlsEmb);
embeds = ExtractorFactory.getEmbededDocsTextExtractors(ext);
assertNotNull(embeds);

// Excel
ext = (POIOLE2TextExtractor)
ExtractorFactory.createExtractor(xlsEmb);
embeds = ExtractorFactory.getEmbeddedDocsTextExtractors(ext);

assertEquals(6, embeds.length);
int numWord = 0, numXls = 0, numPpt = 0, numMsg = 0, numWordX;
Expand Down Expand Up @@ -1016,6 +1028,7 @@ public void bug59074() throws Exception {
}
}

@SuppressWarnings("deprecation")
@Test
public void testGetEmbeddedFromXMLExtractor() {
try {
Expand All @@ -1025,20 +1038,25 @@ public void testGetEmbeddedFromXMLExtractor() {
} catch (IllegalStateException e) {
// expected here
}

try {
// currently not implemented
ExtractorFactory.getEmbeddedDocsTextExtractors((POIXMLTextExtractor)null);
fail("Unsupported currently");
} catch (IllegalStateException e) {
// expected here
}
}

// This bug is currently open. This test will fail with "expected error not thrown" when the bug has been fixed.
// When this happens, change this from @Test(expected=...) to @Test
// bug 45565: text within TextBoxes is extracted by ExcelExtractor and WordExtractor
@Test(expected=AssertionError.class)
public void test45565() throws Exception {
POITextExtractor extractor = ExtractorFactory.createExtractor(HSSFTestDataSamples.getSampleFile("45565.xls"));
try {
try (POITextExtractor extractor = ExtractorFactory.createExtractor(HSSFTestDataSamples.getSampleFile("45565.xls"))) {
String text = extractor.getText();
assertContains(text, "testdoc");
assertContains(text, "test phrase");
} finally {
extractor.close();
}
}
}

0 comments on commit 859ce54

Please sign in to comment.