2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package org
.openoffice
.idesupport
;
22 import java
.io
.IOException
;
23 import java
.util
.ArrayList
;
24 import java
.util
.Enumeration
;
25 import java
.util
.Iterator
;
26 import java
.util
.zip
.ZipEntry
;
27 import java
.util
.zip
.ZipException
;
28 import java
.util
.zip
.ZipFile
;
30 import org
.openoffice
.idesupport
.zip
.ParcelZipper
;
32 public class OfficeDocument
34 public static final String PARCEL_PREFIX_DIR
=
35 ParcelZipper
.PARCEL_PREFIX_DIR
;
37 public static final String
[] OFFICE_EXTENSIONS
=
38 {".sxc" , ".sxw", ".sxi", ".sxd"};
39 public static final String OFFICE_PRODUCT_NAME
= "OpenOffice.org";
41 private File file
= null;
43 public OfficeDocument(File file
) throws IllegalArgumentException
45 if (!file
.exists() || file
.isDirectory() || !isOfficeFile(file
)) {
46 throw new IllegalArgumentException("This is not a valid " +
47 OFFICE_PRODUCT_NAME
+ " document.");
52 private boolean isOfficeFile(File file
) {
53 for (int i
= 0; i
< OFFICE_EXTENSIONS
.length
; i
++)
54 if (file
.getName().endsWith(OFFICE_EXTENSIONS
[i
]))
59 public Iterator
<String
> getParcels() {
61 ArrayList
<String
> parcels
= new ArrayList
<String
>();
66 zp
= new ZipFile(this.file
);
68 for (Enumeration enumer
= zp
.entries(); enumer
.hasMoreElements(); )
70 ZipEntry ze
= (ZipEntry
)enumer
.nextElement();
71 if (ze
.getName().endsWith(ParcelZipper
.PARCEL_DESCRIPTOR_XML
))
73 String tmp
= ze
.getName();
74 int end
= tmp
.lastIndexOf("/");
75 tmp
= tmp
.substring(0, end
);
77 String parcelName
= ze
.getName().substring(0, end
);
78 parcels
.add(parcelName
);
82 catch(ZipException ze
) {
85 catch(IOException ioe
) {
86 ioe
.printStackTrace();
93 catch (IOException asdf
) {
98 return parcels
.iterator();
101 public boolean removeParcel(String parcelName
) {
104 ParcelZipper
.getParcelZipper().removeParcel(file
, parcelName
);
106 catch (IOException ioe
) {
107 ioe
.printStackTrace();