/* ======================================================================== * 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: 4639 $ * */ package net.sourceforge.plantuml.syntax; import java.util.Arrays; import java.util.Collections; import java.util.List; import net.sourceforge.plantuml.ErrorUml; 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 source) { final StringBuilder sb = new StringBuilder(); for (String s : source) { sb.append(s); sb.append("\n"); } 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); result.setErrorLinePosition(0); 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); result.setErrorLinePosition(lastLineNumber(source)); result.addErrorText("No @enduml found"); result.setSuggest(Arrays.asList("Did you mean:", "@enduml")); return result; } final SourceStringReader sourceStringReader = new SourceStringReader(new Defines(), source, Collections. emptyList()); final Diagram system = sourceStringReader.getBlocks().get(0).getDiagram(); 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; result.setErrorLinePosition(sys.getHigherErrorPosition()); for (ErrorUml er : sys.getErrorsUml()) { result.addErrorText(er.getError()); } result.setSuggest(sys.getSuggest()); } else { result.setDescription(system.getDescription().getDescription()); } return result; } 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; } }