Util.java revision 158
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright/*
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright * CDDL HEADER START
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright *
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.
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright *
e592b34ac3f93557b9c7b30f9debc810a4875e79Derek Gathright * See LICENSE.txt included in this distribution for the specific
e592b34ac3f93557b9c7b30f9debc810a4875e79Derek Gathright * language governing permissions and limitations under the License.
e592b34ac3f93557b9c7b30f9debc810a4875e79Derek Gathright *
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 *
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright * CDDL HEADER END
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright */
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright
32db3cd3a46c3850c354f593f6ea5ebef8b50591Derek Gathright/*
32db3cd3a46c3850c354f593f6ea5ebef8b50591Derek Gathright * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathright * Use is subject to license terms.
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathright */
32db3cd3a46c3850c354f593f6ea5ebef8b50591Derek Gathrightpackage org.opensolaris.opengrok.web;
32db3cd3a46c3850c354f593f6ea5ebef8b50591Derek Gathright
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathrightimport java.io.BufferedReader;
a7e9ca67d735ae1bcdc1f7deb35ab98dd9c9f614Derek Gathrightimport java.io.File;
78ac1ef5c64e9e95a94bbfe859662da6d21b243aEric Ferraiuoloimport java.io.FileReader;
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathrightimport java.io.IOException;
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathrightimport java.io.Writer;
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathrightimport java.util.StringTokenizer;
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathrightimport java.text.DecimalFormat;
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathrightimport java.text.NumberFormat;
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathrightimport org.opensolaris.opengrok.history.Annotation;
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright/**
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathright * File for useful functions
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathright */
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathrightpublic class Util {
78ac1ef5c64e9e95a94bbfe859662da6d21b243aEric Ferraiuolo /**
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright * Return a string which represents a <code>CharSequence</code> in HTML.
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright *
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright * @param q a character sequence
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright * @return a string representing the character sequence in HTML
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright */
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathright public static String Htmlize(CharSequence q) {
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathright StringBuilder sb = new StringBuilder(q.length() * 2);
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright Htmlize(q, sb);
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright return sb.toString();
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathright }
78ac1ef5c64e9e95a94bbfe859662da6d21b243aEric Ferraiuolo
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright /**
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright * Append a character sequence to an <code>Appendable</code> object. Escape
347511d70b1f81d9b08363612fdabcef8a85e7a6Derek Gathright * special characters for HTML.
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright *
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
79a7949ec943200ad3c7f1c20af065cf8936907dDerek Gathright */
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathright public static void Htmlize(CharSequence q, Appendable out)
78ac1ef5c64e9e95a94bbfe859662da6d21b243aEric Ferraiuolo throws IOException {
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright for (int i = 0; i < q.length(); i++) {
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright Htmlize(q.charAt(i), out);
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright }
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright }
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathright
79a7949ec943200ad3c7f1c20af065cf8936907dDerek Gathright /**
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>.
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright *
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 */
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathright public static void Htmlize(CharSequence q, StringBuilder out) {
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright try {
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright Htmlize(q, (Appendable) out);
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright } catch (IOException ioe) {
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 }
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright }
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright public static void Htmlize(char[] cs, int length, Appendable out)
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright throws IOException {
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright for (int i = 0; i < length && i < cs.length; i++) {
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathright Htmlize(cs[i], out);
78ac1ef5c64e9e95a94bbfe859662da6d21b243aEric Ferraiuolo }
78ac1ef5c64e9e95a94bbfe859662da6d21b243aEric Ferraiuolo }
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright /**
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 *
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 */
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright private static void Htmlize(char c, Appendable out) throws IOException {
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright switch (c) {
78ac1ef5c64e9e95a94bbfe859662da6d21b243aEric Ferraiuolo case '&':
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright out.append("&amp;");
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright break;
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright case '>':
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathright out.append("&gt;");
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathright break;
78ac1ef5c64e9e95a94bbfe859662da6d21b243aEric Ferraiuolo case '<':
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright out.append("&lt;");
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright break;
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright case '\n':
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright out.append("<br/>");
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright break;
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright default:
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright out.append(c);
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright }
f6eaca952977579049a77179dc827c5fc1d6c808Derek Gathright }
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright
78ac1ef5c64e9e95a94bbfe859662da6d21b243aEric Ferraiuolo public static String breadcrumbPath(String urlPrefix, String l) {
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright return breadcrumbPath(urlPrefix, l, '/');
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright }
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathright public static String breadcrumbPath(String urlPrefix, String l, char sep) {
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathright if (l == null || l.length() <= 1) {
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright return l;
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright }
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright StringBuilder hyperl = new StringBuilder(20);
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright if (l.charAt(0) == sep) {
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright hyperl.append(sep);
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright }
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright int s = 0,
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright e = 0;
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright while ((e = l.indexOf(sep, s)) >= 0) {
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathright if (e - s > 0) {
32db3cd3a46c3850c354f593f6ea5ebef8b50591Derek Gathright hyperl.append("<a href=\"" + urlPrefix);
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathright hyperl.append(l.substring(0, e));
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright hyperl.append("/\">");
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright hyperl.append(l.substring(s, e));
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright hyperl.append("</a>");
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright hyperl.append(sep);
32db3cd3a46c3850c354f593f6ea5ebef8b50591Derek Gathright }
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright s = e + 1;
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright }
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright if (s < l.length()) {
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathright hyperl.append("<a href=\"" + urlPrefix);
32db3cd3a46c3850c354f593f6ea5ebef8b50591Derek Gathright hyperl.append(l);
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathright hyperl.append("\">");
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathright hyperl.append(l.substring(s, l.length()));
a7e9ca67d735ae1bcdc1f7deb35ab98dd9c9f614Derek Gathright hyperl.append("</a>");
32db3cd3a46c3850c354f593f6ea5ebef8b50591Derek Gathright }
32db3cd3a46c3850c354f593f6ea5ebef8b50591Derek Gathright return hyperl.toString();
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright }
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathright
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) {
1321f3d57acdabe63869fd24affd5953be3348b7Derek Gathright return formatter.format(l);
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright } else if (l < 1048576) {
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright return (formatter.format(l / 1024) + "K");
1321f3d57acdabe63869fd24affd5953be3348b7Derek Gathright } else {
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright return ("<b>" + formatter.format(l / 1048576) + "M</b>");
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright }
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright }
1321f3d57acdabe63869fd24affd5953be3348b7Derek Gathright
1321f3d57acdabe63869fd24affd5953be3348b7Derek Gathright public static void readableLine(int num, Writer out, Annotation annotation)
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright throws IOException {
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright String snum = String.valueOf(num);
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright if (num > 1) {
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright out.write("\n");
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright }
1321f3d57acdabe63869fd24affd5953be3348b7Derek Gathright out.write("<a class=\"");
1321f3d57acdabe63869fd24affd5953be3348b7Derek Gathright out.write((num % 10 == 0 ? "hl" : "l"));
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright out.write("\" name=\"");
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright out.write(snum);
e592b34ac3f93557b9c7b30f9debc810a4875e79Derek Gathright out.write("\">");
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright out.write((num > 999 ? " " : (num > 99 ? " " : (num > 9 ? " " : " "))));
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright out.write(snum);
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright out.write(" </a>");
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright if (annotation != null) {
c3e956d9b62a538911d580547f2dff07b7f0a4b0Derek Gathright String r = annotation.getRevision(num);
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright out.write("<span class=\"l\"> ");
c3e956d9b62a538911d580547f2dff07b7f0a4b0Derek Gathright for (int i = r.length(); i < annotation.getWidestRevision(); i++) {
c3e956d9b62a538911d580547f2dff07b7f0a4b0Derek Gathright out.write(" ");
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright }
615e6a4633b53af433f9bfb616363b682d487043Derek Gathright
615e6a4633b53af433f9bfb616363b682d487043Derek Gathright out.write("<a href=\"");
615e6a4633b53af433f9bfb616363b682d487043Derek Gathright out.write(URIEncode(annotation.getFilename()));
cceccdb28016ec7a8589d981db1237c1f72c6f93Derek Gathright out.write("?a=true&r=");
e592b34ac3f93557b9c7b30f9debc810a4875e79Derek Gathright out.write(URIEncode(r));
615e6a4633b53af433f9bfb616363b682d487043Derek Gathright out.write("\">");
1321f3d57acdabe63869fd24affd5953be3348b7Derek Gathright Htmlize(r, out);
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright out.write("</a>");
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright out.write(" </span>");
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright String a = annotation.getAuthor(num);
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright out.write("<span class=\"l\"> ");
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright for (int i = a.length(); i < annotation.getWidestAuthor(); i++) {
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright out.write(" ");
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright }
c3e956d9b62a538911d580547f2dff07b7f0a4b0Derek Gathright Htmlize(a, out);
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright out.write(" </span>");
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright }
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright }
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright /**
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.
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright */
c3e956d9b62a538911d580547f2dff07b7f0a4b0Derek Gathright public static String uid(String path, String date) {
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright return path.replace('/', '\u0000') + "\u0000" + date;
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright }
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright public static String uid2url(String uid) {
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 }
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright private static char[] hexdigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
32db3cd3a46c3850c354f593f6ea5ebef8b50591Derek Gathright
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright public static String URIEncode(String q) {
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathright StringBuilder sb = new StringBuilder();
c3e956d9b62a538911d580547f2dff07b7f0a4b0Derek Gathright char c;
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright for (int i = 0; i < q.length(); i++) {
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright c = q.charAt(i);
e592b34ac3f93557b9c7b30f9debc810a4875e79Derek Gathright if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright || (c == '-') || (c == '.') || (c == '_') || (c == '~')) {
b2e3e4e5b9ad601bb552e859ade3329168457dd6Derek Gathright sb.append(c);
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathright } else {
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathright sb.append("%");
bb09a8bcec28d3fcfe6649ea17c5042b68ea1627Derek Gathright sb.append(hexdigits[(0xf0 & c) >>> 4]);
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathright sb.append(hexdigits[0x0f & c]);
78ac1ef5c64e9e95a94bbfe859662da6d21b243aEric Ferraiuolo }
d8a453d703387a8f8168c110b3a052dc60e4a794Derek Gathright }
c3e956d9b62a538911d580547f2dff07b7f0a4b0Derek Gathright return sb.toString();
e592b34ac3f93557b9c7b30f9debc810a4875e79Derek Gathright }
e592b34ac3f93557b9c7b30f9debc810a4875e79Derek Gathright
e592b34ac3f93557b9c7b30f9debc810a4875e79Derek Gathright public static String URIEncodePath(String path) {
e592b34ac3f93557b9c7b30f9debc810a4875e79Derek Gathright final String SEGMENT_SEP = "/";
e592b34ac3f93557b9c7b30f9debc810a4875e79Derek Gathright StringTokenizer pathTokenizer = new StringTokenizer(path, SEGMENT_SEP, true);
e592b34ac3f93557b9c7b30f9debc810a4875e79Derek Gathright StringBuilder urlPath = new StringBuilder();
e592b34ac3f93557b9c7b30f9debc810a4875e79Derek Gathright
e592b34ac3f93557b9c7b30f9debc810a4875e79Derek Gathright while (pathTokenizer.hasMoreTokens()) {
e592b34ac3f93557b9c7b30f9debc810a4875e79Derek Gathright String token = pathTokenizer.nextToken();
e592b34ac3f93557b9c7b30f9debc810a4875e79Derek Gathright urlPath.append(SEGMENT_SEP.equals(token) ? token : URIEncode(token));
615e6a4633b53af433f9bfb616363b682d487043Derek Gathright }
615e6a4633b53af433f9bfb616363b682d487043Derek Gathright
615e6a4633b53af433f9bfb616363b682d487043Derek Gathright return urlPath.toString();
615e6a4633b53af433f9bfb616363b682d487043Derek Gathright }
615e6a4633b53af433f9bfb616363b682d487043Derek Gathright
615e6a4633b53af433f9bfb616363b682d487043Derek Gathright public static String formQuoteEscape(String q) {
615e6a4633b53af433f9bfb616363b682d487043Derek Gathright if (q == null) {
615e6a4633b53af433f9bfb616363b682d487043Derek Gathright return "";
615e6a4633b53af433f9bfb616363b682d487043Derek Gathright }
615e6a4633b53af433f9bfb616363b682d487043Derek Gathright StringBuilder sb = new StringBuilder();
615e6a4633b53af433f9bfb616363b682d487043Derek Gathright char c;
615e6a4633b53af433f9bfb616363b682d487043Derek Gathright for (int i = 0; i < q.length(); i++) {
c3e956d9b62a538911d580547f2dff07b7f0a4b0Derek Gathright c = q.charAt(i);
c3e956d9b62a538911d580547f2dff07b7f0a4b0Derek Gathright if (c == '"') {
c3e956d9b62a538911d580547f2dff07b7f0a4b0Derek Gathright sb.append("&quot;");
615e6a4633b53af433f9bfb616363b682d487043Derek Gathright } else {
615e6a4633b53af433f9bfb616363b682d487043Derek Gathright sb.append(c);
615e6a4633b53af433f9bfb616363b682d487043Derek Gathright }
615e6a4633b53af433f9bfb616363b682d487043Derek Gathright }
615e6a4633b53af433f9bfb616363b682d487043Derek Gathright return sb.toString();
615e6a4633b53af433f9bfb616363b682d487043Derek Gathright }
615e6a4633b53af433f9bfb616363b682d487043Derek Gathright}
615e6a4633b53af433f9bfb616363b682d487043Derek Gathright