Clover coverage report - QedeqKernelSe Coverage Report
Coverage timestamp: Do Dez 29 2005 18:38:29 CET
file stats: LOC: 194   Methods: 5
NCLOC: 124   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
XPathLocationFinder.java 0% 3,9% 20% 4%
coverage coverage
 1    /* $Id: XPathLocationFinder.java,v 1.12 2005/08/14 06:59:22 m31 Exp $
 2    *
 3    * This file is part of the project "Hilbert II" - http://www.qedeq.org
 4    *
 5    * Copyright 2000-2005, 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    package org.qedeq.kernel.xml.tracker;
 18   
 19    import java.io.File;
 20    import java.io.FileOutputStream;
 21    import java.io.IOException;
 22    import java.io.PrintStream;
 23   
 24    import javax.xml.parsers.ParserConfigurationException;
 25   
 26    import org.qedeq.kernel.context.KernelContext;
 27    import org.qedeq.kernel.log.Trace;
 28    import org.qedeq.kernel.utility.IoUtility;
 29    import org.xml.sax.SAXException;
 30   
 31   
 32    /**
 33    * Find position of simple XPath expressions within an XML file.
 34    *
 35    * @version $Revision: 1.12 $
 36    * @author Michael Meyling
 37    */
 38    public final class XPathLocationFinder {
 39   
 40    /** Location of trace file. */
 41    public static final String TRACE_FILE_PATH = "log/trace.txt";
 42   
 43    /** Is the trace already initialized? */
 44    private static boolean initialized;
 45   
 46    /**
 47    * Constructor.
 48    */
 49  0 private XPathLocationFinder() {
 50    // nothing to do
 51    }
 52   
 53    /**
 54    * Main method.
 55    *
 56    * @param args Various parameters. See implementation of
 57    * {@link #printProgramInformation()}.
 58    * @throws ParserConfigurationException
 59    * @throws SAXException
 60    * @throws IOException
 61    */
 62  0 public static final void main(final String[] args) throws ParserConfigurationException,
 63    SAXException, IOException {
 64  0 String from = null;
 65  0 String xpath = null;
 66   
 67  0 if (args.length == 0) {
 68  0 printProgramInformation();
 69  0 return;
 70    }
 71   
 72  0 for (int i = 0; i < args.length; i++) {
 73  0 if (args[i].startsWith("-")) { // option
 74  0 final String option = args[i].substring(1).toLowerCase();
 75  0 if (option.equals("help") || option.equals("h")
 76    || option.equals("?")) {
 77  0 printProgramInformation();
 78  0 return;
 79    }
 80  0 if (option.equals("xpath") || option.equals("xp")) {
 81  0 if (i + 1 >= args.length) {
 82  0 printProgramInformation();
 83  0 System.err.println("\"-xpath\" must be followed by a xpath.");
 84  0 return;
 85    }
 86  0 xpath = args[i + 1];
 87  0 i++;
 88    } else { // unknown option
 89  0 printProgramInformation();
 90  0 System.err.println("Unknown option: " + option);
 91  0 return;
 92    }
 93    } else { // no option, must be file name
 94  0 from = args[i];
 95    }
 96    }
 97  0 if (from == null) {
 98  0 printProgramInformation();
 99  0 System.err.println("XML file must be specified.");
 100  0 return;
 101    }
 102  0 if (xpath == null) {
 103  0 printProgramInformation();
 104  0 System.err.println("XPath file must be specified.");
 105  0 return;
 106    }
 107  0 initalizeTrace();
 108  0 System.out.println(IoUtility.getClassName(XPathLocationFinder.class) + ", running on: "
 109    + KernelContext.getDescriptiveKernelVersion());
 110  0 getXPathLocation(from, xpath);
 111    }
 112   
 113    /**
 114    * Search simple XPath within an XML file.
 115    *
 116    * @param xmlFile Search this file.
 117    * @param xpath Search for this simple XPath.
 118    * @return Source position information.
 119    * @throws ParserConfigurationException
 120    * @throws SAXException
 121    * @throws IOException
 122    */
 123  409 public static final SimpleXPath getXPathLocation(final String xmlFile, final String xpath)
 124    throws ParserConfigurationException, SAXException, IOException {
 125  409 final XPathLocationParser parser = new XPathLocationParser(xpath);
 126  409 parser.parse(xmlFile);
 127  409 return parser.getFind();
 128    }
 129   
 130    /**
 131    * Writes calling convention to <code>System.err</code>.
 132    */
 133  0 public static final void printProgramInformation() {
 134  0 System.err.println("Name");
 135  0 System.err.println("----");
 136  0 System.err.println(IoUtility.getClassName(XPathLocationFinder.class)
 137    + " - find simple XML paths");
 138  0 System.err.println();
 139  0 System.err.println("Synopsis");
 140  0 System.err.println("-------------------");
 141  0 System.err.println("[-h] -xp[ath] <simpleXPath> <xmlFile>");
 142  0 System.err.println();
 143  0 System.err.println("Description");
 144  0 System.err.println("-----------");
 145  0 System.err.println(
 146    "This program finds the location of a given simple XPath in an XML file.");
 147  0 System.err.println();
 148  0 System.err.println("Options and Parameter");
 149  0 System.err.println("---------------------");
 150  0 System.err.println("-h writes this text and returns");
 151  0 System.err.println("-xpath set the language filter (default: \"en\")");
 152  0 System.err.println(
 153    "<simpleXPath> simple XML XPath, a subset of the abbreviation XPath notation");
 154  0 System.err.println(
 155    " \"/element1/element2[3]@attribute\" is an example for such a");
 156  0 System.err.println(
 157    " notation. This selects from the first occurrence of \"element1\"");
 158  0 System.err.println(
 159    " and from the third occurrence of subnode \"element2\" the attribute");
 160  0 System.err.println(
 161    " \"attribute\". The attribute is optional. It is always exactly one");
 162  0 System.err.println(" node or the attribute of one node specified.");
 163  0 System.err.println(" General syntax:");
 164  0 System.err.println(" {<element>\"[\"<index>\"]}+[\"@\"<attribute>]");
 165  0 System.err.println("<xmlFile> XML file");
 166  0 System.err.println();
 167  0 System.err.println("Parameter Examples");
 168  0 System.err.println("------------------");
 169  0 System.err.println(
 170    "-xp QEDEQ/CHAPTER/SECTION/NODE[2]/PRECEDING/AXIOM/FORMULA/FORALL/VAR@id");
 171  0 System.err.println("sample/qedeq_basic_concept.xml");
 172  0 System.err.println();
 173  0 System.err.println("Further information");
 174  0 System.err.println("-------------------");
 175  0 System.err.println("For more information about *Hilbert II* look at:");
 176  0 System.err.println("\thttp://www.qedeq.org/");
 177  0 System.err.println();
 178    }
 179   
 180    /**
 181    * Initialize trace file. See {@link #TRACE_FILE_PATH}.
 182    *
 183    * @throws IOException Initialization failed.
 184    */
 185  0 private static void initalizeTrace() throws IOException {
 186  0 if (!initialized) {
 187  0 final File traceFile = new File(TRACE_FILE_PATH);
 188  0 IoUtility.createNecessaryDirectories(traceFile);
 189  0 Trace.setPrintStream(new PrintStream(new FileOutputStream(traceFile)));
 190  0 initialized = true;
 191    }
 192    }
 193   
 194    }