| 1 | /* This file is part of the project "Hilbert II" - http://www.qedeq.org |
| 2 | * |
| 3 | * Copyright 2000-2014, 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 | 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 | public static final void main(final String[] args) throws ParserConfigurationException, |
| 53 | SAXException, IOException { |
| 54 | String from = null; |
| 55 | String xpath = null; |
| 56 | |
| 57 | if (args.length == 0) { |
| 58 | printProgramInformation(); |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | for (int i = 0; i < args.length; i++) { |
| 63 | if (args[i].startsWith("-")) { // option |
| 64 | final String option = args[i].substring(1).toLowerCase(Locale.US); |
| 65 | if (option.equals("help") || option.equals("h") |
| 66 | || option.equals("?")) { |
| 67 | printProgramInformation(); |
| 68 | return; |
| 69 | } |
| 70 | if (option.equals("xpath") || option.equals("xp")) { |
| 71 | if (i + 1 >= args.length) { |
| 72 | printProgramInformation(); |
| 73 | System.err.println("\"-xpath\" must be followed by a xpath."); |
| 74 | return; |
| 75 | } |
| 76 | xpath = args[i + 1]; |
| 77 | i++; |
| 78 | } else { // unknown option |
| 79 | printProgramInformation(); |
| 80 | System.err.println("Unknown option: " + option); |
| 81 | return; |
| 82 | } |
| 83 | } else { // no option, must be file name |
| 84 | from = args[i]; |
| 85 | } |
| 86 | } |
| 87 | if (from == null) { |
| 88 | printProgramInformation(); |
| 89 | System.err.println("XML file must be specified."); |
| 90 | return; |
| 91 | } |
| 92 | if (xpath == null) { |
| 93 | printProgramInformation(); |
| 94 | System.err.println("XPath must be specified."); |
| 95 | return; |
| 96 | } |
| 97 | System.out.println(StringUtility.getClassName(XPathLocationFinder.class) + ", running on: " |
| 98 | + KernelContext.getInstance().getDescriptiveKernelVersion()); |
| 99 | try { |
| 100 | final SimpleXPath sXPath = new SimpleXPath(xpath); |
| 101 | SourceArea result = XPathLocationParser.findSourceArea(new File(from), new SimpleXPath(xpath)); |
| 102 | System.out.println(result); |
| 103 | } catch (RuntimeException e) { |
| 104 | System.err.println(e); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Writes calling convention to <code>System.err</code>. |
| 110 | */ |
| 111 | public static final void printProgramInformation() { |
| 112 | System.err.println("Name"); |
| 113 | System.err.println("----"); |
| 114 | System.err.println(StringUtility.getClassName(XPathLocationFinder.class) |
| 115 | + " - find simple XML paths"); |
| 116 | System.err.println(); |
| 117 | System.err.println("Synopsis"); |
| 118 | System.err.println("-------------------"); |
| 119 | System.err.println("[-h] -xp[ath] <simpleXPath> <xmlFile>"); |
| 120 | System.err.println(); |
| 121 | System.err.println("Description"); |
| 122 | System.err.println("-----------"); |
| 123 | System.err.println( |
| 124 | "This program finds the location of a given simple XPath in an XML file."); |
| 125 | System.err.println(); |
| 126 | System.err.println("Options and Parameters"); |
| 127 | System.err.println("---------------------"); |
| 128 | System.err.println("-h writes this text and returns"); |
| 129 | System.err.println("-xpath set the language filter (default: \"en\")"); |
| 130 | System.err.println( |
| 131 | "<simpleXPath> simple XML XPath, a subset of the abbreviation XPath notation"); |
| 132 | System.err.println( |
| 133 | " \"/element1/element2[3]@attribute\" is an example for such a"); |
| 134 | System.err.println( |
| 135 | " notation. This selects from the first occurrence of \"element1\""); |
| 136 | System.err.println( |
| 137 | " and from the third occurrence of subnode \"element2\" the attribute"); |
| 138 | System.err.println( |
| 139 | " \"attribute\". The attribute is optional. It is always exactly one"); |
| 140 | System.err.println(" node or the attribute of one node specified."); |
| 141 | System.err.println(" General syntax:"); |
| 142 | System.err.println(" {<element>\"[\"<index>\"]}+[\"@\"<attribute>]"); |
| 143 | System.err.println("<xmlFile> XML file"); |
| 144 | System.err.println(); |
| 145 | System.err.println("Parameter Examples"); |
| 146 | System.err.println("------------------"); |
| 147 | System.err.println( |
| 148 | "-xp QEDEQ/CHAPTER/SECTION/NODE[2]/PRECEDING/AXIOM/FORMULA/FORALL/VAR@id"); |
| 149 | System.err.println("sample/qedeq_basic_concept.xml"); |
| 150 | System.err.println(); |
| 151 | System.err.println("Further information"); |
| 152 | System.err.println("-------------------"); |
| 153 | System.err.println("For more information about *Hilbert II* look at:"); |
| 154 | System.err.println("\thttp://www.qedeq.org/"); |
| 155 | System.err.println(); |
| 156 | } |
| 157 | |
| 158 | } |