Util.java revision 407
366N/A/*
366N/A * CDDL HEADER START
366N/A *
366N/A * The contents of this file are subject to the terms of the
366N/A * Common Development and Distribution License (the "License").
366N/A * You may not use this file except in compliance with the License.
366N/A *
366N/A * See LICENSE.txt included in this distribution for the specific
366N/A * language governing permissions and limitations under the License.
366N/A *
366N/A * When distributing Covered Code, include this CDDL HEADER in each
366N/A * file and include the License file at LICENSE.txt.
366N/A * If applicable, add the following below this CDDL HEADER, with the
366N/A * fields enclosed by brackets "[]" replaced with your own identifying
366N/A * information: Portions Copyright [yyyy] [name of copyright owner]
366N/A *
366N/A * CDDL HEADER END
366N/A */
366N/A
366N/A/*
366N/A * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
366N/A * Use is subject to license terms.
366N/A */
366N/Apackage org.opensolaris.opengrok.web;
366N/A
366N/Aimport java.io.IOException;
366N/Aimport java.io.UnsupportedEncodingException;
366N/Aimport java.io.Writer;
366N/Aimport java.net.URI;
366N/Aimport java.net.URISyntaxException;
366N/Aimport java.net.URLEncoder;
366N/Aimport java.text.DecimalFormat;
366N/Aimport java.text.NumberFormat;
366N/Aimport org.opensolaris.opengrok.configuration.RuntimeEnvironment;
366N/Aimport org.opensolaris.opengrok.history.Annotation;
366N/A
366N/A/**
366N/A * File for useful functions
366N/A */
366N/Apublic class Util {
366N/A /**
366N/A * Return a string which represents a <code>CharSequence</code> in HTML.
366N/A *
366N/A * @param q a character sequence
366N/A * @return a string representing the character sequence in HTML
366N/A */
366N/A
366N/A public static String htmlize(CharSequence q) {
366N/A StringBuilder sb = new StringBuilder(q.length() * 2);
366N/A htmlize(q, sb);
366N/A return sb.toString();
366N/A }
366N/A
366N/A /**
366N/A * Append a character sequence to an <code>Appendable</code> object. Escape
366N/A * special characters for HTML.
366N/A *
366N/A * @param q a character sequence
366N/A * @param out the object to append the character sequence to
366N/A * @exception IOException if an I/O error occurs
366N/A */
366N/A public static void htmlize(CharSequence q, Appendable out)
366N/A throws IOException {
366N/A for (int i = 0; i < q.length(); i++) {
366N/A htmlize(q.charAt(i), out);
366N/A }
366N/A }
366N/A
366N/A /**
366N/A * Append a character sequence to a <code>StringBuilder</code>
366N/A * object. Escape special characters for HTML. This method is identical to
366N/A * <code>htmlize(CharSequence,Appendable)</code>, except that it is
366N/A * guaranteed not to throw <code>IOException</code> because it uses a
366N/A * <code>StringBuilder</code>.
366N/A *
366N/A * @param q a character sequence
366N/A * @param out the object to append the character sequence to
366N/A * @see #htmlize(CharSequence, Appendable)
366N/A */
366N/A public static void htmlize(CharSequence q, StringBuilder out) {
366N/A try {
366N/A htmlize(q, (Appendable) out);
366N/A } catch (IOException ioe) {
366N/A // StringBuilder's append methods are not declared to throw
366N/A // IOException, so this should never happen.
366N/A throw new RuntimeException("StringBuilder should not throw IOException", ioe);
366N/A }
366N/A }
366N/A
366N/A public static void htmlize(char[] cs, int length, Appendable out)
366N/A throws IOException {
366N/A for (int i = 0; i < length && i < cs.length; i++) {
366N/A htmlize(cs[i], out);
366N/A }
366N/A }
366N/A
366N/A /**
366N/A * Append a character to a an <code>Appendable</code> object. If the
366N/A * character has special meaning in HTML, append a sequence of characters
366N/A * representing the special character.
366N/A *
366N/A * @param c the character to append
366N/A * @param out the object to append the character to
366N/A * @exception IOException if an I/O error occurs
366N/A */
366N/A private static void htmlize(char c, Appendable out) throws IOException {
366N/A switch (c) {
366N/A case '&':
366N/A out.append("&amp;");
366N/A break;
366N/A case '>':
366N/A out.append("&gt;");
366N/A break;
366N/A case '<':
366N/A out.append("&lt;");
366N/A break;
366N/A case '\n':
366N/A out.append("<br/>");
366N/A break;
366N/A default:
366N/A out.append(c);
366N/A }
366N/A }
366N/A
366N/A public static String breadcrumbPath(String urlPrefix, String l) {
return breadcrumbPath(urlPrefix, l, '/');
}
public static String breadcrumbPath(String urlPrefix, String l, char sep) {
if (l == null || l.length() <= 1) {
return l;
}
StringBuilder hyperl = new StringBuilder(20);
if (l.charAt(0) == sep) {
hyperl.append(sep);
}
int s = 0,
e = 0;
while ((e = l.indexOf(sep, s)) >= 0) {
if (e - s > 0) {
hyperl.append("<a href=\"" + urlPrefix);
hyperl.append(l.substring(0, e));
hyperl.append("/\">");
hyperl.append(l.substring(s, e));
hyperl.append("</a>");
hyperl.append(sep);
}
s = e + 1;
}
if (s < l.length()) {
hyperl.append("<a href=\"" + urlPrefix);
hyperl.append(l);
hyperl.append("\">");
hyperl.append(l.substring(s, l.length()));
hyperl.append("</a>");
}
return hyperl.toString();
}
public static String redableSize(long num) {
float l = (float) num;
NumberFormat formatter = new DecimalFormat("#,###,###,###.#");
if (l < 1024) {
return formatter.format(l);
} else if (l < 1048576) {
return (formatter.format(l / 1024) + "K");
} else {
return ("<b>" + formatter.format(l / 1048576) + "M</b>");
}
}
public static void readableLine(int num, Writer out, Annotation annotation)
throws IOException {
String snum = String.valueOf(num);
if (num > 1) {
out.write("\n");
}
out.write("<a class=\"");
out.write((num % 10 == 0 ? "hl" : "l"));
out.write("\" name=\"");
out.write(snum);
out.write("\">");
out.write((num > 999 ? " " : (num > 99 ? " " : (num > 9 ? " " : " "))));
out.write(snum);
out.write(" </a>");
if (annotation != null) {
String r = annotation.getRevision(num);
boolean enabled = annotation.isEnabled(num);
out.write("<span class=\"blame\"><span class=\"l\"> ");
for (int i = r.length(); i < annotation.getWidestRevision(); i++) {
out.write(" ");
}
if (enabled) {
out.write("<a href=\"");
out.write(URIEncode(annotation.getFilename()));
out.write("?a=true&r=");
out.write(URIEncode(r));
out.write("\">");
}
htmlize(r, out);
if (enabled) {
out.write("</a>");
}
out.write(" </span>");
String a = annotation.getAuthor(num);
out.write("<span class=\"l\"> ");
for (int i = a.length(); i < annotation.getWidestAuthor(); i++) {
out.write(" ");
}
String link = RuntimeEnvironment.getInstance().getUserPage();
if (link != null && link.length() > 0) {
out.write("<a href=\"");
out.write(link);
out.write(URIEncode(a));
out.write("\">");
htmlize(a, out);
out.write("</a>");
} else {
htmlize(a, out);
}
out.write(" </span></span>");
}
}
/**
* Append path and date into a string in such a way that lexicographic
* sorting gives the same results as a walk of the file hierarchy. Thus
* null (\u0000) is used both to separate directory components and to
* separate the path from the date.
*/
public static String uid(String path, String date) {
return path.replace('/', '\u0000') + "\u0000" + date;
}
public static String uid2url(String uid) {
String url = uid.replace('\u0000', '/'); // replace nulls with slashes
return url.substring(0, url.lastIndexOf('/')); // remove date from end
}
public static String URIEncode(String q) {
try {
// We should probably use an encoding which supports a larger
// character set, but use ISO-8859-1 for now, since that's what
// we use other places in the code.
return URLEncoder.encode(q, "ISO-8859-1");
} catch (UnsupportedEncodingException e) {
// Should not happen. ISO-8859-1 must be supported by all JVMs.
return null;
}
}
public static String URIEncodePath(String path) {
try {
URI uri = new URI(null, null, path, null);
return uri.getRawPath();
} catch (URISyntaxException ex) {
ex.printStackTrace();
return "";
}
}
public static String formQuoteEscape(String q) {
if (q == null) {
return "";
}
StringBuilder sb = new StringBuilder();
char c;
for (int i = 0; i < q.length(); i++) {
c = q.charAt(i);
if (c == '"') {
sb.append("&quot;");
} else {
sb.append(c);
}
}
return sb.toString();
}
/**
* Build a string that may be converted to a Query and passed to Lucene.
* All parameters may be passed as null or an empty string to indicate that
* they are unused.
*
* @param freetext The string from the "Full Search" text-field. This field
* will be applied as it is specified.
* @param defs The string from the "Definition" text-field. This field
* will be searched for in the <b>defs</b> field in the lucene
* index. All occurences of ":" will be replaced with "\:"
* @param refs The string from the "Symbol" text-field. This field
* will be searched for in the <b>refs</b> field in the lucene
* index. All occurences of ":" will be replaced with "\:"
* @param path The string from the "File Path" text-field. This field
* will be searched for in the <b>path</b> field in the lucene
* index. All occurences of ":" will be replaced with "\:"
* @param hist The string from the "History" text-field. This field
* will be searched for in the <b>hist</b> field in the lucene
* index. All occurences of ":" will be replaced with "\:"
* @return A string to be parsed by the Lucene parser.
*/
public static String buildQueryString(String freetext, String defs, String refs, String path, String hist) {
StringBuilder sb = new StringBuilder();
if (freetext != null && freetext.length() > 0) {
sb.append(freetext.replace("::", "\\:\\:"));
}
if (defs != null && defs.length() > 0) {
sb.append(" defs:(");
sb.append(defs.replace(":", "\\:"));
sb.append(")");
}
if (refs != null && refs.length() > 0) {
sb.append(" refs:(");
sb.append(refs.replace(":", "\\:"));
sb.append(")");
}
if (path != null && path.length() > 0) {
sb.append(" path:(");
sb.append(path.replace(":", "\\:"));
sb.append(")");
}
if (hist != null && hist.length() > 0) {
sb.append(" hist:(");
sb.append(hist.replace(":", "\\:"));
sb.append(")");
}
return sb.toString();
}
}