Clover Coverage Report
Coverage timestamp: Sa Aug 2 2008 13:56:27 CEST
../../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
372   884   110   5,03
62   612   0,3   74
74     1,49  
1    
 
  Context2SimpleXPath       Line # 87 372 110 91,3% 0.9133858
 
  (29)
 
1    /* $Id: Context2SimpleXPath.java,v 1.1 2008/07/26 08:00:50 m31 Exp $
2    *
3    * This file is part of the project "Hilbert II" - http://www.qedeq.org
4    *
5    * Copyright 2000-2008, Michael Meyling <mime@qedeq.org>.
6    *
7    * "Hilbert II" is free software; you can redistribute
8    * it and/or modify it under the terms of the GNU General Public
9    * License as published by the Free Software Foundation; either
10    * version 2 of the License, or (at your option) any later version.
11    *
12    * This program is distributed in the hope that it will be useful,
13    * but WITHOUT ANY WARRANTY; without even the implied warranty of
14    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15    * GNU General Public License for more details.
16    */
17   
18    package org.qedeq.kernel.xml.mapper;
19   
20    import java.util.ArrayList;
21    import java.util.HashMap;
22    import java.util.List;
23    import java.util.Map;
24   
25    import org.qedeq.base.trace.Trace;
26    import org.qedeq.base.utility.Enumerator;
27    import org.qedeq.kernel.base.list.ElementList;
28    import org.qedeq.kernel.base.module.Author;
29    import org.qedeq.kernel.base.module.AuthorList;
30    import org.qedeq.kernel.base.module.Axiom;
31    import org.qedeq.kernel.base.module.Chapter;
32    import org.qedeq.kernel.base.module.ChapterList;
33    import org.qedeq.kernel.base.module.Formula;
34    import org.qedeq.kernel.base.module.FunctionDefinition;
35    import org.qedeq.kernel.base.module.Header;
36    import org.qedeq.kernel.base.module.Import;
37    import org.qedeq.kernel.base.module.ImportList;
38    import org.qedeq.kernel.base.module.Latex;
39    import org.qedeq.kernel.base.module.LatexList;
40    import org.qedeq.kernel.base.module.LinkList;
41    import org.qedeq.kernel.base.module.LiteratureItem;
42    import org.qedeq.kernel.base.module.LiteratureItemList;
43    import org.qedeq.kernel.base.module.Location;
44    import org.qedeq.kernel.base.module.LocationList;
45    import org.qedeq.kernel.base.module.Node;
46    import org.qedeq.kernel.base.module.PredicateDefinition;
47    import org.qedeq.kernel.base.module.Proof;
48    import org.qedeq.kernel.base.module.ProofList;
49    import org.qedeq.kernel.base.module.Proposition;
50    import org.qedeq.kernel.base.module.Qedeq;
51    import org.qedeq.kernel.base.module.Rule;
52    import org.qedeq.kernel.base.module.Section;
53    import org.qedeq.kernel.base.module.SectionList;
54    import org.qedeq.kernel.base.module.Specification;
55    import org.qedeq.kernel.base.module.Subsection;
56    import org.qedeq.kernel.base.module.SubsectionList;
57    import org.qedeq.kernel.base.module.Term;
58    import org.qedeq.kernel.base.module.UsedByList;
59    import org.qedeq.kernel.base.module.VariableList;
60    import org.qedeq.kernel.common.ModuleContext;
61    import org.qedeq.kernel.common.ModuleDataException;
62    import org.qedeq.kernel.visitor.AbstractModuleVisitor;
63    import org.qedeq.kernel.visitor.QedeqNotNullTraverser;
64    import org.qedeq.kernel.xml.tracker.SimpleXPath;
65   
66   
67    /**
68    * Map content string to SimpleXPath string. This class makes it possible to transfer an location
69    * of an {@link org.qedeq.kernel.base.module.Qedeq} object into an XPath like position description
70    * for an XML file representation of that object.
71    *
72    * <p>
73    * See {@link #getXPath(ModuleContext, Qedeq)} for further details.
74    *
75    * <p>
76    * TODO mime 20070217: It seems to work this way but: this class assumes that we can find
77    * QEDEQ/CHAPTER[2]/SECTION[4]/SUBSECTIONS/SUBSECTION[2]
78    * even if we have some ../NODE s inbetween.
79    * (Example: NODE, NODE, SUBSECTION, NODE, SUBSECTION, NODE..)
80    *
81    * Is this still a correct XPath? (Old solution was usage of "*")
82    * Seems ok for official XPath specification, but does it work for our SimpleXPathFinder?
83    *
84    * @version $Revision: 1.1 $
85    * @author Michael Meyling
86    */
 
