This repository has been archived by the owner on Jun 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cppstax.cpp
225 lines (187 loc) · 6.58 KB
/
cppstax.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/* Copyright (C) 2017-2018 Stephan Kreutzer
*
* This file is part of CppStAX.
*
* CppStAX is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License version 3 or any later
* version of the license, as published by the Free Software Foundation.
*
* CppStAX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License 3 for more details.
*
* You should have received a copy of the GNU Affero General Public License 3
* along with CppStAX. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file $/cppstax.cpp
* @brief Demo program.
* @author Stephan Kreutzer
* @since 2017-08-28
*/
#include "XMLInputFactory.h"
#include <memory>
#include <iostream>
#include <fstream>
typedef std::unique_ptr<cppstax::XMLEventReader> XMLEventReader;
typedef std::unique_ptr<cppstax::XMLEvent> XMLEvent;
int Run(std::istream& aStream);
int main(int argc, char* argv[])
{
std::cout << "CppStAX Copyright (C) 2017-2018 Stephan Kreutzer\n"
<< "This program comes with ABSOLUTELY NO WARRANTY.\n"
<< "This is free software, and you are welcome to redistribute it\n"
<< "under certain conditions. See the GNU Affero General Public License 3\n"
<< "or any later version for details. Also, see the source code repository\n"
<< "https://github.com/publishing-systems/CppStAX/ and\n"
<< "the project website http://www.publishing-systems.org.\n"
<< std::endl;
std::unique_ptr<std::ifstream> pStream = nullptr;
try
{
if (argc >= 2)
{
pStream = std::unique_ptr<std::ifstream>(new std::ifstream);
pStream->open(argv[1]);
if (pStream->is_open() != true)
{
std::cout << "Couldn't open input file '" << argv[1] << "'.";
return -1;
}
Run(*pStream);
pStream->close();
}
else
{
std::cout << "Please enter your XML input data, confirm with Ctrl+D:" << std::endl;
Run(std::cin);
}
}
catch (std::exception* pException)
{
std::cout << "Exception: " << pException->what() << std::endl;
if (pStream != nullptr)
{
if (pStream->is_open() == true)
{
pStream->close();
}
}
return -1;
}
return 0;
}
int Run(std::istream& aStream)
{
cppstax::XMLInputFactory aFactory;
XMLEventReader pReader = aFactory.createXMLEventReader(aStream);
// Instead of looking at XMLEvents sequentially, one could
// also implement a "parse tree" to react to XMLEvents, so
// writing state machines can be avoided because of the
// implicit call tree context, pretty much like CppStAX
// does it's parsing.
while (pReader->hasNext() == true)
{
XMLEvent pEvent = pReader->nextEvent();
if (pEvent->isStartElement() == true)
{
cppstax::StartElement& aStartElement = pEvent->asStartElement();
const cppstax::QName& aName = aStartElement.getName();
std::string strTag("<");
std::string strPrefix(aName.getPrefix());
if (!strPrefix.empty())
{
strTag += strPrefix;
strTag += ":";
}
strTag += aName.getLocalPart();
for (std::list<std::shared_ptr<cppstax::Attribute>>::iterator iter = aStartElement.getAttributes()->begin();
iter != aStartElement.getAttributes()->end();
iter++)
{
const cppstax::QName& aAttributeName = (*iter)->getName();
strTag += " ";
std::string strAttributePrefix(aAttributeName.getPrefix());
if (!strAttributePrefix.empty())
{
strTag += strAttributePrefix;
strTag += ":";
}
strTag += aAttributeName.getLocalPart();
strTag += "=\"";
const std::string& strCharacters((*iter)->getValue());
for (std::string::const_iterator iter = strCharacters.begin();
iter != strCharacters.end();
iter++)
{
switch (*iter)
{
case '\"':
strTag += """;
break;
default:
strTag += *iter;
break;
}
}
strTag += "\"";
}
strTag += ">";
std::cout << strTag;
}
else if (pEvent->isEndElement() == true)
{
cppstax::EndElement& aEndElement = pEvent->asEndElement();
cppstax::QName aName = aEndElement.getName();
std::string strTag("</");
std::string strPrefix(aName.getPrefix());
if (!strPrefix.empty())
{
strTag += strPrefix;
strTag += ":";
}
strTag += aName.getLocalPart();
strTag += ">";
std::cout << strTag;
}
else if (pEvent->isCharacters() == true)
{
cppstax::Characters& aCharacters = pEvent->asCharacters();
const std::string& strCharacters(aCharacters.getData());
for (std::string::const_iterator iter = strCharacters.begin();
iter != strCharacters.end();
iter++)
{
switch (*iter)
{
case '&':
std::cout << "&";
break;
case '<':
std::cout << "<";
break;
case '>':
std::cout << ">";
break;
default:
std::cout << *iter;
break;
}
}
}
else if (pEvent->isComment() == true)
{
std::cout << "<!--" << pEvent->asComment().getText() << "-->";
}
else if (pEvent->isProcessingInstruction() == true)
{
const cppstax::ProcessingInstruction& aPI = pEvent->asProcessingInstruction();
std::cout << "<?" << aPI.getTarget() << " " << aPI.getData() << "?>";
}
else
{
}
}
return 0;
}