Bug 61745 - [PATCH] Added Support To create chart object For DOCX
Summary: [PATCH] Added Support To create chart object For DOCX
Alias: None
Product: POI
Classification: Unclassified
Component: XWPF (show other bugs)
Version: 3.17-FINAL
Hardware: All All
: P2 enhancement (vote)
Target Milestone: ---
Assignee: POI Developers List
Depends on:
Reported: 2017-11-11 11:02 UTC by SANDEEP TIWARI
Modified: 2017-12-27 20:57 UTC (History)
1 user (show)

This Patch contains 1 java files XWPFChart and 2 modified java XWPFDocument, XWPFRelation.This patch we can use to create chart object reading Document file. (2.99 KB, application/tar+gzip)
2017-11-11 11:18 UTC, SANDEEP TIWARI
Test java file to check functionality of (1.81 KB, application/octet-stream)
2017-11-11 18:57 UTC, SANDEEP TIWARI
Test input ms-word file (23.69 KB, application/vnd.openxmlformats-officedocument.wordprocessingml.document)
2017-11-11 19:02 UTC, SANDEEP TIWARI
Test output file of ms-word (19.24 KB, application/vnd.openxmlformats-officedocument.wordprocessingml.document)
2017-11-11 19:04 UTC, SANDEEP TIWARI
updated Patch file with required changes (25.48 KB, application/tar+gzip)
2017-11-12 16:22 UTC, SANDEEP TIWARI

Note You need to log in before you can comment on or make changes to this bug.
Description SANDEEP TIWARI 2017-11-11 11:02:19 UTC

Comment 1 SANDEEP TIWARI 2017-11-11 11:18:08 UTC
Created attachment 35516 [details]
This Patch contains 1 java files XWPFChart and 2 modified java XWPFDocument, XWPFRelation.This patch we can use to create chart object reading Document file.

In DOCX (MS-Word) document we are not able to create chart object by reading chart from document file.In this Patch i added 1 java files XWPFChart which we can use to create chart object and then can use all method to add/modify chart properties same as we are using in case of PPT/EXCEL along with that i modified 2 java files XWPFDocument, XWPFRelation to parse chart from document and add relation in document file.
Comment 2 SANDEEP TIWARI 2017-11-11 18:57:31 UTC
Created attachment 35517 [details]
Test java file to check functionality of
Comment 3 SANDEEP TIWARI 2017-11-11 19:01:15 UTC
Comment on attachment 35517 [details]
Test java file to check functionality of

This is the test java file to test the functionality of new enhancement i.e creating chart object from MS-word file
Comment 4 SANDEEP TIWARI 2017-11-11 19:02:41 UTC
Created attachment 35518 [details]
Test input ms-word file

this is test ms-word file from which we are creating chart object,
Comment 5 SANDEEP TIWARI 2017-11-11 19:04:23 UTC
Created attachment 35519 [details]
Test output file of ms-word

this is output file after executing ApacheXWPFChartTest.java file.in test program we shown how we can use our existing function to modify chart properties
Comment 6 Mark Murphy 2017-11-11 21:49:19 UTC
I can't read the patch as it looks like it was just pasted into the text field in a non-text format. Please attach a file that contains the whole patch. I can read your test file though.
Comment 7 Javen O'Neal 2017-11-11 23:57:29 UTC
This patch was generated with "ant -f patch.xml" (patch.tar.gz was the hint)

I fixed the mime-type of the patch to application/tar+gzip (Bugzilla sets the mime type of attachments marked as a patch to "text/plain", hence the binary mess your browser displayed).

This patch should download directly now. Apply this patch to a clean svn checkout using
> ant -f patch.xml apply
> tar xvzf patch.tar.gz
> svn patch patch.txt
> svn add ...

[1] https://poi.apache.org/guidelines.html#Approach+1+-+use+Ant
Comment 8 Javen O'Neal 2017-11-12 00:21:02 UTC
Thank you for adding support for charts in XWPF!

Would you be able to write some unit tests (or modify existing ones) to cover the new and changed functionality? See https://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFPictureData.java?view=log for an example.

Would you also be able to make the following changes:
return Collections.unmodifiableList(charts);

Remove @Deprecated API's in XWPFChart. No need for deprecated API's in new code.
Update javadocs to @since POI 4.0.0

It looks like this was modeled after XWPFPictureData. To whoever ends up committing this code to the svn repo, an "svn cp XWPFPictureData.java XWPFChart.java; cp patch.tar.gz/XWPFChart.java ." might make sense.
If there's any code that XWPFChart and XWPFPictureData share in common, it may make sense to create a class that implements the common methods.
Comment 9 SANDEEP TIWARI 2017-11-12 08:28:20 UTC
@Mark Murphy sir, i used "ant -f patch.xml" command to create patch.

@Javen O'Neal sure sir.
i will create new java file with some test case using junit and will update the patch with required changes.
Comment 10 SANDEEP TIWARI 2017-11-12 16:22:02 UTC
Created attachment 35522 [details]
updated Patch file with required changes

Updated patch file with required changes,along with that added unit test case java file.
Comment 11 Javen O'Neal 2017-11-12 21:54:15 UTC
Applied attachment 35522 [details] in r1815047 with a few modifications:

In XWPFChart.java:
> protected void XWPFChart(InputStream is)
doesn't make sense to me. Why is there a method with the same name as the class that isn't a constructor? I removed this, as well as the body of onDocumentRead().
I didn't check to see if removing the onDocumentRead body causes problems with reading a chart in a document.

chartSpace and chart class variables should be final if you don't intend to change them. (This is how I discovered void XWPFChart, which is not a constructor.) I made these final.

The output stream should be closed in a try-with-resources block to avoid leaks.
> chartSpace.save(out, xmlOptions);
I changed this, since we're dropping support for Java 6.

I updated the `@since POI 3.14-Beta1` Javadoc remarks to 4.0.0.

Some of the new files had inconsistent whitespace (tabs) and were missing the ASL 2.0 license header. I fixed this.
Comment 12 SANDEEP TIWARI 2017-11-13 06:12:18 UTC
@javen thanks for updating patch. I used some of methods for future uses, I am also writing support of create charts in document file without reading any tamplate doc file, I will update my direct chart support in ms doc code accordingly and we will review it in future.
Again thanks for updating patch. :)
Comment 13 Dominik Stadler 2017-12-27 20:57:45 UTC
This is applied as far as I see, please reopen if not or report new issues for further patches.