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

471 lines
13 KiB
Java
Raw Normal View History

2018-06-12 20:50:45 +00:00
package net.sourceforge.plantuml.preproc;
import static java.nio.charset.StandardCharsets.UTF_8;
2022-11-19 14:57:30 +00:00
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
2018-06-12 20:50:45 +00:00
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
2022-11-19 14:57:30 +00:00
import java.io.ByteArrayOutputStream;
2018-06-12 20:50:45 +00:00
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
2018-06-12 20:50:45 +00:00
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
2022-11-19 14:57:30 +00:00
import javax.imageio.ImageIO;
2018-06-12 20:50:45 +00:00
import net.sourceforge.plantuml.brotli.BrotliInputStream;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.klimt.creole.atom.AtomImg;
2022-08-17 17:34:24 +00:00
import net.sourceforge.plantuml.log.Logme;
2020-05-30 15:20:23 +00:00
import net.sourceforge.plantuml.security.SFile;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.utils.Base64Coder;
2022-12-17 11:01:10 +00:00
import net.sourceforge.plantuml.utils.Log;
2023-02-28 21:22:51 +00:00
// ::uncomment when __CORE__
//import java.io.FileInputStream;
//import java.io.FileNotFoundException;
2023-02-22 18:43:48 +00:00
//import static com.plantuml.api.cheerpj.StaticMemory.cheerpjPath;
// ::done
2018-06-12 20:50:45 +00:00
public class Stdlib {
2023-02-28 21:22:51 +00:00
// ::uncomment when __CORE__
// public static InputStream getResourceAsStream(String fullname) {
// fullname = fullname.replace(".puml", "");
// fullname = fullname.replace("awslib/", "awslib14/");
//
2023-02-22 18:43:48 +00:00
// final String fullpath = cheerpjPath + "stdlib/" + fullname + ".puml";
// System.err.println("Trying to read " + fullpath);
// // See https://docs.leaningtech.com/cheerpj/File-System-support
// try {
// return new FileInputStream(fullpath);
// } catch (FileNotFoundException e) {
// System.err.println("Cannot load " + fullpath);
// return null;
// }
// }
// ::done
2023-02-28 21:22:51 +00:00
// ::comment when __CORE__
2018-06-12 20:50:45 +00:00
private static final Map<String, Stdlib> all = new ConcurrentHashMap<String, Stdlib>();
private static final String SEPARATOR = "\uF8FF";
private static final Pattern sizePattern = Pattern.compile("\\[(\\d+)x(\\d+)/16\\]");
private final Map<String, SoftReference<String>> cache = new ConcurrentHashMap<String, SoftReference<String>>();
2018-06-12 20:50:45 +00:00
private final String name;
private final Map<String, String> info = new HashMap<String, String>();
private Stdlib(String name, String info) throws IOException {
this.name = name;
fillMap(info);
}
private void fillMap(String infoString) {
for (String s : infoString.split("\n"))
if (s.contains("=")) {
final String data[] = s.split("=");
this.info.put(data[0], data[1]);
}
}
2018-06-12 20:50:45 +00:00
public static InputStream getResourceAsStream(String fullname) {
fullname = fullname.toLowerCase().replace(".puml", "");
final int last = fullname.indexOf('/');
2022-08-17 17:34:24 +00:00
if (last == -1)
2018-06-12 20:50:45 +00:00
return null;
2022-08-17 17:34:24 +00:00
2018-06-12 20:50:45 +00:00
try {
final Stdlib folder = retrieve(fullname.substring(0, last));
2022-08-17 17:34:24 +00:00
if (folder == null || folder.info.size() == 0)
2018-06-12 20:50:45 +00:00
return null;
2022-08-17 17:34:24 +00:00
2022-10-05 20:32:57 +00:00
final String data = folder.loadResource(fullname.substring(last + 1));
2022-08-17 17:34:24 +00:00
if (data == null)
2018-06-12 20:50:45 +00:00
return null;
2022-08-17 17:34:24 +00:00
return new ByteArrayInputStream(data.getBytes(UTF_8));
2018-06-12 20:50:45 +00:00
} catch (IOException e) {
2022-08-17 17:34:24 +00:00
Logme.error(e);
2018-06-12 20:50:45 +00:00
return null;
}
}
2020-02-18 21:24:31 +00:00
public static Stdlib retrieve(final String name) throws IOException {
2018-06-12 20:50:45 +00:00
Stdlib result = all.get(name);
if (result == null) {
final DataInputStream dataStream = getDataStream(name);
2022-08-17 17:34:24 +00:00
if (dataStream == null)
2018-06-12 20:50:45 +00:00
return null;
2022-08-17 17:34:24 +00:00
2018-06-12 20:50:45 +00:00
final String info = dataStream.readUTF();
dataStream.close();
2022-11-15 23:15:21 +00:00
final String link = getLinkFromInfo(info);
if (link == null)
result = new Stdlib(name, info);
else
result = retrieve(link);
2018-06-12 20:50:45 +00:00
all.put(name, result);
}
return result;
}
2022-11-15 23:15:21 +00:00
private static String getLinkFromInfo(String infoString) {
for (String s : infoString.split("\n"))
if (s.contains("=")) {
final String data[] = s.split("=");
if (data[0].equals("LINK"))
return data[1];
}
return null;
}
2022-11-19 14:57:30 +00:00
private static int read1byte(InputStream is) throws IOException {
return is.read() & 0xFF;
}
private static int read2bytes(InputStream is) throws IOException {
return (read1byte(is) << 8) + read1byte(is);
}
2022-10-05 20:32:57 +00:00
private String loadResource(String file) throws IOException {
2018-06-12 20:50:45 +00:00
final SoftReference<String> cached = cache.get(file.toLowerCase());
if (cached != null) {
final String cachedResult = cached.get();
if (cachedResult != null) {
2018-07-27 21:56:46 +00:00
// Log.info("Using cache for " + file);
2018-06-12 20:50:45 +00:00
return cachedResult;
}
}
2018-07-27 21:56:46 +00:00
Log.info("No cache for " + file);
2018-06-12 20:50:45 +00:00
final DataInputStream dataStream = getDataStream();
2022-08-17 17:34:24 +00:00
if (dataStream == null)
2018-06-12 20:50:45 +00:00
return null;
2022-08-17 17:34:24 +00:00
2018-06-12 20:50:45 +00:00
dataStream.readUTF();
final InputStream spriteStream = getSpriteStream();
if (spriteStream == null) {
dataStream.close();
return null;
}
2022-11-19 14:57:30 +00:00
InputStream dataImagePngBase64Stream = null;
final List<Integer> colors = new ArrayList<>();
2018-06-12 20:50:45 +00:00
try {
StringBuilder found = null;
while (true) {
final String filename = dataStream.readUTF();
if (filename.equals(SEPARATOR)) {
2018-07-27 21:56:46 +00:00
Log.info("Not found " + filename);
2018-06-12 20:50:45 +00:00
return null;
}
2022-08-17 17:34:24 +00:00
if (filename.equalsIgnoreCase(file))
2018-06-12 20:50:45 +00:00
found = new StringBuilder();
2022-08-17 17:34:24 +00:00
2018-06-12 20:50:45 +00:00
while (true) {
2022-11-19 14:57:30 +00:00
String s = dataStream.readUTF();
2018-06-12 20:50:45 +00:00
if (s.equals(SEPARATOR)) {
if (found != null) {
final String result = found.toString();
2021-05-14 08:42:57 +00:00
cache.put(file.toLowerCase(), new SoftReference<>(result));
2018-06-12 20:50:45 +00:00
return result;
}
break;
}
2022-11-19 14:57:30 +00:00
if (s.contains(AtomImg.DATA_IMAGE_PNG_BASE64)) {
if (dataImagePngBase64Stream == null) {
dataImagePngBase64Stream = getDataImagePngBase64();
final int size = read2bytes(dataImagePngBase64Stream);
for (int i = 0; i < size; i++) {
final int alpha = read1byte(dataImagePngBase64Stream);
final int red = read1byte(dataImagePngBase64Stream);
final int green = read1byte(dataImagePngBase64Stream);
final int blue = read1byte(dataImagePngBase64Stream);
final int rgb = (alpha << 24) + (red << 16) + (green << 8) + blue;
colors.add(rgb);
}
}
final String base64 = readOneImage(dataImagePngBase64Stream, colors);
s = s.replaceFirst(AtomImg.DATA_IMAGE_PNG_BASE64, AtomImg.DATA_IMAGE_PNG_BASE64 + base64);
}
2018-06-12 20:50:45 +00:00
if (found != null) {
found.append(s);
found.append("\n");
}
if (isSpriteLine(s)) {
final Matcher m = sizePattern.matcher(s);
final boolean ok = m.find();
2022-08-17 17:34:24 +00:00
if (ok == false)
2018-06-12 20:50:45 +00:00
throw new IOException(s);
2022-08-17 17:34:24 +00:00
2018-06-12 20:50:45 +00:00
final int width = Integer.parseInt(m.group(1));
final int height = Integer.parseInt(m.group(2));
2018-07-27 21:56:46 +00:00
if (found == null) {
skipSprite(width, height, spriteStream);
} else {
final String sprite = readSprite(width, height, spriteStream);
2018-06-12 20:50:45 +00:00
found.append(sprite);
found.append("}\n");
}
}
}
}
} finally {
dataStream.close();
spriteStream.close();
2022-11-19 14:57:30 +00:00
if (dataImagePngBase64Stream != null)
dataImagePngBase64Stream.close();
}
}
private String readOneImage(InputStream is, List<Integer> colors) throws IOException {
final int width = is.read();
final int height = is.read();
final BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
for (int y = 0; y < height; y += 1)
for (int x = 0; x < width; x += 1) {
final int rgb = colors.get(read2bytes(is));
result.setRGB(x, y, rgb);
}
try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
ImageIO.write(result, "png", baos);
return new String(Base64Coder.encode(baos.toByteArray()));
2018-06-12 20:50:45 +00:00
}
}
2018-07-27 21:56:46 +00:00
private void skipSprite(int width, int height, InputStream inputStream) throws IOException {
final int nbLines = (height + 1) / 2;
inputStream.skip(nbLines * width);
}
2018-06-12 20:50:45 +00:00
private String readSprite(int width, int height, InputStream inputStream) throws IOException {
final int nbLines = (height + 1) / 2;
2018-07-27 21:56:46 +00:00
final StringBuilder result = new StringBuilder();
2018-06-12 20:50:45 +00:00
int line = 0;
for (int j = 0; j < nbLines; j++) {
final StringBuilder sb1 = new StringBuilder();
final StringBuilder sb2 = new StringBuilder();
for (int i = 0; i < width; i++) {
final int b = inputStream.read();
final int b1 = (b & 0xF0) >> 4;
final int b2 = (b & 0x0F);
sb1.append(toHexString(b1));
sb2.append(toHexString(b2));
}
result.append(sb1.toString());
result.append("\n");
line++;
if (line < height) {
result.append(sb2.toString());
result.append("\n");
line++;
}
}
return result.toString();
}
private String toHexString(final int b) {
return Integer.toHexString(b).toUpperCase();
}
private boolean isSpriteLine(String s) {
return s.trim().startsWith("sprite") && s.trim().endsWith("{");
}
private static DataInputStream getDataStream(String name) throws IOException {
final InputStream raw = getInternalInputStream(name, "-abx.repx");
2022-08-17 17:34:24 +00:00
if (raw == null)
2018-06-12 20:50:45 +00:00
return null;
2022-08-17 17:34:24 +00:00
2018-06-12 20:50:45 +00:00
return new DataInputStream(new BrotliInputStream(raw));
}
private DataInputStream getDataStream() throws IOException {
return getDataStream(name);
}
private InputStream getSpriteStream() throws IOException {
final InputStream raw = getInternalInputStream(name, "-dex.repx");
2022-08-17 17:34:24 +00:00
if (raw == null)
2018-06-12 20:50:45 +00:00
return null;
2022-11-19 14:57:30 +00:00
return new BrotliInputStream(raw);
}
2022-08-17 17:34:24 +00:00
2022-11-19 14:57:30 +00:00
private InputStream getDataImagePngBase64() throws IOException {
final InputStream raw = getInternalInputStream(name, "-ghx.repx");
if (raw == null)
return null;
2018-06-12 20:50:45 +00:00
return new BrotliInputStream(raw);
}
private static InputStream getInternalInputStream(String fullname, String extension) throws FileNotFoundException {
final String path = "stdlib/" + fullname + extension;
InputStream result = Stdlib.class.getResourceAsStream("/" + path);
if (result == null)
result = new BufferedInputStream(new FileInputStream(path));
return result;
2018-06-12 20:50:45 +00:00
}
public static void extractStdLib() throws IOException {
for (String name : getAll()) {
final Stdlib folder = Stdlib.retrieve(name);
2020-02-18 21:24:31 +00:00
folder.extractMeFull();
2018-06-12 20:50:45 +00:00
}
}
private static Collection<String> getAll() throws IOException {
2021-05-14 08:42:57 +00:00
final Set<String> result = new TreeSet<>();
2018-06-12 20:50:45 +00:00
final InputStream home = getInternalInputStream("home", ".repx");
if (home == null)
throw new IOException("Cannot access to /stdlib/*.repx files");
2018-06-12 20:50:45 +00:00
final BufferedReader br = new BufferedReader(new InputStreamReader(home));
String name;
2022-08-17 17:34:24 +00:00
while ((name = br.readLine()) != null)
2018-06-12 20:50:45 +00:00
result.add(name);
2022-08-17 17:34:24 +00:00
2018-06-12 20:50:45 +00:00
return Collections.unmodifiableCollection(result);
}
2020-02-18 21:24:31 +00:00
private void extractMeFull() throws IOException {
2018-06-12 20:50:45 +00:00
final DataInputStream dataStream = getDataStream();
2022-08-17 17:34:24 +00:00
if (dataStream == null)
2018-06-12 20:50:45 +00:00
return;
2022-08-17 17:34:24 +00:00
2018-06-12 20:50:45 +00:00
dataStream.readUTF();
final InputStream spriteStream = getSpriteStream();
try {
while (true) {
final String filename = dataStream.readUTF();
2022-08-17 17:34:24 +00:00
if (filename.equals(SEPARATOR))
2018-06-12 20:50:45 +00:00
return;
2022-08-17 17:34:24 +00:00
2020-05-30 15:20:23 +00:00
final SFile f = new SFile("stdlib/" + name + "/" + filename + ".puml");
2018-06-12 20:50:45 +00:00
f.getParentFile().mkdirs();
2020-05-30 15:20:23 +00:00
final PrintWriter fos = f.createPrintWriter();
2018-06-12 20:50:45 +00:00
while (true) {
final String s = dataStream.readUTF();
2022-08-17 17:34:24 +00:00
if (s.equals(SEPARATOR))
2018-06-12 20:50:45 +00:00
break;
2022-08-17 17:34:24 +00:00
2018-06-12 20:50:45 +00:00
fos.println(s);
if (isSpriteLine(s)) {
final Matcher m = sizePattern.matcher(s);
final boolean ok = m.find();
2022-08-17 17:34:24 +00:00
if (ok == false)
2018-06-12 20:50:45 +00:00
throw new IOException(s);
2022-08-17 17:34:24 +00:00
2018-06-12 20:50:45 +00:00
final int width = Integer.parseInt(m.group(1));
final int height = Integer.parseInt(m.group(2));
final String sprite = readSprite(width, height, spriteStream);
fos.println(sprite);
fos.println("}");
}
}
fos.close();
}
} finally {
dataStream.close();
spriteStream.close();
}
}
2020-02-18 21:24:31 +00:00
public List<String> extractAllSprites() throws IOException {
2021-05-14 08:42:57 +00:00
final List<String> result = new ArrayList<>();
2020-02-18 21:24:31 +00:00
final DataInputStream dataStream = getDataStream();
2022-08-17 17:34:24 +00:00
if (dataStream == null)
2020-02-18 21:24:31 +00:00
return Collections.unmodifiableList(result);
2022-08-17 17:34:24 +00:00
2020-02-18 21:24:31 +00:00
dataStream.readUTF();
final InputStream spriteStream = getSpriteStream();
try {
while (true) {
final String filename = dataStream.readUTF();
2022-08-17 17:34:24 +00:00
if (filename.equals(SEPARATOR))
2020-02-18 21:24:31 +00:00
return Collections.unmodifiableList(result);
2022-08-17 17:34:24 +00:00
2020-02-18 21:24:31 +00:00
while (true) {
final String s = dataStream.readUTF();
2022-08-17 17:34:24 +00:00
if (s.equals(SEPARATOR))
2020-02-18 21:24:31 +00:00
break;
2022-08-17 17:34:24 +00:00
2020-02-18 21:24:31 +00:00
if (isSpriteLine(s)) {
final Matcher m = sizePattern.matcher(s);
final boolean ok = m.find();
2022-08-17 17:34:24 +00:00
if (ok == false)
2020-02-18 21:24:31 +00:00
throw new IOException(s);
2022-08-17 17:34:24 +00:00
2020-02-18 21:24:31 +00:00
final int width = Integer.parseInt(m.group(1));
final int height = Integer.parseInt(m.group(2));
final String sprite = readSprite(width, height, spriteStream);
2022-08-17 17:34:24 +00:00
if (s.contains("_LARGE") == false)
2020-02-18 21:24:31 +00:00
result.add(s + "\n" + sprite + "}");
2022-08-17 17:34:24 +00:00
2020-02-18 21:24:31 +00:00
}
}
}
} finally {
dataStream.close();
spriteStream.close();
}
}
2018-06-12 20:50:45 +00:00
public static void addInfoVersion(List<String> strings, boolean details) {
try {
for (String name : getAll()) {
final Stdlib folder = Stdlib.retrieve(name);
if (details) {
strings.add("<b>" + name);
strings.add("Version " + folder.getVersion());
strings.add("Delivered by " + folder.getSource());
strings.add(" ");
} else {
strings.add("* " + name + " (Version " + folder.getVersion() + ")");
}
}
} catch (IOException e) {
Log.error("Error " + e);
return;
}
}
private String getVersion() {
return info.get("VERSION");
}
private String getSource() {
return info.get("SOURCE");
}
public static void printStdLib() {
2021-05-14 08:42:57 +00:00
final List<String> print = new ArrayList<>();
2018-06-12 20:50:45 +00:00
addInfoVersion(print, true);
2022-08-17 17:34:24 +00:00
for (String s : print)
2018-06-12 20:50:45 +00:00
System.out.println(s.replace("<b>", ""));
2022-08-17 17:34:24 +00:00
2018-06-12 20:50:45 +00:00
}
// ::done
2018-06-12 20:50:45 +00:00
}