Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7a49d7aaae0a00e96976fd95d6d2604364c758b5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*******************************************************************************
* Copyright (c) 2010, 2011 Oracle. All rights reserved.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0, which accompanies this distribution
* and is available at http://www.eclipse.org/legal/epl-v10.html.
* 
* Contributors:
*     Oracle - initial API and implementation
*******************************************************************************/
package org.eclipse.jpt.dbws.eclipselink.core.gen.internal;

import static java.util.logging.Level.SEVERE;

import java.io.File;
import java.text.MessageFormat;
import java.util.logging.Level;

/**
 *  Tools
 */
public final class Tools
{
	/** empty string */
	public static final String EMPTY_STRING = ""; //$NON-NLS-1$

	private static final String USER_DIRECTORY = System.getProperty("user.dir");	//$NON-NLS-1$

	// ********** queries **********

	/**
	 * Return whether the specified string is null, empty, or contains
	 * only whitespace characters.
	 */
	public static boolean stringIsEmpty(String string) {
		if (string == null) {
			return true;
		}
		int len = string.length();
		if (len == 0) {
			return true;
		}
		return stringIsEmpty_(string.toCharArray(), len);
	}	

	private static boolean stringIsEmpty_(char[] s, int len) {
		for (int i = len; i-- > 0; ) {
			if ( ! Character.isWhitespace(s[i])) {
				return false;
			}
		}
		return true;
	}

	// ********** short name manipulation **********

	
	public static File buildDirectory(String dirName) {
    	File currentDir = currentWorkingDirectory();
    	File dir = new File(currentDir, dirName);
    	dir.mkdir();
		return dir;
	}
	
	/**
	 * Return a file representing the current working directory.
	 */
	public static File currentWorkingDirectory() {
		return new File(USER_DIRECTORY); //$NON-NLS-1$
	}
	
	/**
	 * Strip the extension from the specified file name
	 * and return the result. If the file name has no
	 * extension, it is returned unchanged
	 * File#basePath()
	 */
	public static String stripExtension(String fileName) {
		int index = fileName.lastIndexOf('.');
		if (index == -1) {
			return fileName;
		}
		return fileName.substring(0, index);
	}

	public static String extractDirectory(String path) {
		if( ! path.contains(File.separator)) {
			return EMPTY_STRING;	
		}
		return path.substring(0, path.lastIndexOf(File.separator));
	}
	
	public static void logMessage(Level level, String message) {
		if(level == SEVERE) {
			System.err.println('\n' + message);
		}
		else {
			System.out.println('\n' + message);
		}
	}
	
	// ********** NLS utilities **********

	public static String getString(String key) {
		return JptDbwsCoreMessages.getString(key);
	}
	
	public static String bind(String key, Object argument) {
		return MessageFormat.format(getString(key), argument);
	}
}

Back to the top