CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
XHTMLMaker.cc
Go to the documentation of this file.
1 // $Id: XHTMLMaker.cc,v 1.12 2011/07/07 09:22:45 mommsen Exp $
3 
5 
6 #include <sstream>
7 #include <iomanip>
8 #include <iostream>
9 #include <cstdio>
10 #include <xercesc/framework/StdOutFormatTarget.hpp>
11 #include <xercesc/framework/LocalFileFormatTarget.hpp>
12 #include <xercesc/framework/MemBufFormatTarget.hpp>
13 
14 using namespace std;
15 using namespace xercesc;
16 using namespace stor;
17 
18 namespace
19 {
20  // String to XMLCh: Note that the Xerces-C++ documentation says you
21  // have to call XMLString::release(p) for every pointer p returned
22  // from XMLString::transcode().
23  inline XMLCh* xs_( const std::string& str )
24  {
25  // std::string::data() is not required to return a null-terminated
26  // byte array; c_str() is required to do so.
27  return xercesc::XMLString::transcode(str.c_str());
28  }
29 
30  inline XMLCh* xs_(const char* str)
31  {
32  return xercesc::XMLString::transcode(str);
33  }
34 }
35 
36 
37 XHTMLMaker::XHTMLMaker()
38 {
39  XMLCh* xls = xs_("ls");
40  DOMImplementation* imp =
41  DOMImplementationRegistry::getDOMImplementation(xls);
42 
43  XMLCh* xhtml_s =xs_("html") ;
44  XMLCh* p_id = xs_( "-//W3C//DTD XHTML 1.0 Strict//EN" );
45  XMLCh* s_id = xs_( "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" );
46 
47  typ_ = imp->createDocumentType( xhtml_s, p_id, s_id );
48 
49  XMLCh* ns_uri = xs_( "http://www.w3.org/1999/xhtml" );
50 
51  doc_ = imp->createDocument( ns_uri, xhtml_s, typ_ );
52 
53 
54  if( !doc_ )
55  {
56  std::cerr << "Cannot create document" << std::endl;
57  return;
58  }
59 
60  XMLCh* encoding = xs_("utf-8");
61  doc_->setEncoding(encoding);
62  //doc_->setStandalone( true );
63  XMLCh* version = xs_("1.0");
64  doc_->setVersion(version);
65 
66  pageStarted_ = false;
67 
68  writer_ =
69  ( (DOMImplementationLS*)imp )->createDOMWriter();
70 
71  XMLString::release(&xls);
72  XMLString::release(&xhtml_s);
73  XMLString::release(&p_id);
74  XMLString::release(&s_id);
75  XMLString::release(&ns_uri);
76  XMLString::release(&encoding);
77  XMLString::release(&version);
78 
79  if( !writer_ )
80  {
81  std::cerr << "Cannot create DOM writer" << std::endl;
82  return;
83  }
84 }
85 
89 
90 XHTMLMaker::~XHTMLMaker()
91 {
92  delete doc_;
93  delete writer_;
94  delete typ_;
95 }
96 
100 
102 {
103 
104  if( pageStarted_ )
105  {
106  std::cerr << "Page already started" << std::endl;
107  return (Node*)0;
108  }
109 
110  pageStarted_ = true;
111 
112  // Root element:
113  Node* el_xhtml = doc_->getDocumentElement();
114 
115  // Head:
116  XMLCh* h = xs_("head");
117  head_ = doc_->createElement(h);
118  XMLString::release(&h);
119  el_xhtml->appendChild( head_ );
120 
121 
122  // Title:
123  XMLCh* xtitle = xs_("title");
124  Node* el_title = doc_->createElement(xtitle);
125  XMLString::release(&xtitle);
126  head_->appendChild( el_title );
127  xtitle = xs_(title);
128  DOMText* txt_title = doc_->createTextNode(xtitle);
129  XMLString::release(&xtitle);
130  el_title->appendChild( txt_title );
131 
132  // Body:
133  XMLCh* xbody = xs_("body");
134  Node* el_body = doc_->createElement(xbody);
135  XMLString::release(&xbody);
136  el_xhtml->appendChild( el_body );
137 
138  return el_body;
139 
140 }
141 
145 
146 XHTMLMaker::Node* XHTMLMaker::addNode( const std::string& name,
148  const AttrMap& attrs )
149 {
150  XMLCh* xname = xs_(name);
151  Node* el = doc_->createElement(xname);
152  XMLString::release(&xname);
153  parent->appendChild( el );
154 
155  for( AttrMap::const_iterator i = attrs.begin(); i != attrs.end();
156  ++i )
157  {
158  XMLCh* xfirst = xs_(i->first);
159  XMLCh* xsecond = xs_(i->second);
160  el->setAttribute(xfirst, xsecond);
161  XMLString::release(&xfirst);
162  XMLString::release(&xsecond);
163  }
164 
165  return el;
166 
167 }
168 
172 
173 void XHTMLMaker::addText( Node* parent, const std::string& data )
174 {
175  XMLCh* xdata = xs_(data);
176  DOMText* txt = doc_->createTextNode(xdata);
177  XMLString::release(&xdata);
178  parent->appendChild( txt );
179 }
180 
184 void XHTMLMaker::addInt( Node* parent, const int& value )
185 {
186  ostringstream tmpString;
187  tmpString << value;
188  addText( parent, tmpString.str() );
189 }
190 
194 void XHTMLMaker::addInt( Node* parent, const unsigned int& value )
195 {
196  ostringstream tmpString;
197  tmpString << value;
198  addText( parent, tmpString.str() );
199 }
200 
204 void XHTMLMaker::addInt( Node* parent, const long& value )
205 {
206  ostringstream tmpString;
207  tmpString << value;
208  addText( parent, tmpString.str() );
209 }
210 
214 void XHTMLMaker::addInt( Node* parent, const unsigned long& value )
215 {
216  ostringstream tmpString;
217  tmpString << value;
218  addText( parent, tmpString.str() );
219 }
220 
224 void XHTMLMaker::addInt( Node* parent, const long long& value )
225 {
226  ostringstream tmpString;
227  tmpString << value;
228  addText( parent, tmpString.str() );
229 }
230 
234 void XHTMLMaker::addInt( Node* parent, const unsigned long long& value )
235 {
236  ostringstream tmpString;
237  tmpString << value;
238  addText( parent, tmpString.str() );
239 }
240 
244 void XHTMLMaker::addHex( Node* parent, const unsigned long& value )
245 {
246  ostringstream tmpString;
247  tmpString << "0x" << std::hex << value;
248  addText( parent, tmpString.str() );
249 }
250 
254 void XHTMLMaker::addDouble( Node* parent, const double& value, const unsigned int& precision )
255 {
256  ostringstream tmpString;
257  tmpString << fixed << std::setprecision( precision ) << value;
258  addText( parent, tmpString.str() );
259 }
260 
264 void XHTMLMaker::addBool( Node* parent, const bool& value )
265 {
266  addText( parent, value ? "True" : "False" );
267 }
268 
272 
273 void XHTMLMaker::setWriterFeatures_()
274 {
275 
276  //writer_->setNewLine( (const XMLCh*)( L"\n" ) );
277 
278  if( writer_->canSetFeature( XMLUni::fgDOMWRTSplitCdataSections, true ) )
279  {
280  writer_->setFeature( XMLUni::fgDOMWRTSplitCdataSections, true );
281  }
282 
283  if( writer_->canSetFeature( XMLUni::fgDOMWRTDiscardDefaultContent, true ) )
284  {
285  writer_->setFeature( XMLUni::fgDOMWRTDiscardDefaultContent, true );
286  }
287 
288  if( writer_->canSetFeature( XMLUni::fgDOMWRTFormatPrettyPrint, true ) )
289  {
290  writer_->setFeature( XMLUni::fgDOMWRTFormatPrettyPrint, true );
291  }
292 
293  if( writer_->canSetFeature( XMLUni::fgDOMWRTBOM, true ) )
294  {
295  writer_->setFeature( XMLUni::fgDOMWRTBOM, true );
296  }
297 
298 }
299 
304 {
305  setWriterFeatures_();
306  XMLFormatTarget* ftar = new StdOutFormatTarget();
307  fflush( stdout );
308  writer_->writeNode( ftar, *doc_ );
309  delete ftar;
310 }
311 
315 void XHTMLMaker::out( const std::string& filename )
316 {
317  setWriterFeatures_();
318  XMLCh* xfilename = xs_(filename);
319  XMLFormatTarget* ftar = new LocalFileFormatTarget(xfilename);
320  writer_->writeNode( ftar, *doc_ );
321  XMLString::release(&xfilename);
322  delete ftar;
323 }
324 
328 void XHTMLMaker::out( std::string& dest )
329 {
330  std::ostringstream stream;
331  out( stream );
332  dest = stream.str();
333 }
334 
338 void XHTMLMaker::out( std::ostream& dest )
339 {
340  setWriterFeatures_();
341  MemBufFormatTarget* ftar = new MemBufFormatTarget();
342  writer_->writeNode( ftar, *doc_ );
343  dest << ftar->getRawBuffer();
344  delete ftar;
345 }
346 
int i
Definition: DBlmapReader.cc:9
list parent
Definition: dbtoconf.py:74
static const edm::ProductID s_id
Definition: EventBase.cc:23
std::map< std::string, std::string > AttrMap
Definition: XHTMLMaker.h:36
tuple out
Definition: dbtoconf.py:99
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
tuple filename
Definition: lut2db_cfg.py:20
xercesc::DOMElement Node
Definition: XHTMLMaker.h:35