Clover coverage report - QedeqKernelSe Coverage Report
Coverage timestamp: Sa Okt 21 2006 08:24:31 CEST
file stats: LOC: 205   Methods: 4
NCLOC: 131   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
Context2XPath.java 94,4% 96,4% 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.bo.control.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    * @version $Revision: 1.16 $
 40    * @author Michael Meyling
 41    */
 42    public final class Context2XPath {
 43   
 44    /**
 45    * Constructor.
 46    */
 47  0 private Context2XPath() {
 48    // nothing to do
 49    }
 50   
 51    /**
 52    * Get file path out of context information.
 53    *
 54    * @param context Context information.
 55    * @return File path and name.
 56    */
 57  1 public static final String getFileName(final ModuleContext context) {
 58  1 return context.getModuleLocation();
 59    }
 60   
 61    /**
 62    * Get XPath out of context information.
 63    *
 64    * @param context Context information.
 65    * @return XPath.
 66    */
 67  2309 public static final String getXPath(final ModuleContext context) {
 68  2309 final String method = "getXPath(String)";
 69  2309 String xpath = context.getLocationWithinModule();
 70  2309 Trace.traceParam(Context2XPath.class, method, "context", xpath);
 71  2309 xpath = ReplaceUtility.replace(xpath, ".get(", "[");
 72  2309 xpath = ReplaceUtility.replace(xpath, "()", "");
 73  2309 xpath = ReplaceUtility.replace(xpath, ")", "]");
 74  2309 xpath = ReplaceUtility.replace(xpath, ".get", "/");
 75  2309 xpath = ReplaceUtility.replace(xpath, "get", "/Qedeq/");
 76   
 77  2309 xpath = incrementNumbers(xpath);
 78   
 79    // TODO mime 20050807: what if no Latex, Author, or other, exist? For regular files this is
 80    // ok, but if there is no element in the list?
 81    // TODO mime 20050708: isn't a replacement only one element by one better?
 82  2309 xpath = ReplaceUtility.replace(xpath, "Title[", "Title/Latex[");
 83    // TODO mime 20050708: definition in XML file only formula
 84  2309 xpath = ReplaceUtility.replace(xpath, "PredicateDefinition", "DEFINITION_PREDICATE");
 85  2309 xpath = ReplaceUtility.replace(xpath, "FunctionDefinition", "DEFINITION_FUNCTION");
 86  2309 xpath = ReplaceUtility.replace(xpath, "AuthorList[", "Authors/Author[");
 87  2309 xpath = ReplaceUtility.replace(xpath, "ImportList[", "Imports/Import[");
 88  2309 xpath = ReplaceUtility.replace(xpath, "LiteratureItemList[", "BIBLIOGRAPHY/ITEM[");
 89  2309 xpath = ReplaceUtility.replace(xpath, "LiteratureItemList", "BIBLIOGRAPHY");
 90  2309 xpath = ReplaceUtility.replace(xpath, "/Item[", "/Latex[");
 91  2309 xpath = ReplaceUtility.replace(xpath, "/Item", "/Latex");
 92  2309 xpath = ReplaceUtility.replace(xpath, "UsedByList[", "UsedBy/Specification[");
 93  2309 xpath = ReplaceUtility.replace(xpath, "ChapterList[", "Chapter[");
 94  2309 xpath = ReplaceUtility.replace(xpath, "AuthorList[", "Author[");
 95  2309 xpath = ReplaceUtility.replace(xpath, "AuthorList", "Authors");
 96  2309 xpath = ReplaceUtility.replace(xpath, "ImportList", "Imports");
 97  2309 xpath = ReplaceUtility.replace(xpath, "LocationList", "Locations");
 98  2309 xpath = ReplaceUtility.replace(xpath, "LinkList[", "Link[");
 99  2309 xpath = ReplaceUtility.replace(xpath, "SectionList[", "Section[");
 100  2309 xpath = ReplaceUtility.replace(xpath, "SubsectionList", "Subsections/*");
 101  2309 xpath = ReplaceUtility.replace(xpath, "VariableList[", "VARLIST/VAR[");
 102  2309 xpath = ReplaceUtility.replace(xpath, "VariableList", "VARLIST");
 103  2309 xpath = ReplaceUtility.replace(xpath, "ProofList[", "PROOF[");
 104  2309 xpath = ReplaceUtility.replace(xpath, "ProofList", "PROOF");
 105  2309 xpath = ReplaceUtility.replace(xpath, "List", "");
 106  2309 xpath = ReplaceUtility.replace(xpath, "/NodeType", "");
 107  2309 xpath = ReplaceUtility.replace(xpath, "Summary", "Abstract/Latex");
 108  2309 xpath = ReplaceUtility.replace(xpath, "Introduction", "Introduction/Latex");
 109  2309 xpath = ReplaceUtility.replace(xpath, "PrecedingText", "PRECEDING/Latex");
 110  2309 xpath = ReplaceUtility.replace(xpath, "SucceedingText", "SUCCEEDING/Latex");
 111  2309 xpath = ReplaceUtility.replace(xpath, "Description[", "Description/Latex[");
 112  2309 xpath = ReplaceUtility.replace(xpath, "Element", "*");
 113  2309 xpath = ReplaceUtility.replace(xpath, "Proposition", "Theorem");
 114   
 115    // TODO mime 20050807: integrate non formal proofs
 116  2309 xpath = ReplaceUtility.replace(xpath, "/NonFormalProof[", "/Latex[");
 117  2309 xpath = ReplaceUtility.replace(xpath, "/NonFormalProof", "/Latex");
 118   
 119  2309 xpath = xpath.toUpperCase();
 120   
 121  2309 SimpleXPath sxp = new SimpleXPath(xpath);
 122   
 123  2309 final String beforeLast = sxp.getBeforeLastElement();
 124  2309 final String last = sxp.getLastElement();
 125  2309 if ("EMAIL".equals(last)) {
 126  5 sxp.deleteLastElement();
 127  5 sxp.setAttribute("email");
 128  2304 } else if ("LABEL".equals(last)) {
 129  14 sxp.deleteLastElement();
 130  14 sxp.setAttribute("label");
 131  2290 } else if ("ID".equals(last)) {
 132  103 sxp.deleteLastElement();
 133  103 sxp.setAttribute("id");
 134  2187 } else if ("SPECIFICATION".equals(beforeLast) && "NAME".equals(last)) {
 135  7 sxp.deleteLastElement();
 136  7 sxp.setAttribute("name");
 137  2180 } else if ("SPECIFICATION".equals(beforeLast) && "RULEVERSION".equals(last)) {
 138  7 sxp.deleteLastElement();
 139  7 sxp.setAttribute("ruleVersion");
 140  2173 } else if ("CHAPTER".equals(beforeLast) && "NONUMBER".equals(last)) {
 141  8 sxp.deleteLastElement();
 142  8 sxp.setAttribute("noNumber");
 143  2165 } else if ("SECTION".equals(beforeLast) && "NONUMBER".equals(last)) {
 144  0 sxp.deleteLastElement();
 145  0 sxp.setAttribute("noNumber");
 146  2165 } else if ("*".equals(beforeLast) && "LATEX".equals(last)) {
 147  27 sxp.deleteLastElement();
 148  27 sxp.addElement("TEXT");
 149  27 sxp.addElement("LATEX");
 150  2138 } else if ("DEFINITION_PREDICATE".equals(beforeLast) && "ARGUMENTNUMBER".equals(last)) {
 151  14 sxp.deleteLastElement();
 152  14 sxp.setAttribute("arguments");
 153  2124 } else if ("DEFINITION_PREDICATE".equals(beforeLast) && "NAME".equals(last)) {
 154  14 sxp.deleteLastElement();
 155  14 sxp.setAttribute("name");
 156  2110 } else if ("DEFINITION_FUNCTION".equals(beforeLast) && "ARGUMENTNUMBER".equals(last)) {
 157  16 sxp.deleteLastElement();
 158  16 sxp.setAttribute("arguments");
 159  2094 } else if ("DEFINITION_FUNCTION".equals(beforeLast) && "NAME".equals(last)) {
 160  16 sxp.deleteLastElement();
 161  16 sxp.setAttribute("name");
 162  2078 } else if ("RULE".equals(beforeLast) && "NAME".equals(last)) {
 163  2 sxp.deleteLastElement();
 164  2 sxp.setAttribute("name");
 165  2076 } else if ("*".equals(beforeLast) && "LEVEL".equals(last)) {
 166  72 sxp.deleteLastElement();
 167  72 sxp.setAttribute("level");
 168  2004 } else if ("*".equals(beforeLast) && "NONUMBER".equals(last)) {
 169  0 sxp.deleteLastElement();
 170  0 sxp.setAttribute("noNumber");
 171  2004 } else if ("*".equals(beforeLast) && "NAME".equals(last)) {
 172  147 final int len = sxp.getElementOccurrence(sxp.size() - 1);
 173  147 sxp.deleteLastElement();
 174  147 sxp.addElement("NAME");
 175  147 sxp.addElement("LATEX", len);
 176    }
 177   
 178  2309 xpath = sxp.toString();
 179  2309 Trace.traceParam(Context2XPath.class, method, "xpath", xpath);
 180  2309 return xpath;
 181    }
 182   
 183    /**
 184    * Increment all element occurrence numbers in "[]" by one.
 185    *
 186    * @param xpath Like "a[0]b[1]".
 187    * @return Like "a[1]b[2]".
 188    */
 189  2309 private static String incrementNumbers(final String xpath) {
 190  2309 final StringTokenizer tokenizer = new StringTokenizer(xpath, "/", true);
 191  2309 String newXpath = "";
 192  2309 while (tokenizer.hasMoreTokens()) {
 193  22814 String token = tokenizer.nextToken();
 194  22814 if (token.indexOf('[') >= 0) {
 195  6723 final StringTokenizer getnu = new StringTokenizer(token, "[]");
 196  6723 newXpath += getnu.nextToken() + "[";
 197  6723 newXpath += ((new Integer(getnu.nextToken())).intValue() + 1) + "]";
 198    } else {
 199  16091 newXpath += token;
 200    }
 201    }
 202  2309 return newXpath;
 203    }
 204   
 205    }