| 1 | /* |
|---|
| 2 | * Copyright 2007-2008, Plutext Pty Ltd. |
|---|
| 3 | * |
|---|
| 4 | * This file is part of docx4j. |
|---|
| 5 | |
|---|
| 6 | docx4j is licensed under the Apache License, Version 2.0 (the "License"); |
|---|
| 7 | you may not use this file except in compliance with the License. |
|---|
| 8 | |
|---|
| 9 | You may obtain a copy of the License at |
|---|
| 10 | |
|---|
| 11 | http://www.apache.org/licenses/LICENSE-2.0 |
|---|
| 12 | |
|---|
| 13 | Unless required by applicable law or agreed to in writing, software |
|---|
| 14 | distributed under the License is distributed on an "AS IS" BASIS, |
|---|
| 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|---|
| 16 | See the License for the specific language governing permissions and |
|---|
| 17 | limitations under the License. |
|---|
| 18 | |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | package org.docx4j.openpackaging.parts.WordprocessingML; |
|---|
| 22 | |
|---|
| 23 | import java.io.ByteArrayInputStream; |
|---|
| 24 | import java.io.ByteArrayOutputStream; |
|---|
| 25 | import java.io.FileInputStream; |
|---|
| 26 | import java.io.IOException; |
|---|
| 27 | import java.io.OutputStream; |
|---|
| 28 | import java.nio.ByteBuffer; |
|---|
| 29 | import java.util.Iterator; |
|---|
| 30 | import java.util.List; |
|---|
| 31 | |
|---|
| 32 | import org.apache.log4j.Logger; |
|---|
| 33 | import org.apache.poi.poifs.dev.POIFSViewEngine; |
|---|
| 34 | import org.apache.poi.poifs.filesystem.DocumentInputStream; |
|---|
| 35 | import org.apache.poi.poifs.filesystem.POIFSFileSystem; |
|---|
| 36 | import org.docx4j.openpackaging.exceptions.InvalidFormatException; |
|---|
| 37 | import org.docx4j.openpackaging.parts.PartName; |
|---|
| 38 | import org.docx4j.openpackaging.parts.relationships.Namespaces; |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | public class OleObjectBinaryPart extends BinaryPart { |
|---|
| 42 | |
|---|
| 43 | private static Logger log = Logger.getLogger(OleObjectBinaryPart.class); |
|---|
| 44 | |
|---|
| 45 | public OleObjectBinaryPart(PartName partName) throws InvalidFormatException { |
|---|
| 46 | super(partName); |
|---|
| 47 | init(); |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | public OleObjectBinaryPart() throws InvalidFormatException { |
|---|
| 52 | super( new PartName("/word/embeddings/oleObject1.bin") ); |
|---|
| 53 | init(); |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | public void init() { |
|---|
| 57 | // Used if this Part is added to [Content_Types].xml |
|---|
| 58 | setContentType(new org.docx4j.openpackaging.contenttype.ContentType( |
|---|
| 59 | org.docx4j.openpackaging.contenttype.ContentTypes.OFFICEDOCUMENT_OLE_OBJECT)); |
|---|
| 60 | |
|---|
| 61 | // Used when this Part is added to a rels |
|---|
| 62 | setRelationshipType(Namespaces.OLE_OBJECT); |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | POIFSFileSystem fs; |
|---|
| 68 | public POIFSFileSystem getFs() { |
|---|
| 69 | return fs; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | public void initPOIFSFileSystem() throws IOException { |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | //fs = new POIFSFileSystem( org.docx4j.utils.BufferUtil.newInputStream(bb) ); |
|---|
| 76 | |
|---|
| 77 | // the above seems to be calling methods which aren't implemented, |
|---|
| 78 | // so, for now, brute force.. |
|---|
| 79 | |
|---|
| 80 | bb.clear(); |
|---|
| 81 | byte[] bytes = new byte[bb.capacity()]; |
|---|
| 82 | bb.get(bytes, 0, bytes.length); |
|---|
| 83 | |
|---|
| 84 | ByteArrayInputStream bais = new ByteArrayInputStream(bytes); |
|---|
| 85 | fs = new POIFSFileSystem(bais); |
|---|
| 86 | |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | public void writePOIFSFileSystem() throws IOException { |
|---|
| 90 | |
|---|
| 91 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
|---|
| 92 | |
|---|
| 93 | fs.writeFilesystem(baos); |
|---|
| 94 | |
|---|
| 95 | // Need to put this is bb |
|---|
| 96 | byte[] bytes = baos.toByteArray(); |
|---|
| 97 | |
|---|
| 98 | bb = ByteBuffer.wrap(bytes); |
|---|
| 99 | |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | // java.nio.ByteBuffer bb contains the data |
|---|
| 103 | |
|---|
| 104 | public void viewFile(boolean verbose) throws IOException |
|---|
| 105 | { |
|---|
| 106 | String indent=""; |
|---|
| 107 | boolean withSizes = true; |
|---|
| 108 | org.apache.poi.poifs.dev.POIFSLister.displayDirectory(fs.getRoot(), indent, withSizes); |
|---|
| 109 | |
|---|
| 110 | if (verbose) { |
|---|
| 111 | List strings = POIFSViewEngine.inspectViewable(fs, true, 0, " "); |
|---|
| 112 | Iterator iter = strings.iterator(); |
|---|
| 113 | |
|---|
| 114 | while (iter.hasNext()) { |
|---|
| 115 | System.out.print(iter.next()); |
|---|
| 116 | } |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | // public void extractPdf(OutputStream out) throws IOException { |
|---|
| 122 | // |
|---|
| 123 | // DocumentInputStream inputStream = fs.createDocumentInputStream("CONTENTS"); |
|---|
| 124 | // |
|---|
| 125 | // byte buf[]=new byte[1024]; |
|---|
| 126 | // int len; |
|---|
| 127 | // while ((len=inputStream.read(buf))>0) { |
|---|
| 128 | // out.write(buf,0,len); |
|---|
| 129 | // } |
|---|
| 130 | // out.close(); |
|---|
| 131 | // inputStream.close(); |
|---|
| 132 | // |
|---|
| 133 | // } |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | } |
|---|