poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From one...@apache.org
Subject svn commit: r1815047 - in /poi/trunk: src/ooxml/java/org/apache/poi/xwpf/usermodel/ src/ooxml/testcases/org/apache/poi/xwpf/ src/ooxml/testcases/org/apache/poi/xwpf/usermodel/ test-data/document/
Date Sun, 12 Nov 2017 21:48:42 GMT
Author: onealj
Date: Sun Nov 12 21:48:41 2017
New Revision: 1815047

URL: http://svn.apache.org/viewvc?rev=1815047&view=rev
Log:
bug 61745: add support for charts in XWPF/docx. Thanks to Sandeep Tiwari for the patch

Added:
    poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFChart.java
      - copied, changed from r1815042, poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFPictureData.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFChart.java
      - copied, changed from r1815042, poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFPictureData.java
    poi/trunk/test-data/document/61745.docx   (with props)
Modified:
    poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java
    poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRelation.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/AllXWPFTests.java

Copied: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFChart.java (from r1815042,
poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFPictureData.java)
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFChart.java?p2=poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFChart.java&p1=poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFPictureData.java&r1=1815042&r2=1815047&rev=1815047&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFPictureData.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFChart.java Sun Nov 12 21:48:41
2017
@@ -17,135 +17,98 @@
 
 package org.apache.poi.xwpf.usermodel;
 
+import static org.apache.poi.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
+
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
 import java.util.Arrays;
 
+import javax.xml.namespace.QName;
+
 import org.apache.poi.POIXMLDocumentPart;
 import org.apache.poi.POIXMLException;
-import org.apache.poi.POIXMLRelation;
 import org.apache.poi.openxml4j.opc.OPCPackage;
 import org.apache.poi.openxml4j.opc.PackagePart;
+import org.apache.poi.openxml4j.opc.PackageRelationship;
+import org.apache.poi.util.Beta;
 import org.apache.poi.util.IOUtils;
+import org.apache.poi.util.Internal;
+import org.apache.xmlbeans.XmlException;
+import org.apache.xmlbeans.XmlOptions;
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTChart;
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTChartSpace;
+import org.openxmlformats.schemas.drawingml.x2006.chart.ChartSpaceDocument;
 
 /**
- * Raw picture data, normally attached to a WordprocessingML Drawing.
- * As a rule, pictures are stored in the /word/media/ part of a WordprocessingML package.
- *
- * @author Philipp Epp
+ * Represents a Chart in a .docx file
  */
