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 
23 
24 package org.openoffice.netbeans.modules.office.utils;
25 
26 import java.io.*;
27 import java.util.zip.*;
28 import java.beans.PropertyVetoException;
29 
30 import org.openide.filesystems.FileSystem;
31 import org.openide.filesystems.Repository;
32 
33 import org.openoffice.netbeans.modules.office.filesystem.OpenOfficeDocFileSystem;
34 
35 public class ZipMounter
36 {
37     private static ZipMounter mounter = null;
38 
ZipMounter()39     private ZipMounter() {
40     }
41 
getZipMounter()42     public static ZipMounter getZipMounter() {
43         if (mounter == null) {
44             synchronized(ZipMounter.class) {
45                 if (mounter == null)
46                     mounter = new ZipMounter();
47             }
48         }
49         return mounter;
50     }
51 
mountZipFile(File zipfile)52     public void mountZipFile(File zipfile)
53         throws IOException, PropertyVetoException
54     {
55         if (zipfile != null) {
56             addDocumentToRepository(zipfile, true);
57         }
58     }
59 
addDocumentToRepository(File rootFile, boolean writeable)60     private FileSystem addDocumentToRepository(File rootFile, boolean writeable)
61         throws IOException, PropertyVetoException
62     {
63         Repository repo = Repository.getDefault();
64         OpenOfficeDocFileSystem oofs;
65         oofs = (OpenOfficeDocFileSystem)getMountedDocument(rootFile);
66         if(oofs != null)
67             repo.removeFileSystem(oofs);
68         oofs = new OpenOfficeDocFileSystem();
69         oofs.setDocument(rootFile);
70         repo.addFileSystem(oofs);
71         return oofs;
72     }
73 
74     /** @return FileSystem which has given jar file as its root or
75     * null if no such file system could be found in repository */
getMountedDocument(File rootFile)76     private FileSystem getMountedDocument(File rootFile)
77     {
78         if (rootFile == null)
79             return null;
80         FileSystem oofs = null;
81         try {
82             oofs = Repository.getDefault().findFileSystem(
83                 OpenOfficeDocFileSystem.computeSystemName(rootFile));
84         } catch(Exception exp) {
85         }
86         return oofs;
87     }
88 }
89