Author: fanningpj
Date: Thu Jul 21 09:56:56 2022
New Revision: 1902905

URL: http://svn.apache.org/viewvc?rev=1902905&view=rev
Log:
add getPictureTypeEnum

Modified:
    
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFComments.java
    
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java
    
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java
    poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRun.java
    
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSignatureLine.java
    
poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java
    
poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFRun.java

Modified: 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFComments.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFComments.java?rev=1902905&r1=1902904&r2=1902905&view=diff
==============================================================================
--- 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFComments.java
 (original)
+++ 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFComments.java
 Thu Jul 21 09:56:56 2022
@@ -105,6 +105,7 @@ public class XWPFComments extends POIXML
      * obtained from {@link #getAllPictures()} .
      * @throws InvalidFormatException If the format of the picture is not 
known.
      * @throws IOException            If reading the picture-data from the 
stream fails.
+     * @see #addPictureData(InputStream, PictureType)
      */
     public String addPictureData(InputStream is, int format) throws 
InvalidFormatException, IOException {
         byte[] data = IOUtils.toByteArrayWithMaxLength(is, 
XWPFPictureData.getMaxImageSize());
@@ -114,6 +115,22 @@ public class XWPFComments extends POIXML
     /**
      * Adds a picture to the comments.
      *
+     * @param is     The stream to read image from
+     * @param pictureType The {@link PictureType} of the picture
+     * @return the index to this picture (0 based), the added picture can be
+     * obtained from {@link #getAllPictures()} .
+     * @throws InvalidFormatException If the pictureType of the picture is not 
known.
+     * @throws IOException            If reading the picture-data from the 
stream fails.
+     * @since POI 5.2.3
+     */
+    public String addPictureData(InputStream is, PictureType pictureType) 
throws InvalidFormatException, IOException {
+        byte[] data = IOUtils.toByteArrayWithMaxLength(is, 
XWPFPictureData.getMaxImageSize());
+        return addPictureData(data, pictureType);
+    }
+
+    /**
+     * Adds a picture to the comments.
+     *
      * @param pictureData The picture data
      * @param format      The format of the picture.
      * @return the index to this picture (0 based), the added picture can be
@@ -121,12 +138,29 @@ public class XWPFComments extends POIXML
      * @throws InvalidFormatException If the format of the picture is not 
known.
      */
     public String addPictureData(byte[] pictureData, int format) throws 
InvalidFormatException {
-        XWPFPictureData xwpfPicData = 
document.findPackagePictureData(pictureData, format);
-        POIXMLRelation relDesc = XWPFPictureData.RELATIONS[format];
+        return addPictureData(pictureData, PictureType.findById(format));
+    }
+
+    /**
+     * Adds a picture to the comments.
+     *
+     * @param pictureData The picture data
+     * @param pictureType The {@link PictureType} of the picture.
+     * @return the index to this picture (0 based), the added picture can be
+     * obtained from {@link #getAllPictures()} .
+     * @throws InvalidFormatException If the pictureType of the picture is not 
known.
+     * @since POI 5.2.3
+     */
+    public String addPictureData(byte[] pictureData, PictureType pictureType) 
throws InvalidFormatException {
+        if (pictureType == null) {
+            throw new InvalidFormatException("pictureType parameter is 
invalid");
+        }
+        XWPFPictureData xwpfPicData = 
document.findPackagePictureData(pictureData);
+        POIXMLRelation relDesc = 
XWPFPictureData.RELATIONS[pictureType.getId()];
 
         if (xwpfPicData == null) {
             /* Part doesn't exist, create a new one */
-            int idx = getXWPFDocument().getNextPicNameNumber(format);
+            int idx = getXWPFDocument().getNextPicNameNumber(pictureType);
             xwpfPicData = (XWPFPictureData) createRelationship(relDesc, 
XWPFFactory.getInstance(), idx);
             /* write bytes to new part */
             PackagePart picDataPart = xwpfPicData.getPackagePart();

Modified: 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java?rev=1902905&r1=1902904&r2=1902905&view=diff
==============================================================================
--- 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java
 (original)
+++ 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java
 Thu Jul 21 09:56:56 2022
@@ -1455,7 +1455,7 @@ public class XWPFDocument extends POIXML
         }
     }
 
-    XWPFPictureData findPackagePictureData(byte[] pictureData, int format) {
+    XWPFPictureData findPackagePictureData(byte[] pictureData) {
         long checksum = IOUtils.calculateChecksum(pictureData);
         XWPFPictureData xwpfPicData = null;
         /*
@@ -1475,13 +1475,40 @@ public class XWPFDocument extends POIXML
         return xwpfPicData;
     }
 
+    /**
+     * Adds a picture to the document.
+     *
+     * @param pictureData The picture data
+     * @param format the format of the picture, see constants in {@link 
Document}
+     * @return the index to this picture (0 based), the added picture can be
+     * obtained from {@link #getAllPictures()} .
+     * @throws InvalidFormatException if the format is not known
+     * @see #addPictureData(byte[], PictureType)
+     */
     public String addPictureData(byte[] pictureData, int format) throws 
InvalidFormatException {
-        XWPFPictureData xwpfPicData = findPackagePictureData(pictureData, 
format);
-        POIXMLRelation relDesc = XWPFPictureData.RELATIONS[format];
+        return addPictureData(pictureData, PictureType.findById(format));
+    }
+
+    /**
+     * Adds a picture to the document.
+     *
+     * @param pictureData The picture data
+     * @param pictureType the {@link PictureType}
+     * @return the index to this picture (0 based), the added picture can be
+     * obtained from {@link #getAllPictures()} .
+     * @throws InvalidFormatException if the format is not known
+     * @since POI 5.2.3
+     */
+    public String addPictureData(byte[] pictureData, PictureType pictureType) 
throws InvalidFormatException {
+        if (pictureType == null) {
+            throw new InvalidFormatException("pictureType parameter is 
invalid");
+        }
+        XWPFPictureData xwpfPicData = findPackagePictureData(pictureData);
+        POIXMLRelation relDesc = 
XWPFPictureData.RELATIONS[pictureType.getId()];
 
         if (xwpfPicData == null) {
             /* Part doesn't exist, create a new one */
-            int idx = getNextPicNameNumber(format);
+            int idx = getNextPicNameNumber(pictureType);
             xwpfPicData = (XWPFPictureData) createRelationship(relDesc, 
XWPFFactory.getInstance(), idx);
             /* write bytes to new part */
             PackagePart picDataPart = xwpfPicData.getPackagePart();
@@ -1510,6 +1537,16 @@ public class XWPFDocument extends POIXML
         }
     }
 
+    /**
+     * Adds a picture to the document.
+     *
+     * @param is The picture data
+     * @param format the format of the picture, see constants in {@link 
Document}
+     * @return the index to this picture (0 based), the added picture can be
+     * obtained from {@link #getAllPictures()} .
+     * @throws InvalidFormatException if the format is not known
+     * @see #addPictureData(InputStream, PictureType)
+     */
     public String addPictureData(InputStream is, int format) throws 
InvalidFormatException {
         try {
             byte[] data = IOUtils.toByteArrayWithMaxLength(is, 
XWPFPictureData.getMaxImageSize());
@@ -1520,18 +1557,54 @@ public class XWPFDocument extends POIXML
     }
 
     /**
+     * Adds a picture to the document.
+     *
+     * @param is The picture data
+     * @param pictureType the {@link PictureType}
+     * @return the index to this picture (0 based), the added picture can be
+     * obtained from {@link #getAllPictures()} .
+     * @throws InvalidFormatException if the pictureType is not known
+     * @since POI 5.2.3
+     */
+    public String addPictureData(InputStream is, PictureType pictureType) 
throws InvalidFormatException {
+        try {
+            byte[] data = IOUtils.toByteArrayWithMaxLength(is, 
XWPFPictureData.getMaxImageSize());
+            return addPictureData(data, pictureType);
+        } catch (IOException e) {
+            throw new POIXMLException(e);
+        }
+    }
+
+    /**
      * get the next free ImageNumber
      *
+     * @param format the format of the picture, see constants in {@link 
Document}
      * @return the next free ImageNumber
      * @throws InvalidFormatException If the format of the picture is not 
known.
+     * @see #getNextPicNameNumber(PictureType)
      */
     public int getNextPicNameNumber(int format) throws InvalidFormatException {
+        return getNextPicNameNumber(PictureType.findById(format));
+    }
+
+    /**
+     * get the next free ImageNumber
+     *
+     * @param pictureType the {@link PictureType}
+     * @return the next free ImageNumber
+     * @throws InvalidFormatException If the pictureType of the picture is not 
known.
+     * @since POI 5.2.3
+     */
+    public int getNextPicNameNumber(PictureType pictureType) throws 
InvalidFormatException {
+        if (pictureType == null) {
+            throw new InvalidFormatException("pictureType parameter is 
invalid");
+        }
         int img = getAllPackagePictures().size() + 1;
-        String proposal = XWPFPictureData.RELATIONS[format].getFileName(img);
+        String proposal = 
XWPFPictureData.RELATIONS[pictureType.getId()].getFileName(img);
         PackagePartName createPartName = 
PackagingURIHelper.createPartName(proposal);
         while (this.getPackage().getPart(createPartName) != null) {
             img++;
-            proposal = XWPFPictureData.RELATIONS[format].getFileName(img);
+            proposal = 
XWPFPictureData.RELATIONS[pictureType.getId()].getFileName(img);
             createPartName = PackagingURIHelper.createPartName(proposal);
         }
         return img;

Modified: 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java?rev=1902905&r1=1902904&r2=1902905&view=diff
==============================================================================
--- 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java
 (original)
+++ 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java
 Thu Jul 21 09:56:56 2022
@@ -233,14 +233,31 @@ public abstract class XWPFHeaderFooter e
      * @param format      The format of the picture.
      * @return the index to this picture (0 based), the added picture can be 
obtained from {@link #getAllPictures()} .
      * @throws InvalidFormatException If the format of the picture is not 
known.
+     * @see #addPictureData(byte[], PictureType)
      */
     public String addPictureData(byte[] pictureData, int format) throws 
InvalidFormatException {
-        XWPFPictureData xwpfPicData = 
document.findPackagePictureData(pictureData, format);
-        POIXMLRelation relDesc = XWPFPictureData.RELATIONS[format];
+        return addPictureData(pictureData, PictureType.findById(format));
+    }
+
+    /**
+     * Adds a picture to the document.
+     *
+     * @param pictureData The picture data
+     * @param pictureType The {@link PictureType} of the picture.
+     * @return the index to this picture (0 based), the added picture can be 
obtained from {@link #getAllPictures()} .
+     * @throws InvalidFormatException If the format of the picture is not 
known.
+     * @since POI 5.2.3
+     */
+    public String addPictureData(byte[] pictureData, PictureType pictureType) 
throws InvalidFormatException {
+        if (pictureType == null) {
+            throw new InvalidFormatException("pictureType parameter is 
invalid");
+        }
+        XWPFPictureData xwpfPicData = 
document.findPackagePictureData(pictureData);
+        POIXMLRelation relDesc = 
XWPFPictureData.RELATIONS[pictureType.getId()];
 
         if (xwpfPicData == null) {
             /* Part doesn't exist, create a new one */
-            int idx = document.getNextPicNameNumber(format);
+            int idx = document.getNextPicNameNumber(pictureType);
             xwpfPicData = (XWPFPictureData) createRelationship(relDesc, 
XWPFFactory.getInstance(), idx);
             /* write bytes to new part */
             PackagePart picDataPart = xwpfPicData.getPackagePart();
@@ -277,6 +294,7 @@ public abstract class XWPFHeaderFooter e
      * @return the index to this picture (0 based), the added picture can be 
obtained from {@link #getAllPictures()} .
      * @throws InvalidFormatException If the format of the picture is not 
known.
      * @throws IOException If reading the picture-data from the stream fails.
+     * @see #addPictureData(InputStream, PictureType)
      */
     public String addPictureData(InputStream is, int format) throws 
InvalidFormatException, IOException {
         byte[] data = IOUtils.toByteArrayWithMaxLength(is, 
XWPFPictureData.getMaxImageSize());
@@ -284,6 +302,21 @@ public abstract class XWPFHeaderFooter e
     }
 
     /**
+     * Adds a picture to the document.
+     *
+     * @param is     The stream to read image from
+     * @param pictureType The {@link PictureType} of the picture.
+     * @return the index to this picture (0 based), the added picture can be 
obtained from {@link #getAllPictures()} .
+     * @throws InvalidFormatException If the format of the picture is not 
known.
+     * @throws IOException If reading the picture-data from the stream fails.
+     * @since POI 5.2.3
+     */
+    public String addPictureData(InputStream is, PictureType pictureType) 
throws InvalidFormatException, IOException {
+        byte[] data = IOUtils.toByteArrayWithMaxLength(is, 
XWPFPictureData.getMaxImageSize());
+        return addPictureData(data, pictureType);
+    }
+
+    /**
      * returns the PictureData by blipID
      *
      * @return XWPFPictureData of a specificID

Modified: 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRun.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRun.java?rev=1902905&r1=1902904&r2=1902905&view=diff
==============================================================================
--- 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRun.java 
(original)
+++ 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRun.java 
Thu Jul 21 09:56:56 2022
@@ -1066,10 +1066,32 @@ public class XWPFRun implements ISDTCont
      * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_PICT
      * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_JPEG
      * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_PNG
+     * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_GIF
      * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_DIB
+     * @see #addPicture(InputStream, PictureType, String, int, int)
      */
     public XWPFPicture addPicture(InputStream pictureData, int pictureType, 
String filename, int width, int height)
             throws InvalidFormatException, IOException {
+        return addPicture(pictureData, PictureType.findById(pictureType), 
filename, width, height);
+    }
+
+    /**
+     * Adds a picture to the run. This method handles
+     * attaching the picture data to the overall file.
+     *
+     * @param pictureData The raw picture data
+     * @param pictureType The {@link PictureType} of the picture
+     * @param width       width in EMUs. To convert to / from points use 
{@link org.apache.poi.util.Units}
+     * @param height      height in EMUs. To convert to / from points use 
{@link org.apache.poi.util.Units}
+     * @throws InvalidFormatException If the format of the picture is not 
known.
+     * @throws IOException            If reading the picture-data from the 
stream fails.
+     * @since POI 5.2.3
+     */
+    public XWPFPicture addPicture(InputStream pictureData, PictureType 
pictureType, String filename, int width, int height)
+            throws InvalidFormatException, IOException {
+        if (pictureType == null) {
+            throw new InvalidFormatException("pictureType parameter is 
invalid");
+        }
         String relationId;
         XWPFPictureData picData;
 

Modified: 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSignatureLine.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSignatureLine.java?rev=1902905&r1=1902904&r2=1902905&view=diff
==============================================================================
--- 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSignatureLine.java
 (original)
+++ 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSignatureLine.java
 Thu Jul 21 09:56:56 2022
@@ -22,7 +22,6 @@ import javax.xml.namespace.QName;
 
 import com.microsoft.schemas.office.office.CTSignatureLine;
 import com.microsoft.schemas.vml.CTImageData;
-import org.apache.poi.common.usermodel.PictureType;
 import org.apache.poi.ooxml.util.XPathHelper;
 import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
 import org.apache.poi.poifs.crypt.dsig.SignatureLine;
@@ -60,30 +59,30 @@ public class XWPFSignatureLine extends S
         imageData.setId2(relId);
     }
 
-    private static int mapType(PictureType type) throws InvalidFormatException 
{
+    private static PictureType 
mapType(org.apache.poi.common.usermodel.PictureType type) throws 
InvalidFormatException {
         switch (type) {
             case BMP:
-                return Document.PICTURE_TYPE_BMP;
+                return PictureType.BMP;
             case DIB:
-                return Document.PICTURE_TYPE_DIB;
+                return PictureType.DIB;
             case EMF:
-                return Document.PICTURE_TYPE_EMF;
+                return PictureType.EMF;
             case EPS:
-                return Document.PICTURE_TYPE_EPS;
+                return PictureType.EPS;
             case GIF:
-                return Document.PICTURE_TYPE_GIF;
+                return PictureType.GIF;
             case JPEG:
-                return Document.PICTURE_TYPE_JPEG;
+                return PictureType.JPEG;
             case PICT:
-                return Document.PICTURE_TYPE_PICT;
+                return PictureType.PICT;
             case PNG:
-                return Document.PICTURE_TYPE_PNG;
+                return PictureType.PNG;
             case TIFF:
-                return Document.PICTURE_TYPE_TIFF;
+                return PictureType.TIFF;
             case WMF:
-                return Document.PICTURE_TYPE_WMF;
+                return PictureType.WMF;
             case WPG:
-                return Document.PICTURE_TYPE_WPG;
+                return PictureType.WPG;
             default:
                 throw new InvalidFormatException("Unsupported picture format 
"+type);
         }

Modified: 
poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java?rev=1902905&r1=1902904&r2=1902905&view=diff
==============================================================================
--- 
poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java
 (original)
+++ 
poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java
 Thu Jul 21 09:56:56 2022
@@ -301,7 +301,7 @@ public final class TestXWPFDocument {
     void testFindPackagePictureData() throws IOException {
         try (XWPFDocument doc = 
XWPFTestDataSamples.openSampleDocument("issue_51265_1.docx")) {
             byte[] nature1 = XWPFTestDataSamples.getImage("nature1.gif");
-            XWPFPictureData part = doc.findPackagePictureData(nature1, 
Document.PICTURE_TYPE_GIF);
+            XWPFPictureData part = doc.findPackagePictureData(nature1);
             assertNotNull(part);
             assertTrue(doc.getAllPictures().contains(part));
             assertTrue(doc.getAllPackagePictures().contains(part));

Modified: 
poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFRun.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFRun.java?rev=1902905&r1=1902904&r2=1902905&view=diff
==============================================================================
--- 
poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFRun.java
 (original)
+++ 
poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFRun.java
 Thu Jul 21 09:56:56 2022
@@ -446,6 +446,8 @@ class TestXWPFRun {
                 for (XWPFPicture pic : pictures) {
                     assertNotNull(pic.getPictureData());
                     assertEquals("DOZOR", pic.getDescription());
+                    assertEquals(5, pic.getPictureData().getPictureType());
+                    assertEquals(PictureType.JPEG, 
pic.getPictureData().getPictureTypeEnum());
                 }
 
                 count += pictures.size();



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to