Util.java revision 1463
0N/A * The contents of this file are subject to the terms of the 407N/A * Common Development and Distribution License (the "License"). 0N/A * You may not use this file except in compliance with the License. 0N/A * language governing permissions and limitations under the License. 0N/A * When distributing Covered Code, include this CDDL HEADER in each 0N/A * If applicable, add the following below this CDDL HEADER, with the 0N/A * fields enclosed by brackets "[]" replaced with your own identifying 0N/A * information: Portions Copyright [yyyy] [name of copyright owner] 1297N/A * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. 1356N/A * Portions Copyright 2011, 2012 Jens Elkner. 1185N/A * Class for useful functions. 92N/A * Return a string which represents a <code>CharSequence</code> in HTML. 92N/A * @return a string representing the character sequence in HTML 1185N/A * Append a character sequence to the given destination whereby 1185N/A * special characters for HTML are escaped accordingly. 1185N/A * @param q a character sequence to esacpe 1185N/A * @param dest where to append the character sequence to 1185N/A * Append a character array to the given destination whereby 1185N/A * special characters for HTML are escaped accordingly. 1185N/A * @param cs characters to esacpe 1185N/A * @param length max. number of characters to append, starting from index 0. 1185N/A * @param dest where to append the character sequence to 1190N/A * Append a character to the given destination whereby special characters 1185N/A * special for HTML are escaped accordingly. 1185N/A * @param c the character to append 1185N/A * @param dest where to append the character to 1185N/A * used by BUI - CSS needs this parameter for proper cache refresh (per 1185N/A * changeset) in client browser jel: but useless, since the page cached 990N/A * @return html escaped version (hg changeset) 1185N/A * Convinience method for {@code breadcrumbPath(urlPrefix, path, '/')}. 1185N/A * @param urlPrefix prefix to add to each url 1185N/A * @param path path to crack 1185N/A * @return HTML markup fro the breadcrumb or the path itself. 604N/A * @see #breadcrumbPath(String, String, char) 1185N/A * {@code breadcrumbPath(urlPrefix, path, sep, "", false)}. 1185N/A * @param urlPrefix prefix to add to each url 1185N/A * @param path path to crack 1185N/A * @param sep separator to use to crack the given path 1185N/A * @return HTML markup fro the breadcrumb or the path itself. 1185N/A * @see #breadcrumbPath(String, String, char, String, boolean, boolean) 1185N/A * {@code breadcrumbPath(urlPrefix, path, sep, "", false, path.endsWith(sep)}. 1185N/A * @param urlPrefix prefix to add to each url 1185N/A * @param path path to crack 1185N/A * @param sep separator to use to crack the given path 1185N/A * @param urlPostfix suffix to add to each url 1185N/A * @param compact if {@code true} the given path gets transformed into 1190N/A * its canonical form (.i.e. all '.' and '..' and double separators 1190N/A * removed, but not always resolves to an absolute path) before processing 1185N/A * @return HTML markup fro the breadcrumb or the path itself. 1185N/A * @see #breadcrumbPath(String, String, char, String, boolean, boolean) 1185N/A * @see #getCanonicalPath(String, char) 604N/A * Create a breadcrumb path to allow navigation to each element of a path. 1185N/A * Consecutive separators (<var>sep</var>) in the given <var>path</var> are 1185N/A * always collapsed into a single separator automatically. If 1185N/A * <var>compact</var> is {@code true} path gets translated into a canonical 1185N/A * path similar to {@link File#getCanonicalPath()}, however the current 1185N/A * working directory is assumed to be "/" and no checks are done (e.g. 1185N/A * neither whether the path [component] exists nor which type it is). 1185N/A * what should be prepend to the constructed URL 1185N/A * the full path from which the breadcrumb path is built. 1185N/A * the character that separates the path components in 1185N/A * what should be append to the constructed URL 1185N/A * if {@code true}, a canonical path gets constructed before 1185N/A * if {@code true} a "/" gets append to the last path component's 1185N/A * link and <var>sep</var> to its name 1190N/A * @return <var>path</var> if it resolves to an empty or "/" or 1185N/A * {@code null} path, the HTML markup for the breadcrumb path otherwise. 1190N/A * Normalize the given <var>path</var> to its canonical form. I.e. all 1190N/A * separators (<var>sep</var>) are replaced with a slash ('/'), all 1185N/A * double slashes are replaced by a single slash, all single dot path 1190N/A * components (".") of the formed path are removed and all double dot path 1190N/A * components (".." ) of the formed path are replaced with its parent or 1185N/A * '/' if there is no parent. 1185N/A * So the difference to {@link File#getCanonicalPath()} is, that this method 1185N/A * does not hit the disk (just string manipulation), resolves <var>path</var> 1190N/A * always against '/' and thus always returns an absolute path, which may 1190N/A * actually not exist, and which has a single trailing '/' if the given 1190N/A * <var>path</var> ends with the given <var>sep</var>. 1190N/A * @param path path to mangle. If not absolute or {@code null}, the 1185N/A * current working directory is assumed to be '/'. 1185N/A * @param sep file separator to use to crack <var>path</var> into path 1185N/A * @return always a canonical path which starts with a '/'. 1190N/A // since is not a general purpose method. So we waive to handle 1185N/A // || path.endsWith("/..") || path.endsWith("/.") 1297N/A * Get email address of the author. 1297N/A * string containing author and possibly email address. 1384N/A * @return the first email address found in the given parameter, the 1384N/A * parameter otherwise. Returned values are whitespace trimmed. 1185N/A * Remove all empty and {@code null} string elements from the given 1185N/A * <var>names</var> and optionally all redundant information like "." and 1185N/A * if {@code true}, remove redundant elements as well. 1185N/A * @return a possible empty array of names all with a length > 0. 1185N/A * Generate a regex that matches the specified character. Escape it in case 1185N/A * it is a character that has a special meaning in a regex. 1185N/A * the character that the regex should match 604N/A * @return a six-character string on the form <tt>\u</tt><i>hhhh</i> 1185N/A * Convert the given size into a human readable string. 1185N/A * @param num size to convert. 1185N/A * @return a readable string 154N/A }
else if (l <
1048576) {
1185N/A * Generate a string from the given path and date in a way that allows 1190N/A * stable lexicographic sorting (i.e. gives always the same results) as a 1190N/A * walk of the file hierarchy. Thus null character (\u0000) is used both 1185N/A * to separate directory components and to separate the path from the date. 1185N/A * @param path path to mangle. 1185N/A * @param date date string to use. 1185N/A * @return the mangled path. 1190N/A * The reverse operation for {@link #path2uid(String, String)} - re-creates 1185N/A * the unmangled path from the given uid. 1185N/A * @param uid uid to unmangle. 1185N/A * @return the original path. 849N/A * wrapper arround UTF-8 URL encoding of a string 1185N/A * @param q query to be encoded. If {@code null}, an empty string will 849N/A * @return null if fail, otherwise the encoded string 1185N/A * @see URLEncoder#encode(String, String) 830N/A // Should not happen. UTF-8 must be supported by JVMs. 1185N/A * Append '&name=value" to the given buffer. If the given <var>value</var> 1185N/A * is {@code null}, this method does nothing. 1185N/A * @param buf where to append the query string 1185N/A * @param key the name of the parameter to add. Append as is! 1185N/A * @param value the value for the given parameter. Gets automatically UTF-8 1185N/A * @throws NullPointerException if the given buffer is {@code null}. 1366N/A /* ASCII chars 0..63, which need to be escaped in URI paths 1366N/A * NOTE: URIs use ';' and '=' for path segment params - so we need to escape 1366N/A * URLs do not have this distinction and allow it unescaped. 1463N/A * NOTE 2: A colon doesn't need to be encoded. However, to avoid that browsers 1463N/A * interpret relative pathnames like 'test:whatever' as a full URI 1463N/A * (i.e. scheme == 'test', etc), we encode ':' as well. 1463N/A |
1L<<
'<' |
1L<<
'>' |
1L<<
'?' |
1L<<
';' |
1L<<
'=' |
1L<<
':';
1366N/A /* ASCII chars 64..127, which need to be escaped in URI paths 1366N/A * NOTE: URIs allow '~' unescaped, URLs not. 1366N/A |
1L<<
'^'-
64 |
1L<<
'`'-
64 |
1L<<
'{'-
64 |
1L<<
'|'-
64 |
1L<<
'}'-
64 |
1L<<
63;
1366N/A * URI encode the given path (according to RFC 1866 hyperlinks are URIs) 1366N/A * using UTF-8 encoding for non-ASCII characters. 1185N/A * @param path path to encode. 1185N/A * @return the encoded path. 1185N/A * @throws NullPointerException if a parameter is {@code null} 1185N/A * Replace all quote characters (ASCI 0x22) with the corresponding html 1185N/A * @param q string to escape. 1190N/A * @return an empty string if a parameter is {@code null}, the mangled 1388N/A /** span open tag incl. css class used to tag removed source code */ 1388N/A /** span open tag incl. css class used to tag added source code */ 1185N/A * Tag changes in the given <var>line1</var> and <var>line2</var> 1185N/A * for highlighting. Removed parts are tagged with CSS class {@code d}, 1185N/A * new parts are tagged with CSS class {@code a} using a {@code span} 1185N/A * @param line1 line of the original file 1185N/A * @return the tagged lines (field[0] ~= line1, field[1] ~= line2). 1190N/A * @throws NullPointerException if one of the given parameters is {@code null}. 1124N/A * Dump the configuration as an HTML table. 1185N/A * destination for the HTML output 1185N/A * if an error happens while writing to {@code out} 1185N/A * if the history guru cannot be accesses 1384N/A * Just read the given source and dump as is to the given destination. 1190N/A * Does nothing, if one or more of the parameters is {@code null}. 1185N/A * @param out write destination 1185N/A * @throws NullPointerException if a parameter is {@code null}. 1384N/A * Silently dump a file to the given destination. All {@link IOException}s 1185N/A * gets caught and logged, but not re-thrown. 1185N/A * @param out dump destination 1185N/A * @param dir directory, which should contains the file. 1185N/A * @param filename the basename of the file to dump. 1185N/A * @param compressed if {@code true} the denoted file is assumed to be 1185N/A * @return {@code true} on success (everything read and written). 1185N/A * @throws NullPointerException if a parameter is {@code null}. 1384N/A * Silently dump a file to the given destination. All {@link IOException}s 1185N/A * gets caught and logged, but not re-thrown. 1185N/A * @param out dump destination 1185N/A * @param file file to dump. 1185N/A * @param compressed if {@code true} the denoted file is assumed to be 1185N/A * @return {@code true} on success (everything read and written). 1185N/A * @throws NullPointerException if a parameter is {@code null}. 1124N/A * Print a row in an HTML table. 1185N/A * destination for the HTML output 1185N/A * the values to print in the cells of the row 1185N/A * if an error happens while writing to {@code out} 1124N/A * Print an unordered list (HTML). 1185N/A * destination for the HTML output 1185N/A * if an error happens while writing to {@code out} 1145N/A * Create a string literal for use in JavaScript functions. 1384N/A * @param str the string to be represented by the literal 1384N/A * @return a JavaScript string literal. {@code null} values are converted to ''. 1384N/A * Convert the given array into JSON format and write it to the given stream. 1384N/A * If <var>name</var> is given, the result is {@code name: [ value, ...]}, 1384N/A * otherwise {@code [ value, ... ]}. 1384N/A * @param out where to write the json formatted array 1384N/A * @param name name of the array. Might be {@code null}. 1384N/A * @param values array to convert. {@code null} are converted to ''. 1384N/A * @see #jsStringLiteral(String) 1384N/A * @see #writeJsonArray(Writer, String, String[]) 1384N/A // could just call writeJsonArray(out, name, values.toArray(new String[])) 1384N/A // but this can be an issue for big collections - so we rather 1384N/A * Convert the given array into JSON format and write it to the given stream. 1384N/A * If <var>name</var> is given, the result is {@code name: [ value, ...]}, 1384N/A * otherwise {@code [ value, ... ]}. 1384N/A * @param out where to write the json formatted array 1384N/A * @param name name of the array. Might be {@code null}. 1384N/A * @param values array to convert. {@code null} are converted to ''. 1384N/A * @see #jsStringLiteral(String) 1384N/A * @see #writeJsonArray(Writer, String, Collection)