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

160 lines
5.2 KiB
Java
Raw Normal View History

2020-01-12 22:13:17 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2023-02-22 18:43:48 +00:00
* (C) Copyright 2009-2024, Arnaud Roques
2020-01-12 22:13:17 +00:00
*
2023-02-22 18:43:48 +00:00
* Project Info: https://plantuml.com
2022-08-17 17:34:24 +00:00
*
2020-01-12 22:13:17 +00:00
* If you like this project or if you find it useful, you can support us at:
2022-08-17 17:34:24 +00:00
*
2023-02-22 18:43:48 +00:00
* https://plantuml.com/patreon (only 1$ per month!)
* https://plantuml.com/paypal
2022-08-17 17:34:24 +00:00
*
2020-01-12 22:13:17 +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
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* 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
* Modified by: Nicolas Jouanin
2022-08-17 17:34:24 +00:00
*
2020-01-12 22:13:17 +00:00
*
*/
package net.sourceforge.plantuml.preproc2;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
2021-05-14 08:42:57 +00:00
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
2020-01-12 22:13:17 +00:00
import java.util.regex.Matcher;
import java.util.regex.Pattern;
2022-12-17 11:10:05 +00:00
import net.sourceforge.plantuml.StringUtils;
2022-08-17 17:34:24 +00:00
import net.sourceforge.plantuml.log.Logme;
2020-01-12 22:13:17 +00:00
import net.sourceforge.plantuml.preproc.ReadLine;
import net.sourceforge.plantuml.preproc.ReadLineReader;
import net.sourceforge.plantuml.preproc.ReadLineSimple;
import net.sourceforge.plantuml.preproc.StartDiagramExtractReader;
import net.sourceforge.plantuml.preproc.Stdlib;
2020-05-30 15:20:23 +00:00
import net.sourceforge.plantuml.security.SURL;
2023-02-02 17:59:43 +00:00
import net.sourceforge.plantuml.text.StringLocated;
2020-01-12 22:13:17 +00:00
import net.sourceforge.plantuml.tim.EaterException;
2022-12-17 11:01:10 +00:00
import net.sourceforge.plantuml.utils.LineLocation;
import net.sourceforge.plantuml.utils.Log;
2020-01-12 22:13:17 +00:00
public class PreprocessorUtils {
public static String withEnvironmentVariable(String s) {
final Pattern p = Pattern.compile("%(\\w+)%");
final Matcher m = p.matcher(s);
final StringBuffer sb = new StringBuffer(); // Can't be switched to StringBuilder in order to support Java 8
2020-01-12 22:13:17 +00:00
while (m.find()) {
final String var = m.group(1);
final String value = getenv(var);
2022-08-17 17:34:24 +00:00
if (value != null)
2020-01-12 22:13:17 +00:00
m.appendReplacement(sb, Matcher.quoteReplacement(value));
2022-08-17 17:34:24 +00:00
2020-01-12 22:13:17 +00:00
}
m.appendTail(sb);
s = sb.toString();
return s;
}
public static String getenv(String var) {
final String env = System.getProperty(var);
2022-08-17 17:34:24 +00:00
if (StringUtils.isNotEmpty(env))
2020-01-12 22:13:17 +00:00
return StringUtils.eventuallyRemoveStartingAndEndingDoubleQuote(env);
2022-08-17 17:34:24 +00:00
2020-01-12 22:13:17 +00:00
final String getenv = System.getenv(var);
2022-08-17 17:34:24 +00:00
if (StringUtils.isNotEmpty(getenv))
2020-01-12 22:13:17 +00:00
return StringUtils.eventuallyRemoveStartingAndEndingDoubleQuote(getenv);
2022-08-17 17:34:24 +00:00
2020-01-12 22:13:17 +00:00
return null;
}
private static InputStream getStdlibInputStream(String filename) {
final InputStream result = Stdlib.getResourceAsStream(filename);
// Log.info("Loading sdlib " + filename + " ok");
return result;
}
2023-02-28 21:22:51 +00:00
// ::comment when __CORE__
2023-02-22 18:43:48 +00:00
public static ReadLine getReaderNonstandardInclude(StringLocated s, String filename) {
if (filename.endsWith(".puml") == false)
filename = filename + ".puml";
Log.info("Loading non standard " + filename);
final String res = "/stdlib/" + filename;
InputStream is = Stdlib.class.getResourceAsStream(res);
if (is == null)
return null;
final String description = "[" + filename + "]";
return ReadLineReader.create(new InputStreamReader(is), description);
}
// ::done
2020-01-12 22:13:17 +00:00
public static ReadLine getReaderStdlibInclude(StringLocated s, String filename) {
Log.info("Loading sdlib " + filename);
InputStream is = getStdlibInputStream(filename);
2022-08-17 17:34:24 +00:00
if (is == null)
2020-01-12 22:13:17 +00:00
return null;
2022-08-17 17:34:24 +00:00
2020-01-12 22:13:17 +00:00
final String description = "<" + filename + ">";
try {
if (StartDiagramExtractReader.containsStartDiagram(is, s, description)) {
is = getStdlibInputStream(filename);
return StartDiagramExtractReader.build(is, s, description);
}
is = getStdlibInputStream(filename);
2022-08-17 17:34:24 +00:00
if (is == null)
2020-01-12 22:13:17 +00:00
return null;
2022-08-17 17:34:24 +00:00
2020-01-12 22:13:17 +00:00
return ReadLineReader.create(new InputStreamReader(is), description);
} catch (IOException e) {
2022-08-17 17:34:24 +00:00
Logme.error(e);
2020-01-12 22:13:17 +00:00
return new ReadLineSimple(s, e.toString());
}
}
public static ReadLine getReaderIncludeUrl(final SURL url, StringLocated s, String suf, Charset charset)
2020-01-12 22:13:17 +00:00
throws EaterException {
try {
2022-08-17 17:34:24 +00:00
if (StartDiagramExtractReader.containsStartDiagram(url, s, charset))
2020-01-12 22:13:17 +00:00
return StartDiagramExtractReader.build(url, s, suf, charset);
2022-08-17 17:34:24 +00:00
return getReaderInclude(url, s, charset);
2020-01-12 22:13:17 +00:00
} catch (IOException e) {
2022-08-17 17:34:24 +00:00
Logme.error(e);
throw EaterException.located("Cannot open URL " + e.getMessage(), s);
2020-01-12 22:13:17 +00:00
}
}
public static ReadLine getReaderInclude(SURL url, StringLocated s, Charset charset)
2021-05-14 08:42:57 +00:00
throws EaterException, UnsupportedEncodingException {
final InputStream is = url.openStream();
2022-08-17 17:34:24 +00:00
if (is == null)
throw EaterException.located("Cannot open URL", s);
2022-08-17 17:34:24 +00:00
return ReadLineReader.create(new InputStreamReader(is, charset), url.toString(), s.getLocation());
2021-05-14 08:42:57 +00:00
}
2020-01-12 22:13:17 +00:00
}