diff options
author | Andreas Beeker <kiwiwings@apache.org> | 2015-08-02 20:21:35 +0000 |
---|---|---|
committer | Andreas Beeker <kiwiwings@apache.org> | 2015-08-02 20:21:35 +0000 |
commit | ff4bbd958815ecbb3bb38c8a45071947a8d3f43b (patch) | |
tree | 0e994450055da44dc14a578faf70f5c1a912432b /src/ooxml/java | |
parent | 74b13dee22ed6606a829c31450da5e83ad075fd9 (diff) | |
download | poi-ff4bbd958815ecbb3bb38c8a45071947a8d3f43b.tar.gz poi-ff4bbd958815ecbb3bb38c8a45071947a8d3f43b.zip |
#58190 - The current picture handling uses raw integers for types and index, replace with enum and reference
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1693825 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/ooxml/java')
6 files changed, 160 insertions, 219 deletions
diff --git a/src/ooxml/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java b/src/ooxml/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java index ae2ed3a09c..ecfa5cd625 100644 --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java +++ b/src/ooxml/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java @@ -36,6 +36,7 @@ import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.openxml4j.opc.PackagePartName; import org.apache.poi.openxml4j.opc.TargetMode; import org.apache.poi.sl.usermodel.MasterSheet; +import org.apache.poi.sl.usermodel.PictureData.PictureType; import org.apache.poi.sl.usermodel.Resources; import org.apache.poi.sl.usermodel.SlideShow; import org.apache.poi.util.Beta; @@ -192,7 +193,9 @@ public class XMLSlideShow extends POIXMLDocument implements SlideShow { List<PackagePart> mediaParts = getPackage().getPartsByName(Pattern.compile("/ppt/media/.*?")); _pictures = new ArrayList<XSLFPictureData>(mediaParts.size()); for(PackagePart part : mediaParts){ - _pictures.add(new XSLFPictureData(part, null)); + XSLFPictureData pd = new XSLFPictureData(part, null); + pd.setIndex(_pictures.size()); + _pictures.add(pd); } } return Collections.unmodifiableList(_pictures); @@ -442,26 +445,28 @@ public class XMLSlideShow extends POIXMLDocument implements SlideShow { * @see XSLFPictureData#PICTURE_TYPE_PNG * @see XSLFPictureData#PICTURE_TYPE_DIB */ - public int addPicture(byte[] pictureData, int format) { + public XSLFPictureData addPicture(byte[] pictureData, PictureType format) { XSLFPictureData img = findPictureData(pictureData); - // POIXMLRelation relDesc = XSLFPictureData.RELATIONS[format]; - - if(img == null) { - int imageNumber = _pictures.size(); - img = (XSLFPictureData) createRelationship( - XSLFPictureData.RELATIONS[format], XSLFFactory.getInstance(), imageNumber + 1, true); - _pictures.add(img); - try { - OutputStream out = img.getPackagePart().getOutputStream(); - out.write(pictureData); - out.close(); - } catch (IOException e) { - throw new POIXMLException(e); - } - return _pictures.size() - 1; - } else { - return _pictures.indexOf(img); + + if (img != null) return img; + + int imageNumber = _pictures.size(); + XSLFRelation relType = XSLFPictureData.getRelationForType(format); + if (relType == null) { + throw new IllegalArgumentException("Picture type "+format+" is not supported."); } + img = (XSLFPictureData) createRelationship(relType, XSLFFactory.getInstance(), imageNumber + 1, true); + img.setIndex(imageNumber); + _pictures.add(img); + try { + OutputStream out = img.getPackagePart().getOutputStream(); + out.write(pictureData); + out.close(); + } catch (IOException e) { + throw new POIXMLException(e); + } + + return img; } /** @@ -492,7 +497,7 @@ public class XMLSlideShow extends POIXMLDocument implements SlideShow { return null; } - public MasterSheet createMasterSheet() throws IOException { + public MasterSheet<XSLFShape, XMLSlideShow> createMasterSheet() throws IOException { // TODO: implement! throw new UnsupportedOperationException(); } diff --git a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFGroupShape.java b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFGroupShape.java index 4fc9e0f748..c2fb36f885 100644 --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFGroupShape.java +++ b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFGroupShape.java @@ -23,7 +23,6 @@ import java.awt.geom.Rectangle2D; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
-import java.util.regex.Pattern;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackageRelationship;
@@ -239,16 +238,8 @@ public class XSLFGroupShape extends XSLFShape implements XSLFShapeContainer, Gro return sh;
}
- public XSLFPictureShape createPicture(int pictureIndex){
-
- List<PackagePart> pics = getSheet().getPackagePart().getPackage()
- .getPartsByName(Pattern.compile("/ppt/media/image" + (pictureIndex + 1) + ".*?"));
-
- if(pics.size() == 0) {
- throw new IllegalArgumentException("Picture with index=" + pictureIndex + " was not found");
- }
-
- PackagePart pic = pics.get(0);
+ public XSLFPictureShape createPicture(XSLFPictureData pictureData){
+ PackagePart pic = pictureData.getPackagePart();
PackageRelationship rel = getSheet().getPackagePart().addRelationship(
pic.getPartName(), TargetMode.INTERNAL, XSLFRelation.IMAGES.getRelation());
@@ -321,8 +312,8 @@ public class XSLFGroupShape extends XSLFShape implements XSLFShapeContainer, Gro } else if (shape instanceof XSLFPictureShape) {
XSLFPictureShape p = (XSLFPictureShape)shape;
XSLFPictureData pd = p.getPictureData();
- int picId = getSheet().getSlideShow().addPicture(pd.getData(), pd.getPictureType());
- newShape = createPicture(picId);
+ XSLFPictureData pdNew = getSheet().getSlideShow().addPicture(pd.getData(), pd.getType());
+ newShape = createPicture(pdNew);
} else if (shape instanceof XSLFGroupShape) {
newShape = createGroup();
} else if (shape instanceof XSLFTable) {
diff --git a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFPictureData.java b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFPictureData.java index 8e20be90b3..e6fc0fd4ef 100644 --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFPictureData.java +++ b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFPictureData.java @@ -25,7 +25,6 @@ import java.io.OutputStream; import org.apache.poi.POIXMLDocumentPart; import org.apache.poi.POIXMLException; -import org.apache.poi.POIXMLRelation; import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.openxml4j.opc.PackageRelationship; import org.apache.poi.sl.usermodel.PictureData; @@ -34,94 +33,11 @@ import org.apache.poi.util.IOUtils; /** * Instantiates sub-classes of POIXMLDocumentPart depending on their relationship type - * - * @author Yegor Kozlov */ @Beta public final class XSLFPictureData extends POIXMLDocumentPart implements PictureData { - /** - * Extended windows meta file - */ - public static final int PICTURE_TYPE_EMF = 2; - - /** - * Windows Meta File - */ - public static final int PICTURE_TYPE_WMF = 3; - - /** - * Mac PICT format - */ - public static final int PICTURE_TYPE_PICT = 4; - - /** - * JPEG format - */ - public static final int PICTURE_TYPE_JPEG = 5; - - /** - * PNG format - */ - public static final int PICTURE_TYPE_PNG = 6; - - /** - * Device independent bitmap - */ - public static final int PICTURE_TYPE_DIB = 7; - - /** - * GIF image format - */ - public static final int PICTURE_TYPE_GIF = 8; - - /** - * Tag Image File (.tiff) - */ - public static final int PICTURE_TYPE_TIFF = 9; - - /** - * Encapsulated Postscript (.eps) - */ - public static final int PICTURE_TYPE_EPS = 10; - - - /** - * Windows Bitmap (.bmp) - */ - public static final int PICTURE_TYPE_BMP = 11; - - /** - * WordPerfect graphics (.wpg) - */ - public static final int PICTURE_TYPE_WPG = 12; - - /** - * Microsoft Windows Media Photo image (.wdp) - */ - public static final int PICTURE_TYPE_WDP = 13; - - /** - * Relationships for each known picture type - */ - protected static final POIXMLRelation[] RELATIONS; - - static { - RELATIONS = new POIXMLRelation[14]; - RELATIONS[PICTURE_TYPE_EMF] = XSLFRelation.IMAGE_EMF; - RELATIONS[PICTURE_TYPE_WMF] = XSLFRelation.IMAGE_WMF; - RELATIONS[PICTURE_TYPE_PICT] = XSLFRelation.IMAGE_PICT; - RELATIONS[PICTURE_TYPE_JPEG] = XSLFRelation.IMAGE_JPEG; - RELATIONS[PICTURE_TYPE_PNG] = XSLFRelation.IMAGE_PNG; - RELATIONS[PICTURE_TYPE_DIB] = XSLFRelation.IMAGE_DIB; - RELATIONS[PICTURE_TYPE_GIF] = XSLFRelation.IMAGE_GIF; - RELATIONS[PICTURE_TYPE_TIFF] = XSLFRelation.IMAGE_TIFF; - RELATIONS[PICTURE_TYPE_EPS] = XSLFRelation.IMAGE_EPS; - RELATIONS[PICTURE_TYPE_BMP] = XSLFRelation.IMAGE_BMP; - RELATIONS[PICTURE_TYPE_WPG] = XSLFRelation.IMAGE_WPG; - RELATIONS[PICTURE_TYPE_WDP] = XSLFRelation.IMAGE_WDP; - } - private Long checksum = null; + private int index = -1; /** * Create a new XSLFGraphicData node @@ -150,12 +66,12 @@ public final class XSLFPictureData extends POIXMLDocumentPart implements Picture public InputStream getInputStream() throws IOException { return getPackagePart().getInputStream(); } - + /** * Gets the picture data as a byte array. * * You can grab the picture data directly from the underlying package part with the {@link #getInputStream()} method - * + * * @return the Picture data. */ public byte[] getData() { @@ -187,25 +103,6 @@ public final class XSLFPictureData extends POIXMLDocumentPart implements Picture return getPackagePart().getPartName().getExtension(); } - /** - * Return an integer constant that specifies type of this picture - * - * @return an integer constant that specifies type of this picture - */ - public int getPictureType() { - String contentType = getPackagePart().getContentType(); - for (int i = 0; i < RELATIONS.length; i++) { - if (RELATIONS[i] == null) { - continue; - } - - if (RELATIONS[i].getContentType().equals(contentType)) { - return i; - } - } - return 0; - } - long getChecksum(){ if(checksum == null){ try { @@ -218,17 +115,17 @@ public final class XSLFPictureData extends POIXMLDocumentPart implements Picture } /** - * *PictureData objects store the actual content in the part directly without keeping a + * *PictureData objects store the actual content in the part directly without keeping a * copy like all others therefore we need to handle them differently. */ @Override protected void prepareForCommit() { // do not clear the part here } - + + @Override public String getContentType() { - POIXMLRelation rel = RELATIONS[getPictureType()]; - return (rel == null) ? null : rel.getContentType(); + return getPackagePart().getContentType(); } public void setData(byte[] data) throws IOException { @@ -238,6 +135,65 @@ public final class XSLFPictureData extends POIXMLDocumentPart implements Picture // recalculate now since we already have the data bytes available anyhow checksum = IOUtils.calculateChecksum(data); } + + @Override + public PictureType getType() { + String ct = getContentType(); + if (XSLFRelation.IMAGE_EMF.getContentType().equals(ct)) { + return PictureType.EMF; + } else if (XSLFRelation.IMAGE_WMF.getContentType().equals(ct)) { + return PictureType.WMF; + } else if (XSLFRelation.IMAGE_PICT.getContentType().equals(ct)) { + return PictureType.PICT; + } else if (XSLFRelation.IMAGE_JPEG.getContentType().equals(ct)) { + return PictureType.JPEG; + } else if (XSLFRelation.IMAGE_PNG.getContentType().equals(ct)) { + return PictureType.PNG; + } else if (XSLFRelation.IMAGE_DIB.getContentType().equals(ct)) { + return PictureType.DIB; + } else if (XSLFRelation.IMAGE_GIF.getContentType().equals(ct)) { + return PictureType.GIF; + } else if (XSLFRelation.IMAGE_EPS.getContentType().equals(ct)) { + return PictureType.EPS; + } else if (XSLFRelation.IMAGE_BMP.getContentType().equals(ct)) { + return PictureType.BMP; + } else if (XSLFRelation.IMAGE_WPG.getContentType().equals(ct)) { + return PictureType.WPG; + } else if (XSLFRelation.IMAGE_WDP.getContentType().equals(ct)) { + return PictureType.WDP; + } else { + return null; + } + } - + /* package */ static XSLFRelation getRelationForType(PictureType pt) { + switch (pt) { + case EMF: return XSLFRelation.IMAGE_EMF; + case WMF: return XSLFRelation.IMAGE_WMF; + case PICT: return XSLFRelation.IMAGE_PICT; + case JPEG: return XSLFRelation.IMAGE_JPEG; + case PNG: return XSLFRelation.IMAGE_PNG; + case DIB: return XSLFRelation.IMAGE_DIB; + case GIF: return XSLFRelation.IMAGE_GIF; + case EPS: return XSLFRelation.IMAGE_EPS; + case BMP: return XSLFRelation.IMAGE_BMP; + case WPG: return XSLFRelation.IMAGE_WPG; + case WDP: return XSLFRelation.IMAGE_WDP; + default: return null; + } + } + + /** + * @return the 0-based index of this pictures within the picture parts + */ + public int getIndex() { + return index; + } + + /** + * @param index sets the 0-based index of this pictures within the picture parts + */ + public void setIndex(int index) { + this.index = index; + } }
\ No newline at end of file diff --git a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFRelation.java b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFRelation.java index 82e073167f..b2e3ff2bbc 100644 --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFRelation.java +++ b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFRelation.java @@ -21,15 +21,12 @@ import java.util.Map; import org.apache.poi.POIXMLDocumentPart; import org.apache.poi.POIXMLRelation; +import org.apache.poi.sl.usermodel.PictureData.PictureType; import org.apache.poi.util.Beta; -import org.apache.poi.util.POILogFactory; -import org.apache.poi.util.POILogger; @Beta public class XSLFRelation extends POIXMLRelation { - private static final POILogger log = POILogFactory.getLogger(XSLFRelation.class); - /** * A map to lookup POIXMLRelation by its relation type */ @@ -148,76 +145,76 @@ public class XSLFRelation extends POIXMLRelation { ); public static final XSLFRelation IMAGE_EMF = new XSLFRelation( - "image/x-emf", + PictureType.EMF.contentType, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", "/ppt/media/image#.emf", XSLFPictureData.class - ); - public static final XSLFRelation IMAGE_WMF = new XSLFRelation( - "image/x-wmf", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", - "/ppt/media/image#.wmf", - XSLFPictureData.class - ); - public static final XSLFRelation IMAGE_PICT = new XSLFRelation( - "image/pict", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", - "/ppt/media/image#.pict", - XSLFPictureData.class - ); - public static final XSLFRelation IMAGE_JPEG = new XSLFRelation( - "image/jpeg", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", - "/ppt/media/image#.jpeg", - XSLFPictureData.class - ); - public static final XSLFRelation IMAGE_PNG = new XSLFRelation( - "image/png", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", - "/ppt/media/image#.png", - XSLFPictureData.class - ); - public static final XSLFRelation IMAGE_DIB = new XSLFRelation( - "image/dib", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", - "/ppt/media/image#.dib", - XSLFPictureData.class - ); - public static final XSLFRelation IMAGE_GIF = new XSLFRelation( - "image/gif", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", - "/ppt/media/image#.gif", - XSLFPictureData.class - ); + ); + public static final XSLFRelation IMAGE_WMF = new XSLFRelation( + PictureType.WMF.contentType, + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", + "/ppt/media/image#.wmf", + XSLFPictureData.class + ); + public static final XSLFRelation IMAGE_PICT = new XSLFRelation( + PictureType.PICT.contentType, + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", + "/ppt/media/image#.pict", + XSLFPictureData.class + ); + public static final XSLFRelation IMAGE_JPEG = new XSLFRelation( + PictureType.JPEG.contentType, + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", + "/ppt/media/image#.jpeg", + XSLFPictureData.class + ); + public static final XSLFRelation IMAGE_PNG = new XSLFRelation( + PictureType.PNG.contentType, + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", + "/ppt/media/image#.png", + XSLFPictureData.class + ); + public static final XSLFRelation IMAGE_DIB = new XSLFRelation( + PictureType.DIB.contentType, + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", + "/ppt/media/image#.dib", + XSLFPictureData.class + ); + public static final XSLFRelation IMAGE_GIF = new XSLFRelation( + PictureType.GIF.contentType, + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", + "/ppt/media/image#.gif", + XSLFPictureData.class + ); public static final XSLFRelation IMAGE_TIFF = new XSLFRelation( - "image/tiff", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", - "/ppt/media/image#.tiff", - XSLFPictureData.class + PictureType.TIFF.contentType, + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", + "/ppt/media/image#.tiff", + XSLFPictureData.class ); public static final XSLFRelation IMAGE_EPS = new XSLFRelation( - "image/x-eps", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", - "/ppt/media/image#.eps", - XSLFPictureData.class + PictureType.EPS.contentType, + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", + "/ppt/media/image#.eps", + XSLFPictureData.class ); public static final XSLFRelation IMAGE_BMP = new XSLFRelation( - "image/x-ms-bmp", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", - "/ppt/media/image#.bmp", - XSLFPictureData.class + PictureType.BMP.contentType, + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", + "/ppt/media/image#.bmp", + XSLFPictureData.class ); public static final XSLFRelation IMAGE_WPG = new XSLFRelation( - "image/x-wpg", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", - "/ppt/media/image#.wpg", - XSLFPictureData.class + PictureType.WPG.contentType, + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", + "/ppt/media/image#.wpg", + XSLFPictureData.class ); public static final XSLFRelation IMAGE_WDP = new XSLFRelation( - "image/vnd.ms-photo", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", - "/ppt/media/image#.wdp", - XSLFPictureData.class + PictureType.WDP.contentType, + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", + "/ppt/media/image#.wdp", + XSLFPictureData.class ); public static final XSLFRelation IMAGES = new XSLFRelation( diff --git a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFShapeContainer.java b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFShapeContainer.java index e1d00c58d4..f7078029b6 100644 --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFShapeContainer.java +++ b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFShapeContainer.java @@ -55,7 +55,7 @@ public interface XSLFShapeContainer extends ShapeContainer<XSLFShape> { /** * create a picture belonging to this container */ - XSLFPictureShape createPicture(int pictureIndex); + XSLFPictureShape createPicture(XSLFPictureData pictureData); /** * Removes all of the elements from this container (optional operation). diff --git a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSheet.java b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSheet.java index 9549baf469..fb6ea5446c 100644 --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSheet.java +++ b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSheet.java @@ -25,7 +25,6 @@ import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.regex.Pattern; import javax.xml.namespace.QName; @@ -182,15 +181,8 @@ public abstract class XSLFSheet extends POIXMLDocumentPart implements XSLFShapeC return sh; } - public XSLFPictureShape createPicture(int pictureIndex){ - List<PackagePart> pics = getPackagePart().getPackage() - .getPartsByName(Pattern.compile("/ppt/media/image" + (pictureIndex + 1) + ".*?")); - - if(pics.size() == 0) { - throw new IllegalArgumentException("Picture with index=" + pictureIndex + " was not found"); - } - - PackagePart pic = pics.get(0); + public XSLFPictureShape createPicture(XSLFPictureData pictureData){ + PackagePart pic = pictureData.getPackagePart(); PackageRelationship rel = getPackagePart().addRelationship( pic.getPartName(), TargetMode.INTERNAL, XSLFRelation.IMAGES.getRelation()); @@ -525,8 +517,8 @@ public abstract class XSLFSheet extends POIXMLDocumentPart implements XSLFShapeC XSLFPictureData data = new XSLFPictureData(blipPart, null); XMLSlideShow ppt = getSlideShow(); - int pictureIdx = ppt.addPicture(data.getData(), data.getPictureType()); - PackagePart pic = ppt.getAllPictures().get(pictureIdx).getPackagePart(); + XSLFPictureData pictureData = ppt.addPicture(data.getData(), data.getType()); + PackagePart pic = pictureData.getPackagePart(); PackageRelationship rel = getPackagePart().addRelationship( pic.getPartName(), TargetMode.INTERNAL, blipRel.getRelationshipType()); |