possilibity to generate HTML while embedded on the web site
[meviz.git] / src / main / java / eu / svjatoslav / meviz / htmlindexer / layouts / MixedLayout.java
1 /*
2  * Meviz - Various tools collection to work with multimedia.
3  * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 2 of the GNU General Public License
7  * as published by the Free Software Foundation.
8  */
9
10 package eu.svjatoslav.meviz.htmlindexer.layouts;
11
12 import java.io.UnsupportedEncodingException;
13 import java.util.List;
14
15 import org.apache.log4j.Logger;
16
17 import eu.svjatoslav.commons.file.FilePathParser;
18 import eu.svjatoslav.commons.network.UrlParamEncoder;
19 import eu.svjatoslav.meviz.htmlindexer.AbstractIndexer;
20 import eu.svjatoslav.meviz.htmlindexer.Constants;
21 import eu.svjatoslav.meviz.htmlindexer.IndexingContext;
22 import eu.svjatoslav.meviz.htmlindexer.Layout;
23 import eu.svjatoslav.meviz.htmlindexer.metadata.Dimension;
24 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.AbstractFile;
25 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.GeneralFile;
26 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.Picture;
27
28 public class MixedLayout implements Layout {
29
30         Logger logger = Logger.getLogger(MixedLayout.class);
31
32         StringBuffer dirHtml = new StringBuffer();
33
34         StringBuffer filesHtml = new StringBuffer();
35
36         String galleryTitle;
37
38         List<String> path;
39
40         public static final String encoding = "UTF-8";
41
42         int currentColumn = 0;
43
44         int horizontalColumnsLimit = 2;
45
46         private AbstractIndexer indexer;
47
48         private IndexingContext indexingContext;
49
50         public void columnAdded() {
51                 currentColumn++;
52                 if (currentColumn > horizontalColumnsLimit) {
53
54                         currentColumn = 0;
55
56                         filesHtml.append("</TR>\n");
57                 }
58         }
59
60         @Override
61         public void enlistDirectory(final AbstractFile directory) {
62                 dirHtml.append("<TR>");
63                 dirHtml.append("<TD><b><a href=\""
64                                 + indexer.getDirectoryUrl(directory, indexingContext)
65                                 + "\"> ==&gt; " + directory.fileName + "</a></b></TD>");
66                 dirHtml.append("</TR>\n");
67
68         }
69
70         @Override
71         public void enlistFile(final AbstractFile file) {
72                 finishRow();
73
74                 final String fullFileName = file.fileName;
75
76                 filesHtml.append("<TR>\n");
77                 filesHtml.append("    <TD colspan=\""
78                                 + String.valueOf(horizontalColumnsLimit)
79                                 + "\"><a href=\""
80                                 + indexingContext.getGlobalUrl()
81                                 + UrlParamEncoder.encode(indexingContext.getLocalUrl()
82                                                 + fullFileName) + "\">" + fullFileName + "</a>\n");
83                 filesHtml.append("&nbsp;&nbsp;("
84                                 + FilePathParser.getFileSizeDescription(file.getFileLength())
85                                 + ")</TD>\n");
86                 filesHtml.append("</TR>\n");
87
88         }
89
90         @Override
91         public void enlistImage(final AbstractFile abstractFile) {
92                 try {
93
94                         final Picture picture = (Picture) abstractFile;
95
96                         final Dimension imageSize = picture.getDimensions();
97
98                         final List<Dimension> recommendedDimensions = Picture
99                                         .getThumbnailDimensions(imageSize);
100
101                         Dimension listingDimension = new Dimension(500, 500);
102
103                         if (listingDimension.width > imageSize.width)
104                                 if (listingDimension.height > imageSize.height)
105                                         listingDimension = imageSize;
106
107                         final String listingThumbnailUrl = indexer.getThumbnailUrl(picture,
108                                         listingDimension, indexingContext);
109
110                         startcolumn();
111
112                         filesHtml.append("<TD><center><a href=\""
113                                         + indexingContext.getGlobalUrl()
114                                         + UrlParamEncoder.encode(indexingContext.getLocalUrl()
115                                                         + "/" + picture.fileName) + "\">\n");
116
117                         filesHtml.append("<img border=\"0\" src=\"" + listingThumbnailUrl
118                                         + "\"/></a>\n");
119                         filesHtml.append("<br/><b>"
120                                         + FilePathParser
121                                                         .getFileNameWithoutExtension(picture.fileName)
122                                         + "</b><br/>(");
123
124                         int count = 0;
125                         for (final Dimension dimension : recommendedDimensions) {
126                                 count++;
127                                 if (count > 1)
128                                         filesHtml.append(", ");
129                                 filesHtml.append("<a href=\""
130                                                 + indexer.getThumbnailUrl(picture, dimension,
131                                                                 indexingContext) + "\">");
132                                 filesHtml.append(dimension.width + "x" + dimension.height);
133                                 filesHtml.append("</a>");
134                         }
135
136                         filesHtml.append(")</TD>\n");
137
138                         columnAdded();
139
140                 } catch (final Exception e) {
141                         logger.error(
142                                         "Error generating thumbnails for image file: "
143                                                         + e.toString()
144                                                         + ", thumbnail generation skipped, enlisting as ordinary file instead.",
145                                         e);
146                         enlistFile(abstractFile);
147                 }
148         }
149
150         /** {@inheritDoc} */
151         @Override
152         public void enlistOgv(final AbstractFile abstractFile)
153                         throws UnsupportedEncodingException {
154
155                 final GeneralFile file = (GeneralFile) abstractFile;
156
157                 finishRow();
158
159                 filesHtml
160                                 .append("<TR style=\"background-color: #404040; text-align: center\">\n");
161                 filesHtml.append("<TD style=\"text-align: center\" colspan=\""
162                                 + String.valueOf(horizontalColumnsLimit) + "\">\n");
163
164                 filesHtml.append("<video controls=\"controls\" tabindex=\"0\">\n");
165                 filesHtml
166                                 .append("<source type=\"video/ogg\" codecs=\"theora, vorbis\" src=\""
167                                                 + indexingContext.getGlobalUrl()
168                                                 + UrlParamEncoder.encode(indexingContext.getLocalUrl()
169                                                                 + file.fileName) + "\">\n");
170                 filesHtml.append("</source>\n");
171
172                 // if browser cannot play video directry, fall back to java Applet
173                 filesHtml
174                                 .append("<applet code=\"com.fluendo.player.Cortado.class\" archive=\"http://theora.org/cortado.jar\" width=\"800\" height=\"600\">\n");
175                 filesHtml.append("    <param name=\"url\" value=\""
176                                 + indexingContext.getGlobalUrl()
177                                 + UrlParamEncoder.encode(indexingContext.getLocalUrl()
178                                                 + file.fileName) + "\"/>\n");
179                 filesHtml.append("    <param name=\"keepAspect\" value=\"true\"/>\n");
180                 filesHtml.append("    <param name=\"autoPlay\" value=\"false\"/>\n");
181                 filesHtml.append("</applet>\n");
182
183                 filesHtml.append("</video>\n");
184
185                 filesHtml.append("<br/>\n");
186
187                 filesHtml
188                                 .append("Video: <b>"
189                                                 + FilePathParser
190                                                                 .getFileNameWithoutExtension(file.fileName)
191                                                 + "<b/>  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
192                 filesHtml.append("<a href=\""
193                                 + indexingContext.getGlobalUrl()
194                                 + UrlParamEncoder.encode(indexingContext.getLocalUrl()
195                                                 + file.fileName) + "\">download as OGV</a>\n");
196
197                 filesHtml.append("</TD></TR>\n");
198
199         }
200
201         public void finishRow() {
202
203                 if (currentColumn == 0)
204                         return;
205
206                 while (currentColumn <= horizontalColumnsLimit) {
207                         filesHtml.append("<TD></TD>");
208                         currentColumn++;
209                 }
210                 filesHtml.append("</TR>\n");
211                 currentColumn = 0;
212         }
213
214         /** {@inheritDoc} */
215         @Override
216         public String getFileNameSuffix() {
217                 return "";
218         }
219
220         /** {@inheritDoc} */
221         @Override
222         public String getHtml() {
223
224                 // assemble final page HTML
225                 final StringBuffer finalHtml = new StringBuffer();
226
227                 // page header
228                 finalHtml.append("<HTML>\n");
229                 finalHtml.append("<HEAD>\n");
230                 finalHtml
231                                 .append("    <meta HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8\"/>\n");
232                 finalHtml.append("</HEAD>\n");
233                 finalHtml
234                                 .append("<BODY bgcolor=\"#000000\" alink=\"#50ffff\" link=\"#50ffff\" vlink=\"#ff50ff\" text=\"#ffffff\">\n");
235                 finalHtml.append("<!-- " + Constants.HTML_MAGIC_STRING + " -->\n");
236                 finalHtml.append("<!-- " + Constants.HTML_MEMO + " -->\n");
237                 finalHtml.append("<FONT color=\"#ffffff\">\n");
238                 finalHtml.append("<CENTER><H1>" + galleryTitle + "</H1>\n");
239
240                 finalHtml.append("<H2>");
241                 for (final String p : path)
242                         finalHtml.append(" / " + p);
243                 finalHtml.append("</H2></CENTER>\n");
244
245                 // append directory listing
246                 finalHtml.append("<TABLE>\n");
247
248                 if (path.size() > 0) {
249                         finalHtml.append("<TR>");
250                         finalHtml.append("<TD><b><a href=\""
251                                         + indexer.getParentDirectoryUrl(indexingContext)
252                                         + "\"> &lt;== ..</a></b></TD>");
253                         finalHtml.append("</TR>\n");
254                 }
255
256                 finalHtml.append(dirHtml.toString());
257                 finalHtml.append("\n<TR><TD><br></TD></TR>\n");
258                 finalHtml.append("</TABLE>\n");
259
260                 // append file listing
261
262                 finishRow();
263                 finalHtml.append("<TABLE width=\"1100\">\n");
264                 finalHtml.append(filesHtml.toString());
265                 finalHtml.append("\n</TABLE>\n");
266
267                 finalHtml.append("</font></BODY></HTML>\n");
268
269                 return finalHtml.toString();
270         }
271
272         @Override
273         public void init(final String galleryTitle, final List<String> path,
274                         final AbstractIndexer indexer, final IndexingContext indexingContext) {
275
276                 this.indexer = indexer;
277                 this.galleryTitle = galleryTitle;
278                 this.path = path;
279                 this.indexingContext = indexingContext;
280
281                 dirHtml = new StringBuffer();
282                 filesHtml = new StringBuffer();
283         }
284
285         public void startcolumn() {
286                 if (currentColumn == 0) {
287                         filesHtml.append("<TR>");
288                         currentColumn = 1;
289                 }
290         }
291
292 }