2 * Meviz - Various tools collection to work with multimedia.
3 * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
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.
10 package eu.svjatoslav.meviz.htmlindexer.metadata;
12 import eu.svjatoslav.commons.file.FilePathParser;
13 import eu.svjatoslav.meviz.htmlindexer.AbstractIndexer;
14 import eu.svjatoslav.meviz.htmlindexer.FileSortComparator;
15 import eu.svjatoslav.meviz.htmlindexer.ImageFormatError;
16 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.*;
19 import java.io.IOException;
20 import java.io.Serializable;
24 * Corresponds to single filesystem directory.
26 public class DirectoryMetadata implements Serializable {
28 private static final long serialVersionUID = 3526489102725366334L;
30 private final HashMap<String, AbstractFile> fileNameToFileMap = new HashMap<String, AbstractFile>();
32 public transient boolean changed;
35 * Performs cleanup of directory model. In particular removes metadata for
36 * images that no longer exist in the filesystem.
38 public void cleanup() {
39 final Iterator<AbstractFile> fileIterator = fileNameToFileMap.values()
42 while (fileIterator.hasNext()) {
43 final AbstractFile file = fileIterator.next();
45 if (!file.isMetaInfoVerified())
46 fileIterator.remove();
50 public void ensureFileMetainfoIsUpToDate(final File parentDirectory,
51 final File file) throws Exception {
53 final String fileName = file.getName();
55 if (fileNameToFileMap.containsKey(fileName)) {
56 final boolean metainfoUpdated = fileNameToFileMap.get(fileName)
57 .ensureFileMetainfoIsUpToDate(parentDirectory);
62 registerFile(parentDirectory, file);
66 private AbstractFile getAbstractFile(final File parentDirectory,
67 final File file, final String fileName) throws Exception {
69 if (file.isDirectory())
70 return new DirectoryFile(parentDirectory, fileName);
72 final String fileExtension = FilePathParser.getFileExtension(fileName)
75 if (AbstractIndexer.isImage(fileExtension))
77 return new Picture(parentDirectory, fileName);
78 } catch (final Exception exception) {
79 // in case image decoding failed, handle image as general file
80 return new GeneralFile(parentDirectory, fileName);
83 if (fileName.endsWith(".meviz.org"))
84 return new MevizSpecialFile(parentDirectory, fileName);
86 if (fileName.endsWith(".meviz.html"))
87 return new MevizSpecialFile(parentDirectory, fileName);
89 return new GeneralFile(parentDirectory, fileName);
92 public AbstractFile getFile(final String fileName) {
93 return fileNameToFileMap.get(fileName);
96 public Collection<AbstractFile> getFiles() {
98 final ArrayList<AbstractFile> files = new ArrayList<AbstractFile>(
99 fileNameToFileMap.values());
101 Collections.sort(files, new FileSortComparator());
106 public String getHeaderMevizMetadataHtml() {
107 final AbstractFile headerFile = getFile("header.meviz.html");
108 if (headerFile == null)
111 final MevizSpecialFile mevizSpecialFile = (MevizSpecialFile) headerFile;
112 return mevizSpecialFile.getFileContent();
115 public Dimension getImageDimensions(final String fileName)
116 throws IOException, ImageFormatError {
118 return ((Picture) getFile(fileName)).getDimensions();
121 public AbstractFile registerFile(final File parentDirectory, final File file)
124 final String fileName = file.getName();
126 final AbstractFile abstractFile = getAbstractFile(parentDirectory,
129 fileNameToFileMap.put(fileName, abstractFile);
136 public void removeUnusedMetadataEntries() {
138 for (final String key : new HashSet<String>(fileNameToFileMap.keySet()))
139 if (!fileNameToFileMap.get(key).isMetaInfoVerified()) {
140 fileNameToFileMap.remove(key);