1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.core.tests.util;
14 import java.io.FileInputStream;
15 import java.io.FileNotFoundException;
16 import java.io.FileOutputStream;
17 import java.io.IOException;
18 import java.io.PrintWriter;
19 import java.net.ServerSocket;
21 import net.sourceforge.phpdt.internal.compiler.batch.CompilationUnit;
24 public static String OUTPUT_DIRECTORY = "comptest";
26 public static CompilationUnit[] compilationUnits(String[] testFiles) {
27 int length = testFiles.length / 2;
28 CompilationUnit[] result = new CompilationUnit[length];
30 for (int i = 0; i < length; i++) {
31 result[i] = new CompilationUnit(testFiles[index + 1].toCharArray(),
32 testFiles[index], null);
38 public static String[] concatWithClassLibs(String classpath, boolean inFront) {
39 String[] classLibs = getJavaClassLibs();
40 final int length = classLibs.length;
41 String[] defaultClassPaths = new String[length + 1];
43 System.arraycopy(classLibs, 0, defaultClassPaths, 1, length);
44 defaultClassPaths[0] = classpath;
46 System.arraycopy(classLibs, 0, defaultClassPaths, 0, length);
47 defaultClassPaths[length] = classpath;
49 return defaultClassPaths;
52 public static String convertToIndependantLineDelimiter(String source) {
53 StringBuffer buffer = new StringBuffer();
54 for (int i = 0, length = source.length(); i < length; i++) {
55 char car = source.charAt(i);
58 if (i < length - 1 && source.charAt(i + 1) == '\n') {
59 i++; // skip \n after \r
65 return buffer.toString();
69 * Copy the given source (a file or a directory that must exists) to the
70 * given destination (a directory that must exists).
72 public static void copy(String sourcePath, String destPath) {
73 sourcePath = toNativePath(sourcePath);
74 destPath = toNativePath(destPath);
75 File source = new File(sourcePath);
78 File dest = new File(destPath);
81 if (source.isDirectory()) {
82 String[] files = source.list();
84 for (int i = 0; i < files.length; i++) {
85 String file = files[i];
86 File sourceFile = new File(source, file);
87 if (sourceFile.isDirectory()) {
88 File destSubDir = new File(dest, file);
90 copy(sourceFile.getPath(), destSubDir.getPath());
92 copy(sourceFile.getPath(), dest.getPath());
97 FileInputStream in = null;
98 FileOutputStream out = null;
100 in = new FileInputStream(source);
101 File destFile = new File(dest, source.getName());
102 if (destFile.exists() && !destFile.delete()) {
103 throw new IOException(destFile + " is in use");
105 out = new FileOutputStream(destFile);
106 int bufferLength = 1024;
107 byte[] buffer = new byte[bufferLength];
110 read = in.read(buffer, 0, bufferLength);
112 out.write(buffer, 0, read);
115 } catch (IOException e) {
116 throw new Error(e.toString());
121 } catch (IOException e) {
127 } catch (IOException e) {
134 // public static void createJar(String[] pathsAndContents, Map options,
136 // jarPath) throws IOException {
137 // String classesPath = getOutputDirectory() + File.separator + "classes";
138 // File classesDir = new File(classesPath);
139 // flushDirectoryContent(classesDir);
140 // compile(pathsAndContents, options, classesPath);
141 // zip(classesDir, jarPath);
144 * Generate a display string from the given String.
147 * number of tabs are added at the begining of each line.
150 * [org.eclipse.jdt.core.tests.util.Util.displayString("abc\ndef\tghi")]
152 public static String displayString(String inputString) {
153 return displayString(inputString, 0);
157 * Generate a display string from the given String. It converts:
164 * <li>\" to \\\"</li>
166 * <li>\\ to \\\\</li>
167 * <li>All other characters are unchanged.</li>
169 * This method doesn't convert \r\n to \n.
171 * Example of use: <o>
175 * input string = "abc\ndef\tghi",
177 * result = "\"\t\t\tabc\\n" +
178 * "\t\t\tdef\tghi\""
185 * input string = "abc\ndef\tghi\n",
187 * result = "\"\t\t\tabc\\n" +
188 * "\t\t\tdef\tghi\\n\""
195 * input string = "abc\r\ndef\tghi\r\n",
197 * result = "\"\t\t\tabc\\r\\n" +
198 * "\t\t\tdef\tghi\\r\\n\""
206 * the given input string
208 * number of tabs are added at the begining of each line.
210 * @return the displayed string
212 public static String displayString(String inputString, int indent) {
213 int length = inputString.length();
214 StringBuffer buffer = new StringBuffer(length);
215 java.util.StringTokenizer tokenizer = new java.util.StringTokenizer(
216 inputString, "\n\r", true);
217 for (int i = 0; i < indent; i++)
220 while (tokenizer.hasMoreTokens()) {
222 String token = tokenizer.nextToken();
223 if (token.equals("\r")) {
224 buffer.append("\\r");
225 if (tokenizer.hasMoreTokens()) {
226 token = tokenizer.nextToken();
227 if (token.equals("\n")) {
228 buffer.append("\\n");
229 if (tokenizer.hasMoreTokens()) {
230 buffer.append("\" + \n");
231 for (int i = 0; i < indent; i++)
237 buffer.append("\" + \n");
238 for (int i = 0; i < indent; i++)
245 } else if (token.equals("\n")) {
246 buffer.append("\\n");
247 if (tokenizer.hasMoreTokens()) {
248 buffer.append("\" + \n");
249 for (int i = 0; i < indent; i++)
256 StringBuffer tokenBuffer = new StringBuffer();
257 for (int i = 0; i < token.length(); i++) {
258 char c = token.charAt(i);
261 tokenBuffer.append("\\r");
264 tokenBuffer.append("\\n");
267 tokenBuffer.append("\\b");
270 tokenBuffer.append("\t");
273 tokenBuffer.append("\\f");
276 tokenBuffer.append("\\\"");
279 tokenBuffer.append("\\'");
282 tokenBuffer.append("\\\\");
285 tokenBuffer.append(c);
288 buffer.append(tokenBuffer.toString());
291 return buffer.toString();
295 * Reads the content of the given source file and converts it to a display
299 * [org.eclipse.jdt.core.tests.util.Util.fileContentToDisplayString("c:/temp/X.java",
302 public static String fileContentToDisplayString(String sourceFilePath,
303 int indent, boolean independantLineDelimiter) {
304 File sourceFile = new File(sourceFilePath);
305 if (!sourceFile.exists()) {
306 System.out.println("File " + sourceFilePath + " does not exists.");
309 if (!sourceFile.isFile()) {
310 System.out.println(sourceFilePath + " is not a file.");
313 StringBuffer sourceContentBuffer = new StringBuffer();
314 FileInputStream input = null;
316 input = new FileInputStream(sourceFile);
317 } catch (FileNotFoundException e) {
325 sourceContentBuffer.append((char) read);
327 } while (read != -1);
329 } catch (IOException e) {
335 } catch (IOException e2) {
338 String sourceString = sourceContentBuffer.toString();
339 if (independantLineDelimiter) {
340 sourceString = convertToIndependantLineDelimiter(sourceString);
342 return displayString(sourceString, indent);
346 * Reads the content of the given source file, converts it to a display
347 * string. If the destination file path is not null, writes the result to
348 * this file. Otherwise writes it to the console.
351 * [org.eclipse.jdt.core.tests.util.Util.fileContentToDisplayString("c:/temp/X.java",
354 public static void fileContentToDisplayString(String sourceFilePath,
355 int indent, String destinationFilePath,
356 boolean independantLineDelimiter) {
357 String displayString = fileContentToDisplayString(sourceFilePath,
358 indent, independantLineDelimiter);
359 if (destinationFilePath == null) {
360 System.out.println(displayString);
363 writeToFile(displayString, destinationFilePath);
367 * Flush content of a given directory (leaving it empty), no-op if not a
370 public static void flushDirectoryContent(File dir) {
371 if (dir.isDirectory()) {
372 String[] files = dir.list();
375 for (int i = 0, max = files.length; i < max; i++) {
376 File current = new File(dir, files[i]);
377 if (current.isDirectory()) {
378 flushDirectoryContent(current);
386 * Search the user hard-drive for a Java class library. Returns null if none
389 * Example of use: [org.eclipse.jdt.core.tests.util.Util.getJavaClassLib()]
391 public static String[] getJavaClassLibs() {
392 String jreDir = getJREDirectory();
393 if (jreDir == null) {
394 return new String[] {};
396 final String vmName = System.getProperty("java.vm.name");
397 if ("J9".equals(vmName)) {
398 return new String[] { toNativePath(jreDir
399 + "/lib/jclMax/classes.zip") };
401 File file = new File(jreDir + "/lib/rt.jar");
403 return new String[] { toNativePath(jreDir + "/lib/rt.jar") };
405 return new String[] {
406 toNativePath(jreDir + "/lib/core.jar"),
407 toNativePath(jreDir + "/lib/security.jar"),
408 toNativePath(jreDir + "/lib/graphics.jar") };
414 public static String getJavaClassLibsAsString() {
415 String[] classLibs = getJavaClassLibs();
416 StringBuffer buffer = new StringBuffer();
417 for (int i = 0, max = classLibs.length; i < max; i++) {
418 buffer.append(classLibs[i]).append(File.pathSeparatorChar);
421 return buffer.toString();
425 * Returns the JRE directory this tests are running on. Returns null if none
428 * Example of use: [org.eclipse.jdt.core.tests.util.Util.getJREDirectory()]
430 public static String getJREDirectory() {
431 return System.getProperty("java.home");
435 * Search the user hard-drive for a possible output directory. Returns null
436 * if none could be found.
439 * [org.eclipse.jdt.core.tests.util.Util.getOutputDirectory()]
441 public static String getOutputDirectory() {
442 String container = System.getProperty("user.home");
443 if (container == null) {
446 return toNativePath(container) + File.separator + OUTPUT_DIRECTORY;
451 * Returns the next available port number on the local host.
453 public static int getFreePort() {
454 ServerSocket socket = null;
456 socket = new ServerSocket(0);
457 return socket.getLocalPort();
458 } catch (IOException e) {
461 if (socket != null) {
464 } catch (IOException e) {
473 * Makes the given path a path using native path separators as returned by
474 * File.getPath() and trimming any extra slash.
476 public static String toNativePath(String path) {
477 String nativePath = path.replace('\\', File.separatorChar).replace('/',
479 return nativePath.endsWith("/") || nativePath.endsWith("\\") ? nativePath
480 .substring(0, nativePath.length() - 1)
484 public static void writeToFile(String contents, String destinationFilePath) {
485 File destFile = new File(destinationFilePath);
486 FileOutputStream output = null;
488 output = new FileOutputStream(destFile);
489 PrintWriter writer = new PrintWriter(output);
490 writer.print(contents);
492 } catch (IOException e) {
496 if (output != null) {
499 } catch (IOException e2) {