source: trunk/docx4j/src/main/java/org/docx4j/openpackaging/io/LoadFromZipFile.java @ 1784

Revision 1784, 21.2 KB checked in by jharrop, 5 weeks ago (diff)

XmlSignature? part, which uses JAXB representation of xmldsig-core, contained in separate project.

  • Property svn:eol-style set to native
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.io;
22
23
24
25import java.io.File;
26import java.io.IOException;
27import java.io.InputStream;
28import java.net.URI;
29import java.net.URISyntaxException;
30import java.util.Enumeration;
31import java.util.HashMap;
32import java.util.Iterator;
33import java.util.zip.ZipEntry;
34import java.util.zip.ZipFile;
35
36import javax.xml.bind.JAXBElement;
37import javax.xml.bind.Unmarshaller;
38
39import org.apache.log4j.Logger;
40import org.docx4j.XmlUtils;
41import org.docx4j.jaxb.Context;
42import org.docx4j.model.datastorage.CustomXmlDataStorage;
43import org.docx4j.openpackaging.Base;
44import org.docx4j.openpackaging.URIHelper;
45import org.docx4j.openpackaging.contenttype.ContentTypeManager;
46import org.docx4j.openpackaging.exceptions.Docx4JException;
47import org.docx4j.openpackaging.exceptions.InvalidFormatException;
48import org.docx4j.openpackaging.exceptions.PartUnrecognisedException;
49import org.docx4j.openpackaging.packages.OpcPackage;
50import org.docx4j.openpackaging.parts.DefaultXmlPart;
51import org.docx4j.openpackaging.parts.Part;
52import org.docx4j.openpackaging.parts.PartName;
53import org.docx4j.openpackaging.parts.XmlPart;
54import org.docx4j.openpackaging.parts.WordprocessingML.BibliographyPart;
55import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart;
56import org.docx4j.openpackaging.parts.opendope.ComponentsPart;
57import org.docx4j.openpackaging.parts.opendope.ConditionsPart;
58import org.docx4j.openpackaging.parts.opendope.QuestionsPart;
59import org.docx4j.openpackaging.parts.opendope.XPathsPart;
60import org.docx4j.openpackaging.parts.relationships.Namespaces;
61import org.docx4j.openpackaging.parts.relationships.RelationshipsPart;
62import org.docx4j.relationships.Relationship;
63
64
65/**
66 * Create a Package object from a Zip file.
67 *
68 * @author jharrop
69 *
70 */
71public class LoadFromZipFile extends Load {
72               
73        private static Logger log = Logger.getLogger(LoadFromZipFile.class);
74       
75        private static boolean conserveMemory = false;
76        public static void setConserveMemory(boolean conserveMemoryVal) {
77                conserveMemory = conserveMemoryVal;
78        }
79       
80
81        // Testing
82        public static void main(String[] args) throws Exception {
83                String filepath = System.getProperty("user.dir") + "/sample-docs/FontEmbedded.docx";
84                log.info("Path: " + filepath );
85                LoadFromZipFile loader = new LoadFromZipFile();
86                loader.get(filepath);           
87        }
88
89         // HashMap containing the names of all the zip entries,
90        // so we can tell whether there are any orphans
91//      public HashMap unusedZipEntries = null;
92       
93        public LoadFromZipFile() {
94//              this(new ContentTypeManager() );
95        }
96
97//      public LoadFromZipFile(ContentTypeManager ctm) {
98//              this.ctm = ctm;
99//      }
100       
101       
102        public OpcPackage get(String filepath) throws Docx4JException {
103                return get(new File(filepath));
104        }
105       
106        public OpcPackage get(File f) throws Docx4JException {
107                log.info("Filepath = " + f.getPath() );
108               
109                ZipFile zf = null;
110                try {
111                        if (!f.exists()) {
112                                log.info( "Couldn't find " + f.getPath() );
113                        }
114                        zf = new ZipFile(f);
115                } catch (IOException ioe) {
116                        ioe.printStackTrace() ;
117                        throw new Docx4JException("Couldn't get ZipFile", ioe);
118                }
119               
120                //dumpZipFileContents(zf);
121
122                // 1. The idea is to walk the tree of relationships, getting
123//              everything we need from the zip file.  But I'd like to know
124//              whether there are any orphans, so first we make
125//              a HashMap containing the names of all the zip file
126//              entries, so we can tick them off.
127               
128//              unusedZipEntries = new HashMap();
129                Enumeration entries = zf.entries();
130                while (entries.hasMoreElements()) {
131                        ZipEntry entry = (ZipEntry) entries.nextElement();
132//                      unusedZipEntries.put(entry.getName(), new Boolean(true) );     
133                }
134               
135                // 2. Create a new Package
136                //              Eventually, you'll also be able to create an Excel package etc
137                //              but only the WordML package exists at present
138                ContentTypeManager ctm = new ContentTypeManager();
139               
140                try {
141                        InputStream is = getInputStreamFromZippedPart( zf,  "[Content_Types].xml");
142                        ctm.parseContentTypesFile(is);
143                } catch (IOException e) {
144                        throw new Docx4JException("Couldn't get [Content_Types].xml from ZipFile", e);
145                }
146               
147                OpcPackage p = ctm.createPackage();
148               
149                // 3. Get [Content_Types].xml
150//              Once we've got this, then we can look up the content type for
151//              each PartName, and use it in the Part constructor.
152//              p.setContentTypeManager(ctm); - 20080111 - done by ctm.createPackage();
153//              unusedZipEntries.put("[Content_Types].xml", new Boolean(false));
154               
155                // 4. Start with _rels/.rels
156
157//              <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
158//                <Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>
159//                <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>
160//                <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/>
161//              </Relationships>               
162               
163                String partName = "_rels/.rels";
164                RelationshipsPart rp = getRelationshipsPartFromZip(p, zf,  partName);           
165                p.setRelationships(rp);
166                //rp.setPackageRelationshipPart(true);         
167//              unusedZipEntries.put(partName, new Boolean(false));
168               
169               
170                log.info( "Object created for: " + partName);
171                //log.info( rp.toString());
172               
173                // 5. Now recursively
174//              (i) create new Parts for each thing listed
175//              in the relationships
176//              (ii) add the new Part to the package
177//              (iii) cross the PartName off unusedZipEntries
178                addPartsFromRelationships(zf, p, rp, ctm );
179               
180               
181                // 6. Check unusedZipEntries is empty
182//              if (log.isDebugEnabled()) {             
183//                       Iterator myVeryOwnIterator = unusedZipEntries.keySet().iterator();
184//                       while(myVeryOwnIterator.hasNext()) {
185//                           String key = (String)myVeryOwnIterator.next();
186//                           log.info( key + "  " + unusedZipEntries.get(key));
187//                       }
188//              }               
189                 try {
190                         zf.close();
191                 } catch (IOException exc) {
192                         exc.printStackTrace();
193                 }
194                 
195                registerCustomXmlDataStorageParts(p);
196                 
197                 return p;
198               
199        }
200       
201        private RelationshipsPart getRelationshipsPartFromZip(Base p, ZipFile zf, String partName) 
202                        throws Docx4JException {
203//                      Document contents = null;
204//                      try {
205//                              contents = getDocumentFromZippedPart( zf,  partName);
206//                      } catch (Exception e) {
207//                              e.printStackTrace();
208//                              throw new Docx4JException("Error getting document from Zipped Part", e);
209//                             
210//                      }
211//              // debugPrint(contents);
212//              // TODO - why don't any of the part names in this document start with "/"?
213//              return new RelationshipsPart( p, new PartName("/" + partName), contents );     
214               
215                RelationshipsPart rp = null;
216               
217                InputStream is = null;
218                try {
219                        is =  getInputStreamFromZippedPart( zf,  partName);
220                        //thePart = new RelationshipsPart( p, new PartName("/" + partName), is );
221                        rp = new RelationshipsPart(new PartName("/" + partName) );
222                        rp.setSourceP(p);
223                        rp.unmarshal(is);
224                       
225                } catch (Exception e) {
226                        e.printStackTrace();
227                        throw new Docx4JException("Error getting document from Zipped Part:" + partName, e);
228                       
229                } finally {
230                        if (is != null) {
231                                try {
232                                        is.close();
233                                } catch (IOException exc) {
234                                        exc.printStackTrace();
235                                }
236                        }
237                }
238               
239                return rp;
240        // debugPrint(contents);
241        // TODO - why don't any of the part names in this document start with "/"?
242        }
243
244        private static InputStream getInputStreamFromZippedPart(ZipFile zf, String partName) 
245                throws IOException {
246               
247                InputStream in = null;
248                in = zf.getInputStream( zf.getEntry(partName ) );
249                return in;             
250        }
251       
252       
253       
254        /* recursively
255        (i) create new Parts for each thing listed
256        in the relationships
257        (ii) add the new Part to the package
258        (iii) cross the PartName off unusedZipEntries
259        */
260        private void addPartsFromRelationships(ZipFile zf, Base source, RelationshipsPart rp,
261                        ContentTypeManager ctm)
262                throws Docx4JException {
263               
264                OpcPackage pkg = source.getPackage();                           
265               
266//              for (Iterator it = rp.iterator(); it.hasNext(); ) {
267//                      Relationship r = (Relationship)it.next();
268//                      log.info("For Relationship Id=" + r.getId() + " Source is "
269//                                      + r.getSource().getPartName()
270//                                      + ", Target is " + r.getTargetURI() );
271//                      try {
272//                             
273//                              getPart(zf, pkg, rp, r);
274//                             
275//                      } catch (Exception e) {
276//                              throw new Docx4JException("Failed to add parts from relationships", e);
277//                      }
278//              }
279               
280                for ( Relationship r : rp.getRelationships().getRelationship() ) {
281                       
282                        log.info("\n For Relationship Id=" + r.getId() 
283                                        + " Source is " + rp.getSourceP().getPartName() 
284                                        + ", Target is " + r.getTarget() );
285                                // This is usually the first logged comment for
286                                // a part, so start with a line break.
287                        try {                           
288                                getPart(zf, pkg, rp, r, ctm);
289                        } catch (Exception e) {
290                                throw new Docx4JException("Failed to add parts from relationships", e);
291                        }
292                }
293               
294               
295        }
296
297        /**
298         * Get a Part (except a relationships part), and all its related parts. 
299         * This can be called directly from outside the library, in which case
300         * the Part will not be owned by a Package until the calling code makes it so. 
301         *
302         * @param zf
303         * @param source
304         * @param unusedZipEntries
305         * @param pkg
306         * @param r
307         * @param resolvedPartUri
308         * @throws Docx4JException
309         * @throws InvalidFormatException
310         */
311//      private void getPart(ZipFile zf, Base source,
312//                      Package pkg, String resolvedPartUri, String relationshipType)
313        private void getPart(ZipFile zf, OpcPackage pkg, RelationshipsPart rp, Relationship r,
314                        ContentTypeManager ctm)
315                        throws Docx4JException, InvalidFormatException, URISyntaxException {
316               
317                Base source = null;
318                String resolvedPartUri = null;
319               
320                if (r.getType().equals(Namespaces.HYPERLINK)) {
321                        // Could be Internal or External
322                        // Example of Internal is w:drawing/wp:inline/wp:docPr/a:hlinkClick
323                        log.info("Encountered (but not loading) hyperlink " + r.getTarget()  );                         
324                        return;                 
325                } else         
326                if (r.getTargetMode() == null
327                                || !r.getTargetMode().equals("External") ) {
328                       
329                        // Usual case
330                       
331                        source = rp.getSourceP();
332                        resolvedPartUri = URIHelper.resolvePartUri(rp.getSourceURI(), new URI(r.getTarget() ) ).toString();             
333
334                        // Now drop leading "/'
335                        resolvedPartUri = resolvedPartUri.substring(1);                         
336
337                        // Now normalise it .. ie abc/def/../ghi
338                        // becomes abc/ghi
339                        // Maybe this isn't necessary with a zip file,
340                        // - ZipFile class may be smart enough to do it.
341                        // But it is certainly necessary in the JCR case.
342//                      resolvedPartUri = (new java.net.URI(resolvedPartUri)).normalize().toString();
343//                      log.info("Normalised, it is " + resolvedPartUri );                             
344                       
345                } else {                       
346                        // EXTERNAL                     
347                        if (loadExternalTargets && 
348                                        r.getType().equals( Namespaces.IMAGE ) ) {
349                                        // It could instead be, for example, of type hyperlink,
350                                        // and we don't want to try to fetch that
351                                log.warn("Loading external resource " + r.getTarget() 
352                                                   + " of type " + r.getType() );
353                                BinaryPart bp = ExternalResourceUtils.getExternalResource(r.getTarget());
354                                pkg.getExternalResources().put(bp.getExternalTarget(), bp);                     
355                        } else {                               
356                                log.warn("Encountered (but not loading) external resource " + r.getTarget() 
357                                                   + " of type " + r.getType() );                               
358                        }                                               
359                        return;
360                }
361               
362                if (pkg.handled.get(resolvedPartUri)!=null) return;
363               
364                String relationshipType = r.getType();         
365                       
366                Part part = getRawPart(zf, ctm, resolvedPartUri, r);
367                if (part instanceof BinaryPart
368                                || part instanceof DefaultXmlPart) {
369                        // The constructors of other parts should take care of this...
370                        part.setRelationshipType(relationshipType);
371                }               
372                rp.loadPart(part, r);
373                pkg.handled.put(resolvedPartUri, resolvedPartUri);
374               
375
376                // The source Part (or Package) might have a convenience
377                // method for this
378                if (source.setPartShortcut(part, relationshipType ) ) {
379                        log.info("Convenience method established from " + source.getPartName() 
380                                        + " to " + part.getPartName());
381                }
382               
383//              unusedZipEntries.put(resolvedPartUri, new Boolean(false));
384//              log.info(".. added." );
385               
386                RelationshipsPart rrp = getRelationshipsPart(zf, part);
387                if (rrp!=null) {
388                        // recurse via this parts relationships, if it has any
389                        addPartsFromRelationships(zf, part, rrp, ctm );
390                        String relPart = PartName.getRelationshipsPartName(
391                                        part.getPartName().getName().substring(1) );
392//                      unusedZipEntries.put(relPart, new Boolean(false));                                     
393                }
394        }
395
396        /**
397         * Get the Relationships Part (if there is one) for a given Part. 
398         * Otherwise return null.
399         *
400         * @param zf
401         * @param part
402         * @return
403         * @throws InvalidFormatException
404         */
405        public RelationshipsPart getRelationshipsPart(ZipFile zf, Part part)
406        throws Docx4JException, InvalidFormatException {
407                RelationshipsPart rrp = null;
408                // recurse via this parts relationships, if it has any
409                //String relPart = PartName.getRelationshipsPartName(target);
410                String relPart = PartName.getRelationshipsPartName(
411                                part.getPartName().getName().substring(1) );
412               
413                if (zf.getEntry(relPart) !=null ) {
414                        log.info("Found relationships " + relPart );
415                        log.info("Recursing ... " );
416                        rrp = getRelationshipsPartFromZip(part,  zf,  relPart);
417                        part.setRelationships(rrp);
418                } else {
419                        log.info("No relationships " + relPart );       
420                        return null;
421                }
422                return rrp;
423        }
424       
425       
426
427        /**
428         * Get a Part (except a relationships part), but not its relationships part
429         * or related parts.  Useful if you need quick access to just this part.
430         * This can be called directly from outside the library, in which case
431         * the Part will not be owned by a Package until the calling code makes it so. 
432         * @see  To get a Part and all its related parts, and add all to a package, use
433         * getPart.
434         * @param zf
435         * @param resolvedPartUri
436         * @return
437         * @throws URISyntaxException
438         * @throws InvalidFormatException
439         */
440        public static Part getRawPart(ZipFile zf, ContentTypeManager ctm, String resolvedPartUri, Relationship rel)
441                        throws Docx4JException {
442                Part part = null;
443               
444                InputStream is = null;
445                try {
446                        try {
447                                log.debug("resolved uri: " + resolvedPartUri);
448                                is = getInputStreamFromZippedPart( zf,  resolvedPartUri);
449                               
450                                // Get a subclass of Part appropriate for this content type     
451                                // This will throw UnrecognisedPartException in the absence of
452                                // specific knowledge. Hence it is important to get the is
453                                // first, as we do above.
454                                part = ctm.getPart("/" + resolvedPartUri, rel);                         
455
456                                if (part instanceof org.docx4j.openpackaging.parts.ThemePart) {
457
458                                        ((org.docx4j.openpackaging.parts.JaxbXmlPart)part).setJAXBContext(Context.jcThemePart);
459                                        ((org.docx4j.openpackaging.parts.JaxbXmlPart)part).unmarshal( is );
460                                       
461                                } else if (part instanceof org.docx4j.openpackaging.parts.DocPropsCorePart ) {
462
463                                                ((org.docx4j.openpackaging.parts.JaxbXmlPart)part).setJAXBContext(Context.jcDocPropsCore);
464                                                ((org.docx4j.openpackaging.parts.JaxbXmlPart)part).unmarshal( is );
465                                               
466                                } else if (part instanceof org.docx4j.openpackaging.parts.DocPropsCustomPart ) {
467
468                                                ((org.docx4j.openpackaging.parts.JaxbXmlPart)part).setJAXBContext(Context.jcDocPropsCustom);
469                                                ((org.docx4j.openpackaging.parts.JaxbXmlPart)part).unmarshal( is );
470                                               
471                                } else if (part instanceof org.docx4j.openpackaging.parts.DocPropsExtendedPart ) {
472
473                                                ((org.docx4j.openpackaging.parts.JaxbXmlPart)part).setJAXBContext(Context.jcDocPropsExtended);
474                                                ((org.docx4j.openpackaging.parts.JaxbXmlPart)part).unmarshal( is );
475
476                                } else if (part instanceof org.docx4j.openpackaging.parts.CustomXmlDataStoragePropertiesPart ) {
477
478                                        ((org.docx4j.openpackaging.parts.JaxbXmlPart)part).setJAXBContext(Context.jcCustomXmlProperties);
479                                        ((org.docx4j.openpackaging.parts.JaxbXmlPart)part).unmarshal( is );
480
481                                } else if (part instanceof org.docx4j.openpackaging.parts.digitalsignature.XmlSignaturePart ) {
482
483                                        ((org.docx4j.openpackaging.parts.JaxbXmlPart)part).setJAXBContext(Context.jcXmlDSig);
484                                        ((org.docx4j.openpackaging.parts.JaxbXmlPart)part).unmarshal( is );
485                                                                               
486                                } else if (part instanceof org.docx4j.openpackaging.parts.JaxbXmlPart) {
487
488                                        // MainDocument part, Styles part, Font part etc
489                                       
490                                        ((org.docx4j.openpackaging.parts.JaxbXmlPart)part).setJAXBContext(Context.jc);
491                                        ((org.docx4j.openpackaging.parts.JaxbXmlPart)part).unmarshal( is );
492                                       
493                                } else if (part instanceof org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart) {
494                                       
495                                        log.debug("Detected BinaryPart " + part.getClass().getName() );
496                                        if (conserveMemory) {
497                                                ((BinaryPart)part).setBinaryDataRef(
498                                                        zf.getName(), resolvedPartUri);
499                                        } else {
500                                                ((BinaryPart)part).setBinaryData(is);
501                                        }
502                                       
503                                } else if (part instanceof org.docx4j.openpackaging.parts.CustomXmlDataStoragePart ) {
504                                       
505                                        // Is it a part we know?
506                                        try {
507                                                Unmarshaller u = Context.jc.createUnmarshaller();
508                                                Object o = u.unmarshal( is );                                           
509                                                log.debug(o.getClass().getName());
510                                               
511                                                PartName name = part.getPartName();
512                                               
513                                                if (o instanceof org.opendope.conditions.Conditions) {
514                                                       
515                                                        part = new ConditionsPart(name);
516                                                        ((ConditionsPart)part).setJaxbElement(
517                                                                        (org.opendope.conditions.Conditions)o);
518                                                       
519                                                       
520                                                } else if (o instanceof org.opendope.xpaths.Xpaths) {
521                                                       
522                                                        part = new XPathsPart(name);
523                                                        ((XPathsPart)part).setJaxbElement(
524                                                                        (org.opendope.xpaths.Xpaths)o);
525
526                                                } else if (o instanceof org.opendope.questions.Questionnaire) {
527                                                       
528                                                        part = new QuestionsPart(name);
529                                                        ((QuestionsPart)part).setJaxbElement(
530                                                                        (org.opendope.questions.Questionnaire)o);
531                                                                                                               
532                                                } else if (o instanceof org.opendope.components.Components) {
533                                                       
534                                                        part = new ComponentsPart(name);
535                                                        ((ComponentsPart)part).setJaxbElement(
536                                                                        (org.opendope.components.Components)o);
537
538                                                } else if (o instanceof JAXBElement<?> 
539                                                                && XmlUtils.unwrap(o) instanceof org.docx4j.bibliography.CTSources) {
540                                                        part = new BibliographyPart(name);
541                                                        ((BibliographyPart)part).setJaxbElement(
542                                                                        (JAXBElement<org.docx4j.bibliography.CTSources>)o);
543
544                                                } else {
545                                                       
546                                                        log.warn("No known part after all for CustomXmlPart " + o.getClass().getName());
547
548                                                        CustomXmlDataStorage data = getCustomXmlDataStorageClass().factory();                                   
549                                                        is.reset();
550                                                        data.setDocument(is); // Not necessarily JAXB, that's just our method name
551                                                        ((org.docx4j.openpackaging.parts.CustomXmlDataStoragePart)part).setData(data);                                         
552                                                       
553                                                }
554                                               
555                                        } catch (javax.xml.bind.UnmarshalException ue) {
556                                               
557                                                // No ...
558                                                CustomXmlDataStorage data = getCustomXmlDataStorageClass().factory();   
559                                                is.reset();
560                                                data.setDocument(is); // Not necessarily JAXB, that's just our method name
561                                                ((org.docx4j.openpackaging.parts.CustomXmlDataStoragePart)part).setData(data);                                         
562                                        }                                       
563
564                                } else if (part instanceof org.docx4j.openpackaging.parts.XmlPart ) {
565                                       
566                                        try {
567                                                ((XmlPart)part).setDocument(is);
568                                        } catch (Docx4JException d) {
569                                                // This isn't an XML part after all,
570                                                // even though ContentTypeManager detected it as such
571                                                // So get it as a binary part
572                                                part = getBinaryPart(zf, ctm, resolvedPartUri);
573                                                if (conserveMemory) {
574                                                        ((BinaryPart)part).setBinaryDataRef(
575                                                                        zf.getName(), resolvedPartUri);
576                                                } else {
577                                                        ((BinaryPart)part).setBinaryData(is);
578                                                }
579                                        }
580                                       
581                                } else {
582                                        // Shouldn't happen, since ContentTypeManagerImpl should
583                                        // return an instance of one of the above, or throw an
584                                        // Exception.
585                                       
586                                        log.error("No suitable part found for: " + resolvedPartUri);
587                                        part = null;                                   
588                                }
589                       
590                        } catch (PartUnrecognisedException e) {
591                                log.warn("PartUnrecognisedException shouldn't happen anymore!");
592                                // Try to get it as a binary part
593                                part = getBinaryPart(zf, ctm, resolvedPartUri);
594                                if (conserveMemory) {
595                                        ((BinaryPart)part).setBinaryDataRef(
596                                                        zf.getName(), resolvedPartUri);
597                                } else {
598                                        ((BinaryPart)part).setBinaryData(is);
599                                }
600                                               
601                        }
602                } catch (Exception ex) {
603                        // IOException, URISyntaxException
604                        ex.printStackTrace();
605                        throw new Docx4JException("Failed to getPart", ex);                     
606                       
607                } finally {
608                        if (is != null) {
609                                try {
610                                        is.close();
611                                } catch (IOException exc) {
612                                        exc.printStackTrace();
613                                }
614                        }
615                }
616                return part;
617        }
618       
619        public static Part getBinaryPart(ZipFile zf, ContentTypeManager ctm, String resolvedPartUri)
620                        throws Docx4JException {
621
622                Part part = null;
623                InputStream in = null;                                 
624                try {                   
625                        in = zf.getInputStream( zf.getEntry(resolvedPartUri ) );
626                        part = new BinaryPart( new PartName("/" + resolvedPartUri));
627                       
628                        if (conserveMemory) {
629                                ((BinaryPart)part).setBinaryDataRef(
630                                                zf.getName(), resolvedPartUri);
631                        } else {
632                                ((BinaryPart)part).setBinaryData(in);
633                        }
634                                       
635                        log.info("Stored as BinaryData" );
636                       
637                } catch (IOException ioe) {
638                        ioe.printStackTrace() ;
639                } finally {
640                        if (in != null) {
641                                try {
642                                        in.close();
643                                } catch (IOException exc) {
644                                        exc.printStackTrace();
645                                }
646                        }
647                }
648                return part;
649        }       
650       
651       
652}
Note: See TracBrowser for help on using the repository browser.