mirror of https://github.com/apache/poi.git
Change order of handling shape-types in copy() as FreeFormShape derives from AutoShape and thus would have not been copied correctly currently
Fix some Javadoc, some warnings and code duplications git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1816185 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
e93f78cd13
commit
db15fe7778
|
@ -146,7 +146,8 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
|
||||||
}
|
}
|
||||||
|
|
||||||
HSSFSheet cloneSheet(HSSFWorkbook workbook) {
|
HSSFSheet cloneSheet(HSSFWorkbook workbook) {
|
||||||
this.getDrawingPatriarch();/**Aggregate drawing records**/
|
// Aggregate drawing records
|
||||||
|
this.getDrawingPatriarch();
|
||||||
HSSFSheet sheet = new HSSFSheet(workbook, _sheet.cloneSheet());
|
HSSFSheet sheet = new HSSFSheet(workbook, _sheet.cloneSheet());
|
||||||
int pos = sheet._sheet.findFirstRecordLocBySid(DrawingRecord.sid);
|
int pos = sheet._sheet.findFirstRecordLocBySid(DrawingRecord.sid);
|
||||||
DrawingRecord dr = (DrawingRecord) sheet._sheet.findFirstRecordBySid(DrawingRecord.sid);
|
DrawingRecord dr = (DrawingRecord) sheet._sheet.findFirstRecordBySid(DrawingRecord.sid);
|
||||||
|
|
|
@ -167,13 +167,10 @@ public class EmbeddedExtractor implements Iterable<EmbeddedExtractor> {
|
||||||
protected EmbeddedData extract(DirectoryNode dn) throws IOException {
|
protected EmbeddedData extract(DirectoryNode dn) throws IOException {
|
||||||
assert(canExtract(dn));
|
assert(canExtract(dn));
|
||||||
ByteArrayOutputStream bos = new ByteArrayOutputStream(20000);
|
ByteArrayOutputStream bos = new ByteArrayOutputStream(20000);
|
||||||
POIFSFileSystem dest = new POIFSFileSystem();
|
try (POIFSFileSystem dest = new POIFSFileSystem()) {
|
||||||
try {
|
|
||||||
copyNodes(dn, dest.getRoot());
|
copyNodes(dn, dest.getRoot());
|
||||||
// start with a reasonable big size
|
// start with a reasonable big size
|
||||||
dest.writeFilesystem(bos);
|
dest.writeFilesystem(bos);
|
||||||
} finally {
|
|
||||||
dest.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new EmbeddedData(dn.getName(), bos.toByteArray(), CONTENT_TYPE_BYTES);
|
return new EmbeddedData(dn.getName(), bos.toByteArray(), CONTENT_TYPE_BYTES);
|
||||||
|
@ -369,11 +366,8 @@ public class EmbeddedExtractor implements Iterable<EmbeddedExtractor> {
|
||||||
destDir.setStorageClsid(srcDir.getStorageClsid());
|
destDir.setStorageClsid(srcDir.getStorageClsid());
|
||||||
copyNodes(srcDir, destDir);
|
copyNodes(srcDir, destDir);
|
||||||
} else {
|
} else {
|
||||||
InputStream is = src.createDocumentInputStream(e);
|
try (InputStream is = src.createDocumentInputStream(e)) {
|
||||||
try {
|
|
||||||
dest.createDocument(e.getName(), is);
|
dest.createDocument(e.getName(), is);
|
||||||
} finally {
|
|
||||||
is.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -66,19 +66,16 @@ public final class SAXHelper {
|
||||||
saxFactory = SAXParserFactory.newInstance();
|
saxFactory = SAXParserFactory.newInstance();
|
||||||
saxFactory.setValidating(false);
|
saxFactory.setValidating(false);
|
||||||
saxFactory.setNamespaceAware(true);
|
saxFactory.setNamespaceAware(true);
|
||||||
} catch (RuntimeException re) {
|
} catch (RuntimeException | Error re) {
|
||||||
|
// this also catches NoClassDefFoundError, which may be due to a local class path issue
|
||||||
|
// This may occur if the code is run inside a web container
|
||||||
|
// or a restricted JVM
|
||||||
|
// See bug 61170: https://bz.apache.org/bugzilla/show_bug.cgi?id=61170
|
||||||
logger.log(POILogger.WARN, "Failed to create SAXParserFactory", re);
|
logger.log(POILogger.WARN, "Failed to create SAXParserFactory", re);
|
||||||
throw re;
|
throw re;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.log(POILogger.WARN, "Failed to create SAXParserFactory", e);
|
logger.log(POILogger.WARN, "Failed to create SAXParserFactory", e);
|
||||||
throw new RuntimeException("Failed to create SAXParserFactory", e);
|
throw new RuntimeException("Failed to create SAXParserFactory", e);
|
||||||
} catch (Error e) {
|
|
||||||
// catches NoClassDefFoundError, which may be due to a local class path issue
|
|
||||||
// This may occur if the code is run inside a web container
|
|
||||||
// or a restricted JVM
|
|
||||||
// See bug 61170: https://bz.apache.org/bugzilla/show_bug.cgi?id=61170
|
|
||||||
logger.log(POILogger.WARN, "Failed to create SAXParserFactory", e);
|
|
||||||
throw e;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -481,7 +481,6 @@ implements SlideShow<XSLFShape,XSLFTextParagraph> {
|
||||||
@Override
|
@Override
|
||||||
public XSLFPictureData addPicture(byte[] pictureData, PictureType format) {
|
public XSLFPictureData addPicture(byte[] pictureData, PictureType format) {
|
||||||
XSLFPictureData img = findPictureData(pictureData);
|
XSLFPictureData img = findPictureData(pictureData);
|
||||||
|
|
||||||
if (img != null) {
|
if (img != null) {
|
||||||
return img;
|
return img;
|
||||||
}
|
}
|
||||||
|
@ -491,13 +490,13 @@ implements SlideShow<XSLFShape,XSLFTextParagraph> {
|
||||||
if (relType == null) {
|
if (relType == null) {
|
||||||
throw new IllegalArgumentException("Picture type "+format+" is not supported.");
|
throw new IllegalArgumentException("Picture type "+format+" is not supported.");
|
||||||
}
|
}
|
||||||
|
|
||||||
img = createRelationship(relType, XSLFFactory.getInstance(), imageNumber + 1, true).getDocumentPart();
|
img = createRelationship(relType, XSLFFactory.getInstance(), imageNumber + 1, true).getDocumentPart();
|
||||||
img.setIndex(imageNumber);
|
img.setIndex(imageNumber);
|
||||||
_pictures.add(img);
|
_pictures.add(img);
|
||||||
try {
|
|
||||||
OutputStream out = img.getPackagePart().getOutputStream();
|
try (OutputStream out = img.getPackagePart().getOutputStream()) {
|
||||||
out.write(pictureData);
|
out.write(pictureData);
|
||||||
out.close();
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new POIXMLException(e);
|
throw new POIXMLException(e);
|
||||||
}
|
}
|
||||||
|
@ -536,11 +535,8 @@ implements SlideShow<XSLFShape,XSLFTextParagraph> {
|
||||||
{
|
{
|
||||||
int length = (int) pict.length();
|
int length = (int) pict.length();
|
||||||
byte[] data = IOUtils.safelyAllocate(length, MAX_RECORD_LENGTH);
|
byte[] data = IOUtils.safelyAllocate(length, MAX_RECORD_LENGTH);
|
||||||
FileInputStream is = new FileInputStream(pict);
|
try (InputStream is = new FileInputStream(pict)) {
|
||||||
try {
|
|
||||||
IOUtils.readFully(is, data);
|
IOUtils.readFully(is, data);
|
||||||
} finally {
|
|
||||||
is.close();
|
|
||||||
}
|
}
|
||||||
return addPicture(data, format);
|
return addPicture(data, format);
|
||||||
}
|
}
|
||||||
|
|
|
@ -360,12 +360,12 @@ implements XSLFShapeContainer, GroupShape<XSLFShape,XSLFTextParagraph> {
|
||||||
XSLFShape newShape;
|
XSLFShape newShape;
|
||||||
if (shape instanceof XSLFTextBox) {
|
if (shape instanceof XSLFTextBox) {
|
||||||
newShape = createTextBox();
|
newShape = createTextBox();
|
||||||
|
} else if (shape instanceof XSLFFreeformShape) {
|
||||||
|
newShape = createFreeform();
|
||||||
} else if (shape instanceof XSLFAutoShape) {
|
} else if (shape instanceof XSLFAutoShape) {
|
||||||
newShape = createAutoShape();
|
newShape = createAutoShape();
|
||||||
} else if (shape instanceof XSLFConnectorShape) {
|
} else if (shape instanceof XSLFConnectorShape) {
|
||||||
newShape = createConnector();
|
newShape = createConnector();
|
||||||
} else if (shape instanceof XSLFFreeformShape) {
|
|
||||||
newShape = createFreeform();
|
|
||||||
} else if (shape instanceof XSLFPictureShape) {
|
} else if (shape instanceof XSLFPictureShape) {
|
||||||
XSLFPictureShape p = (XSLFPictureShape)shape;
|
XSLFPictureShape p = (XSLFPictureShape)shape;
|
||||||
XSLFPictureData pd = p.getPictureData();
|
XSLFPictureData pd = p.getPictureData();
|
||||||
|
|
|
@ -116,8 +116,7 @@ public class PPTX2PNG {
|
||||||
if (!quiet) {
|
if (!quiet) {
|
||||||
System.out.println("Processing " + file);
|
System.out.println("Processing " + file);
|
||||||
}
|
}
|
||||||
SlideShow<?,?> ss = SlideShowFactory.create(file, null, true);
|
try (SlideShow<?, ?> ss = SlideShowFactory.create(file, null, true)) {
|
||||||
try {
|
|
||||||
List<? extends Slide<?, ?>> slides = ss.getSlides();
|
List<? extends Slide<?, ?>> slides = ss.getSlides();
|
||||||
|
|
||||||
Set<Integer> slidenum = slideIndexes(slides.size(), slidenumStr);
|
Set<Integer> slidenum = slideIndexes(slides.size(), slidenumStr);
|
||||||
|
@ -160,12 +159,10 @@ public class PPTX2PNG {
|
||||||
File outfile = new File(outdir, outname);
|
File outfile = new File(outdir, outname);
|
||||||
ImageIO.write(img, format, outfile);
|
ImageIO.write(img, format, outfile);
|
||||||
}
|
}
|
||||||
|
|
||||||
graphics.dispose();
|
graphics.dispose();
|
||||||
img.flush();
|
img.flush();
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
ss.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!quiet) {
|
if (!quiet) {
|
||||||
|
|
|
@ -1114,7 +1114,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
||||||
/**
|
/**
|
||||||
* Get the XSSFSheet object at the given index.
|
* Get the XSSFSheet object at the given index.
|
||||||
*
|
*
|
||||||
* @param index of the sheet number (0-based physical & logical)
|
* @param index of the sheet number (0-based physical & logical)
|
||||||
* @return XSSFSheet at the provided index
|
* @return XSSFSheet at the provided index
|
||||||
* @throws IllegalArgumentException if the index is out of range (index
|
* @throws IllegalArgumentException if the index is out of range (index
|
||||||
* < 0 || index >= getNumberOfSheets()).
|
* < 0 || index >= getNumberOfSheets()).
|
||||||
|
@ -1129,7 +1129,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
||||||
* Returns the index of the sheet by his name (case insensitive match)
|
* Returns the index of the sheet by his name (case insensitive match)
|
||||||
*
|
*
|
||||||
* @param name the sheet name
|
* @param name the sheet name
|
||||||
* @return index of the sheet (0 based) or <tt>-1</tt if not found
|
* @return index of the sheet (0 based) or <tt>-1</tt> if not found
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int getSheetIndex(String name) {
|
public int getSheetIndex(String name) {
|
||||||
|
|
|
@ -96,7 +96,7 @@ import java.util.List;
|
||||||
* @param formulaShifter the formula shifting policy
|
* @param formulaShifter the formula shifting policy
|
||||||
*/
|
*/
|
||||||
/*package*/ static void updateRowFormulas(XSSFRow row, FormulaShifter formulaShifter) {
|
/*package*/ static void updateRowFormulas(XSSFRow row, FormulaShifter formulaShifter) {
|
||||||
XSSFSheet sheet = (XSSFSheet) row.getSheet();
|
XSSFSheet sheet = row.getSheet();
|
||||||
for (Cell c : row) {
|
for (Cell c : row) {
|
||||||
XSSFCell cell = (XSSFCell) c;
|
XSSFCell cell = (XSSFCell) c;
|
||||||
|
|
||||||
|
|
|
@ -101,8 +101,8 @@ public class TestXSLFBugs {
|
||||||
|
|
||||||
assertTrue(shapeToDelete1.isPresent());
|
assertTrue(shapeToDelete1.isPresent());
|
||||||
slide1.removeShape(shapeToDelete1.get());
|
slide1.removeShape(shapeToDelete1.get());
|
||||||
slide1.getRelationParts().stream()
|
assertTrue(slide1.getRelationParts().stream()
|
||||||
.allMatch(rp -> "rId1,rId3".contains(rp.getRelationship().getId()) );
|
.allMatch(rp -> "rId1,rId3".contains(rp.getRelationship().getId()) ));
|
||||||
|
|
||||||
assertNotNull(ppt1.getPackage().getPart(ppn));
|
assertNotNull(ppt1.getPackage().getPart(ppn));
|
||||||
ppt1.close();
|
ppt1.close();
|
||||||
|
@ -114,8 +114,8 @@ public class TestXSLFBugs {
|
||||||
slide2.getShapes().stream().filter(s -> s instanceof XSLFPictureShape).skip(1).findFirst();
|
slide2.getShapes().stream().filter(s -> s instanceof XSLFPictureShape).skip(1).findFirst();
|
||||||
assertTrue(shapeToDelete2.isPresent());
|
assertTrue(shapeToDelete2.isPresent());
|
||||||
slide2.removeShape(shapeToDelete2.get());
|
slide2.removeShape(shapeToDelete2.get());
|
||||||
slide2.getRelationParts().stream()
|
assertTrue(slide2.getRelationParts().stream()
|
||||||
.allMatch(rp -> "rId1,rId2".contains(rp.getRelationship().getId()) );
|
.allMatch(rp -> "rId1,rId2".contains(rp.getRelationship().getId()) ));
|
||||||
assertNotNull(ppt2.getPackage().getPart(ppn));
|
assertNotNull(ppt2.getPackage().getPart(ppn));
|
||||||
ppt2.close();
|
ppt2.close();
|
||||||
|
|
||||||
|
@ -124,7 +124,7 @@ public class TestXSLFBugs {
|
||||||
slide3.getShapes().stream()
|
slide3.getShapes().stream()
|
||||||
.filter(s -> s instanceof XSLFPictureShape)
|
.filter(s -> s instanceof XSLFPictureShape)
|
||||||
.collect(Collectors.toList())
|
.collect(Collectors.toList())
|
||||||
.forEach(s -> slide3.removeShape(s));
|
.forEach(slide3::removeShape);
|
||||||
assertNull(ppt3.getPackage().getPart(ppn));
|
assertNull(ppt3.getPackage().getPart(ppn));
|
||||||
ppt3.close();
|
ppt3.close();
|
||||||
}
|
}
|
||||||
|
@ -370,16 +370,8 @@ public class TestXSLFBugs {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add a few pictures
|
// Add a few pictures
|
||||||
for (int i=0; i<10; i++) {
|
addPictures(ss, slide, pics, 0, 10);
|
||||||
XSLFPictureData data = ss.addPicture(pics[i], PictureType.JPEG);
|
|
||||||
assertEquals(i, data.getIndex());
|
|
||||||
assertEquals(i+1, ss.getPictureData().size());
|
|
||||||
|
|
||||||
XSLFPictureShape shape = slide.createPicture(data);
|
|
||||||
assertNotNull(shape.getPictureData());
|
|
||||||
assertArrayEquals(pics[i], shape.getPictureData().getData());
|
|
||||||
assertEquals(i+2, slide.getShapes().size());
|
|
||||||
}
|
|
||||||
// Re-fetch the pictures and check
|
// Re-fetch the pictures and check
|
||||||
for (int i=0; i<10; i++) {
|
for (int i=0; i<10; i++) {
|
||||||
XSLFPictureShape shape = (XSLFPictureShape)slide.getShapes().get(i+1);
|
XSLFPictureShape shape = (XSLFPictureShape)slide.getShapes().get(i+1);
|
||||||
|
@ -388,16 +380,8 @@ public class TestXSLFBugs {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add past 10
|
// Add past 10
|
||||||
for (int i=10; i<15; i++) {
|
addPictures(ss, slide, pics, 10, 15);
|
||||||
XSLFPictureData data = ss.addPicture(pics[i], PictureType.JPEG);
|
|
||||||
assertEquals(i, data.getIndex());
|
|
||||||
assertEquals(i+1, ss.getPictureData().size());
|
|
||||||
|
|
||||||
XSLFPictureShape shape = slide.createPicture(data);
|
|
||||||
assertNotNull(shape.getPictureData());
|
|
||||||
assertArrayEquals(pics[i], shape.getPictureData().getData());
|
|
||||||
assertEquals(i+2, slide.getShapes().size());
|
|
||||||
}
|
|
||||||
// Check all pictures
|
// Check all pictures
|
||||||
for (int i=0; i<15; i++) {
|
for (int i=0; i<15; i++) {
|
||||||
XSLFPictureShape shape = (XSLFPictureShape)slide.getShapes().get(i+1);
|
XSLFPictureShape shape = (XSLFPictureShape)slide.getShapes().get(i+1);
|
||||||
|
@ -446,6 +430,19 @@ public class TestXSLFBugs {
|
||||||
ss.close();
|
ss.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void addPictures(XMLSlideShow ss, XSLFSlide slide, byte[][] pics, int start, int end) {
|
||||||
|
for (int i = start; i< end; i++) {
|
||||||
|
XSLFPictureData data = ss.addPicture(pics[i], PictureType.JPEG);
|
||||||
|
assertEquals(i, data.getIndex());
|
||||||
|
assertEquals(i+1, ss.getPictureData().size());
|
||||||
|
|
||||||
|
XSLFPictureShape shape = slide.createPicture(data);
|
||||||
|
assertNotNull(shape.getPictureData());
|
||||||
|
assertArrayEquals(pics[i], shape.getPictureData().getData());
|
||||||
|
assertEquals(i+2, slide.getShapes().size());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void validateSlides(XMLSlideShow ss, boolean saveAndReload, String... slideTexts) throws IOException {
|
private void validateSlides(XMLSlideShow ss, boolean saveAndReload, String... slideTexts) throws IOException {
|
||||||
if (saveAndReload) {
|
if (saveAndReload) {
|
||||||
XMLSlideShow ss2 = XSLFTestDataSamples.writeOutAndReadBack(ss);
|
XMLSlideShow ss2 = XSLFTestDataSamples.writeOutAndReadBack(ss);
|
||||||
|
|
|
@ -365,11 +365,10 @@ public class TestXSLFTextParagraph {
|
||||||
|
|
||||||
@Test(expected = IllegalStateException.class)
|
@Test(expected = IllegalStateException.class)
|
||||||
public void testLineBreak() throws IOException {
|
public void testLineBreak() throws IOException {
|
||||||
XMLSlideShow ppt = new XMLSlideShow();
|
try (XMLSlideShow ppt = new XMLSlideShow()) {
|
||||||
try {
|
|
||||||
XSLFSlide slide = ppt.createSlide();
|
XSLFSlide slide = ppt.createSlide();
|
||||||
XSLFTextShape sh = slide.createAutoShape();
|
XSLFTextShape sh = slide.createAutoShape();
|
||||||
|
|
||||||
XSLFTextParagraph p = sh.addNewTextParagraph();
|
XSLFTextParagraph p = sh.addNewTextParagraph();
|
||||||
XSLFTextRun r1 = p.addNewTextRun();
|
XSLFTextRun r1 = p.addNewTextRun();
|
||||||
r1.setText("Hello,");
|
r1.setText("Hello,");
|
||||||
|
@ -382,13 +381,11 @@ public class TestXSLFTextParagraph {
|
||||||
r3.setFontSize(20.0);
|
r3.setFontSize(20.0);
|
||||||
XSLFTextRun r4 = p.addLineBreak();
|
XSLFTextRun r4 = p.addLineBreak();
|
||||||
assertEquals(20.0, r4.getFontSize(), 0);
|
assertEquals(20.0, r4.getFontSize(), 0);
|
||||||
|
|
||||||
assertEquals("Hello,\nWorld!\n",sh.getText());
|
assertEquals("Hello,\nWorld!\n", sh.getText());
|
||||||
|
|
||||||
// "You cannot change text of a line break, it is always '\\n'"
|
// "You cannot change text of a line break, it is always '\\n'"
|
||||||
r2.setText("aaa");
|
r2.setText("aaa");
|
||||||
} finally {
|
|
||||||
ppt.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue