1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-02 00:20:49 +00:00
plantuml/src/net/sourceforge/plantuml/preproc/PreprocessorInclude.java

350 lines
12 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
2015-04-07 18:18:37 +00:00
* Modified by: Nicolas Jouanin
2010-11-15 20:35:36 +00:00
*
*
*/
package net.sourceforge.plantuml.preproc;
import java.io.File;
2013-12-10 19:36:50 +00:00
import java.io.FileInputStream;
2010-11-15 20:35:36 +00:00
import java.io.FileReader;
import java.io.IOException;
2015-04-07 18:18:37 +00:00
import java.io.InputStream;
2013-12-10 19:36:50 +00:00
import java.io.InputStreamReader;
2015-04-07 18:18:37 +00:00
import java.net.MalformedURLException;
import java.net.URL;
2017-11-20 16:10:36 +00:00
import java.util.ArrayList;
2015-06-07 10:23:10 +00:00
import java.util.Collections;
import java.util.HashSet;
2017-04-05 17:37:42 +00:00
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 java.util.regex.Matcher;
import java.util.regex.Pattern;
2015-06-20 10:54:49 +00:00
import net.sourceforge.plantuml.CharSequence2;
2017-04-05 17:37:42 +00:00
import net.sourceforge.plantuml.DefinitionsContainer;
2010-11-15 20:35:36 +00:00
import net.sourceforge.plantuml.FileSystem;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.Log;
import net.sourceforge.plantuml.OptionFlags;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.StringUtils;
2016-05-31 19:41:55 +00:00
import net.sourceforge.plantuml.command.regex.Matcher2;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.command.regex.MyPattern;
2016-05-31 19:41:55 +00:00
import net.sourceforge.plantuml.command.regex.Pattern2;
2015-05-03 15:36:36 +00:00
import net.sourceforge.plantuml.utils.StartUtils;
2010-11-15 20:35:36 +00:00
2017-04-05 17:37:42 +00:00
public class PreprocessorInclude implements ReadLine {
2010-11-15 20:35:36 +00:00
2017-04-05 17:37:42 +00:00
private static final Pattern2 includeDefPattern = MyPattern.cmpile("^[%s]*!includedef[%s]+[%g]?([^%g]+)[%g]?$");
2016-05-31 19:41:55 +00:00
private static final Pattern2 includePattern = MyPattern.cmpile("^[%s]*!include[%s]+[%g]?([^%g]+)[%g]?$");
private static final Pattern2 includeManyPattern = MyPattern.cmpile("^[%s]*!include_many[%s]+[%g]?([^%g]+)[%g]?$");
private static final Pattern2 includeURLPattern = MyPattern.cmpile("^[%s]*!includeurl[%s]+[%g]?([^%g]+)[%g]?$");
2010-11-15 20:35:36 +00:00
private final ReadLine reader2;
2013-12-10 19:36:50 +00:00
private final String charset;
2015-04-07 18:18:37 +00:00
private final Defines defines;
2017-11-20 16:10:36 +00:00
private final List<String> config;
2017-04-05 17:37:42 +00:00
private final DefinitionsContainer definitionsContainer;
2013-12-10 19:36:50 +00:00
2010-11-15 20:35:36 +00:00
private int numLine = 0;
private PreprocessorInclude included = null;
2011-03-26 12:37:27 +00:00
2011-08-08 17:48:29 +00:00
private final File oldCurrentDir;
2015-09-28 20:42:17 +00:00
private final Set<FileWithSuffix> filesUsedCurrent;
private final Set<FileWithSuffix> filesUsedGlobal;
2010-11-15 20:35:36 +00:00
2017-11-20 16:10:36 +00:00
public PreprocessorInclude(List<String> config, ReadLine reader, Defines defines, String charset,
File newCurrentDir, DefinitionsContainer definitionsContainer) {
this(config, reader, defines, charset, newCurrentDir, new HashSet<FileWithSuffix>(),
new HashSet<FileWithSuffix>(), definitionsContainer);
2015-06-07 10:23:10 +00:00
}
2015-09-28 20:42:17 +00:00
public Set<FileWithSuffix> getFilesUsedGlobal() {
2015-06-07 10:23:10 +00:00
return Collections.unmodifiableSet(filesUsedGlobal);
}
2017-11-20 16:10:36 +00:00
private PreprocessorInclude(List<String> config, ReadLine reader, Defines defines, String charset,
File newCurrentDir, Set<FileWithSuffix> filesUsedCurrent, Set<FileWithSuffix> filesUsedGlobal,
2017-04-05 17:37:42 +00:00
DefinitionsContainer definitionsContainer) {
2017-11-20 16:10:36 +00:00
this.config = config;
2015-04-07 18:18:37 +00:00
this.defines = defines;
2013-12-10 19:36:50 +00:00
this.charset = charset;
2010-11-15 20:35:36 +00:00
this.reader2 = reader;
2017-04-05 17:37:42 +00:00
this.definitionsContainer = definitionsContainer;
2015-06-07 10:23:10 +00:00
this.filesUsedCurrent = filesUsedCurrent;
this.filesUsedGlobal = filesUsedGlobal;
2011-08-08 17:48:29 +00:00
if (newCurrentDir == null) {
oldCurrentDir = null;
} else {
oldCurrentDir = FileSystem.getInstance().getCurrentDir();
FileSystem.getInstance().setCurrentDir(newCurrentDir);
}
}
private void restoreCurrentDir() {
if (oldCurrentDir != null) {
FileSystem.getInstance().setCurrentDir(oldCurrentDir);
}
2010-11-15 20:35:36 +00:00
}
2015-06-20 10:54:49 +00:00
public CharSequence2 readLine() throws IOException {
final CharSequence2 result = readLineInternal();
2017-11-20 16:10:36 +00:00
if (result != null && StartUtils.isArobaseStartDiagram(result) && config.size() > 0) {
final List<String> empty = new ArrayList<String>();
included = new PreprocessorInclude(empty, new ReadLineList(config, result.getLocation()), defines, charset,
null, filesUsedCurrent, filesUsedGlobal, definitionsContainer);
}
2015-05-03 15:36:36 +00:00
if (result != null && (StartUtils.isArobaseEndDiagram(result) || StartUtils.isArobaseStartDiagram(result))) {
// http://plantuml.sourceforge.net/qa/?qa=3389/error-generating-when-same-file-included-different-diagram
2015-06-07 10:23:10 +00:00
filesUsedCurrent.clear();
2015-05-03 15:36:36 +00:00
}
return result;
}
2015-06-20 10:54:49 +00:00
private CharSequence2 readLineInternal() throws IOException {
2010-11-15 20:35:36 +00:00
if (included != null) {
2015-06-20 10:54:49 +00:00
final CharSequence2 s = included.readLine();
2010-11-15 20:35:36 +00:00
if (s != null) {
return s;
}
included.close();
included = null;
}
2015-06-20 10:54:49 +00:00
final CharSequence2 s = reader2.readLine();
2010-11-15 20:35:36 +00:00
numLine++;
if (s == null) {
return null;
}
2013-12-10 19:36:50 +00:00
if (OptionFlags.ALLOW_INCLUDE) {
assert included == null;
2016-05-31 19:41:55 +00:00
final Matcher2 m1 = includePattern.matcher(s);
2016-05-11 21:31:47 +00:00
if (m1.find()) {
2017-03-15 19:13:31 +00:00
return manageFileInclude(s, m1, false);
2016-05-11 21:31:47 +00:00
}
2016-05-31 19:41:55 +00:00
final Matcher2 m2 = includeManyPattern.matcher(s);
2016-05-11 21:31:47 +00:00
if (m2.find()) {
2017-03-15 19:13:31 +00:00
return manageFileInclude(s, m2, true);
2013-12-10 19:36:50 +00:00
}
2017-04-05 17:37:42 +00:00
final Matcher2 m3 = includeDefPattern.matcher(s);
if (m3.find()) {
return manageDefinitionInclude(s, m3);
}
2010-11-15 20:35:36 +00:00
}
2016-05-31 19:41:55 +00:00
final Matcher2 mUrl = includeURLPattern.matcher(s);
2017-03-15 19:13:31 +00:00
if (s.getPreprocessorError() == null && mUrl.find()) {
return manageUrlInclude(s, mUrl);
2015-04-07 18:18:37 +00:00
}
2010-11-15 20:35:36 +00:00
return s;
}
2017-03-15 19:13:31 +00:00
private CharSequence2 manageUrlInclude(CharSequence2 s, Matcher2 m) throws IOException {
2015-04-07 18:18:37 +00:00
String urlString = m.group(1);
urlString = defines.applyDefines(urlString).get(0);
//
final int idx = urlString.lastIndexOf('!');
String suf = null;
if (idx != -1) {
suf = urlString.substring(idx + 1);
urlString = urlString.substring(0, idx);
}
try {
final URL url = new URL(urlString);
2017-11-20 16:10:36 +00:00
included = new PreprocessorInclude(config, getReaderInclude(s, url, suf), defines, charset, null,
filesUsedCurrent, filesUsedGlobal, definitionsContainer);
2015-04-07 18:18:37 +00:00
} catch (MalformedURLException e) {
2017-03-15 19:13:31 +00:00
return s.withErrorPreprocessor("Cannot include url " + urlString);
2015-04-07 18:18:37 +00:00
}
return this.readLine();
}
2017-04-05 17:37:42 +00:00
private CharSequence2 manageDefinitionInclude(CharSequence2 s, Matcher2 matcher) throws IOException {
final String definitionName = matcher.group(1);
final List<? extends CharSequence> definition = definitionsContainer.getDefinition(definitionName);
2017-11-20 16:10:36 +00:00
included = new PreprocessorInclude(config, new ReadLineList(definition, s.getLocation()), defines, charset,
null, filesUsedCurrent, filesUsedGlobal, definitionsContainer);
2017-04-05 17:37:42 +00:00
return this.readLine();
}
2017-03-15 19:13:31 +00:00
private CharSequence2 manageFileInclude(CharSequence2 s, Matcher2 matcher, boolean allowMany) throws IOException {
2016-05-11 21:31:47 +00:00
String fileName = matcher.group(1);
2015-04-07 18:18:37 +00:00
fileName = defines.applyDefines(fileName).get(0);
2017-10-07 09:46:53 +00:00
if (fileName.startsWith("<") && fileName.endsWith(">")) {
final ReadLine strlibReader = getReaderStdlibInclude(s, fileName.substring(1, fileName.length() - 1));
if (strlibReader == null) {
return s.withErrorPreprocessor("Cannot include " + fileName);
}
2017-11-20 16:10:36 +00:00
included = new PreprocessorInclude(config, strlibReader, defines, charset, null, filesUsedCurrent,
filesUsedGlobal, definitionsContainer);
2017-10-07 09:46:53 +00:00
return this.readLine();
}
2011-03-26 12:37:27 +00:00
final int idx = fileName.lastIndexOf('!');
String suf = null;
if (idx != -1) {
suf = fileName.substring(idx + 1);
fileName = fileName.substring(0, idx);
}
2015-04-07 18:18:37 +00:00
final File f = FileSystem.getInstance().getFile(withEnvironmentVariable(fileName));
2015-09-28 20:42:17 +00:00
final FileWithSuffix f2 = new FileWithSuffix(f, suf);
2017-03-15 19:13:31 +00:00
if (f.exists() == false || f.isDirectory()) {
return s.withErrorPreprocessor("Cannot include " + f.getAbsolutePath());
2016-05-11 21:31:47 +00:00
} else if (allowMany == false && filesUsedCurrent.contains(f2)) {
2016-02-07 21:13:01 +00:00
// return CharSequence2Impl.errorPreprocessor("File already included " + f.getAbsolutePath(), lineLocation);
return this.readLine();
2010-11-15 20:35:36 +00:00
}
2017-11-20 16:10:36 +00:00
filesUsedCurrent.add(f2);
filesUsedGlobal.add(f2);
included = new PreprocessorInclude(config, getReaderInclude(s, f, suf), defines, charset, f.getParentFile(),
filesUsedCurrent, filesUsedGlobal, definitionsContainer);
2010-11-15 20:35:36 +00:00
return this.readLine();
}
2015-04-07 18:18:37 +00:00
static String withEnvironmentVariable(String s) {
final Pattern p = Pattern.compile("%(\\w+)%");
final Matcher m = p.matcher(s);
final StringBuffer sb = new StringBuffer();
while (m.find()) {
final String var = m.group(1);
final String value = getenv(var);
if (value != null) {
m.appendReplacement(sb, Matcher.quoteReplacement(value));
}
}
m.appendTail(sb);
s = sb.toString();
return s;
}
2017-04-05 17:37:42 +00:00
public static String getenv(String var) {
2015-04-07 18:18:37 +00:00
final String env = System.getProperty(var);
if (StringUtils.isNotEmpty(env)) {
return StringUtils.eventuallyRemoveStartingAndEndingDoubleQuote(env);
}
final String getenv = System.getenv(var);
if (StringUtils.isNotEmpty(getenv)) {
return StringUtils.eventuallyRemoveStartingAndEndingDoubleQuote(getenv);
}
return null;
}
2017-10-07 09:46:53 +00:00
private InputStream getStdlibInputStream(String filename) {
if (filename.endsWith(".puml") == false) {
filename = filename + ".puml";
}
InputStream is = PreprocessorInclude.class.getResourceAsStream("/stdlib/" + filename);
if (is == null) {
is = PreprocessorInclude.class.getResourceAsStream("/stdlib/" + filename.toLowerCase());
}
return is;
}
private ReadLine getReaderStdlibInclude(CharSequence2 s, String filename) {
InputStream is = getStdlibInputStream(filename);
if (is == null) {
return null;
}
try {
if (StartDiagramExtractReader.containsStartDiagram(s, is)) {
is = getStdlibInputStream(filename);
return new StartDiagramExtractReader(s, is);
}
is = getStdlibInputStream(filename);
if (is == null) {
return null;
}
return new ReadLineReader(new InputStreamReader(is), filename);
} catch (IOException e) {
return new ReadLineSimple(s, e.toString());
}
}
// private ReadLine getReaderStdlibInclude2(CharSequence2 s, String filename) {
// InputStream is = DummyEmptyStdlibFile.class.getResourceAsStream(filename);
// if (is == null) {
// is = DummyEmptyStdlibFile.class.getResourceAsStream(filename.toLowerCase());
// }
// if (is == null) {
// return null;
// }
// return new ReadLineReader(new InputStreamReader(is), filename);
// }
2017-03-15 19:13:31 +00:00
private ReadLine getReaderInclude(CharSequence2 s, final File f, String suf) {
try {
if (StartDiagramExtractReader.containsStartDiagram(s, f, charset)) {
return new StartDiagramExtractReader(s, f, suf, charset);
}
if (charset == null) {
Log.info("Using default charset");
return new ReadLineReader(new FileReader(f), f.getAbsolutePath(), s.getLocation());
}
Log.info("Using charset " + charset);
return new ReadLineReader(new InputStreamReader(new FileInputStream(f), charset), f.getAbsolutePath(),
s.getLocation());
} catch (IOException e) {
return new ReadLineSimple(s, e.toString());
2011-03-26 12:37:27 +00:00
}
2017-03-15 19:13:31 +00:00
2011-03-26 12:37:27 +00:00
}
2017-03-15 19:13:31 +00:00
private ReadLine getReaderInclude(CharSequence2 s, final URL url, String suf) {
try {
if (StartDiagramExtractReader.containsStartDiagram(s, url, charset)) {
return new StartDiagramExtractReader(s, url, suf, charset);
}
final InputStream is = url.openStream();
if (charset == null) {
Log.info("Using default charset");
return new ReadLineReader(new InputStreamReader(is), url.toString(), s.getLocation());
}
Log.info("Using charset " + charset);
return new ReadLineReader(new InputStreamReader(is, charset), url.toString(), s.getLocation());
} catch (IOException e) {
return new ReadLineSimple(s, e.toString());
2015-04-07 18:18:37 +00:00
}
2017-03-15 19:13:31 +00:00
2015-04-07 18:18:37 +00:00
}
2010-11-15 20:35:36 +00:00
public int getLineNumber() {
return numLine;
}
public void close() throws IOException {
2011-08-08 17:48:29 +00:00
restoreCurrentDir();
2010-11-15 20:35:36 +00:00
reader2.close();
}
}