From f826af1cdb62425a975cd174ecd0905100d3a2dc Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Sun, 3 Feb 2013 12:09:30 +0200 Subject: [PATCH] possilibity to generate HTML while embedded on the web site --- .../meviz/htmlindexer/AbstractIndexer.java | 9 ++- .../meviz/htmlindexer/FilesystemIndexer.java | 46 +++++++------ .../meviz/htmlindexer/IndexingContext.java | 37 +++++++++++ .../svjatoslav/meviz/htmlindexer/Layout.java | 5 +- .../eu/svjatoslav/meviz/htmlindexer/Main.java | 4 +- .../meviz/htmlindexer/WebIndexer.java | 56 +++++++++++----- .../htmlindexer/layouts/MixedLayout.java | 64 +++++++++++-------- 7 files changed, 152 insertions(+), 69 deletions(-) create mode 100644 src/main/java/eu/svjatoslav/meviz/htmlindexer/IndexingContext.java diff --git a/src/main/java/eu/svjatoslav/meviz/htmlindexer/AbstractIndexer.java b/src/main/java/eu/svjatoslav/meviz/htmlindexer/AbstractIndexer.java index 4b1e29c..762fcad 100644 --- a/src/main/java/eu/svjatoslav/meviz/htmlindexer/AbstractIndexer.java +++ b/src/main/java/eu/svjatoslav/meviz/htmlindexer/AbstractIndexer.java @@ -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) diff --git a/src/main/java/eu/svjatoslav/meviz/htmlindexer/FilesystemIndexer.java b/src/main/java/eu/svjatoslav/meviz/htmlindexer/FilesystemIndexer.java index ea6489e..3a48b67 100755 --- a/src/main/java/eu/svjatoslav/meviz/htmlindexer/FilesystemIndexer.java +++ b/src/main/java/eu/svjatoslav/meviz/htmlindexer/FilesystemIndexer.java @@ -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 pathElements; private HashSet layouts; public FilesystemIndexer(final File directoryToIndex, - final IndexingOptions indexingOptions, final String[] pathElements) - throws IOException, ImageFormatError { + final IndexingOptions indexingOptions, + final List 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 getChildPath(final File file) { - public String[] getSubPath(final File file) { + final List result = new ArrayList(); + 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 index 0000000..48c3be2 --- /dev/null +++ b/src/main/java/eu/svjatoslav/meviz/htmlindexer/IndexingContext.java @@ -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 getLocalPathComponents() { + + final List result = new ArrayList(); + + final String[] strings = localUrl.split("/"); + + for (final String pathElement : strings) + if (pathElement.length() > 0) + result.add(pathElement); + + return result; + } + + public String getLocalUrl() { + return localUrl; + } + +} diff --git a/src/main/java/eu/svjatoslav/meviz/htmlindexer/Layout.java b/src/main/java/eu/svjatoslav/meviz/htmlindexer/Layout.java index 2dac2ff..88de6ce 100755 --- a/src/main/java/eu/svjatoslav/meviz/htmlindexer/Layout.java +++ b/src/main/java/eu/svjatoslav/meviz/htmlindexer/Layout.java @@ -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 path, + final AbstractIndexer indexer, IndexingContext context); } diff --git a/src/main/java/eu/svjatoslav/meviz/htmlindexer/Main.java b/src/main/java/eu/svjatoslav/meviz/htmlindexer/Main.java index 94a2253..335b073 100644 --- a/src/main/java/eu/svjatoslav/meviz/htmlindexer/Main.java +++ b/src/main/java/eu/svjatoslav/meviz/htmlindexer/Main.java @@ -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()); } diff --git a/src/main/java/eu/svjatoslav/meviz/htmlindexer/WebIndexer.java b/src/main/java/eu/svjatoslav/meviz/htmlindexer/WebIndexer.java index 61013a4..29ceac1 100644 --- a/src/main/java/eu/svjatoslav/meviz/htmlindexer/WebIndexer.java +++ b/src/main/java/eu/svjatoslav/meviz/htmlindexer/WebIndexer.java @@ -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 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); } } diff --git a/src/main/java/eu/svjatoslav/meviz/htmlindexer/layouts/MixedLayout.java b/src/main/java/eu/svjatoslav/meviz/htmlindexer/layouts/MixedLayout.java index 345cd40..4c00985 100755 --- a/src/main/java/eu/svjatoslav/meviz/htmlindexer/layouts/MixedLayout.java +++ b/src/main/java/eu/svjatoslav/meviz/htmlindexer/layouts/MixedLayout.java @@ -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 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(""); - dirHtml.append(" ==> " + directory.fileName + ""); dirHtml.append("\n"); @@ -73,9 +75,11 @@ public class MixedLayout implements Layout { filesHtml.append("\n"); filesHtml.append(" " - + fullFileName + "\n"); + + String.valueOf(horizontalColumnsLimit) + + "\">" + fullFileName + "\n"); filesHtml.append("  (" + FilePathParser.getFileSizeDescription(file.getFileLength()) + ")\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("
\n"); + + indexingContext.getGlobalUrl() + + UrlParamEncoder.encode(indexingContext.getLocalUrl() + + "/" + picture.fileName) + "\">\n"); - filesHtml.append("\n"); filesHtml.append("
" + FilePathParser @@ -123,8 +127,8 @@ public class MixedLayout implements Layout { if (count > 1) filesHtml.append(", "); filesHtml.append(""); + + indexer.getThumbnailUrl(picture, dimension, + indexingContext) + "\">"); filesHtml.append(dimension.width + "x" + dimension.height); filesHtml.append(""); } @@ -160,17 +164,18 @@ public class MixedLayout implements Layout { filesHtml.append("