-public class XWPFPictureData extends POIXMLDocumentPart {
+@Beta
+public class XWPFChart extends POIXMLDocumentPart {
 
     /**
-     * Relationships for each known picture type
+     * Root element of the Chart part
      */
-    protected static final POIXMLRelation[] RELATIONS;
-
-    static {
-        RELATIONS = new POIXMLRelation[13];
-        RELATIONS[Document.PICTURE_TYPE_EMF] = XWPFRelation.IMAGE_EMF;
-        RELATIONS[Document.PICTURE_TYPE_WMF] = XWPFRelation.IMAGE_WMF;
-        RELATIONS[Document.PICTURE_TYPE_PICT] = XWPFRelation.IMAGE_PICT;
-        RELATIONS[Document.PICTURE_TYPE_JPEG] = XWPFRelation.IMAGE_JPEG;
-        RELATIONS[Document.PICTURE_TYPE_PNG] = XWPFRelation.IMAGE_PNG;
-        RELATIONS[Document.PICTURE_TYPE_DIB] = XWPFRelation.IMAGE_DIB;
-        RELATIONS[Document.PICTURE_TYPE_GIF] = XWPFRelation.IMAGE_GIF;
-        RELATIONS[Document.PICTURE_TYPE_TIFF] = XWPFRelation.IMAGE_TIFF;
-        RELATIONS[Document.PICTURE_TYPE_EPS] = XWPFRelation.IMAGE_EPS;
-        RELATIONS[Document.PICTURE_TYPE_BMP] = XWPFRelation.IMAGE_BMP;
-        RELATIONS[Document.PICTURE_TYPE_WPG] = XWPFRelation.IMAGE_WPG;
-    }
-
-    private Long checksum;
+    private final CTChartSpace chartSpace;
 
     /**
-     * Create a new XWPFGraphicData node
+     * The Chart within that
      */
-    protected XWPFPictureData() {
-        super();
-    }
+    private final CTChart chart;
+
+    // lazy initialization
+    private Long checksum;
 
     /**
-     * Construct XWPFPictureData from a package part
+     * Construct a chart from a package part.
      *
-     * @param part the package part holding the drawing data,
+     * @param part the package part holding the chart data,
+     * the content type must be <code>application/vnd.openxmlformats-officedocument.drawingml.chart+xml</code>
      * 
-     * @since POI 3.14-Beta1
+     * @since POI 4.0.0
      */
-    public XWPFPictureData(PackagePart part) {
+    protected XWPFChart(PackagePart part) throws IOException, XmlException {
         super(part);
+
+        chartSpace = ChartSpaceDocument.Factory.parse(part.getInputStream(), DEFAULT_XML_OPTIONS).getChartSpace();

+        chart = chartSpace.getChart();
     }
-    
+      
     @Override
     protected void onDocumentRead() throws IOException {
         super.onDocumentRead();
     }
 
     /**
-     * Gets the picture data as a byte array.
-     * <p>
-     * Note, that this call might be expensive since all the picture data is copied into
a temporary byte array.
-     * You can grab the picture data directly from the underlying package part as follows:
-     * <br>
-     * <code>
-     * InputStream is = getPackagePart().getInputStream();
-     * </code>
-     * </p>
+     * Return the underlying CTChartSpace bean, the root element of the Chart part.
      *
-     * @return the Picture data.
+     * @return the underlying CTChartSpace bean
      */
-    public byte[] getData() {
-        try {
-            return IOUtils.toByteArray(getPackagePart().getInputStream());
-        } catch (IOException e) {
-            throw new POIXMLException(e);
-        }
+    @Internal
+    public CTChartSpace getCTChartSpace() {
+        return chartSpace;
     }
 
     /**
-     * Returns the file name of the image, eg image7.jpg . The original filename
-     * isn't always available, but if it can be found it's likely to be in the
-     * CTDrawing
-     */
-    public String getFileName() {
-        String name = getPackagePart().getPartName().getName();
-        return name.substring(name.lastIndexOf('/') + 1);
-    }
-
-    /**
-     * Suggests a file extension for this image.
+     * Return the underlying CTChart bean, within the Chart Space
      *
-     * @return the file extension.
+     * @return the underlying CTChart bean
      */
-    public String suggestFileExtension() {
-        return getPackagePart().getPartName().getExtension();
+    @Internal
+    public CTChart getCTChart() {
+        return chart;
     }
 
-    /**
-     * Return an integer constant that specifies type of this picture
-     *
-     * @return an integer constant that specifies type of this picture
-     * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_EMF
-     * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_WMF
-     * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_PICT
-     * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_JPEG
-     * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_PNG
-     * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_DIB
-     */
-    public int getPictureType() {
-        String contentType = getPackagePart().getContentType();
-        for (int i = 0; i < RELATIONS.length; i++) {
-            if (RELATIONS[i] == null) {
-                continue;
-            }
+    @Override
+    protected void commit() throws IOException {
+        XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
+        xmlOptions.setSaveSyntheticDocumentElement(new QName(CTChartSpace.type.getName().getNamespaceURI(),
"chartSpace", "c"));
 
-            if (RELATIONS[i].getContentType().equals(contentType)) {
-                return i;
-            }
+        try (OutputStream out = getPackagePart().getOutputStream()) {
+            chartSpace.save(out, xmlOptions);
         }
-        return 0;
     }
-
+    
     public Long getChecksum() {
         if (this.checksum == null) {
             InputStream is = null;
@@ -156,7 +119,11 @@ public class XWPFPictureData extends POI
             } catch (IOException e) {
                 throw new POIXMLException(e);
             } finally {
-                IOUtils.closeQuietly(is);
+                try {
+                    if (is != null) is.close();
+                } catch (IOException e) {
+                    throw new POIXMLException(e);
+                }
             }
             this.checksum = IOUtils.calculateChecksum(data);
         }
@@ -165,7 +132,7 @@ public class XWPFPictureData extends POI
 
     @Override
     public boolean equals(Object obj) {
-        /*
+        /**
          * In case two objects ARE equal, but its not the same instance, this
          * implementation will always run through the whole
          * byte-array-comparison before returning true. If this will turn into a
@@ -185,55 +152,14 @@ public class XWPFPictureData extends POI
             return false;
         }
 
-        if (!(obj instanceof XWPFPictureData)) {
-            return false;
-        }
-
-        XWPFPictureData picData = (XWPFPictureData) obj;
-        PackagePart foreignPackagePart = picData.getPackagePart();
-        PackagePart ownPackagePart = this.getPackagePart();
-
-        if ((foreignPackagePart != null && ownPackagePart == null)
-                || (foreignPackagePart == null && ownPackagePart != null)) {
-            return false;
-        }
-
-        if (ownPackagePart != null) {
-            OPCPackage foreignPackage = foreignPackagePart.getPackage();
-            OPCPackage ownPackage = ownPackagePart.getPackage();
-
-            if ((foreignPackage != null && ownPackage == null)
-                    || (foreignPackage == null && ownPackage != null)) {
-                return false;
-            }
-            if (ownPackage != null) {
-
-                if (!ownPackage.equals(foreignPackage)) {
-                    return false;
-                }
-            }
-        }
-
-        Long foreignChecksum = picData.getChecksum();
-        Long localChecksum = getChecksum();
-
-        if (!(localChecksum.equals(foreignChecksum))) {
+        if (!(obj instanceof XWPFChart)) {
             return false;
         }
-        return Arrays.equals(this.getData(), picData.getData());
+        return false;
     }
-
+   
     @Override
     public int hashCode() {
         return getChecksum().hashCode();
     }
-
-    /**
-     * *PictureData objects store the actual content in the part directly without keeping
a
-     * copy like all others therefore we need to handle them differently.
-     */
-    @Override
-    protected void prepareForCommit() {
-        // do not clear the part here
-    }
 }

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java?rev=1815047&r1=1815046&r2=1815047&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java Sun Nov 12 21:48:41
2017
@@ -96,6 +96,7 @@ public class XWPFDocument extends POIXML
     protected XWPFFootnotes footnotes;
     private CTDocument1 ctDocument;
     private XWPFSettings settings;
+    protected final List<XWPFChart> charts = new ArrayList<>();
     /**
      * Keeps track on all id-values used in this document and included parts, like headers,
footers, etc.
      */
@@ -220,6 +221,11 @@ public class XWPFDocument extends POIXML
                     picData.onDocumentRead();
                     registerPackagePictureData(picData);
                     pictures.add(picData);
+                } else if (relation.equals(XWPFRelation.CHART.getRelation())) {
+                    //now we can use all methods to modify charts in XWPFDocument
+                    XWPFChart chartData = (XWPFChart) p;
+                    chartData.onDocumentRead();
+                    charts.add(chartData);
                 } else if (relation.equals(XWPFRelation.GLOSSARY_DOCUMENT.getRelation()))
{
                     // We don't currently process the glossary itself
                     // Until we do, we do need to load the glossary child parts of it
@@ -324,6 +330,12 @@ public class XWPFDocument extends POIXML
     }
 
     /**
+     * @return list of XWPFCharts in this document
+     */
+    public List<XWPFChart> getCharts() {
+        return Collections.unmodifiableList(charts);
+    }
+    /**
      * @see org.apache.poi.xwpf.usermodel.IBody#getTableArray(int)
      */
     @Override

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRelation.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRelation.java?rev=1815047&r1=1815046&r2=1815047&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRelation.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRelation.java Sun Nov 12 21:48:41
2017
@@ -112,6 +112,12 @@ public final class XWPFRelation extends
             "/word/theme/theme#.xml",
             null
     );
+    public static final XWPFRelation CHART = new XWPFRelation(
+            "application/vnd.openxmlformats-officedocument.drawingml.chart+xml",
+            "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart",
+            "/word/charts/chart#.xml",
+            XWPFChart.class
+    );
     public static final XWPFRelation HYPERLINK = new XWPFRelation(
             null,
             "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink",

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/AllXWPFTests.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/AllXWPFTests.java?rev=1815047&r1=1815046&r2=1815047&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/AllXWPFTests.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/AllXWPFTests.java Sun Nov 12 21:48:41
2017
@@ -19,6 +19,7 @@ package org.apache.poi.xwpf;
 
 import org.apache.poi.xwpf.extractor.TestXWPFWordExtractor;
 import org.apache.poi.xwpf.model.TestXWPFHeaderFooterPolicy;
+import org.apache.poi.xwpf.usermodel.TestXWPFChart;
 import org.apache.poi.xwpf.usermodel.TestXWPFDocument;
 import org.apache.poi.xwpf.usermodel.TestXWPFHeader;
 import org.apache.poi.xwpf.usermodel.TestXWPFHeadings;
@@ -38,6 +39,7 @@ import org.junit.runners.Suite;
 @Suite.SuiteClasses({
         TestXWPFBugs.class,
         org.apache.poi.xwpf.usermodel.TestXWPFBugs.class,
+        TestXWPFChart.class,
         TestXWPFDocument.class,
         TestXWPFWordExtractor.class,
         TestXWPFHeaderFooterPolicy.class,
@@ -53,4 +55,4 @@ import org.junit.runners.Suite;
         TestPackageCorePropertiesGetKeywords.class
 })
 public final class AllXWPFTests {
-}
\ No newline at end of file
+}

Copied: poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFChart.java (from
r1815042, poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFPictureData.java)
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFChart.java?p2=poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFChart.java&p1=poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFPictureData.java&r1=1815042&r2=1815047&rev=1815047&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFPictureData.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFChart.java Sun Nov
12 21:48:41 2017
@@ -17,164 +17,71 @@
 
 package org.apache.poi.xwpf.usermodel;
 
-import static org.junit.Assert.assertArrayEquals;
-
-import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.util.List;
 
-import junit.framework.TestCase;
-
-import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
-import org.apache.poi.openxml4j.opc.PackageRelationship;
-import org.apache.poi.xssf.usermodel.XSSFRelation;
 import org.apache.poi.xwpf.XWPFTestDataSamples;
-import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTChart;
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTTitle;
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTTx;
+import org.openxmlformats.schemas.drawingml.x2006.main.CTRegularTextRun;
+import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody;
+import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraph;
 
-public class TestXWPFPictureData extends TestCase {
-
-    public void testRead() throws InvalidFormatException, IOException {
-        XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("VariousPictures.docx");
-        List<XWPFPictureData> pictures = sampleDoc.getAllPictures();
-
-        assertEquals(5, pictures.size());
-        String[] ext = {"wmf", "png", "emf", "emf", "jpeg"};
-        for (int i = 0; i < pictures.size(); i++) {
-            assertEquals(ext[i], pictures.get(i).suggestFileExtension());
-        }
-
-        int num = pictures.size();
-
-        byte[] pictureData = XWPFTestDataSamples.getImage("nature1.jpg");
-
-        String relationId = sampleDoc.addPictureData(pictureData, XWPFDocument.PICTURE_TYPE_JPEG);
-        // picture list was updated
-        assertEquals(num + 1, pictures.size());
-        XWPFPictureData pict = (XWPFPictureData) sampleDoc.getRelationById(relationId);
-        assertEquals("jpeg", pict.suggestFileExtension());
-        assertArrayEquals(pictureData, pict.getData());
-    }
+import junit.framework.TestCase;
 
-    public void testPictureInHeader() throws IOException {
-        XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("headerPic.docx");
-        verifyOneHeaderPicture(sampleDoc);
+public class TestXWPFChart extends TestCase {
 
-        XWPFDocument readBack = XWPFTestDataSamples.writeOutAndReadBack(sampleDoc);
-        verifyOneHeaderPicture(readBack);
+    /**
+     * test method to check charts are null
+     *
+     * @throws IOException
+     */
+    public void testRead() throws IOException
+    {
+        XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("61745.docx");
+        List<XWPFChart> charts = sampleDoc.getCharts();
+        assertNotNull(charts);
+        assertEquals(2, charts.size());
+        assertNotNull(charts.get(0));
+        assertNotNull(charts.get(1));
     }
     
-    public void testCreateHeaderPicture() throws Exception {
-        XWPFDocument doc = new XWPFDocument();
-        
-        // Starts with no header
-        XWPFHeaderFooterPolicy policy = doc.getHeaderFooterPolicy();
-        assertNull(policy);
-        
-        // Add a default header
-        policy = doc.createHeaderFooterPolicy();
-        XWPFHeader header = policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);
-        header.createParagraph().createRun().setText("Hello, Header World!");
-        header.createParagraph().createRun().setText("Paragraph 2");
-        assertEquals(0, header.getAllPictures().size());
-        assertEquals(2, header.getParagraphs().size());
-        
-        // Add a picture to the first paragraph
-        header.getParagraphs().get(0).getRuns().get(0).addPicture(
-                new ByteArrayInputStream(new byte[] {1,2,3,4}), 
-                Document.PICTURE_TYPE_JPEG, "test.jpg", 2, 2);
-        
-        // Check
-        verifyOneHeaderPicture(doc);
-        
-        // Save, re-load, re-check
-        XWPFDocument readBack = XWPFTestDataSamples.writeOutAndReadBack(doc);
-        verifyOneHeaderPicture(readBack);
-    }
-
-    private void verifyOneHeaderPicture(XWPFDocument sampleDoc) {
-        XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy();
-
-        XWPFHeader header = policy.getDefaultHeader();
-
-        List<XWPFPictureData> pictures = header.getAllPictures();
-        assertEquals(1, pictures.size());
-    }
-
-    public void testNew() throws InvalidFormatException, IOException {
-        XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("EmptyDocumentWithHeaderFooter.docx");
-        byte[] jpegData = XWPFTestDataSamples.getImage("nature1.jpg");
-        assertNotNull(jpegData);
-        byte[] gifData = XWPFTestDataSamples.getImage("nature1.gif");
-        assertNotNull(gifData);
-        byte[] pngData = XWPFTestDataSamples.getImage("nature1.png");
-        assertNotNull(pngData);
-
-        List<XWPFPictureData> pictures = doc.getAllPictures();
-        assertEquals(0, pictures.size());
-
-        // Document shouldn't have any image relationships
-        assertEquals(13, doc.getPackagePart().getRelationships().size());
-        for (PackageRelationship rel : doc.getPackagePart().getRelationships()) {
-            if (rel.getRelationshipType().equals(XSSFRelation.IMAGE_JPEG.getRelation()))
{
-                fail("Shouldn't have JPEG yet");
-            }
-        }
-
-        // Add the image
-        String relationId = doc.addPictureData(jpegData, XWPFDocument.PICTURE_TYPE_JPEG);
-        assertEquals(1, pictures.size());
-        XWPFPictureData jpgPicData = (XWPFPictureData) doc.getRelationById(relationId);
-        assertEquals("jpeg", jpgPicData.suggestFileExtension());
-        assertArrayEquals(jpegData, jpgPicData.getData());
-
-        // Ensure it now has one
-        assertEquals(14, doc.getPackagePart().getRelationships().size());
-        PackageRelationship jpegRel = null;
-        for (PackageRelationship rel : doc.getPackagePart().getRelationships()) {
-            if (rel.getRelationshipType().equals(XWPFRelation.IMAGE_JPEG.getRelation()))
{
-                if (jpegRel != null)
-                    fail("Found 2 jpegs!");
-                jpegRel = rel;
-            }
-        }
-        assertNotNull("JPEG Relationship not found", jpegRel);
-
-        // Check the details
-        assertNotNull(jpegRel);
-        assertEquals(XWPFRelation.IMAGE_JPEG.getRelation(), jpegRel.getRelationshipType());
-        assertEquals("/word/document.xml", jpegRel.getSource().getPartName().toString());
-        assertEquals("/word/media/image1.jpeg", jpegRel.getTargetURI().getPath());
-
-        XWPFPictureData pictureDataByID = doc.getPictureDataByID(jpegRel.getId());
-        assertArrayEquals(jpegData, pictureDataByID.getData());
-
-        // Save an re-load, check it appears
-        doc = XWPFTestDataSamples.writeOutAndReadBack(doc);
-        assertEquals(1, doc.getAllPictures().size());
-        assertEquals(1, doc.getAllPackagePictures().size());
-
-        // verify the picture that we read back in
-        pictureDataByID = doc.getPictureDataByID(jpegRel.getId());
-        assertArrayEquals(jpegData, pictureDataByID.getData());
-
-    }
-
-    public void testBug51770() throws InvalidFormatException, IOException {
-        XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Bug51170.docx");
-        XWPFHeaderFooterPolicy policy = doc.getHeaderFooterPolicy();
-        XWPFHeader header = policy.getDefaultHeader();
-        for (XWPFParagraph paragraph : header.getParagraphs()) {
-            for (XWPFRun run : paragraph.getRuns()) {
-                for (XWPFPicture picture : run.getEmbeddedPictures()) {
-                    if (paragraph.getDocument() != null) {
-                        XWPFPictureData data = picture.getPictureData();
-                        if (data != null) {
-                            fail("Should have returned null: "+ data.getFileName());
-                        }
-                    }
-                }
-            }
-        }
-
+    /**
+     * test method to add chart title and check whether it's set
+     *
+     * @throws IOException
+     */
+    public void testChartTitle() throws IOException
+    {
+        XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("61745.docx");
+        List<XWPFChart> charts = sampleDoc.getCharts();
+        XWPFChart chart=charts.get(0);
+        CTChart ctChart = chart.getCTChart();
+        CTTitle title = ctChart.getTitle();
+        CTTx tx = title.addNewTx();
+        CTTextBody rich = tx.addNewRich();
+        rich.addNewBodyPr();
+        rich.addNewLstStyle();
+        CTTextParagraph p = rich.addNewP();
+        CTRegularTextRun r = p.addNewR();
+        r.addNewRPr();
+        r.setT("XWPF CHART");
+        assertEquals("XWPF CHART", chart.getCTChart().getTitle().getTx().getRich().getPArray(0).getRArray(0).getT().toString());
+    }
+    /**
+     * test method to check relationship
+     *
+     * @throws IOException
+     */
+    public void testChartRelation() throws IOException
+    {
+        XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("61745.docx");
+        List<XWPFChart> charts = sampleDoc.getCharts();
+        XWPFChart chart=charts.get(0);
+        assertEquals(XWPFRelation.CHART.getContentType(), chart.getPackagePart().getContentType().toString());
+        assertEquals("/word/document.xml", chart.getParent().getPackagePart().getPartName().toString());
+        assertEquals("/word/charts/chart1.xml", chart.getPackagePart().getPartName().toString());
     }
 }
+

Added: poi/trunk/test-data/document/61745.docx
URL: http://svn.apache.org/viewvc/poi/trunk/test-data/document/61745.docx?rev=1815047&view=auto
==============================================================================
Binary file - no diff available.

Propchange: poi/trunk/test-data/document/61745.docx
------------------------------------------------------------------------------
--- svn:mime-type (added)
+++ svn:mime-type Sun Nov 12 21:48:41 2017
@@ -0,0 +1 @@
+application/vnd.openxmlformats-officedocument.wordprocessingml.document



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message