1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  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,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 package org.apache.openoffice.ooxml.framework.part;
23 
24 import java.io.File;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.lang.ref.SoftReference;
28 import java.util.Enumeration;
29 import java.util.HashMap;
30 import java.util.Map;
31 import java.util.Vector;
32 import java.util.zip.ZipEntry;
33 import java.util.zip.ZipFile;
34 
35 public class PartManager
36 {
PartManager(final File aFile)37     public PartManager (final File aFile)
38     {
39         ZipFile aZipFile = null;
40         try
41         {
42             aZipFile = new ZipFile(aFile);
43         }
44         catch (Exception e)
45         {
46             e.printStackTrace();
47         }
48         maZipFile = aZipFile;
49         maPartNameToPartMap = new HashMap<>();
50     }
51 
52 
53 
54 
getStreamForPartName(final PartName aPartName)55     public InputStream getStreamForPartName (final PartName aPartName)
56     {
57         final ZipEntry aEntry = maZipFile.getEntry(
58             ToZipEntryName(aPartName.GetFullname()));
59         if (aEntry == null)
60             return null;
61 
62         try
63         {
64             return maZipFile.getInputStream(aEntry);
65         }
66         catch (IOException e)
67         {
68             e.printStackTrace();
69             return null;
70         }
71     }
72 
73 
74 
75 
76     /** This is the low-level variant of getStreamForPartName().
77      *  It can return streams for entries in the OOXML zip package
78      *  that are not, technically, parts.
79      *  @return
80      *      Returns null when the named stream does not exist or can not be
81      *      opened.
82      */
getStreamForStreamName(final String sStreamName)83     public InputStream getStreamForStreamName (final String sStreamName)
84     {
85         final ZipEntry aEntry = maZipFile.getEntry(
86             ToZipEntryName(sStreamName));
87         try
88         {
89             return maZipFile.getInputStream(aEntry);
90         }
91         catch (IOException e)
92         {
93             e.printStackTrace();
94             return null;
95         }
96     }
97 
98 
99 
100 
getContentTypes()101     public ContentTypes getContentTypes ()
102     {
103         if (maContentTypes == null)
104         {
105             maContentTypes = new ContentTypes(this);
106         }
107         return maContentTypes;
108     }
109 
110 
111 
112 
ToZipEntryName(final String sPath)113     private final String ToZipEntryName (final String sPath)
114     {
115         return sPath.substring(1);
116     }
117 
118 
119 
120 
121     /** Return a list of the names of all streams.
122      *  Note that that list is not necessarily identical to the list of part
123      *  names.  It can contain entries that are not parts.
124      */
listStreamNames()125     public final String[] listStreamNames ()
126     {
127         final Vector<String> aStreamNames =  new Vector<>();
128 
129         final Enumeration<? extends ZipEntry> aEntries = maZipFile.entries();
130         while (aEntries.hasMoreElements())
131         {
132             final ZipEntry aEntry = aEntries.nextElement();
133             aStreamNames.add(aEntry.getName());
134         }
135 
136         return aStreamNames.toArray(new String[0]);
137     }
138 
139 
140 
141 
getPart(final PartName aName)142     public Part getPart (final PartName aName)
143     {
144         SoftReference<Part> aSoftPart = maPartNameToPartMap.get(aName);
145         Part aPart = null;
146         if (aSoftPart != null)
147             aPart = aSoftPart.get();
148         if (aPart == null)
149         {
150             aPart = new Part(
151                 getContentTypes().getTypeForPartName(aName),
152                 this,
153                 aName);
154             maPartNameToPartMap.put(aName, new SoftReference<Part>(aPart));
155         }
156 
157         return aPart;
158     }
159 
160 
161 
162 
163     private ZipFile maZipFile;
164     private ContentTypes maContentTypes;
165     private final Map<PartName, SoftReference<Part>> maPartNameToPartMap;
166 }
167