1 using System;
2
3 void PrintHelp()
4 {
5 Console.Out() << "XPath tester" << endl();
6 Console.Out() << "Usage: XPathTester [options] [XML_FILE]" << endl();
7 Console.Out() << "Options:" << endl();
8 Console.Out() << "--help | -h" << endl();
9 Console.Out() << " Print help and exit." << endl();
10 Console.Out() << "--debug-parse | -d" << endl();
11 Console.Out() << " Write XML parsing log to stderr." << endl();
12 Console.Out() << endl();
13 Console.Out() << "First reads the XML_FILE, or \'library.xml\' if no file given." << endl();
14 Console.Out() << "Then user can enter an XPATH query such as \'/library/objects/object\'. " << endl();
15 Console.Out() << "The program evaluates the query and prints the parsed XPATH expression and results as XML." << endl();
16 Console.Out() << endl();
17 }
18
19 int main(int argc, const char** argv)
20 {
21 #if (WINDOWS)
22 string end = "CTRL-Z";
23 #else
24 string end = "CTRL-D";
25 #endif
26 string xmlFile;
27 bool debugParse = false;
28 for (int i = 1; i < argc; ++i;)
29 {
30 string arg = argv[i];
31 if (arg.StartsWith("--"))
32 {
33 if (arg == "--help")
34 {
35 PrintHelp();
36 return 0;
37 }
38 else if (arg == "--debug-parse")
39 {
40 debugParse = true;
41 }
42 else
43 {
44 Console.Error() << "unknown option \'" << arg << "\'" << endl();
45 return 1;
46 }
47 }
48 else if (arg.StartsWith("-"))
49 {
50 string options = arg.Substring(1);
51 for (char o : options)
52 {
53 switch (o)
54 {
55 case 'h':
56 {
57 PrintHelp();
58 return 0;
59 }
60 case 'd':
61 {
62 debugParse = true;
63 break;
64 }
65 }
66 }
67 }
68 else
69 {
70 xmlFile = arg;
71 }
72 }
73 if (xmlFile.IsEmpty())
74 {
75 string executablePath = GetPathToExecutable();
76 auto projectDirPathResult = System.IO.GetFullPath(
77 System.IO.Path.Combine(System.IO.Path.Combine(System.IO.Path.Combine(System.IO.Path.GetDirectoryName(executablePath), ".."), ".."), ".."));
78 if (projectDirPathResult.Error())
79 {
80 Console.Error() << projectDirPathResult.GetErrorMessage();
81 return 1;
82 }
83 const string& projectDirPath = projectDirPathResult.Value();
84 xmlFile = System.IO.Path.Combine(projectDirPath, "library.xml");
85 }
86 System.Lex.FileMap fileMap;
87 Console.Out() << "reading " << xmlFile << "..." << endl();
88 auto result = System.Xml.ParseXmlDocument(xmlFile, fileMap);
89 if (result.Error())
90 {
91 Console.Error() << result.GetErrorMessage() << endl();
92 return 1;
93 }
94 System.XPath.SetDebug(true);
95 if (debugParse)
96 {
97 System.XPath.SetDebugParse(true);
98 }
99 System.Xml.Document* document = result.Value().Get();
100 Console.Out() << "Enter XPATH query expression, or " << end << " to end> ";
101 auto lineResult = Console.ReadLine();
102 if (lineResult.Error())
103 {
104 Console.Error() << lineResult.GetErrorMessage() << endl();
105 return 1;
106 }
107 string expr = lineResult.Value();
108 while (!Console.In().EndOfStream())
109 {
110 auto result = System.XPath.Evaluate(expr, document);
111 if (result.Error())
112 {
113 Console.Error() << result.GetErrorMessage() << endl();
114 }
115 Console.Out() << "Enter XPATH query expression, or " << end << " to end> ";
116 lineResult = Console.ReadLine();
117 if (lineResult.Error())
118 {
119 Console.Error() << lineResult.GetErrorMessage() << endl();
120 return 1;
121 }
122 expr = lineResult.Value();
123 }
124 Console.WriteLine("bye!");
125 return 0;
126 }