1*cd519653SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*cd519653SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*cd519653SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*cd519653SAndrew Rist  * distributed with this work for additional information
6*cd519653SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*cd519653SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*cd519653SAndrew Rist  * "License"); you may not use this file except in compliance
9*cd519653SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*cd519653SAndrew Rist  *
11*cd519653SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*cd519653SAndrew Rist  *
13*cd519653SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*cd519653SAndrew Rist  * software distributed under the License is distributed on an
15*cd519653SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*cd519653SAndrew Rist  * KIND, either express or implied.  See the License for the
17*cd519653SAndrew Rist  * specific language governing permissions and limitations
18*cd519653SAndrew Rist  * under the License.
19*cd519653SAndrew Rist  *
20*cd519653SAndrew Rist  *************************************************************/
21*cd519653SAndrew Rist 
22*cd519653SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package org.openoffice.idesupport;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import java.io.*;
27cdf0e10cSrcweir import java.util.zip.*;
28cdf0e10cSrcweir import java.util.Vector;
29cdf0e10cSrcweir import java.util.Enumeration;
30cdf0e10cSrcweir import java.util.StringTokenizer;
31cdf0e10cSrcweir 
32cdf0e10cSrcweir import org.openoffice.idesupport.filter.FileFilter;
33cdf0e10cSrcweir import org.openoffice.idesupport.filter.BinaryOnlyFilter;
34cdf0e10cSrcweir import org.openoffice.idesupport.zip.ParcelZipper;
35cdf0e10cSrcweir 
36cdf0e10cSrcweir public class OfficeDocument
37cdf0e10cSrcweir {
38cdf0e10cSrcweir     public static final String PARCEL_PREFIX_DIR =
39cdf0e10cSrcweir         ParcelZipper.PARCEL_PREFIX_DIR;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir     public static final String[] OFFICE_EXTENSIONS =
42cdf0e10cSrcweir         {".sxc" , ".sxw", ".sxi", ".sxd"};
43cdf0e10cSrcweir     public static final String OFFICE_PRODUCT_NAME = "OpenOffice.org";
44cdf0e10cSrcweir 
45cdf0e10cSrcweir     private File file = null;
46cdf0e10cSrcweir 
OfficeDocument(File file)47cdf0e10cSrcweir     public OfficeDocument(File file) throws IllegalArgumentException
48cdf0e10cSrcweir     {
49cdf0e10cSrcweir         if (!file.exists() || file.isDirectory() || !isOfficeFile(file)) {
50cdf0e10cSrcweir             throw new IllegalArgumentException("This is not a valid " +
51cdf0e10cSrcweir                 OFFICE_PRODUCT_NAME + " document.");
52cdf0e10cSrcweir         }
53cdf0e10cSrcweir         this.file = file;
54cdf0e10cSrcweir     }
55cdf0e10cSrcweir 
isOfficeFile(File file)56cdf0e10cSrcweir     private boolean isOfficeFile(File file) {
57cdf0e10cSrcweir         for (int i = 0; i < OFFICE_EXTENSIONS.length; i++)
58cdf0e10cSrcweir             if (file.getName().endsWith(OFFICE_EXTENSIONS[i]))
59cdf0e10cSrcweir                 return true;
60cdf0e10cSrcweir         return false;
61cdf0e10cSrcweir     }
62cdf0e10cSrcweir 
getParcels()63cdf0e10cSrcweir     public Enumeration getParcels() {
64cdf0e10cSrcweir 
65cdf0e10cSrcweir         Vector parcels = new Vector();
66cdf0e10cSrcweir         ZipFile zp = null;
67cdf0e10cSrcweir 
68cdf0e10cSrcweir         try
69cdf0e10cSrcweir         {
70cdf0e10cSrcweir             zp = new ZipFile(this.file);
71cdf0e10cSrcweir 
72cdf0e10cSrcweir             for (Enumeration enumer = zp.entries(); enumer.hasMoreElements(); )
73cdf0e10cSrcweir             {
74cdf0e10cSrcweir                 ZipEntry ze = (ZipEntry)enumer.nextElement();
75cdf0e10cSrcweir                 if (ze.getName().endsWith(ParcelZipper.PARCEL_DESCRIPTOR_XML))
76cdf0e10cSrcweir                 {
77cdf0e10cSrcweir                     String tmp = ze.getName();
78cdf0e10cSrcweir                     int end = tmp.lastIndexOf("/");
79cdf0e10cSrcweir                     tmp = tmp.substring(0, end);
80cdf0e10cSrcweir 
81cdf0e10cSrcweir                     String parcelName = ze.getName().substring(0, end);
82cdf0e10cSrcweir                     parcels.add(parcelName);
83cdf0e10cSrcweir                 }
84cdf0e10cSrcweir             }
85cdf0e10cSrcweir         }
86cdf0e10cSrcweir         catch(ZipException ze) {
87cdf0e10cSrcweir             ze.printStackTrace();
88cdf0e10cSrcweir         }
89cdf0e10cSrcweir         catch(IOException ioe) {
90cdf0e10cSrcweir             ioe.printStackTrace();
91cdf0e10cSrcweir         }
92cdf0e10cSrcweir         finally {
93cdf0e10cSrcweir             if (zp != null) {
94cdf0e10cSrcweir                 try {
95cdf0e10cSrcweir                     zp.close();
96cdf0e10cSrcweir                 }
97cdf0e10cSrcweir                 catch (IOException asdf) {
98cdf0e10cSrcweir                 }
99cdf0e10cSrcweir             }
100cdf0e10cSrcweir         }
101cdf0e10cSrcweir 
102cdf0e10cSrcweir         return parcels.elements();
103cdf0e10cSrcweir     }
104cdf0e10cSrcweir 
removeParcel(String parcelName)105cdf0e10cSrcweir     public boolean removeParcel(String parcelName) {
106cdf0e10cSrcweir 
107cdf0e10cSrcweir         try {
108cdf0e10cSrcweir             ParcelZipper.getParcelZipper().removeParcel(file, parcelName);
109cdf0e10cSrcweir         }
110cdf0e10cSrcweir         catch (IOException ioe) {
111cdf0e10cSrcweir             ioe.printStackTrace();
112cdf0e10cSrcweir             return false;
113cdf0e10cSrcweir         }
114cdf0e10cSrcweir         return true;
115cdf0e10cSrcweir     }
116cdf0e10cSrcweir }
117