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.Picture;
32 * Corresponds to single filesystem directory.
34 public class DirectoryMetadata implements Serializable {
36 private static final long serialVersionUID = 3526489102725366334L;
38 private final HashMap<String, AbstractFile> fileNameToFileMap = new HashMap<String, AbstractFile>();
40 public transient boolean changed;
43 * Performs cleanup of directory model. In particular removes metadata for
44 * images that no longer exist in the filesystem.
46 public void cleanup() {
47 final Iterator<AbstractFile> fileIterator = fileNameToFileMap.values()
50 while (fileIterator.hasNext()) {
51 final AbstractFile file = fileIterator.next();
53 if (!file.isMetaInfoVerified())
54 fileIterator.remove();
58 public void ensureFileMetainfoIsUpToDate(final File parentDirectory,
59 final File file) throws IOException, ImageFormatError {
61 final String fileName = file.getName();
63 if (fileNameToFileMap.containsKey(fileName)) {
64 final boolean metainfoUpdated = fileNameToFileMap.get(fileName)
65 .ensureFileMetainfoIsUpToDate(parentDirectory);
70 registerFile(parentDirectory, file);
74 public AbstractFile getFile(final String fileName) throws IOException {
75 return fileNameToFileMap.get(fileName);
78 public Collection<AbstractFile> getFiles() {
80 final ArrayList<AbstractFile> files = new ArrayList<AbstractFile>(
81 fileNameToFileMap.values());
83 Collections.sort(files, new FileSortComparator());
88 public Dimension getImageDimensions(final String fileName)
89 throws IOException, ImageFormatError {
91 return ((Picture) getFile(fileName)).getDimensions();
94 public AbstractFile registerFile(final File parentDirectory, final File file)
95 throws IOException, ImageFormatError {
97 final String fileName = file.getName();
99 final AbstractFile abstractFile;
101 if (file.isDirectory())
102 abstractFile = new DirectoryFile(parentDirectory, fileName);
104 final String fileExtension = FilePathParser.getFileExtension(
105 fileName).toLowerCase();
107 if (AbstractIndexer.isImage(fileExtension))
108 abstractFile = new Picture(parentDirectory, fileName);
110 abstractFile = new GeneralFile(parentDirectory, fileName);
113 fileNameToFileMap.put(fileName, abstractFile);
120 public void removeUnusedMetadataEntries() {
122 for (final String key : new HashSet<String>(fileNameToFileMap.keySet()))
123 if (!fileNameToFileMap.get(key).isMetaInfoVerified()) {
124 fileNameToFileMap.remove(key);