1
2
3
4
5
6
7
8
9
10
11
12
13
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
31
32
33
34 public final class XPathLocationFinder {
35
36
37
38
39 private XPathLocationFinder() {
40
41 }
42
43
44
45
46
47
48
49
50
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("-")) {
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 {
79 printProgramInformation();
80 System.err.println("Unknown option: " + option);
81 return;
82 }
83 } else {
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
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 }