1 |
|
#include "XMLFormatter.h" |
2 |
|
|
3 |
|
XMLFormatter::XMLFormatter( string newHostInfo ){ |
4 |
– |
// std::cout << "DEBUG: " << newHostInfo << "\n"; |
5 |
– |
xmlData += "<"; |
6 |
– |
xmlData += newHostInfo; |
7 |
– |
xmlData += ">"; |
4 |
|
|
5 |
< |
hostInfo = newHostInfo; |
10 |
< |
// std::cout << "DEBUG: xmldata: " << xmlData << "\n"; |
5 |
> |
if ( newHostInfo != "" ){ |
6 |
|
|
7 |
+ |
xmlData += "<"; |
8 |
+ |
xmlData += newHostInfo; |
9 |
+ |
xmlData += ">"; |
10 |
+ |
hostInfo = newHostInfo; |
11 |
+ |
|
12 |
+ |
} // if |
13 |
|
stackPointer = 0; |
14 |
< |
} |
14 |
> |
|
15 |
> |
} // XMLFormatter |
16 |
|
|
17 |
|
XMLFormatter::XMLFormatter(){ |
18 |
< |
// std::cout << "DEBUG: " << "No Root info" << "\n"; |
18 |
> |
|
19 |
|
hostInfo = ""; // null |
20 |
|
xmlData = ""; |
21 |
|
stackPointer = 0; |
22 |
< |
} |
22 |
> |
|
23 |
> |
} // XMLFormatter |
24 |
|
|
25 |
|
XMLFormatter::XMLFormatter( string newHostInfo, string attributes){ |
26 |
< |
// std::cout << "DEBUG: " << newHostInfo << ":" << attributes << "\n"; |
26 |
> |
|
27 |
|
xmlData += "<"; |
28 |
|
xmlData += newHostInfo; |
29 |
|
xmlData += " "; |
33 |
|
hostInfo = newHostInfo; |
34 |
|
|
35 |
|
stackPointer = 0; |
36 |
< |
} |
36 |
> |
} // XMLFormatter |
37 |
|
|
38 |
|
void XMLFormatter::closeNest(){ |
39 |
< |
// std::cout << "DEBUG: Closing Nest: " << stackPointer << ":" << stack[stackPointer] << "\n"; |
39 |
> |
|
40 |
|
if ( stackPointer >= 0 ){ |
41 |
|
|
42 |
|
stackPointer--; |
43 |
|
xmlData += "</"; |
44 |
|
xmlData += stack[stackPointer]; |
45 |
|
xmlData += ">"; |
46 |
< |
} |
46 |
> |
} // if |
47 |
|
|
48 |
|
return; |
49 |
< |
} |
49 |
> |
} // closeNest |
50 |
|
|
51 |
|
void XMLFormatter::addNest(string nest){ |
49 |
– |
// std::cout << "DEBUG: Adding Nest: " << nest << "\n"; |
50 |
– |
xmlData += "<"; |
51 |
– |
xmlData += nest; |
52 |
– |
xmlData += ">"; |
53 |
– |
|
54 |
– |
// now add the nest to the stack |
55 |
– |
stack[stackPointer] = nest; |
56 |
– |
stackPointer++; |
57 |
– |
return; |
58 |
– |
} |
52 |
|
|
53 |
+ |
// check it is not empty |
54 |
+ |
if ( nest != "" ){ |
55 |
+ |
xmlData += "<"; |
56 |
+ |
xmlData += nest; |
57 |
+ |
xmlData += ">"; |
58 |
+ |
|
59 |
+ |
// now add the nest to the stack |
60 |
+ |
stack[stackPointer] = nest; |
61 |
+ |
stackPointer++; |
62 |
+ |
} // if |
63 |
+ |
|
64 |
+ |
return; |
65 |
+ |
} // addNest |
66 |
+ |
|
67 |
|
void XMLFormatter::addElement(string element, string attributes, string value){ |
68 |
< |
// std::cout << "DEBUG: Add Element: " << element << ":" << attributes << ":" << value << "\n"; |
68 |
> |
|
69 |
|
xmlData += "<"; |
70 |
|
xmlData += element; |
71 |
|
xmlData += " "; |
72 |
|
xmlData += attributes; |
73 |
|
xmlData += ">"; |
74 |
< |
if ( value != "" ){ |
74 |
> |
xmlData += value; |
75 |
> |
xmlData += "</"; |
76 |
> |
xmlData += element; |
77 |
> |
xmlData += ">"; |
78 |
> |
|
79 |
> |
return; |
80 |
> |
} // addElement |
81 |
> |
|
82 |
> |
void XMLFormatter::addElement(string element, string value){ |
83 |
> |
|
84 |
> |
if (( element != "" ) && ( value != "" )){ |
85 |
> |
xmlData += "<"; |
86 |
> |
xmlData += element; |
87 |
> |
xmlData += ">"; |
88 |
|
xmlData += value; |
89 |
|
xmlData += "</"; |
90 |
|
xmlData += element; |
91 |
|
xmlData += ">"; |
92 |
|
} // if |
93 |
|
return; |
94 |
< |
} |
94 |
> |
|
95 |
> |
} // addElement |
96 |
|
|
76 |
– |
void XMLFormatter::addElement(string element, string value){ |
77 |
– |
// std::cout << "DEBUG: Add Element: " << element << ":" << value << "\n"; |
78 |
– |
xmlData += "<"; |
79 |
– |
xmlData += element; |
80 |
– |
xmlData += ">"; |
81 |
– |
xmlData += value; |
82 |
– |
xmlData += "</"; |
83 |
– |
xmlData += element; |
84 |
– |
xmlData += ">"; |
85 |
– |
return; |
86 |
– |
} |
97 |
|
|
88 |
– |
|
98 |
|
string XMLFormatter::returnXML(){ |
99 |
|
// close as many nests as we have open, could (but shouldn't) cause |
100 |
|
// some arraylist out of bounds errors. |
101 |
|
for ( int count= stackPointer; count > 0; count-- ){ |
102 |
|
// close nest |
103 |
|
closeNest(); |
104 |
< |
} |
104 |
> |
} // if |
105 |
|
|
106 |
|
if ( hostInfo.length() > 0 ){ |
107 |
|
xmlData += "</"; |
108 |
|
xmlData += hostInfo; |
109 |
|
xmlData += ">"; |
110 |
< |
} |
110 |
> |
} // if |
111 |
|
|
112 |
|
stackPointer = 0; |
113 |
|
|
105 |
– |
// std::cout << "DEBUG: returning XML: " << xmlData; |
114 |
|
return xmlData; |
115 |
< |
} |
115 |
> |
|
116 |
> |
} // returnXML |