diff options
author | Nick Burch <nick@apache.org> | 2008-07-18 18:22:25 +0000 |
---|---|---|
committer | Nick Burch <nick@apache.org> | 2008-07-18 18:22:25 +0000 |
commit | c7f924e3773c18640262ba073335f5cf1df2548b (patch) | |
tree | a18f6151c028e96d197c60ed5e221603c30ceece /src/ooxml/java/org/apache | |
parent | 6c0afcaed52996c0b56312603925d6e16b16d340 (diff) | |
download | poi-c7f924e3773c18640262ba073335f5cf1df2548b.tar.gz poi-c7f924e3773c18640262ba073335f5cf1df2548b.zip |
Partial support for .xlsm files (bug #45431), but still not quite there as they seem to have some hidden reference we don't know about to update
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@677990 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/ooxml/java/org/apache')
3 files changed, 116 insertions, 3 deletions
diff --git a/src/ooxml/java/org/apache/poi/xssf/model/BinaryPart.java b/src/ooxml/java/org/apache/poi/xssf/model/BinaryPart.java new file mode 100644 index 0000000000..9582b8a61d --- /dev/null +++ b/src/ooxml/java/org/apache/poi/xssf/model/BinaryPart.java @@ -0,0 +1,67 @@ +/* ==================================================================== + 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.model; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +/** + * An implementation of XSSFModel for binary parts of + * the file, eg images or vba macros + */ +public class BinaryPart implements XSSFModel { + private byte[] data; + + public BinaryPart(InputStream in) throws IOException { + readFrom(in); + } + + /** + * Fetch the contents of the binary part + */ + public byte[] getContents() { + return data; + } + /** + * Changes the contents of the binary part + */ + public void setContents(byte[] data) { + this.data = data; + } + + /** + * Reads the contents of the binary part in. + */ + public void readFrom(InputStream is) throws IOException { + int read = 0; + byte[] buffer = new byte[4096]; + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + while( (read = is.read(buffer)) != -1 ) { + if(read > 0) { + baos.write(buffer, 0, read); + } + } + data = baos.toByteArray(); + } + + public void writeTo(OutputStream out) throws IOException { + out.write(data); + } +} diff --git a/src/ooxml/java/org/apache/poi/xssf/model/XSSFModel.java b/src/ooxml/java/org/apache/poi/xssf/model/XSSFModel.java index eedf391179..03d08c030f 100644 --- a/src/ooxml/java/org/apache/poi/xssf/model/XSSFModel.java +++ b/src/ooxml/java/org/apache/poi/xssf/model/XSSFModel.java @@ -20,6 +20,15 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import org.apache.poi.xssf.usermodel.XSSFWorkbook.XSSFRelation; + +/** + * Common interface for XSSF models, which deal with + * parts of the xssf file. + * These should also implement a constructor of + * (InputStream is), so they can be used with + * {@link XSSFRelation} + */ public interface XSSFModel { /** Read from the given InputStream */ public void readFrom(InputStream is) throws IOException; 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 719fc63a1d..c98135d585 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -44,6 +44,7 @@ import org.apache.poi.ss.usermodel.Row.MissingCellPolicy; import org.apache.poi.ss.util.SheetReferences; import org.apache.poi.util.POILogFactory; import org.apache.poi.util.POILogger; +import org.apache.poi.xssf.model.BinaryPart; import org.apache.poi.xssf.model.CommentsTable; import org.apache.poi.xssf.model.SharedStringsTable; import org.apache.poi.xssf.model.StylesTable; @@ -125,14 +126,19 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { null, OLE_OBJECT_REL_TYPE, null, - null + BinaryPart.class ); - public static final XSSFRelation PACKEMBEDDINGS = new XSSFRelation( null, PACK_OBJECT_REL_TYPE, null, - null + BinaryPart.class + ); + public static final XSSFRelation VBA_MACROS = new XSSFRelation( + "application/vnd.ms-office.vbaProject", + "http://schemas.microsoft.com/office/2006/relationships/vbaProject", + "/xl/vbaProject.bin", + BinaryPart.class ); @@ -152,6 +158,26 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { public String getDefaultFileName() { return DEFAULT_NAME; } /** + * Does one of these exist for the given core + * package part? + */ + public boolean exists(PackagePart corePart) throws IOException, InvalidFormatException { + if(corePart == null) { + // new file, can't exist + return false; + } + + PackageRelationshipCollection prc = + corePart.getRelationshipsByType(REL); + Iterator<PackageRelationship> it = prc.iterator(); + if(it.hasNext()) { + return true; + } else { + return false; + } + } + + /** * Returns the filename for the nth one of these, * eg /xl/comments4.xml */ @@ -813,6 +839,17 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { workbook.setDefinedNames(null); } } + + // VBA Macros + if(VBA_MACROS.exists( getCorePart() )) { + // Copy over + try { + XSSFModel vba = VBA_MACROS.load(getCorePart()); + VBA_MACROS.save(vba, corePart); + } catch(Exception e) { + throw new RuntimeException("Unable to copy vba macros over", e); + } + } // Now we can write out the main Workbook, with // the correct references to the other parts |