1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-11 04:32:26 +00:00
plantuml/src/net/sourceforge/plantuml/SourceFileReader.java

276 lines
9.0 KiB
Java
Raw Normal View History

2010-11-15 20:35:36 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2016-01-09 12:15:40 +00:00
* (C) Copyright 2009-2017, 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;
2015-08-05 20:17:01 +00:00
import java.io.BufferedOutputStream;
2010-11-15 20:35:36 +00:00
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
2013-12-10 19:36:50 +00:00
import java.io.FileOutputStream;
2010-11-15 20:35:36 +00:00
import java.io.IOException;
import java.io.InputStreamReader;
2015-08-05 20:17:01 +00:00
import java.io.OutputStream;
2013-12-10 19:36:50 +00:00
import java.io.PrintStream;
2010-11-15 20:35:36 +00:00
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
2011-01-29 15:09:35 +00:00
import java.util.Set;
2010-11-15 20:35:36 +00:00
import net.sourceforge.plantuml.code.Transcoder;
2011-01-05 18:23:06 +00:00
import net.sourceforge.plantuml.code.TranscoderUtil;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.core.Diagram;
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
2011-08-08 17:48:29 +00:00
public class SourceFileReader implements ISourceFileReader {
2010-11-15 20:35:36 +00:00
private final File file;
private final File outputDirectory;
private final BlockUmlBuilder builder;
2011-01-05 18:23:06 +00:00
private FileFormatOption fileFormatOption;
2010-11-15 20:35:36 +00:00
public SourceFileReader(File file) throws IOException {
this(file, file.getAbsoluteFile().getParentFile());
}
2013-12-10 19:36:50 +00:00
public SourceFileReader(File file, File outputDirectory, String charset) throws IOException {
2017-04-05 17:37:42 +00:00
this(Defines.createWithFileName(file), file, outputDirectory, Collections.<String> emptyList(), charset,
new FileFormatOption(FileFormat.PNG));
2013-12-10 19:36:50 +00:00
}
2010-11-15 20:35:36 +00:00
public SourceFileReader(final File file, File outputDirectory) throws IOException {
2017-04-05 17:37:42 +00:00
this(Defines.createWithFileName(file), file, outputDirectory, Collections.<String> emptyList(), null,
new FileFormatOption(FileFormat.PNG));
2010-11-15 20:35:36 +00:00
}
2011-08-08 17:48:29 +00:00
public SourceFileReader(final File file, File outputDirectory, FileFormatOption fileFormatOption)
throws IOException {
2017-04-05 17:37:42 +00:00
this(Defines.createWithFileName(file), file, outputDirectory, Collections.<String> emptyList(), null,
fileFormatOption);
2010-11-15 20:35:36 +00:00
}
public SourceFileReader(Defines defines, final File file, File outputDirectory, List<String> config,
2011-01-05 18:23:06 +00:00
String charset, FileFormatOption fileFormatOption) throws IOException {
2010-11-15 20:35:36 +00:00
this.file = file;
2011-01-05 18:23:06 +00:00
this.fileFormatOption = fileFormatOption;
2010-11-15 20:35:36 +00:00
if (file.exists() == false) {
throw new IllegalArgumentException();
}
FileSystem.getInstance().setCurrentDir(file.getAbsoluteFile().getParentFile());
if (outputDirectory == null) {
outputDirectory = file.getAbsoluteFile().getParentFile();
} else if (outputDirectory.isAbsolute() == false) {
2013-12-10 19:36:50 +00:00
outputDirectory = FileSystem.getInstance().getFile(outputDirectory.getPath());
2010-11-15 20:35:36 +00:00
}
if (outputDirectory.exists() == false) {
outputDirectory.mkdirs();
}
this.outputDirectory = outputDirectory;
2011-08-08 17:48:29 +00:00
2013-12-10 19:36:50 +00:00
builder = new BlockUmlBuilder(config, charset, defines, getReader(charset), file.getAbsoluteFile()
2015-06-20 10:54:49 +00:00
.getParentFile(), file.getAbsolutePath());
2011-08-08 17:48:29 +00:00
}
2015-04-07 18:18:37 +00:00
public boolean hasError() {
2011-08-08 17:48:29 +00:00
for (final BlockUml b : builder.getBlockUmls()) {
2013-12-10 19:36:50 +00:00
if (b.getDiagram() instanceof PSystemError) {
2011-08-08 17:48:29 +00:00
return true;
}
}
return false;
2010-11-15 20:35:36 +00:00
}
2015-08-05 20:17:01 +00:00
private File getDirIfDirectory(String newName) {
Log.info("Checking=" + newName);
if (endsWithSlashOrAntislash(newName)) {
Log.info("It ends with / so it looks like a directory");
newName = newName.substring(0, newName.length() - 1);
File f = new File(newName);
Log.info("f=" + f);
if (f.isAbsolute() == false) {
Log.info("It's relative, so let's change it");
f = new File(outputDirectory, newName);
Log.info("f=" + f);
}
if (f.exists() == false) {
Log.info("It does not exist: let's create it");
try {
f.mkdirs();
} catch (Exception e) {
Log.info("Error " + e);
}
if (f.exists() && f.isDirectory()) {
Log.info("Creation ok");
return f;
}
Log.info("We cannot create it");
} else if (f.isDirectory() == false) {
Log.info("It exists, but is not a directory: we ignore it");
return null;
}
return f;
}
File f = new File(newName);
Log.info("f=" + f);
if (f.isAbsolute() == false) {
Log.info("Relative, so let's change it");
f = new File(outputDirectory, newName);
Log.info("f=" + f);
}
if (f.exists() && f.isDirectory()) {
Log.info("It's an existing directory");
return f;
}
Log.info("It's not a directory");
return null;
}
2013-12-10 19:36:50 +00:00
public List<GeneratedImage> getGeneratedImages() throws IOException {
2010-11-15 20:35:36 +00:00
Log.info("Reading file: " + file);
int cpt = 0;
final List<GeneratedImage> result = new ArrayList<GeneratedImage>();
for (BlockUml blockUml : builder.getBlockUmls()) {
2017-02-15 21:34:36 +00:00
String newName = blockUml.getFileOrDirname(fileFormatOption.getFileFormat());
2015-08-05 20:17:01 +00:00
Log.info("name from block=" + newName);
File suggested = null;
if (newName != null) {
final File dir = getDirIfDirectory(newName);
if (dir == null) {
Log.info(newName + " is not taken as a directory");
suggested = new File(outputDirectory, newName);
} else {
Log.info("We are going to create files in directory " + dir);
newName = fileFormatOption.getFileFormat().changeName(file.getName(), cpt++);
suggested = new File(dir, newName);
}
Log.info("We are going to put data in " + suggested);
}
if (suggested == null) {
2011-08-08 17:48:29 +00:00
newName = fileFormatOption.getFileFormat().changeName(file.getName(), cpt++);
2015-08-05 20:17:01 +00:00
suggested = new File(outputDirectory, newName);
2010-11-15 20:35:36 +00:00
}
suggested.getParentFile().mkdirs();
2015-08-05 20:17:01 +00:00
final Diagram system;
try {
system = blockUml.getDiagram();
} catch (Throwable t) {
2015-09-28 20:42:17 +00:00
final GeneratedImage image = new GeneratedImageImpl(suggested, "Crash Error", blockUml);
2015-08-05 20:17:01 +00:00
OutputStream os = null;
try {
os = new BufferedOutputStream(new FileOutputStream(suggested));
2016-03-06 16:47:34 +00:00
UmlDiagram.exportDiagramError(os, t, fileFormatOption, null, blockUml.getFlashData(),
2015-08-05 20:17:01 +00:00
UmlDiagram.getFailureText2(t));
} finally {
if (os != null) {
os.close();
}
}
return Collections.singletonList(image);
}
2017-03-12 17:22:02 +00:00
final List<FileImageData> exportDiagrams = PSystemUtils.exportDiagrams(system, suggested, fileFormatOption);
2017-04-05 17:37:42 +00:00
if (exportDiagrams.size() > 1) {
cpt += exportDiagrams.size() - 1;
}
2011-08-08 17:48:29 +00:00
OptionFlags.getInstance().logData(file, system);
2017-03-12 17:22:02 +00:00
for (FileImageData fdata : exportDiagrams) {
2011-08-08 17:48:29 +00:00
final String desc = "[" + file.getName() + "] " + system.getDescription();
2017-03-12 17:22:02 +00:00
final File f = fdata.getFile();
2013-12-10 19:36:50 +00:00
if (OptionFlags.getInstance().isWord()) {
final String warnOrError = system.getWarningOrError();
if (warnOrError != null) {
final String name = f.getName().substring(0, f.getName().length() - 4) + ".err";
final File errorFile = new File(f.getParentFile(), name);
final PrintStream ps = new PrintStream(new FileOutputStream(errorFile));
ps.print(warnOrError);
ps.close();
}
}
2015-09-28 20:42:17 +00:00
final GeneratedImage generatedImage = new GeneratedImageImpl(f, desc, blockUml);
2010-11-15 20:35:36 +00:00
result.add(generatedImage);
}
}
Log.info("Number of image(s): " + result.size());
return Collections.unmodifiableList(result);
}
2015-08-05 20:17:01 +00:00
private boolean endsWithSlashOrAntislash(String newName) {
return newName.endsWith("/") || newName.endsWith("\\");
}
2015-04-07 18:18:37 +00:00
public List<String> getEncodedUrl() throws IOException {
2010-11-15 20:35:36 +00:00
final List<String> result = new ArrayList<String>();
2011-01-05 18:23:06 +00:00
final Transcoder transcoder = TranscoderUtil.getDefaultTranscoder();
2010-11-15 20:35:36 +00:00
for (BlockUml blockUml : builder.getBlockUmls()) {
2013-12-10 19:36:50 +00:00
final String source = blockUml.getDiagram().getSource().getPlainString();
2010-11-15 20:35:36 +00:00
final String encoded = transcoder.encode(source);
result.add(encoded);
}
return Collections.unmodifiableList(result);
}
private Reader getReader(String charset) throws FileNotFoundException, UnsupportedEncodingException {
if (charset == null) {
Log.info("Using default charset");
return new InputStreamReader(new FileInputStream(file));
}
Log.info("Using charset " + charset);
return new InputStreamReader(new FileInputStream(file), charset);
}
2011-01-05 18:23:06 +00:00
public final void setFileFormatOption(FileFormatOption fileFormatOption) {
this.fileFormatOption = fileFormatOption;
2010-11-15 20:35:36 +00:00
}
2011-08-08 17:48:29 +00:00
2015-09-28 20:42:17 +00:00
public final Set<FileWithSuffix> getIncludedFiles() {
2011-01-29 15:09:35 +00:00
return builder.getIncludedFiles();
}
2010-11-15 20:35:36 +00:00
}