| 1 | /* This file is part of the project "Hilbert II" - http://www.qedeq.org |
| 2 | * |
| 3 | * Copyright 2000-2014, Michael Meyling <mime@qedeq.org>. |
| 4 | * |
| 5 | * "Hilbert II" is free software; you can redistribute |
| 6 | * it and/or modify it under the terms of the GNU General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
| 16 | package org.qedeq.kernel.bo.service.latex; |
| 17 | |
| 18 | import java.util.HashMap; |
| 19 | import java.util.Map; |
| 20 | |
| 21 | import org.qedeq.kernel.bo.module.InternalModuleServiceCall; |
| 22 | import org.qedeq.kernel.bo.module.KernelQedeqBo; |
| 23 | import org.qedeq.kernel.bo.service.basis.ControlVisitor; |
| 24 | import org.qedeq.kernel.se.base.module.Latex; |
| 25 | import org.qedeq.kernel.se.base.module.LatexList; |
| 26 | import org.qedeq.kernel.se.common.ModuleContext; |
| 27 | import org.qedeq.kernel.se.common.ModuleDataException; |
| 28 | import org.qedeq.kernel.se.common.Service; |
| 29 | import org.qedeq.kernel.se.common.SourceFileExceptionList; |
| 30 | |
| 31 | |
| 32 | /** |
| 33 | * Checks if no duplicate language entries exist. |
| 34 | * |
| 35 | * @author Michael Meyling |
| 36 | */ |
| 37 | public final class QedeqBoDuplicateLanguageChecker extends ControlVisitor { |
| 38 | |
| 39 | /** |
| 40 | * Checks if all formulas of a QEDEQ module are well formed. |
| 41 | * |
| 42 | * @param call Service process we work in. |
| 43 | * @throws SourceFileExceptionList An error occurred. |
| 44 | */ |
| 45 | public static void check(final InternalModuleServiceCall call) throws SourceFileExceptionList { |
| 46 | // TODO 20140125 m31: don't cast to KernelQedeqBo |
| 47 | final QedeqBoDuplicateLanguageChecker checker |
| 48 | = new QedeqBoDuplicateLanguageChecker(call.getService(), (KernelQedeqBo) call.getQedeq()); |
| 49 | checker.traverse(call.getInternalServiceProcess()); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Constructor. |
| 54 | * |
| 55 | * @param service Service we work for. |
| 56 | * @param bo BO QEDEQ module object. |
| 57 | */ |
| 58 | private QedeqBoDuplicateLanguageChecker(final Service service, final KernelQedeqBo bo) { |
| 59 | super(service, bo); |
| 60 | } |
| 61 | |
| 62 | public final void visitEnter(final LatexList list) throws ModuleDataException { |
| 63 | if (list == null) { |
| 64 | return; |
| 65 | } |
| 66 | final String context = getCurrentContext().getLocationWithinModule(); |
| 67 | final Map languages = new HashMap(); |
| 68 | for (int i = 0; i < list.size(); i++) { |
| 69 | final Latex latex = list.get(i); |
| 70 | setLocationWithinModule(context + ".get(" + i + ")"); |
| 71 | if (latex == null) { |
| 72 | throw new LatexListDataException(1000, "Null pointer not permitted.", |
| 73 | getCurrentContext()); |
| 74 | } |
| 75 | if (languages.containsKey(latex.getLanguage())) { |
| 76 | throw new LatexListDataException(1001, "Language entry exists already", |
| 77 | getCurrentContext(), (ModuleContext) languages.get(latex.getLanguage())); |
| 78 | } |
| 79 | languages.put(list.get(i).getLanguage(), getCurrentContext()); |
| 80 | } |
| 81 | setLocationWithinModule(context); |
| 82 | setBlocked(true); |
| 83 | } |
| 84 | |
| 85 | public final void visitLeave(final LatexList list) { |
| 86 | setBlocked(false); |
| 87 | } |
| 88 | |
| 89 | } |