1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
/* ====================================================================
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.xwpf.usermodel;
import org.apache.poi.ooxml.POIXMLDocumentPart;
import org.apache.poi.ooxml.POIXMLException;
import org.apache.poi.ooxml.POIXMLRelation;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.PackagePart;
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.wordprocessingml.x2006.main.CTComment;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTComments;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CommentsDocument;
import javax.xml.namespace.QName;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import static org.apache.poi.ooxml.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
/**
* specifies all of the comments defined in the current document
*/
public class XWPFComments extends POIXMLDocumentPart {
XWPFDocument document;
private List<XWPFComment> comments = new ArrayList<>();
private List<XWPFPictureData> pictures = new ArrayList<>();
private CTComments ctComments;
/**
* Construct XWPFComments from a package part
*
* @param part the package part holding the data of the footnotes,
*/
public XWPFComments(PackagePart part) {
super(part);
}
/**
* Construct XWPFComments from scratch for a new document.
*/
public XWPFComments() {
ctComments = CTComments.Factory.newInstance();
}
/**
* read comments form an existing package
*/
@Override
public void onDocumentRead() throws IOException {
try (InputStream is = getPackagePart().getInputStream()) {
CommentsDocument doc = CommentsDocument.Factory.parse(is, DEFAULT_XML_OPTIONS);
ctComments = doc.getComments();
for (CTComment ctComment : ctComments.getCommentList()) {
comments.add(new XWPFComment(ctComment, this));
}
} catch (XmlException e) {
throw new POIXMLException("Unable to read comments", e);
}
for (POIXMLDocumentPart poixmlDocumentPart : getRelations()) {
if (poixmlDocumentPart instanceof XWPFPictureData) {
XWPFPictureData xwpfPicData = (XWPFPictureData) poixmlDocumentPart;
pictures.add(xwpfPicData);
document.registerPackagePictureData(xwpfPicData);
}
}
}
/**
* Adds a picture to the comments.
*
* @param is The stream to read image from
* @param format The format of the picture.
* @return the index to this picture (0 based), the added picture can be
* obtained from {@link #getAllPictures()} .
* @throws InvalidFormatException If the format of the picture is not known.
* @throws IOException If reading the picture-data from the stream fails.
*/
public String addPictureData(InputStream is, int format) throws InvalidFormatException, IOException {
byte[] data = IOUtils.toByteArray(is);
return addPictureData(data, format);
}
/**
* Adds a picture to the comments.
*
* @param pictureData The picture data
* @param format The format of the picture.
* @return the index to this picture (0 based), the added picture can be
* obtained from {@link #getAllPictures()} .
* @throws InvalidFormatException If the format of the picture is not known.
*/
public String addPictureData(byte[] pictureData, int format) throws InvalidFormatException {
XWPFPictureData xwpfPicData = document.findPackagePictureData(pictureData, format);
POIXMLRelation relDesc = XWPFPictureData.RELATIONS[format];
if (xwpfPicData == null) {
/* Part doesn't exist, create a new one */
int idx = getXWPFDocument().getNextPicNameNumber(format);
xwpfPicData = (XWPFPictureData) createRelationship(relDesc, XWPFFactory.getInstance(), idx);
/* write bytes to new part */
PackagePart picDataPart = xwpfPicData.getPackagePart();
try (OutputStream out = picDataPart.getOutputStream()) {
out.write(pictureData);
} catch (IOException e) {
throw new POIXMLException(e);
}
document.registerPackagePictureData(xwpfPicData);
pictures.add(xwpfPicData);
return getRelationId(xwpfPicData);
} else if (!getRelations().contains(xwpfPicData)) {
/*
* Part already existed, but was not related so far. Create
* relationship to the already existing part and update
* POIXMLDocumentPart data.
*/
// TODO add support for TargetMode.EXTERNAL relations.
RelationPart rp = addRelation(null, XWPFRelation.IMAGES, xwpfPicData);
pictures.add(xwpfPicData);
return rp.getRelationship().getId();
} else {
/* Part already existed, get relation id and return it */
return getRelationId(xwpfPicData);
}
}
/**
* save and commit comments
*/
@Override
protected void commit() throws IOException {
XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
xmlOptions.setSaveSyntheticDocumentElement(new QName(
CTComments.type.getName().getNamespaceURI(), "comments"));
PackagePart part = getPackagePart();
OutputStream out = part.getOutputStream();
ctComments.save(out, xmlOptions);
out.close();
}
public List<XWPFPictureData> getAllPictures() {
return Collections.unmodifiableList(pictures);
}
/**
* Gets the underlying CTComments object for the comments.
*
* @return CTComments object
*/
public CTComments getCtComments() {
return ctComments;
}
/**
* set a new comments
*/
@Internal
public void setCtComments(CTComments ctComments) {
this.ctComments = ctComments;
}
/**
* Get the list of {@link XWPFComment} in the Comments part.
*
* @return
*/
public List<XWPFComment> getComments() {
return comments;
}
/**
* Get the specified comment by position
*
* @param pos Array position of the comment
* @return
*/
public XWPFComment getComment(int pos) {
if (pos >= 0 && pos < ctComments.sizeOfCommentArray()) {
return getComments().get(pos);
}
return null;
}
/**
* Get the specified comment by comment id
*
* @param id comment id
* @return the specified comment
*/
public XWPFComment getCommentByID(String id) {
for (XWPFComment comment : comments) {
if (comment.getId().equals(id)) {
return comment;
}
}
return null;
}
/**
* Get the specified comment by ctComment
*
* @param ctComment
* @return
*/
public XWPFComment getComment(CTComment ctComment) {
for (int i = 0; i < comments.size(); i++) {
if (comments.get(i).getCtComment() == ctComment) {
return comments.get(i);
}
}
return null;
}
/**
* Create a new comment and add it to the document.
*
* @param cid comment Id
* @return
*/
public XWPFComment createComment(BigInteger cid) {
CTComment ctComment = ctComments.addNewComment();
ctComment.setId(cid);
XWPFComment comment = new XWPFComment(ctComment, this);
comments.add(comment);
return comment;
}
/**
* Remove the specified comment if present.
*
* @param pos Array position of the comment to be removed
* @return True if the comment was removed.
*/
public boolean removeComment(int pos) {
if (pos >= 0 && pos < ctComments.sizeOfCommentArray()) {
comments.remove(pos);
ctComments.removeComment(pos);
return true;
}
return false;
}
public XWPFDocument getXWPFDocument() {
if (null != document) {
return document;
}
return (XWPFDocument) getParent();
}
public void setXWPFDocument(XWPFDocument document) {
this.document = document;
}
}
|