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

209 lines
6.4 KiB
Java
Raw Normal View History

2010-11-15 20:35:36 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
* (C) Copyright 2009, Arnaud Roques
*
* 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
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 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.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* Original Author: Arnaud Roques
*
2011-08-08 17:48:29 +00:00
* Revision $Revision: 6622 $
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;
import net.sourceforge.plantuml.graphic.GraphicStrings;
public class PSystemError extends AbstractPSystem {
private final List<String> htmlStrings = new ArrayList<String>();
private final List<String> plainStrings = new ArrayList<String>();
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
appendSource(higherErrorPosition);
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
}
2011-08-08 17:48:29 +00:00
public void exportDiagram(OutputStream os, StringBuilder cmap, int index, FileFormatOption fileFormat)
throws IOException {
2010-11-15 20:35:36 +00:00
getPngError().writeImage(os, getMetadata(), fileFormat);
}
public GraphicStrings getPngError() throws IOException {
return new GraphicStrings(htmlStrings);
}
2011-01-13 21:52:37 +00:00
private void appendSource(int position) {
2010-11-15 20:35:36 +00:00
final int limit = 4;
int start;
final int skip = position - limit + 1;
if (skip <= 0) {
start = 0;
} else {
if (skip == 1) {
htmlStrings.add("... (skipping 1 line) ...");
plainStrings.add("... (skipping 1 line) ...");
} else {
htmlStrings.add("... (skipping " + skip + " lines) ...");
plainStrings.add("... (skipping " + skip + " lines) ...");
}
start = position - limit + 1;
}
for (int i = start; i < position; i++) {
htmlStrings.add(StringUtils.hideComparatorCharacters(getSource().getLine(i)));
plainStrings.add(getSource().getLine(i));
}
final String errorLine = getSource().getLine(position);
htmlStrings.add("<w:red>" + StringUtils.hideComparatorCharacters(errorLine) + "</w>");
plainStrings.add(StringUtils.hideComparatorCharacters(errorLine));
final StringBuilder underscore = new StringBuilder();
for (int i = 0; i < errorLine.length(); i++) {
underscore.append("^");
}
plainStrings.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) {
htmlStrings.add(" <color:red>" + er);
plainStrings.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) {
htmlStrings.add(" <color:white><i>" + s);
} else {
htmlStrings.add("<color:white>" + StringUtils.hideComparatorCharacters(s));
}
first = false;
}
}
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;
}
public String getDescription() {
return "(Error)";
}
public void print(PrintStream ps) {
2011-08-08 17:48:29 +00:00
synchronized (ps) {
for (String s : plainStrings) {
ps.println(StringUtils.showComparatorCharacters(s));
}
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
}
}