/*
* Copyright (C) 2010 Alexander Kolosov
*
* This file is part of FlowBrook.
*
* FlowBrook is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* FlowBrook is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with FlowBrook. If not, see <http://www.gnu.org/licenses/>
*/
package ru.petrsu.akolosov.flowbrook.flowtools;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Deque;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import ru.petrsu.akolosov.flowbrook.FlowSource;
import ru.petrsu.akolosov.flowbrook.FlowSourceSet;
/**
*
* @author Alexander Kolosov
*/
public final class FlowTools {
public static FlowSourceSet getSourceFromDir(File startDir, boolean recursive) {
Logger logger = Logger.getLogger(FlowToolsFile.class.getName());
if (!startDir.isDirectory()) {
throw new IllegalArgumentException("Argument must be a directory.");
}
logger.log(Level.INFO, "Loading flow-tools files from " + startDir.getAbsolutePath());
FlowSourceSet ftDir = new FlowSourceSet();
Deque<File> dirStack = new LinkedList<File>();
dirStack.add(startDir);
int i = 0;
do {
File curDir = dirStack.removeFirst();
logger.log(Level.FINER, "Entering directory " + curDir.getAbsolutePath());
for (File f : curDir.listFiles()) {
if (f.isDirectory() && recursive) {
dirStack.addFirst(f);
continue;
} else if (!f.isFile()) {
logger.log(Level.INFO, "Skipping " + f.getAbsolutePath() + ": not a file.");
continue;
}
try {
ftDir.addFlowSource(getSourceFromFile(f));
i++;
logger.log(Level.FINE, "File " + f.getAbsolutePath() + " loaded.");
} catch (FileNotFoundException ex) {
logger.log(Level.SEVERE, null, ex);
} catch (IOException ex) {
logger.log(Level.WARNING, "Problems with " + f + ": " + ex);
} catch (FlowToolsException ex) {
logger.log(Level.WARNING, "Problems with " + f + ": " + ex);
}
}
logger.log(Level.FINER, "Finished traversing directory " + curDir.getAbsolutePath());
} while (!dirStack.isEmpty());
logger.log(Level.INFO, i + " flow files succesfully loaded.");
return ftDir;
}
public static FlowSource getSourceFromFile(File f) throws IOException, FlowToolsException {
return new FlowToolsSource(new FlowToolsFile(f));
}
public static FlowSource getSourceFromStream(BufferedInputStream bis) throws IOException, FlowToolsException {
return new FlowToolsSource(new FlowToolsStream(bis));
}
}