poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ye...@apache.org
Subject svn commit: r1130120 [3/4] - in /poi/trunk: src/documentation/content/xdocs/ src/java/org/apache/poi/util/ src/ooxml/java/org/apache/poi/ src/ooxml/java/org/apache/poi/openxml4j/opc/ src/ooxml/java/org/apache/poi/openxml4j/opc/internal/ src/ooxml/java/...
Date Wed, 01 Jun 2011 12:07:18 GMT
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=1130120&r1=1130119&r2=1130120&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 Wed Jun  1 12:07:16 2011
@@ -21,6 +21,7 @@ import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 
+import org.apache.poi.POIXMLDocumentPart;
 import org.apache.poi.util.Internal;
 import org.apache.xmlbeans.XmlCursor;
 import org.apache.xmlbeans.XmlObject;
@@ -50,17 +51,16 @@ import org.openxmlformats.schemas.wordpr
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTextAlignment;
 
-
 /**
  * Sketch of XWPF paragraph class
  */
-public class XWPFParagraph implements IBodyElement{
+public class XWPFParagraph implements IBodyElement {
     private final CTP paragraph;
     protected IBody part;
     /** For access to the document's hyperlink, comments, tables etc */
     protected XWPFDocument document;
     protected List<XWPFRun> runs;
-    
+
     private StringBuffer footnoteText = new StringBuffer();
 
     public XWPFParagraph(CTP prgrph, IBody part) {
@@ -260,9 +260,9 @@ public class XWPFParagraph implements IB
      * @return a new text run
      */
     public XWPFRun createRun() {
-       XWPFRun run = new XWPFRun(paragraph.addNewR(), this);
-       runs.add(run);
-       return run;
+        XWPFRun xwpfRun = new XWPFRun(paragraph.addNewR(), this);
+        runs.add(xwpfRun);
+        return xwpfRun;
     }
 
     /**
@@ -1233,53 +1233,65 @@ public class XWPFParagraph implements IB
     	 return false;
     }
 
-	/**
-	 * returns the type of the BodyElement Paragraph
-	 * @see org.apache.poi.xwpf.usermodel.IBodyElement#getElementType()
-	 */
-	public BodyElementType getElementType() {
-		return BodyElementType.PARAGRAPH;
-	}
-
-	/**
-	 * returns the part of the bodyElement
-	 * @see org.apache.poi.xwpf.usermodel.IBody#getPart()
-	 */
-	public IBody getPart() {
-		if(part != null){
-			return part.getPart();
-		}
-		return null;
-	}
+    /**
+     * returns the type of the BodyElement Paragraph
+     * @see org.apache.poi.xwpf.usermodel.IBodyElement#getElementType()
+     */
+    public BodyElementType getElementType() {
+        return BodyElementType.PARAGRAPH;
+    }
 
-	/**
-	 * returns the partType of the bodyPart which owns the bodyElement
-	 * @see org.apache.poi.xwpf.usermodel.IBody#getPartType()
-	 */
-	public BodyType getPartType() {
-		return part.getPartType();
-	}
-	
-	/**
-	 * adds a new Run to the Paragraph
-	 */
-	public void addRun(XWPFRun r){
-		runs.add(r);
-	}
-	
-	/**
-	 * return the XWPFRun-Element which owns the CTR run-Element
-	 */
-	public XWPFRun getRun(CTR r){
-		for(int i=0; i < getRuns().size(); i++){
-			if(getRuns().get(i).getCTR() == r) return getRuns().get(i); 
-		}
-		return null;
-	}
-	
+    @Override
+    public IBody getBody()
+    {
+        return part;
+    }
 
+    /**
+     * returns the part of the bodyElement
+     * @see org.apache.poi.xwpf.usermodel.IBody#getPart()
+     */
+    public POIXMLDocumentPart getPart() {
+        if(part != null){
+            return part.getPart();
+        }
+        return null;
+    }
 
-}//end class
+    /**
+     * returns the partType of the bodyPart which owns the bodyElement
+     * 
+     * @see org.apache.poi.xwpf.usermodel.IBody#getPartType()
+     */
+    public BodyType getPartType() {
+        return part.getPartType();
+    }
 
+    /**
+     * adds a new Run to the Paragraph
+     * 
+     * @param r
+     * @return
+     */
+    public void addRun(XWPFRun r) {
+        if (!runs.contains(r)) {
+            runs.add(r);
+        }
+    }
 
+    /**
+     * return the XWPFRun-Element which owns the CTR run-Element
+     * 
+     * @param r
+     * @return
+     */
+    public XWPFRun getRun(CTR r) {
+        for (int i = 0; i < getRuns().size(); i++) {
+            if (getRuns().get(i).getCTR() == r) {
+                return getRuns().get(i);
+            }
+        }
+        return null;
+    }
 
+}//end class
\ No newline at end of file

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=1130120&r1=1130119&r2=1130120&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 Wed Jun  1 12:07:16 2011
@@ -25,28 +25,25 @@ import org.openxmlformats.schemas.drawin
  * @author Philipp Epp
  */
 public class XWPFPicture {
-	protected XWPFParagraph paragraph;
-	private CTPicture ctPic;
+
+    private CTPicture ctPic;
     private String description;
+    private XWPFRun run;
+
+    public XWPFPicture(CTPicture ctPic, XWPFRun run){
+        this.run = run;
+        this.ctPic = ctPic;
+        description = ctPic.getNvPicPr().getCNvPr().getDescr();
+    }
+
+    /**
+     * Link Picture with PictureData
+     * @param rel
+     */
+    public void setPictureReference(PackageRelationship rel){
+        ctPic.getBlipFill().getBlip().setEmbed(rel.getId());
+    }
 
-    public XWPFParagraph getParagraph(){
-		 return paragraph;
-	 }
-	 
-	 public XWPFPicture(CTPicture ctPic, XWPFParagraph paragraph){
-		 this.paragraph = paragraph;
-		 this.ctPic = ctPic;
-         description = ctPic.getNvPicPr().getCNvPr().getDescr();
-     }
-	 
-	 /**
-	  * Link Picture with PictureData
-	  * @param rel
-	  */
-	 public void setPictureReference(PackageRelationship rel){
-		 ctPic.getBlipFill().getBlip().setEmbed(rel.getId());
-	 }
-	 
     /**
      * Return the underlying CTPicture bean that holds all properties for this picture
      *
@@ -55,19 +52,22 @@ public class XWPFPicture {
     public CTPicture getCTPicture(){
         return ctPic;
     }
-    
+
     /**
      * 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();
-        for(POIXMLDocumentPart part: ((POIXMLDocumentPart) paragraph.getPart()).getRelations()){
-    		  if(part.getPackageRelationship().getId().equals(blipId)){
-    			  return (XWPFPictureData)part;
-    		  }
-    	}
-    	return null;
+        String blipId = ctPic.getBlipFill().getBlip().getEmbed();
+        POIXMLDocumentPart part = run.getParagraph().getPart();
+        if (part != null)
+        {
+            POIXMLDocumentPart relatedPart = part.getRelationById(blipId);
+            if (relatedPart instanceof XWPFPictureData) {
+                return (XWPFPictureData) relatedPart;
+            }
+        }
+        return null;
     }
 
     public String getDescription() {

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFPictureData.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFPictureData.java?rev=1130120&r1=1130119&r2=1130120&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFPictureData.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFPictureData.java Wed Jun  1 12:07:16 2011
@@ -18,9 +18,13 @@
 package org.apache.poi.xwpf.usermodel;
 
 import java.io.IOException;
+import java.io.InputStream;
+import java.util.Arrays;
 
 import org.apache.poi.POIXMLDocumentPart;
+import org.apache.poi.POIXMLException;
 import org.apache.poi.POIXMLRelation;
+import org.apache.poi.openxml4j.opc.OPCPackage;
 import org.apache.poi.openxml4j.opc.PackagePart;
 import org.apache.poi.openxml4j.opc.PackageRelationship;
 import org.apache.poi.util.IOUtils;
@@ -32,29 +36,31 @@ import org.apache.poi.util.IOUtils;
 
 /**
  * @author Philipp Epp
- *
  */
 public class XWPFPictureData extends POIXMLDocumentPart {
-	
+
     /**
      * Relationships for each known picture type
      */
     protected static final POIXMLRelation[] RELATIONS;
     static {
         RELATIONS = new POIXMLRelation[9];
-        RELATIONS[Document.PICTURE_TYPE_EMF] =  XWPFRelation.IMAGE_EMF;
-        RELATIONS[Document.PICTURE_TYPE_WMF] =  XWPFRelation.IMAGE_WMF;
+        RELATIONS[Document.PICTURE_TYPE_EMF] = XWPFRelation.IMAGE_EMF;
+        RELATIONS[Document.PICTURE_TYPE_WMF] = XWPFRelation.IMAGE_WMF;
         RELATIONS[Document.PICTURE_TYPE_PICT] = XWPFRelation.IMAGE_PICT;
         RELATIONS[Document.PICTURE_TYPE_JPEG] = XWPFRelation.IMAGE_JPEG;
-        RELATIONS[Document.PICTURE_TYPE_PNG] =  XWPFRelation.IMAGE_PNG;
-        RELATIONS[Document.PICTURE_TYPE_DIB] =  XWPFRelation.IMAGE_DIB;
-        RELATIONS[Document.PICTURE_TYPE_GIF] =  XWPFRelation.IMAGE_GIF;
+        RELATIONS[Document.PICTURE_TYPE_PNG] = XWPFRelation.IMAGE_PNG;
+        RELATIONS[Document.PICTURE_TYPE_DIB] = XWPFRelation.IMAGE_DIB;
+        RELATIONS[Document.PICTURE_TYPE_GIF] = XWPFRelation.IMAGE_GIF;
     }
+
+    private Long checksum = null;
+
     /**
      * Create a new XWPFGraphicData node
      *
      */
-	protected XWPFPictureData() {
+    protected XWPFPictureData() {
         super();
     }
 
@@ -65,11 +71,15 @@ public class XWPFPictureData extends POI
      * @param rel  the package relationship holding this drawing,
      * the relationship type must be http://schemas.openxmlformats.org/officeDocument/2006/relationships/image
      */
-	
-	public XWPFPictureData(PackagePart part, PackageRelationship rel) {
+    public XWPFPictureData(PackagePart part, PackageRelationship rel) {
         super(part, rel);
     }
-    
+
+    @Override
+    protected void onDocumentRead() throws IOException {
+        super.onDocumentRead();
+    }
+
     /**
      * Gets the picture data as a byte array.
      * <p>
@@ -80,44 +90,40 @@ public class XWPFPictureData extends POI
      * InputStream is = getPackagePart().getInputStream();
      * </code>
      * </p>
-     *
      * @return the Picture data.
      */
     public byte[] getData() {
-          try {
-			return IOUtils.toByteArray(getPackagePart().getInputStream());
-		} catch (IOException e) {
-			// TODO Auto-generated catch block
-			e.printStackTrace();
-		}
-		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 
+        try {
+            return IOUtils.toByteArray(getPackagePart().getInputStream());
+        } catch (IOException e) {
+            throw new POIXMLException(e);
+        }
+    }
+
+    /**
+     * 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);
+        String name = getPackagePart().getPartName().getName();
+        if (name == null)
+            return null;
+        return name.substring(name.lastIndexOf('/') + 1);
     }
-    
+
     /**
      * Suggests a file extension for this image.
-     *
      * @return the file extension.
      */
     public String suggestFileExtension() {
         return getPackagePart().getPartName().getExtension();
     }
-    
+
     /**
      * Return an integer constant that specifies type of this picture
-     *
-     * @return an integer constant that specifies type of this picture 
+     * 
+     * @return an integer constant that specifies type of this picture
      * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_EMF
      * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_WMF
      * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_PICT
@@ -125,15 +131,103 @@ public class XWPFPictureData extends POI
      * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_PNG
      * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_DIB
      */
-    public int getPictureType(){
+    public int getPictureType() {
         String contentType = getPackagePart().getContentType();
         for (int i = 0; i < RELATIONS.length; i++) {
-            if(RELATIONS[i] == null) continue;
+            if (RELATIONS[i] == null) {
+                continue;
+            }
 
-            if(RELATIONS[i].getContentType().equals(contentType)){
+            if (RELATIONS[i].getContentType().equals(contentType)) {
                 return i;
             }
         }
         return 0;
     }
+
+    public Long getChecksum() {
+        if (this.checksum == null) {
+            InputStream is = null;
+            byte[] data;
+            try {
+                is = getPackagePart().getInputStream();
+                data = IOUtils.toByteArray(is);
+            } catch (IOException e) {
+                throw new POIXMLException(e);
+            } finally {
+                try {
+                    is.close();
+                } catch (IOException e) {
+                    throw new POIXMLException(e);
+                }
+            }
+            this.checksum = IOUtils.calculateChecksum(data);
+        }
+        return this.checksum;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        /**
+         * In case two objects ARE equal, but its not the same instance, this
+         * implementation will always run through the whole
+         * byte-array-comparison before returning true. If this will turn into a
+         * performance issue, two possible approaches are available:<br>
+         * a) Use the checksum only and take the risk that two images might have
+         * the same CRC32 sum, although they are not the same.<br>
+         * b) Use a second (or third) checksum algorithm to minimise the chance
+         * that two images have the same checksums but are not equal (e.g.
+         * CRC32, MD5 and SHA-1 checksums, additionally compare the
+         * data-byte-array lengths).
+         */
+        if (obj == this) {
+            return true;
+        }
+
+        if (obj == null) {
+            return false;
+        }
+
+        if (!(obj instanceof XWPFPictureData)) {
+            return false;
+        }
+
+        XWPFPictureData picData = (XWPFPictureData) obj;
+        PackagePart foreignPackagePart = picData.getPackagePart();
+        PackagePart ownPackagePart = this.getPackagePart();
+
+        if ((foreignPackagePart != null && ownPackagePart == null)
+                || (foreignPackagePart == null && ownPackagePart != null)) {
+            return false;
+        }
+
+        if (ownPackagePart != null) {
+            OPCPackage foreignPackage = foreignPackagePart.getPackage();
+            OPCPackage ownPackage = ownPackagePart.getPackage();
+
+            if ((foreignPackage != null && ownPackage == null)
+                    || (foreignPackage == null && ownPackage != null)) {
+                return false;
+            }
+            if (ownPackage != null) {
+
+                if (!ownPackage.equals(foreignPackage)) {
+                    return false;
+                }
+            }
+        }
+        
+        Long foreignChecksum = picData.getChecksum();
+        Long localChecksum = getChecksum();
+
+        if (!(localChecksum.equals(foreignChecksum))) {
+            return false;
+        }
+        return Arrays.equals(this.getData(), picData.getData());
+    }
+
+    @Override
+    public int hashCode() {
+        return getChecksum().hashCode();
+    }
 }

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=1130120&r1=1130119&r2=1130120&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 Wed Jun  1 12:07:16 2011
@@ -45,6 +45,7 @@ import org.openxmlformats.schemas.drawin
 import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
 import org.openxmlformats.schemas.drawingml.x2006.main.CTTransform2D;
 import org.openxmlformats.schemas.drawingml.x2006.main.STShapeType;
+import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTAnchor;
 import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBr;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDrawing;
@@ -87,56 +88,75 @@ public class XWPFRun {
     public XWPFRun(CTR r, XWPFParagraph p) {
         this.run = r;
         this.paragraph = p;
-        
+
+        /**
+         * reserve already occupied drawing ids, so reserving new ids later will
+         * not corrupt the document
+         */
+        List<CTDrawing> drawingList = r.getDrawingList();
+        for (CTDrawing ctDrawing : drawingList) {
+            List<CTAnchor> anchorList = ctDrawing.getAnchorList();
+            for (CTAnchor anchor : anchorList) {
+                if (anchor.getDocPr() != null) {
+                    getDocument().getDrawingIdManager().reserve(anchor.getDocPr().getId());
+                }
+            }
+            List<CTInline> inlineList = ctDrawing.getInlineList();
+            for (CTInline inline : inlineList) {
+                if (inline.getDocPr() != null) {
+                    getDocument().getDrawingIdManager().reserve(inline.getDocPr().getId());
+                }
+            }
+        }
+
         // 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());
+        pictTextObjs.addAll(drawingList);
         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());
-                 }
-              }
-           }
+            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) {
-           for(CTPicture pict : getCTPictures(o)) {
-              XWPFPicture picture = new XWPFPicture( pict, p );
-              pictures.add(picture);
-           }
+            for(CTPicture pict : getCTPictures(o)) {
+                XWPFPicture picture = new XWPFPicture(pict, this);
+                pictures.add(picture);
+            }
         }
     }
-    
+
     private List<CTPicture> getCTPictures(XmlObject o) {
-       List<CTPicture> pictures = new ArrayList<CTPicture>(); 
-       XmlObject[] picts = o.selectPath("declare namespace pic='"+CTPicture.type.getName().getNamespaceURI()+"' .//pic:pic");
-       for(XmlObject pict : picts) {
-          if(pict instanceof XmlAnyTypeImpl) {
-             // Pesky XmlBeans bug - see Bugzilla #49934
-             try {
-                pict = CTPicture.Factory.parse( pict.toString() );
-             } catch(XmlException e) {
-                throw new POIXMLException(e);
-             }
-          }
-          if(pict instanceof CTPicture) {
-             pictures.add((CTPicture)pict);
-          }
-       }
-       return pictures;
+        List<CTPicture> pictures = new ArrayList<CTPicture>(); 
+        XmlObject[] picts = o.selectPath("declare namespace pic='"+CTPicture.type.getName().getNamespaceURI()+"' .//pic:pic");
+        for(XmlObject pict : picts) {
+            if(pict instanceof XmlAnyTypeImpl) {
+                // Pesky XmlBeans bug - see Bugzilla #49934
+                try {
+                    pict = CTPicture.Factory.parse( pict.toString() );
+                } catch(XmlException e) {
+                    throw new POIXMLException(e);
+                }
+            }
+            if(pict instanceof CTPicture) {
+                pictures.add((CTPicture)pict);
+            }
+        }
+        return pictures;
     }
 
     /**
@@ -155,18 +175,29 @@ public class XWPFRun {
     public XWPFParagraph getParagraph() {
         return paragraph;
     }
-    
+
+    /**
+     * @return The {@link XWPFDocument} instance, this run belongs to, or
+     *         <code>null</code> if parent structure (paragraph > document) is not properly set.
+     */
+    public XWPFDocument getDocument() {
+        if (paragraph != null) {
+            return paragraph.getDocument();
+        }
+        return null;
+    }
+
     /**
      * For isBold, isItalic etc
      */
     private boolean isCTOnOff(CTOnOff onoff) {
-       if(! onoff.isSetVal())
-          return true;
-       if(onoff.getVal() == STOnOff.ON)
-          return true;
-       if(onoff.getVal() == STOnOff.TRUE)
-          return true;
-       return false;
+        if(! onoff.isSetVal())
+            return true;
+        if(onoff.getVal() == STOnOff.ON)
+            return true;
+        if(onoff.getVal() == STOnOff.TRUE)
+            return true;
+        return false;
     }
 
     /**
@@ -177,8 +208,9 @@ public class XWPFRun {
      */
     public boolean isBold() {
         CTRPr pr = run.getRPr();
-        if(pr == null || !pr.isSetB())
-           return false;
+        if(pr == null || !pr.isSetB()) {
+            return false;
+        }
         return isCTOnOff(pr.getB());
     }
 
@@ -221,7 +253,7 @@ public class XWPFRun {
         return run.sizeOfTArray() == 0 ? null : run.getTArray(pos)
                 .getStringValue();
     }
-    
+
     /**
      * Returns text embedded in pictures
      */
@@ -235,7 +267,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());
     }
 
     /**
@@ -245,13 +277,12 @@ public class XWPFRun {
      * @param pos - position in the text array (NB: 0 based)
      */
     public void setText(String value, int pos) {
-	if(pos > run.sizeOfTArray()) throw new ArrayIndexOutOfBoundsException("Value too large for the parameter position in XWPFRun.setText(String value,int pos)");
+        if(pos > run.sizeOfTArray()) throw new ArrayIndexOutOfBoundsException("Value too large for the parameter position in XWPFRun.setText(String value,int pos)");
         CTText t = (pos < run.sizeOfTArray() && pos >= 0) ? run.getTArray(pos) : run.addNewT();
         t.setStringValue(value);
         preserveSpaces(t);
     }
 
