-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIdentityHandler.cpp
117 lines (94 loc) · 3.3 KB
/
IdentityHandler.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
/*
IdentityHandler.cpp
Concrete class specific to Identity inheriting from the abstract class XMLParser
*/
#include "IdentityHandler.hpp"
#include <iostream>
#include <algorithm>
int unclosedBrackets = 0;
// provides literal string operator""sv
using namespace std::literals::string_view_literals;
std::string escape(std::string_view unescaped) {
std::string unescapedString(unescaped);
std::string escapedString = "";
for(auto c: unescapedString) {
if(c == '<') {
escapedString += "<";
} else if (c == '>') {
escapedString += ">";
} else if (c == '&') {
escapedString += "&";
} else {
escapedString += c;
}
}
return escapedString;
}
// constructor
IdentityHandler::IdentityHandler() = default;
//get loc
int IdentityHandler::getLoc() {
return loc;
}
// start Document Handler
void IdentityHandler::handleStartDocument() {}
// XML Declaration Handler
void IdentityHandler::handleXMLDeclaration(std::string_view version, std::optional<std::string_view>& encoding, std::optional<std::string_view>& standalone) {
std::cout << "<?xml version=\"" << version << "\" ";
if (encoding.has_value()) {
std::cout << "encoding=\"" << encoding.value() << "\" ";
}
if (standalone.has_value()) {
std::cout << "standalone=\"" << standalone.value() << "\"";
}
std::cout << "?>" << std::endl;
}
// Start Tag Handler
void IdentityHandler::handleStartTag(std::string_view qName, std::string_view prefix, std::string_view localName) {
if (unclosedBrackets > 0) {
--unclosedBrackets;
std::cout << ">";
}
std::cout << "<" << qName;
++unclosedBrackets;
}
// End Tag Handler
void IdentityHandler::handleEndTag(std::string_view qName, std::string_view prefix, std::string_view localName) {
std::cout << "</" << qName << ">";
}
// Character Handler
void IdentityHandler::handleCharacter(std::string_view characters) {
if (unclosedBrackets > 0) {
--unclosedBrackets;
std::cout << ">";
}
std::cout << escape(characters);
loc += static_cast<int>(std::count(characters.cbegin(), characters.cend(), '\n'));
}
// attribute Handler
void IdentityHandler::handleAttribute(std::string_view qName, std::string_view prefix, std::string_view localName, std::string_view value) {
std::cout << " " << qName << "=\"" << value << "\"";
}
// XML Namespace Handler
void IdentityHandler::handleXMLNamespace(std::string_view prefix, std::string_view uri) {
std::cout << " xmlns";
if (!prefix.empty()) {
std::cout << ":" << prefix;
}
std::cout << "=\"" << uri << "\"";
}
// XML Comment Handler
void IdentityHandler::handleXMLComment(std::string_view value) {
std::cout << "<!--" << value << "-->" << std::endl;
}
// CDATA Handler
void IdentityHandler::handleCDATA(std::string_view characters) {
std::cout << "<![CDATA[" << escape(characters) << "]]>";
loc += static_cast<int>(std::count(characters.cbegin(), characters.cend(), '\n'));
}
// processing Instruction Handler
void IdentityHandler::handleProcessingInstruction(std::string_view target, std::string_view data) {
std::cout << "<?" << target << " " << data << "?>" << std::endl;
}
// end Document Handler
void IdentityHandler::handleEndDocument() {}