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 &