-    
     /**
      * Whether the italic property should be applied to all non-complex script
      * characters in the contents of this run when displayed in a document.
@@ -261,7 +292,7 @@ public class XWPFRun {
     public boolean isItalic() {
         CTRPr pr = run.getRPr();
         if(pr == null || !pr.isSetI())
-           return false;
+            return false;
         return isCTOnOff(pr.getI());
     }
 
@@ -306,7 +337,7 @@ public class XWPFRun {
     public UnderlinePatterns getUnderline() {
         CTRPr pr = run.getRPr();
         return (pr != null && pr.isSetU()) ? UnderlinePatterns.valueOf(pr
-                .getU().getVal().intValue()) : UnderlinePatterns.NONE;
+            .getU().getVal().intValue()) : UnderlinePatterns.NONE;
     }
 
     /**
@@ -339,7 +370,7 @@ public class XWPFRun {
     public boolean isStrike() {
         CTRPr pr = run.getRPr();
         if(pr == null || !pr.isSetStrike())
-           return false;
+            return false;
         return isCTOnOff(pr.getStrike());
     }
 
@@ -384,8 +415,7 @@ public class XWPFRun {
      */
     public VerticalAlign getSubscript() {
         CTRPr pr = run.getRPr();
-        return (pr != null && pr.isSetVertAlign()) ? VerticalAlign.valueOf(pr
-                .getVertAlign().getVal().intValue()) : VerticalAlign.BASELINE;
+        return (pr != null && pr.isSetVertAlign()) ? VerticalAlign.valueOf(pr.getVertAlign().getVal().intValue()) : VerticalAlign.BASELINE;
     }
 
     /**
@@ -460,7 +490,7 @@ public class XWPFRun {
      * @param size
      */
     public void setFontSize(int size) {
-	BigInteger bint=new BigInteger(""+size);
+        BigInteger bint=new BigInteger(""+size);
         CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
         CTHpsMeasure ctSize = pr.isSetSz() ? pr.getSz() : pr.addNewSz();
         ctSize.setVal(bint.multiply(new BigInteger("2")));
@@ -503,7 +533,7 @@ public class XWPFRun {
      * @param val
      */
     public void setTextPosition(int val) {
-	BigInteger bint=new BigInteger(""+val);
+        BigInteger bint=new BigInteger(""+val);
         CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
         CTSignedHpsMeasure position = pr.isSetPosition() ? pr.getPosition() : pr.addNewPosition();
         position.setVal(bint);
@@ -513,7 +543,7 @@ public class XWPFRun {
      * 
      */
     public void removeBreak() {
-	// TODO
+        // TODO
     }
 
     /**
@@ -525,7 +555,7 @@ public class XWPFRun {
      * @see #addCarriageReturn() 
      */
     public void addBreak() {
-	run.addNewBr();
+        run.addNewBr();
     } 
 
     /**
@@ -542,11 +572,10 @@ public class XWPFRun {
      * @see BreakType
      */
     public void addBreak(BreakType type){
-	CTBr br=run.addNewBr();
-	br.setType(STBrType.Enum.forInt(type.getValue()));
+        CTBr br=run.addNewBr();
+        br.setType(STBrType.Enum.forInt(type.getValue()));
     }
 
-    
     /**
      * Specifies that a break shall be placed at the current location in the run
      * content. A break is a special character which is used to override the
@@ -560,9 +589,9 @@ public class XWPFRun {
      * @see BreakClear
      */
     public void addBreak(BreakClear clear){
-	CTBr br=run.addNewBr();
-	br.setType(STBrType.Enum.forInt(BreakType.TEXT_WRAPPING.getValue()));
-	    br.setClear(STBrClear.Enum.forInt(clear.getValue()));
+        CTBr br=run.addNewBr();
+        br.setType(STBrType.Enum.forInt(BreakType.TEXT_WRAPPING.getValue()));
+        br.setClear(STBrClear.Enum.forInt(clear.getValue()));
     }
 
     /**
@@ -578,13 +607,13 @@ public class XWPFRun {
      * restarted on the next available line in the document.
      */
     public void addCarriageReturn() {
-	run.addNewCr();
+        run.addNewCr();
     }
 
     public void removeCarriageReturn() {
-	//TODO
-    }   
-    
+        //TODO
+    }    
+
     /**
      * Adds a picture to the run. This method handles
      *  attaching the picture data to the overall file.
@@ -598,100 +627,104 @@ public class XWPFRun {
      *  
      * @param pictureData The raw picture data
      * @param pictureType The type of the picture, eg {@link Document#PICTURE_TYPE_JPEG}
-    * @throws IOException 
-    * @throws org.apache.poi.openxml4j.exceptions.InvalidFormatException 
-    * @throws IOException 
+     * @throws IOException 
+     * @throws org.apache.poi.openxml4j.exceptions.InvalidFormatException 
+     * @throws IOException 
      */
     public XWPFPicture addPicture(InputStream pictureData, int pictureType, String filename, int width, int height)
     throws InvalidFormatException, IOException {
-       XWPFDocument doc = paragraph.document;
-       
-       // Add the picture + relationship
-       int picNumber = doc.addPicture(pictureData, pictureType);
-       XWPFPictureData picData = doc.getAllPackagePictures().get(picNumber);
-       
-       // Create the drawing entry for it
-       try {
-          CTDrawing drawing = run.addNewDrawing();
-          CTInline inline = drawing.addNewInline();
-          
-          // Do the fiddly namespace bits on the inline
-          // (We need full control of what goes where and as what)
-          String xml = 
-             "<a:graphic xmlns:a=\"" + CTGraphicalObject.type.getName().getNamespaceURI() + "\">" +
-             "<a:graphicData uri=\"" + CTGraphicalObject.type.getName().getNamespaceURI() + "\">" +
-             "<pic:pic xmlns:pic=\"" + CTPicture.type.getName().getNamespaceURI() + "\" />" +
-             "</a:graphicData>" +
-             "</a:graphic>";
-          inline.set(XmlToken.Factory.parse(xml));
-          
-          // Setup the inline
-          inline.setDistT(0);
-          inline.setDistR(0);
-          inline.setDistB(0);
-          inline.setDistL(0);
-          
-          CTNonVisualDrawingProps docPr = inline.addNewDocPr();
-          docPr.setId(picNumber);
-          docPr.setName("Picture " + picNumber);
-          docPr.setDescr(filename);
-          
-          CTPositiveSize2D extent = inline.addNewExtent();
-          extent.setCx(width);
-          extent.setCy(height);
-   
-          // Grab the picture object
-          CTGraphicalObject graphic = inline.getGraphic();
-          CTGraphicalObjectData graphicData = graphic.getGraphicData();
-          CTPicture pic = getCTPictures(graphicData).get(0);
-          
-          // Set it up
-          CTPictureNonVisual nvPicPr = pic.addNewNvPicPr();
-          
-          CTNonVisualDrawingProps cNvPr = nvPicPr.addNewCNvPr();
-          cNvPr.setId(picNumber);
-          cNvPr.setName("Picture " + picNumber);
-          cNvPr.setDescr(filename);
-          
-          CTNonVisualPictureProperties cNvPicPr = nvPicPr.addNewCNvPicPr();
-          cNvPicPr.addNewPicLocks().setNoChangeAspect(true);
-          
-          CTBlipFillProperties blipFill = pic.addNewBlipFill();
-          CTBlip blip = blipFill.addNewBlip();
-          blip.setEmbed( picData.getPackageRelationship().getId() );
-          blipFill.addNewStretch().addNewFillRect();
-          
-          CTShapeProperties spPr = pic.addNewSpPr();
-          CTTransform2D xfrm = spPr.addNewXfrm();
-          
-          CTPoint2D off = xfrm.addNewOff();
-          off.setX(0);
-          off.setY(0);
-          
-          CTPositiveSize2D ext = xfrm.addNewExt();
-          ext.setCx(width);
-          ext.setCy(height);
-          
-          CTPresetGeometry2D prstGeom = spPr.addNewPrstGeom();
-          prstGeom.setPrst(STShapeType.RECT);
-          prstGeom.addNewAvLst();
-          
-          // Finish up
-          XWPFPicture xwpfPicture = new XWPFPicture(pic, paragraph);
-          pictures.add(xwpfPicture);
-          return xwpfPicture;
-       } catch(XmlException e) {
-          throw new IllegalStateException(e);
-       }
+        XWPFDocument doc = paragraph.document;
+
+        // Add the picture + relationship
+        String relationId = doc.addPictureData(pictureData, pictureType);
+        XWPFPictureData picData = (XWPFPictureData) doc.getRelationById(relationId);
+
+        // Create the drawing entry for it
+        try {
+            CTDrawing drawing = run.addNewDrawing();
+            CTInline inline = drawing.addNewInline();
+
+            // Do the fiddly namespace bits on the inline
+            // (We need full control of what goes where and as what)
+            String xml = 
+                "<a:graphic xmlns:a=\"" + CTGraphicalObject.type.getName().getNamespaceURI() + "\">" +
+                "<a:graphicData uri=\"" + CTGraphicalObject.type.getName().getNamespaceURI() + "\">" +
+                "<pic:pic xmlns:pic=\"" + CTPicture.type.getName().getNamespaceURI() + "\" />" +
+                "</a:graphicData>" +
+                "</a:graphic>";
+            inline.set(XmlToken.Factory.parse(xml));
+
+            // Setup the inline
+            inline.setDistT(0);
+            inline.setDistR(0);
+            inline.setDistB(0);
+            inline.setDistL(0);
+
+            CTNonVisualDrawingProps docPr = inline.addNewDocPr();
+            long id = getParagraph().document.getDrawingIdManager().reserveNew();
+            docPr.setId(id);
+            /* This name is not visible in Word 2010 anywhere. */
+            docPr.setName("Drawing " + id);
+            docPr.setDescr(filename);
+
+            CTPositiveSize2D extent = inline.addNewExtent();
+            extent.setCx(width);
+            extent.setCy(height);
+
+            // Grab the picture object
+            CTGraphicalObject graphic = inline.getGraphic();
+            CTGraphicalObjectData graphicData = graphic.getGraphicData();
+            CTPicture pic = getCTPictures(graphicData).get(0);
+
+            // Set it up
+            CTPictureNonVisual nvPicPr = pic.addNewNvPicPr();
+
+            CTNonVisualDrawingProps cNvPr = nvPicPr.addNewCNvPr();
+            /* use "0" for the id. See ECM-576, 20.2.2.3 */
+            cNvPr.setId(0L);
+            /* This name is not visible in Word 2010 anywhere */
+            cNvPr.setName("Picture " + id);
+            cNvPr.setDescr(filename);
+
+            CTNonVisualPictureProperties cNvPicPr = nvPicPr.addNewCNvPicPr();
+            cNvPicPr.addNewPicLocks().setNoChangeAspect(true);
+
+            CTBlipFillProperties blipFill = pic.addNewBlipFill();
+            CTBlip blip = blipFill.addNewBlip();
+            blip.setEmbed( picData.getPackageRelationship().getId() );
+            blipFill.addNewStretch().addNewFillRect();
+
+            CTShapeProperties spPr = pic.addNewSpPr();
+            CTTransform2D xfrm = spPr.addNewXfrm();
+
+            CTPoint2D off = xfrm.addNewOff();
+            off.setX(0);
+            off.setY(0);
+
+            CTPositiveSize2D ext = xfrm.addNewExt();
+            ext.setCx(width);
+            ext.setCy(height);
+
+            CTPresetGeometry2D prstGeom = spPr.addNewPrstGeom();
+            prstGeom.setPrst(STShapeType.RECT);
+            prstGeom.addNewAvLst();
+
+            // Finish up
+            XWPFPicture xwpfPicture = new XWPFPicture(pic, this);
+            pictures.add(xwpfPicture);
+            return xwpfPicture;
+        } catch(XmlException e) {
+            throw new IllegalStateException(e);
+        }
     }
-    
+
     /**
      * 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;
+        return pictures;
     }
 
     /**
@@ -714,56 +747,56 @@ public class XWPFRun {
      *  carriage returns in place of their xml equivalents.
      */
     public String toString() {
-       StringBuffer text = new StringBuffer();
-       
-       // Grab the text and tabs of the text run
-       // Do so in a way that preserves the ordering
-       XmlCursor c = run.newCursor();
-       c.selectPath("./*");
-       while (c.toNextSelection()) {
-           XmlObject o = c.getObject();
-           if (o instanceof CTText) {
-               String tagName = o.getDomNode().getNodeName();
-               // Field Codes (w:instrText, defined in spec sec. 17.16.23)
-               //  come up as instances of CTText, but we don't want them
-               //  in the normal text output
-               if (!"w:instrText".equals(tagName)) {
-                  text.append(((CTText) o).getStringValue());
-               }
-           }
-           
-           if (o instanceof CTPTab) {
-               text.append("\t");
-           }
-           if (o instanceof CTBr) {
-              text.append("\n");
-           }
-           if (o instanceof CTEmpty) {
-              // Some inline text elements get returned not as
-              //  themselves, but as CTEmpty, owing to some odd
-              //  definitions around line 5642 of the XSDs
-              // This bit works around it, and replicates the above
-              //  rules for that case
-              String tagName = o.getDomNode().getNodeName();
-              if ("w:tab".equals(tagName)) {
-                 text.append("\t");
-              }
-              if ("w:br".equals(tagName)) {
-                 text.append("\n");
-              }
-              if ("w:cr".equals(tagName)) {
-                 text.append("\n");
-              }
-           }
-       }
-
-       c.dispose();
-       
-       // Any picture text?
-       if(pictureText != null && pictureText.length() > 0) {
-          text.append("\n").append(pictureText);
-       }
-       
-       return text.toString();
+        StringBuffer text = new StringBuffer();
+
+        // Grab the text and tabs of the text run
+        // Do so in a way that preserves the ordering
+        XmlCursor c = run.newCursor();
+        c.selectPath("./*");
+        while (c.toNextSelection()) {
+            XmlObject o = c.getObject();
+            if (o instanceof CTText) {
+                String tagName = o.getDomNode().getNodeName();
+                // Field Codes (w:instrText, defined in spec sec. 17.16.23)
+                //  come up as instances of CTText, but we don't want them
+                //  in the normal text output
+                if (!"w:instrText".equals(tagName)) {
+                    text.append(((CTText) o).getStringValue());
+                }
+            }
+
+            if (o instanceof CTPTab) {
+                text.append("\t");
+            }
+            if (o instanceof CTBr) {
+                text.append("\n");
+            }
+            if (o instanceof CTEmpty) {
+                // Some inline text elements get returned not as
+                //  themselves, but as CTEmpty, owing to some odd
+                //  definitions around line 5642 of the XSDs
+                // This bit works around it, and replicates the above
+                //  rules for that case
+                String tagName = o.getDomNode().getNodeName();
+                if ("w:tab".equals(tagName)) {
+                    text.append("\t");
+                }
+                if ("w:br".equals(tagName)) {
+                    text.append("\n");
+                }
+                if ("w:cr".equals(tagName)) {
+                    text.append("\n");
+                }
+            }
+        }
+
+        c.dispose();
+
+        // Any picture text?
+        if(pictureText != null && pictureText.length() > 0) {
+            text.append("\n").append(pictureText);
+        }
+
+        return text.toString();
     }
 }

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSettings.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSettings.java?rev=1130120&r1=1130119&r2=1130120&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSettings.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSettings.java Wed Jun  1 12:07:16 2011
@@ -42,7 +42,6 @@ public class XWPFSettings extends POIXML
 
     public XWPFSettings(PackagePart part, PackageRelationship rel) throws IOException {
         super(part, rel);
-        readFrom(part.getInputStream());
     }
 
     public XWPFSettings() {
@@ -50,19 +49,26 @@ public class XWPFSettings extends POIXML
         ctSettings = CTSettings.Factory.newInstance();
     }
 
+    @Override
+    protected void onDocumentRead() throws IOException
+    {
+        super.onDocumentRead();
+        readFrom(getPackagePart().getInputStream());
+    }
+
     /**
      * Set zoom.<br/>
      * In the zoom tag inside settings.xml file <br/>
      * it sets the value of zoom
      * <br/>
-     * sample snippet from settings.xml 
+     * sample snippet from settings.xml
      * <pre>
-     *    &lt;w:zoom w:percent="50" /&gt; 
+     *    &lt;w:zoom w:percent="50" /&gt;
      * <pre>
      * @return percentage as an integer of zoom level
      */
     public long getZoomPercent() {
-       CTZoom zoom; 
+       CTZoom zoom;
        if (!ctSettings.isSetZoom()) {
           zoom = ctSettings.addNewZoom();
        } else {

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java?rev=1130120&r1=1130119&r2=1130120&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java Wed Jun  1 12:07:16 2011
@@ -24,7 +24,6 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.lang.String;
 
 import javax.xml.namespace.QName;
 
@@ -49,11 +48,12 @@ import org.openxmlformats.schemas.wordpr
  *
  */
 public class XWPFStyles extends POIXMLDocumentPart{
-	private CTStyles ctStyles;
-	protected XWPFLatentStyles latentStyles;
-	protected List<XWPFStyle> listStyle;
-	
-	/**
+    
+    private List<XWPFStyle> listStyle = new ArrayList<XWPFStyle>();
+    private CTStyles ctStyles;
+    XWPFLatentStyles latentStyles;
+
+    /**
      * Construct XWPFStyles from a package part
      *
      * @param part the package part holding the data of the styles,
@@ -62,14 +62,12 @@ public class XWPFStyles extends POIXMLDo
 
 	public XWPFStyles(PackagePart part, PackageRelationship rel) throws IOException, OpenXML4JException{
 		super(part, rel);
-		onDocumentRead();
 	}
 
 	/**
 	 * Construct XWPFStyles from scratch for a new document.
 	 */
 	public XWPFStyles() {
-		listStyle = new ArrayList<XWPFStyle>();
 	}
 
 	/**
@@ -77,7 +75,6 @@ public class XWPFStyles extends POIXMLDo
 	 */
 	 @Override
 	protected void onDocumentRead ()throws IOException{
-		listStyle = new ArrayList<XWPFStyle>();
 		StylesDocument stylesDoc;
 		try {
 			InputStream is = getPackagePart().getInputStream();
@@ -98,7 +95,7 @@ public class XWPFStyles extends POIXMLDo
 	    protected void commit() throws IOException {
 	        XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
 	        xmlOptions.setSaveSyntheticDocumentElement(new QName(CTStyles.type.getName().getNamespaceURI(), "styles"));
-	        Map map = new HashMap();
+	        Map<String,String> map = new HashMap<String,String>();
 	        map.put("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "r");
 	        map.put("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w");
 	        xmlOptions.setSaveSuggestedPrefixes(map);

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTable.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTable.java?rev=1130120&r1=1130119&r2=1130120&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTable.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTable.java Wed Jun  1 12:07:16 2011
@@ -20,6 +20,7 @@ import java.math.BigInteger;
 import java.util.ArrayList;
 import java.util.List;
 
+import org.apache.poi.POIXMLDocumentPart;
 import org.apache.poi.util.Internal;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow;
@@ -58,7 +59,6 @@ public class XWPFTable implements IBodyE
         }
     }
 
-
     public XWPFTable(CTTbl table, IBody part){
     	this.part = part;
         this.ctTbl = table;
@@ -131,17 +131,17 @@ public class XWPFTable implements IBodyE
         return text.toString();
     }
 
-
     public void addNewRowBetween(int start, int end) {
         // TODO
     }
 
-
     /**
      * add a new column for each row in this table
      */
     public void addNewCol() {
-        if (ctTbl.sizeOfTrArray() == 0) createRow();
+        if (ctTbl.sizeOfTrArray() == 0) {
+            createRow();
+        }
         for (int i = 0; i < ctTbl.sizeOfTrArray(); i++) {
             XWPFTableRow tabRow = new XWPFTableRow(ctTbl.getTrArray(i), this);
             tabRow.createCell();
@@ -268,12 +268,12 @@ public class XWPFTable implements IBodyE
      * @param pos	position the Row in the Table
      */
     public boolean removeRow(int pos) throws IndexOutOfBoundsException {
-    	if(pos > 0 && pos < tableRows.size()){
-    		ctTbl.removeTr(pos);
-    		tableRows.remove(pos);
-    		return true;
-    	}
-    	return false;
+        if (pos >= 0 && pos < tableRows.size()) {
+            ctTbl.removeTr(pos);
+            tableRows.remove(pos);
+            return true;
+        }
+        return false;
     }
 	
     public List<XWPFTableRow> getRows() {
@@ -289,26 +289,30 @@ public class XWPFTable implements IBodyE
 		return BodyElementType.TABLE;
 	}
 
+    @Override
+    public IBody getBody()
+    {
+        return part;
+    }
 
-	/**
-	 * returns the part of the bodyElement
-	 * @see org.apache.poi.xwpf.usermodel.IBody#getPart()
-	 */
-	public IBody getPart() {
-		if(part != null){
-			return part.getPart();
-		}
-		return null;
-	}
-
+    /**
+     * returns the part of the bodyElement
+     * @see org.apache.poi.xwpf.usermodel.IBody#getPart()
+     */
+    public POIXMLDocumentPart getPart() {
+        if(part != null){
+            return part.getPart();
+        }
+        return null;
+    }
 
-	/**
-	 * returns the partType of the bodyPart which owns the bodyElement
-	 * @see org.apache.poi.xwpf.usermodel.IBody#getPartType()
-	 */
-	public BodyType getPartType() {
-		return ((IBody)part).getPartType();
-	}
+    /**
+     * returns the partType of the bodyPart which owns the bodyElement
+     * @see org.apache.poi.xwpf.usermodel.IBody#getPartType()
+     */
+    public BodyType getPartType() {
+        return part.getPartType();
+    }
 
 	/**
 	 * returns the XWPFRow which belongs to the CTRow row

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java?rev=1130120&r1=1130119&r2=1130120&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java Wed Jun  1 12:07:16 2011
@@ -20,6 +20,7 @@ import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 
+import org.apache.poi.POIXMLDocumentPart;
 import org.apache.poi.util.Internal;
 import org.apache.xmlbeans.XmlCursor;
 import org.apache.xmlbeans.XmlObject;
@@ -248,16 +249,14 @@ public class XWPFTableCell implements IB
 		return null;
 	}
 
-
-
-
-	/**
-	 * get the to which the TableCell belongs 
-	 * @see org.apache.poi.xwpf.usermodel.IBody#getPart()
-	 */
-	public IBody getPart() {
-		return tableRow.getTable().getPart();
-	}
+    /**
+     * get the to which the TableCell belongs
+     * 
+     * @see org.apache.poi.xwpf.usermodel.IBody#getPart()
+     */
+    public POIXMLDocumentPart getPart() {
+        return tableRow.getTable().getPart();
+    }
 
 
 	/** 

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableRow.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableRow.java?rev=1130120&r1=1130119&r2=1130120&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableRow.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableRow.java Wed Jun  1 12:07:16 2011
@@ -52,7 +52,7 @@ public class XWPFTableRow {
      * @return the newly created XWPFTableCell
      */
     public XWPFTableCell createCell() {
-        XWPFTableCell tableCell = new XWPFTableCell(ctRow.addNewTc(), this, table.getPart());
+        XWPFTableCell tableCell = new XWPFTableCell(ctRow.addNewTc(), this, table.getBody());
         tableCells.add(tableCell);
         return tableCell;
     }
@@ -69,7 +69,7 @@ public class XWPFTableRow {
      */
     public XWPFTableCell addNewTableCell(){
     	CTTc cell = ctRow.addNewTc();
-    	XWPFTableCell tableCell = new XWPFTableCell(cell, this, table.getPart());
+    	XWPFTableCell tableCell = new XWPFTableCell(cell, this, table.getBody());
     	tableCells.add(tableCell);
     	return tableCell;
     }
@@ -123,7 +123,7 @@ public class XWPFTableRow {
     	if(tableCells == null){
     		List<XWPFTableCell> cells = new ArrayList<XWPFTableCell>();
     		for (CTTc tableCell : ctRow.getTcList()) {
-    			cells.add(new XWPFTableCell(tableCell, this, table.getPart()));
+    			cells.add(new XWPFTableCell(tableCell, this, table.getBody()));
     		}
     		this.tableCells = cells;
     	}

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/TestPOIXMLDocument.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/TestPOIXMLDocument.java?rev=1130120&r1=1130119&r2=1130120&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/TestPOIXMLDocument.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/TestPOIXMLDocument.java Wed Jun  1 12:07:16 2011
@@ -141,4 +141,15 @@ public final class TestPOIXMLDocument ex
                 PackageHelper.open(POIDataSamples.getDocumentInstance().openResourceAsStream("WordWithAttachments.docx"))
                 );
     }
+
+    public void testRelationOrder() throws Exception {
+        OPCPackage pkg = PackageHelper.open(POIDataSamples.getDocumentInstance().openResourceAsStream("WordWithAttachments.docx"));
+        OPCParser doc = new OPCParser(pkg);
+        doc.parse(new TestFactory());
+
+        for(POIXMLDocumentPart rel : doc.getRelations()){
+            System.out.println(rel);
+        }
+
+    }
 }

Copied: poi/trunk/src/ooxml/testcases/org/apache/poi/TestPOIXMLDocumentPart.java (from r1130067, poi/trunk/src/ooxml/testcases/org/apache/poi/TestPOIXMLDocument.java)
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/TestPOIXMLDocumentPart.java?p2=poi/trunk/src/ooxml/testcases/org/apache/poi/TestPOIXMLDocumentPart.java&p1=poi/trunk/src/ooxml/testcases/org/apache/poi/TestPOIXMLDocument.java&r1=1130067&r2=1130120&rev=1130120&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/TestPOIXMLDocument.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/TestPOIXMLDocumentPart.java Wed Jun  1 12:07:16 2011
@@ -35,7 +35,7 @@ import org.apache.poi.util.PackageHelper
 /**
  * Test recursive read and write of OPC packages
  */
-public final class TestPOIXMLDocument extends TestCase {
+public final class TestPOIXMLDocumentPart extends TestCase {
 
     private static class OPCParser extends POIXMLDocument {
 

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/TestPOIXMLProperties.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/TestPOIXMLProperties.java?rev=1130120&r1=1130119&r2=1130120&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/TestPOIXMLProperties.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/TestPOIXMLProperties.java Wed Jun  1 12:07:16 2011
@@ -17,16 +17,21 @@
 
 package org.apache.poi;
 
-import java.util.*;
+import java.io.IOException;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.GregorianCalendar;
+import java.util.Locale;
+import java.util.TimeZone;
 
 import junit.framework.TestCase;
 
 import org.apache.poi.POIXMLProperties.CoreProperties;
+import org.apache.poi.openxml4j.util.Nullable;
 import org.apache.poi.xssf.XSSFTestDataSamples;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.apache.poi.xwpf.XWPFTestDataSamples;
 import org.apache.poi.xwpf.usermodel.XWPFDocument;
-import org.apache.poi.openxml4j.util.Nullable;
 
 /**
  * Test setting extended and custom OOXML properties
@@ -35,7 +40,7 @@ public final class TestPOIXMLProperties 
 	private POIXMLProperties _props;
 	private CoreProperties _coreProperties;
 
-	public void setUp() {
+	public void setUp() throws IOException {
 		XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("documentProperties.docx");
 		_props = sampleDoc.getProperties();
 		_coreProperties = _props.getCoreProperties();
@@ -152,7 +157,7 @@ public final class TestPOIXMLProperties 
 		assertEquals("Hello World", title);
 	}
 
-    public void testTransitiveSetters() {
+    public void testTransitiveSetters() throws IOException {
 		XWPFDocument doc = new XWPFDocument();
         CoreProperties cp = doc.getProperties().getCoreProperties();
 

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java?rev=1130120&r1=1130119&r2=1130120&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java Wed Jun  1 12:07:16 2011
@@ -25,8 +25,8 @@ import java.io.IOException;
 import java.io.OutputStream;
 import java.lang.reflect.Field;
 import java.net.URI;
-import java.util.TreeMap;
-import java.util.Iterator;
+import java.util.*;
+import java.util.regex.Pattern;
 
 import junit.framework.TestCase;
 
@@ -509,4 +509,23 @@ public final class TestPackage extends T
 		f.setAccessible(true);
 		return (ContentTypeManager)f.get(pkg);
 	}
+
+    public void testGetPartsByName() throws Exception {
+        String filepath =  OpenXML4JTestDataSamples.getSampleFileName("sample.docx");
+
+        OPCPackage pkg = OPCPackage.open(filepath, PackageAccess.READ_WRITE);
+        List<PackagePart> rs =  pkg.getPartsByName(Pattern.compile("/word/.*?\\.xml"));
+        HashMap<String, PackagePart>  selected = new HashMap<String, PackagePart>();
+
+        for(PackagePart p : rs)
+            selected.put(p.getPartName().getName(), p);
+
+        assertEquals(6, selected.size());
+        assertTrue(selected.containsKey("/word/document.xml"));
+        assertTrue(selected.containsKey("/word/fontTable.xml"));
+        assertTrue(selected.containsKey("/word/settings.xml"));
+        assertTrue(selected.containsKey("/word/styles.xml"));
+        assertTrue(selected.containsKey("/word/theme/theme1.xml"));
+        assertTrue(selected.containsKey("/word/webSettings.xml"));
+    }
 }

Added: poi/trunk/src/ooxml/testcases/org/apache/poi/util/TestIdentifierManager.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/util/TestIdentifierManager.java?rev=1130120&view=auto
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/util/TestIdentifierManager.java (added)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/util/TestIdentifierManager.java Wed Jun  1 12:07:16 2011
@@ -0,0 +1,113 @@
+package org.apache.poi.util;
+
+import junit.framework.TestCase;
+
+public class TestIdentifierManager extends TestCase
+{
+    public void testBasic()
+    {
+        IdentifierManager manager = new IdentifierManager(0L,100L);
+        assertEquals(101L,manager.getRemainingIdentifiers());
+        assertEquals(0L,manager.reserveNew());
+        assertEquals(100L,manager.getRemainingIdentifiers());
+        assertEquals(1L,manager.reserve(0L));
+        assertEquals(99L,manager.getRemainingIdentifiers());
+    }
+
+    public void testLongLimits()
+    {
+        long min = IdentifierManager.MIN_ID;
+        long max = IdentifierManager.MAX_ID;
+        IdentifierManager manager = new IdentifierManager(min,max);
+        assertTrue("Limits lead to a long variable overflow", max - min + 1 > 0);
+        assertTrue("Limits lead to a long variable overflow", manager.getRemainingIdentifiers() > 0);
+        assertEquals(min,manager.reserveNew());
+        assertEquals(max,manager.reserve(max));
+        assertEquals(max - min -1, manager.getRemainingIdentifiers());
+        manager.release(max);
+        manager.release(min);
+    }
+    
+    public void testReserve()
+    {
+        IdentifierManager manager = new IdentifierManager(10L,30L);
+        assertEquals(12L,manager.reserve(12L));
+        long reserve = manager.reserve(12L);
+        assertFalse("Same id must be reserved twice!",reserve == 12L);
+        assertTrue(manager.release(12L));
+        assertTrue(manager.release(reserve));
+        assertFalse(manager.release(12L));
+        assertFalse(manager.release(reserve));
+        
+        manager = new IdentifierManager(0L,2L);
+        assertEquals(0L,manager.reserve(0L));
+        assertEquals(1L,manager.reserve(1L));
+        assertEquals(2L,manager.reserve(2L));
+        try
+        {
+            manager.reserve(0L);
+            fail("Exception expected");
+        }
+        catch(IllegalStateException e)
+        {
+            // expected
+        }
+        try
+        {
+            manager.reserve(1L);
+            fail("Exception expected");
+        }
+        catch(IllegalStateException e)
+        {
+            // expected
+        }
+        try
+        {
+            manager.reserve(2L);
+            fail("Exception expected");
+        }
+        catch(IllegalStateException e)
+        {
+            // expected
+        }
+    }
+
+    public void testReserveNew()
+    {
+        IdentifierManager manager = new IdentifierManager(10L,12L);
+        assertSame(10L,manager.reserveNew());
+        assertSame(11L,manager.reserveNew());
+        assertSame(12L,manager.reserveNew());
+        try {
+            manager.reserveNew();
+            fail("IllegalStateException expected");
+        }
+        catch (IllegalStateException e)
+        {
+            // expected
+        }
+    }
+    
+    public void testRelease() {
+        IdentifierManager manager = new IdentifierManager(10L,20L);
+        assertEquals(10L,manager.reserve(10L));
+        assertEquals(11L,manager.reserve(11L));
+        assertEquals(12L,manager.reserve(12L));
+        assertEquals(13L,manager.reserve(13L));
+        assertEquals(14L,manager.reserve(14L));
+        
+        assertTrue(manager.release(10L));
+        assertEquals(10L,manager.reserve(10L));
+        assertTrue(manager.release(10L));
+        
+        assertTrue(manager.release(11L));
+        assertEquals(11L,manager.reserve(11L));
+        assertTrue(manager.release(11L));
+        assertFalse(manager.release(11L));
+        assertFalse(manager.release(10L));
+
+        assertEquals(10L,manager.reserve(10L));
+        assertEquals(11L,manager.reserve(11L));
+        assertTrue(manager.release(12L));
+    }
+}

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/AllXWPFTests.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/AllXWPFTests.java?rev=1130120&r1=1130119&r2=1130120&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/AllXWPFTests.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/AllXWPFTests.java Wed Jun  1 12:07:16 2011
@@ -22,6 +22,7 @@ import junit.framework.TestSuite;
 
 import org.apache.poi.xwpf.extractor.TestXWPFWordExtractor;
 import org.apache.poi.xwpf.model.TestXWPFHeaderFooterPolicy;
+import org.apache.poi.xwpf.usermodel.TestXWPFDocument;
 import org.apache.poi.xwpf.usermodel.TestXWPFHeader;
 import org.apache.poi.xwpf.usermodel.TestXWPFHeadings;
 import org.apache.poi.xwpf.usermodel.TestXWPFNumbering;

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/TestAllExtendedProperties.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/TestAllExtendedProperties.java?rev=1130120&r1=1130119&r2=1130120&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/TestAllExtendedProperties.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/TestAllExtendedProperties.java Wed Jun  1 12:07:16 2011
@@ -17,6 +17,8 @@
 
 package org.apache.poi.xwpf;
 
+import java.io.IOException;
+
 import junit.framework.TestCase;
 
 import org.apache.poi.POIXMLProperties.CoreProperties;
@@ -41,7 +43,7 @@ import org.openxmlformats.schemas.office
  * 
  */
 public final class TestAllExtendedProperties extends TestCase {
-	public void testGetAllExtendedProperties() {
+	public void testGetAllExtendedProperties() throws IOException {
 		XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("TestPoiXMLDocumentCorePropertiesGetKeywords.docx");
 		CTProperties ctProps = doc.getProperties().getExtendedProperties().getUnderlyingProperties();
 		assertEquals("Microsoft Office Word",ctProps.getApplication());

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/TestPackageCorePropertiesGetKeywords.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/TestPackageCorePropertiesGetKeywords.java?rev=1130120&r1=1130119&r2=1130120&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/TestPackageCorePropertiesGetKeywords.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/TestPackageCorePropertiesGetKeywords.java Wed Jun  1 12:07:16 2011
@@ -17,6 +17,8 @@
 
 package org.apache.poi.xwpf;
 
+import java.io.IOException;
+
 import junit.framework.TestCase;
 
 import org.apache.poi.POIXMLProperties.CoreProperties;
@@ -37,7 +39,7 @@ import org.apache.poi.xwpf.usermodel.XWP
  * 
  */
 public final class TestPackageCorePropertiesGetKeywords extends TestCase {
-	public void testGetSetKeywords() {
+	public void testGetSetKeywords() throws IOException {
 		XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("TestPoiXMLDocumentCorePropertiesGetKeywords.docx");
 		String keywords = doc.getProperties().getCoreProperties().getKeywords();
 		assertEquals("extractor, test, rdf", keywords);

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/XWPFTestDataSamples.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/XWPFTestDataSamples.java?rev=1130120&r1=1130119&r2=1130120&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/XWPFTestDataSamples.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/XWPFTestDataSamples.java Wed Jun  1 12:07:16 2011
@@ -22,6 +22,8 @@ import java.io.IOException;
 import java.io.InputStream;
 
 import org.apache.poi.POIDataSamples;
+import org.apache.poi.util.IOUtils;
+import org.apache.poi.util.PackageHelper;
 import org.apache.poi.xwpf.usermodel.XWPFDocument;
 
 /**
@@ -29,23 +31,21 @@ import org.apache.poi.xwpf.usermodel.XWP
  */
 public class XWPFTestDataSamples {
 
-    public static XWPFDocument openSampleDocument(String sampleName) {
+    public static XWPFDocument openSampleDocument(String sampleName) throws IOException {
         InputStream is = POIDataSamples.getDocumentInstance().openResourceAsStream(sampleName);
-        try {
-            return new XWPFDocument(is);
-        } catch (IOException e) {
-            throw new RuntimeException(e);
-        }
+        return new XWPFDocument(is);
     }
 
-    public static XWPFDocument writeOutAndReadBack(XWPFDocument doc) {
-        try {
-            ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
-            doc.write(baos);
-            ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
-            return new XWPFDocument(bais);
-        } catch (IOException e) {
-            throw new RuntimeException(e);
-        }
+    public static XWPFDocument writeOutAndReadBack(XWPFDocument doc) throws IOException {
+        ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
+        doc.write(baos);
+        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+        return new XWPFDocument(bais);
+    }
+
+    public static byte[] getImage(String filename) throws IOException {
+        InputStream is = POIDataSamples.getDocumentInstance().openResourceAsStream(filename);
+        byte[] result = IOUtils.toByteArray(is);
+        return result;
     }
 }

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/extractor/TestXWPFWordExtractor.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/extractor/TestXWPFWordExtractor.java?rev=1130120&r1=1130119&r2=1130120&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/extractor/TestXWPFWordExtractor.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/extractor/TestXWPFWordExtractor.java Wed Jun  1 12:07:16 2011
@@ -17,6 +17,8 @@
 
 package org.apache.poi.xwpf.extractor;
 
+import java.io.IOException;
+
 import junit.framework.TestCase;
 
 import org.apache.poi.xwpf.XWPFTestDataSamples;
@@ -29,8 +31,9 @@ public class TestXWPFWordExtractor exten
 
     /**
      * Get text out of the simple file
+     * @throws IOException 
      */
-    public void testGetSimpleText() {
+    public void testGetSimpleText() throws IOException {
         XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx");
         XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
 
@@ -58,8 +61,9 @@ public class TestXWPFWordExtractor exten
 
     /**
      * Tests getting the text out of a complex file
+     * @throws IOException 
      */
-    public void testGetComplexText() {
+    public void testGetComplexText() throws IOException {
         XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("IllustrativeCases.docx");
         XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
 
@@ -91,7 +95,7 @@ public class TestXWPFWordExtractor exten
         assertEquals(134, ps);
     }
 
-    public void testGetWithHyperlinks() {
+    public void testGetWithHyperlinks() throws IOException {
         XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("TestDocument.docx");
         XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
 
@@ -116,7 +120,7 @@ public class TestXWPFWordExtractor exten
         );
     }
 
-    public void testHeadersFooters() {
+    public void testHeadersFooters() throws IOException {
         XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("ThreeColHeadFoot.docx");
         XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
 
@@ -159,7 +163,7 @@ public class TestXWPFWordExtractor exten
         );
     }
 
-    public void testFootnotes() {
+    public void testFootnotes() throws IOException {
         XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("footnotes.docx");
         XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
 
@@ -167,14 +171,14 @@ public class TestXWPFWordExtractor exten
     }
 
 
-    public void testTableFootnotes() {
+    public void testTableFootnotes() throws IOException {
         XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("table_footnotes.docx");
         XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
 
         assertTrue(extractor.getText().contains("snoska"));
     }
 
-    public void testFormFootnotes() {
+    public void testFormFootnotes() throws IOException {
         XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("form_footnotes.docx");
         XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
 
@@ -183,14 +187,14 @@ public class TestXWPFWordExtractor exten
         assertTrue("Unable to find expected word in text\n" + text, text.contains("test phrase"));
     }
 
-    public void testEndnotes() {
+    public void testEndnotes() throws IOException {
         XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("endnotes.docx");
         XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
 
         assertTrue(extractor.getText().contains("XXX"));
     }
 
-    public void testInsertedDeletedText() {
+    public void testInsertedDeletedText() throws IOException {
         XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("delins.docx");
         XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
 
@@ -198,7 +202,7 @@ public class TestXWPFWordExtractor exten
         assertTrue(extractor.getText().contains("extremely well"));
     }
 
-    public void testParagraphHeader() {
+    public void testParagraphHeader() throws IOException {
         XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Headers.docx");
         XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
 
@@ -210,8 +214,9 @@ public class TestXWPFWordExtractor exten
     /**
      * Test that we can open and process .docm
      *  (macro enabled) docx files (bug #45690)
+     * @throws IOException 
      */
-    public void testDOCMFiles() {
+    public void testDOCMFiles() throws IOException {
         XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("45690.docm");
         XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
 
@@ -224,8 +229,9 @@ public class TestXWPFWordExtractor exten
      * Test that we handle things like tabs and
      *  carriage returns properly in the text that
      *  we're extracting (bug #49189)
+     * @throws IOException 
      */
-    public void testDocTabs() {
+    public void testDocTabs() throws IOException {
        XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("WithTabs.docx");
        XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
  
@@ -241,8 +247,9 @@ public class TestXWPFWordExtractor exten
     /**
      * The output should not contain field codes, e.g. those specified in the
      * w:instrText tag (spec sec. 17.16.23)
+     * @throws IOException 
      */
-    public void testNoFieldCodes() {
+    public void testNoFieldCodes() throws IOException {
         XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("FieldCodes.docx");
         XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
         String text = extractor.getText();
@@ -254,8 +261,9 @@ public class TestXWPFWordExtractor exten
     /**
      * The output should contain the values of simple fields, those specified
      * with the fldSimple element (spec sec. 17.16.19)
+     * @throws IOException 
      */
-    public void testFldSimpleContent() {
+    public void testFldSimpleContent() throws IOException {
         XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("FldSimple.docx");
         XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
         String text = extractor.getText();

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/model/TestXWPFDecorators.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/model/TestXWPFDecorators.java?rev=1130120&r1=1130119&r2=1130120&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/model/TestXWPFDecorators.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/model/TestXWPFDecorators.java Wed Jun  1 12:07:16 2011
@@ -17,6 +17,8 @@
 
 package org.apache.poi.xwpf.model;
 
+import java.io.IOException;
+
 import junit.framework.TestCase;
 
 import org.apache.poi.xwpf.XWPFTestDataSamples;
@@ -32,10 +34,10 @@ public class TestXWPFDecorators extends 
    private XWPFDocument hyperlink;
    private XWPFDocument comments;
 
-   protected void setUp() {
-      simple = XWPFTestDataSamples.openSampleDocument("SampleDoc.docx");
-      hyperlink = XWPFTestDataSamples.openSampleDocument("TestDocument.docx");
-      comments = XWPFTestDataSamples.openSampleDocument("WordWithAttachments.docx");
+   protected void setUp() throws IOException {
+        simple = XWPFTestDataSamples.openSampleDocument("SampleDoc.docx");
+        hyperlink = XWPFTestDataSamples.openSampleDocument("TestDocument.docx");
+        comments = XWPFTestDataSamples.openSampleDocument("WordWithAttachments.docx");
    }
 
    public void testHyperlink() {

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/model/TestXWPFHeaderFooterPolicy.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/model/TestXWPFHeaderFooterPolicy.java?rev=1130120&r1=1130119&r2=1130120&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/model/TestXWPFHeaderFooterPolicy.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/model/TestXWPFHeaderFooterPolicy.java Wed Jun  1 12:07:16 2011
@@ -17,6 +17,8 @@
 
 package org.apache.poi.xwpf.model;
 
+import java.io.IOException;
+
 import junit.framework.TestCase;
 
 import org.apache.poi.xwpf.XWPFTestDataSamples;
@@ -33,9 +35,9 @@ public class TestXWPFHeaderFooterPolicy 
 	private XWPFDocument oddEven;
 	private XWPFDocument diffFirst;
 
-	protected void setUp() {
+	protected void setUp() throws IOException {
 
-		noHeader = XWPFTestDataSamples.openSampleDocument("NoHeadFoot.docx");
+	    noHeader = XWPFTestDataSamples.openSampleDocument("NoHeadFoot.docx");
 		header = XWPFTestDataSamples.openSampleDocument("ThreeColHead.docx");
 		headerFooter = XWPFTestDataSamples.openSampleDocument("SimpleHeadThreeColFoot.docx");
 		footer = XWPFTestDataSamples.openSampleDocument("FancyFoot.docx");

Copied: poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java (from r1130067, poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/TestXWPFDocument.java)
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java?p2=poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java&p1=poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/TestXWPFDocument.java&r1=1130067&r2=1130120&rev=1130120&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/TestXWPFDocument.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java Wed Jun  1 12:07:16 2011
@@ -15,9 +15,11 @@
    limitations under the License.
 ==================================================================== */
 
-package org.apache.poi.xwpf;
+package org.apache.poi.xwpf.usermodel;
 
-import java.util.ArrayList;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.Arrays;
 import java.util.List;
 
 import junit.framework.TestCase;
@@ -27,10 +29,11 @@ import org.apache.poi.POIXMLProperties;
 import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
 import org.apache.poi.openxml4j.opc.OPCPackage;
 import org.apache.poi.openxml4j.opc.PackagePart;
-import org.apache.poi.xwpf.usermodel.XWPFDocument;
-import org.apache.poi.xwpf.usermodel.XWPFParagraph;
-import org.apache.poi.xwpf.usermodel.XWPFPictureData;
-import org.apache.poi.xwpf.usermodel.XWPFRelation;
+import org.apache.poi.openxml4j.opc.PackagePartName;
+import org.apache.poi.openxml4j.opc.PackageRelationship;
+import org.apache.poi.openxml4j.opc.PackagingURIHelper;
+import org.apache.poi.openxml4j.opc.TargetMode;
+import org.apache.poi.xwpf.XWPFTestDataSamples;
 import org.apache.xmlbeans.XmlCursor;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
 
@@ -70,7 +73,7 @@ public final class TestXWPFDocument exte
 		assertNotNull(xml.getStyle());
 	}
 
-	public void testMetadataBasics() {
+	public void testMetadataBasics() throws IOException {
 		XWPFDocument xml = XWPFTestDataSamples.openSampleDocument("sample.docx");
 		assertNotNull(xml.getProperties().getCoreProperties());
 		assertNotNull(xml.getProperties().getExtendedProperties());
@@ -83,7 +86,7 @@ public final class TestXWPFDocument exte
 		assertEquals(null, xml.getProperties().getCoreProperties().getUnderlyingProperties().getSubjectProperty().getValue());
 	}
 
-	public void testMetadataComplex() {
+	public void testMetadataComplex() throws IOException {
 		XWPFDocument xml = XWPFTestDataSamples.openSampleDocument("IllustrativeCases.docx");
 		assertNotNull(xml.getProperties().getCoreProperties());
 		assertNotNull(xml.getProperties().getExtendedProperties());
@@ -103,7 +106,7 @@ public final class TestXWPFDocument exte
 		assertEquals("Apache POI", props.getExtendedProperties().getUnderlyingProperties().getApplication());
 	}
 	
-	public void testAddParagraph(){
+	public void testAddParagraph() throws IOException{
 	   XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx");
 	   assertEquals(3, doc.getParagraphs().size());
 
@@ -122,24 +125,22 @@ public final class TestXWPFDocument exte
 	   assertSame(cP, doc.getParagraphs().get(0));
 	   assertEquals(5, doc.getParagraphs().size());
 	}
-	
-	public void testAddPicture(){
-		XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx");
-		byte[] jpeg = "This is a jpeg".getBytes();
-		try {
-			int jpegNum = doc.addPicture(jpeg, XWPFDocument.PICTURE_TYPE_JPEG);
-			byte[] newJpeg = doc.getAllPictures().get(jpegNum).getData();
-			assertEquals(newJpeg.length, jpeg.length);
-			for(int i = 0 ; i < jpeg.length; i++){
-				assertEquals(newJpeg[i], jpeg[i]); 
-			}
-		} catch (InvalidFormatException e) {
-			// TODO Auto-generated catch block
-			e.printStackTrace();
-		}
-	}
-	
-	public void testRemoveBodyElement() {
+
+    public void testAddPicture() throws IOException, InvalidFormatException
+    {
+        XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx");
+        byte[] jpeg = XWPFTestDataSamples.getImage("nature1.jpg");
+        String relationId = doc.addPictureData(jpeg,XWPFDocument.PICTURE_TYPE_JPEG);
+        
+        byte[] newJpeg = ((XWPFPictureData) doc.getRelationById(relationId)).getData();
+        assertEquals(newJpeg.length,jpeg.length);
+        for (int i = 0 ; i < jpeg.length ; i++)
+        {
+            assertEquals(newJpeg[i],jpeg[i]);
+        }
+    }
+
+	public void testRemoveBodyElement() throws IOException {
 	   XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx");
 	   assertEquals(3, doc.getParagraphs().size());
       assertEquals(3, doc.getBodyElements().size());
@@ -200,46 +201,119 @@ public final class TestXWPFDocument exte
       assertEquals(p3, doc.getParagraphs().get(0));
 	}
 	
-	public void testSettings() throws Exception {
-	   XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("WithGIF.docx");
-	   assertEquals(120, doc.getZoomPercent());
-	   assertEquals(false, doc.isEnforcedCommentsProtection());
-      assertEquals(false, doc.isEnforcedFillingFormsProtection());
-      assertEquals(false, doc.isEnforcedReadonlyProtection());
-      assertEquals(false, doc.isEnforcedTrackedChangesProtection());
-      
-      doc.setZoomPercent(124);
-      
-      // Only one enforcement allowed, last one wins!
-      doc.enforceFillingFormsProtection();
-      doc.enforceReadonlyProtection();
-      
-      doc = XWPFTestDataSamples.writeOutAndReadBack(doc);
-      
-      assertEquals(124, doc.getZoomPercent());
-      assertEquals(false, doc.isEnforcedCommentsProtection());
-      assertEquals(false, doc.isEnforcedFillingFormsProtection());
-      assertEquals(true, doc.isEnforcedReadonlyProtection());
-      assertEquals(false, doc.isEnforcedTrackedChangesProtection());
-	}
-	
-	public void testGIFSupport() throws Exception {
-	    XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("WithGIF.docx");
-	    ArrayList<PackagePart> gifParts = doc.getPackage().getPartsByContentType(XWPFRelation.IMAGE_GIF.getContentType());
-	    assertEquals("Expected exactly one GIF part in package.",1,gifParts.size());
-	    PackagePart gifPart = gifParts.get(0);
-	    
-	    List<POIXMLDocumentPart> relations = doc.getRelations();
-	    POIXMLDocumentPart gifDocPart = null;
-	    for (POIXMLDocumentPart docPart : relations)
-        {
-            if (gifPart == docPart.getPackagePart())
-            {
-                assertNull("More than one POIXMLDocumentPart for GIF PackagePart.",gifDocPart);
-                gifDocPart = docPart;
-            }
+	public void testRegisterPackagePictureData() throws IOException, InvalidFormatException {
+	    XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("issue_51265_1.docx");
+	    
+	    /* manually assemble a new image package part*/
+	    OPCPackage opcPckg = doc.getPackage();
+	    XWPFRelation jpgRelation = XWPFRelation.IMAGE_JPEG;
+	    PackagePartName partName = PackagingURIHelper.createPartName(jpgRelation.getDefaultFileName().replace('#', '2'));
+        PackagePart newImagePart = opcPckg.createPart(partName, jpgRelation.getContentType());
+        byte[] nature1 = XWPFTestDataSamples.getImage("abstract4.jpg");
+        OutputStream os = newImagePart.getOutputStream();
+        os.write(nature1);
+	    os.close();
+	    XWPFHeader xwpfHeader = doc.getHeaderList().get(0);
+	    PackageRelationship relationship = xwpfHeader.getPackagePart().addRelationship(partName, TargetMode.INTERNAL, jpgRelation.getRelation());
+	    XWPFPictureData newPicData = new XWPFPictureData(newImagePart,relationship);
+	    /* new part is now ready to rumble */
+	    
+	    assertFalse(xwpfHeader.getAllPictures().contains(newPicData));
+	    assertFalse(doc.getAllPictures().contains(newPicData));
+	    assertFalse(doc.getAllPackagePictures().contains(newPicData));
+
+	    doc.registerPackagePictureData(newPicData);
+	    
+	    assertFalse(xwpfHeader.getAllPictures().contains(newPicData));
+	    assertFalse(doc.getAllPictures().contains(newPicData));
+	    assertTrue(doc.getAllPackagePictures().contains(newPicData));
+	    
+	    doc.getPackage().revert();
+	}
+
+	public void testFindPackagePictureData() throws IOException {
+	    XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("issue_51265_1.docx");
+	    byte[] nature1 = XWPFTestDataSamples.getImage("nature1.gif");
+	    XWPFPictureData part = doc.findPackagePictureData(nature1, Document.PICTURE_TYPE_GIF);
+	    assertNotNull(part);
+	    assertTrue(doc.getAllPictures().contains(part));
+	    assertTrue(doc.getAllPackagePictures().contains(part));
+	    doc.getPackage().revert();
+	}
+	
+	public void testGetAllPictures() throws IOException {
+	    XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("issue_51265_3.docx");
+	    List<XWPFPictureData> allPictures = doc.getAllPictures();
+	    List<XWPFPictureData> allPackagePictures = doc.getAllPackagePictures();
+	    
+	    assertNotNull(allPictures);
+	    assertEquals(3,allPictures.size());
+	    for (XWPFPictureData xwpfPictureData : allPictures) {
+	        assertTrue(allPackagePictures.contains(xwpfPictureData));
         }
-	    assertNotNull("GIF part not related to document.xml PackagePart",gifDocPart);
-	    assertTrue("XWPFRelation for GIF image was not recognized properly, as the POIXMLDocumentPart created was of a wrong type.",XWPFRelation.IMAGE_GIF.getRelationClass().isInstance(gifDocPart));
+
+	    try {
+            allPictures.add(allPictures.get(0));
+            fail("This list must be unmodifiable!");
+        } catch (UnsupportedOperationException e) {
+            // all ok
+        }
+	    
+	    doc.getPackage().revert();
+	}
+
+	public void testGetAllPackagePictures() throws IOException {
+	       XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("issue_51265_3.docx");
+	        List<XWPFPictureData> allPackagePictures = doc.getAllPackagePictures();
+	        
+	        assertNotNull(allPackagePictures);
+	        assertEquals(5,allPackagePictures.size());
+
+	        try {
+	            allPackagePictures.add(allPackagePictures.get(0));
+	            fail("This list must be unmodifiable!");
+	        } catch (UnsupportedOperationException e) {
+	            // all ok
+	        }
+	        
+	        doc.getPackage().revert();
+	}
+	
+	public void testPictureHandlingSimpleFile() throws IOException, InvalidFormatException {
+	    XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("issue_51265_1.docx");
+	    assertEquals(1,doc.getAllPackagePictures().size());
+	    byte[] newPic = XWPFTestDataSamples.getImage("abstract4.jpg");
+	    String id1 = doc.addPictureData(newPic, Document.PICTURE_TYPE_JPEG);
+	    assertEquals(2,doc.getAllPackagePictures().size());
+	    /* copy data, to avoid instance-equality */
+	    byte[] newPicCopy = Arrays.copyOf(newPic, newPic.length);
+	    String id2 = doc.addPictureData(newPicCopy, Document.PICTURE_TYPE_JPEG);
+	    assertEquals(id1,id2);
+	    doc.getPackage().revert();
+	}
+	
+	public void testPictureHandlingHeaderDocumentImages() throws IOException {
+	    XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("issue_51265_2.docx");
+	    assertEquals(1,doc.getAllPictures().size());
+	    assertEquals(1,doc.getAllPackagePictures().size());
+	    assertEquals(1,doc.getHeaderList().get(0).getAllPictures().size());
+	    doc.getPackage().revert();
+	}
+	
+	public void testPictureHandlingComplex() throws IOException, InvalidFormatException {
+	    XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("issue_51265_3.docx");
+	    XWPFHeader xwpfHeader = doc.getHeaderList().get(0);
+
+	    assertEquals(3,doc.getAllPictures().size());
+        assertEquals(3,xwpfHeader.getAllPictures().size());
+	    assertEquals(5,doc.getAllPackagePictures().size());
+	    
+	    byte[] nature1 = XWPFTestDataSamples.getImage("nature1.jpg");
+	    String id = doc.addPictureData(nature1, Document.PICTURE_TYPE_JPEG);
+	    POIXMLDocumentPart part1 = xwpfHeader.getRelationById("rId1");
+	    XWPFPictureData part2 = (XWPFPictureData) doc.getRelationById(id);
+	    assertSame(part1,part2);
+	    
+	    doc.getPackage().revert();
 	}
 }



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


Mime
View raw message