Util.java revision 62
98N/A/*
98N/A * CDDL HEADER START
98N/A *
98N/A * The contents of this file are subject to the terms of the
606N/A * Common Development and Distribution License (the "License").
733N/A * You may not use this file except in compliance with the License.
98N/A *
98N/A * See LICENSE.txt included in this distribution for the specific
98N/A * language governing permissions and limitations under the License.
98N/A *
98N/A * When distributing Covered Code, include this CDDL HEADER in each
98N/A * file and include the License file at LICENSE.txt.
98N/A * If applicable, add the following below this CDDL HEADER, with the
98N/A * fields enclosed by brackets "[]" replaced with your own identifying
98N/A * information: Portions Copyright [yyyy] [name of copyright owner]
98N/A *
98N/A * CDDL HEADER END
98N/A */
98N/A
98N/A/*
98N/A * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
98N/A * Use is subject to license terms.
98N/A */
98N/A
98N/A/*
98N/A * ident "@(#)Util.java 1.2 05/12/01 SMI"
98N/A */
98N/A
98N/Apackage org.opensolaris.opengrok.web;
98N/A
98N/Aimport java.util.regex.*;
98N/Aimport java.text.*;
851N/Aimport java.io.*;
98N/A
98N/A/**
235N/A * File for useful functions
156N/A */
156N/Apublic class Util {
156N/A public static String Htmlize(String q) {
156N/A StringBuilder sb = new StringBuilder(q.length() * 2);
98N/A char c;
98N/A for(int i=0; i < q.length() ; i++) {
98N/A c = q.charAt(i);
98N/A if (c == '&') {
493N/A sb.append("&amp;");
493N/A } else if(c == '>') {
98N/A sb.append("&gt;");
98N/A } else if(c == '<') {
235N/A sb.append("&lt;");
493N/A } else if(c == '\n') {
98N/A sb.append("<br/>");
98N/A } else {
98N/A sb.append(c);
98N/A }
606N/A }
98N/A return sb.toString();
98N/A }
98N/A public static void Htmlize(char[] cs, int length, Writer out) throws IOException {
606N/A char c;
606N/A for(int i=0; i < length && i < cs.length; i++) {
98N/A c = cs[i];
493N/A if (c == '&') {
493N/A out.append("&amp;");
493N/A } else if(c == '>') {
98N/A out.append("&gt;");
98N/A } else if(c == '<') {
98N/A out.append("&lt;");
98N/A } else if(c == '\n') {
606N/A out.append("<br/>");
591N/A } else {
493N/A out.append(c);
493N/A }
493N/A }
493N/A }
493N/A
493N/A public static String breadcrumbPath(String urlPrefix, String l) {
493N/A return breadcrumbPath(urlPrefix, l, '/');
493N/A }
493N/A
705N/A public static String breadcrumbPath(String urlPrefix, String l, char sep) {
493N/A if(l == null || l.length() <=1 )
557N/A return l;
557N/A StringBuilder hyperl = new StringBuilder(20);
493N/A if(l.charAt(0) == sep) {
493N/A hyperl.append(sep);
606N/A }
606N/A int s = 0,e = 0;
851N/A while((e = l.indexOf(sep, s)) >= 0) {
851N/A if(e-s>0){
851N/A hyperl.append("<a href=\"" + urlPrefix);
851N/A hyperl.append(l.substring(0,e));
851N/A hyperl.append("/\">");
851N/A hyperl.append(l.substring(s,e));
851N/A hyperl.append("</a>");
98N/A hyperl.append(sep);
810N/A }
810N/A s = e+1;
591N/A }
810N/A if (s < l.length()) {
810N/A hyperl.append("<a href=\"" + urlPrefix);
851N/A hyperl.append(l);
591N/A hyperl.append("\">");
851N/A hyperl.append(l.substring(s, l.length()));
98N/A hyperl.append("</a>");
98N/A }
98N/A return hyperl.toString();
98N/A }
98N/A
606N/A public static String redableSize(long num) {
98N/A float l = (float) num;
606N/A NumberFormat formatter = new DecimalFormat("#,###,###,###.#");
98N/A if ( l < 1024 ) {
591N/A return formatter.format(l);
851N/A } else if ( l < 1048576 ) {
111N/A return (formatter.format(l / 1024) + "K");
111N/A } else {
111N/A return ("<b>" + formatter.format(l / 1048576)+ "M</b>");
111N/A }
111N/A }
606N/A
851N/A public static void readableLine(int num, Writer out) throws IOException {
851N/A String snum = String.valueOf(num);
851N/A if (num > 1) {
606N/A out.write("\n");
98N/A }
851N/A out.write("<a class=\"");
733N/A out.write((num % 10 == 0 ? "hl" : "l"));
733N/A out.write("\" name=\"");
733N/A out.write(snum);
733N/A out.write("\">");
733N/A out.write((num > 999 ? " " : (num > 99 ? " " : (num > 9 ? " " : " "))));
606N/A out.write(snum);
851N/A out.write(" </a>");
606N/A }
98N/A
851N/A /**
851N/A * Append path and date into a string in such a way that lexicographic
851N/A * sorting gives the same results as a walk of the file hierarchy. Thus
851N/A * null (\u0000) is used both to separate directory components and to
851N/A * separate the path from the date.
851N/A */
851N/A public static String uid(String path, String date) {
212N/A return path.replace(File.separatorChar, '\u0000') + "\u0000" + date;
851N/A }
606N/A
606N/A public static String uid2url(String uid) {
606N/A String url = uid.replace('\u0000', '/'); // replace nulls with slashes
606N/A return url.substring(0, url.lastIndexOf('/')); // remove date from end
212N/A }
98N/A
98N/A private static char[] hexdigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
98N/A
98N/A public static String URIEncode(String q) {
182N/A StringBuilder sb = new StringBuilder();
98N/A char c;
for(int i=0; i < q.length() ; i++) {
c = q.charAt(i);
if ((c >= 'a' && c <= 'z')
||(c >= 'A' && c <= 'Z')
||(c >= '0' && c <= '9')) {
sb.append(c);
} else {
sb.append("%");
sb.append(hexdigits[(0xf0 & c) >>> 4 ]);
sb.append(hexdigits[0x0f & c]);
}
}
return sb.toString();
}
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();
}
public static String FileType(String filename) {
return null;
}
public static File getRCSFile(File file) {
return getRCSFile(file.getParent(), file.getName());
}
public static File getRCSFile(String parent, String name) {
File rcsDir = new File(parent + File.separator + "RCS");
File rcsFile = new File(rcsDir, name + ",v");
if (rcsFile.exists()) {
return rcsFile;
}
// not RCS, try CVS instead
return getCVSFile(parent, name);
}
private static File getCVSFile(String parent, String name) {
try{
File CVSdir = new File(parent + "/CVS");
if(CVSdir.isDirectory() && CVSdir.canRead()) {
File root = new File(CVSdir, "Root");
if (root.canRead()) {
BufferedReader rootReader = new BufferedReader(new FileReader(root));
String cvsroot = rootReader.readLine();
if(cvsroot.startsWith("/")) {
File repository = new File(CVSdir, "Repository");
BufferedReader repoReader = new BufferedReader(new FileReader(repository));
String repo = repoReader.readLine();
repoReader.close();
rootReader.close();
String dir = cvsroot + File.separatorChar + repo;
String filename = name + ",v";
File rcsFile = new File(dir, filename);
if (!rcsFile.exists()) {
File atticFile = new File(dir + File.separatorChar + "Attic", filename);
if (atticFile.exists())
rcsFile = atticFile;
}
return rcsFile;
}
rootReader.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static File getSCCSFile(File file) {
return getSCCSFile(file.getParent(), file.getName());
}
public static File getSCCSFile(String parent, String name) {
return new File(parent + "/SCCS/s." + name);
}
}