Util.java revision 25
0N/A/*
0N/A * CDDL HEADER START
0N/A *
0N/A * The contents of this file are subject to the terms of the
0N/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/*
0N/A * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
0N/A * Use is subject to license terms.
0N/A */
0N/A
0N/A/*
0N/A * ident "@(#)Util.java 1.2 05/12/01 SMI"
0N/A */
0N/A
0N/Apackage org.opensolaris.opengrok.web;
0N/A
0N/Aimport java.util.regex.*;
0N/Aimport java.text.*;
0N/Aimport java.io.*;
0N/A
0N/A/**
0N/A * File for useful functions
0N/A */
0N/Apublic class Util {
0N/A public static String Htmlize(String q) {
0N/A StringBuilder sb = new StringBuilder(q.length() * 2);
0N/A char c;
0N/A for(int i=0; i < q.length() ; i++) {
0N/A c = q.charAt(i);
0N/A if (c == '&') {
0N/A sb.append("&amp;");
0N/A } else if(c == '>') {
0N/A sb.append("&gt;");
0N/A } else if(c == '<') {
0N/A sb.append("&lt;");
0N/A } else if(c == '\n') {
0N/A sb.append("<br/>");
0N/A } else {
0N/A sb.append(c);
0N/A }
0N/A }
0N/A return sb.toString();
0N/A }
0N/A public static void Htmlize(char[] cs, int length, Writer out) throws IOException {
0N/A char c;
0N/A for(int i=0; i < length && i < cs.length; i++) {
0N/A c = cs[i];
0N/A if (c == '&') {
0N/A out.append("&amp;");
0N/A } else if(c == '>') {
0N/A out.append("&gt;");
0N/A } else if(c == '<') {
0N/A out.append("&lt;");
0N/A } else if(c == '\n') {
0N/A out.append("<br/>");
0N/A } else {
0N/A out.append(c);
0N/A }
0N/A }
0N/A }
0N/A
0N/A public static String breadcrumbPath(String urlPrefix, String l) {
0N/A return breadcrumbPath(urlPrefix, l, '/');
0N/A }
0N/A
0N/A public static String breadcrumbPath(String urlPrefix, String l, char sep) {
0N/A if(l == null || l.length() <=1 )
0N/A return l;
0N/A StringBuilder hyperl = new StringBuilder(20);
0N/A if(l.charAt(0) == sep) {
0N/A hyperl.append(sep);
0N/A }
0N/A int s = 0,e = 0;
0N/A while((e = l.indexOf(sep, s)) >= 0) {
0N/A if(e-s>0){
0N/A hyperl.append("<a href=\"" + urlPrefix);
0N/A hyperl.append(l.substring(0,e));
0N/A hyperl.append("/\">");
0N/A hyperl.append(l.substring(s,e));
0N/A hyperl.append("</a>");
0N/A hyperl.append(sep);
0N/A }
0N/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 }
0N/A
0N/A public static String redableSize(long num) {
0N/A float l = (float) num;
0N/A NumberFormat formatter = new DecimalFormat("#,###,###,###.#");
0N/A if ( l < 1024 ) {
0N/A return formatter.format(l);
0N/A } else if ( l < 1048576 ) {
0N/A return (formatter.format(l / 1024) + "K");
0N/A } else {
0N/A return ("<b>" + formatter.format(l / 1048576)+ "M</b>");
0N/A }
0N/A }
0N/A
0N/A/* public static String simpleLine(int num) {
0N/A //return("\n<a name=\""+num + "\">" + num + "\t</a>");
0N/A return("\n<a class=\"l\" name=\""+num + "\">" + (num < 10 ? " " : (num < 100 ? " " : (num < 1000? " " : ""))) + num + " </a>");
0N/A }
0N/A */
0N/A public static void readableLine(int num, Writer out) throws IOException {
0N/A/* out.write("\n<a name=\"");
0N/A out.write(num);
0N/A out.write("\">");
0N/A out.write(num);
0N/A out.write("\t</a>");*/
0N/A String snum = String.valueOf(num);
0N/A out.write("\n<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>");
0N/A }
0N/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) {
0N/A return path.replace(File.separatorChar, '\u0000') + "\u0000" + date;
0N/A }
0N/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 }
0N/A
0N/A private static char[] hexdigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
0N/A
0N/A public static String URIEncode(String q) {
0N/A StringBuilder sb = new StringBuilder();
0N/A char c;
0N/A for(int i=0; i < q.length() ; i++) {
0N/A c = q.charAt(i);
0N/A if ((c >= 'a' && c <= 'z')
0N/A ||(c >= 'A' && c <= 'Z')
0N/A ||(c >= '0' && c <= '9')) {
0N/A sb.append(c);
0N/A } else {
0N/A sb.append("%");
0N/A sb.append(hexdigits[(0xf0 & c) >>> 4 ]);
0N/A sb.append(hexdigits[0x0f & c]);
0N/A }
0N/A }
0N/A return sb.toString();
0N/A }
0N/A public static String formQuoteEscape(String q) {
0N/A StringBuilder sb = new StringBuilder();
0N/A char c;
0N/A for(int i=0; i < q.length() ; i++) {
0N/A c = q.charAt(i);
0N/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 }
0N/A
0N/A public static String FileType(String filename) {
0N/A return null;
0N/A }
0N/A
25N/A public static File getRCSFile(File file) {
0N/A return getRCSFile(file.getParent(), file.getName());
0N/A }
0N/A
25N/A public static File getRCSFile(String parent, String name) {
0N/A try{
0N/A File CVSdir = new File(parent + "/CVS");
0N/A if(CVSdir.isDirectory() && CVSdir.canRead()) {
0N/A File root = new File(CVSdir, "Root");
0N/A if (root.canRead()) {
0N/A BufferedReader rootReader = new BufferedReader(new FileReader(root));
0N/A String cvsroot = rootReader.readLine();
0N/A if(cvsroot.startsWith("/")) {
0N/A File repository = new File(CVSdir, "Repository");
0N/A BufferedReader repoReader = new BufferedReader(new FileReader(repository));
0N/A String repo = repoReader.readLine();
0N/A repoReader.close();
0N/A rootReader.close();
1N/A String dir = cvsroot + File.separatorChar + repo;
1N/A String filename = name + ",v";
1N/A File rcsFile = new File(dir, filename);
1N/A if (!rcsFile.exists()) {
1N/A File atticFile = new File(dir + File.separatorChar + "Attic", filename);
1N/A if (atticFile.exists())
1N/A rcsFile = atticFile;
1N/A }
25N/A return rcsFile;
0N/A }
0N/A rootReader.close();
0N/A }
0N/A }
0N/A } catch (Exception e) {
0N/A e.printStackTrace();
0N/A }
0N/A return null;
0N/A }
25N/A
25N/A public static File getSCCSFile(File file) {
25N/A return getSCCSFile(file.getParent(), file.getName());
25N/A }
25N/A
25N/A public static File getSCCSFile(String parent, String name) {
25N/A return new File(parent + "/SCCS/s." + name);
25N/A }
0N/A
0N/A}