possilibity to generate HTML while embedded on the web site
authorSvjatoslav Agejenko <n0@n0-Aspire-8920.(none)>
Sun, 3 Feb 2013 10:09:30 +0000 (12:09 +0200)
committerSvjatoslav Agejenko <n0@n0-Aspire-8920.(none)>
Sun, 3 Feb 2013 10:09:30 +0000 (12:09 +0200)
src/main/java/eu/svjatoslav/meviz/htmlindexer/AbstractIndexer.java
src/main/java/eu/svjatoslav/meviz/htmlindexer/FilesystemIndexer.java
src/main/java/eu/svjatoslav/meviz/htmlindexer/IndexingContext.java [new file with mode: 0644]
src/main/java/eu/svjatoslav/meviz/htmlindexer/Layout.java
src/main/java/eu/svjatoslav/meviz/htmlindexer/Main.java
src/main/java/eu/svjatoslav/meviz/htmlindexer/WebIndexer.java
src/main/java/eu/svjatoslav/meviz/htmlindexer/layouts/MixedLayout.java

index 4b1e29c..762fcad 100644 (file)
@@ -32,10 +32,13 @@ public abstract class AbstractIndexer {
 
        }
 
-       public abstract String getDirectoryUrl(final AbstractFile directory);
+       public abstract String getDirectoryUrl(final AbstractFile directory,
+                       IndexingContext context);
 
