]> source.dussan.org Git - poi.git/commitdiff
Various XWPF picture tweaks and more unit tests
authorNick Burch <nick@apache.org>
Tue, 14 Sep 2010 18:50:10 +0000 (18:50 +0000)
committerNick Burch <nick@apache.org>
Tue, 14 Sep 2010 18:50:10 +0000 (18:50 +0000)
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

src/ooxml/java/org/apache/poi/util/OOXMLLite.java
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFPictureData.java
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java
src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFParagraph.java

index b0e7b26dc45be108f64f120c46d8a06874e87209..cb0a755ee378e9141cdfb0e4063d287edf4378ab 100644 (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();
index 5094763f7316a86db36c1fe6ca6494dfe9f86c47..ecae82b3fa851734667c9f8699157a2cf20fbfff 100644 (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;
     }
-    
-    
 }
index a72a0c76028b0bb29005c5b9d27c39dd067434fd..4f9a68b2b4aa22d9ea69bcffc73dfafacd7cf3b4 100644 (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);
               }
            }
         }
index 5abc6a20055cb5d89139d814ccb0a91eb4bc731f..6bf3c03ff0a5d92da088efae2e5ea4fff4a389ce 100644 (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);
     }
 }