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