|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objecthudson.Util
public class Util
Various utility methods that don't have more proper home.
Field Summary | |
---|---|
static boolean |
NO_SYMLINK
On Unix environment that cannot run "ln", set this to true. |
static org.apache.commons.lang.time.FastDateFormat |
RFC822_DATETIME_FORMATTER
|
static boolean |
SYMLINK_ESCAPEHATCH
|
static org.apache.commons.lang.time.FastDateFormat |
XS_DATETIME_FORMATTER
|
Constructor Summary | |
---|---|
Util()
|
Method Summary | ||
---|---|---|
static File |
changeExtension(File dst,
String ext)
Returns a file name by changing its extension. |
|
static String |
combine(long n,
String suffix)
Deprecated. Use individual localization methods instead. See Messages.Util_year(Object) for an example.
Deprecated since 2009-06-24, remove method after 2009-12-24. |
|
static void |
copyFile(File src,
File dst)
Copies a single file by using Ant. |
|
static void |
copyStream(InputStream in,
OutputStream out)
|
|
static void |
copyStream(Reader in,
Writer out)
|
|
static void |
copyStreamAndClose(InputStream in,
OutputStream out)
|
|
static void |
copyStreamAndClose(Reader in,
Writer out)
|
|
static org.apache.tools.ant.types.FileSet |
createFileSet(File baseDir,
String includes)
|
|
static org.apache.tools.ant.types.FileSet |
createFileSet(File baseDir,
String includes,
String excludes)
Creates Ant FileSet with the base dir and include pattern. |
|
static
|
createSubList(Collection<?> source,
Class<T> type)
Create a sub-list by only picking up instances of the specified type. |
|
static void |
createSymlink(File baseDir,
String targetPath,
String symlinkPath,
TaskListener listener)
Creates a symlink to targetPath at baseDir+symlinkPath. |
|
static File |
createTempDir()
Creates a new temporary directory. |
|
static void |
deleteContentsRecursive(File file)
Deletes the contents of the given directory (but not the directory itself) recursively. |
|
static void |
deleteFile(File f)
Deletes this file (and does not take no for an answer). |
|
static void |
deleteRecursive(File dir)
|
|
static void |
displayIOException(IOException e,
TaskListener listener)
On Windows, error messages for IOException aren't very helpful. |
|
static String |
encode(String s)
Escapes non-ASCII characters in URL. |
|
static String |
encodeRFC2396(String url)
Deprecated. since 2008-05-13. This method is broken (see ISSUE#1666). It should probably be removed but I'm not sure if it is considered part of the public API that needs to be maintained for backwards compatibility. Use encode(String) instead. |
|
static String |
ensureEndsWith(String subject,
String suffix)
Ensure string ends with suffix |
|
static String |
escape(String text)
Escapes HTML unsafe characters like <, & to the respective character entities. |
|
static
|
filter(Iterable<?> base,
Class<T> type)
Creates a filtered sublist. |
|
static
|
filter(List<?> base,
Class<T> type)
Creates a filtered sublist. |
|
static String |
fixEmpty(String s)
Convert empty string to null. |
|
static String |
fixEmptyAndTrim(String s)
Convert empty string to null, and trim whitespace. |
|
static
|
fixNull(Collection<T> l)
|
|
static
|
fixNull(Iterable<T> l)
|
|
static
|
fixNull(List<T> l)
|
|
static
|
fixNull(Set<T> l)
|
|
static String |
fixNull(String s)
Convert null to "". |
|
static byte[] |
fromHexString(String data)
|
|
static String |
getDigestOf(InputStream source)
Computes MD5 digest of the given input stream. |
|
static String |
getDigestOf(String text)
|
|
static String |
getFileName(String filePath)
Cuts all the leading path portion and get just the file name. |
|
static String |
getHostName()
Guesses the current host name. |
|
static String |
getPastTimeString(long duration)
Get a human readable string representing strings like "xxx days ago", which should be used to point to the occurrence of an event in the past. |
|
static String |
getTimeSpanString(long duration)
Returns a human readable text of the time duration, for example "3 minutes 40 seconds". |
|
static String |
getWin32ErrorMessage(int n)
Gets a human readable message for the given Win32 error code. |
|
static String |
getWin32ErrorMessage(IOException e)
|
|
static String |
getWin32ErrorMessage(Throwable e)
Extracts the Win32 error message from Throwable if possible. |
|
static String |
intern(String s)
Null-safe String intern method. |
|
static boolean |
isAbsoluteUri(String uri)
Return true if the systemId denotes an absolute URI . |
|
static boolean |
isOverridden(Class base,
Class derived,
String methodName,
Class... types)
Checks if the public method defined on the base type with the given arguments are overridden in the given derived type. |
|
static boolean |
isSymlink(File file)
Checks if the given file represents a symlink. |
|
static String |
join(Collection<?> strings,
String separator)
Concatenate multiple strings by inserting a separator. |
|
static
|
join(Collection<? extends T>... items)
Combines all the given collections into a single list. |
|
static String |
loadFile(File logfile)
Loads the contents of a file into a string. |
|
static String |
loadFile(File logfile,
Charset charset)
|
|
static Properties |
loadProperties(String properties)
Loads a key/value pair string as Properties |
|
static String[] |
mapToEnv(Map<String,String> m)
Converts the map format of the environment variables to the K=V format in the array. |
|
static int |
min(int x,
int... values)
|
|
static String |
nullify(String v)
|
|
static String |
rawEncode(String s)
Encode a single path component for use in an HTTP URL. |
|
static String |
removeTrailingSlash(String s)
|
|
static String |
replaceMacro(String s,
Map<String,String> properties)
Replaces the occurrence of '$key' by properties.get('key'). |
|
static String |
replaceMacro(String s,
VariableResolver<String> resolver)
Replaces the occurrence of '$key' by resolver.get('key'). |
|
static String |
resolveSymlink(File link)
Resolves symlink, if the given file is a symlink. |
|
static String |
resolveSymlink(File link,
TaskListener listener)
Deprecated. as of 1.456 Use resolveSymlink(File) |
|
static File |
resolveSymlinkToFile(File link)
Resolves a symlink to the File that points to. |
|
static String |
singleQuote(String s)
Surrounds by a single-quote. |
|
static SecretKey |
toAes128Key(String s)
Converts a string into 128-bit AES key. |
|
static String |
toHexString(byte[] bytes)
|
|
static String |
toHexString(byte[] data,
int start,
int len)
|
|
static String[] |
tokenize(String s)
|
|
static String[] |
tokenize(String s,
String delimiter)
Tokenizes the text separated by delimiters. |
|
static void |
touch(File file)
Creates an empty file. |
|
static Number |
tryParseNumber(String numberStr,
Number defaultNumber)
Returns the parsed string if parsed successful; otherwise returns the default number. |
|
static String |
wrapToErrorSpan(String s)
Wraps with the error icon and the CSS class to render error message. |
|
static String |
xmlEscape(String text)
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final org.apache.commons.lang.time.FastDateFormat XS_DATETIME_FORMATTER
public static final org.apache.commons.lang.time.FastDateFormat RFC822_DATETIME_FORMATTER
public static boolean NO_SYMLINK
public static boolean SYMLINK_ESCAPEHATCH
Constructor Detail |
---|
public Util()
Method Detail |
---|
public static <T> List<T> filter(Iterable<?> base, Class<T> type)
public static <T> List<T> filter(List<?> base, Class<T> type)
public static String replaceMacro(String s, Map<String,String> properties)
Unlike shell, undefined variables are left as-is (this behavior is the same as Ant.)
public static String replaceMacro(String s, VariableResolver<String> resolver)
Unlike shell, undefined variables are left as-is (this behavior is the same as Ant.)
public static String loadFile(File logfile) throws IOException
IOException
public static String loadFile(File logfile, Charset charset) throws IOException
IOException
public static void deleteContentsRecursive(File file) throws IOException
IOException
- if the operation fails.public static void deleteFile(File f) throws IOException
f
- a file to delete
IOException
- if it exists but could not be successfully deletedpublic static void deleteRecursive(File dir) throws IOException
IOException
public static boolean isSymlink(File file) throws IOException
IOException
public static File createTempDir() throws IOException
IOException
public static void displayIOException(IOException e, TaskListener listener)
public static String getWin32ErrorMessage(IOException e)
public static String getWin32ErrorMessage(Throwable e)
Throwable
if possible.
public static String getWin32ErrorMessage(int n)
public static String getHostName()
public static void copyStream(InputStream in, OutputStream out) throws IOException
IOException
public static void copyStream(Reader in, Writer out) throws IOException
IOException
public static void copyStreamAndClose(InputStream in, OutputStream out) throws IOException
IOException
public static void copyStreamAndClose(Reader in, Writer out) throws IOException
IOException
public static String[] tokenize(String s, String delimiter)
In 1.210, this method was changed to handle quotes like Unix shell does.
Before that, this method just used StringTokenizer
.
QuotedStringTokenizer
public static String[] tokenize(String s)
public static String[] mapToEnv(Map<String,String> m)
public static int min(int x, int... values)
public static String nullify(String v)
public static String removeTrailingSlash(String s)
public static String ensureEndsWith(String subject, String suffix)
subject
- Examined stringsuffix
- Desired suffix
public static String getDigestOf(InputStream source) throws IOException
source
- The stream will be closed by this method at the end of this method.
IOException
public static String getDigestOf(String text)
public static SecretKey toAes128Key(String s)
public static String toHexString(byte[] data, int start, int len)
public static String toHexString(byte[] bytes)
public static byte[] fromHexString(String data)
public static String getTimeSpanString(long duration)
duration
- number of milliseconds.public static String getPastTimeString(long duration)
public static String combine(long n, String suffix)
Messages.Util_year(Object)
for an example.
Deprecated since 2009-06-24, remove method after 2009-12-24.
public static <T> List<T> createSubList(Collection<?> source, Class<T> type)
public static String encode(String s)
Note that this methods only escapes non-ASCII but leaves other URL-unsafe characters,
such as '#'.
rawEncode(String)
should generally be used instead, though be careful to pass only
a single path component to that method (it will encode /, but this method does not).
public static String rawEncode(String s)
"#%<>[\]^`{|}~
)
and HTTP special characters (/;:?
) as specified in RFC1738.
(so alphanumeric and !@$&*()-_=+',.
are not encoded)
Note that slash (/
) is encoded, so the given string should be a
single path component used in constructing a URL.
Method name inspired by PHP's rawurlencode.
public static String singleQuote(String s)
public static String escape(String text)
public static String xmlEscape(String text)
public static void touch(File file) throws IOException
IOException
public static void copyFile(File src, File dst) throws org.apache.tools.ant.BuildException
org.apache.tools.ant.BuildException
public static String fixNull(String s)
public static String fixEmpty(String s)
public static String fixEmptyAndTrim(String s)
public static <T> List<T> fixNull(List<T> l)
public static <T> Set<T> fixNull(Set<T> l)
public static <T> Collection<T> fixNull(Collection<T> l)
public static <T> Iterable<T> fixNull(Iterable<T> l)
public static String getFileName(String filePath)
public static String join(Collection<?> strings, String separator)
public static <T> List<T> join(Collection<? extends T>... items)
public static org.apache.tools.ant.types.FileSet createFileSet(File baseDir, String includes, String excludes)
FileSet
with the base dir and include pattern.
The difference with this and using AbstractFileSet.setIncludes(String)
is that this method doesn't treat whitespace as a pattern separator,
which makes it impossible to use space in the file path.
includes
- String like "foo/bar/*.xml" Multiple patterns can be separated
by ',', and whitespace can surround ',' (so that you can write
"abc, def" and "abc,def" to mean the same thing.excludes
- Exclusion pattern. Follows the same format as the 'includes' parameter.
Can be null.public static org.apache.tools.ant.types.FileSet createFileSet(File baseDir, String includes)
public static void createSymlink(File baseDir, String targetPath, String symlinkPath, TaskListener listener) throws InterruptedException
If there's a prior symlink at baseDir+symlinkPath, it will be overwritten.
baseDir
- Base directory to resolve the 'symlinkPath' parameter.targetPath
- The file that the symlink should point to. Usually relative to the directory of the symlink but may instead be an absolute path.symlinkPath
- Where to create a symlink in (relative to baseDir
)
InterruptedException
public static String resolveSymlink(File link, TaskListener listener) throws InterruptedException, IOException
resolveSymlink(File)
InterruptedException
IOException
public static File resolveSymlinkToFile(File link) throws InterruptedException, IOException
File
that points to.
InterruptedException
IOException
public static String resolveSymlink(File link) throws InterruptedException, IOException
If the resolution fails, report an error.
InterruptedException
IOException
@Deprecated public static String encodeRFC2396(String url)
encode(String)
instead.
public static String wrapToErrorSpan(String s)
public static Number tryParseNumber(String numberStr, Number defaultNumber)
numberStr
- string to parsedefaultNumber
- number to return if the string can not be parsed
public static boolean isOverridden(Class base, Class derived, String methodName, Class... types)
public static File changeExtension(File dst, String ext)
ext
- For example, ".zip"public static String intern(String s)
public static boolean isAbsoluteUri(String uri)
URI
, but
implementing this by ourselves allow it to be more lenient about
escaping of URI.
@IgnoreJRERequirement public static Properties loadProperties(String properties) throws IOException
Properties
IOException
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |