Util.java revision 158
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright * CDDL HEADER START
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright * The contents of this file are subject to the terms of the
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright * Common Development and Distribution License (the "License").
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright * You may not use this file except in compliance with the License.
e592b34ac3f93557b9c7b30f9debc810a4875e79Derek Gathright * See LICENSE.txt included in this distribution for the specific
e592b34ac3f93557b9c7b30f9debc810a4875e79Derek Gathright * language governing permissions and limitations under the License.
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright * When distributing Covered Code, include this CDDL HEADER in each
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright * file and include the License file at LICENSE.txt.
32db3cd3a46c3850c354f593f6ea5ebef8b50591Derek Gathright * If applicable, add the following below this CDDL HEADER, with the
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright * fields enclosed by brackets "[]" replaced with your own identifying
32db3cd3a46c3850c354f593f6ea5ebef8b50591Derek Gathright * information: Portions Copyright [yyyy] [name of copyright owner]
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright * CDDL HEADER END
32db3cd3a46c3850c354f593f6ea5ebef8b50591Derek Gathright * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathright * Use is subject to license terms.
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathrightimport org.opensolaris.opengrok.history.Annotation;
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathright * File for useful functions
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathrightpublic class Util {
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright * Return a string which represents a <code>CharSequence</code> in HTML.
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright * @param q a character sequence
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright * @return a string representing the character sequence in HTML
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathright public static String Htmlize(CharSequence q) {
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathright StringBuilder sb = new StringBuilder(q.length() * 2);
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright * Append a character sequence to an <code>Appendable</code> object. Escape
347511d70b1f81d9b08363612fdabcef8a85e7a6Derek Gathright * special characters for HTML.
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright * @param q a character sequence
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright * @param out the object to append the character sequence to
79a7949ec943200ad3c7f1c20af065cf8936907dDerek Gathright * @exception IOException if an I/O error occurs
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathright public static void Htmlize(CharSequence q, Appendable out)
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathright * Append a character sequence to a <code>StringBuilder</code>
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathright * object. Escape special characters for HTML. This method is identical to
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright * <code>Htmlize(CharSequence,Appendable)</code>, except that it is
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright * guaranteed not to throw <code>IOException</code> because it uses a
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright * <code>StringBuilder</code>.
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathright * @param q a character sequence
79a7949ec943200ad3c7f1c20af065cf8936907dDerek Gathright * @param out the object to append the character sequence to
22084377ce30892c9fd181dada2d206969298e07Eric Ferraiuolo * @see #Htmlize(CharSequence, Appendable)
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathright public static void Htmlize(CharSequence q, StringBuilder out) {
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright // StringBuilder's append methods are not declared to throw
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright // IOException, so this should never happen.
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright throw new RuntimeException("StringBuilder should not throw IOException", ioe);
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright public static void Htmlize(char[] cs, int length, Appendable out)
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright for (int i = 0; i < length && i < cs.length; i++) {
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright * Append a character to a an <code>Appendable</code> object. If the
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright * character has special meaning in HTML, append a sequence of characters
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright * representing the special character.
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright * @param c the character to append
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright * @param out the object to append the character to
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright * @exception IOException if an I/O error occurs
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright private static void Htmlize(char c, Appendable out) throws IOException {
78ac1ef5c64e9e95a94bbfe859662da6d21b243aEric Ferraiuolo public static String breadcrumbPath(String urlPrefix, String l) {
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathright public static String breadcrumbPath(String urlPrefix, String l, char sep) {
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright StringBuilder hyperl = new StringBuilder(20);
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathright if (e - s > 0) {
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright if (s < l.length()) {
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright public static String redableSize(long num) {
1321f3d57acdabe63869fd24affd5953be3348b7Derek Gathright float l = (float) num;
1321f3d57acdabe63869fd24affd5953be3348b7Derek Gathright NumberFormat formatter = new DecimalFormat("#,###,###,###.#");
1321f3d57acdabe63869fd24affd5953be3348b7Derek Gathright if (l < 1024) {
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright } else if (l < 1048576) {
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright return ("<b>" + formatter.format(l / 1048576) + "M</b>");
1321f3d57acdabe63869fd24affd5953be3348b7Derek Gathright public static void readableLine(int num, Writer out, Annotation annotation)
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright out.write((num > 999 ? " " : (num > 99 ? " " : (num > 9 ? " " : " "))));
c3e956d9b62a538911d580547f2dff07b7f0a4b0Derek Gathright for (int i = r.length(); i < annotation.getWidestRevision(); i++) {
615e6a4633b53af433f9bfb616363b682d487043Derek Gathright out.write(URIEncode(annotation.getFilename()));
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright for (int i = a.length(); i < annotation.getWidestAuthor(); i++) {
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright * Append path and date into a string in such a way that lexicographic
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathright * sorting gives the same results as a walk of the file hierarchy. Thus
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright * null (\u0000) is used both to separate directory components and to
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright * separate the path from the date.
c3e956d9b62a538911d580547f2dff07b7f0a4b0Derek Gathright public static String uid(String path, String date) {
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright return path.replace('/', '\u0000') + "\u0000" + date;
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright String url = uid.replace('\u0000', '/'); // replace nulls with slashes
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright return url.substring(0, url.lastIndexOf('/')); // remove date from end
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright private static char[] hexdigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
e592b34ac3f93557b9c7b30f9debc810a4875e79Derek Gathright if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright || (c == '-') || (c == '.') || (c == '_') || (c == '~')) {
e592b34ac3f93557b9c7b30f9debc810a4875e79Derek Gathright public static String URIEncodePath(String path) {
e592b34ac3f93557b9c7b30f9debc810a4875e79Derek Gathright StringTokenizer pathTokenizer = new StringTokenizer(path, SEGMENT_SEP, true);
e592b34ac3f93557b9c7b30f9debc810a4875e79Derek Gathright StringBuilder urlPath = new StringBuilder();
e592b34ac3f93557b9c7b30f9debc810a4875e79Derek Gathright urlPath.append(SEGMENT_SEP.equals(token) ? token : URIEncode(token));
615e6a4633b53af433f9bfb616363b682d487043Derek Gathright public static String formQuoteEscape(String q) {
c3e956d9b62a538911d580547f2dff07b7f0a4b0Derek Gathright if (c == '"') {