-       public abstract String getThumbnailPath(Picture picture,
-                       final Dimension desiredDimension);
+       public abstract String getParentDirectoryUrl(IndexingContext context);
+
+       public abstract String getThumbnailUrl(Picture picture,
+                       final Dimension desiredDimension, IndexingContext context);
 
        public static boolean isImage(final String fileExtension) {
                for (final String ext : Constants.SUPPORTED_IMAGE_EXTENSIONS)
index ea6489e..3a48b67 100755 (executable)
@@ -15,11 +15,14 @@ import java.io.FileNotFoundException;
 import java.io.FileReader;
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
+import java.util.ArrayList;
 import java.util.HashSet;
+import java.util.List;
 
 import org.apache.log4j.Logger;
 
 import eu.svjatoslav.commons.file.IOHelper;
+import eu.svjatoslav.commons.network.UrlParamEncoder;
 import eu.svjatoslav.meviz.htmlindexer.layouts.MixedLayout;
 import eu.svjatoslav.meviz.htmlindexer.metadata.Dimension;
 import eu.svjatoslav.meviz.htmlindexer.metadata.DirectoryMetadata;
@@ -50,13 +53,14 @@ public class FilesystemIndexer extends AbstractIndexer {
 
        private final IndexingOptions indexingOptions;
 
-       private final String[] pathElements;
+       private final List<String> pathElements;
 
        private HashSet<Layout> layouts;
 
        public FilesystemIndexer(final File directoryToIndex,
-                       final IndexingOptions indexingOptions, final String[] pathElements)
-                       throws IOException, ImageFormatError {
+                       final IndexingOptions indexingOptions,
+                       final List<String> pathElements) throws IOException,
+                       ImageFormatError {
 
                this.directoryToIndex = directoryToIndex;
                this.indexingOptions = indexingOptions;
@@ -148,25 +152,29 @@ public class FilesystemIndexer extends AbstractIndexer {
                }
        }
 
-       @Override
-       public String getDirectoryUrl(final AbstractFile directory) {
-               return directory.fileName + "/index.html";
-       }
+       public List<String> getChildPath(final File file) {
 
-       public String[] getSubPath(final File file) {
+               final List<String> result = new ArrayList<String>();
+               result.addAll(pathElements);
+               result.add(file.getName());
 
-               final String[] subPath = new String[pathElements.length + 1];
+               return result;
+       }
 
-               for (int i = 0; i < pathElements.length; i++)
-                       subPath[i] = pathElements[i];
+       @Override
+       public String getDirectoryUrl(final AbstractFile directory,
+                       final IndexingContext context) {
+               return directory.fileName + "/index.html";
+       }
 
-               subPath[pathElements.length] = file.getName();
-               return subPath;
+       @Override
+       public String getParentDirectoryUrl(final IndexingContext context) {
+               return "../index.html";
        }
 
        @Override
-       public String getThumbnailPath(final Picture picture,
-                       final Dimension desiredDimension) {
+       public String getThumbnailUrl(final Picture picture,
+                       final Dimension desiredDimension, final IndexingContext context) {
 
                // in case thumbnail size was equal to original, then return original
                // file path
@@ -188,7 +196,8 @@ public class FilesystemIndexer extends AbstractIndexer {
                        Picture.makeThumbnail(originalFile, thumbnailFile,
                                        desiredDimension.getAwtDimension());
                }
-               return Constants.THUMBNAILS_DIRECTORY_NAME + "/" + thumbnailFileName;
+               return Constants.THUMBNAILS_DIRECTORY_NAME + "/"
+                               + UrlParamEncoder.encode(thumbnailFileName);
        }
 
        public void indexForLayout(final Layout layout)
@@ -214,7 +223,8 @@ public class FilesystemIndexer extends AbstractIndexer {
                layouts.add(new MixedLayout());
 
                for (final Layout layout : layouts)
-                       layout.init(indexingOptions.galleryTitle, pathElements, this, "");
+                       layout.init(indexingOptions.galleryTitle, pathElements, this,
+                                       new IndexingContext("", ""));
        }
 
        public void initializeThumbnailsDirectory() {
@@ -255,7 +265,7 @@ public class FilesystemIndexer extends AbstractIndexer {
 
                                if (file.isDirectory())
                                        new FilesystemIndexer(file, indexingOptions,
-                                                       getSubPath(file));
+                                                       getChildPath(file));
                        }
        }
 }
diff --git a/src/main/java/eu/svjatoslav/meviz/htmlindexer/IndexingContext.java b/src/main/java/eu/svjatoslav/meviz/htmlindexer/IndexingContext.java
new file mode 100644 (file)
index 0000000..48c3be2
--- /dev/null
@@ -0,0 +1,37 @@
+package eu.svjatoslav.meviz.htmlindexer;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class IndexingContext {
+
+       private final String localUrl;
+       private final String globalUrl;
+
+       public IndexingContext(final String globalUrl, final String localUrl) {
+               this.globalUrl = globalUrl;
+               this.localUrl = localUrl;
+       }
+
+       public String getGlobalUrl() {
+               return globalUrl;
+       }
+
+       public List<String> getLocalPathComponents() {
+
+               final List<String> result = new ArrayList<String>();
+
+               final String[] strings = localUrl.split("/");
+
+               for (final String pathElement : strings)
+                       if (pathElement.length() > 0)
+                               result.add(pathElement);
+
+               return result;
+       }
+
+       public String getLocalUrl() {
+               return localUrl;
+       }
+
+}
index 2dac2ff..88de6ce 100755 (executable)
@@ -10,6 +10,7 @@
 package eu.svjatoslav.meviz.htmlindexer;
 
 import java.io.UnsupportedEncodingException;
+import java.util.List;
 
 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.AbstractFile;
 
@@ -61,6 +62,6 @@ public interface Layout {
        /**
         * Initialize layout for particular directory.
         */
-       public void init(String galleryTitle, String[] path,
-                       final AbstractIndexer indexer, final String pathPrefix);
+       public void init(String galleryTitle, List<String> path,
+                       final AbstractIndexer indexer, IndexingContext context);
 }
index 94a2253..335b073 100644 (file)
@@ -1,5 +1,7 @@
 package eu.svjatoslav.meviz.htmlindexer;
 
+import java.util.ArrayList;
+
 import eu.svjatoslav.meviz.Module;
 
 public class Main implements Module {
@@ -32,7 +34,7 @@ public class Main implements Module {
                }
 
                new FilesystemIndexer(indexingOptions.workingDirectory,
-                               indexingOptions, new String[] {});
+                               indexingOptions, new ArrayList<String>());
 
        }
 
index 61013a4..29ceac1 100644 (file)
@@ -5,6 +5,7 @@ import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.util.List;
 
 import eu.svjatoslav.commons.network.UrlParamEncoder;
 import eu.svjatoslav.meviz.htmlindexer.layouts.MixedLayout;
@@ -15,24 +16,29 @@ import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.Picture;
 
 public class WebIndexer extends AbstractIndexer {
 
-       private final String localPrefix;
        private final String globalPrefix;
 
-       public WebIndexer(final String globalPrefix, final String localPrefix) {
+       public WebIndexer(final String globalPrefix) {
                this.globalPrefix = globalPrefix;
-               this.localPrefix = localPrefix;
        }
 
        @Override
-       public String getDirectoryUrl(final AbstractFile directory) {
-               return "photos.jsp?path=/" + UrlParamEncoder.encode(directory.fileName);
+       public String getDirectoryUrl(final AbstractFile directory,
+                       final IndexingContext context) {
+               return "photos.jsp?path=" + context.getLocalUrl() + "/"
+                               + UrlParamEncoder.encode(directory.fileName);
        }
 
-       public String getHtml(final String requestPath)
-                       throws MalformedURLException, IOException, ClassNotFoundException {
+       public String getHtml(String requestPath) throws MalformedURLException,
+                       IOException, ClassNotFoundException {
+
+               if (requestPath.equals("/"))
+                       requestPath = "";
 
                final MixedLayout layout = new MixedLayout();
-               layout.init("Photos", new String[] {}, this, localPrefix + requestPath);
+               final IndexingContext context = new IndexingContext(globalPrefix,
+                               requestPath);
+               layout.init("Photos", context.getLocalPathComponents(), this, context);
 
                final DirectoryMetadata metadata = getMetadataForPath(requestPath);
 
@@ -47,11 +53,8 @@ public class WebIndexer extends AbstractIndexer {
                final StringBuffer urlStringBuffer = new StringBuffer();
 
                urlStringBuffer.append(globalPrefix);
-               urlStringBuffer.append(UrlParamEncoder.encode(localPrefix));
                urlStringBuffer.append(UrlParamEncoder.encode(requestPath));
-               if (!requestPath.equals("/"))
-                       urlStringBuffer.append("/");
-               urlStringBuffer.append(".thumbnails/metadata_6.dat");
+               urlStringBuffer.append("/.thumbnails/metadata_6.dat");
 
                final String urlString = urlStringBuffer.toString();
 
@@ -67,19 +70,38 @@ public class WebIndexer extends AbstractIndexer {
        }
 
        @Override
-       public String getThumbnailPath(final Picture picture,
-                       final Dimension desiredDimension) {
+       public String getParentDirectoryUrl(final IndexingContext context) {
+               final StringBuffer result = new StringBuffer();
+
+               result.append("photos.jsp?path=");
+
+               final List<String> components = context.getLocalPathComponents();
+
+               for (final String pathComponent : components.subList(0,
+                               components.size() - 1)) {
+                       result.append("/");
+                       result.append(pathComponent);
+               }
+
+               return result.toString();
+       }
+
+       @Override
+       public String getThumbnailUrl(final Picture picture,
+                       final Dimension desiredDimension, final IndexingContext context) {
 
                // in case thumbnail size was equal to original, then return original
                // file path
                if (picture.getDimensions().equals(desiredDimension))
-                       return localPrefix + picture.fileName;
+                       return context.getGlobalUrl() + context.getLocalUrl() + "/"
+                                       + picture.fileName;
 
                final String thumbnailFileName = picture
                                .getRelativeThumbnailFileName(desiredDimension);
 
-               return localPrefix + "/" + Constants.THUMBNAILS_DIRECTORY_NAME + "/"
-                               + thumbnailFileName;
+               return context.getGlobalUrl() + context.getLocalUrl() + "/"
+                               + Constants.THUMBNAILS_DIRECTORY_NAME + "/"
+                               + UrlParamEncoder.encode(thumbnailFileName);
        }
 
 }
index 345cd40..4c00985 100755 (executable)
@@ -18,6 +18,7 @@ import eu.svjatoslav.commons.file.FilePathParser;
 import eu.svjatoslav.commons.network.UrlParamEncoder;
 import eu.svjatoslav.meviz.htmlindexer.AbstractIndexer;
 import eu.svjatoslav.meviz.htmlindexer.Constants;
+import eu.svjatoslav.meviz.htmlindexer.IndexingContext;
 import eu.svjatoslav.meviz.htmlindexer.Layout;
 import eu.svjatoslav.meviz.htmlindexer.metadata.Dimension;
 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.AbstractFile;
@@ -34,7 +35,7 @@ public class MixedLayout implements Layout {
 
        String galleryTitle;
 
-       String[] path;
+       List<String> path;
 
        public static final String encoding = "UTF-8";
 
@@ -44,7 +45,7 @@ public class MixedLayout implements Layout {
 
        private AbstractIndexer indexer;
 
-       private String pathPrefix;
+       private IndexingContext indexingContext;
 
        public void columnAdded() {
                currentColumn++;
@@ -59,7 +60,8 @@ public class MixedLayout implements Layout {
        @Override
        public void enlistDirectory(final AbstractFile directory) {
                dirHtml.append("<TR>");
-               dirHtml.append("<TD><b><a href=\"" + indexer.getDirectoryUrl(directory)
+               dirHtml.append("<TD><b><a href=\""
+                               + indexer.getDirectoryUrl(directory, indexingContext)
                                + "\"> ==&gt; " + directory.fileName + "</a></b></TD>");
                dirHtml.append("</TR>\n");
 
@@ -73,9 +75,11 @@ public class MixedLayout implements Layout {
 
                filesHtml.append("<TR>\n");
                filesHtml.append("    <TD colspan=\""
-                               + String.valueOf(horizontalColumnsLimit) + "\"><a href=\""
-                               + UrlParamEncoder.encode(pathPrefix + fullFileName) + "\">"
-                               + fullFileName + "</a>\n");
+                               + String.valueOf(horizontalColumnsLimit)
+                               + "\"><a href=\""
+                               + indexingContext.getGlobalUrl()
+                               + UrlParamEncoder.encode(indexingContext.getLocalUrl()
+                                               + fullFileName) + "\">" + fullFileName + "</a>\n");
                filesHtml.append("&nbsp;&nbsp;("
                                + FilePathParser.getFileSizeDescription(file.getFileLength())
                                + ")</TD>\n");
@@ -100,17 +104,17 @@ public class MixedLayout implements Layout {
                                if (listingDimension.height > imageSize.height)
                                        listingDimension = imageSize;
 
-                       final String listingThumbnailPath = indexer.getThumbnailPath(
-                                       picture, listingDimension);
+                       final String listingThumbnailUrl = indexer.getThumbnailUrl(picture,
+                                       listingDimension, indexingContext);
 
                        startcolumn();
 
                        filesHtml.append("<TD><center><a href=\""
-                                       + UrlParamEncoder.encode(pathPrefix + picture.fileName)
-                                       + "\">\n");
+                                       + indexingContext.getGlobalUrl()
+                                       + UrlParamEncoder.encode(indexingContext.getLocalUrl()
+                                                       + "/" + picture.fileName) + "\">\n");
 
-                       filesHtml.append("<img border=\"0\" src=\""
-                                       + UrlParamEncoder.encode(listingThumbnailPath)
+                       filesHtml.append("<img border=\"0\" src=\"" + listingThumbnailUrl
                                        + "\"/></a>\n");
                        filesHtml.append("<br/><b>"
                                        + FilePathParser
@@ -123,8 +127,8 @@ public class MixedLayout implements Layout {
                                if (count > 1)
                                        filesHtml.append(", ");
                                filesHtml.append("<a href=\""
-                                               + UrlParamEncoder.encode(indexer.getThumbnailPath(
-                                                               picture, dimension)) + "\">");
+                                               + indexer.getThumbnailUrl(picture, dimension,
+                                                               indexingContext) + "\">");
                                filesHtml.append(dimension.width + "x" + dimension.height);
                                filesHtml.append("</a>");
                        }
@@ -160,17 +164,18 @@ public class MixedLayout implements Layout {
                filesHtml.append("<video controls=\"controls\" tabindex=\"0\">\n");
                filesHtml
                                .append("<source type=\"video/ogg\" codecs=\"theora, vorbis\" src=\""
-                                               + UrlParamEncoder.encode(pathPrefix + file.fileName)
-                                               + "\">\n");
+                                               + indexingContext.getGlobalUrl()
+                                               + UrlParamEncoder.encode(indexingContext.getLocalUrl()
+                                                               + file.fileName) + "\">\n");
                filesHtml.append("</source>\n");
 
                // if browser cannot play video directry, fall back to java Applet
                filesHtml
                                .append("<applet code=\"com.fluendo.player.Cortado.class\" archive=\"http://theora.org/cortado.jar\" width=\"800\" height=\"600\">\n");
-               filesHtml
-                               .append("    <param name=\"url\" value=\""
-                                               + UrlParamEncoder.encode(pathPrefix + file.fileName)
-                                               + "\"/>\n");
+               filesHtml.append("    <param name=\"url\" value=\""
+                               + indexingContext.getGlobalUrl()
+                               + UrlParamEncoder.encode(indexingContext.getLocalUrl()
+                                               + file.fileName) + "\"/>\n");
                filesHtml.append("    <param name=\"keepAspect\" value=\"true\"/>\n");
                filesHtml.append("    <param name=\"autoPlay\" value=\"false\"/>\n");
                filesHtml.append("</applet>\n");
@@ -185,8 +190,9 @@ public class MixedLayout implements Layout {
                                                                .getFileNameWithoutExtension(file.fileName)
                                                + "<b/>  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
                filesHtml.append("<a href=\""
-                               + UrlParamEncoder.encode(pathPrefix + file.fileName)
-                               + "\">download as OGV</a>\n");
+                               + indexingContext.getGlobalUrl()
+                               + UrlParamEncoder.encode(indexingContext.getLocalUrl()
+                                               + file.fileName) + "\">download as OGV</a>\n");
 
                filesHtml.append("</TD></TR>\n");
 
@@ -239,10 +245,11 @@ public class MixedLayout implements Layout {
                // append directory listing
                finalHtml.append("<TABLE>\n");
 
-               if (path.length > 0) {
+               if (path.size() > 0) {
                        finalHtml.append("<TR>");
-                       finalHtml
-                                       .append("<TD><b><a href=\"../index.html\"> &lt;== ..</a></b></TD>");
+                       finalHtml.append("<TD><b><a href=\""
+                                       + indexer.getParentDirectoryUrl(indexingContext)
+                                       + "\"> &lt;== ..</a></b></TD>");
                        finalHtml.append("</TR>\n");
                }
 
@@ -263,12 +270,13 @@ public class MixedLayout implements Layout {
        }
 
        @Override
-       public void init(final String galleryTitle, final String[] path,
-                       final AbstractIndexer indexer, final String pathPrefix) {
+       public void init(final String galleryTitle, final List<String> path,
+                       final AbstractIndexer indexer, final IndexingContext indexingContext) {
+
                this.indexer = indexer;
                this.galleryTitle = galleryTitle;
                this.path = path;
-               this.pathPrefix = pathPrefix;
+               this.indexingContext = indexingContext;
 
                dirHtml = new StringBuffer();
                filesHtml = new StringBuffer();