Various XWPF picture tweaks and more unit tests

Note that XWPFRun -> XWPFPicture currently only works with the full ooxml schemas file, and not with poi-ooxml-schemas, due to some sort of missing resources around the namespace switch


git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@997037 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2010-09-14 18:50:10 +00:00
parent 3f972827af
commit 58d1404684
4 changed files with 91 additions and 9 deletions

View File

@ -129,10 +129,9 @@ public final class OOXMLLite {
JarFile jar = new JarFile(_ooxmlJar);
for(Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements(); ){
JarEntry je = e.nextElement();
if(je.getName().matches("schemaorg_apache_xmlbeans/system/\\w+/\\w+\\.xsb")){
File destFile = new File(_destDest, je.getName());
//System.out.println(je.getName() + " --> " + destFile);
copyFile(jar.getInputStream(je), destFile);
if(je.getName().matches("schemaorg_apache_xmlbeans/system/\\w+/\\w+\\.xsb")) {
File destFile = new File(_destDest, je.getName());
copyFile(jar.getInputStream(je), destFile);
}
}
jar.close();

View File

@ -92,6 +92,18 @@ public class XWPFPictureData extends POIXMLDocumentPart {
return null;
}
/**
* 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();
if(name == null)
return null;
return name.substring(name.lastIndexOf('/') + 1);
}
/**
* Suggests a file extension for this image.
*
@ -123,6 +135,4 @@ public class XWPFPictureData extends POIXMLDocumentPart {
}
return 0;
}
}

View File

@ -26,6 +26,7 @@ import org.apache.poi.util.Internal;
import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlString;
import org.openxmlformats.schemas.drawingml.x2006.picture.CTPicture;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTEmpty;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts;
@ -94,9 +95,10 @@ public class XWPFRun {
.selectPath("declare namespace pic='http://schemas.openxmlformats.org/drawingml/2006/picture' .//pic:pic");
for(XmlObject pict : picts) {
if(pict instanceof org.openxmlformats.schemas.drawingml.x2006.picture.CTPicture) {
pictures.add(new XWPFPicture(
XWPFPicture picture = new XWPFPicture(
(org.openxmlformats.schemas.drawingml.x2006.picture.CTPicture)pict, p
));
);
pictures.add(picture);
}
}
}

View File

@ -38,6 +38,9 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STLineSpacingRule;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTextAlignment;
import org.openxmlformats.schemas.drawingml.x2006.picture.CTPicture;
import org.openxmlformats.schemas.drawingml.x2006.picture.PicDocument;
import org.openxmlformats.schemas.drawingml.x2006.picture.impl.PicDocumentImpl;
/**
* Tests for XWPF Paragraphs
@ -250,6 +253,74 @@ public final class TestXWPFParagraph extends TestCase {
}
public void testPictures() throws Exception {
// TODO
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("VariousPictures.docx");
assertEquals(7, doc.getParagraphs().size());
XWPFParagraph p;
XWPFRun r;
// Text paragraphs
assertEquals("Sheet with various pictures", doc.getParagraphs().get(0).getText());
assertEquals("(jpeg, png, wmf, emf and pict) ", doc.getParagraphs().get(1).getText());
// Spacer ones
assertEquals("", doc.getParagraphs().get(2).getText());
assertEquals("", doc.getParagraphs().get(3).getText());
assertEquals("", doc.getParagraphs().get(4).getText());
// Image one
p = doc.getParagraphs().get(5);
assertEquals(6, p.getRuns().size());
r = p.getRuns().get(0);
assertEquals("", r.toString());
assertEquals(1, r.getEmbeddedPictures().size());
assertNotNull(r.getEmbeddedPictures().get(0).getPictureData());
assertEquals("image1.wmf", r.getEmbeddedPictures().get(0).getPictureData().getFileName());
r = p.getRuns().get(1);
assertEquals("", r.toString());
assertEquals(1, r.getEmbeddedPictures().size());
assertNotNull(r.getEmbeddedPictures().get(0).getPictureData());
assertEquals("image2.png", r.getEmbeddedPictures().get(0).getPictureData().getFileName());
r = p.getRuns().get(2);
assertEquals("", r.toString());
assertEquals(1, r.getEmbeddedPictures().size());
assertNotNull(r.getEmbeddedPictures().get(0).getPictureData());
assertEquals("image3.emf", r.getEmbeddedPictures().get(0).getPictureData().getFileName());
r = p.getRuns().get(3);
assertEquals("", r.toString());
assertEquals(1, r.getEmbeddedPictures().size());
assertNotNull(r.getEmbeddedPictures().get(0).getPictureData());
assertEquals("image4.emf", r.getEmbeddedPictures().get(0).getPictureData().getFileName());
r = p.getRuns().get(4);
assertEquals("", r.toString());
assertEquals(1, r.getEmbeddedPictures().size());
assertNotNull(r.getEmbeddedPictures().get(0).getPictureData());
assertEquals("image5.jpeg", r.getEmbeddedPictures().get(0).getPictureData().getFileName());
r = p.getRuns().get(5);
assertEquals(" ", r.toString());
assertEquals(0, r.getEmbeddedPictures().size());
// Final spacer
assertEquals("", doc.getParagraphs().get(6).getText());
// Look in detail at one
r = p.getRuns().get(4);
XWPFPicture pict = r.getEmbeddedPictures().get(0);
CTPicture picture = pict.getCTPicture();
assertEquals("rId8", picture.getBlipFill().getBlip().getEmbed());
// Ensure that the ooxml compiler finds everything we need
r.getCTR().getDrawingArray(0);
r.getCTR().getDrawingArray(0).getInlineArray(0);
r.getCTR().getDrawingArray(0).getInlineArray(0).getGraphic();
r.getCTR().getDrawingArray(0).getInlineArray(0).getGraphic().getGraphicData();
PicDocument pd = new PicDocumentImpl(null);
}
}