source: trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/OleObjectBinaryPart.java @ 626

Revision 626, 3.6 KB checked in by jharrop, 3 years ago (diff)

Update to match revised POIFS api

Line 
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
21package org.docx4j.openpackaging.parts.WordprocessingML;
22
23import java.io.ByteArrayInputStream;
24import java.io.ByteArrayOutputStream;
25import java.io.FileInputStream;
26import java.io.IOException;
27import java.io.OutputStream;
28import java.nio.ByteBuffer;
29import java.util.Iterator;
30import java.util.List;
31
32import org.apache.log4j.Logger;
33import org.apache.poi.poifs.dev.POIFSViewEngine;
34import org.apache.poi.poifs.filesystem.DocumentInputStream;
35import org.apache.poi.poifs.filesystem.POIFSFileSystem;
36import org.docx4j.openpackaging.exceptions.InvalidFormatException;
37import org.docx4j.openpackaging.parts.PartName;
38import org.docx4j.openpackaging.parts.relationships.Namespaces;
39
40
41public 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}
Note: See TracBrowser for help on using the repository browser.