forked from nlp-and-learning/SimpleSaxParser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSaxFormatter.cpp
147 lines (130 loc) · 2.74 KB
/
SaxFormatter.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
#include "SaxFormatter.h"
SaxFormatter::SaxFormatter()
{
m_nIndentValue=0; m_bValue=false; m_bOpen=false;
m_IndentString="\t";
}
void SaxFormatter::DoIndent()
{
for (unsigned int i=0; i<m_nIndentValue; i++)
{
Output(m_IndentString);
}
}
void SaxFormatter::OnElementBegin(const std::string szName)
{
if (m_bOpen)
Output(">");
DoBreakLine();
DoIndent();
Output("<"); Output(ToXML(szName));
m_bOpen=true;
m_bValue=false;
Incr();
}
void SaxFormatter::OnCloseSingleElement(const std::string szName)
{
Output("/>");
Decr();
m_bOpen=false;
}
void SaxFormatter::OnElementEnd(const std::string szName)
{
if (m_bOpen)
Output(">");
if (!m_bValue && !m_bOpen)
{
DoBreakLine();
Decr();
DoIndent();
}
else
{
Decr();
}
Output("</"); Output(ToXML(szName)); Output(">");
m_bOpen=false;
m_bValue=false;
}
void SaxFormatter::OnAttribute(const std::string szName, const std::string szValue)
{
Output(" "); Output(ToXML(szName)); Output("=");
Output("\""); Output(ToXML(szValue)); Output("\"");
}
void SaxFormatter::OnCDATA(const std::string szValue)
{
if (m_bOpen)
Output(">");
DoBreakLine();
DoIndent();
Output("<![CDATA[ "); Output(ToXML(szValue)); Output(" ]]>");
m_bValue=false;
m_bOpen=false;
}
void SaxFormatter::OnComment(const std::string szText)
{
if (m_bOpen)
Output(">");
DoBreakLine();
DoIndent();
Output("<!-- "); Output(ToXML(szText)); Output(" -->");
m_bValue=false;
m_bOpen=false;
}
void SaxFormatter::OnDeclaration(const std::string szVersion, const std::string szEncoding, const std::string szStandAlone)
{
DoIndent();
Output("<?xml ");
if (!szVersion.empty())
{Output("version=\""); Output(ToXML(szVersion)); Output("\"");}
if (!szEncoding.empty())
{Output(" encoding=\""); Output(ToXML(szEncoding)); Output("\"");}
if (!szStandAlone.empty())
{Output("standalone=\""); Output(ToXML(szStandAlone)); Output("\"");}
Output(" ?>");
}
void SaxFormatter::OnProcessing(const std::string szValue)
{
if (m_bOpen)
Output(">");
DoBreakLine();
DoIndent();
Output("<? "); Output(ToXML(szValue)); Output(" ?>");
DoBreakLine();
}
void SaxFormatter::OnText(const std::string szValue)
{
if (m_bOpen)
{
Output(">");
m_bValue=true;
}
else
{
DoIndent();
}
Output(ToXML(szValue));
m_bOpen=false;
}
void SaxFormatter::SetIndentAsTab()
{
m_IndentString = "\t";
}
void SaxFormatter::SetIndentAsBlank(int n)
{
m_IndentString = "";
for (int i=0; i<n; i++)
m_IndentString += ' ';
}
inline void SaxFormatter::DoBreakLine()
{
Output("\n");
}
inline void SaxFormatter::Incr()
{
m_nIndentValue++;
}
inline void SaxFormatter::Decr()
{
m_nIndentValue--;
}