1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-03 09:00:48 +00:00
plantuml/src/net/sourceforge/plantuml/PSystemError.java

388 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
*
* 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
*
*/
package net.sourceforge.plantuml;
2016-12-21 22:10:29 +00:00
import java.awt.geom.Dimension2D;
2010-11-15 20:35:36 +00:00
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.api.ImageDataSimple;
import net.sourceforge.plantuml.asciiart.UmlCharArea;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.core.DiagramDescription;
import net.sourceforge.plantuml.core.ImageData;
import net.sourceforge.plantuml.core.UmlSource;
2016-12-21 22:10:29 +00:00
import net.sourceforge.plantuml.eggs.PSystemEmpty;
2017-03-12 17:22:02 +00:00
import net.sourceforge.plantuml.graphic.GraphicPosition;
2010-11-15 20:35:36 +00:00
import net.sourceforge.plantuml.graphic.GraphicStrings;
2016-12-21 22:10:29 +00:00
import net.sourceforge.plantuml.graphic.UDrawable;
2016-11-18 21:12:09 +00:00
import net.sourceforge.plantuml.svek.TextBlockBackcolored;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.ugraphic.ColorMapperIdentity;
import net.sourceforge.plantuml.ugraphic.ImageBuilder;
2016-12-21 22:10:29 +00:00
import net.sourceforge.plantuml.ugraphic.UChangeBackColor;
import net.sourceforge.plantuml.ugraphic.UGraphic;
import net.sourceforge.plantuml.ugraphic.URectangle;
import net.sourceforge.plantuml.ugraphic.UTranslate;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.ugraphic.txt.UGraphicTxt;
2010-11-15 20:35:36 +00:00
public class PSystemError extends AbstractPSystem {
private final int higherErrorPosition;
2011-01-13 21:52:37 +00:00
private final List<ErrorUml> printedErrors;
2015-05-31 18:56:03 +00:00
private final List<String> debugLines = new ArrayList<String>();
public PSystemError(UmlSource source, ErrorUml singleError, List<String> debugLines) {
this(source, Collections.singletonList(singleError), debugLines);
}
2010-11-15 20:35:36 +00:00
2015-05-31 18:56:03 +00:00
private PSystemError(UmlSource source, List<ErrorUml> all, List<String> debugLines) {
2010-11-15 20:35:36 +00:00
this.setSource(source);
2011-01-13 21:52:37 +00:00
final int higherErrorPositionExecution = getHigherErrorPosition(ErrorUmlType.EXECUTION_ERROR, all);
final int higherErrorPositionSyntax = getHigherErrorPosition(ErrorUmlType.SYNTAX_ERROR, all);
2011-01-09 19:00:05 +00:00
if (higherErrorPositionExecution == Integer.MIN_VALUE && higherErrorPositionSyntax == Integer.MIN_VALUE) {
throw new IllegalStateException();
}
if (higherErrorPositionExecution >= higherErrorPositionSyntax) {
higherErrorPosition = higherErrorPositionExecution;
2011-01-13 21:52:37 +00:00
printedErrors = getErrorsAt(higherErrorPositionExecution, ErrorUmlType.EXECUTION_ERROR, all);
2010-11-15 20:35:36 +00:00
} else {
2011-01-09 19:00:05 +00:00
assert higherErrorPositionSyntax > higherErrorPositionExecution;
higherErrorPosition = higherErrorPositionSyntax;
2011-01-13 21:52:37 +00:00
printedErrors = getErrorsAt(higherErrorPositionSyntax, ErrorUmlType.SYNTAX_ERROR, all);
2010-11-15 20:35:36 +00:00
}
2015-05-31 18:56:03 +00:00
if (debugLines != null) {
this.debugLines.addAll(debugLines);
}
2010-11-15 20:35:36 +00:00
}
2015-05-31 18:56:03 +00:00
private String getSuggestColor(boolean useRed) {
if (useRed) {
return "black";
}
return "white";
}
private String getRed(boolean useRed) {
if (useRed) {
return "#CD0A0A";
}
return "red";
2010-11-15 20:35:36 +00:00
}
2016-12-01 20:29:25 +00:00
@Override
final protected ImageData exportDiagramNow(OutputStream os, int num, FileFormatOption fileFormat)
throws IOException {
2015-04-07 18:18:37 +00:00
if (fileFormat.getFileFormat() == FileFormat.ATXT || fileFormat.getFileFormat() == FileFormat.UTXT) {
final UGraphicTxt ugt = new UGraphicTxt();
final UmlCharArea area = ugt.getCharArea();
area.drawStringsLR(getTextStrings(), 0, 0);
area.print(new PrintStream(os));
return new ImageDataSimple(1, 1);
}
final boolean useRed = fileFormat.isUseRedForError();
2016-11-18 21:12:09 +00:00
final TextBlockBackcolored result = GraphicStrings.createForError(getHtmlStrings(useRed), useRed);
2016-12-21 22:10:29 +00:00
final UDrawable udrawable;
2015-04-07 18:18:37 +00:00
final ImageBuilder imageBuilder = new ImageBuilder(new ColorMapperIdentity(), 1.0, result.getBackcolor(),
getMetadata(), null, 0, 0, null, false);
2016-12-21 22:10:29 +00:00
if (getSource().getTotalLineCount() < 4) {
2017-03-12 17:22:02 +00:00
final TextBlockBackcolored welcome = new PSystemEmpty(GraphicPosition.BACKGROUND_CORNER_TOP_RIGHT).getGraphicStrings();
2016-12-21 22:10:29 +00:00
udrawable = new UDrawable() {
public void drawU(UGraphic ug) {
final Dimension2D dim1 = welcome.calculateDimension(ug.getStringBounder());
final Dimension2D dim2 = result.calculateDimension(ug.getStringBounder());
final URectangle frame = new URectangle(Math.max(dim1.getWidth(), dim2.getWidth()),
dim1.getHeight());
ug.apply(new UChangeBackColor(welcome.getBackcolor())).apply(new UTranslate(1, 1)).draw(frame);
welcome.drawU(ug);
ug = ug.apply(new UTranslate(0, dim1.getHeight()));
result.drawU(ug);
}
};
} else {
udrawable = result;
}
imageBuilder.setUDrawable(udrawable);
2015-05-31 18:56:03 +00:00
return imageBuilder.writeImageTOBEMOVED(fileFormat, os);
2010-11-15 20:35:36 +00:00
}
2015-04-07 18:18:37 +00:00
private List<String> getTextStrings() {
2015-06-20 10:54:49 +00:00
final List<String> result = new ArrayList<String>(getStack());
if (result.size() > 0) {
result.add(" ");
}
2015-04-07 18:18:37 +00:00
2010-11-15 20:35:36 +00:00
final int limit = 4;
int start;
2015-04-07 18:18:37 +00:00
final int skip = higherErrorPosition - limit + 1;
2010-11-15 20:35:36 +00:00
if (skip <= 0) {
start = 0;
} else {
if (skip == 1) {
2015-04-07 18:18:37 +00:00
result.add("... (skipping 1 line) ...");
2010-11-15 20:35:36 +00:00
} else {
2015-04-07 18:18:37 +00:00
result.add("... (skipping " + skip + " lines) ...");
2010-11-15 20:35:36 +00:00
}
2015-04-07 18:18:37 +00:00
start = higherErrorPosition - limit + 1;
2010-11-15 20:35:36 +00:00
}
2015-04-07 18:18:37 +00:00
for (int i = start; i < higherErrorPosition; i++) {
result.add(getSource().getLine(i));
}
final String errorLine = getSource().getLine(higherErrorPosition);
final String err = StringUtils.hideComparatorCharacters(errorLine);
if (StringUtils.isNotEmpty(err)) {
result.add(err);
2010-11-15 20:35:36 +00:00
}
final StringBuilder underscore = new StringBuilder();
for (int i = 0; i < errorLine.length(); i++) {
underscore.append("^");
}
2015-04-07 18:18:37 +00:00
result.add(underscore.toString());
2011-01-23 19:36:52 +00:00
final Collection<String> textErrors = new LinkedHashSet<String>();
2011-01-13 21:52:37 +00:00
for (ErrorUml er : printedErrors) {
2011-01-23 19:36:52 +00:00
textErrors.add(er.getError());
}
for (String er : textErrors) {
2015-04-07 18:18:37 +00:00
result.add(" " + er);
2011-01-13 21:52:37 +00:00
}
2011-08-08 17:48:29 +00:00
boolean first = true;
for (String s : getSuggest()) {
if (first) {
2015-04-07 18:18:37 +00:00
result.add(" " + s);
2011-08-08 17:48:29 +00:00
} else {
2015-04-07 18:18:37 +00:00
result.add(s);
2011-08-08 17:48:29 +00:00
}
first = false;
}
2015-05-31 18:56:03 +00:00
result.addAll(this.debugLines);
2015-04-07 18:18:37 +00:00
return result;
}
2015-06-20 10:54:49 +00:00
private List<String> getStack() {
LineLocation lineLocation = getLineLocation();
final List<String> result = new ArrayList<String>();
if (lineLocation != null) {
append(result, lineLocation);
while (lineLocation.getParent() != null) {
lineLocation = lineLocation.getParent();
append(result, lineLocation);
}
}
return result;
}
public LineLocation getLineLocation() {
for (ErrorUml err : printedErrors) {
if (err.getLineLocation() != null) {
return err.getLineLocation();
}
}
return null;
}
private void append(List<String> result, LineLocation lineLocation) {
if (lineLocation.getDescription() != null) {
result.add("[From " + lineLocation.getDescription() + " (line " + (lineLocation.getPosition() + 1) + ") ]");
}
}
2015-04-07 18:18:37 +00:00
private List<String> getHtmlStrings(boolean useRed) {
2015-06-20 10:54:49 +00:00
final List<String> htmlStrings = new ArrayList<String>(getStack());
if (htmlStrings.size() > 0) {
htmlStrings.add("----");
}
2015-04-07 18:18:37 +00:00
final int limit = 4;
int start;
final int skip = higherErrorPosition - limit + 1;
if (skip <= 0) {
start = 0;
} else {
if (skip == 1) {
htmlStrings.add("... (skipping 1 line) ...");
} else {
htmlStrings.add("... (skipping " + skip + " lines) ...");
}
start = higherErrorPosition - limit + 1;
}
for (int i = start; i < higherErrorPosition; i++) {
htmlStrings.add(StringUtils.hideComparatorCharacters(getSource().getLine(i)));
}
final String errorLine = getSource().getLine(higherErrorPosition);
final String err = StringUtils.hideComparatorCharacters(errorLine);
if (StringUtils.isNotEmpty(err)) {
htmlStrings.add("<w:" + getRed(useRed) + ">" + err + "</w>");
}
2015-05-31 18:56:03 +00:00
// final StringBuilder underscore = new StringBuilder();
// for (int i = 0; i < errorLine.length(); i++) {
// underscore.append("^");
// }
2015-04-07 18:18:37 +00:00
final Collection<String> textErrors = new LinkedHashSet<String>();
for (ErrorUml er : printedErrors) {
textErrors.add(er.getError());
}
for (String er : textErrors) {
htmlStrings.add(" <color:" + getRed(useRed) + ">" + er + "</color>");
}
boolean first = true;
for (String s : getSuggest()) {
if (first) {
htmlStrings.add(" <color:" + getSuggestColor(useRed) + "><i>" + s + "</i></color>");
} else {
htmlStrings.add("<color:" + getSuggestColor(useRed) + ">" + StringUtils.hideComparatorCharacters(s)
+ "</color>");
}
first = false;
}
2015-05-31 18:56:03 +00:00
htmlStrings.addAll(this.debugLines);
2015-04-07 18:18:37 +00:00
return htmlStrings;
2011-08-08 17:48:29 +00:00
}
public List<String> getSuggest() {
2011-01-13 21:52:37 +00:00
boolean suggested = false;
for (ErrorUml er : printedErrors) {
if (er.hasSuggest()) {
suggested = true;
}
}
2011-08-08 17:48:29 +00:00
if (suggested == false) {
return Collections.emptyList();
}
final List<String> result = new ArrayList<String>();
result.add("Did you mean:");
for (ErrorUml er : printedErrors) {
if (er.hasSuggest()) {
result.add(er.getSuggest().getSuggestedLine());
2011-01-13 21:52:37 +00:00
}
2010-11-15 20:35:36 +00:00
}
2011-08-08 17:48:29 +00:00
return Collections.unmodifiableList(result);
2010-11-15 20:35:36 +00:00
}
2011-01-13 21:52:37 +00:00
private Collection<ErrorUml> getErrors(ErrorUmlType type, List<ErrorUml> all) {
2010-11-15 20:35:36 +00:00
final Collection<ErrorUml> result = new LinkedHashSet<ErrorUml>();
2011-01-13 21:52:37 +00:00
for (ErrorUml error : all) {
2010-11-15 20:35:36 +00:00
if (error.getType() == type) {
result.add(error);
}
}
return result;
}
2011-01-13 21:52:37 +00:00
private int getHigherErrorPosition(ErrorUmlType type, List<ErrorUml> all) {
2010-11-15 20:35:36 +00:00
int max = Integer.MIN_VALUE;
2011-01-13 21:52:37 +00:00
for (ErrorUml error : getErrors(type, all)) {
2010-11-15 20:35:36 +00:00
if (error.getPosition() > max) {
max = error.getPosition();
}
}
2011-01-13 21:52:37 +00:00
// if (max == Integer.MIN_VALUE) {
// throw new IllegalStateException();
// }
2010-11-15 20:35:36 +00:00
return max;
}
2011-01-13 21:52:37 +00:00
private List<ErrorUml> getErrorsAt(int position, ErrorUmlType type, List<ErrorUml> all) {
final List<ErrorUml> result = new ArrayList<ErrorUml>();
for (ErrorUml error : getErrors(type, all)) {
2010-11-15 20:35:36 +00:00
if (error.getPosition() == position && StringUtils.isNotEmpty(error.getError())) {
2011-01-13 21:52:37 +00:00
result.add(error);
2010-11-15 20:35:36 +00:00
}
}
return result;
}
2013-12-10 19:36:50 +00:00
public DiagramDescription getDescription() {
2017-03-12 17:22:02 +00:00
return new DiagramDescription("(Error)");
2010-11-15 20:35:36 +00:00
}
public final int getHigherErrorPosition() {
return higherErrorPosition;
}
2011-01-13 21:52:37 +00:00
public final Collection<ErrorUml> getErrorsUml() {
return Collections.unmodifiableCollection(printedErrors);
2010-11-15 20:35:36 +00:00
}
2013-12-10 19:36:50 +00:00
@Override
public String getWarningOrError() {
final StringBuilder sb = new StringBuilder();
sb.append(getDescription());
sb.append('\n');
2016-01-30 12:20:07 +00:00
for (CharSequence t : getTitle().getDisplay()) {
2013-12-10 19:36:50 +00:00
sb.append(t);
sb.append('\n');
}
sb.append('\n');
for (String s : getSuggest()) {
sb.append(s);
sb.append('\n');
}
return sb.toString();
}
2015-05-31 18:56:03 +00:00
public static PSystemError merge(Collection<PSystemError> ps) {
UmlSource source = null;
final List<ErrorUml> errors = new ArrayList<ErrorUml>();
final List<String> debugs = new ArrayList<String>();
for (PSystemError system : ps) {
2015-06-20 10:54:49 +00:00
if (system == null) {
continue;
}
2015-05-31 18:56:03 +00:00
if (system.getSource() != null && source == null) {
source = system.getSource();
}
errors.addAll(system.getErrorsUml());
debugs.addAll(system.debugLines);
if (system.debugLines.size() > 0) {
debugs.add("-");
}
}
if (source == null) {
throw new IllegalStateException();
}
return new PSystemError(source, errors, debugs);
}
2010-11-15 20:35:36 +00:00
}