1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-07 02:40:52 +00:00
plantuml/src/net/sourceforge/plantuml/DirWatcher.java

136 lines
3.9 KiB
Java
Raw Normal View History

2010-11-15 20:35:36 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2022-03-07 19:33:46 +00:00
* (C) Copyright 2009-2023, Arnaud Roques
2010-11-15 20:35:36 +00:00
*
2016-03-06 16:47:34 +00:00
* Project Info: http://plantuml.com
2010-11-15 20:35:36 +00:00
*
2017-03-15 19:13:31 +00:00
* If you like this project or if you find it useful, you can support us at:
*
* http://plantuml.com/patreon (only 1$ per month!)
* http://plantuml.com/paypal
*
2010-11-15 20:35:36 +00:00
* This file is part of PlantUML.
*
* PlantUML is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PlantUML distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
2013-12-10 19:36:50 +00:00
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
2010-11-15 20:35:36 +00:00
* License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
*
* Original Author: Arnaud Roques
*
*
*/
package net.sourceforge.plantuml;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
2011-01-29 15:09:35 +00:00
import java.util.Set;
2010-11-15 20:35:36 +00:00
import net.sourceforge.plantuml.preproc.Defines;
2015-09-28 20:42:17 +00:00
import net.sourceforge.plantuml.preproc.FileWithSuffix;
2010-11-15 20:35:36 +00:00
2013-12-10 19:36:50 +00:00
@Deprecated
2010-11-15 20:35:36 +00:00
public class DirWatcher {
// ::remove file when WASM
2010-11-15 20:35:36 +00:00
final private File dir;
final private Option option;
final private String pattern;
2011-01-29 15:09:35 +00:00
final private Map<File, FileWatcher> modifieds = new HashMap<File, FileWatcher>();
2010-11-15 20:35:36 +00:00
public DirWatcher(File dir, Option option, String pattern) {
this.dir = dir;
this.option = option;
this.pattern = pattern;
}
public List<GeneratedImage> buildCreatedFiles() throws IOException, InterruptedException {
2011-08-08 17:48:29 +00:00
boolean error = false;
2021-05-14 08:42:57 +00:00
final List<GeneratedImage> result = new ArrayList<>();
if (dir.listFiles() != null)
for (File f : dir.listFiles()) {
if (error)
continue;
if (f.isFile() == false)
continue;
if (fileToProcess(f.getName()) == false)
continue;
final FileWatcher watcher = modifieds.get(f);
if (watcher == null || watcher.hasChanged()) {
final SourceFileReader sourceFileReader = new SourceFileReader(Defines.createWithFileName(f), f,
option.getOutputDir(), option.getConfig(), option.getCharset(),
option.getFileFormatOption());
final Set<File> files = FileWithSuffix.convert(sourceFileReader.getIncludedFiles());
files.add(f);
for (GeneratedImage g : sourceFileReader.getGeneratedImages()) {
result.add(g);
if (option.isFailfastOrFailfast2() && g.lineErrorRaw() != -1) {
error = true;
}
2011-08-08 17:48:29 +00:00
}
modifieds.put(f, new FileWatcher(files));
2010-11-15 20:35:36 +00:00
}
}
2011-08-08 17:48:29 +00:00
Collections.sort(result);
return Collections.unmodifiableList(result);
}
public File getErrorFile() throws IOException, InterruptedException {
if (dir.listFiles() != null)
for (File f : dir.listFiles()) {
if (f.isFile() == false)
continue;
if (fileToProcess(f.getName()) == false)
continue;
final FileWatcher watcher = modifieds.get(f);
if (watcher == null || watcher.hasChanged()) {
final SourceFileReader sourceFileReader = new SourceFileReader(Defines.createWithFileName(f), f,
option.getOutputDir(), option.getConfig(), option.getCharset(),
option.getFileFormatOption());
if (sourceFileReader.hasError())
return f;
2011-08-08 17:48:29 +00:00
}
}
return null;
2010-11-15 20:35:36 +00:00
}
private boolean fileToProcess(String name) {
return name.matches(pattern);
}
public final File getDir() {
return dir;
}
// public void setPattern(String pattern) {
// this.pattern = pattern;
// }
}