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