87    public final class Context2SimpleXPath extends AbstractModuleVisitor {
88   
89    /** This class. */
90    private static final Class CLASS = Context2SimpleXPath.class;
91   
92    /** Traverse QEDEQ module with this traverser. */
93    private QedeqNotNullTraverser traverser;
94   
95    /** QEDEQ object to work on. */
96    private Qedeq qedeq;
97   
98    /** Search for this context. */
99    private final ModuleContext find;
100   
101    /** We are currently at this position. */
102    private SimpleXPath current;
103   
104    /** Element stack. */
105    private final List elements;
106   
107    /** Current stack level. */
108    private int level;
109   
110    /** Is the current context already matching the beginning of the search context? */
111    private boolean matching;
112   
113    /** Last matching begin of search context. See {@link #matching}. */
114    private String matchingBegin;
115   
116    /** Corresponding XPath for the {@link #matchingBegin}. */
117    private SimpleXPath matchingPath;
118   
119    /**
120    * Constructor.
121    *
122    * @param find Find this location.
123    * @param qedeq Within this QEDEQ object.
124    */
 
125  32548 toggle private Context2SimpleXPath(final ModuleContext find, final Qedeq qedeq) {
126  32548 this.qedeq = qedeq;
127  32548 traverser = new QedeqNotNullTraverser(find.getModuleLocation(), this);
128  32548 this.find = find;
129  32548 elements = new ArrayList(20);
130    }
131   
132    /**
133    * This method finds a {@link ModuleContext} something like<br>
134    * <code>
135    * getChapterList().get(4).getSectionList().get(0).getSubsectionList().get(4).getLatex().get(0)
136    * </code><br>
137    * within a {@link Qedeq} module and returns a kind of XPath location for an associated
138    * XML document:<br>
139    * <code>QEDEQ/CHAPTER[5]/SECTION/SUBSECTIONS/SUBSECTION[2]/TEXT/LATEX</code>
140    *
141    * <p>
142    * At this example one can already see that <code>getSubsectionList().get(4)</code> is
143    * transformed into <code>SUBSECTIONS/SUBSECTION[2]</code>. This is due to the fact that
144    * <code>SUBSECTION</code> contains a sequence of <code>SUBSECTION</code> or <code>NODE</code>
145    * elements. The transformation depends not only from the context but also from
146    * the concrete QEDEQ module.
147    *
148    * <p>
149    * Especially the transformation of formula location information in their XML counterpart
150    * demands parsing the whole formula.
151    *
152    * @param find Find this location.
153    * @param qedeq Within this QEDEQ object.
154    * @return XPath for this location in the XML document.
155    * @throws ModuleDataException
156    */
 
157  32548 toggle public static SimpleXPath getXPath(final ModuleContext find, final Qedeq qedeq)
158    throws ModuleDataException {
159  32548 final Context2SimpleXPath converter = new Context2SimpleXPath(find, qedeq);
160  32548 return converter.find();
161    }
162   
 
163  32548 toggle private final SimpleXPath find() throws ModuleDataException {
164  32548 final String method = "find()";
165  32548 Trace.paramInfo(CLASS, this, method, "find", find);
166  32548 elements.clear();
167  32548 level = 0;
168  32548 current = new SimpleXPath();
169  32548 try {
170  32548 traverser.accept(qedeq);
171    } catch (LocationFoundException e) {
172  32548 Trace.paramInfo(CLASS, this, method, "location found", current);
173  32548 return current;
174    }
175  0 Trace.param(CLASS, this, method, "level", level); // level should be equal to zero now
176  0 Trace.info(CLASS, this, method, "location was not found");
177  0 throw new LocationNotFoundException(find);
178    }
179   
 
180  32548 toggle public final void visitEnter(final Qedeq qedeq) throws ModuleDataException {
181  32548 enter("QEDEQ");
182  32548 final String method = "visitEnter(Qedeq)";
183  32548 Trace.param(CLASS, this, method, "current", current);
184  32548 checkMatching(method);
185    }
186   
 
187  0 toggle public final void visitLeave(final Qedeq qedeq) {
188  0 leave();
189    }
190   
 
191  32546 toggle public final void visitEnter(final Header header) throws ModuleDataException {
192  32546 enter("HEADER");
193  32546 final String method = "visitEnter(Header)";
194  32546 Trace.param(CLASS, this, method, "current", current);
195  32546 final String context = traverser.getCurrentContext().getLocationWithinModule();
196  32546 checkMatching(method);
197   
198  32533 traverser.setLocationWithinModule(context + ".getEmail()");
199  32533 current.setAttribute("email");
200  32533 checkIfFound();
201    }
202   
 
203  31979 toggle public final void visitLeave(final Header header) {
204  31979 leave();
205    }
206   
 
207  662 toggle public final void visitEnter(final Specification specification) throws ModuleDataException {
208  662 enter("SPECIFICATION");
209  662 final String method = "visitEnter(Specification)";
210  662 Trace.param(CLASS, this, method, "current", current);
211  662 final String context = traverser.getCurrentContext().getLocationWithinModule();
212  662 checkMatching(method);
213   
214  614 traverser.setLocationWithinModule(context + ".getName()");
215  614 current.setAttribute("name");
216  614 checkIfFound();
217   
218  592 traverser.setLocationWithinModule(context + ".getRuleVersion()");
219  592 current.setAttribute("ruleVersion");
220  592 checkIfFound();
221    }
222   
 
223  438 toggle public final void visitLeave(final Specification specification) {
224  438 leave();
225    }
226   
 
227  176383 toggle public final void visitEnter(final LatexList latexList) throws ModuleDataException {
228  176383 final String method = "visitEnter(LatexList)";
229  176383 final String context = traverser.getCurrentContext().getLocationWithinModule();
230  176383 final String name;
231  176383 if (context.endsWith(".getTitle()")) {
232  74927 name = "TITLE";
233  101456 } else if (context.endsWith(".getSummary()")) {
234  392 name = "ABSTRACT";
235  101064 } else if (context.endsWith(".getIntroduction()")) {
236  61780 name = "INTRODUCTION";
237  39284 } else if (context.endsWith(".getName()")) {
238  12535 name = "NAME";
239  26749 } else if (context.endsWith(".getPrecedingText()")) {
240  25885 name = "PRECEDING";
241  864 } else if (context.endsWith(".getSucceedingText()")) {
242  284 name = "SUCCEEDING";
243  580 } else if (context.endsWith(".getLatex()")) {
244  180 name = "TEXT";
245  400 } else if (context.endsWith(".getDescription()")) {
246  160 name = "DESCRIPTION";
247  240 } else if (context.endsWith(".getNonFormalProof()")) { // no extra XSD element
248  142 name = null;
249  98 } else if (context.endsWith(".getItem()")) { // no extra XSD element
250  98 name = null;
251    } else { // programming error
252  0 throw new IllegalArgumentException("unknown LatexList " + context);
253    }
254  176383 Trace.param(CLASS, this, method, "name", name);
255  176383 if (name != null) {
256  176143 enter(name);
257    }
258  176383 Trace.param(CLASS, this, method, "current", current);
259   
260  176383 checkMatching(method);
261    }
262   
 
263  170885 toggle public final void visitLeave(final LatexList latexList) {
264  170885 final String context = traverser.getCurrentContext().getLocationWithinModule();
265  170885 if (!context.endsWith(".getNonFormalProof()") // no extra XSD element
266    && !context.endsWith(".getItem()")) {
267  170885 leave();
268    }
269    }
270   
 
271  4227 toggle public final void visitEnter(final Latex latex) throws ModuleDataException {
272  4227 final String context = traverser.getCurrentContext().getLocationWithinModule();
273  4227 if (context.indexOf(".getAuthorList().get(") >= 0) { // TODO mime 20070216: why is the
274  13 enter("NAME"); // XSD so cruel???
275    }
276  4227 enter("LATEX");
277  4227 final String method = "visitEnter(Latex)";
278  4227 Trace.param(CLASS, this, method, "current", current);
279  4227 checkMatching(method);
280   
281  1330 traverser.setLocationWithinModule(context + ".getLanguage()");
282  1330 current.setAttribute("language");
283  1330 checkIfFound();
284   
285  1330 traverser.setLocationWithinModule(context + ".getLatex()");
286  1330 current.setAttribute(null); // element character data of LATEX is LaTeX content
287  1330 checkIfFound();
288    }
289   
 
290  1330 toggle public final void visitLeave(final Latex latex) {
291    // because NAME of AUTHOR/NAME/LATEX has no equivalent in interfaces:
292  1330 final String context = traverser.getCurrentContext().getLocationWithinModule();
293  1330 if (context.indexOf(".getAuthorList().get(") >= 0) {
294  0 leave();
295    }
296  1330 leave();
297    }
298   
 
299  132 toggle public final void visitEnter(final LocationList locationList) throws ModuleDataException {
300  132 enter("LOCATIONS");
301  132 final String method = "visitEnter(LocationList)";
302  132 Trace.param(CLASS, this, method, "current", current);
303  132 checkMatching(method);
304   
305    }
306   
 
307  0 toggle public final void visitLeave(final LocationList locationList) {
308  0 leave();
309    }
310   
 
311  98 toggle public final void visitEnter(final Location location) throws ModuleDataException {
312  98 enter("LOCATION");
313  98 final String method = "visitEnter(Location)";
314  98 Trace.param(CLASS, this, method, "current", current);
315  98 final String context = traverser.getCurrentContext().getLocationWithinModule();
316  98 checkMatching(method);
317   
318  40 traverser.setLocationWithinModule(context + ".getLocation()");
319  40 current.setAttribute("value");
320  40 checkIfFound();
321    }
322   
 
323  14 toggle public final void visitLeave(final Location location) {
324  14 leave();
325    }
326   
 
327  346 toggle public final void visitEnter(final AuthorList authorList) throws ModuleDataException {
328  346 enter("AUTHORS");
329  346 final String method = "visitEnter(AuthorList)";
330  346 Trace.param(CLASS, this, method, "current", current);
331  346 checkMatching(method);
332    }
333   
 
334  274 toggle public final void visitLeave(final AuthorList authorList) {
335  274 leave();
336    }
337   
 
338  48 toggle public final void visitEnter(final Author author) throws ModuleDataException {
339  48 enter("AUTHOR");
340  48 final String method = "visitEnter(Author)";
341  48 Trace.param(CLASS, this, method, "current", current);
342  48 final String context = traverser.getCurrentContext().getLocationWithinModule();
343  48 checkMatching(method);
344   
345  24 traverser.setLocationWithinModule(context + ".getEmail()");
346  24 current.setAttribute("email");
347  24 checkIfFound();
348    }
349   
 
350  0 toggle public final void visitLeave(final Author author) {
351  0 leave();
352    }
353   
 
354  252 toggle public final void visitEnter(final ImportList importList) throws ModuleDataException {
355  252 enter("IMPORTS");
356  252 final String method = "visitEnter(ImportList)";
357  252 Trace.param(CLASS, this, method, "current", current);
358  252 checkMatching(method);
359    }
360   
 
361  33 toggle public final void visitLeave(final ImportList importList) {
362  33 leave();
363    }
364   
 
365  292 toggle public final void visitEnter(final Import imp) throws ModuleDataException {
366  292 enter("IMPORT");
367  292 final String method = "visitEnter(Import)";
368  292 Trace.param(CLASS, this, method, "current", current);
369  292 final String context = traverser.getCurrentContext().getLocationWithinModule();
370  292 checkMatching(method);
371   
372  278 traverser.setLocationWithinModule(context + ".getLabel()");
373  278 current.setAttribute("label");
374  278 checkIfFound();
375    }
376   
 
377  85 toggle public final void visitLeave(final Import imp) {
378  85 leave();
379    }
380   
 
381  55 toggle public final void visitEnter(final UsedByList usedByList) throws ModuleDataException {
382  55 enter("USEDBY");
383  55 final String method = "visitEnter(UsedByList)";
384  55 Trace.param(CLASS, this, method, "current", current);
385  55 checkMatching(method);
386    }
387   
 
388  0 toggle public final void visitLeave(final UsedByList usedByList) {
389  0 leave();
390    }
391   
 
392  31979 toggle public final void visitEnter(final ChapterList chapterList) throws ModuleDataException {
393  31979 final String method = "visitEnter(ChapterList)";
394    // because no equivalent level of "getChapterList()" exists in the XSD we simply
395    // point to the current location that must be "QEDEQ"
396  31979 checkMatching(method);
397    }
398   
 
399  170 toggle public final void visitLeave(final ChapterList chapterList) {
400  170 traverser.setBlocked(false); // free sub node search
401    }
402   
 
403  140246 toggle public final void visitEnter(final Chapter chapter) throws ModuleDataException {
404  140246 enter("CHAPTER");
405  140246 final String method = "visitEnter(Chapter)";
406  140246 Trace.param(CLASS, this, method, "current", current);
407  140246 final String context = traverser.getCurrentContext().getLocationWithinModule();
408  140246 checkMatching(method);
409   
410  140120 traverser.setLocationWithinModule(context + ".getNoNumber()");
411  140120 current.setAttribute("noNumber");
412  140120 checkIfFound();
413    }
414   
 
415  108450 toggle public final void visitLeave(final Chapter chapter) {
416  108450 leave();
417    }
418   
 
419  31169 toggle public final void visitEnter(final SectionList sectionList) throws ModuleDataException {
420  31169 final String method = "visitEnter(SectionList)";
421    // because no equivalent level of "getSectionList()" exists in the XSD we simply
422    // point to the current location that must be "QEDEQ/CHAPTER[x]"
423  31169 checkMatching(method);
424    }
425   
 
426  0 toggle public final void visitLeave(final SectionList sectionList) {
427  0 traverser.setBlocked(false); // free sub node search
428    }
429   
 
430  75365 toggle public final void visitEnter(final Section section) throws ModuleDataException {
431  75365 enter("SECTION");
432  75365 final String method = "visitEnter(Section)";
433  75365 Trace.param(CLASS, this, method, "current", current);
434  75365 final String context = traverser.getCurrentContext().getLocationWithinModule();
435  75365 checkMatching(method);
436   
437  75131 traverser.setLocationWithinModule(context + ".getNoNumber()");
438  75131 current.setAttribute("noNumber");
439  75131 checkIfFound();
440    }
441   
 
442  44272 toggle public final void visitLeave(final Section section) {
443  44272 leave();
444    }
445   
 
446  29939 toggle public final void visitEnter(final SubsectionList subsectionList) throws ModuleDataException {
447  29939 enter("SUBSECTIONS");
448  29939 final String method = "visitEnter(SubsectionList)";
449  29939 Trace.param(CLASS, this, method, "current", current);
450  29939 checkMatching(method);
451    }
452   
 
453  0 toggle public final void visitLeave(final SubsectionList subsectionList) {
454  0 leave();
455    }
456   
 
457  5470 toggle public final void visitEnter(final Subsection subsection) throws ModuleDataException {
458  5470 enter("SUBSECTION");
459  5470 final String method = "visitEnter(Subsection)";
460  5470 Trace.param(CLASS, this, method, "current", current);
461  5470 final String context = traverser.getCurrentContext().getLocationWithinModule();
462  5470 checkMatching(method);
463   
464  5362 traverser.setLocationWithinModule(context + ".getId()");
465  5362 current.setAttribute("id");
466  5362 checkIfFound();
467   
468  5362 traverser.setLocationWithinModule(context + ".getLevel()");
469  5362 current.setAttribute("level");
470  5362 checkIfFound();
471    }
472   
 
473  5062 toggle public final void visitLeave(final Subsection subsection) {
474  5062 leave();
475    }
476   
 
477  146527 toggle public final void visitEnter(final Node node) throws ModuleDataException {
478  146527 enter("NODE");
479  146527 final String method = "visitEnter(Node)";
480  146527 Trace.param(CLASS, this, method, "current", current);
481  146527 final String context = traverser.getCurrentContext().getLocationWithinModule();
482  146527 checkMatching(method);
483   
484  145819 traverser.setLocationWithinModule(context + ".getId()");
485  145819 current.setAttribute("id");
486  145819 checkIfFound();
487   
488  145557 traverser.setLocationWithinModule(context + ".getLevel()");
489  145557 current.setAttribute("level");
490  145557 checkIfFound();
491   
492    // we dont't differentiate the different node types here and point to the parent element
493  145406 traverser.setLocationWithinModule(context + ".getNodeType()");
494  145406 current.setAttribute(null);
495  145406 checkIfFound();
496   
497    }
498   
 
499  117126 toggle public final void visitLeave(final Node node) {
500  117126 leave();
501    }
502   
 
503  2425 toggle public final void visitEnter(final Axiom axiom) throws ModuleDataException {
504  2425 enter("AXIOM");
505  2425 final String method = "visitEnter(Axiom)";
506  2425 Trace.param(CLASS, this, method, "current", current);
507  2425 checkMatching(method);
508    }
509   
 
510  68 toggle public final void visitLeave(final Axiom axiom) {
511  68 leave();
512    }
513   
 
514  18395 toggle public final void visitEnter(final Proposition proposition) throws ModuleDataException {
515  18395 enter("THEOREM");
516  18395 final String method = "visitEnter(Proposition)";
517  18395 Trace.param(CLASS, this, method, "current", current);
518  18395 checkMatching(method);
519    }
520   
 
521  56 toggle public final void visitLeave(final Proposition proposition) {
522  56 leave();
523    }
524   
 
525  319 toggle public final void visitEnter(final ProofList proofList) throws ModuleDataException {
526  319 final String method = "visitEnter(ProofList)";
527    // because no equivalent level of "getProofList()" exists in the XSD we simply
528    // point to the current location that must be within the element "THEOREM"
529  319 checkMatching(method);
530    }
531   
 
532  291 toggle public final void visitEnter(final Proof proof) throws ModuleDataException {
533  291 enter("PROOF");
534  291 final String method = "visitEnter(Proof)";
535  291 Trace.param(CLASS, this, method, "current", current);
536  291 final String context = traverser.getCurrentContext().getLocationWithinModule();
537  291 checkMatching(method);
538   
539  210 traverser.setLocationWithinModule(context + ".getKind()");
540  210 current.setAttribute("kind");
541  210 checkIfFound();
542   
543  189 traverser.setLocationWithinModule(context + ".getLevel()");
544  189 current.setAttribute("level");
545  189 checkIfFound();
546    }
547   
 
548  26 toggle public final void visitLeave(final Proof proof) {
549  26 leave();
550    }
551   
 
552  1846 toggle public final void visitEnter(final PredicateDefinition definition) throws ModuleDataException {
553  1846 enter("DEFINITION_PREDICATE");
554  1846 final String method = "visitEnter(PredicateDefinition)";
555  1846 Trace.param(CLASS, this, method, "current", current);
556  1846 final String context = traverser.getCurrentContext().getLocationWithinModule();
557  1846 checkMatching(method);
558   
559  1752 traverser.setLocationWithinModule(context + ".getArgumentNumber()");
560  1752 current.setAttribute("arguments");
561  1752 checkIfFound();
562   
563  1712 traverser.setLocationWithinModule(context + ".getName()");
564  1712 current.setAttribute("name");
565  1712 checkIfFound();
566   
567  1672 traverser.setLocationWithinModule(context + ".getLatexPattern()");
568  1672 enter("LATEXPATTERN");
569  1672 if (find.getLocationWithinModule().equals(traverser.getCurrentContext()
570    .getLocationWithinModule())) {
571  40 if (definition.getLatexPattern() == null) { // NOT FOUND
572  0 leave();
573    }
574  40 throw new LocationFoundException(traverser.getCurrentContext());
575    }
576  1632 leave();
577    }
578   
 
579  80 toggle public final void visitLeave(final PredicateDefinition definition) {
580  80 leave();
581    }
582   
 
583  2062 toggle public final void visitEnter(final FunctionDefinition definition) throws ModuleDataException {
584  2062 enter("DEFINITION_FUNCTION");
585  2062 final String method = "visitEnter(FunctionDefinition)";
586  2062 Trace.param(CLASS, this, method, "current", current);
587  2062 final String context = traverser.getCurrentContext().getLocationWithinModule();
588  2062 checkMatching(method);
589   
590  1966 traverser.setLocationWithinModule(context + ".getArgumentNumber()");
591  1966 current.setAttribute("arguments");
592  1966 checkIfFound();
593   
594  1934 traverser.setLocationWithinModule(context + ".getName()");
595  1934 current.setAttribute("name");
596  1934 checkIfFound();
597   
598  1902 traverser.setLocationWithinModule(context + ".getLatexPattern()");
599  1902 enter("LATEXPATTERN");
600  1902 if (find.getLocationWithinModule().equals(traverser.getCurrentContext()
601    .getLocationWithinModule())) {
602  32 if (definition.getLatexPattern() == null) { // NOT FOUND
603  0 leave();
604    }
605  32 throw new LocationFoundException(traverser.getCurrentContext());
606    }
607  1870 leave();
608    }
609   
 
610  42 toggle public final void visitLeave(final FunctionDefinition definition) {
611  42 leave();
612    }
613   
 
614  240 toggle public final void visitEnter(final Rule rule) throws ModuleDataException {
615  240 enter("RULE");
616  240 final String method = "visitEnter(Rule)";
617  240 Trace.param(CLASS, this, method, "current", current);
618  240 final String context = traverser.getCurrentContext().getLocationWithinModule();
619  240 checkMatching(method);
620   
621  186 traverser.setLocationWithinModule(context + ".getName()");
622  186 current.setAttribute("name");
623  186 checkIfFound();
624    }
625   
 
626  38 toggle public final void visitLeave(final Rule rule) {
627  38 leave();
628    }
629   
 
630  22 toggle public final void visitEnter(final LinkList linkList) throws ModuleDataException {
631  22 final String method = "visitEnter(LinkList)";
632  22 Trace.param(CLASS, this, method, "current", current);
633  22 final String context = traverser.getCurrentContext().getLocationWithinModule();
634  22 checkMatching(method);
635   
636  30 for (int i = 0; i < linkList.size(); i++) {
637  16 enter("LINK");
638  16 if (linkList.get(i) != null) {
639  16 traverser.setLocationWithinModule(context + ".get(" + i + ")");
640  16 current.setAttribute("id");
641  16 checkIfFound();
642    }
643  14 leave();
644  14 };
645    }
646   
 
647  14 toggle public final void visitLeave(final LinkList linkList) {
648    }
649   
 
650  21450 toggle public final void visitEnter(final Formula formula) throws ModuleDataException {
651  21450 enter("FORMULA");
652  21450 final String method = "visitEnter(Formula)";
653  21450 Trace.param(CLASS, this, method, "current", current);
654  21450 checkMatching(method);
655    }
656   
 
657  387 toggle public final void visitLeave(final Formula formula) {
658  387 leave();
659    }
660   
 
661  1576 toggle public final void visitEnter(final Term term) throws ModuleDataException {
662  1576 enter("TERM");
663  1576 final String method = "visitEnter(Term)";
664  1576 Trace.param(CLASS, this, method, "current", current);
665  1576 checkMatching(method);
666    }
667   
 
668  42 toggle public final void visitLeave(final Term term) {
669  42 leave();
670    }
671   
 
672  3254 toggle public final void visitEnter(final VariableList variableList) throws ModuleDataException {
673  3254 enter("VARLIST");
674  3254 final String method = "visitEnter(VariableList)";
675  3254 Trace.param(CLASS, this, method, "current", current);
676  3254 checkMatching(method);
677    }
678   
 
679  2566 toggle public final void visitLeave(final VariableList variableList) {
680  2566 leave();
681    }
682   
 
683  185831 toggle public final void visitEnter(final ElementList list) throws ModuleDataException {
684  185831 final String operator = list.getOperator();
685  185831 enter(operator);
686  185831 final String method = "visitEnter(ElementList)";
687  185831 Trace.param(CLASS, this, method, "current", current);
688  185831 final String context = traverser.getCurrentContext().getLocationWithinModule();
689   
690    // to find something like getElement(0).getList().getElement(0)
691  185831 if (context.startsWith(find.getLocationWithinModule())) {
692  20059 throw new LocationFoundException(find);
693    }
694   
695  165772 checkMatching(method);
696   
697  165772 traverser.setLocationWithinModule(context + ".getOperator()");
698  165772 checkIfFound();
699  165772 traverser.setLocationWithinModule(context);
700  165772 final boolean firstIsAtom = list.size() > 0 && list.getElement(0).isAtom();
701  165772 if (firstIsAtom) {
702  60274 traverser.setLocationWithinModule(context + ".getElement(0).getAtom()");
703  60274 if ("VAR".equals(operator) || "PREDVAR".equals(operator)
704    || "FUNVAR".equals(operator)) {
705  14606 current.setAttribute("id");
706  14606 checkIfFound();
707  45668 } else if ("PREDCON".equals(operator) || "FUNCON".equals(operator)) {
708  45668 current.setAttribute("ref");
709  45668 checkIfFound();
710    } else { // should not occur, but just in case
711  0 current.setAttribute(null);
712  0 Trace.info(CLASS, this, method, "unknown operator " + operator);
713  0 throw new LocationFoundException(traverser.getCurrentContext());
714    }
715    }
716    }
717   
718    /* TODO remove method
719    public final void visitEnter(final Atom atom) throws ModuleDataException {
720    final String method = "visitEnter(Atom)";
721    Trace.param(this, method, "current", current);
722    final String context = traverser.getCurrentContext().getLocationWithinModule();
723    // mime 20070217: should never occur
724    checkMatching(method);
725    }
726    */
 
727  104089 toggle public final void visitLeave(final ElementList list) {
728  104089 leave();
729    }
730   
 
731  170 toggle public final void visitEnter(final LiteratureItemList list) throws ModuleDataException {
732  170 enter("BIBLIOGRAPHY");
733  170 final String method = "visitEnter(LiteratureItemList)";
734  170 Trace.param(CLASS, this, method, "current", current);
735  170 checkMatching(method);
736    }
737   
 
738  0 toggle public final void visitLeave(final LiteratureItemList list) {
739  0 leave();
740    }
741   
 
742  524 toggle public final void visitEnter(final LiteratureItem item) throws ModuleDataException {
743  524 enter("ITEM");
744  524 final String method = "visitEnter(LiteratureItem)";
745  524 Trace.param(CLASS, this, method, "current", current);
746  524 final String context = traverser.getCurrentContext().getLocationWithinModule();
747  524 checkMatching(method);
748   
749  478 traverser.setLocationWithinModule(context + ".getLabel()");
750  478 current.setAttribute("label");
751  478 checkIfFound();
752    }
753   
 
754  360 toggle public final void visitLeave(final LiteratureItem item) {
755  360 leave();
756    }
757   
758    /**
759    * Check if searched for context is already reached.
760    *
761    * @throws LocationFoundException We have found it.
762    */
 
763  1860617 toggle private final void checkIfFound() throws LocationFoundException {
764  1860617 if (find.getLocationWithinModule().equals(traverser.getCurrentContext()
765    .getLocationWithinModule())) {
766  12417 throw new LocationFoundException(traverser.getCurrentContext());
767    }
768    }
769   
770    /**
771    * Checks if the current context matches the beginning of the context we want to find.
772    * This method must be called at the beginning of the <code>visitEnter</code> method when
773    * {@link #current} is correctly set. If the context of a previously visited node was already
774    * matching and the current node doesn't start with the old matching context any longer we
775    * throw a {@link LocationNotFoundException}.
776    *
777    * @param method Method we were called from.
778    * @throws LocationNotFoundException
779    * @throws LocationFoundException
780    */
 
781  926630 toggle private final void checkMatching(final String method)
782    throws LocationNotFoundException, LocationFoundException {
783  926630 final String context = traverser.getCurrentContext().getLocationWithinModule();
784  926630 if (find.getLocationWithinModule().startsWith(context)) {
785  338744 Trace.info(CLASS, this, method, "beginning matches");
786  338744 Trace.paramInfo(CLASS, this, method, "context", context);
787  338744 matching = true;
788  338744 matchingBegin = context; // remember matching context
789  338744 matchingPath = new SimpleXPath(current); // remember last matching XPath
790    } else {
791  587886 if (matching) {
792    // for example we are looking for "getHeader().getImportList().getImport(2)"
793    // getHeader() matches, we remember "getHeader()"
794    // getHeader().getSpecification() doesn't match, but still starts with "getHeader()"
795    // getHeader().getImportList() matches, we remember "getHeader.getImportList()"
796    // getHeader().getImportList().get(0) doesn't match but still starts with
797    // "getHeader.getImportList()"
798  587886 if (!context.startsWith(matchingBegin)) {
799    // Matching lost, that means we will never found the location
800    // so what can we do? We just return the last matching location and hope
801    // it is close enough to the searched one. But at least we do some
802    // logging here:
803  0 Trace.info(CLASS, this, method, "matching lost");
804  0 Trace.paramInfo(CLASS, this, method, "last match ", matchingBegin);
805  0 Trace.paramInfo(CLASS, this, method, "current context", context);
806  0 Trace.paramInfo(CLASS, this, method,
807    "find context ", find.getLocationWithinModule());
808   
809    // throw new LocationNotFoundException(find); // when we really want to fail
810   
811  0 Trace.traceStack(CLASS, this, method);
812  0 Trace.info(CLASS, this, method, "changing XPath to last matching one");
813    // now we change the current XPath to the last matching one because the
814    // contents of "current" is used as the resulting XPath later on when
815    // catching the exception in {@link #find()}
816  0 current = matchingPath;
817  0 throw new LocationFoundException(new ModuleContext(find.getModuleLocation(),
818    matchingBegin));
819    }
820    }
821  587886 traverser.setBlocked(true); // block further search in sub nodes
822    }
823  926630 checkIfFound();
824    }
825   
826    /**
827    * Enter new XML element.
828    *
829    * @param element Element name.
830    */
 
831  886563 toggle private final void enter(final String element) {
832  886563 level++;
833  886563 current.addElement(element, addOccurence(element));
834    }
835   
836    /**
837    * Enter first occurrence of XML element of one level.
838    *
839    * @param element
840    */
 
841  0 toggle private final void enterFirst(final String element) {
842  0 level++;
843  0 current.addElement(element);
844    }
845   
846    /**
847    * Leave last XML element.
848    */
 
849  591218 toggle private final void leave() {
850  591218 level--;
851  591218 current.deleteLastElement();
852  591218 traverser.setBlocked(false); // enable further search in sub notes
853    }
854   
855    /**
856    * Add element occurrence. For example we have <code>getHeader().getImportList().get(2)</code>
857    * and we want to get <code>QEDEQ/HEADER/IMPORTS/IMPORT[3]</code>.
858    * So we call <code>enter("QEDEQ")</code>, <code>enter("HEADER")</code>,
859    * <code>enter("IMPORTS")</code> and last but not least
860    * three times the sequence <code>enter("IMPORT")</code>, <code>leave("IMPORT")</code>,
861    *
862    * @param name Element that occurred.
863    * @return Number of occurrences including this one.
864    */
 
865  886563 toggle private final int addOccurence(final String name) {
866  886685 while (level < elements.size()) {
867  122 elements.remove(elements.size() - 1);
868    }
869  1182173 while (level > elements.size()) {
870  295610 elements.add(new HashMap());
871    }
872  886563 final Map levelMap = (Map) elements.get(level - 1);
873  886563 final Enumerator counter;
874  886563 if (levelMap.containsKey(name)) {
875  348857 counter = (Enumerator) levelMap.get(name);
876  348857 counter.increaseNumber();
877    } else {
878  537706 counter = new Enumerator(1);
879  537706 levelMap.put(name, counter);
880    }
881  886563 return counter.getNumber();
882    }
883   
884    }