Util.java revision 1195
* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. * Portions Copyright 2011 Jens Elkner. * Class for useful functions. public final class Util {
* Return a string which represents a <code>CharSequence</code> in HTML. * @return a string representing the character sequence in HTML * Append a character sequence to the given destination whereby * special characters for HTML are escaped accordingly. * @param q a character sequence to esacpe * @param dest where to append the character sequence to for (
int i =
0; i < q.
length(); i++ ) {
* Append a character array to the given destination whereby * special characters for HTML are escaped accordingly. * @param cs characters to esacpe * @param length max. number of characters to append, starting from index 0. * @param dest where to append the character sequence to for (
int i =
0; i <
length; i++ ) {
* Append a character to the given destination whereby special characters * special for HTML are escaped accordingly. * @param c the character to append * @param dest where to append the character to * used by BUI - CSS needs this parameter for proper cache refresh (per * changeset) in client browser jel: but useless, since the page cached * @return html escaped version (hg changeset) * Convinience method for {@code breadcrumbPath(urlPrefix, path, '/')}. * @param urlPrefix prefix to add to each url * @param path path to crack * @return HTML markup fro the breadcrumb or the path itself. * @see #breadcrumbPath(String, String, char) * {@code breadcrumbPath(urlPrefix, path, sep, "", false)}. * @param urlPrefix prefix to add to each url * @param path path to crack * @param sep separator to use to crack the given path * @return HTML markup fro the breadcrumb or the path itself. * @see #breadcrumbPath(String, String, char, String, boolean, boolean) * {@code breadcrumbPath(urlPrefix, path, sep, "", false, path.endsWith(sep)}. * @param urlPrefix prefix to add to each url * @param path path to crack * @param sep separator to use to crack the given path * @param urlPostfix suffix to add to each url * @param compact if {@code true} the given path gets transformed into * its canonical form (.i.e. all '.' and '..' and double separators * removed, but not always resolves to an absolute path) before processing * @return HTML markup fro the breadcrumb or the path itself. * @see #breadcrumbPath(String, String, char, String, boolean, boolean) * @see #getCanonicalPath(String, char) * Create a breadcrumb path to allow navigation to each element of a path. * Consecutive separators (<var>sep</var>) in the given <var>path</var> are * always collapsed into a single separator automatically. If * <var>compact</var> is {@code true} path gets translated into a canonical * path similar to {@link File#getCanonicalPath()}, however the current * working directory is assumed to be "/" and no checks are done (e.g. * neither whether the path [component] exists nor which type it is). * what should be prepend to the constructed URL * the full path from which the breadcrumb path is built. * the character that separates the path components in * what should be append to the constructed URL * if {@code true}, a canonical path gets constructed before * if {@code true} a "/" gets append to the last path component's * link and <var>sep</var> to its name * @return <var>path</var> if it resolves to an empty or "/" or * {@code null} path, the HTML markup for the breadcrumb path otherwise. * Normalize the given <var>path</var> to its canonical form. I.e. all * separators (<var>sep</var>) are replaced with a slash ('/'), all * double slashes are replaced by a single slash, all single dot path * components (".") of the formed path are removed and all double dot path * components (".." ) of the formed path are replaced with its parent or * '/' if there is no parent. * So the difference to {@link File#getCanonicalPath()} is, that this method * does not hit the disk (just string manipulation), resolves <var>path</var> * always against '/' and thus always returns an absolute path, which may * actually not exist, and which has a single trailing '/' if the given * <var>path</var> ends with the given <var>sep</var>. * @param path path to mangle. If not absolute or {@code null}, the * current working directory is assumed to be '/'. * @param sep file separator to use to crack <var>path</var> into path * @return always a canonical path which starts with a '/'. // since is not a general purpose method. So we waive to handle // || path.endsWith("/..") || path.endsWith("/.") * Remove all empty and {@code null} string elements from the given * <var>names</var> and optionally all redundant information like "." and * if {@code true}, remove redundant elements as well. * @return a possible empty array of names all with a length > 0. * Generate a regex that matches the specified character. Escape it in case * it is a character that has a special meaning in a regex. * the character that the regex should match * @return a six-character string on the form <tt>\u</tt><i>hhhh</i> for (
int i =
0; i <
4 -
hex.
length(); i++ ) {
* Convert the given size into a human readable string. * @param num size to convert. * @return a readable string }
else if (l <
1048576) {
* Converts different html special characters into their encodings used in * html. Currently used only for tooltips of annotation revision number view * @return encoded text for use in <a title=""> tag for (
int i =
0; i < s.
length(); i++ ) {
* Write out line information wrt. to the given annotation in the format: * {@code Linenumber Blame Author} incl. appropriate links. * @param num linenumber to print * @param out print destination * @param annotation annotation to use. If {@code null} only the * linenumber gets printed. * @param userPageLink see {@link RuntimeEnvironment#getUserPage()} * @param userPageSuffix see {@link RuntimeEnvironment#getUserPageSuffix()} * @throws IOException depends on the destination (<var>out</var>). // this method should go to JFlexXref * Generate a string from the given path and date in a way that allows * stable lexicographic sorting (i.e. gives always the same results) as a * walk of the file hierarchy. Thus null character (\u0000) is used both * to separate directory components and to separate the path from the date. * @param path path to mangle. * @param date date string to use. * @return the mangled path. * The reverse operation for {@link #path2uid(String, String)} - re-creates * the unmangled path from the given uid. * @param uid uid to unmangle. * @return the original path. * wrapper arround UTF-8 URL encoding of a string * @param q query to be encoded. If {@code null}, an empty string will * @return null if fail, otherwise the encoded string * @see URLEncoder#encode(String, String) // Should not happen. UTF-8 must be supported by JVMs. * Append '&name=value" to the given buffer. If the given <var>value</var> * is {@code null}, this method does nothing. * @param buf where to append the query string * @param key the name of the parameter to add. Append as is! * @param value the value for the given parameter. Gets automatically UTF-8 * @throws NullPointerException if the given buffer is {@code null}. * @see #URIEncode(String) * URI encode the given path. * @param path path to encode. * @return the encoded path. * @throws NullPointerException if a parameter is {@code null} "Could not encode path " +
path,
ex);
* Replace all quote characters (ASCI 0x22) with the corresponding html * @param q string to escape. * @return an empty string if a parameter is {@code null}, the mangled for (
int i =
0; i < q.
length(); i++ ) {
* Tag changes in the given <var>line1</var> and <var>line2</var> * for highlighting. Removed parts are tagged with CSS class {@code d}, * new parts are tagged with CSS class {@code a} using a {@code span} * @param line1 line of the original file * @return the tagged lines (field[0] ~= line1, field[1] ~= line2). * @throws NullPointerException if one of the given parameters is {@code null}. while (s <= m && s <= n &&
csl1[s] ==
csl2[s]) {
while (s <= m && s <= n &&
csl1[m] ==
csl2[n]) {
* Dump the configuration as an HTML table. * destination for the HTML output * if an error happens while writing to {@code out} * @throws HistoryException * if the history guru cannot be accesses out.
append(
"<table border=\"1\" width=\"100%\">");
out.
append(
"<tr><th>Variable</th><th>Value</th></tr>");
out.
append(
"<tr><td>Ignored files</td><td>");
* Just read the given source and dump as is to the given destionation. * Does nothing, if one or more of the parameters is {@code null}. * @param out write destination * @param in source to read * @throws NullPointerException if a parameter is {@code null}. char[]
buf =
new char[
8192];
* Silently dump a file to the given destionation. All {@link IOException}s * gets caught and logged, but not re-thrown. * @param out dump destination * @param dir directory, which should contains the file. * @param filename the basename of the file to dump. * @param compressed if {@code true} the denoted file is assumed to be * @return {@code true} on success (everything read and written). * @throws NullPointerException if a parameter is {@code null}. * Silently dump a file to the given destionation. All {@link IOException}s * gets caught and logged, but not re-thrown. * @param out dump destination * @param file file to dump. * @param compressed if {@code true} the denoted file is assumed to be * @return {@code true} on success (everything read and written). * @throws NullPointerException if a parameter is {@code null}. "An error occured while piping file " +
file +
": ", e);
* Print a row in an HTML table. * destination for the HTML output * the values to print in the cells of the row * if an error happens while writing to {@code out} * Print an unordered list (HTML). * destination for the HTML output * if an error happens while writing to {@code out} * Create a string literal for use in JavaScript functions. * the string to be represented by the literal * @return a JavaScript string literal