2011-05-27 10:35:44 -04:00
|
|
|
/* ====================================================================
|
|
|
|
Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
contributor license agreements. See the NOTICE file distributed with
|
|
|
|
this work for additional information regarding copyright ownership.
|
|
|
|
The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
(the "License"); you may not use this file except in compliance with
|
|
|
|
the License. You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
==================================================================== */
|
|
|
|
package org.apache.poi.xslf;
|
|
|
|
|
2014-10-26 20:00:05 -04:00
|
|
|
import static junit.framework.TestCase.assertEquals;
|
|
|
|
import static org.apache.poi.POITestCase.assertContains;
|
|
|
|
import static org.junit.Assert.assertNotNull;
|
|
|
|
import static org.junit.Assert.fail;
|
|
|
|
|
|
|
|
import java.awt.Color;
|
|
|
|
import java.awt.Dimension;
|
|
|
|
import java.awt.Graphics2D;
|
|
|
|
import java.awt.RenderingHints;
|
|
|
|
import java.awt.geom.AffineTransform;
|
|
|
|
import java.awt.geom.Rectangle2D;
|
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
import java.io.File;
|
2013-06-25 20:45:18 -04:00
|
|
|
import java.net.URI;
|
|
|
|
import java.util.List;
|
|
|
|
|
2014-10-26 20:00:05 -04:00
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
|
2011-09-19 11:38:59 -04:00
|
|
|
import org.apache.poi.POIXMLDocumentPart;
|
2011-05-27 10:35:44 -04:00
|
|
|
import org.apache.poi.openxml4j.opc.PackagePart;
|
2013-06-25 20:45:18 -04:00
|
|
|
import org.apache.poi.xslf.usermodel.DrawingParagraph;
|
|
|
|
import org.apache.poi.xslf.usermodel.DrawingTextBody;
|
2011-05-27 10:35:44 -04:00
|
|
|
import org.apache.poi.xslf.usermodel.XMLSlideShow;
|
2014-08-04 17:34:51 -04:00
|
|
|
import org.apache.poi.xslf.usermodel.XSLFPictureData;
|
|
|
|
import org.apache.poi.xslf.usermodel.XSLFPictureShape;
|
2011-09-19 11:38:59 -04:00
|
|
|
import org.apache.poi.xslf.usermodel.XSLFRelation;
|
2014-08-04 17:34:51 -04:00
|
|
|
import org.apache.poi.xslf.usermodel.XSLFShape;
|
2011-05-27 10:35:44 -04:00
|
|
|
import org.apache.poi.xslf.usermodel.XSLFSlide;
|
2011-09-19 11:38:59 -04:00
|
|
|
import org.apache.poi.xslf.usermodel.XSLFSlideLayout;
|
2014-10-26 20:00:05 -04:00
|
|
|
import org.junit.Ignore;
|
|
|
|
import org.junit.Test;
|
2011-05-27 10:35:44 -04:00
|
|
|
|
2014-10-26 20:00:05 -04:00
|
|
|
public class TestXSLFBugs {
|
2011-05-27 10:35:44 -04:00
|
|
|
|
2014-10-26 20:00:05 -04:00
|
|
|
@Test
|
2012-03-05 04:42:36 -05:00
|
|
|
@SuppressWarnings("deprecation")
|
2014-10-26 20:00:05 -04:00
|
|
|
public void bug51187() throws Exception {
|
2011-05-27 10:35:44 -04:00
|
|
|
XMLSlideShow ss = XSLFTestDataSamples.openSampleDocument("51187.pptx");
|
|
|
|
|
|
|
|
assertEquals(1, ss.getSlides().length);
|
|
|
|
XSLFSlide slide = ss.getSlides()[0];
|
|
|
|
|
|
|
|
// Check the relations on it
|
|
|
|
// Note - rId3 is a self reference
|
|
|
|
PackagePart slidePart = ss._getXSLFSlideShow().getSlidePart(
|
|
|
|
ss._getXSLFSlideShow().getSlideReferences().getSldIdArray(0)
|
|
|
|
);
|
|
|
|
assertEquals("/ppt/slides/slide1.xml", slidePart.getPartName().toString());
|
|
|
|
assertEquals("/ppt/slideLayouts/slideLayout12.xml", slidePart.getRelationship("rId1").getTargetURI().toString());
|
|
|
|
assertEquals("/ppt/notesSlides/notesSlide1.xml", slidePart.getRelationship("rId2").getTargetURI().toString());
|
|
|
|
assertEquals("/ppt/slides/slide1.xml", slidePart.getRelationship("rId3").getTargetURI().toString());
|
|
|
|
assertEquals("/ppt/media/image1.png", slidePart.getRelationship("rId4").getTargetURI().toString());
|
|
|
|
|
|
|
|
// Save and re-load
|
|
|
|
ss = XSLFTestDataSamples.writeOutAndReadBack(ss);
|
|
|
|
assertEquals(1, ss.getSlides().length);
|
|
|
|
|
|
|
|
slidePart = ss._getXSLFSlideShow().getSlidePart(
|
|
|
|
ss._getXSLFSlideShow().getSlideReferences().getSldIdArray(0)
|
|
|
|
);
|
|
|
|
assertEquals("/ppt/slides/slide1.xml", slidePart.getPartName().toString());
|
|
|
|
assertEquals("/ppt/slideLayouts/slideLayout12.xml", slidePart.getRelationship("rId1").getTargetURI().toString());
|
|
|
|
assertEquals("/ppt/notesSlides/notesSlide1.xml", slidePart.getRelationship("rId2").getTargetURI().toString());
|
|
|
|
// TODO Fix this
|
2011-08-11 04:13:00 -04:00
|
|
|
assertEquals("/ppt/slides/slide1.xml", slidePart.getRelationship("rId3").getTargetURI().toString());
|
2011-05-27 10:35:44 -04:00
|
|
|
assertEquals("/ppt/media/image1.png", slidePart.getRelationship("rId4").getTargetURI().toString());
|
|
|
|
}
|
2011-09-14 10:39:20 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Slide relations with anchors in them
|
|
|
|
*/
|
2014-10-26 20:00:05 -04:00
|
|
|
@Test
|
|
|
|
public void tika705() {
|
2011-09-14 10:39:20 -04:00
|
|
|
XMLSlideShow ss = XSLFTestDataSamples.openSampleDocument("with_japanese.pptx");
|
2011-09-19 11:38:59 -04:00
|
|
|
|
|
|
|
// Should have one slide
|
|
|
|
assertEquals(1, ss.getSlides().length);
|
|
|
|
XSLFSlide slide = ss.getSlides()[0];
|
|
|
|
|
|
|
|
// Check the relations from this
|
|
|
|
List<POIXMLDocumentPart> rels = slide.getRelations();
|
|
|
|
|
|
|
|
// Should have 6 relations:
|
|
|
|
// 1 external hyperlink (skipped from list)
|
|
|
|
// 4 internal hyperlinks
|
|
|
|
// 1 slide layout
|
|
|
|
assertEquals(5, rels.size());
|
|
|
|
int layouts = 0;
|
|
|
|
int hyperlinks = 0;
|
|
|
|
for(POIXMLDocumentPart p : rels) {
|
|
|
|
if(p.getPackageRelationship().getRelationshipType().equals(XSLFRelation.HYPERLINK.getRelation())) {
|
|
|
|
hyperlinks++;
|
|
|
|
} else if(p instanceof XSLFSlideLayout) {
|
|
|
|
layouts++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assertEquals(1, layouts);
|
|
|
|
assertEquals(4, hyperlinks);
|
|
|
|
|
|
|
|
// Hyperlinks should all be to #_ftn1 or #ftnref1
|
|
|
|
for(POIXMLDocumentPart p : rels) {
|
|
|
|
if(p.getPackageRelationship().getRelationshipType().equals(XSLFRelation.HYPERLINK.getRelation())) {
|
|
|
|
URI target = p.getPackageRelationship().getTargetURI();
|
|
|
|
|
|
|
|
if(target.getFragment().equals("_ftn1") ||
|
|
|
|
target.getFragment().equals("_ftnref1")) {
|
|
|
|
// Good
|
|
|
|
} else {
|
|
|
|
fail("Invalid target " + target.getFragment() + " on " + target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-09-14 10:39:20 -04:00
|
|
|
}
|
2013-06-25 20:45:18 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A slideshow can have more than one rID pointing to a given
|
|
|
|
* slide, eg presentation.xml rID1 -> slide1.xml, but slide1.xml
|
|
|
|
* rID2 -> slide3.xml
|
|
|
|
*/
|
2014-10-26 20:00:05 -04:00
|
|
|
@Test
|
|
|
|
@Ignore
|
|
|
|
public void bug54916() throws Exception {
|
2013-06-25 20:45:18 -04:00
|
|
|
XMLSlideShow ss = XSLFTestDataSamples.openSampleDocument("OverlappingRelations.pptx");
|
|
|
|
XSLFSlide slide;
|
|
|
|
|
|
|
|
// Should find 4 slides
|
|
|
|
assertEquals(4, ss.getSlides().length);
|
|
|
|
|
|
|
|
// Check the text, to see we got them in order
|
|
|
|
slide = ss.getSlides()[0];
|
|
|
|
assertContains("POI cannot read this", getSlideText(slide));
|
|
|
|
|
|
|
|
slide = ss.getSlides()[1];
|
|
|
|
assertContains("POI can read this", getSlideText(slide));
|
|
|
|
assertContains("Has a relationship to another slide", getSlideText(slide));
|
|
|
|
|
|
|
|
slide = ss.getSlides()[2];
|
|
|
|
assertContains("POI can read this", getSlideText(slide));
|
|
|
|
|
|
|
|
slide = ss.getSlides()[3];
|
|
|
|
assertContains("POI can read this", getSlideText(slide));
|
|
|
|
}
|
|
|
|
|
2014-08-04 17:34:51 -04:00
|
|
|
/**
|
|
|
|
* When the picture is not embedded but inserted only as a "link to file",
|
|
|
|
* there is no data available and XSLFPictureShape.getPictureData()
|
|
|
|
* gives a NPE, see bug #56812
|
|
|
|
*/
|
2014-10-26 20:00:05 -04:00
|
|
|
@Test
|
|
|
|
public void bug56812() throws Exception {
|
2014-08-04 17:34:51 -04:00
|
|
|
XMLSlideShow ppt = XSLFTestDataSamples.openSampleDocument("56812.pptx");
|
|
|
|
|
2014-08-04 18:00:47 -04:00
|
|
|
int internalPictures = 0;
|
|
|
|
int externalPictures = 0;
|
2014-08-04 17:34:51 -04:00
|
|
|
for (XSLFSlide slide : ppt.getSlides()){
|
|
|
|
for (XSLFShape shape : slide.getShapes()){
|
|
|
|
assertNotNull(shape);
|
|
|
|
|
|
|
|
if (shape instanceof XSLFPictureShape) {
|
2014-08-04 18:00:47 -04:00
|
|
|
XSLFPictureShape picture = (XSLFPictureShape)shape;
|
|
|
|
if (picture.isExternalLinkedPicture()) {
|
|
|
|
externalPictures++;
|
|
|
|
|
|
|
|
assertNotNull(picture.getPictureLink());
|
|
|
|
} else {
|
|
|
|
internalPictures++;
|
|
|
|
|
|
|
|
XSLFPictureData data = picture.getPictureData();
|
|
|
|
assertNotNull(data);
|
|
|
|
assertNotNull(data.getFileName());
|
|
|
|
}
|
2014-08-04 17:34:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-04 18:00:47 -04:00
|
|
|
assertEquals(2, internalPictures);
|
|
|
|
assertEquals(1, externalPictures);
|
2014-08-04 17:34:51 -04:00
|
|
|
}
|
2014-10-26 20:00:05 -04:00
|
|
|
|
|
|
|
@Test
|
|
|
|
@Ignore("Similar to TestFontRendering it doesn't make sense to compare images because of tiny rendering differences in windows/unix")
|
|
|
|
public void bug54542() throws Exception {
|
|
|
|
XMLSlideShow ss = XSLFTestDataSamples.openSampleDocument("54542_cropped_bitmap.pptx");
|
|
|
|
|
|
|
|
Dimension pgsize = ss.getPageSize();
|
|
|
|
|
|
|
|
XSLFSlide slide = ss.getSlides()[0];
|
|
|
|
|
|
|
|
// render it
|
|
|
|
double zoom = 1;
|
|
|
|
AffineTransform at = new AffineTransform();
|
|
|
|
at.setToScale(zoom, zoom);
|
|
|
|
|
|
|
|
BufferedImage imgActual = new BufferedImage((int)Math.ceil(pgsize.width*zoom), (int)Math.ceil(pgsize.height*zoom), BufferedImage.TYPE_3BYTE_BGR);
|
|
|
|
Graphics2D graphics = imgActual.createGraphics();
|
|
|
|
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
|
|
|
graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
|
|
|
|
graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
|
|
|
|
graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
|
|
|
|
graphics.setTransform(at);
|
|
|
|
graphics.setPaint(Color.white);
|
|
|
|
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
|
|
|
|
slide.draw(graphics);
|
|
|
|
|
|
|
|
ImageIO.write(imgActual, "PNG", new File("bug54542.png"));
|
|
|
|
}
|
2014-08-04 17:34:51 -04:00
|
|
|
|
2013-06-25 20:45:18 -04:00
|
|
|
protected String getSlideText(XSLFSlide slide) {
|
|
|
|
StringBuffer text = new StringBuffer();
|
|
|
|
for(DrawingTextBody textBody : slide.getCommonSlideData().getDrawingText()) {
|
|
|
|
for (DrawingParagraph p : textBody.getParagraphs()) {
|
|
|
|
text.append(p.getText());
|
|
|
|
text.append("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return text.toString();
|
|
|
|
}
|
2011-05-27 10:35:44 -04:00
|
|
|
}
|