package eu.lsem.bakalarka.service;
import eu.lsem.bakalarka.filetypeprocess.archives.ArchiveDecompressor;
import eu.lsem.bakalarka.filetypeprocess.document.DocumentParser;
import eu.lsem.bakalarka.filetypeprocess.document.ParsingException;
import org.apache.commons.configuration.Configuration;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ApplicationConfigurationImpl implements ApplicationConfiguration {
private Configuration configuration;
public void setConfiguration(Configuration configuration) { //injected by spring
this.configuration = configuration;
}
/**
* Vraci podporovane archivy (ve tvaru tar.gz, rar apod)
*
* @return
*/
@Override
public List<String> getSupportedArchives() {
Iterator keys = configuration.getKeys();
List<String> archives = new ArrayList<String>();
while (keys.hasNext()) {
String key = (String) keys.next();
if (!key.startsWith("archive."))
continue;
archives.add(key.substring(8));
}
return archives;
}
@Override
public String getSiteName() {
return configuration.getString("site.name");
}
@Override
public List<String> getSupportedDocuments() {
Iterator keys = configuration.getKeys();
List<String> documents = new ArrayList<String>();
while (keys.hasNext()) {
String key = (String) keys.next();
if (!key.startsWith("document.parser."))
continue;
documents.add(key.substring(16));
}
return documents;
}
@Override
public String getDipstoreDir() {
return configuration.getString("dipstore.dir");
}
@Override
public String getSystemWideSalt() {
return configuration.getString("salt.systemwide");
}
@Override
@SuppressWarnings("unchecked")
public DocumentParser getDocumentParser(File f) {
for (String s : getSupportedDocuments()) {
if (f.getName().endsWith(s)) {
try {
return (DocumentParser) Class.forName(getDocumentParserClassName(s)).newInstance();
} catch (Exception e) {
return null;
}
}
}
return null;
}
@Override
@SuppressWarnings("unchecked")
public ArchiveDecompressor getArchiveDecompressor(File file) {
for (String s : getSupportedArchives()) {
if (file.getName().endsWith(s)) {
try {
return (ArchiveDecompressor) Class.forName(getArchiveDecompressorClassName(s)).newInstance();
} catch (Exception e) {
return null;
}
}
}
return null;
}
/**
* Vrati nazev tridy, ktera rozbaluje archiv.
*
* @param extension Ve tvaru "rar", nebo "tar.bz2" apod. Cili bez tecky pred priponou.
* @return
* @throws IOException Pokud neni archiv podporovan
*/
private String getArchiveDecompressorClassName(String extension) throws IOException {
String className = configuration.getString("archive." + extension, null);
if (className == null)
throw new IOException("Archive not supported.");
else
return className;
}
private String getDocumentParserClassName(String extension) throws ParsingException {
String className = configuration.getString("document.parser." + extension, null);
if (className == null)
throw new ParsingException("Document type not supported.");
else
return className;
}
}