poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r996927 - in /poi/trunk/src: documentation/content/xdocs/ ooxml/java/org/apache/poi/xwpf/extractor/ ooxml/java/org/apache/poi/xwpf/usermodel/ ooxml/testcases/org/apache/poi/xwpf/usermodel/
Date Tue, 14 Sep 2010 14:52:36 GMT
Author: nick
Date: Tue Sep 14 14:52:35 2010
New Revision: 996927

URL: http://svn.apache.org/viewvc?rev=996927&view=rev
Log:
Link XWPFPicture to XWPFRun, so that embedded pictures can be access from where they live
in the text stream

Modified:
    poi/trunk/src/documentation/content/xdocs/status.xml
    poi/trunk/src/ooxml/java/org/apache/poi/xwpf/extractor/XWPFWordExtractor.java
    poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/IBodyElement.java
    poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFFactory.java
    poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java
    poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFPicture.java
    poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFParagraph.java

Modified: poi/trunk/src/documentation/content/xdocs/status.xml
URL: http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/status.xml?rev=996927&r1=996926&r2=996927&view=diff
==============================================================================
--- poi/trunk/src/documentation/content/xdocs/status.xml (original)
+++ poi/trunk/src/documentation/content/xdocs/status.xml Tue Sep 14 14:52:35 2010
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.7-beta3" date="2010-??-??">
+           <action dev="poi-developers" type="add">Link XWPFPicture to XWPFRun, so
that embedded pictures can be access from where they live in the text stream</action>
            <action dev="poi-developers" type="fix">Improve handling of Hyperlinks inside
XWPFParagraph objects through XWPFHyperlinkRun</action>
            <action dev="poi-developers" type="fix">Make XWPFParagraph make more use
of XWPFRun, and less on internal StringBuffers</action>
            <action dev="poi-developers" type="add">Add a getBodyElements() method to
XWPF IBody, to make access to embedded paragraphs and tables easier</action>

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/extractor/XWPFWordExtractor.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/extractor/XWPFWordExtractor.java?rev=996927&r1=996926&r2=996927&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/extractor/XWPFWordExtractor.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/extractor/XWPFWordExtractor.java Tue Sep
14 14:52:35 2010
@@ -120,14 +120,11 @@ public class XWPFWordExtractor extends P
 				XWPFCommentsDecorator decorator = new XWPFCommentsDecorator(paragraph, null);
 				text.append(decorator.getCommentText()).append('\n');
 				
