Remove dynamic exceptions
[anna.git] / example / xml / xmlBasic / 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 () noexcept(false);      
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
52 Test::Test () : 
53    app::Application ("testfunctions", "Checking trace system", "1.0") 
54 {
55    CommandLine& commandLine (CommandLine::instantiate ());
56       
57    commandLine.add ("document", CommandLine::Argument::Mandatory, "XML document");
58    commandLine.add ("dtd", CommandLine::Argument::Optional, "DTD file");
59 }
60
61 void Test::run () 
62    noexcept(false)
63 {
64    CommandLine& commandLine (CommandLine::instantiate ());
65
66    xml::DocumentFile xmlDoc;
67    xml::Parser parser;
68    xml::Compiler xmlCompiler;
69    xml::DTDFile xmlDTD;
70    bool withDTD (false);
71
72    xmlDoc.initialize (commandLine.getValue ("document"));
73
74    if (commandLine.exists ("dtd")) {
75       xmlDTD.initialize (commandLine.getValue ("dtd"));
76       withDTD = true;
77    }
78
79    LOGDEBUG (
80        string msg ("DTD: ");
81        msg += (withDTD == true) ? "true": "false";
82        msg += " | Document: ";
83        msg +=  anna::functions::asString (xmlDoc.getContent (), 24);
84        Logger::debug (msg, ANNA_FILE_LOCATION);
85    );
86        
87    const xml::Node* root = (withDTD) ? parser.apply (xmlDoc, xmlDTD):  parser.apply (xmlDoc);
88    const xml::Node* node;
89
90    cout << "Root: " << root->asString () << endl;
91
92    for (xml::Node::const_child_iterator ii = root->child_begin (), maxii = root->child_end (); ii != maxii; ii ++) {
93       node = xml::Node::node (ii);
94       cout << "Level 1 - Node: " << node->asString () << endl;
95    } 
96
97    cout << "xml::Compiler (Default= xml::Compiler::Mode::Visual): " << endl << xmlCompiler.apply (root) << endl << endl;
98
99    xml::Node* root2 = new xml::Node ("sample");
100    xml::Node* node2;
101    node2 = root2->createChild ("theNode");
102    node2->createAttribute ("attr1", 1222);
103    node2->createText ("text");
104    node2->createAttribute ("Empty", (char*) NULL);
105    delete root2;
106
107    cout << "xml::Compiler2: " << endl << xmlCompiler.apply (root2) << endl << endl;
108
109    const char* compact =  xmlCompiler.apply (xmlDoc, root, xml::Compiler::Mode::Compact); 
110    cout << "xml::Compiler (Compact): " << endl << compact << endl << endl;
111
112    xml::DocumentMemory memoDoc;
113
114    DataBlock doc (compact, anna_strlen (compact), false);
115
116    memoDoc.initialize (doc);
117    root = (withDTD) ? parser.apply (memoDoc, xmlDTD):  parser.apply (memoDoc);
118    for (xml::Node::const_child_iterator ii = root->child_begin (), maxii = root->child_end (); ii != maxii; ii ++) {
119       node = xml::Node::node (ii);
120       cout << "Level 1 - Memo-Node: " << node->asString () << endl;
121    } 
122    cout << "xml::CompilerMemo: " << endl << xmlCompiler.apply (root) << endl << endl;
123
124    memoDoc.initialize (compact);
125 }