net.sourceforge.plantuml.PSystemError Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of plantuml Show documentation
Show all versions of plantuml Show documentation
PlantUML is a component that allows to quickly write :
* sequence diagram,
* use case diagram,
* class diagram,
* activity diagram,
* component diagram,
* state diagram
* object diagram
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
* (C) Copyright 2009-2013, 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 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
*
* Revision $Revision: 11296 $
*/
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.core.ImageData;
import net.sourceforge.plantuml.core.UmlSource;
import net.sourceforge.plantuml.graphic.GraphicStrings;
public class PSystemError extends AbstractPSystem {
private final List htmlStrings = new ArrayList();
private final List plainStrings = new ArrayList();
private final int higherErrorPosition;
private final List printedErrors;
public PSystemError(UmlSource source, List all) {
this.setSource(source);
final int higherErrorPositionExecution = getHigherErrorPosition(ErrorUmlType.EXECUTION_ERROR, all);
final int higherErrorPositionSyntax = getHigherErrorPosition(ErrorUmlType.SYNTAX_ERROR, all);
if (higherErrorPositionExecution == Integer.MIN_VALUE && higherErrorPositionSyntax == Integer.MIN_VALUE) {
throw new IllegalStateException();
}
if (higherErrorPositionExecution >= higherErrorPositionSyntax) {
higherErrorPosition = higherErrorPositionExecution;
printedErrors = getErrorsAt(higherErrorPositionExecution, ErrorUmlType.EXECUTION_ERROR, all);
} else {
assert higherErrorPositionSyntax > higherErrorPositionExecution;
higherErrorPosition = higherErrorPositionSyntax;
printedErrors = getErrorsAt(higherErrorPositionSyntax, ErrorUmlType.SYNTAX_ERROR, all);
}
appendSource(higherErrorPosition);
}
public PSystemError(UmlSource source, ErrorUml singleError) {
this(source, Collections.singletonList(singleError));
}
public ImageData exportDiagram(OutputStream os, int num, FileFormatOption fileFormat) throws IOException {
final GraphicStrings result = new GraphicStrings(htmlStrings);
return result.exportDiagram(os, getMetadata(), fileFormat);
}
private void appendSource(int position) {
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("" + StringUtils.hideComparatorCharacters(errorLine) + "");
plainStrings.add(StringUtils.hideComparatorCharacters(errorLine));
final StringBuilder underscore = new StringBuilder();
for (int i = 0; i < errorLine.length(); i++) {
underscore.append("^");
}
plainStrings.add(underscore.toString());
final Collection textErrors = new LinkedHashSet();
for (ErrorUml er : printedErrors) {
textErrors.add(er.getError());
}
for (String er : textErrors) {
htmlStrings.add(" " + er);
plainStrings.add(" " + er);
}
boolean first = true;
for (String s : getSuggest()) {
if (first) {
htmlStrings.add(" " + s);
} else {
htmlStrings.add("" + StringUtils.hideComparatorCharacters(s));
}
first = false;
}
}
public List getSuggest() {
boolean suggested = false;
for (ErrorUml er : printedErrors) {
if (er.hasSuggest()) {
suggested = true;
}
}
if (suggested == false) {
return Collections.emptyList();
}
final List result = new ArrayList();
result.add("Did you mean:");
for (ErrorUml er : printedErrors) {
if (er.hasSuggest()) {
result.add(er.getSuggest().getSuggestedLine());
}
}
return Collections.unmodifiableList(result);
}
private Collection getErrors(ErrorUmlType type, List all) {
final Collection result = new LinkedHashSet();
for (ErrorUml error : all) {
if (error.getType() == type) {
result.add(error);
}
}
return result;
}
private int getHigherErrorPosition(ErrorUmlType type, List all) {
int max = Integer.MIN_VALUE;
for (ErrorUml error : getErrors(type, all)) {
if (error.getPosition() > max) {
max = error.getPosition();
}
}
// if (max == Integer.MIN_VALUE) {
// throw new IllegalStateException();
// }
return max;
}
private List getErrorsAt(int position, ErrorUmlType type, List all) {
final List result = new ArrayList();
for (ErrorUml error : getErrors(type, all)) {
if (error.getPosition() == position && StringUtils.isNotEmpty(error.getError())) {
result.add(error);
}
}
return result;
}
public String getDescription() {
return "(Error)";
}
public void print(PrintStream ps) {
synchronized (ps) {
for (String s : plainStrings) {
ps.println(StringUtils.showComparatorCharacters(s));
}
}
}
public final int getHigherErrorPosition() {
return higherErrorPosition;
}
public final Collection getErrorsUml() {
return Collections.unmodifiableCollection(printedErrors);
}
@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();
}
}