Clover coverage report - QedeqKernelSe Coverage Report
Coverage timestamp: Do Jan 11 2007 09:03:50 CET
file stats: LOC: 213   Methods: 4
NCLOC: 133   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
Context2XPath.java 94,4% 96,5% 75% 95,4%
coverage coverage
 1    /* $Id: Context2XPath.java,v 1.16 2006/10/20 20:23:07 m31 Exp $
 2    *
 3    * This file is part of the project "Hilbert II" - http://www.qedeq.org
 4    *
 5    * Copyright 2000-2006, 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.StringTokenizer;
 21   
 22    import org.qedeq.kernel.context.ModuleContext;
 23    import org.qedeq.kernel.log.Trace;
 24    import org.qedeq.kernel.utility.ReplaceUtility;
 25    import org.qedeq.kernel.xml.tracker.SimpleXPath;
 26   
 27    /**
 28    * Map content string to SimpleXPath string. This class makes it possible to transfer an location
 29    * of an {@link org.qedeq.kernel.base.module.Qedeq} object into an XPath like position description
 30    * for an XML file representation of that object.
 31    * <p>
 32    * This class has maps something like<br>
 33    * <code>
 34    * getChapterList().get(4).getSectionList().get(0).getSubsectionList().get(4).getLatex().get(0)
 35    * </code><br>
 36    * into<br>
 37    * <code>QEDEQ/CHAPTER[5]/SECTION/SUBSECTIONS/*[5]/TEXT/LATEX</code>
 38    *
 39    * TODO mime 20070109: use visitor and Qedeq-Object to get XPath; this is necessary because
 40    * an Atom (if it is the first in an ElementList) is always an attribute tag - therefore the
 41    * XML element counting doesn't work (you have to subtract one if the first element is an
 42    * Atom)
 43    *
 44    * @version $Revision: 1.16 $
 45    * @author Michael Meyling
 46    */
 47    public final class Context2XPath {
 48   
 49    /**
 50    * Constructor.
 51    */
 52  0 private Context2XPath() {
 53    // nothing to do
 54    }
 55   
 56    /**
 57    * Get file path out of context information.
 58    *
 59    * @param context Context information.
 60    * @return File path and name.
 61    */
 62  1 public static final String getFileName(final ModuleContext context) {
 63  1 return context.getModuleLocation();
 64    }
 65   
 66    /**
 67    * Get XPath out of context information.
 68    *
 69    * @param context Context information.
 70    * @return XPath.
 71    */
 72  3153 public static final String getXPath(final ModuleContext context) {
 73  3153 final String method = "getXPath(String)";
 74  3153 String xpath = context.getLocationWithinModule();
 75  3153 Trace.param(Context2XPath.class, method, "context", xpath);
 76  3153 xpath = ReplaceUtility.replace(xpath, ".get(", "[");
 77  3153 xpath = ReplaceUtility.replace(xpath, "()", "");
 78  3153 xpath = ReplaceUtility.replace(xpath, ")", "]");
 79  3153 xpath = ReplaceUtility.replace(xpath, ".get", "/");
 80  3153 xpath = ReplaceUtility.replace(xpath, "get", "/Qedeq/");
 81   
 82    // TODO mime 20050807: what if no Latex, Author, or other, exist? For regular files this is
 83    // ok, but if there is no element in the list?
 84    // TODO mime 20050708: isn't a replacement only one element by one better?
 85  3153 xpath = ReplaceUtility.replace(xpath, "Title[", "Title/Latex[");
 86    // TODO mime 20050708: definition in XML file only formula
 87  3153 xpath = ReplaceUtility.replace(xpath, "PredicateDefinition", "DEFINITION_PREDICATE");
 88  3153 xpath = ReplaceUtility.replace(xpath, "FunctionDefinition", "DEFINITION_FUNCTION");
 89  3153 xpath = ReplaceUtility.replace(xpath, "AuthorList[", "Authors/Author[");
 90  3153 xpath = ReplaceUtility.replace(xpath, "ImportList[", "Imports/Import[");
 91  3153 xpath = ReplaceUtility.replace(xpath, "LiteratureItemList[", "BIBLIOGRAPHY/ITEM[");
 92  3153 xpath = ReplaceUtility.replace(xpath, "LiteratureItemList", "BIBLIOGRAPHY");
 93  3153 xpath = ReplaceUtility.replace(xpath, "/Item[", "/Latex[");
 94  3153 xpath = ReplaceUtility.replace(xpath, "/Item", "/Latex");
 95  3153 xpath = ReplaceUtility.replace(xpath, "UsedByList[", "UsedBy/Specification[");
 96  3153 xpath = ReplaceUtility.replace(xpath, "ChapterList[", "Chapter[");
 97  3153 xpath = ReplaceUtility.replace(xpath, "AuthorList[", "Author[");
 98  3153 xpath = ReplaceUtility.replace(xpath, "AuthorList", "Authors");
 99  3153 xpath = ReplaceUtility.replace(xpath, "ImportList", "Imports");
 100  3153 xpath = ReplaceUtility.replace(xpath, "LocationList", "Locations");
 101  3153 xpath = ReplaceUtility.replace(xpath, "LinkList[", "Link[");
 102  3153 xpath = ReplaceUtility.replace(xpath, "SectionList[", "Section[");
 103  3153 xpath = ReplaceUtility.replace(xpath, "SubsectionList", "Subsections/*");
 104  3153 xpath = ReplaceUtility.replace(xpath, "VariableList", "VARLIST/*");
 105  3153 xpath = ReplaceUtility.replace(xpath, "ProofList[", "PROOF[");
 106  3153 xpath = ReplaceUtility.replace(xpath, "ProofList", "PROOF");
 107  3153 xpath = ReplaceUtility.replace(xpath, "/NodeType", "");
 108  3153 xpath = ReplaceUtility.replace(xpath, "Summary", "Abstract/Latex");
 109  3153 xpath = ReplaceUtility.replace(xpath, "Introduction", "Introduction/Latex");
 110  3153 xpath = ReplaceUtility.replace(xpath, "PrecedingText", "PRECEDING/Latex");
 111  3153 xpath = ReplaceUtility.replace(xpath, "SucceedingText", "SUCCEEDING/Latex");
 112  3153 xpath = ReplaceUtility.replace(xpath, "Description[", "Description/Latex[");
 113  3153 xpath = ReplaceUtility.replace(xpath, "Proposition", "Theorem");
 114  3153 xpath = ReplaceUtility.replace(xpath, "Formula/Element/", "Formula/*/");
 115  3153 xpath = ReplaceUtility.replace(xpath, "Element", "*");
 116   
 117    // TODO mime 20050807: integrate non formal proofs
 118  3153 xpath = ReplaceUtility.replace(xpath, "/NonFormalProof[", "/Latex[");
 119  3153 xpath = ReplaceUtility.replace(xpath, "/NonFormalProof", "/Latex");
 120   
 121  3153 xpath = ReplaceUtility.replace(xpath, "/List", "");
 122  3153 xpath = ReplaceUtility.replace(xpath, "List", "");
 123  3153 xpath = ReplaceUtility.replace(xpath, "(", "[");
 124  3153 xpath = xpath.toUpperCase();
 125   
 126  3153 xpath = incrementNumbers(xpath);
 127   
 128   
 129  3153 SimpleXPath sxp = new SimpleXPath(xpath);
 130   
 131  3153 final String beforeLast = sxp.getBeforeLastElement();
 132  3153 final String last = sxp.getLastElement();
 133  3153 if ("EMAIL".equals(last)) {
 134  10 sxp.deleteLastElement();
 135  10 sxp.setAttribute("email");
 136  3143 } else if ("LABEL".equals(last)) {
 137  14 sxp.deleteLastElement();
 138  14 sxp.setAttribute("label");
 139  3129 } else if ("ID".equals(last)) {
 140  103 sxp.deleteLastElement();
 141  103 sxp.setAttribute("id");
 142  3026 } else if ("SPECIFICATION".equals(beforeLast) && "NAME".equals(last)) {
 143  12 sxp.deleteLastElement();
 144  12 sxp.setAttribute("name");
 145  3014 } else if ("SPECIFICATION".equals(beforeLast) && "RULEVERSION".equals(last)) {
 146  12 sxp.deleteLastElement();
 147  12 sxp.setAttribute("ruleVersion");
 148  3002 } else if ("CHAPTER".equals(beforeLast) && "NONUMBER".equals(last)) {
 149  8 sxp.deleteLastElement();
 150  8 sxp.setAttribute("noNumber");
 151  2994 } else if ("SECTION".equals(beforeLast) && "NONUMBER".equals(last)) {
 152  0 sxp.deleteLastElement();
 153  0 sxp.setAttribute("noNumber");
 154  2994 } else if ("*".equals(beforeLast) && "LATEX".equals(last)) {
 155  40 sxp.deleteLastElement();
 156  40 sxp.addElement("TEXT");
 157  40 sxp.addElement("LATEX");
 158  2954 } else if ("DEFINITION_PREDICATE".equals(beforeLast) && "ARGUMENTNUMBER".equals(last)) {
 159  14 sxp.deleteLastElement();
 160  14 sxp.setAttribute("arguments");
 161  2940 } else if ("DEFINITION_PREDICATE".equals(beforeLast) && "NAME".equals(last)) {
 162  14 sxp.deleteLastElement();
 163  14 sxp.setAttribute("name");
 164  2926 } else if ("DEFINITION_FUNCTION".equals(beforeLast) && "ARGUMENTNUMBER".equals(last)) {
 165  16 sxp.deleteLastElement();
 166  16 sxp.setAttribute("arguments");
 167  2910 } else if ("DEFINITION_FUNCTION".equals(beforeLast) && "NAME".equals(last)) {
 168  16 sxp.deleteLastElement();
 169  16 sxp.setAttribute("name");
 170  2894 } else if ("RULE".equals(beforeLast) && "NAME".equals(last)) {
 171  2 sxp.deleteLastElement();
 172  2 sxp.setAttribute("name");
 173  2892 } else if ("*".equals(beforeLast) && "LEVEL".equals(last)) {
 174  72 sxp.deleteLastElement();
 175  72 sxp.setAttribute("level");
 176  2820 } else if ("*".equals(beforeLast) && "NONUMBER".equals(last)) {
 177  0 sxp.deleteLastElement();
 178  0 sxp.setAttribute("noNumber");
 179  2820 } else if ("*".equals(beforeLast) && "NAME".equals(last)) {
 180  196 final int len = sxp.getElementOccurrence(sxp.size() - 1);
 181  196 sxp.deleteLastElement();
 182  196 sxp.addElement("NAME");
 183  196 sxp.addElement("LATEX", len);
 184    }
 185   
 186  3153 xpath = sxp.toString();
 187  3153 Trace.param(Context2XPath.class, method, "xpath", xpath);
 188  3153 return xpath;
 189    }
 190   
 191    /**
 192    * Increment all element occurrence numbers in "[]" by one.
 193    *
 194    * @param xpath Like "a[0]b[1]".
 195    * @return Like "a[1]b[2]".
 196    */
 197  3153 private static String incrementNumbers(final String xpath) {
 198  3153 final StringTokenizer tokenizer = new StringTokenizer(xpath, "/", true);
 199  3153 String newXpath = "";
 200  3153 while (tokenizer.hasMoreTokens()) {
 201  36200 String token = tokenizer.nextToken();
 202  36200 if (token.indexOf('[') >= 0) {
 203  8704 final StringTokenizer getnu = new StringTokenizer(token, "[]");
 204  8704 newXpath += getnu.nextToken() + "[";
 205  8704 newXpath += ((new Integer(getnu.nextToken())).intValue() + 1) + "]";
 206    } else {
 207  27496 newXpath += token;
 208    }
 209    }
 210  3153 return newXpath;
 211    }
 212   
 213    }