Go to the documentation of this file.00001
00003
00004 #include "EventFilter/StorageManager/interface/XHTMLMaker.h"
00005
00006 #include <sstream>
00007 #include <iomanip>
00008 #include <iostream>
00009 #include <cstdio>
00010 #include <xercesc/framework/StdOutFormatTarget.hpp>
00011 #include <xercesc/framework/LocalFileFormatTarget.hpp>
00012 #include <xercesc/framework/MemBufFormatTarget.hpp>
00013
00014 using namespace std;
00015 using namespace xercesc;
00016 using namespace stor;
00017
00018 namespace
00019 {
00020
00021
00022
00023 inline XMLCh* xs_( const std::string& str )
00024 {
00025
00026
00027 return xercesc::XMLString::transcode(str.c_str());
00028 }
00029
00030 inline XMLCh* xs_(const char* str)
00031 {
00032 return xercesc::XMLString::transcode(str);
00033 }
00034 }
00035
00036
00037 XHTMLMaker::XHTMLMaker()
00038 {
00039 XMLCh* xls = xs_("ls");
00040 DOMImplementation* imp =
00041 DOMImplementationRegistry::getDOMImplementation(xls);
00042
00043 XMLCh* xhtml_s =xs_("html") ;
00044 XMLCh* p_id = xs_( "-//W3C//DTD XHTML 1.0 Strict//EN" );
00045 XMLCh* s_id = xs_( "https://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" );
00046
00047 typ_ = imp->createDocumentType( xhtml_s, p_id, s_id );
00048
00049 XMLCh* ns_uri = xs_( "https://www.w3.org/1999/xhtml" );
00050
00051 doc_ = imp->createDocument( ns_uri, xhtml_s, typ_ );
00052
00053
00054 if( !doc_ )
00055 {
00056 std::cerr << "Cannot create document" << std::endl;
00057 return;
00058 }
00059
00060 XMLCh* encoding = xs_("utf-8");
00061 doc_->setEncoding(encoding);
00062
00063 XMLCh* version = xs_("1.0");
00064 doc_->setVersion(version);
00065
00066 pageStarted_ = false;
00067
00068 writer_ =
00069 ( (DOMImplementationLS*)imp )->createDOMWriter();
00070
00071 XMLString::release(&xls);
00072 XMLString::release(&xhtml_s);
00073 XMLString::release(&p_id);
00074 XMLString::release(&s_id);
00075 XMLString::release(&ns_uri);
00076 XMLString::release(&encoding);
00077 XMLString::release(&version);
00078
00079 if( !writer_ )
00080 {
00081 std::cerr << "Cannot create DOM writer" << std::endl;
00082 return;
00083 }
00084 }
00085
00089
00090 XHTMLMaker::~XHTMLMaker()
00091 {
00092 delete doc_;
00093 delete writer_;
00094 delete typ_;
00095 }
00096
00100
00101 XHTMLMaker::Node* XHTMLMaker::start( const std::string& title )
00102 {
00103
00104 if( pageStarted_ )
00105 {
00106 std::cerr << "Page already started" << std::endl;
00107 return (Node*)0;
00108 }
00109
00110 pageStarted_ = true;
00111
00112
00113 Node* el_xhtml = doc_->getDocumentElement();
00114
00115
00116 XMLCh* h = xs_("head");
00117 head_ = doc_->createElement(h);
00118 XMLString::release(&h);
00119 el_xhtml->appendChild( head_ );
00120
00121
00122
00123 XMLCh* xtitle = xs_("title");
00124 Node* el_title = doc_->createElement(xtitle);
00125 XMLString::release(&xtitle);
00126 head_->appendChild( el_title );
00127 xtitle = xs_(title);
00128 DOMText* txt_title = doc_->createTextNode(xtitle);
00129 XMLString::release(&xtitle);
00130 el_title->appendChild( txt_title );
00131
00132
00133 XMLCh* xbody = xs_("body");
00134 Node* el_body = doc_->createElement(xbody);
00135 XMLString::release(&xbody);
00136 el_xhtml->appendChild( el_body );
00137
00138 return el_body;
00139
00140 }
00141
00145
00146 XHTMLMaker::Node* XHTMLMaker::addNode( const std::string& name,
00147 XHTMLMaker::Node* parent,
00148 const AttrMap& attrs )
00149 {
00150 XMLCh* xname = xs_(name);
00151 Node* el = doc_->createElement(xname);
00152 XMLString::release(&xname);
00153 parent->appendChild( el );
00154
00155 for( AttrMap::const_iterator i = attrs.begin(); i != attrs.end();
00156 ++i )
00157 {
00158 XMLCh* xfirst = xs_(i->first);
00159 XMLCh* xsecond = xs_(i->second);
00160 el->setAttribute(xfirst, xsecond);
00161 XMLString::release(&xfirst);
00162 XMLString::release(&xsecond);
00163 }
00164
00165 return el;
00166
00167 }
00168
00172
00173 void XHTMLMaker::addText( Node* parent, const std::string& data )
00174 {
00175 XMLCh* xdata = xs_(data);
00176 DOMText* txt = doc_->createTextNode(xdata);
00177 XMLString::release(&xdata);
00178 parent->appendChild( txt );
00179 }
00180
00184 void XHTMLMaker::addInt( Node* parent, const int& value )
00185 {
00186 ostringstream tmpString;
00187 tmpString << value;
00188 addText( parent, tmpString.str() );
00189 }
00190
00194 void XHTMLMaker::addInt( Node* parent, const unsigned int& value )
00195 {
00196 ostringstream tmpString;
00197 tmpString << value;
00198 addText( parent, tmpString.str() );
00199 }
00200
00204 void XHTMLMaker::addInt( Node* parent, const long& value )
00205 {
00206 ostringstream tmpString;
00207 tmpString << value;
00208 addText( parent, tmpString.str() );
00209 }
00210
00214 void XHTMLMaker::addInt( Node* parent, const unsigned long& value )
00215 {
00216 ostringstream tmpString;
00217 tmpString << value;
00218 addText( parent, tmpString.str() );
00219 }
00220
00224 void XHTMLMaker::addInt( Node* parent, const long long& value )
00225 {
00226 ostringstream tmpString;
00227 tmpString << value;
00228 addText( parent, tmpString.str() );
00229 }
00230
00234 void XHTMLMaker::addInt( Node* parent, const unsigned long long& value )
00235 {
00236 ostringstream tmpString;
00237 tmpString << value;
00238 addText( parent, tmpString.str() );
00239 }
00240
00244 void XHTMLMaker::addHex( Node* parent, const unsigned long& value )
00245 {
00246 ostringstream tmpString;
00247 tmpString << "0x" << std::hex << value;
00248 addText( parent, tmpString.str() );
00249 }
00250
00254 void XHTMLMaker::addDouble( Node* parent, const double& value, const unsigned int& precision )
00255 {
00256 ostringstream tmpString;
00257 tmpString << fixed << std::setprecision( precision ) << value;
00258 addText( parent, tmpString.str() );
00259 }
00260
00264 void XHTMLMaker::addBool( Node* parent, const bool& value )
00265 {
00266 addText( parent, value ? "True" : "False" );
00267 }
00268
00272
00273 void XHTMLMaker::setWriterFeatures_()
00274 {
00275
00276
00277
00278 if( writer_->canSetFeature( XMLUni::fgDOMWRTSplitCdataSections, true ) )
00279 {
00280 writer_->setFeature( XMLUni::fgDOMWRTSplitCdataSections, true );
00281 }
00282
00283 if( writer_->canSetFeature( XMLUni::fgDOMWRTDiscardDefaultContent, true ) )
00284 {
00285 writer_->setFeature( XMLUni::fgDOMWRTDiscardDefaultContent, true );
00286 }
00287
00288 if( writer_->canSetFeature( XMLUni::fgDOMWRTFormatPrettyPrint, true ) )
00289 {
00290 writer_->setFeature( XMLUni::fgDOMWRTFormatPrettyPrint, true );
00291 }
00292
00293 if( writer_->canSetFeature( XMLUni::fgDOMWRTBOM, true ) )
00294 {
00295 writer_->setFeature( XMLUni::fgDOMWRTBOM, true );
00296 }
00297
00298 }
00299
00303 void XHTMLMaker::out()
00304 {
00305 setWriterFeatures_();
00306 XMLFormatTarget* ftar = new StdOutFormatTarget();
00307 fflush( stdout );
00308 writer_->writeNode( ftar, *doc_ );
00309 delete ftar;
00310 }
00311
00315 void XHTMLMaker::out( const std::string& filename )
00316 {
00317 setWriterFeatures_();
00318 XMLCh* xfilename = xs_(filename);
00319 XMLFormatTarget* ftar = new LocalFileFormatTarget(xfilename);
00320 writer_->writeNode( ftar, *doc_ );
00321 XMLString::release(&xfilename);
00322 delete ftar;
00323 }
00324
00328 void XHTMLMaker::out( std::string& dest )
00329 {
00330 std::ostringstream stream;
00331 out( stream );
00332 dest = stream.str();
00333 }
00334
00338 void XHTMLMaker::out( std::ostream& dest )
00339 {
00340 setWriterFeatures_();
00341 MemBufFormatTarget* ftar = new MemBufFormatTarget();
00342 writer_->writeNode( ftar, *doc_ );
00343 dest << ftar->getRawBuffer();
00344 delete ftar;
00345 }
00346