/* ======================================================================== * PlantUML : a free UML diagram generator * ======================================================================== * * (C) Copyright 2009-2014, 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.BlockUml; 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 List blocks = sourceStringReader.getBlocks(); if (blocks.size() == 0) { result.setError(true); result.setErrorLinePosition(lastLineNumber(source)); 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()); 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()); result.setLineLocation(sys.getLineLocation()); for (ErrorUml er : sys.getErrorsUml()) { result.addErrorText(er.getError()); } result.setSuggest(sys.getSuggest()); } else { result.setDescription(system.getDescription().getDescription()); } return result; } public static SyntaxResult checkSyntaxFair(String source) { final SyntaxResult result = new SyntaxResult(); final SourceStringReader sourceStringReader = new SourceStringReader(new Defines(), source, Collections. emptyList()); final List blocks = sourceStringReader.getBlocks(); if (blocks.size() == 0) { result.setError(true); result.setErrorLinePosition(lastLineNumber(source)); 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()); 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()); result.setLineLocation(sys.getLineLocation()); for (ErrorUml er : sys.getErrorsUml()) { result.addErrorText(er.getError()); } result.setSystemError(sys); 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; } }