Util.java revision 181
0N/A/*
0N/A * CDDL HEADER START
0N/A *
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 *
0N/A * See LICENSE.txt included in this distribution for the specific
0N/A * language governing permissions and limitations under the License.
0N/A *
0N/A * When distributing Covered Code, include this CDDL HEADER in each
0N/A * file and include the License file at LICENSE.txt.
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]
0N/A *
0N/A * CDDL HEADER END
0N/A */
0N/A
0N/A/*
154N/A * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
0N/A * Use is subject to license terms.
0N/A */
0N/Apackage org.opensolaris.opengrok.web;
0N/A
154N/Aimport java.io.IOException;
159N/Aimport java.io.UnsupportedEncodingException;
154N/Aimport java.io.Writer;
292N/Aimport java.net.URLEncoder;
292N/Aimport java.util.StringTokenizer;
159N/Aimport java.text.DecimalFormat;
154N/Aimport java.text.NumberFormat;
154N/Aimport org.opensolaris.opengrok.history.Annotation;
605N/A
434N/A/**
434N/A * File for useful functions
259N/A */
89N/Apublic class Util {
0N/A /**
0N/A * Return a string which represents a <code>CharSequence</code> in HTML.
0N/A *
0N/A * @param q a character sequence
456N/A * @return a string representing the character sequence in HTML
92N/A */
92N/A
92N/A public static String Htmlize(CharSequence q) {
92N/A StringBuilder sb = new StringBuilder(q.length() * 2);
92N/A Htmlize(q, sb);
92N/A return sb.toString();
154N/A }
456N/A
456N/A /**
456N/A * Append a character sequence to an <code>Appendable</code> object. Escape
456N/A * special characters for HTML.
345N/A *
0N/A * @param q a character sequence
345N/A * @param out the object to append the character sequence to
0N/A * @exception IOException if an I/O error occurs
0N/A */
92N/A public static void Htmlize(CharSequence q, Appendable out)
92N/A throws IOException {
92N/A for (int i = 0; i < q.length(); i++) {
92N/A Htmlize(q.charAt(i), out);
92N/A }
92N/A }
92N/A
92N/A /**
92N/A * Append a character sequence to a <code>StringBuilder</code>
345N/A * object. Escape special characters for HTML. This method is identical to
92N/A * <code>Htmlize(CharSequence,Appendable)</code>, except that it is
92N/A * guaranteed not to throw <code>IOException</code> because it uses a
345N/A * <code>StringBuilder</code>.
0N/A *
0N/A * @param q a character sequence
92N/A * @param out the object to append the character sequence to
92N/A * @see #Htmlize(CharSequence, Appendable)
92N/A */
92N/A public static void Htmlize(CharSequence q, StringBuilder out) {
345N/A try {
92N/A Htmlize(q, (Appendable) out);
92N/A } catch (IOException ioe) {
92N/A // StringBuilder's append methods are not declared to throw
92N/A // IOException, so this should never happen.
92N/A throw new RuntimeException("StringBuilder should not throw IOException", ioe);
345N/A }
92N/A }
439N/A
345N/A public static void Htmlize(char[] cs, int length, Appendable out)
92N/A throws IOException {
345N/A for (int i = 0; i < length && i < cs.length; i++) {
92N/A Htmlize(cs[i], out);
92N/A }
92N/A }
154N/A
92N/A /**
92N/A * Append a character to a an <code>Appendable</code> object. If the
92N/A * character has special meaning in HTML, append a sequence of characters
345N/A * representing the special character.
92N/A *
154N/A * @param c the character to append
345N/A * @param out the object to append the character to
92N/A * @exception IOException if an I/O error occurs
92N/A */
92N/A private static void Htmlize(char c, Appendable out) throws IOException {
92N/A switch (c) {
92N/A case '&':
92N/A out.append("&amp;");
92N/A break;
92N/A case '>':
92N/A out.append("&gt;");
92N/A break;
92N/A case '<':
92N/A out.append("&lt;");
345N/A break;
92N/A case '\n':
154N/A out.append("<br/>");
154N/A break;
154N/A default:
154N/A out.append(c);
154N/A }
154N/A }
154N/A
154N/A public static String breadcrumbPath(String urlPrefix, String l) {
154N/A return breadcrumbPath(urlPrefix, l, '/');
154N/A }
154N/A
154N/A public static String breadcrumbPath(String urlPrefix, String l, char sep) {
154N/A if (l == null || l.length() <= 1) {
154N/A return l;
92N/A }
92N/A StringBuilder hyperl = new StringBuilder(20);
92N/A if (l.charAt(0) == sep) {
604N/A hyperl.append(sep);
604N/A }
604N/A int s = 0,
604N/A e = 0;
0N/A while ((e = l.indexOf(sep, s)) >= 0) {
0N/A if (e - s > 0) {
0N/A hyperl.append("<a href=\"" + urlPrefix);
154N/A hyperl.append(l.substring(0, e));
583N/A hyperl.append("/\">");
583N/A hyperl.append(l.substring(s, e));
583N/A hyperl.append("</a>");
583N/A hyperl.append(sep);
604N/A }
604N/A s = e + 1;
605N/A }
605N/A if (s < l.length()) {
604N/A hyperl.append("<a href=\"" + urlPrefix);
0N/A hyperl.append(l);
605N/A hyperl.append("\">");
604N/A hyperl.append(l.substring(s, l.length()));
604N/A hyperl.append("</a>");
604N/A }
604N/A return hyperl.toString();
604N/A }
604N/A
604N/A public static String redableSize(long num) {
604N/A float l = (float) num;
604N/A NumberFormat formatter = new DecimalFormat("#,###,###,###.#");
605N/A if (l < 1024) {
605N/A return formatter.format(l);
604N/A } else if (l < 1048576) {
604N/A return (formatter.format(l / 1024) + "K");
604N/A } else {
605N/A return ("<b>" + formatter.format(l / 1048576) + "M</b>");
605N/A }
154N/A }
0N/A
154N/A public static void readableLine(int num, Writer out, Annotation annotation)
0N/A throws IOException {
604N/A String snum = String.valueOf(num);
604N/A if (num > 1) {
605N/A out.write("\n");
605N/A }
604N/A out.write("<a class=\"");
604N/A out.write((num % 10 == 0 ? "hl" : "l"));
604N/A out.write("\" name=\"");
604N/A out.write(snum);
604N/A out.write("\">");
604N/A out.write((num > 999 ? " " : (num > 99 ? " " : (num > 9 ? " " : " "))));
604N/A out.write(snum);
604N/A out.write(" </a>");
604N/A if (annotation != null) {
604N/A String r = annotation.getRevision(num);
604N/A boolean enabled = annotation.isEnabled(num);
605N/A
605N/A out.write("<span class=\"blame\"><span class=\"l\"> ");
604N/A for (int i = r.length(); i < annotation.getWidestRevision(); i++) {
604N/A out.write(" ");
604N/A }
604N/A
604N/A if (enabled) {
605N/A out.write("<a href=\"");
605N/A out.write(URIEncode(annotation.getFilename()));
604N/A out.write("?a=true&r=");
604N/A out.write(URIEncode(r));
604N/A out.write("\">");
604N/A }
604N/A
604N/A Htmlize(r, out);
605N/A
604N/A if (enabled) {
604N/A out.write("</a>");
604N/A }
604N/A
604N/A out.write(" </span>");
604N/A
604N/A String a = annotation.getAuthor(num);
604N/A out.write("<span class=\"l\"> ");
0N/A for (int i = a.length(); i < annotation.getWidestAuthor(); i++) {
0N/A out.write(" ");
604N/A }
604N/A Htmlize(a, out);
604N/A out.write(" </span></span>");
604N/A }
604N/A }
604N/A
604N/A /**
604N/A * Append path and date into a string in such a way that lexicographic
604N/A * sorting gives the same results as a walk of the file hierarchy. Thus
604N/A * null (\u0000) is used both to separate directory components and to
605N/A * separate the path from the date.
605N/A */
604N/A public static String uid(String path, String date) {
604N/A return path.replace('/', '\u0000') + "\u0000" + date;
605N/A }
605N/A
605N/A public static String uid2url(String uid) {
605N/A String url = uid.replace('\u0000', '/'); // replace nulls with slashes
605N/A return url.substring(0, url.lastIndexOf('/')); // remove date from end
605N/A }
605N/A
604N/A public static String URIEncode(String q) {
605N/A try {
605N/A // We should probably use an encoding which supports a larger
605N/A // character set, but use ISO-8859-1 for now, since that's what
605N/A // we use other places in the code.
605N/A return URLEncoder.encode(q, "ISO-8859-1");
605N/A } catch (UnsupportedEncodingException e) {
605N/A // Should not happen. ISO-8859-1 must be supported by all JVMs.
605N/A return null;
605N/A }
605N/A }
605N/A
605N/A public static String URIEncodePath(String path) {
605N/A final String SEGMENT_SEP = "/";
605N/A StringTokenizer pathTokenizer = new StringTokenizer(path, SEGMENT_SEP, true);
605N/A StringBuilder urlPath = new StringBuilder();
605N/A
605N/A while (pathTokenizer.hasMoreTokens()) {
605N/A String token = pathTokenizer.nextToken();
604N/A urlPath.append(SEGMENT_SEP.equals(token) ? token : URIEncode(token));
0N/A }
0N/A
604N/A return urlPath.toString();
604N/A }
604N/A
604N/A public static String formQuoteEscape(String q) {
604N/A if (q == null) {
604N/A return "";
604N/A }
604N/A StringBuilder sb = new StringBuilder();
604N/A char c;
604N/A for (int i = 0; i < q.length(); i++) {
604N/A c = q.charAt(i);
604N/A if (c == '"') {
604N/A sb.append("&quot;");
604N/A } else {
604N/A sb.append(c);
0N/A }
604N/A }
604N/A return sb.toString();
0N/A }
154N/A}
0N/A