First commit
[anna.git] / example / xml / xmlBasic / main.cpp
1 // ANNA - Anna is Not 'N' Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
11 //     * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 //     * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 //     * Neither the name of Google Inc. nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 #include <iostream>
38
39 #include <functional>
40
41 #include <anna/core/core.hpp>
42 #include <anna/xml/xml.hpp>
43
44 #include <anna/app/Application.hpp>
45
46 class Test : public app::Application {
47 public:
48    Test ();
49    
50 private:
51    void run () throw (anna::RuntimeException);      
52 };
53
54 using namespace std;
55
56 int main (int argc, const char** argv)
57 {
58    CommandLine& commandLine (CommandLine::instantiate ());
59    Test test;
60
61    xml::functions::initialize ();
62    
63    try {
64       commandLine.initialize (argv, argc);
65       commandLine.verify ();
66
67       Logger::setLevel (Logger::Debug);
68       Logger::initialize ("XML", new TraceWriter ("file.trace", 2048000));
69
70       test.start ();
71    }
72    catch (Exception& ex) {
73       cout << ex.asString () << endl;
74    }
75    
76    return 0;
77 }
78
79
80 Test::Test () : 
81    app::Application ("testfunctions", "Checking trace system", "1.0") 
82 {
83    CommandLine& commandLine (CommandLine::instantiate ());
84       
85    commandLine.add ("document", CommandLine::Argument::Mandatory, "XML document");
86    commandLine.add ("dtd", CommandLine::Argument::Optional, "DTD file");
87 }
88
89 void Test::run () 
90    throw (RuntimeException)
91 {
92    CommandLine& commandLine (CommandLine::instantiate ());
93
94    xml::DocumentFile xmlDoc;
95    xml::Parser parser;
96    xml::Compiler xmlCompiler;
97    xml::DTDFile xmlDTD;
98    bool withDTD (false);
99
100    xmlDoc.initialize (commandLine.getValue ("document"));
101
102    if (commandLine.exists ("dtd")) {
103       xmlDTD.initialize (commandLine.getValue ("dtd"));
104       withDTD = true;
105    }
106
107    LOGDEBUG (
108        string msg ("DTD: ");
109        msg += (withDTD == true) ? "true": "false";
110        msg += " | Document: ";
111        msg +=  anna::functions::asString (xmlDoc.getContent (), 24);
112        Logger::debug (msg, ANNA_FILE_LOCATION);
113    );
114        
115    const xml::Node* root = (withDTD) ? parser.apply (xmlDoc, xmlDTD):  parser.apply (xmlDoc);
116    const xml::Node* node;
117
118    cout << "Root: " << root->asString () << endl;
119
120    for (xml::Node::const_child_iterator ii = root->child_begin (), maxii = root->child_end (); ii != maxii; ii ++) {
121       node = xml::Node::node (ii);
122       cout << "Level 1 - Node: " << node->asString () << endl;
123    } 
124
125    cout << "xml::Compiler (Default= xml::Compiler::Mode::Visual): " << endl << xmlCompiler.apply (root) << endl << endl;
126
127    xml::Node* root2 = new xml::Node ("sample");
128    xml::Node* node2;
129    node2 = root2->createChild ("theNode");
130    node2->createAttribute ("attr1", 1222);
131    node2->createText ("text");
132    node2->createAttribute ("Empty", (char*) NULL);
133    delete root2;
134
135    cout << "xml::Compiler2: " << endl << xmlCompiler.apply (root2) << endl << endl;
136
137    const char* compact =  xmlCompiler.apply (xmlDoc, root, xml::Compiler::Mode::Compact); 
138    cout << "xml::Compiler (Compact): " << endl << compact << endl << endl;
139
140    xml::DocumentMemory memoDoc;
141
142    DataBlock doc (compact, anna_strlen (compact), false);
143
144    memoDoc.initialize (doc);
145    root = (withDTD) ? parser.apply (memoDoc, xmlDTD):  parser.apply (memoDoc);
146    for (xml::Node::const_child_iterator ii = root->child_begin (), maxii = root->child_end (); ii != maxii; ii ++) {
147       node = xml::Node::node (ii);
148       cout << "Level 1 - Memo-Node: " << node->asString () << endl;
149    } 
150    cout << "xml::CompilerMemo: " << endl << xmlCompiler.apply (root) << endl << endl;
151
152    memoDoc.initialize (compact);
153 }