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 | 0 | columnDecorators.add(new ColumnDecorator("name-column", "sort-header", "Name")); |
34 | 0 | columnDecorators.add(new ColumnDecorator("last-modified-column", "sort-header", "Last Modified")); |
35 | 0 | columnDecorators.add(new ColumnDecorator("size-column", "sort-header", "Size")); |
36 | 0 | return columnDecorators; |
37 | |
} |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
protected String getDirectory(final String prefix, final String delimiter) { |
45 | 0 | if (prefix == null) { |
46 | 0 | return delimiter; |
47 | |
} |
48 | 0 | if (prefix.endsWith(delimiter)) { |
49 | 0 | return delimiter + prefix.substring(0, prefix.length() - delimiter.length()); |
50 | |
} else { |
51 | 0 | return delimiter + prefix; |
52 | |
} |
53 | |
} |
54 | |
|
55 | |
protected String getHtmlComment() { |
56 | 0 | return "<!-- Generated on " + sdf.format(new Date()) + " -->\n"; |
57 | |
} |
58 | |
|
59 | |
protected String getDocType() { |
60 | 0 | return "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"; |
61 | |
} |
62 | |
|
63 | |
protected String getMeta() { |
64 | 0 | return "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>\n"; |
65 | |
} |
66 | |
|
67 | |
protected String getGoogleAnalyticsJavascript() { |
68 | 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"; |
69 | |
} |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
public String getHtml(final List<String[]> data, final String prefix, final String delimiter) { |
75 | 0 | String directory = getDirectory(prefix, delimiter); |
76 | |
|
77 | 0 | Tag html = new Tag("html"); |
78 | 0 | Tag title = new Tag("title"); |
79 | 0 | Tag head = new Tag("head"); |
80 | 0 | Tag body = new Tag("body"); |
81 | 0 | Tag div1 = new Tag("div", "title"); |
82 | 0 | Tag span1 = new Tag("span", null, "title"); |
83 | 0 | Tag div2 = new Tag("div", "data"); |
84 | 0 | Tag div3 = new Tag("div", "footer", "footer-left"); |
85 | 0 | Tag span2 = new Tag("span", null, "footer-text"); |
86 | |
|
87 | 0 | StringBuffer sb = new StringBuffer(); |
88 | 0 | sb.append(getDocType()); |
89 | 0 | sb.append(this.html.openTag(html)); |
90 | 0 | sb.append(this.html.getIndentedContent(getHtmlComment())); |
91 | 0 | sb.append(this.html.getTag(title, "Directory listing for " + directory)); |
92 | 0 | sb.append(this.html.openTag(head)); |
93 | 0 | sb.append(this.html.getIndentedContent("<link href=\"" + context.getCss() |
94 | |
+ "\" rel=\"stylesheet\" type=\"text/css\"/>\n")); |
95 | 0 | sb.append(this.html.getIndentedContent(getMeta())); |
96 | 0 | sb.append(this.html.getIndentedContent(getGoogleAnalyticsJavascript())); |
97 | 0 | sb.append(this.html.closeTag(head)); |
98 | 0 | sb.append(this.html.openTag(body)); |
99 | 0 | sb.append(this.html.openTag(div1)); |
100 | 0 | sb.append(this.html.getTag(span1, "Directory listing for " + directory)); |
101 | 0 | sb.append(this.html.closeTag(div1)); |
102 | 0 | sb.append(this.html.getIndentedContent("<hr>\n")); |
103 | 0 | sb.append(this.html.openTag(div2)); |
104 | 0 | sb.append(getHtmlTable(data, getColumnDecorators())); |
105 | 0 | sb.append(this.html.closeTag(div2)); |
106 | 0 | sb.append(this.html.getIndentedContent("<hr>\n")); |
107 | 0 | sb.append(this.html.openTag(div3)); |
108 | 0 | sb.append(this.html.getTag(span2, context.getAbout())); |
109 | 0 | sb.append(this.html.closeTag(div3)); |
110 | 0 | sb.append(this.html.closeTag(body)); |
111 | 0 | sb.append(this.html.closeTag(html)); |
112 | 0 | return sb.toString(); |
113 | |
} |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
protected String getTableCell(final String content, final ColumnDecorator decorator) { |
119 | 0 | Tag td = new Tag("td", decorator.getTableDataClass()); |
120 | 0 | return html.getTag(td, content); |
121 | |
} |
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
protected boolean isEmpty(final Collection<?> c) { |
127 | 0 | return c == null || c.size() == 0; |
128 | |
} |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
protected Tag getTableRowTag(final int row) { |
134 | 0 | if ((row % 2) == 0) { |
135 | 0 | return new Tag("tr", "table-tr-odd"); |
136 | |
} else { |
137 | 0 | return new Tag("tr"); |
138 | |
} |
139 | |
} |
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
protected String getTableRow(final int row, final String[] data, final List<ColumnDecorator> columnDecorators) { |
145 | 0 | StringBuffer sb = new StringBuffer(); |
146 | 0 | Tag tr = getTableRowTag(row); |
147 | 0 | sb.append(html.openTag(tr)); |
148 | 0 | for (int i = 0; i < data.length; i++) { |
149 | 0 | sb.append(getTableCell(data[i], columnDecorators.get(i))); |
150 | |
} |
151 | 0 | sb.append(html.closeTag(tr)); |
152 | 0 | return sb.toString(); |
153 | |
} |
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
protected String getTableRows(final List<String[]> data, final List<ColumnDecorator> columnDecorators) { |
159 | 0 | StringBuffer sb = new StringBuffer(); |
160 | 0 | for (int i = 0; i < data.size(); i++) { |
161 | 0 | sb.append(getTableRow(i, data.get(i), columnDecorators)); |
162 | |
} |
163 | 0 | return sb.toString(); |
164 | |
} |
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
protected String getTableHeaders(final List<ColumnDecorator> columnDecorators) { |
170 | 0 | StringBuffer sb = new StringBuffer(); |
171 | 0 | for (int i = 0; i < columnDecorators.size(); i++) { |
172 | 0 | ColumnDecorator decorator = columnDecorators.get(i); |
173 | 0 | Tag th = new Tag("th", decorator.getTableDataClass()); |
174 | 0 | sb.append(html.openTag(th)); |
175 | 0 | sb.append(html.getTag(new Tag("span", decorator.getSpanClass()), decorator.getColumnTitle())); |
176 | 0 | sb.append(html.closeTag(th)); |
177 | |
} |
178 | 0 | return sb.toString(); |
179 | |
} |
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
protected String getHtmlTable(final List<String[]> data, final List<ColumnDecorator> columnDecorators) { |
185 | 0 | if (isEmpty(data)) { |
186 | 0 | return ""; |
187 | |
} |
188 | 0 | StringBuffer sb = new StringBuffer(); |
189 | 0 | Tag table = new Tag("table", "mainTable"); |
190 | 0 | Tag thead = new Tag("thead"); |
191 | 0 | Tag tr = new Tag("tr"); |
192 | 0 | Tag tbody = new Tag("tbody"); |
193 | 0 | sb.append(html.openTag(table)); |
194 | 0 | sb.append(html.openTag(thead)); |
195 | 0 | sb.append(html.openTag(tr)); |
196 | 0 | sb.append(getTableHeaders(columnDecorators)); |
197 | 0 | sb.append(html.closeTag(tr)); |
198 | 0 | sb.append(html.closeTag(thead)); |
199 | 0 | sb.append(html.openTag(tbody)); |
200 | 0 | sb.append(getTableRows(data, columnDecorators)); |
201 | 0 | sb.append(html.closeTag(tbody)); |
202 | 0 | sb.append(html.closeTag(table)); |
203 | 0 | return sb.toString(); |
204 | |
} |
205 | |
|
206 | |
public S3BucketContext getContext() { |
207 | 0 | return context; |
208 | |
} |
209 | |
|
210 | |
public void setContext(final S3BucketContext context) { |
211 | 0 | this.context = context; |
212 | 0 | } |
213 | |
|
214 | |
} |