diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFPictureData.java b/src/java/org/apache/poi/hssf/usermodel/HSSFPictureData.java index 439100a53a..8607e5cf7b 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFPictureData.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFPictureData.java @@ -17,15 +17,15 @@ package org.apache.poi.hssf.usermodel; -import org.apache.poi.ddf.EscherBitmapBlip; import org.apache.poi.ddf.EscherBlipRecord; +import org.apache.poi.ss.usermodel.PictureData; /** * Represents binary data stored in the file. Eg. A GIF, JPEG etc... * * @author Daniel Noll */ -public class HSSFPictureData +public class HSSFPictureData implements PictureData { // MSOBI constants for various formats. public static final short MSOBI_WMF = 0x2160; @@ -52,20 +52,16 @@ public class HSSFPictureData this.blip = blip; } - /** - * Gets the picture data. - * - * @return the picture data. + /* (non-Javadoc) + * @see org.apache.poi.hssf.usermodel.PictureData#getData() */ public byte[] getData() { return blip.getPicturedata(); } - /** - * Suggests a file extension for this image. - * - * @return the file extension. + /* (non-Javadoc) + * @see org.apache.poi.hssf.usermodel.PictureData#suggestFileExtension() */ public String suggestFileExtension() { diff --git a/src/ooxml/interfaces-jdk14/org/apache/poi/ss/usermodel/PictureData.java b/src/ooxml/interfaces-jdk14/org/apache/poi/ss/usermodel/PictureData.java new file mode 100644 index 0000000000..a38a70a02a --- /dev/null +++ b/src/ooxml/interfaces-jdk14/org/apache/poi/ss/usermodel/PictureData.java @@ -0,0 +1,20 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.ss.usermodel; + +public interface PictureData {} \ No newline at end of file diff --git a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/PictureData.java b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/PictureData.java new file mode 100644 index 0000000000..a799de88eb --- /dev/null +++ b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/PictureData.java @@ -0,0 +1,36 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.ss.usermodel; + +public interface PictureData { + + /** + * Gets the picture data. + * + * @return the picture data. + */ + byte[] getData(); + + /** + * Suggests a file extension for this image. + * + * @return the file extension. + */ + String suggestFileExtension(); + +} \ No newline at end of file diff --git a/src/ooxml/java/org/apache/poi/POIXMLDocument.java b/src/ooxml/java/org/apache/poi/POIXMLDocument.java index f84418545a..6f8a7e37d8 100644 --- a/src/ooxml/java/org/apache/poi/POIXMLDocument.java +++ b/src/ooxml/java/org/apache/poi/POIXMLDocument.java @@ -65,7 +65,14 @@ public abstract class POIXMLDocument { return this.corePart; } - protected PackagePart getPart(PackageRelationship rel) throws InvalidFormatException { + /** + * Get the PackagePart that is the target of a relationship. + * + * @param rel The relationship + * @return The target part + * @throws InvalidFormatException + */ + protected PackagePart getTargetPart(PackageRelationship rel) throws InvalidFormatException { PackagePartName relName = PackagingURIHelper.createPartName(rel.getTargetURI()); PackagePart part = getPackage().getPart(relName); if (part == null) { diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPictureData.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPictureData.java new file mode 100644 index 0000000000..91ed5f8658 --- /dev/null +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPictureData.java @@ -0,0 +1,42 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.xssf.usermodel; + +import org.apache.poi.ss.usermodel.PictureData; +import org.openxml4j.opc.PackagePart; + + +public class XSSFPictureData implements PictureData { + + private PackagePart packagePart; + + public XSSFPictureData(PackagePart packagePart) { + this.packagePart = packagePart; + } + + public byte[] getData() { + // TODO Auto-generated method stub + return null; + } + + public String suggestFileExtension() { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java index c1e0c05af7..dfded4ae73 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -17,6 +17,7 @@ package org.apache.poi.xssf.usermodel; +import java.util.Collection; import java.util.Iterator; import java.util.LinkedList; import java.util.List; @@ -28,6 +29,7 @@ import org.apache.poi.ss.usermodel.Comment; import org.apache.poi.ss.usermodel.Footer; import org.apache.poi.ss.usermodel.Header; import org.apache.poi.ss.usermodel.Patriarch; +import org.apache.poi.ss.usermodel.PictureData; import org.apache.poi.ss.usermodel.PrintSetup; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java index 4d261a4b31..bc4d943cd6 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -31,6 +31,7 @@ import org.apache.poi.ss.usermodel.DataFormat; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.Name; import org.apache.poi.ss.usermodel.Palette; +import org.apache.poi.ss.usermodel.PictureData; import org.apache.poi.ss.usermodel.SharedStringSource; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; @@ -66,6 +67,10 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { private static final String SHARED_STRINGS_RELATIONSHIP = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings"; + private static final String DRAWING_RELATIONSHIP = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing"; + + private static final String IMAGE_RELATIONSHIP = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"; + private CTWorkbook workbook; private List sheets = new LinkedList(); @@ -92,17 +97,15 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { Iterator it = prc.iterator(); if (it.hasNext()) { PackageRelationship rel = it.next(); - PackagePart part = getPart(rel); + PackagePart part = getTargetPart(rel); this.sharedStringSource = new SharedStringsTable(part); } // Load individual sheets for (CTSheet ctSheet : this.workbook.getSheets().getSheetArray()) { - PackageRelationship rel = this.getCorePart().getRelationship(ctSheet.getId()); - if (rel == null) { - log.log(log.WARN, "No relationship found for sheet " + ctSheet.getId()); + PackagePart part = getPackagePart(ctSheet); + if (part == null) { continue; } - PackagePart part = getPart(rel); WorksheetDocument worksheetDoc = WorksheetDocument.Factory.parse(part.getInputStream()); XSSFSheet sheet = new XSSFSheet(ctSheet, worksheetDoc.getWorksheet(), this); this.sheets.add(sheet); @@ -117,6 +120,22 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { protected CTWorkbook getWorkbook() { return this.workbook; } + + /** + * Get the PackagePart corresponding to a given sheet. + * + * @param ctSheet The sheet + * @return A PackagePart, or null if no matching part found. + * @throws InvalidFormatException + */ + private PackagePart getPackagePart(CTSheet ctSheet) throws InvalidFormatException { + PackageRelationship rel = this.getCorePart().getRelationship(ctSheet.getId()); + if (rel == null) { + log.log(POILogger.WARN, "No relationship found for sheet " + ctSheet.getId()); + return null; + } + return getTargetPart(rel); + } public int addPicture(byte[] pictureData, int format) { // TODO Auto-generated method stub @@ -209,9 +228,30 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { return null; } - public List getAllPictures() { - // TODO Auto-generated method stub - return null; + public List getAllPictures() { + // In OOXML pictures are referred to in sheets + List pictures = new LinkedList(); + for (CTSheet ctSheet : this.workbook.getSheets().getSheetArray()) { + try { + PackagePart sheetPart = getPackagePart(ctSheet); + if (sheetPart == null) { + continue; + } + PackageRelationshipCollection prc = sheetPart.getRelationshipsByType(DRAWING_RELATIONSHIP); + for (PackageRelationship rel : prc) { + PackagePart drawingPart = getTargetPart(rel); + PackageRelationshipCollection prc2 = drawingPart.getRelationshipsByType(IMAGE_RELATIONSHIP); + for (PackageRelationship rel2 : prc2) { + PackagePart imagePart = getTargetPart(rel2); + XSSFPictureData pd = new XSSFPictureData(imagePart); + pictures.add(pd); + } + } + } catch (InvalidFormatException e) { + throw new RuntimeException(e.getMessage(), e); + } + } + return pictures; } public boolean getBackupFlag() { diff --git a/src/ooxml/testcases/org/apache/poi/xssf/data/picture.xlsx b/src/ooxml/testcases/org/apache/poi/xssf/data/picture.xlsx new file mode 100644 index 0000000000..9cdec42562 Binary files /dev/null and b/src/ooxml/testcases/org/apache/poi/xssf/data/picture.xlsx differ diff --git a/src/ooxml/testcases/org/apache/poi/xssf/io/TestLoadSaveXSSF.java b/src/ooxml/testcases/org/apache/poi/xssf/io/TestLoadSaveXSSF.java index ec2b113b86..ff7f0ff47c 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/io/TestLoadSaveXSSF.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/io/TestLoadSaveXSSF.java @@ -18,11 +18,13 @@ package org.apache.poi.xssf.io; import java.io.File; +import java.util.List; import junit.framework.TestCase; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.PictureData; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; @@ -62,5 +64,11 @@ public class TestLoadSaveXSSF extends TestCase { CellStyle style = cell.getCellStyle(); // assertNotNull(style); } + + public void testLoadPictures() throws Exception { + XSSFWorkbook workbook = new XSSFWorkbook(new File(filename, "picture.xlsx").getAbsolutePath()); + List pictures = workbook.getAllPictures(); + assertEquals(1, pictures.size()); + } }