Util.java revision d8d744ec4741226cba1ed387037d5f8dafc3d813
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* See LICENSE.txt included in this distribution for the specific
* language governing permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at LICENSE.txt.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Portions Copyright 2011 Jens Elkner.
*/
/**
* Class for useful functions.
*/
public final class Util {
private Util() {
// singleton
}
/**
* Return a string which represents a <code>CharSequence</code> in HTML.
*
* @param q a character sequence
* @return a string representing the character sequence in HTML
*/
try {
} catch (IOException ioe) {
// IOException cannot happen when the destination is a
// StringBuilder. Wrap in an AssertionError so that callers
// don't have to check for an IOException that should never
// happen.
}
}
/**
* Append a character sequence to the given destination whereby special
* characters for HTML are escaped accordingly.
*
* @param q a character sequence to escape
* @param dest where to append the character sequence to
* @throws IOException if an error occurred when writing to {@code dest}
*/
throws IOException {
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 escape
* @param length max. number of characters to append, starting from index 0.
* @param dest where to append the character sequence to
* @throws IOException if an error occurred when writing to {@code dest}
*/
throws IOException {
}
for (int i = 0; i < len; 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
* @throws IOException if an error occurred when writing to {@code dest}
*/
switch (c) {
case '&':
break;
case '>':
break;
case '<':
break;
case '\n':
break;
default:
}
}
/**
* used by BUI - CSS needs this parameter for proper cache refresh (per
* changeset) in client browser TODO jel: but useless, since the page cached
* anyway.
*
* @return html escaped version (hg changeset)
*/
public static String versionParameter() {
return versionP;
}
/**
* Convenience 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)
*/
}
/**
* Convenience method for
* {@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)
*/
}
/**
* Convenience method for
* {@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 starts.
* @return HTML markup fro the breadcrumb or the path itself.
* @see #breadcrumbPath(String, String, char, String, boolean, boolean)
* @see #getCanonicalPath(String, char)
*/
return path;
}
}
/**
* 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).
*
* @param urlPrefix what should be prepend to the constructed URL
* @param path the full path from which the breadcrumb path is built.
* @param sep the character that separates the path components in
* <var>path</var>
* @param urlPostfix what should be append to the constructed URL
* @param compact if {@code true}, a canonical path gets constructed before
* processing.
* @param isDir 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.
*/
return path;
}
return path;
}
}
}
}
}
}
/**
* 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.
* <p>
* 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
* components
* @return always a canonical path which starts with a '/'.
*/
return "/";
}
return "/";
}
}
// since is not a general purpose method. So we waive to handle
// cases like:
// || path.endsWith("/..") || path.endsWith("/.")
}
}
private final static Pattern EMAIL_PATTERN
/**
* Get email address of the author.
*
* @param author string containing author and possibly email address.
* @return email address of the author or full author string if the author
* string does not contain an email address.
*/
if (emailMatcher.find()) {
}
return email;
}
/**
* Remove all empty and {@code null} string elements from the given
* <var>names</var> and optionally all redundant information like "." and
* "..".
*
* @param names names to check
* @param canonical if {@code true}, remove redundant elements as well.
* @return a possible empty array of names all with a length > 0.
*/
return new String[0];
}
continue;
}
if (canonical) {
res.removeLast();
}
} else {
}
} else {
}
}
.size()]);
}
/**
* Generate a regexp that matches the specified character. Escape it in case
* it is a character that has a special meaning in a regexp.
*
* @param c the character that the regexp should match
* @return a six-character string on the form <tt>\u</tt><i>hhhh</i>
*/
private static String escapeForRegex(char c) {
}
}
/**
* Convert the given size into a human readable string.
*
* NOTE: when changing the output of this function make sure to adapt the
* jQuery tablesorter custom parsers in web/httpheader.jspf
*
* @param num size to convert.
* @return a readable string
*/
float l = num;
if (l < 1024) {
} else if (l < 1048576) {
} else if (l < 1073741824) {
} else {
}
}
/**
* Converts different html special characters into their encodings used in
* html. Currently used only for tooltips of annotation revision number view
*
* @param s input text
* @return encoded text for use in <a title=""> tag
*/
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
switch (c) {
case '\'':
break;
case '"':
break; // \\\
case '&':
break;
case '>':
break;
case '<':
break;
case ' ':
break;
case '\t':
break;
case '\n':
break;
case '\r':
break;
default:
break;
}
}
}
/**
* 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()}
* @param project project that is used
* @throws IOException depends on the destination (<var>out</var>).
*/
throws IOException
{
}
throws IOException
{
// this method should go to JFlexXref
}
if (annotation != null) {
if (enabled) {
/*
version number, 1 is the most recent
generates css classes version_color_n
*/
}
}
}
}
if (enabled) {
// Write link to search the revision in current project.
}
if (userPageLink == null) {
} else {
if (userPageSuffix != null) {
}
}
}
}
/**
* 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.
*/
}
/**
* Write the 'H A D' links. This is used for search results and directory
* listings.
*
* @param out writer for producing output
* @param ctxE URI encoded prefix
* @param is_dir is directory
* @throws IOException depends on the destination (<var>out</var>).
*/
boolean is_dir) throws IOException {
}
if (!is_dir) {
}
}
/**
* wrapper around UTF-8 URL encoding of a string
*
* @param q query to be encoded. If {@code null}, an empty string will be
* used instead.
* @return null if fail, otherwise the encoded string
* @see URLEncoder#encode(String, String)
*/
try {
} catch (UnsupportedEncodingException e) {
// Should not happen. UTF-8 must be supported by JVMs.
}
return null;
}
/**
* 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
* URL encoded.
* @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}
*/
// Bug #19188: Ideally, we'd like to use the standard class library to
// encode the paths. We're aware of the following two methods:
//
// 1) URLEncoder.encode() - this method however transforms space to +
// instead of %20 (which is right for HTML form data, but not for
// paths), and it also does not preserve the separator chars (/).
//
// 2) URI.getRawPath() - transforms space and / as expected, but gets
// confused when the path name contains a colon character (it thinks
// parts of the path is schema in that case)
//
// For now, encode manually the way we want it.
// URLEncoder's javadoc says a-z, A-Z, ., -, _ and * are safe
// characters, so we preserve those to make the encoded string
// shorter and easier to read. We also preserve the separator
// chars (/). All other characters are encoded (as UTF-8 byte
// sequences).
if ((b == '/') || (b >= 'a' && b <= 'z')
|| (b >= 'A' && b <= 'Z') || (b >= '0' && b <= '9')
|| (b == '.') || (b == '-') || (b == '_') || (b == '*')) {
} else {
int u = b & 0xFF; // Make the byte value unsigned.
if (u <= 0x0F) {
// Add leading zero if required.
}
}
}
}
/**
* Escape a string for use as in an HTML attribute value. The returned value
* is not enclosed in double quotes. The caller needs to add those.
*
* @param q string to escape.
* @return an empty string if a parameter is {@code null}, the mangled
* string otherwise.
*/
return "";
}
char c;
for (int i = 0; i < q.length(); i++) {
c = q.charAt(i);
switch (c) {
case '"':
break;
case '&':
break;
default:
break;
}
}
}
/**
* 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} element.
* The input parameters must not have any HTML escapes in them.
*
* @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}.
*/
int s = 0;
s++;
}
m--;
n--;
}
// deleted
if (s <= m) {
} else {
}
// added
if (s <= n) {
} else {
}
return ret;
}
/**
* Dump the configuration as an HTML table.
*
* @param out destination for the HTML output
* @throws IOException if an error happens while writing to {@code out}
* @throws HistoryException if the history guru cannot be accesses
*/
@SuppressWarnings("boxing")
.getCacheInfo());
}
/**
* Just read the given source and dump as is to the given destination. 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}.
*/
return;
}
char[] buf = new char[8192];
int len = 0;
}
}
/**
* Silently dump a file to the given destination. 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
* gzipped.
* @return {@code true} on success (everything read and written).
* @throws NullPointerException if a parameter is {@code null}.
*/
boolean compressed) {
}
/**
* Silently dump a file to the given destination. 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
* gzipped.
* @return {@code true} on success (everything read and written).
* @throws NullPointerException if a parameter is {@code null}.
*/
return false;
}
? new InputStreamReader(new GZIPInputStream(
new FileInputStream(file)))
: new FileReader(file)) {
return true;
} catch (IOException e) {
}
return false;
}
/**
* Print list of messages into output
*
* @param out output
* @param set set of messages
*/
}
/**
* Print set of messages into output
*
* @param out output
* @param set set of messages
* @param limited if the container should be limited
*/
try {
if (limited) {
}
}
} catch (IOException ex) {
"An error occured for a group of messages", ex);
}
}
}
/**
* Print set of messages into json array
*
* @param set set of messages
* @return json array containing the set of messages
*/
@SuppressWarnings("unchecked")
}
}
return array;
}
/**
* Print set of messages into json object for given tag.
*
* @param tag return messages in json format for the given tag
* @return json object with 'tag' and 'messages' attribute or null
*/
@SuppressWarnings("unchecked")
return null;
}
return toRet;
}
/**
* Print messages for given tags into json array
*
* @param array the array where the result should be stored
* @param tags list of tags
* @return json array of the messages (the same as the parameter)
* @see #messagesToJsonObject(String)
*/
@SuppressWarnings("unchecked")
continue;
}
}
return array;
}
/**
* Print messages for given tags into json array
*
* @param tags list of tags
* @return json array of the messages
* @see #messagesToJson(JSONArray, String...)
* @see #messagesToJsonObject(String)
*/
}
/**
* Print messages for given project into json array. These messages are
* tagged by project description or tagged by any of the project's group
* name.
*
* @param project the project
* @param additionalTags additional list of tags
* @return the json array
* @see #messagesToJson(String...)
*/
return new JSONArray();
}
});
}
/**
* Print messages for given project into json array. These messages are
* tagged by project description or tagged by any of the project's group
* name
*
* @param project the project
* @return the json array
* @see #messagesToJson(Project, String...)
*/
}
/**
* Print a row in an HTML table.
*
* @param out destination for the HTML output
* @param cells the values to print in the cells of the row
* @throws IOException if an error happens while writing to {@code out}
*/
throws IOException {
}
}
/**
* Print an unordered list (HTML).
*
* @param out destination for the HTML output
* @param items the list items
* @throws IOException if an error happens while writing to {@code out}
*/
}
}
/**
* Create a string literal for use in JavaScript functions.
*
* @param str the string to be represented by the literal
* @return a JavaScript string literal
*/
switch (c) {
case '"':
break;
case '\\':
break;
case '\n':
break;
case '\r':
break;
default:
}
}
}
/**
* Make a path relative by stripping off a prefix. If the path does not have
* the given prefix, return the full path unchanged.
*
* @param prefix the prefix to strip off
* @param fullPath the path from which to remove the prefix
* @return a path relative to {@code prefix} if {@code prefix} is a parent
* directory of {@code fullPath}; otherwise, {@code fullPath}
*/
// Find the length of the prefix to strip off. The prefix should
// represent a directory, so it could end with a slash. In case it
// doesn't end with a slash, increase the length by one so that we
// strip off the leading slash from the relative path.
prefixLength++;
}
// If the full path starts with the prefix, strip off the prefix.
}
// Otherwise, return the full path.
return fullPath;
}
}