1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package org.openoffice.idesupport;
29 
30 import java.io.*;
31 import java.util.zip.*;
32 import java.util.Vector;
33 import java.util.Enumeration;
34 import java.util.StringTokenizer;
35 
36 import org.openoffice.idesupport.filter.FileFilter;
37 import org.openoffice.idesupport.filter.BinaryOnlyFilter;
38 import org.openoffice.idesupport.zip.ParcelZipper;
39 
40 public class OfficeDocument
41 {
42     public static final String PARCEL_PREFIX_DIR =
43         ParcelZipper.PARCEL_PREFIX_DIR;
44 
45     public static final String[] OFFICE_EXTENSIONS =
46         {".sxc" , ".sxw", ".sxi", ".sxd"};
47     public static final String OFFICE_PRODUCT_NAME = "OpenOffice.org";
48 
49     private File file = null;
50 
51     public OfficeDocument(File file) throws IllegalArgumentException
52     {
53         if (!file.exists() || file.isDirectory() || !isOfficeFile(file)) {
54             throw new IllegalArgumentException("This is not a valid " +
55                 OFFICE_PRODUCT_NAME + " document.");
56         }
57         this.file = file;
58     }
59 
60     private boolean isOfficeFile(File file) {
61         for (int i = 0; i < OFFICE_EXTENSIONS.length; i++)
62             if (file.getName().endsWith(OFFICE_EXTENSIONS[i]))
63                 return true;
64         return false;
65     }
66 
67     public Enumeration getParcels() {
68 
69         Vector parcels = new Vector();
70         ZipFile zp = null;
71 
72         try
73         {
74             zp = new ZipFile(this.file);
75 
76             for (Enumeration enumer = zp.entries(); enumer.hasMoreElements(); )
77             {
78                 ZipEntry ze = (ZipEntry)enumer.nextElement();
79                 if (ze.getName().endsWith(ParcelZipper.PARCEL_DESCRIPTOR_XML))
80                 {
81                     String tmp = ze.getName();
82                     int end = tmp.lastIndexOf("/");
83                     tmp = tmp.substring(0, end);
84 
85                     String parcelName = ze.getName().substring(0, end);
86                     parcels.add(parcelName);
87                 }
88             }
89         }
90         catch(ZipException ze) {
91             ze.printStackTrace();
92         }
93         catch(IOException ioe) {
94             ioe.printStackTrace();
95         }
96         finally {
97             if (zp != null) {
98                 try {
99                     zp.close();
100                 }
101                 catch (IOException asdf) {
102                 }
103             }
104         }
105 
106         return parcels.elements();
107     }
108 
109     public boolean removeParcel(String parcelName) {
110 
111         try {
112             ParcelZipper.getParcelZipper().removeParcel(file, parcelName);
113         }
114         catch (IOException ioe) {
115             ioe.printStackTrace();
116             return false;
117         }
118         return true;
119     }
120 }
121