]> source.dussan.org Git - poi.git/commitdiff
Fix initialization of text body when used in XSLF
authorAlain Béarez <abearez@apache.org>
Wed, 11 Dec 2019 00:10:09 +0000 (00:10 +0000)
committerAlain Béarez <abearez@apache.org>
Wed, 11 Dec 2019 00:10:09 +0000 (00:10 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1871150 13f79535-47bb-0310-9956-ffa450edef68

15 files changed:
src/ooxml/java/org/apache/poi/xddf/usermodel/text/XDDFTextBody.java
src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFAutoShape.java
src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTable.java
src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTableCell.java
src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTextBox.java
src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTextRun.java
src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTextShape.java
src/ooxml/testcases/org/apache/poi/sl/draw/TestDrawPictureShape.java
src/ooxml/testcases/org/apache/poi/xddf/usermodel/text/TestXDDFTextBodyProperties.java
src/ooxml/testcases/org/apache/poi/xddf/usermodel/text/TestXDDFTextRun.java
src/ooxml/testcases/org/apache/poi/xslf/TestXSLFBugs.java
src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestPPTX2PNG.java
src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXMLSlideShow.java
src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFAutoShape.java
src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFPictureShape.java

index 19555cd88424de9a789a8f1d9b121826856f1f26..6eae7b88c2159918eed0c562200ac66a0796998c 100644 (file)
@@ -38,7 +38,6 @@ public class XDDFTextBody {
 
     public XDDFTextBody(TextContainer parent) {
         this(parent, CTTextBody.Factory.newInstance());
-        initialize();
     }
 
     @Internal
@@ -64,6 +63,7 @@ public class XDDFTextBody {
         bp.setRightToLeft(false);
         XDDFTextParagraph p = addNewParagraph();
         p.setTextAlignment(TextAlignment.LEFT);
+        p.appendRegularRun("");
         XDDFRunProperties end = p.addAfterLastRunProperties();
         end.setLanguage(Locale.US);
         end.setFontSize(11.0);
index 744ccfd175a3d53e7fec1e43ae94ddccd28aba32..a69af0e0e797f135434b71ecaaffaa13a1682953 100644 (file)
@@ -78,10 +78,8 @@ public class XSLFAutoShape extends XSLFTextShape implements AutoShape<XSLFShape,
         CTTextBody txBody = shape.getTxBody();
         if (txBody == null && create) {
             XDDFTextBody body = new XDDFTextBody(this);
-            initTextBody(body);
             shape.setTxBody(body.getXmlObject());
             txBody = shape.getTxBody();
-            getTextParagraphs().add(newTextParagraph(txBody.getPArray(0)));
         }
         return txBody;
     }
index c8ba9f7182bf0d96b4dd132daba91901688943aa..a28d339fce06f52941212d8240779373390ce7e4 100644 (file)
@@ -33,6 +33,7 @@ import org.apache.poi.sl.draw.DrawTextShape;
 import org.apache.poi.sl.usermodel.TableShape;
 import org.apache.poi.util.Internal;
 import org.apache.poi.util.Units;
+import org.apache.poi.xddf.usermodel.text.XDDFTextBody;
 import org.apache.xmlbeans.XmlCursor;
 import org.apache.xmlbeans.XmlObject;
 import org.apache.xmlbeans.impl.values.XmlAnyTypeImpl;
@@ -181,7 +182,7 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow
         col.setW(width);
         for(XSLFTableRow row : _rows) {
             XSLFTableCell cell = row.addCell();
-            cell.getTextBody(true);
+            new XDDFTextBody(cell, cell.getTextBody(true)).initialize();
         }
         updateRowColIndexes();
     }
@@ -200,7 +201,7 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow
         col.setW(width);
         for(XSLFTableRow row : _rows) {
             XSLFTableCell cell = row.insertCell(colIdx);
-            cell.getTextBody(true);
+            new XDDFTextBody(cell, cell.getTextBody(true)).initialize();
         }
         updateRowColIndexes();
     }
index 5071852aec4dd5f16f2832c37602d00aca36dc5d..011914f16edc99404f573bdb818daf287d3a7ea1 100644 (file)
@@ -89,10 +89,8 @@ public class XSLFTableCell extends XSLFTextShape implements TableCell<XSLFShape,
         CTTextBody txBody = cell.getTxBody();
         if (txBody == null && create) {
             XDDFTextBody body = new XDDFTextBody(this);
-            initTextBody(body);
             cell.setTxBody(body.getXmlObject());
             txBody = cell.getTxBody();
-            getTextParagraphs().add(newTextParagraph(txBody.getPArray(0)));
         }
         return txBody;
     }
