source: XMLIO_V2/dev/dev_rv/src/xmlio/node/context.cpp @ 196

Last change on this file since 196 was 188, checked in by hozdoba, 13 years ago
File size: 9.5 KB
RevLine 
[152]1#include "context.hpp"
2
[173]3#include "tree_manager.hpp"
4
[152]5#include "attribute_template_impl.hpp"
6#include "object_template_impl.hpp"
7#include "group_template_impl.hpp"
8
[188]9#include "calendar_type.hpp"
10
[168]11namespace xmlioserver {
12namespace tree {
13   
14   /// ////////////////////// Définitions ////////////////////// ///
[152]15
[168]16   CContext::CContext(void)
17      : CObjectTemplate<CContext>(), CContextAttributes()
[188]18      , calendar()
[168]19   { /* Ne rien faire de plus */ }
[152]20
[168]21   CContext::CContext(const StdString & id)
22      : CObjectTemplate<CContext>(id), CContextAttributes()
[188]23      , calendar()
[168]24   { /* Ne rien faire de plus */ }
[152]25
[168]26   CContext::~CContext(void)
27   { /* Ne rien faire de plus */ }
[152]28
[168]29   //----------------------------------------------------------------
[152]30
[168]31   StdString CContext::GetName(void)   { return (StdString("context")); }
32   StdString CContext::GetDefName(void){ return (CContext::GetName()); }
33   ENodeType CContext::GetType(void)   { return (eContext); }
[152]34
[168]35   //----------------------------------------------------------------
[152]36
[168]37   boost::shared_ptr<CContextGroup> CContext::GetContextGroup(void)
38   { 
39      static boost::shared_ptr<CContextGroup> group_context
40                          (new CContextGroup(xml::CXMLNode::GetRootName()));
41      return (group_context); 
42   }
[188]43   
44   //----------------------------------------------------------------
45   
46   boost::shared_ptr<date::CCalendar> CContext::getCalendar(void) const
47   {
48      return (this->calendar);
49   }
50   
51   //----------------------------------------------------------------
52   
53   void CContext::setCalendar(boost::shared_ptr<date::CCalendar> newCalendar)
54   {
55      this->calendar = newCalendar;
56      calendar_type.setValue(this->calendar->getId());
57      start_date.setValue(this->calendar->getInitDate().toString());
58   }
59   
60   //----------------------------------------------------------------
[152]61
[188]62   void CContext::solveCalendar(void)
63   {
64      if (this->calendar.get() != NULL) return;
65      if (calendar_type.isEmpty() || start_date.isEmpty())
66         ERROR(" CContext::solveCalendar(void)",
67               << "[ context id = " << this->getId() << " ] "
68               << "Impossible de définir un calendrier (un attribut est manquant).");
69
70#define DECLARE_CALENDAR(MType  , mtype)                        \
71   if (calendar_type.getValue().compare(#mtype) == 0)           \
72   {                                                            \
73      this->calendar =  boost::shared_ptr<date::CCalendar>      \
74         (new date::C##MType##Calendar(start_date.getValue())); \
75      return;                                                   \
76   }
77#include "calendar_type.conf"
78
79      ERROR("CContext::solveCalendar(void)",
80            << "[ calendar_type = " << calendar_type.getValue() << " ] "
81            << "Le calendrier n'est pas définie dans le code !");
82   }
83   
[168]84   //----------------------------------------------------------------
[152]85
[168]86   void CContext::parse(xml::CXMLNode & node)
87   {
88      CContext::SuperClass::parse(node);
[152]89
[168]90      // PARSING POUR GESTION DES ENFANTS
91      xml::THashAttributes attributes;
[152]92
[168]93      if (node.getElementName().compare(CContext::GetName()))
94         DEBUG("Le noeud est mal nommé mais sera traité comme un contexte !");
[152]95
[168]96      if (!(node.goToChildElement()))
97      {
98         DEBUG("Le context ne contient pas d'enfant !");
99      }
100      else
101      {
102         do { // Parcours des contextes pour traitement.
[152]103
[168]104            StdString name = node.getElementName();
105            attributes.clear();
106            attributes = node.getAttributes();
[152]107
[168]108            if (attributes.end() != attributes.find("id"))
109            { DEBUG(<< "Le noeud de définition possÚde un identifiant,"
110                    << " ce dernier ne sera pas pris en compte lors du traitement !"); }
[152]111
[173]112#define DECLARE_NODE(Name_, name_)    \
113   if (name.compare(C##Name_##Definition::GetDefName()) == 0) \
114   { CObjectFactory::CreateObject<C##Name_##Definition>(C##Name_##Definition::GetDefName()) -> parse(node); \
[168]115   continue; }
[173]116#define DECLARE_NODE_PAR(Name_, name_)
117#include "node_type.conf"
[152]118
[168]119            DEBUG(<< "L'élément nommé \'"     << name
120                  << "\' dans le contexte \'" << CObjectFactory::GetCurrentContextId()
121                  << "\' ne représente pas une définition !");
[152]122
[168]123         } while (node.goToNextElement());
[152]124
[168]125         node.goToParentElement(); // Retour au parent
[152]126      }
[168]127   }
[152]128
[168]129   //----------------------------------------------------------------
[152]130
[168]131   void CContext::ShowTree(StdOStream & out)
132   {
133      StdString currentContextId =
134         CObjectFactory::GetCurrentContextId();
135      std::vector<boost::shared_ptr<CContext> > def_vector =
136         CContext::GetContextGroup()->getChildList();
137      std::vector<boost::shared_ptr<CContext> >::iterator
138         it = def_vector.begin(), end = def_vector.end();
[152]139
[168]140      out << "<? xml version=\"1.0\" ?>" << std::endl;
[173]141      out << "<"  << xml::CXMLNode::GetRootName() << " >" << std::endl;
[168]142     
143      for (; it != end; it++)
144      {
[173]145         boost::shared_ptr<CContext> context = *it;         
146         CTreeManager::SetCurrentContextId(context->getId());         
[168]147         out << *context << std::endl;
148      }
149     
[173]150      out << "</" << xml::CXMLNode::GetRootName() << " >" << std::endl;
151      CTreeManager::SetCurrentContextId(currentContextId); 
[168]152   }
[173]153   
[168]154   //----------------------------------------------------------------
[173]155   
156   void CContext::toBinary(StdOStream & os) const
157   {
158      SuperClass::toBinary(os);
159       
160#define DECLARE_NODE(Name_, name_)                                         \
161   {                                                                       \
162      ENodeType renum = C##Name_##Definition::GetType();                   \
163      bool val = CObjectFactory::HasObject<C##Name_##Definition>           \
164                     (C##Name_##Definition::GetDefName());                 \
165      os.write (reinterpret_cast<const char*>(&renum), sizeof(ENodeType)); \
166      os.write (reinterpret_cast<const char*>(&val), sizeof(bool));        \
167      if (val) CObjectFactory::GetObject<C##Name_##Definition>             \
168                     (C##Name_##Definition::GetDefName())->toBinary(os);   \
169   }   
170#define DECLARE_NODE_PAR(Name_, name_)
171#include "node_type.conf"
172   }
173   
174   //----------------------------------------------------------------
175   
176   void CContext::fromBinary(StdIStream & is)
177   {
178      SuperClass::fromBinary(is);
179#define DECLARE_NODE(Name_, name_)                                         \
180   {                                                                       \
181      bool val = false;                                                    \
182      ENodeType renum = Unknown;                                           \
183      is.read (reinterpret_cast<char*>(&renum), sizeof(ENodeType));        \
184      is.read (reinterpret_cast<char*>(&val), sizeof(bool));               \
185      if (renum != C##Name_##Definition::GetType())                        \
186         ERROR("CContext::fromBinary(StdIStream & is)",                    \
187               << "[ renum = " << renum << "] Bad type !");                \
188      if (val) CObjectFactory::CreateObject<C##Name_##Definition>          \
189                  (C##Name_##Definition::GetDefName()) -> fromBinary(is);  \
190   }   
191#define DECLARE_NODE_PAR(Name_, name_)
192#include "node_type.conf"
193     
194   }
195   
196   
197   //----------------------------------------------------------------
[152]198
[168]199   StdString CContext::toString(void) const
200   {
201      StdOStringStream oss;
[173]202      oss << "<" << CContext::GetName()
[168]203          << " id=\"" << this->getId() << "\" "
204          << SuperClassAttribute::toString() << ">" << std::endl;
205      if (!this->hasChild())
[152]206      {
[168]207         //oss << "<!-- No definition -->" << std::endl; // fait planter l'incrémentation
[152]208      }
[168]209      else
210      {
[152]211
[173]212#define DECLARE_NODE(Name_, name_)    \
213   if (CObjectFactory::HasObject<C##Name_##Definition>(C##Name_##Definition::GetDefName())) \
[168]214   oss << *CObjectFactory::GetObject<C##Name_##Definition>(C##Name_##Definition::GetDefName()) << std::endl;
[173]215#define DECLARE_NODE_PAR(Name_, name_)
216#include "node_type.conf"
[168]217
[152]218      }
219
[173]220      oss << "</" << CContext::GetName() << " >";
[168]221
222      return (oss.str());
223   }
224
225   //----------------------------------------------------------------
226
227   void CContext::solveDescInheritance(const CAttributeMap * const UNUSED(parent))
228   {
[173]229#define DECLARE_NODE(Name_, name_)    \
230   if (CObjectFactory::HasObject<C##Name_##Definition>(C##Name_##Definition::GetDefName())) \
[168]231   CObjectFactory::GetObject<C##Name_##Definition>(C##Name_##Definition::GetDefName())->solveDescInheritance();
[173]232#define DECLARE_NODE_PAR(Name_, name_)
233#include "node_type.conf"
[168]234   }
235
236   //----------------------------------------------------------------
237
238   bool CContext::hasChild(void) const
239   {
[173]240      return (
241#define DECLARE_NODE(Name_, name_)    \
242   CObjectFactory::HasObject<C##Name_##Definition>  (C##Name_##Definition::GetDefName())   ||
243#define DECLARE_NODE_PAR(Name_, name_)
244#include "node_type.conf"
[168]245      false);
[173]246}
[168]247
248   //----------------------------------------------------------------
249
250   void CContext::solveFieldRefInheritance(void)
251   {
252      if (!this->hasId()) return;
253      std::vector<boost::shared_ptr<CField> > allField
254               = CObjectTemplate<CField>::GetAllVectobject(this->getId());
255      std::vector<boost::shared_ptr<CField> >::iterator
256         it = allField.begin(), end = allField.end();
257           
258      for (; it != end; it++)
[152]259      {
[168]260         boost::shared_ptr<CField> field = *it;
261         field->solveRefInheritance();
[152]262      }
[168]263   }
[152]264
[168]265   ///---------------------------------------------------------------
266
267} // namespace tree
[152]268} // namespace xmlioserver
Note: See TracBrowser for help on using the repository browser.