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.idesupport;
25 
26 import java.io.*;
27 import java.util.zip.*;
28 import java.util.Vector;
29 import java.util.Enumeration;
30 import java.util.StringTokenizer;
31 
32 import org.openoffice.idesupport.filter.FileFilter;
33 import org.openoffice.idesupport.filter.BinaryOnlyFilter;
34 import org.openoffice.idesupport.zip.ParcelZipper;
35 
36 public class OfficeDocument
37 {
38     public static final String PARCEL_PREFIX_DIR =
39         ParcelZipper.PARCEL_PREFIX_DIR;
40 
41     public static final String[] OFFICE_EXTENSIONS =
42         {".sxc" , ".sxw", ".sxi", ".sxd"};
43     public static final String OFFICE_PRODUCT_NAME = "OpenOffice.org";
44 
45     private File file = null;
46 
OfficeDocument(File file)47     public OfficeDocument(File file) throws IllegalArgumentException
48     {
49         if (!file.exists() || file.isDirectory() || !isOfficeFile(file)) {
50             throw new IllegalArgumentException("This is not a valid " +
51                 OFFICE_PRODUCT_NAME + " document.");
52         }
53         this.file = file;
54     }
55 
isOfficeFile(File file)56     private boolean isOfficeFile(File file) {
57         for (int i = 0; i < OFFICE_EXTENSIONS.length; i++)
58             if (file.getName().endsWith(OFFICE_EXTENSIONS[i]))
59                 return true;
60         return false;
61     }
62 
getParcels()63     public Enumeration getParcels() {
64 
65         Vector parcels = new Vector();
66         ZipFile zp = null;
67 
68         try
69         {
70             zp = new ZipFile(this.file);
71 
72             for (Enumeration enumer = zp.entries(); enumer.hasMoreElements(); )
73             {
74                 ZipEntry ze = (ZipEntry)enumer.nextElement();
75                 if (ze.getName().endsWith(ParcelZipper.PARCEL_DESCRIPTOR_XML))
76                 {
77                     String tmp = ze.getName();
78                     int end = tmp.lastIndexOf("/");
79                     tmp = tmp.substring(0, end);
80 
81                     String parcelName = ze.getName().substring(0, end);
82                     parcels.add(parcelName);
83                 }
84             }
85         }
86         catch(ZipException ze) {
87             ze.printStackTrace();
88         }
89         catch(IOException ioe) {
90             ioe.printStackTrace();
91         }
92         finally {
93             if (zp != null) {
94                 try {
95                     zp.close();
96                 }
97                 catch (IOException asdf) {
98                 }
99             }
100         }
101 
102         return parcels.elements();
103     }
104 
removeParcel(String parcelName)105     public boolean removeParcel(String parcelName) {
106 
107         try {
108             ParcelZipper.getParcelZipper().removeParcel(file, parcelName);
109         }
110         catch (IOException ioe) {
111             ioe.printStackTrace();
112             return false;
113         }
114         return true;
115     }
116 }
117