-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
196 lines (151 loc) · 7.56 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
* @file main.cpp
* @author Christian
*
* XMLCC is distributed under the MIT License (MIT); this file is part of.
*
* Copyright (c) 2008-2022 Christian ([email protected])
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef __cplusplus
#error use a C++ compiler for XMLCC::
#endif
/******************************************************************************/
#include "./xmlcc/xmlcc.h" // include of XMLCC::
/******************************************************************************/
// XMLCC:: is still keeping DOM::, SAX::, CFG::, SYS::, and TEST::
using namespace XMLCC;
/******************************************************************************/
int // main
main( int argc, char** argv ) {
/****************************************************************************/
try { // try everything ~8>
/**************************************************************************/
if( true ) { // print version and built date at start up
std::cout << "XMLCC:: version " << _XMLCC_VERSION_ << " package "
<< _XMLCC_PACKAGE_ << std::endl << std::flush;
std::cout << "XMLCC:: built " << _XMLCC_BUILD_DATE_ << " "
<< _XMLCC_BUILD_TIME_ << std::endl << std::flush;
std::cout << std::endl << std::flush;
} // if
/**************************************************************************/
if( true ) { // generating the actual config file for XMLCC
std::cout << "checking config file .. " << std::flush;
CFG::Config config;
if( config.exists( ) ) {
std::cout << "exists!" << std::endl << std::endl << std::flush;
} else {
std::cout << "not existing; going to generating one .. " << std::flush;
config.write( config.generate( ) );
std::cout << "done!" << std::endl << std::endl << std::flush;
} // if
} // if
/**************************************************************************/
#ifdef _XMLCC_USE_TEST_ // see xmlccTest.h & xmlccTest.cpp
XMLCC::TEST::run( ); // run unit tests of XMLCC as set in config file
#endif // _XMLCC_USE_TEST_
/**************************************************************************/
if( true ) { // generating a simple web page by using DOM objects directly
// example how to generate, view, and write content in XML (HTML)
std::cout << "generating DOM tree .. " << std::flush;
DOM::Root* xml = 0;
xml = new DOM::Root( "xmlcc.html", new DOM::Doctype( ),
new DOM::Comment( "XMLCC 1.01 20200307 Amara Faith" ),
new DOM::Element( "html",
new DOM::Element( "head",
new DOM::Element( "title",
new DOM::Value( "HTML generated by XMLCC" ) ) ),
new DOM::Element( "body" ),
new DOM::Element( "center",
new DOM::Comment( "HTML was generated by XMLCC" ),
new DOM::Value( "Hello" ),
new DOM::Element( "b", new DOM::Value( "WWW" ) ),
new DOM::Value( "from XMLCC" ), new DOM::Element( "br" ),
new DOM::Element( "a",
new DOM::Attribute( "href",
"https://github.com/graetz23/xmlcc/" ),
new DOM::Value( "visit project page" ) ) ) ) );
std::cout << "done!" << std::endl << std::flush;
// std::cout << std::endl << std::flush;
// std::cout << "showing DOM tree to file .. " << std::endl << std::flush;
// std::cout << xml << std::endl << std::flush; // DOM:: 2 std::cout
// std::cout << "done!" << std::endl << std::flush;
std::cout << "writing DOM tree to file .. " << std::flush;
std::fstream file; // open file
file.open( ( (char*)( xml->getStr( ).c_str( ) ) ), std::ios::out );
file << xml; // DOM tree 2 std::ostream
file.close( );
delete xml; // deletes complete DOM tree
std::cout << "done!" << std::endl << std::endl << std::flush;
} // if
/**************************************************************************/
if( true ) { // generating a simple web page by using DOM objects directly
// TODO implement web page example using DOM::Controller
} // if
/**************************************************************************/
XMLCC::Str fileName = ""; // file name by console or use xmlccMalformed.xml
if( argc >= 2 )
fileName.append( argv[ 1 ] );
else
fileName = "xmlccMalformed.xml";
/**************************************************************************/
if( true ) { // parsing to document object model (DOM)
std::cout << "parsing to document object model (DOM): " << fileName
<< std::endl << std::flush;
XMLCC::DOM::Core core; // methods for parsing 2 DOM tree
XMLCC::DOM::Root* root = core.parseFile2DomTree( fileName ); // DOM tree
if( root != 0 ) {
std::cout << root << std::flush << std::endl; // DOM tree 2 std::ostream
delete root; // deletes complete DOM tree
} // if root
} // if
/**************************************************************************/
if( true ) { // parsing using simple API for XML (SAX)
std::cout << "parsing by simple API for XML (SAX): " << fileName
<< std::endl << std::flush;
// SAX parser; parsing, cleaning, and calling SAX handler methods
XMLCC::SAX::Parser* parser = new XMLCC::SAX::Parser(
new XMLCC::SAX::HandlerExample( ) );
parser->parseFile( fileName ); // HandlerExample is streaming to std::cout
delete parser;
} // if
/**************************************************************************/
// std::cout << "done!" << std::endl << std::flush;
/**************************************************************************/
} catch( XMLCC::SYS::Failure& f ) { // own exception class
std::cout << "SYS::Failure caught:" << std::endl << std::flush;
std::cout << f;
} catch( XMLCC::SYS::Error& e ) { // own exception class
std::cout << "SYS::Error caught:" << std::endl << std::flush;
std::cout << e;
} catch( XMLCC::SYS::Exception& e ) { // own exception class
std::cout << "SYS::Exception caught:" << std::endl << std::flush;
std::cout << e;
} catch( std::exception& e ) { // standard exception class
std::cout << "std::exception caught:" << std::endl << std::flush;
std::cout << e.what( ) << std::endl << std::flush;
} catch( ... ) { // any other exception thrown
std::cout << "Strange exception caught:" << std::endl << std::flush;
XMLCC::SYS::Exception e;
e.nuke( ); // nuke screen
} // try
return 0;
} // main
/******************************************************************************/