QedeqBoDuplicateLanguageChecker.java
01 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
02  *
03  * Copyright 2000-2013,  Michael Meyling <mime@qedeq.org>.
04  *
05  * "Hilbert II" is free software; you can redistribute
06  * it and/or modify it under the terms of the GNU General Public
07  * License as published by the Free Software Foundation; either
08  * version 2 of the License, or (at your option) any later version.
09  *
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.common.ServiceProcess;
22 import org.qedeq.kernel.bo.module.ControlVisitor;
23 import org.qedeq.kernel.bo.module.KernelQedeqBo;
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.Plugin;
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   process Service process we work in.
43      @param   plugin  Plugin we work for.
44      @param   prop              QEDEQ BO.
45      @throws  SourceFileExceptionList An error occurred.
46      */
47     public static void check(final ServiceProcess process,
48             final Plugin plugin, final KernelQedeqBo propthrows SourceFileExceptionList {
49         final QedeqBoDuplicateLanguageChecker checker
50             new QedeqBoDuplicateLanguageChecker(plugin, prop);
51         checker.traverse(process);
52     }
53 
54     /**
55      * Constructor.
56      *
57      @param   plugin  Plugin we work for.
58      @param   bo      BO QEDEQ module object.
59      */
60     private QedeqBoDuplicateLanguageChecker(final Plugin plugin, final KernelQedeqBo bo) {
61         super(plugin, bo);
62     }
63 
64     public final void visitEnter(final LatexList listthrows ModuleDataException {
65         if (list == null) {
66             return;
67         }
68         final String context = getCurrentContext().getLocationWithinModule();
69         final Map languages = new HashMap();
70         for (int i = 0; i < list.size(); i++) {
71             final Latex latex = list.get(i);
72             setLocationWithinModule(context + ".get(" + i + ")");
73             if (latex == null) {
74                 throw new LatexListDataException(1000"Null pointer not permitted.",
75                     getCurrentContext());
76             }
77             if (languages.containsKey(latex.getLanguage())) {
78                 throw new LatexListDataException(1001"Language entry exists already",
79                     getCurrentContext()(ModuleContextlanguages.get(latex.getLanguage()));
80             }
81             languages.put(list.get(i).getLanguage(), getCurrentContext());
82         }
83         setLocationWithinModule(context);
84         setBlocked(true);
85     }
86 
87     public final void visitLeave(final LatexList list) {
88         setBlocked(false);
89     }
90 
91 }