index 7fe3252995b51dc35cfd98851b628f94acda0c1d..e3e30d18c6cc1dd7a2990fed2f5156e2eb007453 100644 (file)
@@ -57,7 +57,7 @@ public class XSLFTextBox extends XSLFAutoShape implements TextBox<XSLFShape, XSL
         prst.setPrst(STShapeType.RECT);
         prst.addNewAvLst();
         XDDFTextBody body = new XDDFTextBody(null);
-        initTextBody(body);
+        body.initialize();
         ct.setTxBody(body.getXmlObject());
 
         return ct;
index 6d2d215181062bdad33345bd5ba147bbb924a8d5..3305b1a26da9c605420bdef6d96a55f26159606e 100644 (file)
@@ -28,9 +28,9 @@ import org.apache.poi.openxml4j.opc.PackagePart;
 import org.apache.poi.sl.draw.DrawPaint;
 import org.apache.poi.sl.usermodel.PaintStyle;
 import org.apache.poi.sl.usermodel.PaintStyle.SolidPaint;
-import org.apache.poi.sl.usermodel.TextParagraph;
 import org.apache.poi.sl.usermodel.TextRun;
 import org.apache.poi.util.Beta;
+import org.apache.poi.util.Internal;
 import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
 import org.apache.poi.xslf.model.CharacterPropertyFetcher;
@@ -101,6 +101,7 @@ public class XSLFTextRun implements TextRun {
      *
      * @return the xmlbeans object
      */
+    @Internal
     public XmlObject getXmlObject(){
         return _r;
     }
@@ -558,7 +559,9 @@ public class XSLFTextRun implements TextRun {
 
         Double srcFontSize = r.getFontSize();
         if (srcFontSize == null) {
-            if (getFontSize() != null) setFontSize(null);
+            if (getFontSize() != null) {
+                setFontSize(null);
+            }
         } else if(!srcFontSize.equals(getFontSize())) {
             setFontSize(srcFontSize);
         }
@@ -644,7 +647,7 @@ public class XSLFTextRun implements TextRun {
                 }
                 return;
             }
-            
+
             CTTextCharacterProperties props = getRPr(false);
             if (props == null) {
                 return;
@@ -811,7 +814,7 @@ public class XSLFTextRun implements TextRun {
                     font = coll.getLatin();
                 }
                 // SYMBOL is missing
-                
+
                 if (font == null || !font.isSetTypeface() || "".equals(font.getTypeface())) {
                     font = coll.getLatin();
                 }
index 9286d5354aacd23b81cded89256b7769ce6478e7..0ba39d520fcc0653597e313eda4c2bb8c5116a39 100644 (file)
@@ -38,7 +38,6 @@ import org.apache.poi.util.Beta;
 import org.apache.poi.util.Units;
 import org.apache.poi.xddf.usermodel.text.TextContainer;
 import org.apache.poi.xddf.usermodel.text.XDDFTextBody;
-import org.apache.poi.xddf.usermodel.text.XDDFTextParagraph;
 import org.apache.poi.xslf.model.PropertyFetcher;
 import org.apache.poi.xslf.model.TextBodyPropertyFetcher;
 import org.apache.xmlbeans.XmlObject;
@@ -72,11 +71,6 @@ public abstract class XSLFTextShape extends XSLFSimpleShape
         }
     }
 
