mirror of
https://github.com/octoleo/plantuml.git
synced 2024-11-26 14:56:28 +00:00
Simplify Charset usage by using non-null Charset objects instead of charset name in nullable Strings
This commit is contained in:
parent
5cb202295d
commit
91749a2da3
@ -35,7 +35,10 @@
|
||||
*/
|
||||
package net.sourceforge.plantuml;
|
||||
|
||||
import static net.sourceforge.plantuml.utils.CharsetUtils.charsetOrDefault;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@ -72,7 +75,7 @@ public class BlockUml {
|
||||
}
|
||||
|
||||
BlockUml(String... strings) {
|
||||
this(convert(strings), Defines.createEmpty(), null, null);
|
||||
this(convert(strings), Defines.createEmpty(), null, null, null);
|
||||
}
|
||||
|
||||
public String getEncodedUrl() throws IOException {
|
||||
@ -108,7 +111,15 @@ public class BlockUml {
|
||||
|
||||
private boolean preprocessorError;
|
||||
|
||||
/**
|
||||
* @deprecated being kept for backwards compatibility, perhaps other projects are using this?
|
||||
*/
|
||||
@Deprecated
|
||||
public BlockUml(List<StringLocated> strings, Defines defines, ISkinSimple skinParam, PreprocessorModeSet mode) {
|
||||
this(strings, defines, skinParam, mode, charsetOrDefault(mode.getCharset()));
|
||||
}
|
||||
|
||||
public BlockUml(List<StringLocated> strings, Defines defines, ISkinSimple skinParam, PreprocessorModeSet mode, Charset charset) {
|
||||
this.rawSource = new ArrayList<>(strings);
|
||||
this.localDefines = defines;
|
||||
this.skinParam = skinParam;
|
||||
@ -119,7 +130,7 @@ public class BlockUml {
|
||||
if (mode == null) {
|
||||
this.data = new ArrayList<>(strings);
|
||||
} else {
|
||||
final TimLoader timLoader = new TimLoader(mode.getImportedFiles(), defines, mode.getCharset(),
|
||||
final TimLoader timLoader = new TimLoader(mode.getImportedFiles(), defines, charset,
|
||||
(DefinitionsContainer) mode);
|
||||
this.included.addAll(timLoader.load(strings));
|
||||
this.data = timLoader.getResultList();
|
||||
|
@ -35,8 +35,12 @@
|
||||
*/
|
||||
package net.sourceforge.plantuml;
|
||||
|
||||
import static java.util.Objects.requireNonNull;
|
||||
import static net.sourceforge.plantuml.utils.CharsetUtils.charsetOrDefault;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
@ -60,13 +64,23 @@ public final class BlockUmlBuilder implements DefinitionsContainer {
|
||||
private final UncommentReadLine reader;
|
||||
private final Defines defines;
|
||||
private final ImportedFiles importedFiles;
|
||||
private final String charset;
|
||||
private final Charset charset;
|
||||
|
||||
/**
|
||||
* @deprecated being kept for backwards compatibility, perhaps other projects are using this?
|
||||
*/
|
||||
@Deprecated
|
||||
public BlockUmlBuilder(List<String> config, String charset, Defines defines, Reader readerInit, SFile newCurrentDir,
|
||||
String desc) throws IOException {
|
||||
|
||||
this(config, charsetOrDefault(charset), defines, readerInit, newCurrentDir, desc);
|
||||
}
|
||||
|
||||
public BlockUmlBuilder(List<String> config, Charset charset, Defines defines, Reader readerInit, SFile newCurrentDir,
|
||||
String desc) throws IOException {
|
||||
|
||||
this.defines = defines;
|
||||
this.charset = charset;
|
||||
this.charset = requireNonNull(charset);
|
||||
this.reader = new UncommentReadLine(ReadLineReader.create(readerInit, desc));
|
||||
this.importedFiles = ImportedFiles.createImportedFiles(new AParentFolderRegular(newCurrentDir));
|
||||
|
||||
@ -77,6 +91,10 @@ public final class BlockUmlBuilder implements DefinitionsContainer {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated being kept for backwards compatibility, perhaps other projects are using this?
|
||||
*/
|
||||
@Deprecated
|
||||
public BlockUmlBuilder(List<String> config, String charset, Defines defines, Reader reader) throws IOException {
|
||||
this(config, charset, defines, reader, null, null);
|
||||
}
|
||||
@ -116,7 +134,7 @@ public final class BlockUmlBuilder implements DefinitionsContainer {
|
||||
if (paused) {
|
||||
current.add(s);
|
||||
}
|
||||
final BlockUml uml = new BlockUml(current, defines.cloneMe(), null, this);
|
||||
final BlockUml uml = new BlockUml(current, defines.cloneMe(), null, this, charset);
|
||||
usedFiles.addAll(uml.getIncluded());
|
||||
blocks.add(uml);
|
||||
current = null;
|
||||
@ -146,8 +164,12 @@ public final class BlockUmlBuilder implements DefinitionsContainer {
|
||||
return importedFiles;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated being kept for backwards compatibility, perhaps other projects are using this?
|
||||
*/
|
||||
@Deprecated
|
||||
public final String getCharset() {
|
||||
return charset;
|
||||
return charset.name();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -194,7 +194,7 @@ public class EmbeddedDiagram implements CharSequence {
|
||||
}
|
||||
|
||||
private Diagram getSystem() throws IOException, InterruptedException {
|
||||
final BlockUml blockUml = new BlockUml(system.as2(), Defines.createEmpty(), skinParam, null);
|
||||
final BlockUml blockUml = new BlockUml(system.as2(), Defines.createEmpty(), skinParam, null, null);
|
||||
return blockUml.getDiagram();
|
||||
|
||||
}
|
||||
|
@ -35,6 +35,8 @@
|
||||
*/
|
||||
package net.sourceforge.plantuml;
|
||||
|
||||
import static net.sourceforge.plantuml.utils.CharsetUtils.charsetOrDefault;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
@ -45,6 +47,7 @@ import java.io.OutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.io.Reader;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@ -69,13 +72,15 @@ public abstract class SourceFileReaderAbstract implements ISourceFileReader {
|
||||
private boolean checkMetadata;
|
||||
private boolean noerror;
|
||||
|
||||
public SourceFileReaderAbstract(File file, FileFormatOption fileFormatOption, Defines defines, List<String> config, String charset)
|
||||
public SourceFileReaderAbstract(File file, FileFormatOption fileFormatOption, Defines defines, List<String> config, String charsetName)
|
||||
throws IOException {
|
||||
|
||||
if (!file.exists()) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
final Charset charset = charsetOrDefault(charsetName);
|
||||
|
||||
this.file = file;
|
||||
this.fileFormatOption = fileFormatOption;
|
||||
this.builder = new BlockUmlBuilder(config, charset, defines, getReader(charset),
|
||||
@ -99,12 +104,7 @@ public abstract class SourceFileReaderAbstract implements ISourceFileReader {
|
||||
return builder.getBlockUmls();
|
||||
}
|
||||
|
||||
protected Reader getReader(String charset) throws FileNotFoundException, UnsupportedEncodingException {
|
||||
if (charset == null) {
|
||||
Log.info("Using default charset");
|
||||
return new InputStreamReader(new BufferedInputStream(new FileInputStream(file)));
|
||||
}
|
||||
Log.info("Using charset " + charset);
|
||||
protected Reader getReader(Charset charset) throws FileNotFoundException, UnsupportedEncodingException {
|
||||
return new InputStreamReader(new BufferedInputStream(new FileInputStream(file)), charset);
|
||||
}
|
||||
|
||||
|
@ -36,6 +36,7 @@
|
||||
package net.sourceforge.plantuml;
|
||||
|
||||
import static net.sourceforge.plantuml.ugraphic.ImageBuilder.plainImageBuilder;
|
||||
import static net.sourceforge.plantuml.utils.CharsetUtils.charsetOrDefault;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
@ -85,7 +86,7 @@ public class SourceStringReader {
|
||||
// // WARNING GLOBAL LOCK HERE
|
||||
// synchronized (SourceStringReader.class) {
|
||||
try {
|
||||
final BlockUmlBuilder builder = new BlockUmlBuilder(config, charset, defines, new StringReader(source),
|
||||
final BlockUmlBuilder builder = new BlockUmlBuilder(config, charsetOrDefault(charset), defines, new StringReader(source),
|
||||
newCurrentDir, "string");
|
||||
this.blocks = builder.getBlockUmls();
|
||||
} catch (IOException e) {
|
||||
|
@ -42,6 +42,7 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.zip.ZipEntry;
|
||||
@ -68,7 +69,7 @@ public class FileWithSuffix {
|
||||
return file.toString();
|
||||
}
|
||||
|
||||
public Reader getReader(String charset) throws IOException {
|
||||
public Reader getReader(Charset charset) throws IOException {
|
||||
if (file == null) {
|
||||
return null;
|
||||
}
|
||||
@ -77,22 +78,12 @@ public class FileWithSuffix {
|
||||
return null;
|
||||
}
|
||||
if (entry == null) {
|
||||
if (charset == null) {
|
||||
Log.info("Using default charset");
|
||||
return new InputStreamReader(tmp);
|
||||
}
|
||||
Log.info("Using charset " + charset);
|
||||
return new InputStreamReader(tmp, charset);
|
||||
}
|
||||
final InputStream is = getDataFromZip(tmp, entry);
|
||||
if (is == null) {
|
||||
return null;
|
||||
}
|
||||
if (charset == null) {
|
||||
Log.info("Using default charset");
|
||||
return new InputStreamReader(is);
|
||||
}
|
||||
Log.info("Using charset " + charset);
|
||||
return new InputStreamReader(is, charset);
|
||||
}
|
||||
|
||||
|
@ -39,6 +39,7 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import net.sourceforge.plantuml.Log;
|
||||
import net.sourceforge.plantuml.StringLocated;
|
||||
@ -50,11 +51,11 @@ public class StartDiagramExtractReader implements ReadLine {
|
||||
private final ReadLine raw;
|
||||
private boolean finished = false;
|
||||
|
||||
public static StartDiagramExtractReader build(FileWithSuffix f2, StringLocated s, String charset) {
|
||||
public static StartDiagramExtractReader build(FileWithSuffix f2, StringLocated s, Charset charset) {
|
||||
return new StartDiagramExtractReader(getReadLine(f2, s, charset), f2.getSuffix());
|
||||
}
|
||||
|
||||
public static StartDiagramExtractReader build(SURL url, StringLocated s, String uid, String charset) {
|
||||
public static StartDiagramExtractReader build(SURL url, StringLocated s, String uid, Charset charset) {
|
||||
return new StartDiagramExtractReader(getReadLine(url, s, charset), uid);
|
||||
}
|
||||
|
||||
@ -101,7 +102,7 @@ public class StartDiagramExtractReader implements ReadLine {
|
||||
return false;
|
||||
}
|
||||
|
||||
private static ReadLine getReadLine(FileWithSuffix f2, StringLocated s, String charset) {
|
||||
private static ReadLine getReadLine(FileWithSuffix f2, StringLocated s, Charset charset) {
|
||||
try {
|
||||
final Reader tmp1 = f2.getReader(charset);
|
||||
if (tmp1 == null) {
|
||||
@ -117,29 +118,20 @@ public class StartDiagramExtractReader implements ReadLine {
|
||||
return new UncommentReadLine(ReadLineReader.create(new InputStreamReader(is), description));
|
||||
}
|
||||
|
||||
private static ReadLine getReadLine(SURL url, StringLocated s, String charset) {
|
||||
try {
|
||||
final InputStream tmp = url.openStream();
|
||||
if (tmp == null) {
|
||||
return new ReadLineSimple(s, "Cannot connect");
|
||||
}
|
||||
if (charset == null) {
|
||||
Log.info("Using default charset");
|
||||
return new UncommentReadLine(ReadLineReader.create(new InputStreamReader(tmp), url.toString()));
|
||||
}
|
||||
Log.info("Using charset " + charset);
|
||||
return new UncommentReadLine(ReadLineReader.create(new InputStreamReader(tmp, charset), url.toString()));
|
||||
} catch (IOException e) {
|
||||
return new ReadLineSimple(s, e.toString());
|
||||
private static ReadLine getReadLine(SURL url, StringLocated s, Charset charset) {
|
||||
final InputStream tmp = url.openStream();
|
||||
if (tmp == null) {
|
||||
return new ReadLineSimple(s, "Cannot connect");
|
||||
}
|
||||
return new UncommentReadLine(ReadLineReader.create(new InputStreamReader(tmp, charset), url.toString()));
|
||||
}
|
||||
|
||||
static public boolean containsStartDiagram(FileWithSuffix f2, StringLocated s, String charset) throws IOException {
|
||||
static public boolean containsStartDiagram(FileWithSuffix f2, StringLocated s, Charset charset) throws IOException {
|
||||
final ReadLine r = getReadLine(f2, s, charset);
|
||||
return containsStartDiagram(r);
|
||||
}
|
||||
|
||||
static public boolean containsStartDiagram(SURL url, StringLocated s, String charset) throws IOException {
|
||||
static public boolean containsStartDiagram(SURL url, StringLocated s, Charset charset) throws IOException {
|
||||
final ReadLine r = getReadLine(url, s, charset);
|
||||
return containsStartDiagram(r);
|
||||
}
|
||||
|
@ -40,6 +40,7 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@ -115,7 +116,7 @@ public class PreprocessorUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static ReadLine getReaderIncludeUrl(final SURL url, StringLocated s, String suf, String charset)
|
||||
public static ReadLine getReaderIncludeUrl(final SURL url, StringLocated s, String suf, Charset charset)
|
||||
throws EaterException {
|
||||
try {
|
||||
if (StartDiagramExtractReader.containsStartDiagram(url, s, charset)) {
|
||||
@ -129,17 +130,12 @@ public class PreprocessorUtils {
|
||||
|
||||
}
|
||||
|
||||
public static ReadLine getReaderInclude(SURL url, LineLocation lineLocation, String charset)
|
||||
public static ReadLine getReaderInclude(SURL url, LineLocation lineLocation, Charset charset)
|
||||
throws EaterException, UnsupportedEncodingException {
|
||||
final InputStream is = url.openStream();
|
||||
if (is == null) {
|
||||
throw EaterException.located("Cannot open URL");
|
||||
}
|
||||
if (charset == null) {
|
||||
Log.info("Using default charset");
|
||||
return ReadLineReader.create(new InputStreamReader(is), url.toString(), lineLocation);
|
||||
}
|
||||
Log.info("Using charset " + charset);
|
||||
return ReadLineReader.create(new InputStreamReader(is, charset), url.toString(), lineLocation);
|
||||
}
|
||||
|
||||
|
@ -34,6 +34,8 @@
|
||||
*/
|
||||
package net.sourceforge.plantuml.tim;
|
||||
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
@ -85,7 +87,7 @@ public class EaterTheme extends Eater {
|
||||
throw EaterException.located("Cannot open URL");
|
||||
}
|
||||
try {
|
||||
return PreprocessorUtils.getReaderInclude(url, getLineLocation(), "UTF-8");
|
||||
return PreprocessorUtils.getReaderInclude(url, getLineLocation(), UTF_8);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
throw EaterException.located("Cannot decode charset");
|
||||
@ -94,7 +96,7 @@ public class EaterTheme extends Eater {
|
||||
|
||||
try {
|
||||
final FileWithSuffix file = context.getFileWithSuffix(from, realName);
|
||||
return ReadLineReader.create(file.getReader("UTF-8"), "theme " + realName);
|
||||
return ReadLineReader.create(file.getReader(UTF_8), "theme " + realName);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
throw EaterException.located("Cannot load " + realName);
|
||||
|
@ -34,8 +34,11 @@
|
||||
*/
|
||||
package net.sourceforge.plantuml.tim;
|
||||
|
||||
import static java.util.Objects.requireNonNull;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
@ -124,7 +127,7 @@ public class TContext {
|
||||
public final FunctionsSet functionsSet = new FunctionsSet();
|
||||
|
||||
private ImportedFiles importedFiles;
|
||||
private final String charset;
|
||||
private final Charset charset;
|
||||
|
||||
private final Map<String, Sub> subs = new HashMap<String, Sub>();
|
||||
private final DefinitionsContainer definitionsContainer;
|
||||
@ -183,11 +186,11 @@ public class TContext {
|
||||
// %trim
|
||||
}
|
||||
|
||||
public TContext(ImportedFiles importedFiles, Defines defines, String charset,
|
||||
public TContext(ImportedFiles importedFiles, Defines defines, Charset charset,
|
||||
DefinitionsContainer definitionsContainer) {
|
||||
this.definitionsContainer = definitionsContainer;
|
||||
this.importedFiles = importedFiles;
|
||||
this.charset = charset;
|
||||
this.charset = requireNonNull(charset);
|
||||
this.addStandardFunctions(defines);
|
||||
}
|
||||
|
||||
|
@ -34,6 +34,7 @@
|
||||
*/
|
||||
package net.sourceforge.plantuml.tim;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@ -50,7 +51,7 @@ public class TimLoader {
|
||||
private boolean preprocessorError;
|
||||
private List<StringLocated> resultList;
|
||||
|
||||
public TimLoader(ImportedFiles importedFiles, Defines defines, String charset,
|
||||
public TimLoader(ImportedFiles importedFiles, Defines defines, Charset charset,
|
||||
DefinitionsContainer definitionsContainer) {
|
||||
this.context = new TContext(importedFiles, defines, charset, definitionsContainer);
|
||||
try {
|
||||
|
19
src/net/sourceforge/plantuml/utils/CharsetUtils.java
Normal file
19
src/net/sourceforge/plantuml/utils/CharsetUtils.java
Normal file
@ -0,0 +1,19 @@
|
||||
package net.sourceforge.plantuml.utils;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import net.sourceforge.plantuml.Log;
|
||||
|
||||
public class CharsetUtils {
|
||||
|
||||
public static Charset charsetOrDefault(String charsetName) {
|
||||
|
||||
if (charsetName == null) {
|
||||
Log.info("Using default charset");
|
||||
return Charset.defaultCharset();
|
||||
} else {
|
||||
Log.info("Using charset " + charsetName);
|
||||
return Charset.forName(charsetName);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user