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

297 lines
9.0 KiB
Java
Raw Normal View History

2010-11-15 20:35:36 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2015-04-07 18:18:37 +00:00
* (C) Copyright 2009-2014, Arnaud Roques
2010-11-15 20:35:36 +00:00
*
* Project Info: http://plantuml.sourceforge.net
*
* 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.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* Original Author: Arnaud Roques
*
2015-04-07 18:18:37 +00:00
* Revision $Revision: 15848 $
2010-11-15 20:35:36 +00:00
*/
package net.sourceforge.plantuml;
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.DiagramDescriptionImpl;
import net.sourceforge.plantuml.core.ImageData;
import net.sourceforge.plantuml.core.UmlSource;
2010-11-15 20:35:36 +00:00
import net.sourceforge.plantuml.graphic.GraphicStrings;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.ugraphic.ColorMapperIdentity;
import net.sourceforge.plantuml.ugraphic.ImageBuilder;
import net.sourceforge.plantuml.ugraphic.txt.UGraphicTxt;
2010-11-15 20:35:36 +00:00
public class PSystemError extends AbstractPSystem {
2015-04-07 18:18:37 +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
private final int higherErrorPosition;
2011-01-13 21:52:37 +00:00
private final List<ErrorUml> printedErrors;
2010-11-15 20:35:36 +00:00
2011-01-13 21:52:37 +00:00
public PSystemError(UmlSource source, List<ErrorUml> all) {
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
}
}
2011-01-13 21:52:37 +00:00
public PSystemError(UmlSource source, ErrorUml singleError) {
this(source, Collections.singletonList(singleError));
2010-11-15 20:35:36 +00:00
}
2013-12-10 19:36:50 +00:00
public ImageData exportDiagram(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();
final GraphicStrings result = GraphicStrings.createDefault(getHtmlStrings(useRed), useRed);
final ImageBuilder imageBuilder = new ImageBuilder(new ColorMapperIdentity(), 1.0, result.getBackcolor(),
getMetadata(), null, 0, 0, null, false);
imageBuilder.addUDrawable(result);
return imageBuilder.writeImageTOBEMOVED(fileFormat.getFileFormat(), os);
2010-11-15 20:35:36 +00:00
}
2015-04-07 18:18:37 +00:00
private List<String> getTextStrings() {
final List<String> result = new ArrayList<String>();
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-04-07 18:18:37 +00:00
return result;
}
private List<String> getHtmlStrings(boolean useRed) {
final List<String> htmlStrings = new ArrayList<String>();
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>");
}
// final StringBuilder underscore = new StringBuilder();
// for (int i = 0; i < errorLine.length(); i++) {
// underscore.append("^");
// }
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;
}
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() {
return new DiagramDescriptionImpl("(Error)", getClass());
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');
for (CharSequence t : getTitle()) {
sb.append(t);
sb.append('\n');
}
sb.append('\n');
for (String s : getSuggest()) {
sb.append(s);
sb.append('\n');
}
return sb.toString();
}
2010-11-15 20:35:36 +00:00
}