-    protected static void initTextBody(XDDFTextBody body) {
-        XDDFTextParagraph p = body.getParagraph(0);
-        p.appendRegularRun("");
-    }
-
     @Beta
     public XDDFTextBody getTextBody() {
         CTTextBody txBody = getTextBody(false);
@@ -213,6 +207,7 @@ public abstract class XSLFTextShape extends XSLFSimpleShape
         CTTextParagraph p;
         if (txBody == null) {
             txBody = getTextBody(true);
+            new XDDFTextBody(this, txBody).initialize();
             p = txBody.getPArray(0);
             p.removeR(0);
         } else {
@@ -669,11 +664,11 @@ public abstract class XSLFTextShape extends XSLFSimpleShape
 
         XSLFTextShape otherTS = (XSLFTextShape) other;
         CTTextBody otherTB = otherTS.getTextBody(false);
-        CTTextBody thisTB = getTextBody(true);
         if (otherTB == null) {
             return;
         }
 
+        CTTextBody thisTB = getTextBody(true);
         thisTB.setBodyPr((CTTextBodyProperties) otherTB.getBodyPr().copy());
 
         if (thisTB.isSetLstStyle()) {
index 45f92e8b24d33b5b9293a7a2eda20a8d9005c1bd..d6c32bfd82c1d01f6318ca8ec60a067c4162f1e2 100644 (file)
@@ -52,7 +52,7 @@ public class TestDrawPictureShape {
             xslfOnly = true;
         }
     }
-    
+
     /** a generic way to open a sample slideshow document **/
     public static SlideShow<?,?> openSampleDocument(String sampleName) throws IOException {
         InputStream is = ssSamples.openResourceAsStream(sampleName);
@@ -70,16 +70,16 @@ public class TestDrawPictureShape {
         assumeFalse(xslfOnly);
         testResize("pictures.ppt");
     }
-    
+
     @Test
     public void testResizeXSLF() throws IOException {
         testResize("shapes.pptx");
     }
-    
-        
+
+
     public void testResize(String file) throws IOException {
         SlideShow<?,?> ss = openSampleDocument(file);
-        
+
         Slide<?,?> slide = ss.getSlides().get(0);
         PictureShape<?,?> picShape = null;
         for (Shape<?,?> shape : slide.getShapes()) {
@@ -97,7 +97,7 @@ public class TestDrawPictureShape {
             (int)picShape.getAnchor().getHeight()
         );
         assertEquals(dimPd, dimShape);
-        
+
         double newWidth = (dimPd.getWidth()*(100d/dimPd.getHeight()));
         // ... -1 is a rounding error
         Rectangle2D expRect = new Rectangle2D.Double(rbf(50+300-newWidth, picShape), 50, rbf(newWidth, picShape), 100);
@@ -110,7 +110,7 @@ public class TestDrawPictureShape {
         assertEquals(expRect.getHeight(), actRect.getHeight(), .0001);
         ss.close();
     }
-    
+
     // round back and forth - points -> master -> points
     static double rbf(double val, PictureShape<?,?> picShape) {
         if (picShape.getClass().getName().contains("HSLF")) {
index 3b137d656cae1b47fb6fe96b51908b4814816b98..e93f2bdc3d533c771bb51dcf054731ae6b036ec5 100644 (file)
@@ -30,7 +30,9 @@ public class TestXDDFTextBodyProperties {
 
     @Test
     public void testProperties() throws IOException {
-        XDDFBodyProperties body = new XDDFTextBody(null).getBodyProperties();
+        XDDFTextBody text = new XDDFTextBody(null);
+        text.initialize();
+        XDDFBodyProperties body = text.getBodyProperties();
         CTTextBodyProperties props = body.getXmlObject();
 
         body.setBottomInset(null);
index 67d509fa42dc1a047557c7516434b3401c55a19c..e162ba18fb47134d3799900b5dbc0b276caa56aa 100644 (file)
@@ -46,6 +46,7 @@ public class TestXDDFTextRun {
             sh.addNewTextParagraph();
 
             XDDFTextBody body = sh.getTextBody();
+            body.initialize();
             XDDFTextParagraph para = body.getParagraph(0);
             XDDFTextRun r = para.appendRegularRun("text");
             assertEquals(LocaleUtil.getUserLocale().toLanguageTag(), r.getLanguage().toLanguageTag());
index da25fc77e881c9646a511fe72706e267756117d5..a9a9e16b7c6883cf99165b8a92904da7395dce8c 100644 (file)
@@ -84,7 +84,6 @@ import org.junit.Test;
 import org.openxmlformats.schemas.drawingml.x2006.main.CTOuterShadowEffect;
 import org.openxmlformats.schemas.presentationml.x2006.main.CTShape;
 
-
 public class TestXSLFBugs {
     private static final POIDataSamples slTests = POIDataSamples.getSlideShowInstance();
 
@@ -294,18 +293,18 @@ public class TestXSLFBugs {
         is.close();
 
         PackagePartName ppn = PackagingURIHelper.createPartName("/ppt/media/image1.png");
-        
+
         XMLSlideShow ppt1 = new XMLSlideShow(new ByteArrayInputStream(buf));
         XSLFSlide slide1 = ppt1.getSlides().get(0);
-        
+
         Optional<XSLFShape> shapeToDelete1 =
             slide1.getShapes().stream().filter(s -> s instanceof XSLFPictureShape).findFirst();
-        
+
         assertTrue(shapeToDelete1.isPresent());
         slide1.removeShape(shapeToDelete1.get());
         assertTrue(slide1.getRelationParts().stream()
             .allMatch(rp -> "rId1,rId3".contains(rp.getRelationship().getId()) ));
-        
+
         assertNotNull(ppt1.getPackage().getPart(ppn));
         ppt1.close();
 
@@ -330,7 +329,7 @@ public class TestXSLFBugs {
         assertNull(ppt3.getPackage().getPart(ppn));
         ppt3.close();
     }
-    
+
     @Test
     public void bug51187() throws Exception {
        XMLSlideShow ss1 = XSLFTestDataSamples.openSampleDocument("51187.pptx");
index 0c754437b0cd578c107b25dc0e0b6bdd58a00e40..713b2f2b84329130876bee9f6cb398910a962fd3 100644 (file)
@@ -37,7 +37,7 @@ import org.junit.runners.Parameterized.Parameter;
 import org.junit.runners.Parameterized.Parameters;
 
 /**
- * Test class for testing PPTX2PNG utility which renderes .ppt and .pptx slideshows
+ * Test class for testing PPTX2PNG utility which renders .ppt and .pptx slideshows
  */
 @RunWith(Parameterized.class)
 public class TestPPTX2PNG {
@@ -49,8 +49,8 @@ public class TestPPTX2PNG {
         "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 {
@@ -59,11 +59,11 @@ public class TestPPTX2PNG {
             xslfOnly = true;
         }
     }
-    
+
     // use filename instead of File object to omit full pathname in test name
     @Parameter(value = 0)
     public String pptFile;
-    
+
     @SuppressWarnings("ConstantConditions")
     @Parameters(name="{0}")
     public static Collection<String> data() {
index bb6167ee2fec4429bcec8367407833ad5f731122..715daa47c8c6d21061544766d84cf4b742969d3d 100644 (file)
@@ -40,7 +40,7 @@ import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideMasterIdListE
 
 public class TestXMLSlideShow extends BaseTestSlideShow<XSLFShape,XSLFTextParagraph> {
    private OPCPackage pack;
-   
+
    @Override
    public XMLSlideShow createSlideShow() {
        return new XMLSlideShow();
@@ -50,7 +50,7 @@ public class TestXMLSlideShow extends BaseTestSlideShow<XSLFShape,XSLFTextParagr
    public void setUp() throws Exception {
       pack = OPCPackage.open(slTests.openResourceAsStream("sample.pptx"));
    }
-   
+
    @After
    public void tearDown() {
        pack.revert();
@@ -77,7 +77,7 @@ public class TestXMLSlideShow extends BaseTestSlideShow<XSLFShape,XSLFTextParagr
       // Check it has some slides
       assertFalse(xml.getSlides().isEmpty());
       assertFalse(xml.getSlideMasters().isEmpty());
-      
+
       xml.close();
    }
 
@@ -122,10 +122,10 @@ public class TestXMLSlideShow extends BaseTestSlideShow<XSLFShape,XSLFTextParagr
       assertNotNull(notesMaster);
 
       assertNotNull(xml.getNotesMaster());
-      
+
       xml.close();
    }
-       
+
    @Test
    public void testMetadataBasics() throws IOException {
       XMLSlideShow xml = new XMLSlideShow(pack);
@@ -139,34 +139,34 @@ public class TestXMLSlideShow extends BaseTestSlideShow<XSLFShape,XSLFTextParagr
 
       assertNull(xml.getProperties().getCoreProperties().getTitle());
       assertFalse(xml.getProperties().getCoreProperties().getUnderlyingProperties().getSubjectProperty().isPresent());
-      
+
       xml.close();
    }
-   
+
    @Test
    public void testComments() throws Exception {
       // Default sample file has none
       XMLSlideShow xml = new XMLSlideShow(pack);
 
       assertNull(xml.getCommentAuthors());
-      
+
       for (XSLFSlide slide : xml.getSlides()) {
          assertTrue(slide.getComments().isEmpty());
       }
-      
+
       // Try another with comments
       XMLSlideShow xmlComments = new XMLSlideShow(slTests.openResourceAsStream("45545_Comment.pptx"));
-      
+
       // Has one author
       assertNotNull(xmlComments.getCommentAuthors());
       assertEquals(1, xmlComments.getCommentAuthors().getCTCommentAuthorsList().sizeOfCmAuthorArray());
       assertEquals("XPVMWARE01", xmlComments.getCommentAuthors().getAuthorById(0).getName());
-      
+
       // First two slides have comments
       int i = -1;
       for (XSLFSlide slide : xmlComments.getSlides()) {
          i++;
-         
+
          if(i == 0) {
             assertNotNull(slide.getCommentsPart());
             assertEquals(1, slide.getCommentsPart().getNumberOfComments());
@@ -182,11 +182,11 @@ public class TestXMLSlideShow extends BaseTestSlideShow<XSLFShape,XSLFTextParagr
             assertTrue(slide.getComments().isEmpty());
          }
       }
-      
+
       xmlComments.close();
       xml.close();
    }
-   
+
    @Override
    public XMLSlideShow reopen(SlideShow<XSLFShape,XSLFTextParagraph> show) {
       try {
index d7869d722b71b285c4f651ae6ddcd2d74a9f68ff..7e7d708ced611702b2cfedc7a397e35ebda445ff 100644 (file)
@@ -108,7 +108,7 @@ public class TestXSLFAutoShape {
         assertEquals(TextDirection.VERTICAL, shape.getTextDirection());
         shape.setTextDirection(null);
         assertEquals(TextDirection.HORIZONTAL, shape.getTextDirection());
-        
+
         ppt.close();
     }
 
@@ -211,7 +211,7 @@ public class TestXSLFAutoShape {
         p.setTextAlign(null);
         assertEquals(TextAlign.LEFT, p.getTextAlign());
         assertFalse(p.getXmlObject().getPPr().isSetAlgn());
-        
+
         ppt.close();
     }
 
@@ -231,7 +231,7 @@ public class TestXSLFAutoShape {
         assertEquals(1, p.getTextRuns().size());
         assertSame(r, p.getTextRuns().get(0));
 
-        
+
         assertEquals(18.0, r.getFontSize(), 0); // default font size for text boxes
         assertFalse(rPr.isSetSz());
         r.setFontSize(10.0);
@@ -285,7 +285,7 @@ public class TestXSLFAutoShape {
         assertEquals("POI", r.getRawText());
         r.setText(null);
         assertNull(r.getRawText());
-        
+
         ppt.close();
     }
 
@@ -301,11 +301,13 @@ public class TestXSLFAutoShape {
         assertEquals(ShapeType.TRIANGLE, shape.getShapeType());
 
         for(ShapeType tp : ShapeType.values()) {
-            if (tp.ooxmlId == -1 || tp == ShapeType.SEAL) continue;
+            if (tp.ooxmlId == -1 || tp == ShapeType.SEAL) {
+                continue;
+            }
             shape.setShapeType(tp);
             assertEquals(tp, shape.getShapeType());
         }
-        
+
         ppt.close();
     }
 }
\ No newline at end of file
index a2caca6feb1bf9d359389af5e6aae6c613ff25c1..c80fafef186142930d0e98a0f826cd7a1e721728 100644 (file)
@@ -16,7 +16,6 @@
 ==================================================================== */
 package org.apache.poi.xslf.usermodel;
 
-import static org.apache.poi.POIDataSamples.TEST_PROPERTY;
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
@@ -226,31 +225,31 @@ public class TestXSLFPictureShape {
         InputStream is = _slTests.openResourceAsStream("shapes.pptx");
         XMLSlideShow ppt = new XMLSlideShow(is);
         is.close();
-        
+
         XSLFSlide slide = ppt.getSlides().get(0);
         XSLFPictureShape ps = (XSLFPictureShape)slide.getShapes().get(3);
         slide.removeShape(ps);
-        
+
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
         ppt.write(bos);
         ppt.close();
-        
+
         XMLSlideShow ppt2 = new XMLSlideShow(new ByteArrayInputStream(bos.toByteArray()));
         assertTrue(ppt2.getPictureData().isEmpty());
         ppt2.close();
     }
-    
+
     @Test
     public void testTiffImageBug59742() throws Exception {
         XMLSlideShow slideShow = new XMLSlideShow();
         final InputStream tiffStream = _slTests.openResourceAsStream("testtiff.tif");
         final byte[] pictureData = IOUtils.toByteArray(tiffStream);
         IOUtils.closeQuietly(tiffStream);
-        
+
         XSLFPictureData pic = slideShow.addPicture(pictureData, PictureType.TIFF);
         assertEquals("image/tiff", pic.getContentType());
         assertEquals("image1.tiff", pic.getFileName());
-        
+
         slideShow.close();
     }