]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/jarpackagerfat/FatJarPackagerUtil.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / jarpackagerfat / FatJarPackagerUtil.java
CommitLineData
1b2798f6
EK
1/*******************************************************************************
2 * Copyright (c) 2000, 2009 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 * Ferenc Hechler, ferenc_hechler@users.sourceforge.net - 83258 [jar exporter] Deploy java application as executable jar
11 * Ferenc Hechler, ferenc_hechler@users.sourceforge.net - 213638 [jar exporter] create ANT build file for current settings
12 * Ferenc Hechler, ferenc_hechler@users.sourceforge.net - 219530 [jar application] add Jar-in-Jar ClassLoader option
13 *******************************************************************************/
14package org.eclipse.jdt.internal.ui.jarpackagerfat;
15
16import java.io.ByteArrayOutputStream;
17import java.io.File;
18import java.io.IOException;
19import java.io.InputStream;
20
21import org.eclipse.swt.widgets.Display;
22import org.eclipse.swt.widgets.Shell;
23
24import org.eclipse.jface.dialogs.MessageDialog;
25
26import org.eclipse.jdt.internal.corext.util.Messages;
27
28import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels;
29
30/**
31 * Utility methods for Runnable JAR Import/Export.
32 */
33public final class FatJarPackagerUtil {
34
35 static final String ANTSCRIPT_EXTENSION= "xml"; //$NON-NLS-1$
36
37 private FatJarPackagerUtil() {
38 // Do nothing
39 }
40
41 public static boolean askToCreateAntScriptDirectory(final Shell parent, File directory) {
42 if (parent == null)
43 return false;
44
45 return queryDialog(parent, FatJarPackagerMessages.FatJarPackage_confirmCreate_title, Messages.format(FatJarPackagerMessages.FatJarPackageAntScript_confirmCreate_message, BasicElementLabels.getPathLabel(directory)));
46 }
47
48 private static boolean queryDialog(final Shell parent, final String title, final String message) {
49 Display display= parent.getDisplay();
50 if (display == null || display.isDisposed())
51 return false;
52
53 final boolean[] returnValue= new boolean[1];
54 Runnable runnable= new Runnable() {
55 public void run() {
56 returnValue[0]= MessageDialog.openQuestion(parent, title, message);
57 }
58 };
59 display.syncExec(runnable);
60
61 return returnValue[0];
62 }
63
64 public static byte[] readInputStream(InputStream is) throws IOException {
65 ByteArrayOutputStream result= new ByteArrayOutputStream();
66 byte[] buf= new byte[1024];
67 int cnt= is.read(buf);
68 while (cnt > 0) {
69 result.write(buf, 0, cnt);
70 cnt= is.read(buf);
71 }
72 return result.toByteArray();
73 }
74
75 /**
76 * Increments [nr] for files in the format "[name][_nr][.ext]".<br>
77 * Increment of "[name][.ext]" is "[name]_2[.ext]<br>
78 * Increment of "[name]_2[.ext]" is "[name]_3[.ext]<br>
79 * [.ext] might be empty<br>
80 *
81 * @param fileName the file name to increment
82 * @return incremented filename
83 */
84 public static String nextNumberedFileName(String fileName) {
85 String name;
86 String number;
87 String ext;
88 if (fileName.matches(".*[_]\\d+[.][^.]*")) { //$NON-NLS-1$
89 name= fileName.replaceFirst("(.*)[_](\\d+)([.][^.]*)", "$1"); //$NON-NLS-1$ //$NON-NLS-2$
90 number= fileName.replaceFirst("(.*)[_](\\d+)([.][^.]*)", "$2"); //$NON-NLS-1$ //$NON-NLS-2$
91 ext= fileName.replaceFirst("(.*)[_](\\d+)([.][^.]*)", "$3"); //$NON-NLS-1$ //$NON-NLS-2$
92 } else if (fileName.matches(".*[.][^.]*")) { //$NON-NLS-1$
93 name= fileName.replaceFirst("(.*)([.][^.]*)", "$1"); //$NON-NLS-1$ //$NON-NLS-2$
94 number= "1"; //$NON-NLS-1$
95 ext= fileName.replaceFirst("(.*)([.][^.]*)", "$2"); //$NON-NLS-1$ //$NON-NLS-2$
96 } else {
97 name= fileName;
98 number= "1"; //$NON-NLS-1$
99 ext= ""; //$NON-NLS-1$
100 }
101 fileName= name + "_" + (Integer.parseInt(number) + 1) + ext; //$NON-NLS-1$
102 return fileName;
103 }
104
105}