Remove warnings
[anna.git] / example / xml / xpath / main.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 #include <iostream>
10
11 #include <functional>
12
13 #include <anna/core/core.hpp>
14 #include <anna/xml/xml.hpp>
15
16 #include <anna/app/Application.hpp>
17
18 class Test : public app::Application {
19 public:
20    Test ();
21    
22 private:
23    void run () throw (anna::RuntimeException);      
24 };
25
26 using namespace std;
27
28 int main (int argc, const char** argv)
29 {
30    CommandLine& commandLine (CommandLine::instantiate ());
31    Test test;
32
33    xml::functions::initialize ();
34    
35    try {
36       commandLine.initialize (argv, argc);
37       commandLine.verify ();
38
39       Logger::setLevel (Logger::Debug);
40       Logger::initialize ("XML", new TraceWriter ("file.trace", 2048000));
41
42       test.start ();
43    }
44    catch (Exception& ex) {
45       cout << ex.asString () << endl;
46    }
47    
48    return 0;
49 }
50
51 Test::Test () : 
52    app::Application ("testfunctions", "Checking xpath", "1.0") 
53 {
54    CommandLine& commandLine (CommandLine::instantiate ());
55       
56    commandLine.add ("xml", CommandLine::Argument::Mandatory, "XML document");
57    commandLine.add ("xpath", CommandLine::Argument::Mandatory, "XPath condition");
58    commandLine.add ("mode", CommandLine::Argument::Optional, "Result type (simple,full,namespace)");
59 }
60
61 void Test::run () 
62    throw (RuntimeException)
63 {
64    CommandLine& commandLine (CommandLine::instantiate ());
65
66    xml::DocumentFile xmlDoc;
67    xml::Compiler xmlCompiler;
68    XPath xpath ("testing");
69
70    xmlDoc.initialize (commandLine.getValue ("xml"));
71
72    Microsecond init, final;
73
74    const char* expression = commandLine.getValue ("xpath");
75    const char* smode = NULL;
76
77    int mode = xml::XPath::Mode::Simple;
78
79    if (commandLine.exists ("mode")) {
80       smode = commandLine.getValue ("mode");
81       if (anna_strstr (smode, "full") != 0)
82          mode = xml::XPath::Mode::Full;
83       if (anna_strstr (smode, "namespace") != 0)
84          mode |= xml::XPath::Mode::Namespace;
85    }
86
87    // Several times to check reuse
88    for (int i = 0; i < 5; i ++) {
89       init = anna::functions::hardwareClock ();
90       xpath.apply (xmlDoc, expression, mode);
91       final = anna::functions::hardwareClock  ();
92       cout << "Time (apply" << i << "): " << final - init << " us" << endl << endl;
93       if (xpath.isEmpty () == false) {
94          if (i == 0) {
95             for (Node::const_child_iterator ii = xpath.node_begin (), maxii = xpath.node_end (); ii != maxii; ii ++) 
96                cout << xmlCompiler.apply (XPath::node (ii)) << endl;
97          }
98       }
99       else
100          cout << "apply: no node matching expression" << endl << endl;
101    }
102
103    init = anna::functions::hardwareClock ();
104    bool match = xpath.match (xmlDoc, expression, mode);
105    final = anna::functions::hardwareClock  ();
106    cout << "Time (match): " << final - init << " us" << endl << endl;
107
108    if (match == false)
109       cout << "match: no node matching expression" << endl << endl;
110
111    xpath.apply (xmlDoc, expression, mode);
112    for (Node::const_child_iterator ii = xpath.node_begin (), maxii = xpath.node_end (); ii != maxii; ii ++) 
113       cout << xmlCompiler.apply (XPath::node (ii), xml::Compiler::Mode::Compact | xml::Compiler::Mode::NoNamespaces) << endl;
114 }
115