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 | |
---|
11 | namespace xmlioserver { |
---|
12 | namespace tree { |
---|
13 | |
---|
14 | /// ////////////////////// Définitions ////////////////////// /// |
---|
15 | |
---|
16 | CContext::CContext(void) |
---|
17 | : CObjectTemplate<CContext>(), CContextAttributes() |
---|
18 | , calendar() |
---|
19 | { /* Ne rien faire de plus */ } |
---|
20 | |
---|
21 | CContext::CContext(const StdString & id) |
---|
22 | : CObjectTemplate<CContext>(id), CContextAttributes() |
---|
23 | , calendar() |
---|
24 | { /* Ne rien faire de plus */ } |
---|
25 | |
---|
26 | CContext::~CContext(void) |
---|
27 | { /* Ne rien faire de plus */ } |
---|
28 | |
---|
29 | //---------------------------------------------------------------- |
---|
30 | |
---|
31 | StdString CContext::GetName(void) { return (StdString("context")); } |
---|
32 | StdString CContext::GetDefName(void){ return (CContext::GetName()); } |
---|
33 | ENodeType CContext::GetType(void) { return (eContext); } |
---|
34 | |
---|
35 | //---------------------------------------------------------------- |
---|
36 | |
---|
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 | } |
---|
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 | //---------------------------------------------------------------- |
---|
61 | |
---|
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 | |
---|
84 | //---------------------------------------------------------------- |
---|
85 | |
---|
86 | void CContext::parse(xml::CXMLNode & node) |
---|
87 | { |
---|
88 | CContext::SuperClass::parse(node); |
---|
89 | |
---|
90 | // PARSING POUR GESTION DES ENFANTS |
---|
91 | xml::THashAttributes attributes; |
---|
92 | |
---|
93 | if (node.getElementName().compare(CContext::GetName())) |
---|
94 | DEBUG("Le noeud est mal nommé mais sera traité comme un contexte !"); |
---|
95 | |
---|
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. |
---|
103 | |
---|
104 | StdString name = node.getElementName(); |
---|
105 | attributes.clear(); |
---|
106 | attributes = node.getAttributes(); |
---|
107 | |
---|
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 !"); } |
---|
111 | |
---|
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); \ |
---|
115 | continue; } |
---|
116 | #define DECLARE_NODE_PAR(Name_, name_) |
---|
117 | #include "node_type.conf" |
---|
118 | |
---|
119 | DEBUG(<< "L'élément nommé \'" << name |
---|
120 | << "\' dans le contexte \'" << CObjectFactory::GetCurrentContextId() |
---|
121 | << "\' ne représente pas une définition !"); |
---|
122 | |
---|
123 | } while (node.goToNextElement()); |
---|
124 | |
---|
125 | node.goToParentElement(); // Retour au parent |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | //---------------------------------------------------------------- |
---|
130 | |
---|
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(); |
---|
139 | |
---|
140 | out << "<? xml version=\"1.0\" ?>" << std::endl; |
---|
141 | out << "<" << xml::CXMLNode::GetRootName() << " >" << std::endl; |
---|
142 | |
---|
143 | for (; it != end; it++) |
---|
144 | { |
---|
145 | boost::shared_ptr<CContext> context = *it; |
---|
146 | CTreeManager::SetCurrentContextId(context->getId()); |
---|
147 | out << *context << std::endl; |
---|
148 | } |
---|
149 | |
---|
150 | out << "</" << xml::CXMLNode::GetRootName() << " >" << std::endl; |
---|
151 | CTreeManager::SetCurrentContextId(currentContextId); |
---|
152 | } |
---|
153 | |
---|
154 | //---------------------------------------------------------------- |
---|
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 | //---------------------------------------------------------------- |
---|
198 | |
---|
199 | StdString CContext::toString(void) const |
---|
200 | { |
---|
201 | StdOStringStream oss; |
---|
202 | oss << "<" << CContext::GetName() |
---|
203 | << " id=\"" << this->getId() << "\" " |
---|
204 | << SuperClassAttribute::toString() << ">" << std::endl; |
---|
205 | if (!this->hasChild()) |
---|
206 | { |
---|
207 | //oss << "<!-- No definition -->" << std::endl; // fait planter l'incrémentation |
---|
208 | } |
---|
209 | else |
---|
210 | { |
---|
211 | |
---|
212 | #define DECLARE_NODE(Name_, name_) \ |
---|
213 | if (CObjectFactory::HasObject<C##Name_##Definition>(C##Name_##Definition::GetDefName())) \ |
---|
214 | oss << *CObjectFactory::GetObject<C##Name_##Definition>(C##Name_##Definition::GetDefName()) << std::endl; |
---|
215 | #define DECLARE_NODE_PAR(Name_, name_) |
---|
216 | #include "node_type.conf" |
---|
217 | |
---|
218 | } |
---|
219 | |
---|
220 | oss << "</" << CContext::GetName() << " >"; |
---|
221 | |
---|
222 | return (oss.str()); |
---|
223 | } |
---|
224 | |
---|
225 | //---------------------------------------------------------------- |
---|
226 | |
---|
227 | void CContext::solveDescInheritance(const CAttributeMap * const UNUSED(parent)) |
---|
228 | { |
---|
229 | #define DECLARE_NODE(Name_, name_) \ |
---|
230 | if (CObjectFactory::HasObject<C##Name_##Definition>(C##Name_##Definition::GetDefName())) \ |
---|
231 | CObjectFactory::GetObject<C##Name_##Definition>(C##Name_##Definition::GetDefName())->solveDescInheritance(); |
---|
232 | #define DECLARE_NODE_PAR(Name_, name_) |
---|
233 | #include "node_type.conf" |
---|
234 | } |
---|
235 | |
---|
236 | //---------------------------------------------------------------- |
---|
237 | |
---|
238 | bool CContext::hasChild(void) const |
---|
239 | { |
---|
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" |
---|
245 | false); |
---|
246 | } |
---|
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++) |
---|
259 | { |
---|
260 | boost::shared_ptr<CField> field = *it; |
---|
261 | field->solveRefInheritance(); |
---|
262 | } |
---|
263 | } |
---|
264 | |
---|
265 | ///--------------------------------------------------------------- |
---|
266 | |
---|
267 | } // namespace tree |
---|
268 | } // namespace xmlioserver |
---|