source: XMLIO_V2/dev/common/src/xmlio/node/context.cpp @ 278

Last change on this file since 278 was 278, checked in by hozdoba, 13 years ago

Corrections

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