Clover Coverage Report
Coverage timestamp: Fri May 24 2013 13:47:27 UTC
../../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
72   158   15   24
16   112   0.21   3
3     5  
1    
 
  XPathLocationFinder       Line # 34 72 15 91.2% 0.9120879
 
  (9)
 
1    /* This file is part of the project "Hilbert II" - http://www.qedeq.org
2    *
3    * Copyright 2000-2013, 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    package org.qedeq.kernel.xml.tracker;
16   
17    import java.io.File;
18    import java.io.IOException;
19    import java.util.Locale;
20   
21    import javax.xml.parsers.ParserConfigurationException;
22   
23    import org.qedeq.base.io.SourceArea;
24    import org.qedeq.base.utility.StringUtility;
25    import org.qedeq.kernel.bo.KernelContext;
26    import org.xml.sax.SAXException;
27   
28   
29    /**
30    * Find position of simple XPath expressions within an XML file.
31    *
32    * @author Michael Meyling
33    */
 
34    public final class XPathLocationFinder {
35   
36    /**
37    * Constructor.
38    */
 
39  0 toggle private XPathLocationFinder() {
40    // nothing to do
41    }
42   
43    /**
44    * Main method.
45    *
46    * @param args Various parameters. See implementation of
47    * {@link #printProgramInformation()}.
48    * @throws ParserConfigurationException Severe parser configuration problem.
49    * @throws SAXException XML problem.
50    * @throws IOException IO problem.
51    */
 
52  15 toggle public static final void main(final String[] args) throws ParserConfigurationException,
53    SAXException, IOException {
54  15 String from = null;
55  15 String xpath = null;
56   
57  15 if (args.length == 0) {
58  0 printProgramInformation();
59  0 return;
60    }
61   
62  39 for (int i = 0; i < args.length; i++) {
63  26 if (args[i].startsWith("-")) { // option
64  14 final String option = args[i].substring(1).toLowerCase(Locale.US);
65  14 if (option.equals("help") || option.equals("h")
66    || option.equals("?")) {
67  1 printProgramInformation();
68  1 return;
69    }
70  13 if (option.equals("xpath") || option.equals("xp")) {
71  12 if (i + 1 >= args.length) {
72  0 printProgramInformation();
73  0 System.err.println("\"-xpath\" must be followed by a xpath.");
74  0 return;
75    }
76  12 xpath = args[i + 1];
77  12 i++;
78    } else { // unknown option
79  1 printProgramInformation();
80  1 System.err.println("Unknown option: " + option);
81  1 return;
82    }
83    } else { // no option, must be file name
84  12 from = args[i];
85    }
86    }
87  13 if (from == null) {
88  1 printProgramInformation();
89  1 System.err.println("XML file must be specified.");
90  1 return;
91    }
92  12 if (xpath == null) {
93  1 printProgramInformation();
94  1 System.err.println("XPath must be specified.");
95  1 return;
96    }
97  11 System.out.println(StringUtility.getClassName(XPathLocationFinder.class) + ", running on: "
98    + KernelContext.getInstance().getDescriptiveKernelVersion());
99  11 try {
100  11 final SimpleXPath sXPath = new SimpleXPath(xpath);
101  9 SourceArea result = XPathLocationParser.findSourceArea(new File(from), new SimpleXPath(xpath));
102  9 System.out.println(result);
103    } catch (RuntimeException e) {
104  2 System.err.println(e);
105    }
106    }
107   
108    /**
109    * Writes calling convention to <code>System.err</code>.
110    */
 
111  4 toggle public static final void printProgramInformation() {
112  4 System.err.println("Name");
113  4 System.err.println("----");
114  4 System.err.println(StringUtility.getClassName(XPathLocationFinder.class)
115    + " - find simple XML paths");
116  4 System.err.println();
117  4 System.err.println("Synopsis");
118  4 System.err.println("-------------------");
119  4 System.err.println("[-h] -xp[ath] <simpleXPath> <xmlFile>");
120  4 System.err.println();
121  4 System.err.println("Description");
122  4 System.err.println("-----------");
123  4 System.err.println(
124    "This program finds the location of a given simple XPath in an XML file.");
125  4 System.err.println();
126  4 System.err.println("Options and Parameters");
127  4 System.err.println("---------------------");
128  4 System.err.println("-h writes this text and returns");
129  4 System.err.println("-xpath set the language filter (default: \"en\")");
130  4 System.err.println(
131    "<simpleXPath> simple XML XPath, a subset of the abbreviation XPath notation");
132  4 System.err.println(
133    " \"/element1/element2[3]@attribute\" is an example for such a");
134  4 System.err.println(
135    " notation. This selects from the first occurrence of \"element1\"");
136  4 System.err.println(
137    " and from the third occurrence of subnode \"element2\" the attribute");
138  4 System.err.println(
139    " \"attribute\". The attribute is optional. It is always exactly one");
140  4 System.err.println(" node or the attribute of one node specified.");
141  4 System.err.println(" General syntax:");
142  4 System.err.println(" {<element>\"[\"<index>\"]}+[\"@\"<attribute>]");
143  4 System.err.println("<xmlFile> XML file");
144  4 System.err.println();
145  4 System.err.println("Parameter Examples");
146  4 System.err.println("------------------");
147  4 System.err.println(
148    "-xp QEDEQ/CHAPTER/SECTION/NODE[2]/PRECEDING/AXIOM/FORMULA/FORALL/VAR@id");
149  4 System.err.println("sample/qedeq_basic_concept.xml");
150  4 System.err.println();
151  4 System.err.println("Further information");
152  4 System.err.println("-------------------");
153  4 System.err.println("For more information about *Hilbert II* look at:");
154  4 System.err.println("\thttp://www.qedeq.org/");
155  4 System.err.println();
156    }
157   
158    }