-				// Do endnotes, footnotes and pictures
-				for(String str : new String[] {
-				      paragraph.getFootnoteText(), paragraph.getPictureText()
-				}) {
-				   if(str != null && str.length() > 0) {
-				      text.append(str + "\n");
-				   }
-				}
+				// Do endnotes and footnotes
+				String footnameText = paragraph.getFootnoteText();
+			   if(footnameText != null && footnameText.length() > 0) {
+			      text.append(footnameText + "\n");
+			   }
 
 				if (ctSectPr!=null) {
 					extractFooters(text, headerFooterPolicy);

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/IBodyElement.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/IBodyElement.java?rev=996927&r1=996926&r2=996927&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/IBodyElement.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/IBodyElement.java Tue Sep 14 14:52:35
2010
@@ -17,7 +17,6 @@
 
 package org.apache.poi.xwpf.usermodel;
 
-import org.apache.poi.POIXMLDocumentPart;
 
 /**
  * 9 Jan 2010

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFFactory.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFFactory.java?rev=996927&r1=996926&r2=996927&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFFactory.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFFactory.java Tue Sep 14 14:52:35
2010
@@ -17,16 +17,16 @@
 
 package org.apache.poi.xwpf.usermodel;
 
-import org.apache.poi.POIXMLFactory;
+import java.lang.reflect.Constructor;
+
 import org.apache.poi.POIXMLDocumentPart;
 import org.apache.poi.POIXMLException;
+import org.apache.poi.POIXMLFactory;
 import org.apache.poi.POIXMLRelation;
-import org.apache.poi.util.POILogger;
-import org.apache.poi.util.POILogFactory;
-import org.apache.poi.openxml4j.opc.PackageRelationship;
 import org.apache.poi.openxml4j.opc.PackagePart;
-
-import java.lang.reflect.Constructor;
+import org.apache.poi.openxml4j.opc.PackageRelationship;
+import org.apache.poi.util.POILogFactory;
+import org.apache.poi.util.POILogger;
 
 /**
  * @author Yegor Kozlov

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java?rev=996927&r1=996926&r2=996927&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java Tue Sep 14 14:52:35
2010
@@ -64,10 +64,8 @@ public class XWPFParagraph implements IB
     protected XWPFDocument document;
     protected List<XWPFRun> runs;
     
-    private StringBuffer pictureText = new StringBuffer();
     private StringBuffer footnoteText = new StringBuffer();
 
-
     public XWPFParagraph(CTP prgrph) {
         this(prgrph, null);
     }
@@ -125,6 +123,7 @@ public class XWPFParagraph implements IB
           
           // Check for bits that only apply when
           //  attached to a core document
+          // TODO Make this nicer by tracking the XWPFFootnotes directly
           if(document != null) {
              c = r.newCursor();
              c.selectPath("child::*");
@@ -151,22 +150,6 @@ public class XWPFParagraph implements IB
                 }
              }
           }
-
-          // Loop over pictures inside our
-          // paragraph, looking for text in them
-          for(CTPicture pict : r.getPictList()) {
-              XmlObject[] t = pict
-                      .selectPath("declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'
.//w:t");
-              for (int m = 0; m < t.length; m++) {
-                  NodeList kids = t[m].getDomNode().getChildNodes();
-                  for (int n = 0; n < kids.getLength(); n++) {
-                      if (kids.item(n) instanceof Text) {
-                          pictureText.append("\n");
-                          pictureText.append(kids.item(n).getNodeValue());
-                      }
-                  }
-              }
-          }
       }
     }
 
@@ -196,7 +179,7 @@ public class XWPFParagraph implements IB
         for(XWPFRun run : runs) {
            out.append(run.toString());
         }
-        out.append(footnoteText).append(pictureText);
+        out.append(footnoteText);
         return out.toString();
     }
 	
@@ -261,7 +244,11 @@ public class XWPFParagraph implements IB
      * Returns any text from any suitable pictures in the paragraph
      */
     public String getPictureText() {
-        return pictureText.toString();
+       StringBuffer out = new StringBuffer();
+       for(XWPFRun run : runs) {
+          out.append(run.getPictureText());
+       }
+       return out.toString();
     }
 
     /**

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFPicture.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFPicture.java?rev=996927&r1=996926&r2=996927&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFPicture.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFPicture.java Tue Sep 14 14:52:35
2010
@@ -25,14 +25,13 @@ import org.openxmlformats.schemas.drawin
 
 /**
  * @author Philipp Epp
- *
  */
 public class XWPFPicture {
-	private static final POILogger logger = POILogFactory.getLogger(XWPFPicture.class); 
+	private static final POILogger logger = POILogFactory.getLogger(XWPFPicture.class);
+	
 	protected XWPFParagraph paragraph;
 	private CTPicture ctPic;
 	 
-	 
 	 public XWPFParagraph getParagraph(){
 		 return paragraph;
 	 }
@@ -41,6 +40,7 @@ public class XWPFPicture {
 		 this.paragraph = paragraph;
 		 this.ctPic = ctPic;
 	 }
+	 
 	 /**
 	  * Link Picture with PictureData
 	  * @param rel
@@ -57,9 +57,10 @@ public class XWPFPicture {
     public CTPicture getCTPicture(){
         return ctPic;
     }
+    
     /**
-     * Get the PictureData of the Picture
-     * @return
+     * Get the PictureData of the Picture, if present.
+     * Note - not all kinds of picture have data
      */
     public XWPFPictureData getPictureData(){
     	String blipId = ctPic.getBlipFill().getBlip().getEmbed();
@@ -71,4 +72,4 @@ public class XWPFPicture {
     	return null;
     }
     
-}//end class
+}

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java?rev=996927&r1=996926&r2=996927&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java Tue Sep 14 14:52:35
2010
@@ -17,11 +17,15 @@
 package org.apache.poi.xwpf.usermodel;
 
 import java.math.BigInteger;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.xml.namespace.QName;
 
 import org.apache.poi.util.Internal;
+import org.apache.xmlbeans.XmlCursor;
 import org.apache.xmlbeans.XmlObject;
 import org.apache.xmlbeans.XmlString;
-import org.apache.xmlbeans.XmlCursor;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBr;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTEmpty;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts;
@@ -39,8 +43,8 @@ import org.openxmlformats.schemas.wordpr
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.STUnderline;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalAlignRun;
-
-import javax.xml.namespace.QName;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Text;
 
 /**
  * XWPFRun object defines a region of text with a common set of properties
@@ -49,7 +53,9 @@ import javax.xml.namespace.QName;
  */
 public class XWPFRun {
     private CTR run;
+    private String pictureText;
     private XWPFParagraph paragraph;
+    private List<XWPFPicture> pictures;
 
     /**
      * @param r the CTR bean which holds the run attributes
@@ -58,6 +64,42 @@ public class XWPFRun {
     public XWPFRun(CTR r, XWPFParagraph p) {
         this.run = r;
         this.paragraph = p;
+        
+        // Look for any text in any of our pictures or drawings
+        StringBuffer text = new StringBuffer();
+        List<XmlObject> pictTextObjs = new ArrayList<XmlObject>();
+        pictTextObjs.addAll(r.getPictList());
+        pictTextObjs.addAll(r.getDrawingList());
+        for(XmlObject o : pictTextObjs) {
+           XmlObject[] t = o
+                 .selectPath("declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'
.//w:t");
+           for (int m = 0; m < t.length; m++) {
+              NodeList kids = t[m].getDomNode().getChildNodes();
+              for (int n = 0; n < kids.getLength(); n++) {
+                 if (kids.item(n) instanceof Text) {
+                    if(text.length() > 0)
+                       text.append("\n");
+                    text.append(kids.item(n).getNodeValue());
+                 }
+              }
+           }
+        }
+        pictureText = text.toString();
+        
+        // Do we have any embedded pictures?
+        // (They're a different CTPicture, under the drawingml namespace)
+        pictures = new ArrayList<XWPFPicture>();
+        for(XmlObject o : pictTextObjs) {
+           XmlObject[] picts = o
+                 .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(
+                       (org.openxmlformats.schemas.drawingml.x2006.picture.CTPicture)pict,
p
+                 ));
+              }
+           }
+        }
     }
 
     /**
@@ -127,6 +169,13 @@ public class XWPFRun {
         return run.sizeOfTArray() == 0 ? null : run.getTArray(pos)
                 .getStringValue();
     }
+    
+    /**
+     * Returns text embedded in pictures
+     */
+    public String getPictureText() {
+        return pictureText;
+    }
 
     /**
      * Sets the text of this text run
@@ -134,7 +183,7 @@ public class XWPFRun {
      * @param value the literal text which shall be displayed in the document
      */
     public void setText(String value) {
-	setText(value,run.getTList().size());
+       setText(value,run.getTList().size());
     }
 
     /**
@@ -479,6 +528,15 @@ public class XWPFRun {
     public void removeCarriageReturn() {
 	//TODO
     }    
+    
+    /**
+     * Returns the embedded pictures of the run. These
+     *  are pictures which reference an external, 
+     *  embedded picture image such as a .png or .jpg
+     */
+    public List<XWPFPicture> getEmbeddedPictures() {
+       return pictures;
+    }
 
     /**
      * Add the xml:spaces="preserve" attribute if the string has leading or trailing white
spaces
@@ -534,6 +592,11 @@ public class XWPFRun {
            }
        }
        
+       // Any picture text?
+       if(pictureText != null && pictureText.length() > 0) {
+          text.append("\n").append(pictureText);
+       }
+       
        return text.toString();
     }
 }

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFParagraph.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFParagraph.java?rev=996927&r1=996926&r2=996927&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFParagraph.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFParagraph.java Tue
Sep 14 14:52:35 2010
@@ -249,4 +249,7 @@ public final class TestXWPFParagraph ext
         assertEquals("10", p.getNumID().toString());
     }
     
+    public void testPictures() throws Exception {
+       // TODO
+    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message