Size: 6462
Comment:
|
Size: 9787
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 103: | Line 103: |
g++ challenge.cpp -o challenge -lxml2 g++ challenge.cpp -o challenge -lstdc++ -lxml2 -I/usr/include/libxml2 g++ challenge.cpp -o challenge -lstdc++ -lxerces-c // added random ... |
|
Line 112: | Line 116: |
Line 115: | Line 118: |
#include <cstdio> | |
Line 120: | Line 123: |
#include <xercesc/sax/ErrorHandler.hpp> | |
Line 122: | Line 124: |
#include <xercesc/dom/DOM.hpp> #include <xercesc/dom/DOMDocument.hpp> #include <xercesc/dom/DOMNodeList.hpp> #include <xercesc/dom/DOMNode.hpp> |
|
Line 126: | Line 131: |
const int NO_OPTION=-1; const int GEN_XML=1; const int GEN_JSON=2; const int EXIT=3; const char* OUT_XML="out.xml"; |
|
Line 133: | Line 144: |
class OutOfRangeException:public exception { public: OutOfRangeException(int value){ this->value = value; this->message = new char[128]; } virtual const char *what() const throw() { char ret[128]; sprintf(this->message,"%d out of range!",this->value); return this->message; } private: int value; char* message; }; |
|
Line 135: | Line 165: |
virtual void set_string(string value) { this->value = value; } virtual string get_string() { return this->value; } virtual string to_xml() { string ret = ""; ret += "<bit:a xmlns:bit=\"http://bitarus.allowed.org/types\" "; ret += "xsi:schemaLocation=\"http://bitarus.allowed.org/types test.xsd\" "; ret += "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"; ret += "<bit:b>"; ret += this->value; ret += "</bit:b>"; ret += "</bit:a>"; return ret; } |
Data(){ this->b = ""; } virtual void set_string(string value) { this->b = value; } virtual string get_string() { return this->b; } virtual string to_xml(const char* xsd_file) { string ret = ""; ret += "<bit:a xmlns:bit=\"http://bitarus.allowed.org/types\" "; ret += "xsi:schemaLocation=\"http://bitarus.allowed.org/types " + string(xsd_file) + "\" "; ret += "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n"; ret += "<bit:b>"; ret += this->b; ret += "</bit:b>\n"; ret += "</bit:a>\n"; return ret; } virtual string to_json() { string ret = ""; ret += "{\"a\": \n"; ret += " {\"b\":\""; ret += this->b; ret += "\"} \n"; ret += "} "; return ret; } static Data* from_xml(const char* xml_file){ Data* ret = new Data(); try{ XercesDOMParser *parser = new XercesDOMParser; parser->setValidationScheme(XercesDOMParser::Val_Never); parser->parse(xml_file); DOMDocument *doc = parser->getDocument(); DOMElement* root = doc->getDocumentElement(); DOMNodeList* nl = root->getElementsByTagName( XMLString::transcode("bit:b") ); if(nl->getLength() > 0){ DOMNode* dn = nl->item(0); string v = string(XMLString::transcode(dn->getLastChild()->getNodeValue()) ); ret->set_string(v); } } catch(const DOMException& toCatch) { char *message = XMLString::transcode(toCatch.msg); cout << "DOMException message is:" << message << endl; XMLString::release(&message); } catch(...){ cout << "Ups" << endl; } return ret; } |
Line 157: | Line 229: |
string value; | string b; |
Line 162: | Line 234: |
void warning(const SAXParseException & exc) { | void warning(const SAXParseException& exc) { |
Line 166: | Line 238: |
void error(const SAXParseException & exc) { | void error(const SAXParseException& exc) { |
Line 169: | Line 241: |
Line 177: | Line 250: |
void gen_xml(char *xsd_file) { int ret; cout << "XSD file " << xsd_file << "\n"; Data d = Data(); srand(time(0)); int val = rand(); d.set_string("asffgg" + std::to_string(val)); ofstream xml_out; xml_out.open("out.xml"); xml_out << d.to_xml() << "\n"; xml_out.close(); |
void check_xml(const char* xsd_file, const char* out_file){ |
Line 194: | Line 255: |
Grammar *g = parser->loadGrammar(xsd_file, Grammar::SchemaGrammarType); |
Grammar *g = parser->loadGrammar(xsd_file, Grammar::SchemaGrammarType); |
Line 198: | Line 258: |
}; | } |
Line 205: | Line 265: |
parser->parse("out.xml"); cout << "XML is okay !\n"; } catch(const XMLException & toCatch) { |
parser->parse(out_file); cout << "XML is okay !" << endl; } catch(const XMLException& toCatch) { |
Line 210: | Line 270: |
cout << "XMLException message is: \n" << message << "\n"; | cout << "XMLException message is: " << message << endl; |
Line 213: | Line 273: |
catch(const DOMException & toCatch) { | catch(const DOMException& toCatch) { |
Line 215: | Line 275: |
cout << "DOMException message is: \n" << message << "\n"; | cout << "DOMException message is: " << message << endl; |
Line 218: | Line 278: |
catch(const SAXParseException & toCatch) { | catch(const SAXParseException& toCatch) { |
Line 220: | Line 280: |
cout << "SAXParseException message is: \n" << message << "\n"; | cout << "SAXParseException message is: " << message << endl; |
Line 223: | Line 283: |
catch(const ErrorLoadXSDException * toCatch) { cout << "ErrorLoadXSDException message is: \n" << toCatch->what() << "\n"; |
catch(const ErrorLoadXSDException* toCatch) { cout << "ErrorLoadXSDException message is: " << toCatch->what() << endl; |
Line 232: | Line 291: |
delete parser; } void gen_json() { cout << "Print JSON" << "\n"; } int menu(char *file_xsd) { cout << "\nUsing " << file_xsd << "\n"; std::cout << "1) Generate XML\r\n"; std::cout << "2) Generate JSON\r\n"; std::cout << "3) Quit\r\n"; |
delete parser; } void gen_xml(const char*xsd_file, const char* out_file, Data* d){ int ret; cout << "XSD file " << xsd_file << endl; ofstream xml_out; xml_out.open(out_file); xml_out << d->to_xml(xsd_file) << endl; xml_out.close(); cout << "Generated XML file " << out_file << endl; check_xml(xsd_file,out_file); } void gen_json(const char* out_file, Data* d) { ofstream json_out; json_out.open(out_file); json_out << d->to_json() << endl; json_out.close(); cout << "Generated JSON file " << out_file << endl; } int menu(char* file_xsd) { cout << endl << "Using " << file_xsd << endl; std::cout << "1) Generate XML" << endl; std::cout << "2) Generate JSON" << endl; std::cout << "3) Quit" << std::endl; |
Line 249: | Line 321: |
return std::stoi(option.c_str()); } int main(int argc, char **argv) { |
int ret=-1; try{ ret = std::stoi(option.c_str()); if(ret<GEN_XML || ret >EXIT){ throw OutOfRangeException(ret); } } catch(OutOfRangeException ex){ cout << ex.what() <<"\n"; } catch(...){ std::cout << "Invalid option selected!" << endl; } return ret; } int main(int argc, char** argv) { |
Line 257: | Line 341: |
catch(const XMLException & toCatch) { | catch(const XMLException& toCatch) { |
Line 262: | Line 346: |
int res = -1; while (res != 3) { |
int res = NO_OPTION; while (res != EXIT) { |
Line 265: | Line 350: |
if (res == 1) gen_xml(argv[1]); if (res == 2) gen_json(); |
if (res == GEN_XML){ Data d = Data(); srand(time(0)); int val = rand(); d.set_string("xml" + std::to_string(val) ); gen_xml(argv[1],OUT_XML,&d); } if (res == GEN_JSON){ Data* d=Data::from_xml(OUT_XML); gen_json("out.json",d); } |
Line 275: | Line 369: |
CPlusPlus
C++ programming language created by Bjarne Stroustrup
Sample app for Linux
1 /*
2 gcc test.cpp -lstdc++ -o test
3 cpp test.cpp -o test
4 g++ test.cpp -o test
5 */
6
7 #include<vector>
8 #include<iostream>
9 #include<sstream>
10
11 class Teste{
12 public:
13 Teste(std::string xyz){
14 std::cout << xyz << "\r\n";
15 }
16 };
17
18 int main(){
19 std::vector<int> listInst = std::vector<int>();
20 std::cout <<"Hello world \r\n";
21 std::ostringstream oss(std::ostringstream::out);
22 int a = 1234;
23 char xpto[16]="AAA BBB";
24 oss << a << xpto << "\r\n";
25
26 std::cout << oss.str();
27 //std::ostringstream ost = std::ostringstream(ostringstream::out);
28 Teste x1("Ola aaaaaaa");
29 return 0;
30 }
31
32 /*
33 To compile in gcc container
34 g++ test.cpp -o test
35
36 g++ (GCC) 8.2.0
37 */
38 #include<vector>
39 #include<iostream>
40 #include<sstream>
41 #include<random>
42 #include<ctime>
43
44 class Teste{
45 public:
46 Teste(std::string xyz){
47 std::cout << xyz << "\r\n";
48 }
49 };
50
51 int main(){
52 std::vector<int> listInst = std::vector<int>();
53 std::cout <<"Hello world \r\n";
54 std::ostringstream oss(std::ostringstream::out);
55 int a = 1234;
56 char xpto[16]="AAA BBB";
57 oss << a << xpto << "\r\n";
58
59 std::cout << oss.str();
60 //std::ostringstream ost = std::ostringstream(ostringstream::out);
61 Teste x1("Ola aaaaaaa");
62 srand( (unsigned) time(0) );
63 std::cout << rand() <<"\n";
64 std::cout << rand() <<"\n";
65 std::cout << std::to_string(1234) << " " << std::to_string(34.56) << "\n";
66 return 0;
67 }
dummy1.cpp
1 /**
2 g++ dummy1.cpp -o dummy1
3 */
4 #include <iostream>
5 #include <string>
6 using namespace std;
7
8 int main(){
9 std::string input;
10 std::cout <<"Hello world \r\n";
11 cout << ">";
12 std::cin >> input;
13 std::cout << input << "!!!\n";
14 char x[128];
15 std::cout << ">";
16 cin >> x;
17 std::cout << x << "???\n";
18 return 0;
19 }
challenge.cpp
1 /**
2 g++ challenge.cpp -o challenge -lxml2
3 g++ challenge.cpp -o challenge -lstdc++ -lxml2 -I/usr/include/libxml2
4 g++ challenge.cpp -o challenge -lstdc++ -lxerces-c
5 // added random ...
6 g++ challenge.cpp -o challenge -lstdc++ -lxerces-c -std=c++11
7 indent -kr -nut challenge.cpp
8 */
9 #include <iostream>
10 #include <string>
11 #include <typeinfo>
12 #include <exception>
13 #include <fstream>
14 #include <random>
15 #include <ctime>
16 #include <cstdlib>
17 #include <cstdio>
18 #include <xercesc/util/PlatformUtils.hpp>
19 #include <xercesc/parsers/XercesDOMParser.hpp>
20 #include <xercesc/dom/DOM.hpp>
21 #include <xercesc/sax/SAXParseException.hpp>
22 #include <xercesc/sax/HandlerBase.hpp>
23 #include <xercesc/dom/DOM.hpp>
24 #include <xercesc/dom/DOMDocument.hpp>
25 #include <xercesc/dom/DOMNodeList.hpp>
26 #include <xercesc/dom/DOMNode.hpp>
27 using namespace std;
28 using namespace xercesc;
29
30 const int NO_OPTION=-1;
31 const int GEN_XML=1;
32 const int GEN_JSON=2;
33 const int EXIT=3;
34 const char* OUT_XML="out.xml";
35
36 class ErrorLoadXSDException:public exception {
37 public:
38 virtual const char *what() const throw() {
39 return "Error loading XSD";
40 }
41 };
42
43 class OutOfRangeException:public exception {
44 public:
45
46 OutOfRangeException(int value){
47 this->value = value;
48 this->message = new char[128];
49 }
50
51 virtual const char *what() const throw() {
52 char ret[128];
53 sprintf(this->message,"%d out of range!",this->value);
54 return this->message;
55 }
56
57 private:
58 int value;
59 char* message;
60 };
61
62 class Data {
63 public:
64
65 Data(){
66 this->b = "";
67 }
68
69 virtual void set_string(string value) {
70 this->b = value;
71 }
72
73 virtual string get_string() {
74 return this->b;
75 }
76
77 virtual string to_xml(const char* xsd_file) {
78 string ret = "";
79 ret += "<bit:a xmlns:bit=\"http://bitarus.allowed.org/types\" ";
80 ret += "xsi:schemaLocation=\"http://bitarus.allowed.org/types " + string(xsd_file) + "\" ";
81 ret += "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n";
82 ret += "<bit:b>";
83 ret += this->b;
84 ret += "</bit:b>\n";
85 ret += "</bit:a>\n";
86 return ret;
87 }
88
89 virtual string to_json() {
90 string ret = "";
91 ret += "{\"a\": \n";
92 ret += " {\"b\":\"";
93 ret += this->b;
94 ret += "\"} \n";
95 ret += "} ";
96 return ret;
97 }
98
99 static Data* from_xml(const char* xml_file){
100 Data* ret = new Data();
101 try{
102 XercesDOMParser *parser = new XercesDOMParser;
103 parser->setValidationScheme(XercesDOMParser::Val_Never);
104 parser->parse(xml_file);
105 DOMDocument *doc = parser->getDocument();
106 DOMElement* root = doc->getDocumentElement();
107 DOMNodeList* nl = root->getElementsByTagName( XMLString::transcode("bit:b") );
108
109 if(nl->getLength() > 0){
110 DOMNode* dn = nl->item(0);
111 string v = string(XMLString::transcode(dn->getLastChild()->getNodeValue()) );
112 ret->set_string(v);
113 }
114 }
115 catch(const DOMException& toCatch) {
116 char *message = XMLString::transcode(toCatch.msg);
117 cout << "DOMException message is:" << message << endl;
118 XMLString::release(&message);
119 }
120 catch(...){
121 cout << "Ups" << endl;
122 }
123
124 return ret;
125 }
126
127 private:
128 string b;
129 };
130
131 class MyErrorHandler:public HandlerBase {
132 public:
133 void warning(const SAXParseException& exc) {
134 throw exc;
135 }
136
137 void error(const SAXParseException& exc) {
138 throw exc;
139 }
140
141 void fatalError(const SAXParseException & exc) {
142 throw exc;
143 }
144
145 void resetErrors() {
146 }
147 };
148
149 void check_xml(const char* xsd_file, const char* out_file){
150 XercesDOMParser *parser = new XercesDOMParser();
151 MyErrorHandler *errorHandler = new MyErrorHandler();
152
153 try {
154 Grammar *g = parser->loadGrammar(xsd_file, Grammar::SchemaGrammarType);
155 if (g == NULL) {
156 throw new ErrorLoadXSDException();
157 }
158
159 parser->setErrorHandler(errorHandler);
160 parser->setDoSchema(true);
161 parser->setDoNamespaces(true);
162 parser->setValidationSchemaFullChecking(true);
163 parser->setValidationScheme(XercesDOMParser::Val_Always);
164 parser->parse(out_file);
165 cout << "XML is okay !" << endl;
166 }
167 catch(const XMLException& toCatch) {
168 char *message = XMLString::transcode(toCatch.getMessage());
169 cout << "XMLException message is: " << message << endl;
170 XMLString::release(&message);
171 }
172 catch(const DOMException& toCatch) {
173 char *message = XMLString::transcode(toCatch.msg);
174 cout << "DOMException message is: " << message << endl;
175 XMLString::release(&message);
176 }
177 catch(const SAXParseException& toCatch) {
178 char *message = XMLString::transcode(toCatch.getMessage());
179 cout << "SAXParseException message is: " << message << endl;
180 XMLString::release(&message);
181 }
182 catch(const ErrorLoadXSDException* toCatch) {
183 cout << "ErrorLoadXSDException message is: " << toCatch->what() << endl;
184 }
185 catch(...) {
186 cout << "Unexpected Exception \n";
187 }
188
189 delete errorHandler;
190 delete parser;
191 }
192
193 void gen_xml(const char*xsd_file, const char* out_file, Data* d){
194 int ret;
195 cout << "XSD file " << xsd_file << endl;
196 ofstream xml_out;
197 xml_out.open(out_file);
198 xml_out << d->to_xml(xsd_file) << endl;
199 xml_out.close();
200 cout << "Generated XML file " << out_file << endl;
201 check_xml(xsd_file,out_file);
202 }
203
204 void gen_json(const char* out_file, Data* d) {
205 ofstream json_out;
206 json_out.open(out_file);
207 json_out << d->to_json() << endl;
208 json_out.close();
209 cout << "Generated JSON file " << out_file << endl;
210 }
211
212 int menu(char* file_xsd) {
213 cout << endl << "Using " << file_xsd << endl;
214 std::cout << "1) Generate XML" << endl;
215 std::cout << "2) Generate JSON" << endl;
216 std::cout << "3) Quit" << std::endl;
217 std::cout << ">";
218 string option;
219 std::cin >> option;
220 int ret=-1;
221 try{
222 ret = std::stoi(option.c_str());
223 if(ret<GEN_XML || ret >EXIT){
224 throw OutOfRangeException(ret);
225 }
226 }
227 catch(OutOfRangeException ex){
228 cout << ex.what() <<"\n";
229 }
230 catch(...){
231 std::cout << "Invalid option selected!" << endl;
232 }
233 return ret;
234 }
235
236 int main(int argc, char** argv) {
237 try {
238 XMLPlatformUtils::Initialize();
239 }
240 catch(const XMLException& toCatch) {
241 return 1;
242 }
243
244 if (argc == 2) {
245 int res = NO_OPTION;
246 while (res != EXIT) {
247
248 res = menu(argv[1]);
249
250 if (res == GEN_XML){
251 Data d = Data();
252 srand(time(0));
253 int val = rand();
254 d.set_string("xml" + std::to_string(val) );
255 gen_xml(argv[1],OUT_XML,&d);
256 }
257
258 if (res == GEN_JSON){
259 Data* d=Data::from_xml(OUT_XML);
260 gen_json("out.json",d);
261 }
262 }
263 }
264
265 XMLPlatformUtils::Terminate();
266 return 0;
267 }