Confdump Agent  1.4.0
XmlFormatter.cpp
Go to the documentation of this file.
00001 /* 
00002  * Confdump-Agent - Dump static and runtime system configuration
00003  * Copyright (C) 2009-2012  Straton IT, SAS
00004  * 
00005  * This program is free software: you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 3 as
00007  * published by the Free Software Foundation.
00008  * 
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  * 
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #include "boost/foreach.hpp"
00019 
00020 #include "QXmlStreamWriter"
00021 #include "QFile"
00022 #include "QList"
00023 #include "QIODevice"
00024 #include "QDateTime"
00025 #include "QVariant"
00026 #include "QByteArray"
00027 #include "QString"
00028 
00029 #include "Confdump/Result.hpp"
00030 #include "Confdump/XmlFormatter.hpp"
00031 
00032 namespace
00033 {
00034 
00035 void writeCharacters( boost::shared_ptr<QXmlStreamWriter> writer, QVariant value )
00036 {
00037     if ( value.type() == QVariant::ByteArray )
00038         writer->writeCharacters( value.toByteArray().toHex() );
00039         else if ( value.type() == QVariant::Invalid )
00040         writer->writeComment( "undefined" );
00041     else if ( value.canConvert( QVariant::String ) )
00042         writer->writeCharacters( value.toString() );
00043     else
00044         writer->writeComment( QString( "data of unhandled " ) + value.typeName() + " type" );
00045 }
00046 
00047 } // ns 
00048 
00049 namespace Confdump
00050 {
00051 
00052 XmlFormatter::XmlFormatter()
00053 {
00054 }
00055 
00056 XmlFormatter::~XmlFormatter()
00057 {
00058 }
00059 
00060 void XmlFormatter::doPrintHeader( QString macAddress )
00061 {
00062     writer_.reset( new QXmlStreamWriter( device().get() ) );
00063     writer_->setAutoFormatting( true );
00064     writer_->writeStartDocument();
00065     writer_->writeStartElement( "collect" );
00066 
00067     QDateTime now = QDateTime::currentDateTime();
00068     writer_->writeAttribute( "date", now.toUTC().toString( "ddMMyyyyhhmmss" ) );
00069     writer_->writeAttribute( "timestamp", now.toUTC().toString( Qt::ISODate ) + "Z" );
00070     writer_->writeAttribute( "uuid", macAddress );
00071 }
00072 
00073 void XmlFormatter::doPrintTable( QString title, const Result &result )
00074 {
00075     writer_->writeStartElement( "query" );
00076     writer_->writeAttribute( "class", title );
00077 
00078     BOOST_FOREACH( Result::Row row, result )
00079     {
00080         QStringList names( result.propertyNames() );
00081         QList<QVariant> values( result.propertyValues( row ) );
00082 
00083         writer_->writeStartElement( "item" );
00084         if( values.size() == names.size() )
00085         {
00086             for ( int i = 0; i < names.size(); ++i )
00087             {
00088                 writer_->writeStartElement( "attr" );
00089                 writer_->writeAttribute( "name", names[i] );
00090                 writer_->writeAttribute( "operator", "=" );
00091 
00092                 if ( values[i].type() == QVariant::List )
00093                 {
00094                     BOOST_FOREACH( QVariant element, values[i].toList() )
00095                     {
00096                                                 writer_->writeStartElement( "element" );
00097                         writeCharacters( writer_, element );
00098                                                 writer_->writeEndElement();
00099                                         }
00100                 }
00101                 else
00102                 {
00103                     writeCharacters( writer_, values[i] );
00104                 }
00105 
00106                 writer_->writeEndElement();
00107             }
00108         }
00109         writer_->writeEndElement();
00110     }
00111 
00112     writer_->writeEndElement();
00113 }
00114 
00115 void XmlFormatter::doPrintFooter()
00116 {
00117     writer_->writeEndElement();
00118 }
00119 
00120 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends