plantuml/src/net/sourceforge/plantuml/utils/BlocLines.java

380 lines
10 KiB
Java
Raw Normal View History

2015-06-20 10:54:49 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2022-03-07 19:33:46 +00:00
* (C) Copyright 2009-2023, Arnaud Roques
2015-06-20 10:54:49 +00:00
*
2016-03-06 16:47:34 +00:00
* Project Info: http://plantuml.com
2015-06-20 10:54:49 +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
*
2015-06-20 10:54:49 +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
*
*
*/
2022-12-17 11:01:10 +00:00
package net.sourceforge.plantuml.utils;
2015-06-20 10:54:49 +00:00
2019-01-16 18:34:41 +00:00
import java.io.BufferedReader;
import java.io.IOException;
2019-04-21 20:40:01 +00:00
import java.io.InputStream;
import java.io.InputStreamReader;
2015-06-20 10:54:49 +00:00
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
2017-04-26 17:48:37 +00:00
import net.sourceforge.plantuml.BackSlash;
2022-12-17 11:01:10 +00:00
import net.sourceforge.plantuml.command.MultilinesStrategy;
2015-06-20 10:54:49 +00:00
import net.sourceforge.plantuml.cucadiagram.Display;
2020-05-30 15:20:23 +00:00
import net.sourceforge.plantuml.security.SFile;
2021-02-02 10:12:15 +00:00
import net.sourceforge.plantuml.ugraphic.color.NoSuchColorException;
2015-06-20 10:54:49 +00:00
2019-03-29 22:14:07 +00:00
public class BlocLines implements Iterable<StringLocated> {
private List<StringLocated> lines;
2015-09-06 17:28:59 +00:00
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
for (StringLocated line : lines) {
sb.append("<<<");
sb.append(line);
sb.append(">>>");
}
return sb.toString();
2015-09-06 17:28:59 +00:00
}
2015-06-20 10:54:49 +00:00
2020-05-30 15:20:23 +00:00
public static BlocLines load(SFile f, LineLocation location) throws IOException {
final BufferedReader br = f.openBufferedReader();
2022-07-27 13:51:02 +00:00
if (br == null)
2020-05-30 15:20:23 +00:00
return null;
2022-07-27 13:51:02 +00:00
2019-04-21 20:40:01 +00:00
return loadInternal(br, location);
}
public static BlocLines load(InputStream is, LineLocation location) throws IOException {
final BufferedReader br = new BufferedReader(new InputStreamReader(is));
return loadInternal(br, location);
}
public static BlocLines from(List<StringLocated> lines) {
return new BlocLines(lines);
}
2019-04-21 20:40:01 +00:00
private static BlocLines loadInternal(final BufferedReader br, LineLocation location) throws IOException {
2021-05-14 08:42:57 +00:00
final List<StringLocated> result = new ArrayList<>();
2019-01-16 18:34:41 +00:00
String s;
try {
2022-07-27 13:51:02 +00:00
while ((s = br.readLine()) != null)
result.add(new StringLocated(s, location));
} finally {
br.close();
2019-01-16 18:34:41 +00:00
}
return new BlocLines(result);
}
2019-03-29 22:14:07 +00:00
private BlocLines(List<StringLocated> lines) {
this.lines = Collections.unmodifiableList(lines);
2015-06-20 10:54:49 +00:00
}
2015-08-05 20:17:01 +00:00
2021-02-02 10:12:15 +00:00
public Display toDisplay() throws NoSuchColorException {
2019-03-29 22:14:07 +00:00
return Display.createFoo(lines);
2015-06-20 10:54:49 +00:00
}
public static BlocLines single(StringLocated single) {
2021-05-14 08:42:57 +00:00
final List<StringLocated> result = new ArrayList<>();
2019-03-29 22:14:07 +00:00
result.add(single);
return new BlocLines(result);
2015-06-20 10:54:49 +00:00
}
2015-08-05 20:17:01 +00:00
2019-03-29 22:14:07 +00:00
public static BlocLines singleString(String single) {
2021-05-14 08:42:57 +00:00
final List<StringLocated> result = new ArrayList<>();
2019-03-29 22:14:07 +00:00
result.add(new StringLocated(single, null));
return new BlocLines(result);
}
2020-02-18 21:24:31 +00:00
public static BlocLines fromArray(String[] array) {
2021-05-14 08:42:57 +00:00
final List<StringLocated> result = new ArrayList<>();
2022-07-27 13:51:02 +00:00
for (String single : array)
2020-02-18 21:24:31 +00:00
result.add(new StringLocated(single, null));
2022-07-27 13:51:02 +00:00
2020-02-18 21:24:31 +00:00
return new BlocLines(result);
}
2019-03-29 22:14:07 +00:00
public static BlocLines getWithNewlines(String s) {
2021-05-14 08:42:57 +00:00
final List<StringLocated> result = new ArrayList<>();
2022-07-27 13:51:02 +00:00
for (String cs : BackSlash.getWithNewlines(s))
2019-03-29 22:14:07 +00:00
result.add(new StringLocated(cs, null));
2022-07-27 13:51:02 +00:00
2019-03-29 22:14:07 +00:00
return new BlocLines(result);
2015-06-20 10:54:49 +00:00
}
2022-03-19 12:48:23 +00:00
public static BlocLines create() {
return new BlocLines(new ArrayList<StringLocated>());
2015-06-20 10:54:49 +00:00
}
public BlocLines add(StringLocated s) {
2021-05-14 08:42:57 +00:00
final List<StringLocated> copy = new ArrayList<>(lines);
2015-06-20 10:54:49 +00:00
copy.add(s);
return new BlocLines(copy);
}
2019-03-29 22:14:07 +00:00
public BlocLines addString(String s) {
2021-05-14 08:42:57 +00:00
final List<StringLocated> copy = new ArrayList<>(lines);
2019-03-29 22:14:07 +00:00
copy.add(new StringLocated(s, null));
return new BlocLines(copy);
}
public List<String> getLinesAsStringForSprite() {
2021-05-14 08:42:57 +00:00
final List<String> result = new ArrayList<>();
2022-07-27 13:51:02 +00:00
for (StringLocated s : lines)
2019-03-29 22:14:07 +00:00
result.add(s.getString());
2022-07-27 13:51:02 +00:00
2019-03-29 22:14:07 +00:00
return result;
2015-06-20 10:54:49 +00:00
}
public int size() {
return lines.size();
}
public StringLocated getAt(int i) {
2015-06-20 10:54:49 +00:00
return lines.get(i);
}
public StringLocated getFirst() {
2022-07-27 13:51:02 +00:00
if (lines.size() == 0)
2017-12-11 21:02:10 +00:00
return null;
2022-07-27 13:51:02 +00:00
2015-06-20 10:54:49 +00:00
return lines.get(0);
}
public StringLocated getLast() {
2015-06-20 10:54:49 +00:00
return lines.get(lines.size() - 1);
}
public BlocLines cleanList(MultilinesStrategy strategy) {
2021-05-14 08:42:57 +00:00
final List<StringLocated> copy = new ArrayList<>(lines);
2015-06-20 10:54:49 +00:00
strategy.cleanList(copy);
return new BlocLines(copy);
}
public BlocLines trim() {
2021-05-14 08:42:57 +00:00
final List<StringLocated> copy = new ArrayList<>(lines);
2015-06-20 10:54:49 +00:00
for (int i = 0; i < copy.size(); i++) {
2019-03-29 22:14:07 +00:00
final StringLocated s = copy.get(i);
copy.set(i, s.getTrimmed());
2015-06-20 10:54:49 +00:00
}
return new BlocLines(copy);
}
public BlocLines removeEmptyLines() {
2021-05-14 08:42:57 +00:00
final List<StringLocated> copy = new ArrayList<>(lines);
2022-07-27 13:51:02 +00:00
for (final Iterator<StringLocated> it = copy.iterator(); it.hasNext();)
if (it.next().getString().length() == 0)
it.remove();
2022-07-27 13:51:02 +00:00
2015-06-20 10:54:49 +00:00
return new BlocLines(copy);
}
// public BlocLines trimRight() {
2021-05-14 08:42:57 +00:00
// final List<StringLocated> copy = new ArrayList<>(lines);
// for (int i = 0; i < copy.size(); i++) {
// final StringLocated s = copy.get(i);
// copy.set(i, s.getTrimmedRight());
// }
// return new BlocLines(copy);
// }
2015-06-20 10:54:49 +00:00
public BlocLines removeEmptyColumns() {
2022-07-27 13:51:02 +00:00
if (firstColumnRemovable(lines) == false)
2015-06-20 10:54:49 +00:00
return this;
2022-07-27 13:51:02 +00:00
2021-05-14 08:42:57 +00:00
final List<StringLocated> copy = new ArrayList<>(lines);
2015-06-20 10:54:49 +00:00
do {
for (int i = 0; i < copy.size(); i++) {
2019-03-29 22:14:07 +00:00
final StringLocated s = copy.get(i);
2022-07-27 13:51:02 +00:00
if (s.getString().length() > 0)
2020-03-03 22:29:34 +00:00
copy.set(i, s.substring(1, s.getString().length()));
2022-07-27 13:51:02 +00:00
2015-06-20 10:54:49 +00:00
}
} while (firstColumnRemovable(copy));
return new BlocLines(copy);
}
2019-03-29 22:14:07 +00:00
private static boolean firstColumnRemovable(List<StringLocated> data) {
2015-06-20 10:54:49 +00:00
boolean allEmpty = true;
2019-03-29 22:14:07 +00:00
for (StringLocated s : data) {
2022-07-27 13:51:02 +00:00
if (s.getString().length() == 0)
2015-06-20 10:54:49 +00:00
continue;
2022-07-27 13:51:02 +00:00
2015-06-20 10:54:49 +00:00
allEmpty = false;
2019-03-29 22:14:07 +00:00
final char c = s.getString().charAt(0);
2022-07-27 13:51:02 +00:00
if (c != ' ' && c != '\t')
2015-06-20 10:54:49 +00:00
return false;
2022-07-27 13:51:02 +00:00
2015-06-20 10:54:49 +00:00
}
return allEmpty == false;
}
public char getLastChar() {
2019-03-29 22:14:07 +00:00
final StringLocated s = lines.get(lines.size() - 1);
return s.getString().charAt(s.getString().length() - 1);
2015-06-20 10:54:49 +00:00
}
public BlocLines removeStartingAndEnding(String data, int removeAtEnd) {
2022-07-27 13:51:02 +00:00
if (lines.size() == 0)
2015-06-20 10:54:49 +00:00
return this;
2022-07-27 13:51:02 +00:00
2021-05-14 08:42:57 +00:00
final List<StringLocated> copy = new ArrayList<>(lines);
2019-03-29 22:14:07 +00:00
copy.set(0, new StringLocated(data, null));
2015-06-20 10:54:49 +00:00
final int n = copy.size() - 1;
2019-03-29 22:14:07 +00:00
final StringLocated s = copy.get(n);
copy.set(n, s.substring(0, s.getString().length() - removeAtEnd));
return new BlocLines(copy);
}
public BlocLines overrideLastLine(String last) {
2022-07-27 13:51:02 +00:00
if (lines.size() == 0)
return this;
2022-07-27 13:51:02 +00:00
2021-05-14 08:42:57 +00:00
final List<StringLocated> copy = new ArrayList<>(lines);
final int n = copy.size() - 1;
final StringLocated currentLast = copy.get(n);
copy.set(n, new StringLocated(last, currentLast.getLocation()));
2015-06-20 10:54:49 +00:00
return new BlocLines(copy);
}
2017-06-05 11:27:21 +00:00
public BlocLines toSingleLineWithHiddenNewLine() {
2015-06-20 10:54:49 +00:00
final StringBuilder sb = new StringBuilder();
2019-03-29 22:14:07 +00:00
for (StringLocated line : lines) {
sb.append(line.getString());
2017-04-26 17:48:37 +00:00
sb.append(BackSlash.hiddenNewLine());
2015-06-20 10:54:49 +00:00
}
2019-03-29 22:14:07 +00:00
return singleString(sb.substring(0, sb.length() - 1).toString());
2015-06-20 10:54:49 +00:00
}
public BlocLines trimSmart(int referenceLine) {
2022-07-27 13:51:02 +00:00
if (lines.size() <= referenceLine)
2015-06-20 10:54:49 +00:00
return this;
2022-07-27 13:51:02 +00:00
2021-05-14 08:42:57 +00:00
final List<StringLocated> copy = new ArrayList<>(lines);
2019-03-29 22:14:07 +00:00
final int nbStartingSpace = nbStartingSpace(copy.get(referenceLine).getString());
2015-06-20 10:54:49 +00:00
for (int i = referenceLine; i < copy.size(); i++) {
2019-03-29 22:14:07 +00:00
final StringLocated s = copy.get(i);
2015-06-20 10:54:49 +00:00
copy.set(i, removeStartingSpaces(s, nbStartingSpace));
}
return new BlocLines(copy);
}
private static int nbStartingSpace(CharSequence s) {
int nb = 0;
2022-07-27 13:51:02 +00:00
while (nb < s.length() && isSpaceOrTab(s.charAt(nb)))
2015-06-20 10:54:49 +00:00
nb++;
2022-07-27 13:51:02 +00:00
2015-06-20 10:54:49 +00:00
return nb;
}
private static boolean isSpaceOrTab(char c) {
return c == ' ' || c == '\t';
}
2019-03-29 22:14:07 +00:00
private static StringLocated removeStartingSpaces(StringLocated arg, int nbStartingSpace) {
2022-10-18 20:57:44 +00:00
if (arg.getString().length() == 0)
2015-06-20 10:54:49 +00:00
return arg;
2022-10-18 20:57:44 +00:00
2015-06-20 10:54:49 +00:00
int i = 0;
2022-10-18 20:57:44 +00:00
while (i < nbStartingSpace && i < arg.getString().length() && isSpaceOrTab(arg.getString().charAt(i)))
2015-06-20 10:54:49 +00:00
i++;
2022-10-18 20:57:44 +00:00
if (i == 0)
2015-06-20 10:54:49 +00:00
return arg;
2022-10-18 20:57:44 +00:00
2020-03-03 22:29:34 +00:00
return arg.substring(i, arg.getString().length());
2015-06-20 10:54:49 +00:00
}
2019-01-16 18:34:41 +00:00
public BlocLines subExtract(int margeStart, int margeEnd) {
2021-05-14 08:42:57 +00:00
List<StringLocated> copy = new ArrayList<>(lines);
2019-01-16 18:34:41 +00:00
copy = copy.subList(margeStart, copy.size() - margeEnd);
2015-06-20 10:54:49 +00:00
return new BlocLines(copy);
}
2019-01-16 18:34:41 +00:00
public BlocLines subList(int start, int end) {
return new BlocLines(lines.subList(start, end));
}
2019-03-29 22:14:07 +00:00
public Iterator<StringLocated> iterator() {
2015-06-20 10:54:49 +00:00
return lines.iterator();
}
2018-06-12 20:50:45 +00:00
public BlocLines eventuallyMoveBracket() {
2022-07-27 13:51:02 +00:00
if (size() < 2)
2018-06-12 20:50:45 +00:00
return this;
2022-07-27 13:51:02 +00:00
final String first = getFirst().getTrimmed().getString();
final String second = getAt(1).getTrimmed().getString();
2018-06-12 20:50:45 +00:00
if (first.endsWith("{") == false && second.equals("{")) {
final StringLocated vline = getFirst().append(" {");
2021-05-14 08:42:57 +00:00
final List<StringLocated> result = new ArrayList<>();
2019-09-14 18:12:04 +00:00
result.add(vline);
2018-06-12 20:50:45 +00:00
result.addAll(this.lines.subList(2, this.lines.size()));
return new BlocLines(result);
}
2017-12-11 21:02:10 +00:00
return this;
2015-11-01 18:37:20 +00:00
}
2019-01-16 18:34:41 +00:00
2019-09-14 18:12:04 +00:00
public BlocLines eventuallyMoveAllEmptyBracket() {
2021-05-14 08:42:57 +00:00
final List<StringLocated> result = new ArrayList<>();
2022-07-27 13:51:02 +00:00
for (StringLocated line : lines)
2019-09-14 18:12:04 +00:00
if (line.getTrimmed().toString().equals("{")) {
if (result.size() > 0) {
final int pos = result.size() - 1;
StringLocated last = result.get(pos);
result.set(pos, last.append(" {"));
}
} else {
result.add(line);
}
2022-07-27 13:51:02 +00:00
2019-09-14 18:12:04 +00:00
return new BlocLines(result);
}
2022-10-18 20:57:44 +00:00
public BlocLines removeFewChars(int nb) {
final List<StringLocated> result = new ArrayList<>();
result.add(getFirst().substring(nb).getTrimmed());
result.addAll(this.lines.subList(1, this.lines.size() - 1));
final StringLocated last = getLast();
result.add(last.substring(0, last.length() - nb).getTrimmed());
return new BlocLines(result);
}
2022-12-17 11:01:10 +00:00
public CharInspector inspector() {
2023-01-09 19:13:37 +00:00
return new CharInspectorImpl(this, false);
}
public CharInspector inspectorWithNewlines() {
return new CharInspectorImpl(this, true);
2022-12-17 11:01:10 +00:00
}
2015-06-20 10:54:49 +00:00
}