/******************************************************************************* * Copyright 2013 * Ubiquitous Knowledge Processing (UKP) Lab * Technische Universität Darmstadt * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. ******************************************************************************/ package de.tudarmstadt.ukp.csniper.webapp.evaluation; import java.io.BufferedReader; import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.Serializable; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Properties; import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.annotation.Required; import de.tudarmstadt.ukp.csniper.webapp.evaluation.model.Corpus; import de.tudarmstadt.ukp.csniper.webapp.search.CorpusService; import de.tudarmstadt.ukp.csniper.webapp.search.SearchEngine; public class CorpusServiceImpl implements CorpusService, Serializable { private static final long serialVersionUID = 5380779638911392541L; private final Log log = LogFactory.getLog(getClass()); private File repositoryPath; private String corpusInfoFile; private List<SearchEngine> engines; @Required public void setEngines(List<SearchEngine> aEngines) { engines = aEngines; } @Required public void setCorpusInfoFile(String aFilename) { corpusInfoFile = aFilename; } @Required public void setRepositoryPath(File aFile) { repositoryPath = aFile; } @Override public File getRepositoryPath() { return repositoryPath; } @Override public List<String> listCorpora() { List<String> corpora = new ArrayList<String>(); for (File f : getCorporaDirs()) { corpora.add(f.getName().toUpperCase()); } return corpora; } @Override public Corpus getCorpus(String aCorpusId) { BufferedReader br = null; Corpus c = new Corpus(); c.setId(aCorpusId); try { File corpusPath = new File(repositoryPath, aCorpusId); Properties p = new Properties(); br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(corpusPath, corpusInfoFile)), "UTF-8")); p.load(br); c.setName(p.getProperty("name")); c.setDescription(p.getProperty("description")); c.setLanguage(p.getProperty("language")); } catch (IOException e) { if (log.isErrorEnabled()) { log.error(e); } } finally { IOUtils.closeQuietly(br); } return c; } @Override public List<SearchEngine> listEngines(String aCorpusId) { File corpusPath = new File(repositoryPath, aCorpusId); List<SearchEngine> filteredEngines = new ArrayList<SearchEngine>(); for (SearchEngine engine : engines) { if (new File(corpusPath, engine.getName()).exists()) { filteredEngines.add(engine); } } return filteredEngines; } private List<File> getCorporaDirs() { File[] dirs = repositoryPath.listFiles(new FileFilter() { @Override public boolean accept(File aPath) { return new File(aPath, corpusInfoFile).exists(); } }); return (dirs == null) ? new ArrayList<File>() : Arrays.asList(dirs); } }