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.File;
27 import java.io.FileReader;
28 import java.io.FileWriter;
29 import java.io.BufferedReader;
30 import java.io.BufferedWriter;
31 import java.io.IOException;
32 
33 import org.openoffice.idesupport.zip.ParcelZipper;
34 
35 public class PackageRemover {
PackageRemover()36     private PackageRemover() {
37     }
38 
removeDeclaration(File source)39     public static void removeDeclaration(File source) throws IOException {
40         File tmp = new File(source.getAbsolutePath() + ".tmp");
41 
42         BufferedReader in = new BufferedReader(new FileReader(source));
43         BufferedWriter out = new BufferedWriter(new FileWriter(tmp));
44 
45         try {
46             String line;
47             while ((line = in.readLine()) != null) {
48                 if (line.startsWith("package")) {
49                     String newDeclaration = evaluate(line);
50                     if (newDeclaration != null) {
51                         out.write(newDeclaration, 0, newDeclaration.length());
52                         out.newLine();
53                     }
54                 }
55                 else {
56                     out.write(line, 0, line.length());
57                     out.newLine();
58                 }
59             }
60         }
61         finally {
62             if (in != null) {
63                 in.close();
64             }
65             if (out != null) {
66                 out.close();
67             }
68         }
69 
70         if (source.delete() == false) {
71             tmp.delete();
72             throw new IOException("Could not overwrite " + source);
73         }
74         else {
75             tmp.renameTo(source);
76         }
77     }
78 
evaluate(String line)79     public static String evaluate(String line) {
80 
81         int idx = line.indexOf(ParcelZipper.CONTENTS_DIRNAME);
82         if (idx == -1)
83             return line;
84 
85         idx = idx + ParcelZipper.CONTENTS_DIRNAME.length();
86         if (line.charAt(idx) == '.')
87             return "package " + line.substring(idx + 1);;
88 
89         return null;
90     }
91 
main(String[] args)92     public static void main(String[] args) {
93         File source = new File(args[0]);
94 
95         try {
96             removeDeclaration(source);
97         }
98         catch (IOException ioe) {
99             ioe.printStackTrace();
100         }
101     }
102 }
103