| 1 | |
package org.kuali.maven.mojo.s3; |
| 2 | |
|
| 3 | |
import java.text.SimpleDateFormat; |
| 4 | |
import java.util.ArrayList; |
| 5 | |
import java.util.Collection; |
| 6 | |
import java.util.Date; |
| 7 | |
import java.util.List; |
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
public class CloudFrontHtmlGenerator { |
| 13 | 0 | HtmlUtils html = new HtmlUtils(); |
| 14 | 0 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS z"); |
| 15 | |
|
| 16 | |
S3BucketContext context; |
| 17 | |
|
| 18 | |
public CloudFrontHtmlGenerator() { |
| 19 | 0 | this(null); |
| 20 | 0 | } |
| 21 | |
|
| 22 | |
public CloudFrontHtmlGenerator(final S3BucketContext context) { |
| 23 | 0 | super(); |
| 24 | 0 | this.context = context; |
| 25 | 0 | } |
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
protected List<ColumnDecorator> getColumnDecorators() { |
| 31 | 0 | List<ColumnDecorator> columnDecorators = new ArrayList<ColumnDecorator>(); |
| 32 | 0 | columnDecorators.add(new ColumnDecorator("image-column", "sort-header", |
| 33 | |
"")); |
| 34 | 0 | columnDecorators.add(new ColumnDecorator("name-column", "sort-header", |
| 35 | |
"Name")); |
| 36 | 0 | columnDecorators.add(new ColumnDecorator("last-modified-column", |
| 37 | |
"sort-header", "Last Modified")); |
| 38 | 0 | columnDecorators.add(new ColumnDecorator("size-column", "sort-header", |
| 39 | |
"Size")); |
| 40 | 0 | return columnDecorators; |
| 41 | |
} |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
protected String getDirectory(final String prefix, final String delimiter) { |
| 49 | 0 | if (prefix == null) { |
| 50 | 0 | return delimiter; |
| 51 | |
} |
| 52 | 0 | if (prefix.endsWith(delimiter)) { |
| 53 | 0 | return delimiter |
| 54 | |
+ prefix.substring(0, prefix.length() - delimiter.length()); |
| 55 | |
} else { |
| 56 | 0 | return delimiter + prefix; |
| 57 | |
} |
| 58 | |
} |
| 59 | |
|
| 60 | |
protected String getHtmlComment() { |
| 61 | 0 | return "<!-- Generated on " + sdf.format(new Date()) + " -->\n"; |
| 62 | |
} |
| 63 | |
|
| 64 | |
protected String getDocType() { |
| 65 | 0 | return "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"; |
| 66 | |
} |
| 67 | |
|
| 68 | |
protected String getMeta() { |
| 69 | 0 | return "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>\n"; |
| 70 | |
} |
| 71 | |
|
| 72 | |
protected String getGoogleAnalyticsJavascript() { |
| 73 | 0 | return "<script type=\"text/javascript\">var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-16781661-1']); _gaq.push(['_setDomainName', '.kuali.org']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })();</script>\n"; |
| 74 | |
} |
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
public String getHtml(final List<String[]> data, final String prefix, final String delimiter) { |
| 80 | 0 | String directory = getDirectory(prefix, delimiter); |
| 81 | |
|
| 82 | 0 | Tag html = new Tag("html"); |
| 83 | 0 | Tag title = new Tag("title"); |
| 84 | 0 | Tag head = new Tag("head"); |
| 85 | 0 | Tag body = new Tag("body"); |
| 86 | 0 | Tag div1 = new Tag("div", "title"); |
| 87 | 0 | Tag span1 = new Tag("span", null, "title"); |
| 88 | 0 | Tag div2 = new Tag("div", "data"); |
| 89 | 0 | Tag div3 = new Tag("div", "footer", "footer-left"); |
| 90 | 0 | Tag span2 = new Tag("span", null, "footer-text"); |
| 91 | |
|
| 92 | 0 | StringBuffer sb = new StringBuffer(); |
| 93 | 0 | sb.append(getDocType()); |
| 94 | 0 | sb.append(this.html.openTag(html)); |
| 95 | 0 | sb.append(this.html.getIndentedContent(getHtmlComment())); |
| 96 | 0 | sb.append(this.html.getTag(title, "Directory listing for " + directory)); |
| 97 | 0 | sb.append(this.html.openTag(head)); |
| 98 | 0 | sb.append(this.html.getIndentedContent("<link href=\"" |
| 99 | |
+ context.getCss() |
| 100 | |
+ "\" rel=\"stylesheet\" type=\"text/css\"/>\n")); |
| 101 | 0 | sb.append(this.html.getIndentedContent(getMeta())); |
| 102 | 0 | sb.append(this.html.getIndentedContent(getGoogleAnalyticsJavascript())); |
| 103 | 0 | sb.append(this.html.closeTag(head)); |
| 104 | 0 | sb.append(this.html.openTag(body)); |
| 105 | 0 | sb.append(this.html.openTag(div1)); |
| 106 | 0 | sb.append(this.html.getTag(span1, "Directory listing for " + directory)); |
| 107 | 0 | sb.append(this.html.closeTag(div1)); |
| 108 | 0 | sb.append(this.html.getIndentedContent("<hr>\n")); |
| 109 | 0 | sb.append(this.html.openTag(div2)); |
| 110 | 0 | sb.append(getHtmlTable(data, getColumnDecorators())); |
| 111 | 0 | sb.append(this.html.closeTag(div2)); |
| 112 | 0 | sb.append(this.html.getIndentedContent("<hr>\n")); |
| 113 | 0 | sb.append(this.html.openTag(div3)); |
| 114 | 0 | sb.append(this.html.getTag(span2, context.getAbout())); |
| 115 | 0 | sb.append(this.html.closeTag(div3)); |
| 116 | 0 | sb.append(this.html.closeTag(body)); |
| 117 | 0 | sb.append(this.html.closeTag(html)); |
| 118 | 0 | return sb.toString(); |
| 119 | |
} |
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
protected String getTableCell(final String content, final ColumnDecorator decorator) { |
| 125 | 0 | Tag td = new Tag("td", decorator.getTableDataClass()); |
| 126 | 0 | return html.getTag(td, content); |
| 127 | |
} |
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
protected boolean isEmpty(final Collection<?> c) { |
| 134 | 0 | return c == null || c.size() == 0; |
| 135 | |
} |
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
protected Tag getTableRowTag(final int row) { |
| 141 | 0 | if ((row % 2) == 0) { |
| 142 | 0 | return new Tag("tr", "table-tr-odd"); |
| 143 | |
} else { |
| 144 | 0 | return new Tag("tr"); |
| 145 | |
} |
| 146 | |
} |
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
protected String getTableRow(final int row, final String[] data, |
| 152 | |
final List<ColumnDecorator> columnDecorators) { |
| 153 | 0 | StringBuffer sb = new StringBuffer(); |
| 154 | 0 | Tag tr = getTableRowTag(row); |
| 155 | 0 | sb.append(html.openTag(tr)); |
| 156 | 0 | for (int i = 0; i < data.length; i++) { |
| 157 | 0 | sb.append(getTableCell(data[i], columnDecorators.get(i))); |
| 158 | |
} |
| 159 | 0 | sb.append(html.closeTag(tr)); |
| 160 | 0 | return sb.toString(); |
| 161 | |
} |
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
protected String getTableRows(final List<String[]> data, |
| 167 | |
final List<ColumnDecorator> columnDecorators) { |
| 168 | 0 | StringBuffer sb = new StringBuffer(); |
| 169 | 0 | for (int i = 0; i < data.size(); i++) { |
| 170 | 0 | sb.append(getTableRow(i, data.get(i), columnDecorators)); |
| 171 | |
} |
| 172 | 0 | return sb.toString(); |
| 173 | |
} |
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
protected String getTableHeaders(final List<ColumnDecorator> columnDecorators) { |
| 179 | 0 | StringBuffer sb = new StringBuffer(); |
| 180 | 0 | for (int i = 0; i < columnDecorators.size(); i++) { |
| 181 | 0 | ColumnDecorator decorator = columnDecorators.get(i); |
| 182 | 0 | Tag th = new Tag("th", decorator.getTableDataClass()); |
| 183 | 0 | sb.append(html.openTag(th)); |
| 184 | 0 | sb.append(html.getTag(new Tag("span", decorator.getSpanClass()), |
| 185 | |
decorator.getColumnTitle())); |
| 186 | 0 | sb.append(html.closeTag(th)); |
| 187 | |
} |
| 188 | 0 | return sb.toString(); |
| 189 | |
} |
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
protected String getHtmlTable(final List<String[]> data, |
| 195 | |
final List<ColumnDecorator> columnDecorators) { |
| 196 | 0 | if (isEmpty(data)) { |
| 197 | 0 | return ""; |
| 198 | |
} |
| 199 | 0 | StringBuffer sb = new StringBuffer(); |
| 200 | 0 | Tag table = new Tag("table", "mainTable"); |
| 201 | 0 | Tag thead = new Tag("thead"); |
| 202 | 0 | Tag tr = new Tag("tr"); |
| 203 | 0 | Tag tbody = new Tag("tbody"); |
| 204 | 0 | sb.append(html.openTag(table)); |
| 205 | 0 | sb.append(html.openTag(thead)); |
| 206 | 0 | sb.append(html.openTag(tr)); |
| 207 | 0 | sb.append(getTableHeaders(columnDecorators)); |
| 208 | 0 | sb.append(html.closeTag(tr)); |
| 209 | 0 | sb.append(html.closeTag(thead)); |
| 210 | 0 | sb.append(html.openTag(tbody)); |
| 211 | 0 | sb.append(getTableRows(data, columnDecorators)); |
| 212 | 0 | sb.append(html.closeTag(tbody)); |
| 213 | 0 | sb.append(html.closeTag(table)); |
| 214 | 0 | return sb.toString(); |
| 215 | |
} |
| 216 | |
|
| 217 | |
public S3BucketContext getContext() { |
| 218 | 0 | return context; |
| 219 | |
} |
| 220 | |
|
| 221 | |
public void setContext(final S3BucketContext context) { |
| 222 | 0 | this.context = context; |
| 223 | 0 | } |
| 224 | |
|
| 225 | |
} |