Lines 15-31
Link Here
|
15 |
limitations under the License. |
15 |
limitations under the License. |
16 |
==================================================================== */ |
16 |
==================================================================== */ |
17 |
|
17 |
|
18 |
package org.apache.poi.xwpf; |
18 |
package org.apache.poi.xwpf.usermodel; |
|
|
19 |
|
20 |
import java.io.IOException; |
21 |
import java.io.OutputStream; |
22 |
import java.util.Arrays; |
23 |
import java.util.List; |
19 |
|
24 |
|
20 |
import junit.framework.TestCase; |
25 |
import junit.framework.TestCase; |
21 |
|
26 |
|
|
|
27 |
import org.apache.poi.POIXMLDocumentPart; |
22 |
import org.apache.poi.POIXMLProperties; |
28 |
import org.apache.poi.POIXMLProperties; |
23 |
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; |
29 |
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; |
24 |
import org.apache.poi.openxml4j.opc.OPCPackage; |
30 |
import org.apache.poi.openxml4j.opc.OPCPackage; |
25 |
import org.apache.poi.openxml4j.opc.PackagePart; |
31 |
import org.apache.poi.openxml4j.opc.PackagePart; |
26 |
import org.apache.poi.xwpf.usermodel.XWPFDocument; |
32 |
import org.apache.poi.openxml4j.opc.PackagePartName; |
27 |
import org.apache.poi.xwpf.usermodel.XWPFParagraph; |
33 |
import org.apache.poi.openxml4j.opc.PackageRelationship; |
28 |
import org.apache.poi.xwpf.usermodel.XWPFRelation; |
34 |
import org.apache.poi.openxml4j.opc.PackagingURIHelper; |
|
|
35 |
import org.apache.poi.openxml4j.opc.TargetMode; |
36 |
import org.apache.poi.xwpf.XWPFTestDataSamples; |
29 |
import org.apache.xmlbeans.XmlCursor; |
37 |
import org.apache.xmlbeans.XmlCursor; |
30 |
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP; |
38 |
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP; |
31 |
|
39 |
|
Lines 65-71
Link Here
|
65 |
assertNotNull(xml.getStyle()); |
73 |
assertNotNull(xml.getStyle()); |
66 |
} |
74 |
} |
67 |
|
75 |
|
68 |
public void testMetadataBasics() { |
76 |
public void testMetadataBasics() throws IOException { |
69 |
XWPFDocument xml = XWPFTestDataSamples.openSampleDocument("sample.docx"); |
77 |
XWPFDocument xml = XWPFTestDataSamples.openSampleDocument("sample.docx"); |
70 |
assertNotNull(xml.getProperties().getCoreProperties()); |
78 |
assertNotNull(xml.getProperties().getCoreProperties()); |
71 |
assertNotNull(xml.getProperties().getExtendedProperties()); |
79 |
assertNotNull(xml.getProperties().getExtendedProperties()); |
Lines 78-84
Link Here
|
78 |
assertEquals(null, xml.getProperties().getCoreProperties().getUnderlyingProperties().getSubjectProperty().getValue()); |
86 |
assertEquals(null, xml.getProperties().getCoreProperties().getUnderlyingProperties().getSubjectProperty().getValue()); |
79 |
} |
87 |
} |
80 |
|
88 |
|
81 |
public void testMetadataComplex() { |
89 |
public void testMetadataComplex() throws IOException { |
82 |
XWPFDocument xml = XWPFTestDataSamples.openSampleDocument("IllustrativeCases.docx"); |
90 |
XWPFDocument xml = XWPFTestDataSamples.openSampleDocument("IllustrativeCases.docx"); |
83 |
assertNotNull(xml.getProperties().getCoreProperties()); |
91 |
assertNotNull(xml.getProperties().getCoreProperties()); |
84 |
assertNotNull(xml.getProperties().getExtendedProperties()); |
92 |
assertNotNull(xml.getProperties().getExtendedProperties()); |
Lines 98-104
Link Here
|
98 |
assertEquals("Apache POI", props.getExtendedProperties().getUnderlyingProperties().getApplication()); |
106 |
assertEquals("Apache POI", props.getExtendedProperties().getUnderlyingProperties().getApplication()); |
99 |
} |
107 |
} |
100 |
|
108 |
|
101 |
public void testAddParagraph(){ |
109 |
public void testAddParagraph() throws IOException{ |
102 |
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx"); |
110 |
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx"); |
103 |
assertEquals(3, doc.getParagraphs().size()); |
111 |
assertEquals(3, doc.getParagraphs().size()); |
104 |
|
112 |
|
Lines 117-140
Link Here
|
117 |
assertSame(cP, doc.getParagraphs().get(0)); |
125 |
assertSame(cP, doc.getParagraphs().get(0)); |
118 |
assertEquals(5, doc.getParagraphs().size()); |
126 |
assertEquals(5, doc.getParagraphs().size()); |
119 |
} |
127 |
} |
120 |
|
128 |
|
121 |
public void testAddPicture(){ |
129 |
public void testAddPicture() throws IOException, InvalidFormatException |
122 |
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx"); |
130 |
{ |
123 |
byte[] jpeg = "This is a jpeg".getBytes(); |
131 |
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx"); |
124 |
try { |
132 |
byte[] jpeg = XWPFTestDataSamples.getImage("nature1.jpg"); |
125 |
int jpegNum = doc.addPicture(jpeg, XWPFDocument.PICTURE_TYPE_JPEG); |
133 |
String relationId = doc.addPictureData(jpeg,XWPFDocument.PICTURE_TYPE_JPEG); |
126 |
byte[] newJpeg = doc.getAllPictures().get(jpegNum).getData(); |
134 |
|
127 |
assertEquals(newJpeg.length, jpeg.length); |
135 |
byte[] newJpeg = ((XWPFPictureData) doc.getRelationById(relationId)).getData(); |
128 |
for(int i = 0 ; i < jpeg.length; i++){ |
136 |
assertEquals(newJpeg.length,jpeg.length); |
129 |
assertEquals(newJpeg[i], jpeg[i]); |
137 |
for (int i = 0 ; i < jpeg.length ; i++) |
130 |
} |
138 |
{ |
131 |
} catch (InvalidFormatException e) { |
139 |
assertEquals(newJpeg[i],jpeg[i]); |
132 |
// TODO Auto-generated catch block |
140 |
} |
133 |
e.printStackTrace(); |
141 |
} |
134 |
} |
142 |
|
135 |
} |
143 |
public void testRemoveBodyElement() throws IOException { |
136 |
|
|
|
137 |
public void testRemoveBodyElement() { |
138 |
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx"); |
144 |
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx"); |
139 |
assertEquals(3, doc.getParagraphs().size()); |
145 |
assertEquals(3, doc.getParagraphs().size()); |
140 |
assertEquals(3, doc.getBodyElements().size()); |
146 |
assertEquals(3, doc.getBodyElements().size()); |
Lines 194-197
Link Here
|
194 |
assertEquals(p3, doc.getBodyElements().get(0)); |
200 |
assertEquals(p3, doc.getBodyElements().get(0)); |
195 |
assertEquals(p3, doc.getParagraphs().get(0)); |
201 |
assertEquals(p3, doc.getParagraphs().get(0)); |
196 |
} |
202 |
} |
|
|
203 |
|
204 |
public void testRegisterPackagePictureData() throws IOException, InvalidFormatException { |
205 |
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("issue_51265_1.docx"); |
206 |
|
207 |
/* manually assemble a new image package part*/ |
208 |
OPCPackage opcPckg = doc.getPackage(); |
209 |
XWPFRelation jpgRelation = XWPFRelation.IMAGE_JPEG; |
210 |
PackagePartName partName = PackagingURIHelper.createPartName(jpgRelation.getDefaultFileName().replace('#', '2')); |
211 |
PackagePart newImagePart = opcPckg.createPart(partName, jpgRelation.getContentType()); |
212 |
byte[] nature1 = XWPFTestDataSamples.getImage("abstract4.jpg"); |
213 |
OutputStream os = newImagePart.getOutputStream(); |
214 |
os.write(nature1); |
215 |
os.close(); |
216 |
XWPFHeader xwpfHeader = doc.getHeaderList().get(0); |
217 |
PackageRelationship relationship = xwpfHeader.getPackagePart().addRelationship(partName, TargetMode.INTERNAL, jpgRelation.getRelation()); |
218 |
XWPFPictureData newPicData = new XWPFPictureData(newImagePart,relationship); |
219 |
/* new part is now ready to rumble */ |
220 |
|
221 |
assertFalse(xwpfHeader.getAllPictures().contains(newPicData)); |
222 |
assertFalse(doc.getAllPictures().contains(newPicData)); |
223 |
assertFalse(doc.getAllPackagePictures().contains(newPicData)); |
224 |
|
225 |
doc.registerPackagePictureData(newPicData); |
226 |
|
227 |
assertFalse(xwpfHeader.getAllPictures().contains(newPicData)); |
228 |
assertFalse(doc.getAllPictures().contains(newPicData)); |
229 |
assertTrue(doc.getAllPackagePictures().contains(newPicData)); |
230 |
|
231 |
doc.getPackage().revert(); |
232 |
} |
233 |
|
234 |
public void testFindPackagePictureData() throws IOException { |
235 |
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("issue_51265_1.docx"); |
236 |
byte[] nature1 = XWPFTestDataSamples.getImage("nature1.gif"); |
237 |
XWPFPictureData part = doc.findPackagePictureData(nature1, Document.PICTURE_TYPE_GIF); |
238 |
assertNotNull(part); |
239 |
assertTrue(doc.getAllPictures().contains(part)); |
240 |
assertTrue(doc.getAllPackagePictures().contains(part)); |
241 |
doc.getPackage().revert(); |
242 |
} |
243 |
|
244 |
public void testGetAllPictures() throws IOException { |
245 |
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("issue_51265_3.docx"); |
246 |
List<XWPFPictureData> allPictures = doc.getAllPictures(); |
247 |
List<XWPFPictureData> allPackagePictures = doc.getAllPackagePictures(); |
248 |
|
249 |
assertNotNull(allPictures); |
250 |
assertEquals(3,allPictures.size()); |
251 |
for (XWPFPictureData xwpfPictureData : allPictures) { |
252 |
assertTrue(allPackagePictures.contains(xwpfPictureData)); |
253 |
} |
254 |
|
255 |
try { |
256 |
allPictures.add(allPictures.get(0)); |
257 |
fail("This list must be unmodifiable!"); |
258 |
} catch (UnsupportedOperationException e) { |
259 |
// all ok |
260 |
} |
261 |
|
262 |
doc.getPackage().revert(); |
263 |
} |
264 |
|
265 |
public void testGetAllPackagePictures() throws IOException { |
266 |
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("issue_51265_3.docx"); |
267 |
List<XWPFPictureData> allPackagePictures = doc.getAllPackagePictures(); |
268 |
|
269 |
assertNotNull(allPackagePictures); |
270 |
assertEquals(5,allPackagePictures.size()); |
271 |
|
272 |
try { |
273 |
allPackagePictures.add(allPackagePictures.get(0)); |
274 |
fail("This list must be unmodifiable!"); |
275 |
} catch (UnsupportedOperationException e) { |
276 |
// all ok |
277 |
} |
278 |
|
279 |
doc.getPackage().revert(); |
280 |
} |
281 |
|
282 |
public void testPictureHandlingSimpleFile() throws IOException, InvalidFormatException { |
283 |
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("issue_51265_1.docx"); |
284 |
assertEquals(1,doc.getAllPackagePictures().size()); |
285 |
byte[] newPic = XWPFTestDataSamples.getImage("abstract4.jpg"); |
286 |
String id1 = doc.addPictureData(newPic, Document.PICTURE_TYPE_JPEG); |
287 |
assertEquals(2,doc.getAllPackagePictures().size()); |
288 |
/* copy data, to avoid instance-equality */ |
289 |
byte[] newPicCopy = Arrays.copyOf(newPic, newPic.length); |
290 |
String id2 = doc.addPictureData(newPicCopy, Document.PICTURE_TYPE_JPEG); |
291 |
assertEquals(id1,id2); |
292 |
doc.getPackage().revert(); |
293 |
} |
294 |
|
295 |
public void testPictureHandlingHeaderDocumentImages() throws IOException { |
296 |
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("issue_51265_2.docx"); |
297 |
assertEquals(1,doc.getAllPictures().size()); |
298 |
assertEquals(1,doc.getAllPackagePictures().size()); |
299 |
assertEquals(1,doc.getHeaderList().get(0).getAllPictures().size()); |
300 |
doc.getPackage().revert(); |
301 |
} |
302 |
|
303 |
public void testPictureHandlingComplex() throws IOException, InvalidFormatException { |
304 |
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("issue_51265_3.docx"); |
305 |
XWPFHeader xwpfHeader = doc.getHeaderList().get(0); |
306 |
|
307 |
assertEquals(3,doc.getAllPictures().size()); |
308 |
assertEquals(3,xwpfHeader.getAllPictures().size()); |
309 |
assertEquals(5,doc.getAllPackagePictures().size()); |
310 |
|
311 |
byte[] nature1 = XWPFTestDataSamples.getImage("nature1.jpg"); |
312 |
String id = doc.addPictureData(nature1, Document.PICTURE_TYPE_JPEG); |
313 |
POIXMLDocumentPart part1 = xwpfHeader.getRelationById("rId1"); |
314 |
XWPFPictureData part2 = (XWPFPictureData) doc.getRelationById(id); |
315 |
assertSame(part1,part2); |
316 |
|
317 |
doc.getPackage().revert(); |
318 |
} |
197 |
} |
319 |
} |
198 |
+ native |
320 |
+ native |