XmlController.cxx
Go to the documentation of this file.
1 
12 // for truncation warning
13 #ifdef _MSC_VER
14 #include "msdevstudio/MSconfig.h"
15 #endif
16 
17 #include "XmlController.h"
18 
19 #include "DataRepXML.h"
20 #include "HiNTupleXML.h"
21 #include "TupleCutXML.h"
22 #include "ViewBaseXML.h"
23 #include "XmlDocument.h"
24 #include "XmlElement.h"
25 
27 #include "datasrcs/DataSource.h"
29 #include "graphics/ViewBase.h"
30 #include "graphics/ViewFactory.h"
31 
32 #include <stdexcept>
33 #include <cassert>
34 
35 using std::map;
36 using std::list;
37 using std::string;
38 using std::vector;
39 
40 using namespace hippodraw;
41 
43 
45  : m_pasteboard ( 0 ),
46  m_selectboard ( 0 ),
47  m_id ( 0 )
48 {
49  m_datarep_xml = new DataRepXML ( this );
50  m_ntuple = new HiNTupleXML ( this );
51  m_tuple_cut_xml = new TupleCutXML ( this );
52  m_view = new ViewBaseXML ( this );
53 }
54 
56 {
57  delete m_pasteboard;
58  delete m_datarep_xml;
59  delete m_ntuple;
60  delete m_tuple_cut_xml;
61  delete m_view;
62 }
63 
64 void
66 createDocument ( const std::string & doc_name )
67 {
68  newDocument ( doc_name );
69  XmlElement * root = m_xml_doc->createElement ( doc_name );
70 
71  m_xml_doc->appendChild ( *root );
72  delete root;
73 }
74 
77 openDocument ( const std::string & filename )
78 {
79  newDocument ( "HippoDraw" );
80 
81  XmlDocument::Status rc = m_xml_doc->setContent ( filename );
82  if ( rc == XmlDocument::OpenError ) return OpenError;
83  if ( rc == XmlDocument::ParseError ) return ParseError;
84 
85  return Success;
86 }
87 
90 openFile ( const std::string & filename )
91 {
92  Status status = openDocument ( filename );
93  if ( status != Success ) return status;
94 
95  m_views.clear();
96  m_tuple_map.clear ();
97  m_tuple_cut_map.clear ();
98 
99  return openNTuples ();
100 }
101 
105 {
106  m_missing_tuples.clear ();
107  m_missing_ids.clear ();
108 
109  XmlElement * root = m_xml_doc->documentElement ();
110  list < XmlElement * > nodelist;
111  m_ntuple->fillNodeList ( root, nodelist );
112 
113  std::list < XmlElement * >::const_iterator first = nodelist.begin();
114 
115  while ( first != nodelist.end() ) {
116  XmlElement * element = *first++;
117  int id = element->getID ();
118  try {
119  const DataSource * ntuple = m_ntuple->getObject ( *element );
120  m_tuple_map[id] = ntuple;
121  }
122  catch ( const std::runtime_error & ) {
123  const string name = m_ntuple->getName ( *element );
124  m_missing_tuples.push_back ( name );
125  m_missing_ids.push_back ( id );
126  }
127  }
128  if ( m_missing_tuples.empty () == true ) return Success;
129 
130  return NTupleError;
131 }
132 
136 {
137  std::list < string >::iterator it;
138  int i = 0;
139  for ( it=m_missing_tuples.begin();
140  it!=m_missing_tuples.end(); ++it, ++i )
141  {
142  const DataSource * ntuple = m_ntuple->getObject ( *it );
143  if ( ntuple == 0 )
144  {
145  return NTupleError;
146  }
147  else
148  {
149  int id = m_missing_ids[i];
150  m_tuple_map[id] = ntuple;
151  }
152  }
153  return Success;
154 }
155 
156 
157 bool
160 {
161  return m_pasteboard == 0;
162 }
163 
164 const list < ViewBase * > &
167  const std::vector < const ViewBase * > & views )
168 {
170 
171  return getFromDocument ( factory, views );
172 }
173 
174 const list < ViewBase * > &
177  const std::vector < const ViewBase * > & views )
178 {
180 
181  return getFromDocument ( factory, views );
182 }
183 
184 const list < ViewBase * > &
187  const std::vector < const ViewBase * > & views )
188 {
189  fillNTupleMap ();
190  fillTupleCutMap ( views );
191  m_view_factory = factory;
192 
193  getViews ();
194 
195  CutController * controller = CutController::instance ();
196  controller->connectDataRep ( m_views, views );
197 
198  return m_views;
199 }
200 
201 const list < ViewBase * > &
203 {
204  m_view_factory = factory;
205  getViews ();
206  delete m_xml_doc; // done with it.
207 
208  return m_views;
209 }
210 
211 bool
213 areDataSourcesSaved ( const std::vector < const ViewBase * > & views )
214 {
215  bool yes = true;
216 #ifdef ITERATOR_MEMBER_DEFECT
217  std::
218 #endif
219  vector < const ViewBase * > ::const_iterator first = views.begin ();
220  while ( first != views.end () ) {
221  const ViewBase * view = *first++;
222  yes = m_view->areDataSourcesSaved ( *view );
223  if ( yes == false ) break;
224  }
225 
226  return yes;
227 }
228 
229 bool
231 serializeViews ( const std::vector < const ViewBase * > & views )
232 {
233  createDocument ( "HippoDraw" );
234 
235  if ( m_xml_doc == 0 ) return false;
236 
237  addViews ( views );
238 
239  return true;
240 }
241 
242 void
245 {
246  map < const DataSource *, XmlElement * > ::iterator first
247  = m_data.begin();
248  for ( ; first != m_data.end (); ++ first ) {
249  delete first->second;
250  }
251 
252  m_data.clear ();
253 }
254 
255 void
257 saveToDocument ( const std::vector < const ViewBase * > & views )
258 {
260  m_addr_map.clear();
261  serializeViews ( views );
262 }
263 
264 void
266 saveToPasteboard ( const std::vector < const ViewBase * > & selviews )
267 {
268  saveToDocument ( selviews );
269 
270  if ( m_pasteboard != 0 ) delete m_pasteboard;
272 }
273 
274 void
276 saveToSelectionCopy ( const std::vector < const ViewBase * > & views )
277 {
278  saveToDocument ( views );
279 
280  if ( m_selectboard != 0 ) delete m_selectboard;
282 }
283 
286 saveToFile ( const std::vector < const ViewBase * > & views,
287  const std::string & filename )
288 {
290  m_addr_map.clear();
291  serializeViews ( views );
292  XmlDocument::Status status = m_xml_doc->saveToFile ( filename );
293  delete m_xml_doc;
294 
295  if ( status != XmlDocument::Success ) return OpenError;
296 
297  return Success;
298 }
299 
300 void
302 addViews ( const std::vector < const ViewBase * > & views )
303 {
304  XmlElement * root = m_xml_doc->documentElement ();
305 
306  std::vector < const ViewBase * >::const_iterator it = views.begin();
307  for ( ; it != views.end(); ++it ) {
308  XmlElement * elem = m_view->createElement ( **it );
309 
310  root->appendChild ( *elem );
311  delete elem;
312  }
313  delete root;
314 }
315 
317 {
318  ViewBase * view = m_view_factory->createView ( plotter );
319  m_views.push_back ( view );
320 
321  return view;
322 }
323 
324 FontBase *
326 createFont () const
327 {
328  return m_view_factory -> createFont ();
329 }
330 
332 {
333  std::map < const DataSource *, XmlElement * >::const_iterator it
334  = m_data.find ( ntuple );
335  if ( it != m_data.end () ) {
336  return it->second;
337  }
338  XmlElement * element = m_ntuple->createElement ( *ntuple );
339 
340  m_data[ntuple] = element;
341 
342  XmlElement * root = m_xml_doc->documentElement ();
343  root->appendChild ( *element );
344  delete root;
345  return element;
346 }
347 
349 {
350  std::map < int, const DataSource * >::const_iterator it
351  = m_tuple_map.find ( ref );
352  if ( it == m_tuple_map.end () ) {
353  return 0;
354  }
355 
356  return it->second;
357 }
358 
360 {
361  const TupleCut * cut = 0;
362 
363  std::map < int, const TupleCut * >::iterator it
364  = m_tuple_cut_map.find ( ref );
365  if ( it == m_tuple_cut_map.end() &&
366  ref >=0 ) {
367  it = m_tuple_cut_map.find ( -ref ); // old multiDim TupleCut
368  }
369  if ( it != m_tuple_cut_map.end() ) {
370  cut = it->second;
371  }
372 
373  return cut;
374 }
375 
377 {
378  std::map < int, DataRep * >::iterator it
379  = m_datarep_map.find ( ref );
380 
381  if ( it == m_datarep_map.end() ) return 0;
382 
383  return it->second;
384 }
385 
386 PlotterBase *
388 getPlotter ( int ref )
389 {
390  std::map < int, PlotterBase * >::iterator it
391  = m_plotter_map.find ( ref );
392 
393  if ( it == m_plotter_map.end() ) return 0;
394 
395  return it->second;
396 }
397 
398 void
400 registerPlotter ( int id, PlotterBase * plotter )
401 {
402  m_plotter_map [ id ] = plotter;
403 }
404 
405 void
408 {
409  m_tuple_map.clear();
411  vector < DataSource * > tuples;
412  controller->getDataSources ( tuples ); // get all
413 #ifdef ITERATOR_MEMBER_DEFECT
414  std::
415 #endif
416  vector < DataSource * > ::const_iterator first = tuples.begin();
417  for ( ; first != tuples.end(); ++first ) {
418  const void * addr = reinterpret_cast < const void * > ( *first );
419  int ref = getId ( addr );
420  m_tuple_map[ref] = *first;
421  }
422 }
423 
424 void XmlController::
425 fillTupleCutMap ( const std::vector < const ViewBase * > & views )
426 {
427  m_tuple_cut_map.clear();
428  vector < const TupleCut * > cut_list;
429  CutController * controller = CutController::instance ();
430 
431  controller->fillTupleCutList ( views, cut_list );
432 
433  vector < const TupleCut * > :: iterator first = cut_list.begin ();
434  while ( first != cut_list.end () ) {
435  const TupleCut * cut = *first++;
436  void * addr = reinterpret_cast < void * > ( &cut );
437  int id = getId ( addr );
438  m_tuple_cut_map [ id ] = cut;
439  }
440 }
441 
443 {
444  XmlElement * root = m_xml_doc->documentElement ();
445  list < XmlElement * > nodelist;
446  m_datarep_xml->fillNodeList ( root, nodelist );
447 
448 #ifdef ITERATOR_MEMBER_DEFECT
449  std::
450 #endif
451  list < XmlElement * > ::const_iterator first = nodelist.begin();
452 
453  for ( ; first != nodelist.end(); ++first ) {
454  XmlElement * element = *first;
455  int id = element->getID ();
456  DataRep * rep = m_datarep_xml->createObject ( element );
457  m_datarep_map[id] = rep;
458  }
459 
460 }
461 
463 {
464  XmlElement * root = m_xml_doc->documentElement ();
465  list < XmlElement * > nodelist;
466  m_tuple_cut_xml->fillNodeList ( root, nodelist );
467 
468  // std:: for MS VC++ 6.0
469  std::list < XmlElement * >::const_iterator first = nodelist.begin();
470 
471  for ( ; first != nodelist.end(); ++first ) {
472  XmlElement * element = *first;
473  if ( m_tuple_cut_xml -> hasMultiDimTupleCut ( element ) ) {
474  vector < TupleCut * > cuts;
475  m_tuple_cut_xml -> getObjects ( element, cuts );
476  for ( unsigned int i = 0; i < cuts.size (); i++ ) {
477  int id = element -> getID ();
478  if ( i == 1 ) id = -id;
479  m_tuple_cut_map[id] = cuts[i];
480  }
481  }
482  else {
483  TupleCut * cut = m_tuple_cut_xml->getObject ( *element );
484  int id = element->getID ();
485  m_tuple_cut_map[id] = cut;
486  }
487  }
488 }
489 
491 {
492  m_views.clear();
493  if ( m_xml_doc != 0 ) {
494  getTupleCuts ();
495  getDataReps ();
496 
497  XmlElement * root = m_xml_doc->documentElement ();
498  list < XmlElement * > nodelist;
499  m_view->fillNodeList ( root, nodelist );
500 
501  if ( nodelist.empty () ) return;
502 
503  // std:: for MS VC++ 6.0
504  std::list < XmlElement * >::const_iterator first = nodelist.begin ();
505  for ( ; first != nodelist.end(); ++ first ) {
506  m_view->getObjects ( *first );
507  }
508 
509  m_view -> connectPlotters ( root );
510  }
511 }
512 
513 int
515 getId ( const void * addr )
516 {
517  int id = 0;
518  AddrMapIterator_t it = m_addr_map.find ( addr );
519  if ( it == m_addr_map.end () ) { // not found
520  id = m_id;
521  m_addr_map [ addr ] = m_id++;
522  }
523  else { // found
524  id = it -> second;
525  }
526 
527  return id;
528 }
529 
530 std::list < std::string > &
533 {
534  return m_missing_tuples;
535 }
XmlDocument * m_pasteboard
The pasteboard object.
Definition: XmlController.h:99
const std::list< ViewBase * > & getFromPasteboard(ViewFactory *factory, const std::vector< const ViewBase * > &views)
Creates and returns a list of objects that was copied to the pasteboard.
A singleton class that handles the application logic for cuts.
Definition: CutController.h:38
The base class for the PlotterBase hierarchy.
Definition: PlotterBase.h:55
virtual Status openNTuples()
Attempts to open ntuple files in the current document and put them into the map.
void getDataSources(std::vector< DataSource * > &, bool all=true) const
Clears and fills the vector with the registered DataSource objects.
DataRepXML * m_datarep_xml
The singleton instance of the DataRepXML object.
const std::list< ViewBase * > & getFromDocument(ViewFactory *facotry, const std::vector< const ViewBase * > &views)
Restores the views in the current document.
Status saveToFile(const std::vector< const ViewBase * > &views, const std::string &filename)
Saves the document represented by list of ViewBase objects to file with name filename.
std::map< int, PlotterBase * > m_plotter_map
The mapping from PlotterBase Id to created PlotterBase.
virtual void getObjects(const XmlElement *element)
Creates an object derived from ViewBase.
Definition: ViewBaseXML.cxx:82
std::list< std::string > m_missing_tuples
A list of missing NTuples.
Definition: XmlController.h:71
A class that is does XML serialization and de-serialization of derived classes of TupleCut...
Definition: TupleCutXML.h:32
hippodraw::ViewBase class interface
const DataSource * getObject(const XmlElement &tag) const
Returns the DataSource corresponding to XmlElement.
virtual ViewBase * createView(PlotterBase *plotter) const =0
Creates a view for the plotter.
void fillTupleCutList(const std::vector< const ViewBase * > &views, std::vector< const TupleCut * > &cut_list)
Fills the cut_map with the TupleCut objects contained the list of views, if any.
bool isPasteboardEmpty()
Returns true if global pasteboard is empty.
Status openFile(const std::string &filename)
Attempts to open and parse the specified HippoDraw document.
A abstract base class for font handling.
Definition: FontBase.h:32
virtual Status openDocument(const std::string &filename)
Opens an existing DOM Document file filename.
XmlElement class interface.
virtual ~XmlController()
The destructor.
const DataSource * getDataSource(int ref_id)
Returns pointer to data source from the xml reference ID.
Status
Status codes for opening an XML document.
Definition: XmlDocument.h:37
virtual int getID() const =0
Returns the ID of the element, if it has one.
std::map< const void *, int > m_addr_map
The map of memory address to unique integer identifier.
void fillNTupleMap()
Fills the NTuple map with NTuple objects currently in memory.
TupleCutXML class interface.
hippodraw::CutController class interface
Status
Return codes of opening file.
Definition: XmlController.h:59
HiNTupleXML * m_ntuple
The singleton instance of the XML handler for data source.
A singleton class that is does XML serialization and deserialization of derived classes of HiNTuple...
Definition: HiNTupleXML.h:29
static CutController * instance()
Returns the pointer to the singleton instance.
void fillTupleCutMap(const std::vector< const ViewBase * > &views)
Fills the TupleCut map with TupleCut objecs contained in one in the list of views.
virtual XmlElement * documentElement() const =0
Returns the root document element.
virtual Status setContent(const std::string &filename)=0
Sets the content of the XML document from the text in the file filename.
const std::list< ViewBase * > & getFromSelectionCopy(ViewFactory *factory, const std::vector< const ViewBase * > &views)
Creates and returns objects that were copied to the selection copy.
void clearDataSourceMap()
Deletes each element in the map and clears the map.
XmlController()
A default constructor for avoiding creation except by itself or with derived classes.
XmlController class interface.
const TupleCut * getTupleCut(int ref_id)
Returns pointer to the TupleCut from the xml reference Id.
hippodraw::DataRep * createObject(const XmlElement *element)
Returns a newly created object based on the XmlElement.
Definition: DataRepXML.cxx:245
static XmlDocument * m_xml_doc
The current document being generated or read.
void saveToDocument(const std::vector< const ViewBase * > &selviews)
Save the ViewBase objects in selview to a XML in memory document.
bool areDataSourcesSaved(const ViewBase &view)
Returns true if all the NTuple objects used by the view have been save to or read from a file...
Definition: ViewBaseXML.cxx:47
std::list< ViewBase * > m_views
A list of reconstructed views.
The abstract base class for a factory that creates objects closely related to the graphics subsystem ...
Definition: ViewFactory.h:35
HiNTupleXML class interface.
XmlElement * createElement(const DataSource &ntuple)
Returns a newly created XmlElement for DataSource.
Definition: HiNTupleXML.cxx:52
ViewBaseXML * m_view
The singleton instance of a ViewBaseXML object.
void registerPlotter(int id, PlotterBase *plotter)
Registers the plotter with id.
virtual XmlElement * addDataSource(const DataSource *ntuple)
Adds the DataSource to the document if it doesn't already exist.
std::map< int, const TupleCut * > m_tuple_cut_map
A mapping from XML id to created TupleCut.
DataRep * getDataRep(int ref)
Returns a pointer to the DataRep from the xml reference Id.
A pure virtual base class of XML element wrapper.
Definition: XmlElement.h:30
virtual void appendChild(XmlElement &)=0
Appends the root element to the document.
virtual Status openUpdatedNTuples()
Open the missing tuples updated by GUI.
void connectDataRep(DataRep *rep, const std::vector< const ViewBase * > &views)
Connects the DataRep object to its appropriate Cut Plotter.
virtual void createDocument(const std::string &doc_name)
Creates an DOM Document with name doc_name.
hippodraw::DataSource class interface.
Namespace for HippoDraw.
Definition: AxesType.cxx:21
hippodraw::DataRepXML class interface
virtual void newDocument(const std::string &name)=0
Creates a new document.
TupleCutXML * m_tuple_cut_xml
The singleton instance of the XML handler for TupleCut objects.
ViewBaseXML class interface.
std::map< int, DataRep * > m_datarep_map
A mapping from DataRep Id to created DataRep.
virtual Status saveToFile(const std::string &filename)=0
Saves the document to the file.
void getDataReps()
Creates all the DataRep objects and fills the reference map.
Base class for DataSource.
Definition: DataSource.h:55
ViewBase * createView(PlotterBase *)
Creates a new view for the current PlotterBase object and adds it to the list of views.
PlotterBase * getPlotter(int ref)
Returns a pointer to the PlotterBase object from the XML refereed Id.
std::string getName(const XmlElement &tag) const
Returns the name of the HiNTuple object.
Definition: HiNTupleXML.cxx:68
std::map< int, const DataSource * > m_tuple_map
A mapping from XML id to created data sources.
static DataSourceController * instance()
Returns the pointer to the singleton instance.
XmlDocument * m_selectboard
A copy of a selection.
std::vector< int > m_missing_ids
A vector of the ids in the Xml file of the missing NTuples.
Definition: XmlController.h:75
An abstract base class of XML document element wrapper.
Definition: XmlDocument.h:32
bool areDataSourcesSaved(const std::vector< const ViewBase * > &views)
Returns true if all the NTuple objects used by the views have been saved to or read from a file...
void getTupleCuts()
Creates all the TupleCut objects and fills the reference map.
virtual XmlElement * createElement(const std::string &tagName)=0
Creates a new DOM element wrapper object and returns a pointer to it.
XmlDocument class interface.
void addViews(const std::vector< const ViewBase * > &views)
Adds ViewBase elements to the root of the document.
A singleton class that is the interface between GUI and the DataSource objects.
A class XML creation and parsing of XmlElement for ViewBase class.
Definition: ViewBaseXML.h:29
XmlElement * createElement(const ViewBase &view)
Returns a newly created XmlElement with attributes set for view.
Definition: ViewBaseXML.cxx:63
The base class for data representations.
Definition: DataRep.h:68
A class that is does XML serialization and de-serialization of derived classes of DataRep...
Definition: DataRepXML.h:46
FontBase * createFont() const
Creates a empty QtFont object.
void saveToPasteboard(const std::vector< const ViewBase * > &)
Saves the document represented by the list of ViewBase objects to the pasteboard. ...
std::list< std::string > & getMissingTuples()
Return a list of missing tuples.
std::map< const void *, int >::iterator AddrMapIterator_t
The iterator for memory address to unique integer identifier.
std::map< const DataSource *, XmlElement * > m_data
The map of data sources to XmlElements for the current document.
virtual bool serializeViews(const std::vector< const ViewBase * > &views)
Serializes the ViewBase objects in views and their appropriate subcomponents.
STL namespace.
virtual void getViews()
De-serializes the ViewBase objects.
virtual void appendChild(const XmlNode &child)=0
Appends a child element to the element.
void saveToSelectionCopy(const std::vector< const ViewBase * > &views)
Saves the document represented by the list of ViewBase objects to the selection copy.
void fillNodeList(const XmlElement *element, std::list< XmlElement * > &nodelist)
Fills the nodelist with immediate child nodes of element with nodes that can be handled by this objec...
Definition: BaseXML.cxx:58
int m_id
The next integer identifier to be generated.
ViewFactory * m_view_factory
The factory to be used for creating views.
Definition: XmlController.h:64
The class expresses a cut on a DataSource, i.e.
Definition: TupleCut.h:43
TupleCut * getObject(const XmlElement &tag) const
Creates an TupleCut object and sets its properties from the XmlElement.
Definition: TupleCutXML.cxx:71
DataSourceController class interface.
int getId(const void *address)
Returns the unique integer identifier for given address.
The abstract base class for views.
Definition: ViewBase.h:62
ViewFactory class interface.

Generated for HippoDraw Class Library by doxygen