CharSequence
in HTML.
*
* @param q
* a character sequence
* @return a string representing the character sequence in HTML
*/
public static String htmlize(CharSequence q) {
StringBuilder sb = new StringBuilder(q.length() * 2);
htmlize(q, sb);
return sb.toString();
}
/**
* Append a character sequence to the given destination whereby
* special characters for HTML are escaped accordingly.
*
* @param q a character sequence to esacpe
* @param dest where to append the character sequence to
*/
public static void htmlize(CharSequence q, StringBuilder dest) {
for (int i = 0; i < q.length(); i++ ) {
htmlize(q.charAt(i), dest);
}
}
/**
* Append a character array to the given destination whereby
* special characters for HTML are escaped accordingly.
*
* @param cs characters to esacpe
* @param length max. number of characters to append, starting from index 0.
* @param dest where to append the character sequence to
*/
public static void htmlize(char[] cs, int length, StringBuilder dest) {
int len = length;
if (cs.length < length) {
len = cs.length;
}
for (int i = 0; i < len; i++ ) {
htmlize(cs[i], dest);
}
}
/**
* Append a character to the given destination whereby special characters
* special for HTML are escaped accordingly.
*
* @param c the character to append
* @param dest where to append the character to
*/
private static void htmlize(char c, StringBuilder dest) {
switch (c) {
case '&':
dest.append("&");
break;
case '>':
dest.append(">");
break;
case '<':
dest.append("<");
break;
case '\n':
dest.append("
* So the difference to {@link File#getCanonicalPath()} is, that this method
* does not hit the disk (just string manipulation), resolves path
* always against '/' and thus always returns an absolute path, which may
* actually not exist, and which has a single trailing '/' if the given
* path ends with the given sep.
*
* @param path path to mangle. If not absolute or {@code null}, the
* current working directory is assumed to be '/'.
* @param sep file separator to use to crack path into path
* components
* @return always a canonical path which starts with a '/'.
*/
public static String getCanonicalPath(String path, char sep) {
if (path == null || path.length() == 0) {
return "/";
}
String[] pnames = normalize(path.split(escapeForRegex(sep)), true);
if (pnames.length == 0) {
return "/";
}
StringBuilder buf = new StringBuilder(path.length());
buf.append('/');
for (int i=0; i < pnames.length; i++) {
buf.append(pnames[i]).append('/');
}
if (path.charAt(path.length()-1) != sep) {
// since is not a general purpose method. So we waive to handle
// cases like:
// || path.endsWith("/..") || path.endsWith("/.")
buf.setLength(buf.length()-1);
}
return buf.toString();
}
private final static Pattern EMAIL_PATTERN =
Pattern.compile("([^<\\s]+@[^>\\s]+)");
/**
* Get email address of the author.
*
* @param author
* string containing author and possibly email address.
* @return email address of the author or
* full author string if the author string does not contain an email
* address.
*/
public static String getEmail(String author) {
Matcher email_matcher = EMAIL_PATTERN.matcher(author);
String email = author;
if (email_matcher.find()) {
email = email_matcher.group(1).trim();
}
return email;
}
/**
* Remove all empty and {@code null} string elements from the given
* names and optionally all redundant information like "." and
* "..".
*
* @param names
* names to check
* @param canonical
* if {@code true}, remove redundant elements as well.
* @return a possible empty array of names all with a length > 0.
*/
private static String[] normalize(String[] names, boolean canonical) {
LinkedList");
out.append("
");
}
/**
* Just read the given source and dump as is to the given destionation.
* Does nothing, if one or more of the parameters is {@code null}.
* @param out write destination
* @param in source to read
* @throws IOException as defined by the given reader/writer
* @throws NullPointerException if a parameter is {@code null}.
*/
public static void dump(Writer out, Reader in) throws IOException {
if (in == null || out == null) {
return;
}
char[] buf = new char[8192];
int len = 0;
while ((len = in.read(buf)) >= 0) {
out.write(buf, 0, len);
}
}
/**
* Silently dump a file to the given destionation. All {@link IOException}s
* gets caught and logged, but not re-thrown.
*
* @param out dump destination
* @param dir directory, which should contains the file.
* @param filename the basename of the file to dump.
* @param compressed if {@code true} the denoted file is assumed to be
* gzipped.
* @return {@code true} on success (everything read and written).
* @throws NullPointerException if a parameter is {@code null}.
*/
public static boolean dump(Writer out, File dir, String filename,
boolean compressed)
{
return dump(out, new File(dir, filename), compressed);
}
/**
* Silently dump a file to the given destionation. All {@link IOException}s
* gets caught and logged, but not re-thrown.
*
* @param out dump destination
* @param file file to dump.
* @param compressed if {@code true} the denoted file is assumed to be
* gzipped.
* @return {@code true} on success (everything read and written).
* @throws NullPointerException if a parameter is {@code null}.
*/
public static boolean dump(Writer out, File file, boolean compressed) {
if (!file.exists()) {
return false;
}
FileInputStream fis = null;
GZIPInputStream gis = null;
Reader in = null;
try {
if (compressed) {
fis = new FileInputStream(file);
gis = new GZIPInputStream(fis);
in = new InputStreamReader(gis);
} else {
in = new FileReader(file);
}
dump(out, in);
return true;
} catch(IOException e) {
OpenGrokLogger.getLogger().log(Level.WARNING,
"An error occured while piping file " + file + ": ", e);
} finally {
IOUtils.close(in);
IOUtils.close(gis);
IOUtils.close(fis);
}
return false;
}
/**
* Print a row in an HTML table.
*
* @param out
* destination for the HTML output
* @param cells
* the values to print in the cells of the row
* @throws IOException
* if an error happens while writing to {@code out}
*/
private static void printTableRow(Appendable out, Object... cells)
throws IOException
{
out.append(" ");
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
printTableRow(out, "Source root", env.getSourceRootPath());
printTableRow(out, "Data root", env.getDataRootPath());
printTableRow(out, "CTags", env.getCtags());
printTableRow(out, "Bug page", env.getBugPage());
printTableRow(out, "Bug pattern", env.getBugPattern());
printTableRow(out, "User page", env.getUserPage());
printTableRow(out, "User page suffix", env.getUserPageSuffix());
printTableRow(out, "Review page", env.getReviewPage());
printTableRow(out, "Review pattern", env.getReviewPattern());
printTableRow(out, "Using projects", env.hasProjects());
out.append("Variable Value ");
printTableRow(out, "Index word limit", env.getIndexWordLimit());
printTableRow(out, "Allow leading wildcard in search",
env.isAllowLeadingWildcard());
printTableRow(out, "History cache", HistoryGuru.getInstance()
.getCacheInfo());
out.append("Ignored files ");
printUnorderedList(out, env.getIgnoredNames().getItems());
out.append(" ");
StringBuilder buf = new StringBuilder(256);
for (Object cell : cells) {
out.append(" ");
}
/**
* Print an unordered list (HTML).
*
* @param out
* destination for the HTML output
* @param items
* the list items
* @throws IOException
* if an error happens while writing to {@code out}
*/
private static void printUnorderedList(Appendable out,
Collection");
String str = (cell == null) ? "null" : cell.toString();
htmlize(str, buf);
out.append(str);
buf.setLength(0);
out.append(" ");
}
out.append("");
StringBuilder buf = new StringBuilder(256);
for (String item : items) {
out.append("
");
}
/**
* Create a string literal for use in JavaScript functions.
*
* @param str
* the string to be represented by the literal
* @return a JavaScript string literal
*/
public static String jsStringLiteral(String str) {
StringBuilder sb = new StringBuilder();
sb.append('"');
for (int i = 0; i < str.length(); i++ ) {
char c = str.charAt(i);
switch (c) {
case '"':
sb.append("\\\"");
break;
case '\\':
sb.append("\\\\");
break;
case '\n':
sb.append("\\n");
break;
case '\r':
sb.append("\\r");
break;
default:
sb.append(c);
}
}
sb.append('"');
return sb.toString();
}
}