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