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;
13 import java.io.IOException;
14 import java.io.Serializable;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.HashSet;
20 import java.util.Iterator;
22 import eu.svjatoslav.commons.file.FilePathParser;
23 import eu.svjatoslav.meviz.htmlindexer.AbstractIndexer;
24 import eu.svjatoslav.meviz.htmlindexer.FileSortComparator;
25 import eu.svjatoslav.meviz.htmlindexer.ImageFormatError;
26 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.AbstractFile;
27 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.DirectoryFile;
28 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.GeneralFile;
29 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.MevizSpecialFile;
30 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.Picture;
33 * Corresponds to single filesystem directory.
35 public class DirectoryMetadata implements Serializable {
37 private static final long serialVersionUID = 3526489102725366334L;
39 private final HashMap<String, AbstractFile> fileNameToFileMap = new HashMap<String, AbstractFile>();
41 public transient boolean changed;
44 * Performs cleanup of directory model. In particular removes metadata for
45 * images that no longer exist in the filesystem.
47 public void cleanup() {
48 final Iterator<AbstractFile> fileIterator = fileNameToFileMap.values()
51 while (fileIterator.hasNext()) {
52 final AbstractFile file = fileIterator.next();
54 if (!file.isMetaInfoVerified())
55 fileIterator.remove();
59 public void ensureFileMetainfoIsUpToDate(final File parentDirectory,
60 final File file) throws Exception {
62 final String fileName = file.getName();
64 if (fileNameToFileMap.containsKey(fileName)) {
65 final boolean metainfoUpdated = fileNameToFileMap.get(fileName)
66 .ensureFileMetainfoIsUpToDate(parentDirectory);
71 registerFile(parentDirectory, file);
75 private AbstractFile getAbstractFile(final File parentDirectory,
76 final File file, final String fileName) throws Exception {
78 if (file.isDirectory())
79 return new DirectoryFile(parentDirectory, fileName);
81 final String fileExtension = FilePathParser.getFileExtension(fileName)
84 if (AbstractIndexer.isImage(fileExtension))
86 return new Picture(parentDirectory, fileName);
87 } catch (final Exception exception) {
88 // in case image decoding failed, handle image as general file
89 return new GeneralFile(parentDirectory, fileName);
92 if (fileName.endsWith(".meviz.org"))
93 return new MevizSpecialFile(parentDirectory, fileName);
95 if (fileName.endsWith(".meviz.html"))
96 return new MevizSpecialFile(parentDirectory, fileName);
98 return new GeneralFile(parentDirectory, fileName);
101 public AbstractFile getFile(final String fileName) throws IOException {
102 return fileNameToFileMap.get(fileName);
105 public Collection<AbstractFile> getFiles() {
107 final ArrayList<AbstractFile> files = new ArrayList<AbstractFile>(
108 fileNameToFileMap.values());
110 Collections.sort(files, new FileSortComparator());
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);