/
cpXmlCmdOutput.cpp
213 lines (174 loc) · 7.13 KB
/
cpXmlCmdOutput.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
/*
* File: cpXmlCmdOutput.cpp
* Author: Jay Sprenkle
*
* Created on April 28, 2015
*/
#include <stdexcept>
#include "cpXmlCmdOutput.h"
cpXmlCmdOutput::cpXmlCmdOutput( const char* ResultDocumentFileName )
: encoding( (const xmlChar*) "ISO-8859-1" )
, cpXmlCmdNamespace( (const xmlChar*) "http://www.XmlCommandLine.org/cpXmlCmd/1.0" )
{
/* Create a new XmlWriter for uri, with no compression. */
writer = xmlNewTextWriterFilename( ResultDocumentFileName, 0 );
if ( writer == NULL )
throw ::std::runtime_error( "Error creating the xml writer" );
/* Start the document with the xml default for the version,
* encoding ISO 8859-1 and the default for the standalone
* declaration. */
if ( xmlTextWriterStartDocument( writer, NULL, (const char*)encoding, NULL ) < 0 )
throw ::std::runtime_error( "Error starting the output document" );
// Start root element
if ( xmlTextWriterStartElementNS( writer, NULL, (xmlChar *) "cpXmlCmdLog", cpXmlCmdNamespace ) < 0 )
throw ::std::runtime_error( "Error writing to the output document" );
}
cpXmlCmdOutput::~cpXmlCmdOutput()
{
/* Here we could close open elements but since we do not want to
* write any other elements, we simply call xmlTextWriterEndDocument,
* which will do all the work */
if ( xmlTextWriterEndDocument( writer ) < 0 )
throw ::std::runtime_error( "Error ending to the output document" );
xmlFreeTextWriter( writer );
}
void cpXmlCmdOutput::Context( const ::std::string& args, const ::std::string& User, const ::std::string& Host )
{
// Start Context element
if ( xmlTextWriterStartElement( writer, (xmlChar *) "Context" ) < 0 )
throw ::std::runtime_error( "Error writing to the output document" );
if ( xmlTextWriterStartElement( writer, (xmlChar *) "Environment" ) < 0 )
throw ::std::runtime_error( "Error writing to the output document" );
xmlChar* tmp;
tmp = ConvertInput( (xmlChar*)args.c_str(), encoding );
if ( xmlTextWriterWriteElement( writer, (xmlChar *) "args", tmp ) < 0 )
throw ::std::runtime_error( "Error writing to the output document" );
if ( tmp != NULL )
xmlFree( tmp );
tmp = ConvertInput( (xmlChar*)User.c_str(), encoding );
if ( xmlTextWriterWriteElement( writer, (xmlChar *) "User", tmp ) < 0 )
throw ::std::runtime_error( "Error writing to the output document" );
if ( tmp != NULL )
xmlFree( tmp );
tmp = ConvertInput( (xmlChar*)Host.c_str(), encoding );
if ( xmlTextWriterWriteElement( writer, (xmlChar *) "Host", tmp ) < 0 )
throw ::std::runtime_error( "Error writing to the output document" );
if ( tmp != NULL )
xmlFree( tmp );
// </Environment>
xmlTextWriterEndElement( writer );
// </Context>
xmlTextWriterEndElement( writer );
}
void cpXmlCmdOutput::StartContent()
{
// Start Content element
if ( xmlTextWriterStartElement( writer, (xmlChar *) "Content" ) < 0 )
throw ::std::runtime_error( "Error writing to the output document" );
}
void cpXmlCmdOutput::FinishContent()
{
// </Content>
xmlTextWriterEndElement( writer );
}
void cpXmlCmdOutput::Notes( const char* msg )
{
xmlChar* tmp = ConvertInput( (xmlChar*)msg, encoding );
if ( xmlTextWriterWriteElement( writer, (xmlChar *) "Notes", tmp ) < 0 )
throw ::std::runtime_error( "Error writing to the output document" );
if ( tmp != NULL )
xmlFree( tmp );
}
void cpXmlCmdOutput::cp( const char* source, const char* destination, time_t start )
{
xmlChar* tmp;
if ( xmlTextWriterStartElement( writer, (xmlChar*) "cp" ) < 0 )
throw ::std::runtime_error( "Error writing to the output document" );
tmp = ConvertInput( (xmlChar*)source, encoding );
if ( xmlTextWriterWriteAttribute( writer, (xmlChar *) "Source", tmp ) < 0 )
throw ::std::runtime_error( "Error writing to the output document" );
tmp = ConvertInput( (xmlChar*)destination, encoding );
if ( xmlTextWriterWriteAttribute( writer, (xmlChar *) "Destination", tmp ) < 0 )
throw ::std::runtime_error( "Error writing to the output document" );
char TimeStampBuffer[32];
strftime( TimeStampBuffer, sizeof TimeStampBuffer, "%Y-%m-%dT%H:%M:%SZ", gmtime( &start ) );
if ( xmlTextWriterWriteAttribute( writer, (xmlChar *) "Start", (xmlChar *) TimeStampBuffer ) < 0 )
throw ::std::runtime_error( "Error writing to the output document" );
xmlTextWriterEndElement( writer );
if ( tmp != NULL )
xmlFree( tmp );
}
void cpXmlCmdOutput::Status( bool success, time_t start, time_t finish )
{
if ( xmlTextWriterStartElement( writer, (xmlChar*) "Status" ) < 0 )
throw ::std::runtime_error( "Error writing to the output document" );
if ( xmlTextWriterWriteAttribute( writer, (xmlChar *) "success", (xmlChar *) ( success ? "true" : "false" ) ) < 0 )
throw ::std::runtime_error( "Error writing to the output document" );
char TimeStampBuffer[32];
strftime( TimeStampBuffer, sizeof(TimeStampBuffer), "%Y-%m-%dT%H:%M:%SZ", gmtime( &start ) );
if ( xmlTextWriterWriteAttribute( writer, (xmlChar *) "Start", (xmlChar *) TimeStampBuffer ) < 0 )
throw ::std::runtime_error( "Error writing to the output document" );
strftime( TimeStampBuffer, sizeof(TimeStampBuffer), "%Y-%m-%dT%H:%M:%SZ", gmtime( &finish ) );
if ( xmlTextWriterWriteAttribute( writer, (xmlChar *) "Finish", (xmlChar *) TimeStampBuffer ) < 0 )
throw ::std::runtime_error( "Error writing to the output document" );
xmlTextWriterEndElement( writer );
}
/**
* ConvertInput:
* @in: string in a given encoding
* @encoding: the encoding used
*
* Converts @in into UTF-8 for processing with libxml2 APIs
*
* Returns the converted UTF-8 string, or NULL in case of error.
*/
xmlChar* cpXmlCmdOutput::ConvertInput( const xmlChar* in, const xmlChar* encoding )
{
xmlChar *out;
int ret;
int size;
int out_size;
int temp;
xmlCharEncodingHandlerPtr handler;
if ( in == 0 )
return 0;
handler = xmlFindCharEncodingHandler( (const char *)encoding );
if ( !handler )
{
printf( "ConvertInput: no encoding handler found for '%s'\n", encoding != 0 ? (const char *)encoding : "" );
return 0;
}
size = (int) strlen( (const char*) in ) + 1;
out_size = size * 2 - 1;
out = (xmlChar *) xmlMalloc( (size_t) out_size );
if ( out != 0 )
{
temp = size - 1;
ret = handler->input( out, &out_size, in, &temp );
if ( ( ret < 0 ) || ( temp - size + 1 ) )
{
if ( ret < 0 )
{
printf( "ConvertInput: conversion wasn't successful.\n" );
}
else
{
printf
( "ConvertInput: conversion wasn't successful. converted: %i octets.\n",
temp );
}
xmlFree( out );
out = 0;
}
else
{
out = (xmlChar *) xmlRealloc( out, out_size + 1 );
out[out_size] = 0; //null termination
}
}
else
{
printf( "ConvertInput: no mem\n" );
}
return out;
}