2013-12-10 19:36:50 +00:00
|
|
|
/* ========================================================================
|
|
|
|
* PlantUML : a free UML diagram generator
|
|
|
|
* ========================================================================
|
|
|
|
*
|
2016-01-09 12:15:40 +00:00
|
|
|
* (C) Copyright 2009-2017, Arnaud Roques
|
2013-12-10 19:36:50 +00:00
|
|
|
*
|
2016-03-06 16:47:34 +00:00
|
|
|
* Project Info: http://plantuml.com
|
2013-12-10 19:36:50 +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
|
|
|
|
*
|
2013-12-10 19:36:50 +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
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
package net.sourceforge.plantuml.syntax;
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
|
|
|
|
2017-06-05 11:27:21 +00:00
|
|
|
import net.sourceforge.plantuml.BackSlash;
|
2015-04-07 18:18:37 +00:00
|
|
|
import net.sourceforge.plantuml.BlockUml;
|
2013-12-10 19:36:50 +00:00
|
|
|
import net.sourceforge.plantuml.ErrorUml;
|
2017-06-05 11:27:21 +00:00
|
|
|
import net.sourceforge.plantuml.LineLocation;
|
|
|
|
import net.sourceforge.plantuml.LineLocationImpl;
|
2013-12-10 19:36:50 +00:00
|
|
|
import net.sourceforge.plantuml.OptionFlags;
|
|
|
|
import net.sourceforge.plantuml.PSystemError;
|
|
|
|
import net.sourceforge.plantuml.SourceStringReader;
|
|
|
|
import net.sourceforge.plantuml.UmlDiagram;
|
|
|
|
import net.sourceforge.plantuml.core.Diagram;
|
|
|
|
import net.sourceforge.plantuml.preproc.Defines;
|
|
|
|
|
|
|
|
public class SyntaxChecker {
|
|
|
|
|
|
|
|
public static SyntaxResult checkSyntax(List<String> source) {
|
|
|
|
final StringBuilder sb = new StringBuilder();
|
|
|
|
for (String s : source) {
|
|
|
|
sb.append(s);
|
2017-06-05 11:27:21 +00:00
|
|
|
sb.append(BackSlash.NEWLINE);
|
2013-12-10 19:36:50 +00:00
|
|
|
}
|
|
|
|
return checkSyntax(sb.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static SyntaxResult checkSyntax(String source) {
|
|
|
|
OptionFlags.getInstance().setQuiet(true);
|
|
|
|
final SyntaxResult result = new SyntaxResult();
|
|
|
|
|
|
|
|
if (source.startsWith("@startuml\n") == false) {
|
|
|
|
result.setError(true);
|
2017-06-05 11:27:21 +00:00
|
|
|
result.setLineLocation(new LineLocationImpl(null, null).oneLineRead());
|
|
|
|
// result.setErrorLinePosition(0);
|
2013-12-10 19:36:50 +00:00
|
|
|
result.addErrorText("No @startuml found");
|
|
|
|
result.setSuggest(Arrays.asList("Did you mean:", "@startuml"));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
if (source.endsWith("@enduml\n") == false && source.endsWith("@enduml") == false) {
|
|
|
|
result.setError(true);
|
2017-06-05 11:27:21 +00:00
|
|
|
result.setLineLocation(lastLineNumber2(source));
|
|
|
|
// result.setErrorLinePosition(lastLineNumber(source));
|
2013-12-10 19:36:50 +00:00
|
|
|
result.addErrorText("No @enduml found");
|
|
|
|
result.setSuggest(Arrays.asList("Did you mean:", "@enduml"));
|
|
|
|
return result;
|
|
|
|
}
|
2017-04-05 17:37:42 +00:00
|
|
|
final SourceStringReader sourceStringReader = new SourceStringReader(Defines.createEmpty(), source,
|
2013-12-10 19:36:50 +00:00
|
|
|
Collections.<String> emptyList());
|
|
|
|
|
2015-04-07 18:18:37 +00:00
|
|
|
final List<BlockUml> blocks = sourceStringReader.getBlocks();
|
|
|
|
if (blocks.size() == 0) {
|
|
|
|
result.setError(true);
|
2017-06-05 11:27:21 +00:00
|
|
|
result.setLineLocation(lastLineNumber2(source));
|
|
|
|
// result.setErrorLinePosition(lastLineNumber(source));
|
2015-04-07 18:18:37 +00:00
|
|
|
result.addErrorText("No @enduml found");
|
|
|
|
result.setSuggest(Arrays.asList("Did you mean:", "@enduml"));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
final Diagram system = blocks.get(0).getDiagram();
|
|
|
|
result.setCmapData(system.hasUrl());
|
2013-12-10 19:36:50 +00:00
|
|
|
if (system instanceof UmlDiagram) {
|
|
|
|
result.setUmlDiagramType(((UmlDiagram) system).getUmlDiagramType());
|
|
|
|
result.setDescription(system.getDescription().getDescription());
|
|
|
|
} else if (system instanceof PSystemError) {
|
|
|
|
result.setError(true);
|
|
|
|
final PSystemError sys = (PSystemError) system;
|
2017-06-05 11:27:21 +00:00
|
|
|
// result.setErrorLinePosition(sys.getHigherErrorPosition());
|
2015-06-20 10:54:49 +00:00
|
|
|
result.setLineLocation(sys.getLineLocation());
|
2016-09-29 19:51:18 +00:00
|
|
|
result.setSystemError(sys);
|
2013-12-10 19:36:50 +00:00
|
|
|
for (ErrorUml er : sys.getErrorsUml()) {
|
|
|
|
result.addErrorText(er.getError());
|
|
|
|
}
|
|
|
|
result.setSuggest(sys.getSuggest());
|
|
|
|
} else {
|
|
|
|
result.setDescription(system.getDescription().getDescription());
|
|
|
|
}
|
|
|
|
return result;
|
2015-04-07 18:18:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static SyntaxResult checkSyntaxFair(String source) {
|
|
|
|
final SyntaxResult result = new SyntaxResult();
|
2017-04-05 17:37:42 +00:00
|
|
|
final SourceStringReader sourceStringReader = new SourceStringReader(Defines.createEmpty(), source,
|
2015-04-07 18:18:37 +00:00
|
|
|
Collections.<String> emptyList());
|
|
|
|
|
|
|
|
final List<BlockUml> blocks = sourceStringReader.getBlocks();
|
|
|
|
if (blocks.size() == 0) {
|
|
|
|
result.setError(true);
|
2017-06-05 11:27:21 +00:00
|
|
|
result.setLineLocation(lastLineNumber2(source));
|
2015-04-07 18:18:37 +00:00
|
|
|
result.addErrorText("No @enduml found");
|
|
|
|
result.setSuggest(Arrays.asList("Did you mean:", "@enduml"));
|
|
|
|
return result;
|
|
|
|
}
|
2013-12-10 19:36:50 +00:00
|
|
|
|
2015-04-07 18:18:37 +00:00
|
|
|
final Diagram system = blocks.get(0).getDiagram();
|
|
|
|
result.setCmapData(system.hasUrl());
|
|
|
|
if (system instanceof UmlDiagram) {
|
|
|
|
result.setUmlDiagramType(((UmlDiagram) system).getUmlDiagramType());
|
|
|
|
result.setDescription(system.getDescription().getDescription());
|
|
|
|
} else if (system instanceof PSystemError) {
|
|
|
|
result.setError(true);
|
|
|
|
final PSystemError sys = (PSystemError) system;
|
2017-06-05 11:27:21 +00:00
|
|
|
// result.setErrorLinePosition(sys.getHigherErrorPosition());
|
2015-06-20 10:54:49 +00:00
|
|
|
result.setLineLocation(sys.getLineLocation());
|
2015-04-07 18:18:37 +00:00
|
|
|
for (ErrorUml er : sys.getErrorsUml()) {
|
|
|
|
result.addErrorText(er.getError());
|
|
|
|
}
|
|
|
|
result.setSystemError(sys);
|
|
|
|
result.setSuggest(sys.getSuggest());
|
|
|
|
} else {
|
|
|
|
result.setDescription(system.getDescription().getDescription());
|
|
|
|
}
|
|
|
|
return result;
|
2013-12-10 19:36:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static int lastLineNumber(String source) {
|
|
|
|
int result = 0;
|
|
|
|
for (int i = 0; i < source.length(); i++) {
|
|
|
|
if (source.charAt(i) == '\n') {
|
|
|
|
result++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2017-06-05 11:27:21 +00:00
|
|
|
|
|
|
|
private static LineLocation lastLineNumber2(String source) {
|
|
|
|
LineLocationImpl result = new LineLocationImpl(null, null).oneLineRead();
|
|
|
|
for (int i = 0; i < source.length(); i++) {
|
|
|
|
if (source.charAt(i) == '\n') {
|
|
|
|
result = result.oneLineRead();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2013-12-10 19:36:50 +00:00
|
|
|
}
|