/
El.java
executable file
·84 lines (71 loc) · 2.42 KB
/
El.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package org.thomnichols.pythonwebconsole;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class El {
private static Pattern NEWLINE = Pattern.compile( "\r?\n" );
private static Pattern TAGS = Pattern.compile( "<|>" );
private static DateFormat SHORT_DF = new SimpleDateFormat( "yyyy-MM-dd" );
private static DateFormat LONG_DF = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss z" );
/**
* Take some text and format it so it displays nicely as HTML
* @param text
* @return
*/
public static String toHTML( String text ) {
return NEWLINE.matcher( text ).replaceAll( "<br/>\n" );
}
/**
* More terse mechanism to escape XML since EL doesn't by default
* (Why oh why???) and <c:out is both verbose and makes XML editors
* choke when used in an HTML attribute.
* @param text
* @return
*/
public static String esc( String text ) {
Matcher m = TAGS.matcher( text );
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, "<".equals( m.group() ) ? "<": ">" );
}
m.appendTail(sb);
return sb.toString();
}
public static String escURL( String text ) throws UnsupportedEncodingException {
return URLEncoder.encode( text, "utf-8" );
}
public static String shortDate( Date dt ) {
return SHORT_DF.format( dt );
}
public static String longDate( Date dt ) {
return LONG_DF.format( dt );
}
public static String relativeDate( Date dt ) {
long difference = System.currentTimeMillis() - dt.getTime();
if ( difference < 60000 ) {
difference = difference / 1000;
if ( difference == 1 ) return "1 second ago";
return "" + difference + " seconds ago";
}
if ( difference < 3600000 ) {
difference = difference / 60000;
if ( difference == 1 ) return "1 minute ago";
return "" + difference + " minutes ago";
}
if ( difference < 86400000 ) {
difference = difference / 3600000;
if ( difference == 1 ) return "1 hour ago";
return "" + difference + " hours ago";
}
return El.longDate( dt ); // if > 24 hours, just show date.
}
public static int size( Collection<?> c ) {
return c.size();
}
// TODO tag for relative times, e.g. 'nn [seconds|minutes] ago'
}