Util.java revision 582
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.URI;
292N/Aimport java.net.URISyntaxException;
159N/Aimport java.net.URLEncoder;
154N/Aimport java.text.DecimalFormat;
154N/Aimport java.text.NumberFormat;
434N/Aimport java.util.logging.Level;
434N/Aimport org.opensolaris.opengrok.OpenGrokLogger;
259N/Aimport org.opensolaris.opengrok.configuration.RuntimeEnvironment;
89N/Aimport org.opensolaris.opengrok.history.Annotation;
0N/A
0N/A/**
0N/A * File for useful functions
0N/A */
456N/Apublic final class Util {
92N/A /**
92N/A * Return a string which represents a <code>CharSequence</code> in HTML.
92N/A *
92N/A * @param q a character sequence
92N/A * @return a string representing the character sequence in HTML
92N/A */
154N/A
456N/A private Util() {
456N/A // Util class, should not be constructed
456N/A }
456N/A
345N/A public static String htmlize(CharSequence q) {
0N/A StringBuilder sb = new StringBuilder(q.length() * 2);
345N/A htmlize(q, sb);
0N/A return sb.toString();
0N/A }
92N/A
92N/A /**
92N/A * Append a character sequence to an <code>Appendable</code> object. Escape
92N/A * special characters for HTML.
92N/A *
92N/A * @param q a character sequence
92N/A * @param out the object to append the character sequence to
92N/A * @exception IOException if an I/O error occurs
92N/A */
345N/A public static void htmlize(CharSequence q, Appendable out)
92N/A throws IOException {
92N/A for (int i = 0; i < q.length(); i++) {
345N/A htmlize(q.charAt(i), out);
0N/A }
0N/A }
92N/A
92N/A /**
92N/A * Append a character sequence to a <code>StringBuilder</code>
92N/A * object. Escape special characters for HTML. This method is identical to
345N/A * <code>htmlize(CharSequence,Appendable)</code>, except that it is
92N/A * guaranteed not to throw <code>IOException</code> because it uses a
92N/A * <code>StringBuilder</code>.
92N/A *
92N/A * @param q a character sequence
92N/A * @param out the object to append the character sequence to
345N/A * @see #htmlize(CharSequence, Appendable)
92N/A */
439N/A @SuppressWarnings("PMD.AvoidThrowingRawExceptionTypes")
345N/A public static void htmlize(CharSequence q, StringBuilder out) {
92N/A try {
345N/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.
154N/A throw new RuntimeException("StringBuilder should not throw IOException", ioe);
92N/A }
92N/A }
92N/A
345N/A public static void htmlize(char[] cs, int length, Appendable out)
92N/A throws IOException {
154N/A for (int i = 0; i < length && i < cs.length; i++) {
345N/A htmlize(cs[i], out);
92N/A }
92N/A }
92N/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
92N/A * representing the special character.
92N/A *
92N/A * @param c the character to append
92N/A * @param out the object to append the character to
92N/A * @exception IOException if an I/O error occurs
92N/A */
345N/A private static void htmlize(char c, Appendable out) throws IOException {
92N/A switch (c) {
154N/A case '&':
154N/A out.append("&amp;");
154N/A break;
154N/A case '>':
154N/A out.append("&gt;");
154N/A break;
154N/A case '<':
154N/A out.append("&lt;");
154N/A break;
154N/A case '\n':
154N/A out.append("<br/>");
154N/A break;
154N/A default:
154N/A out.append(c);
92N/A }
92N/A }
92N/A
0N/A public static String breadcrumbPath(String urlPrefix, String l) {
0N/A return breadcrumbPath(urlPrefix, l, '/');
0N/A }
154N/A
0N/A public static String breadcrumbPath(String urlPrefix, String l, char sep) {
154N/A if (l == null || l.length() <= 1) {
0N/A return l;
154N/A }
0N/A StringBuilder hyperl = new StringBuilder(20);
154N/A if (l.charAt(0) == sep) {
0N/A hyperl.append(sep);
0N/A }
154N/A int s = 0,
154N/A e = 0;
154N/A while ((e = l.indexOf(sep, s)) >= 0) {
154N/A if (e - s > 0) {
0N/A hyperl.append("<a href=\"" + urlPrefix);
154N/A hyperl.append(l.substring(0, e));
0N/A hyperl.append("/\">");
154N/A hyperl.append(l.substring(s, e));
0N/A hyperl.append("</a>");
0N/A hyperl.append(sep);
0N/A }
154N/A s = e + 1;
0N/A }
0N/A if (s < l.length()) {
0N/A hyperl.append("<a href=\"" + urlPrefix);
0N/A hyperl.append(l);
0N/A hyperl.append("\">");
0N/A hyperl.append(l.substring(s, l.length()));
0N/A hyperl.append("</a>");
0N/A }
0N/A return hyperl.toString();
0N/A }
154N/A
0N/A public static String redableSize(long num) {
0N/A float l = (float) num;
0N/A NumberFormat formatter = new DecimalFormat("#,###,###,###.#");
154N/A if (l < 1024) {
0N/A return formatter.format(l);
154N/A } else if (l < 1048576) {
0N/A return (formatter.format(l / 1024) + "K");
0N/A } else {
154N/A return ("<b>" + formatter.format(l / 1048576) + "M</b>");
0N/A }
0N/A }
154N/A
89N/A public static void readableLine(int num, Writer out, Annotation annotation)
89N/A throws IOException {
0N/A String snum = String.valueOf(num);
54N/A if (num > 1) {
54N/A out.write("\n");
54N/A }
54N/A out.write("<a class=\"");
0N/A out.write((num % 10 == 0 ? "hl" : "l"));
0N/A out.write("\" name=\"");
0N/A out.write(snum);
0N/A out.write("\">");
0N/A out.write((num > 999 ? " " : (num > 99 ? " " : (num > 9 ? " " : " "))));
0N/A out.write(snum);
0N/A out.write(" </a>");
89N/A if (annotation != null) {
89N/A String r = annotation.getRevision(num);
168N/A boolean enabled = annotation.isEnabled(num);
168N/A
181N/A out.write("<span class=\"blame\"><span class=\"l\"> ");
89N/A for (int i = r.length(); i < annotation.getWidestRevision(); i++) {
89N/A out.write(" ");
89N/A }
154N/A
168N/A if (enabled) {
168N/A out.write("<a href=\"");
168N/A out.write(URIEncode(annotation.getFilename()));
168N/A out.write("?a=true&r=");
168N/A out.write(URIEncode(r));
168N/A out.write("\">");
168N/A }
168N/A
345N/A htmlize(r, out);
168N/A
168N/A if (enabled) {
168N/A out.write("</a>");
168N/A }
168N/A
89N/A out.write(" </span>");
89N/A
89N/A String a = annotation.getAuthor(num);
89N/A out.write("<span class=\"l\"> ");
89N/A for (int i = a.length(); i < annotation.getWidestAuthor(); i++) {
89N/A out.write(" ");
89N/A }
259N/A String link = RuntimeEnvironment.getInstance().getUserPage();
259N/A if (link != null && link.length() > 0) {
259N/A out.write("<a href=\"");
259N/A out.write(link);
259N/A out.write(URIEncode(a));
259N/A out.write("\">");
345N/A htmlize(a, out);
259N/A out.write("</a>");
259N/A } else {
345N/A htmlize(a, out);
259N/A }
181N/A out.write(" </span></span>");
89N/A }
0N/A }
154N/A
0N/A /**
0N/A * Append path and date into a string in such a way that lexicographic
0N/A * sorting gives the same results as a walk of the file hierarchy. Thus
0N/A * null (\u0000) is used both to separate directory components and to
0N/A * separate the path from the date.
0N/A */
0N/A public static String uid(String path, String date) {
153N/A return path.replace('/', '\u0000') + "\u0000" + date;
0N/A }
154N/A
0N/A public static String uid2url(String uid) {
0N/A String url = uid.replace('\u0000', '/'); // replace nulls with slashes
0N/A return url.substring(0, url.lastIndexOf('/')); // remove date from end
0N/A }
154N/A
0N/A public static String URIEncode(String q) {
159N/A try {
159N/A // We should probably use an encoding which supports a larger
159N/A // character set, but use ISO-8859-1 for now, since that's what
159N/A // we use other places in the code.
159N/A return URLEncoder.encode(q, "ISO-8859-1");
159N/A } catch (UnsupportedEncodingException e) {
159N/A // Should not happen. ISO-8859-1 must be supported by all JVMs.
159N/A return null;
0N/A }
0N/A }
154N/A
158N/A public static String URIEncodePath(String path) {
292N/A try {
292N/A URI uri = new URI(null, null, path, null);
292N/A return uri.getRawPath();
292N/A } catch (URISyntaxException ex) {
434N/A OpenGrokLogger.getLogger().log(Level.WARNING, "Could not encode path " + path, ex);
292N/A return "";
158N/A }
158N/A }
158N/A
0N/A public static String formQuoteEscape(String q) {
58N/A if (q == null) {
58N/A return "";
58N/A }
0N/A StringBuilder sb = new StringBuilder();
0N/A char c;
154N/A for (int i = 0; i < q.length(); i++) {
0N/A c = q.charAt(i);
154N/A if (c == '"') {
0N/A sb.append("&quot;");
0N/A } else {
0N/A sb.append(c);
0N/A }
0N/A }
0N/A return sb.toString();
0N/A }
226N/A
226N/A /**
226N/A * Build a string that may be converted to a Query and passed to Lucene.
226N/A * All parameters may be passed as null or an empty string to indicate that
226N/A * they are unused.
226N/A *
226N/A * @param freetext The string from the "Full Search" text-field. This field
226N/A * will be applied as it is specified.
226N/A * @param defs The string from the "Definition" text-field. This field
226N/A * will be searched for in the <b>defs</b> field in the lucene
226N/A * index. All occurences of ":" will be replaced with "\:"
226N/A * @param refs The string from the "Symbol" text-field. This field
226N/A * will be searched for in the <b>refs</b> field in the lucene
226N/A * index. All occurences of ":" will be replaced with "\:"
226N/A * @param path The string from the "File Path" text-field. This field
226N/A * will be searched for in the <b>path</b> field in the lucene
226N/A * index. All occurences of ":" will be replaced with "\:"
226N/A * @param hist The string from the "History" text-field. This field
226N/A * will be searched for in the <b>hist</b> field in the lucene
226N/A * index. All occurences of ":" will be replaced with "\:"
226N/A * @return A string to be parsed by the Lucene parser.
226N/A */
226N/A public static String buildQueryString(String freetext, String defs, String refs, String path, String hist) {
226N/A StringBuilder sb = new StringBuilder();
226N/A if (freetext != null && freetext.length() > 0) {
276N/A sb.append(freetext.replace("::", "\\:\\:"));
226N/A }
226N/A
226N/A if (defs != null && defs.length() > 0) {
226N/A sb.append(" defs:(");
582N/A sb.append(escapeQueryString(defs));
226N/A sb.append(")");
226N/A }
226N/A
226N/A if (refs != null && refs.length() > 0) {
226N/A sb.append(" refs:(");
582N/A sb.append(escapeQueryString(refs));
226N/A sb.append(")");
226N/A }
226N/A
226N/A if (path != null && path.length() > 0) {
226N/A sb.append(" path:(");
582N/A sb.append(escapeQueryString(path));
226N/A sb.append(")");
226N/A }
226N/A
226N/A if (hist != null && hist.length() > 0) {
226N/A sb.append(" hist:(");
582N/A sb.append(escapeQueryString(hist));
226N/A sb.append(")");
226N/A }
226N/A
226N/A return sb.toString();
226N/A }
582N/A
582N/A private static String escapeQueryString(String input) {
582N/A return input.replace(":", "\\:");
582N/A }
0N/A}