XPathLocationFinder.java
001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002  *
003  * Copyright 2000-2013,  Michael Meyling <mime@qedeq.org>.
004  *
005  * "Hilbert II" is free software; you can redistribute
006  * it and/or modify it under the terms of the GNU General Public
007  * License as published by the Free Software Foundation; either
008  * version 2 of the License, or (at your option) any later version.
009  *
010  * This program is distributed in the hope that it will be useful,
011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013  * GNU General Public License for more details.
014  */
015 package org.qedeq.kernel.xml.tracker;
016 
017 import java.io.File;
018 import java.io.IOException;
019 import java.util.Locale;
020 
021 import javax.xml.parsers.ParserConfigurationException;
022 
023 import org.qedeq.base.io.SourceArea;
024 import org.qedeq.base.utility.StringUtility;
025 import org.qedeq.kernel.bo.KernelContext;
026 import org.xml.sax.SAXException;
027 
028 
029 /**
030  * Find position of simple XPath expressions within an XML file.
031  *
032  @author  Michael Meyling
033  */
034 public final class XPathLocationFinder {
035 
036     /**
037      * Constructor.
038      */
039     private XPathLocationFinder() {
040         // nothing to do
041     }
042 
043     /**
044      * Main method.
045      *
046      @param   args    Various parameters. See implementation of
047      *                  {@link #printProgramInformation()}.
048      @throws  ParserConfigurationException    Severe parser configuration problem.
049      @throws  SAXException                    XML problem.
050      @throws  IOException                     IO problem.
051      */
052     public static final void main(final String[] argsthrows ParserConfigurationException,
053             SAXException, IOException {
054         String from = null;
055         String xpath = null;
056 
057         if (args.length == 0) {
058             printProgramInformation();
059             return;
060         }
061 
062         for (int i = 0; i < args.length; i++) {
063             if (args[i].startsWith("-")) {  // option
064                 final String option = args[i].substring(1).toLowerCase(Locale.US);
065                 if (option.equals("help"|| option.equals("h")
066                         || option.equals("?")) {
067                     printProgramInformation();
068                     return;
069                 }
070                 if (option.equals("xpath"|| option.equals("xp")) {
071                     if (i + >= args.length) {
072                         printProgramInformation();
073                         System.err.println("\"-xpath\" must be followed by a xpath.");
074                         return;
075                     }
076                     xpath = args[i + 1];
077                     i++;
078                 else {                    // unknown option
079                     printProgramInformation();
080                     System.err.println("Unknown option: " + option);
081                     return;
082                 }
083             else {                        // no option, must be file name
084                 from = args[i];
085             }
086         }
087         if (from == null) {
088             printProgramInformation();
089             System.err.println("XML file must be specified.");
090             return;
091         }
092         if (xpath == null) {
093             printProgramInformation();
094             System.err.println("XPath file must be specified.");
095             return;
096         }
097         System.out.println(StringUtility.getClassName(XPathLocationFinder.class", running on: "
098             + KernelContext.getInstance().getDescriptiveKernelVersion());
099         SourceArea result = XPathLocationParser.findSourceArea(new File(from)new SimpleXPath(xpath));
100         System.out.println(result);
101     }
102 
103     /**
104      * Writes calling convention to <code>System.err</code>.
105      */
106     public static final void printProgramInformation() {
107         System.err.println("Name");
108         System.err.println("----");
109         System.err.println(StringUtility.getClassName(XPathLocationFinder.class)
110             " - find simple XML paths");
111         System.err.println();
112         System.err.println("Synopsis");
113         System.err.println("-------------------");
114         System.err.println("[-h] -xp[ath] <simpleXPath> <xmlFile>");
115         System.err.println();
116         System.err.println("Description");
117         System.err.println("-----------");
118         System.err.println(
119             "This program finds the location of a given simple XPath in an XML file.");
120         System.err.println();
121         System.err.println("Options and Parameter");
122         System.err.println("---------------------");
123         System.err.println("-h             writes this text and returns");
124         System.err.println("-xpath         set the language filter (default: \"en\")");
125         System.err.println(
126             "<simpleXPath>  simple XML XPath, a subset of the abbreviation XPath notation");
127         System.err.println(
128             "                \"/element1/element2[3]@attribute\" is an example for such a");
129         System.err.println(
130             "                notation. This selects from the first occurrence of \"element1\"");
131         System.err.println(
132             "                and from the third occurrence of subnode \"element2\" the attribute");
133         System.err.println(
134             "                \"attribute\". The attribute is optional. It is always exactly one");
135         System.err.println("                node or the attribute of one node specified.");
136         System.err.println("                General syntax:");
137         System.err.println("                {<element>\"[\"<index>\"]}+[\"@\"<attribute>]");
138         System.err.println("<xmlFile>      XML file");
139         System.err.println();
140         System.err.println("Parameter Examples");
141         System.err.println("------------------");
142         System.err.println(
143             "-xp QEDEQ/CHAPTER/SECTION/NODE[2]/PRECEDING/AXIOM/FORMULA/FORALL/VAR@id");
144         System.err.println("sample/qedeq_basic_concept.xml");
145         System.err.println();
146         System.err.println("Further information");
147         System.err.println("-------------------");
148         System.err.println("For more information about *Hilbert II* look at:");
149         System.err.println("\thttp://www.qedeq.org/");
150         System.err.println();
151     }
152 
153 }