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 | namespace xmlioserver { |
---|
10 | namespace tree { |
---|
11 | |
---|
12 | /// ////////////////////// Définitions ////////////////////// /// |
---|
13 | |
---|
14 | CContext::CContext(void) |
---|
15 | : CObjectTemplate<CContext>(), CContextAttributes() |
---|
16 | { /* Ne rien faire de plus */ } |
---|
17 | |
---|
18 | CContext::CContext(const StdString & id) |
---|
19 | : CObjectTemplate<CContext>(id), CContextAttributes() |
---|
20 | { /* Ne rien faire de plus */ } |
---|
21 | |
---|
22 | CContext::~CContext(void) |
---|
23 | { /* Ne rien faire de plus */ } |
---|
24 | |
---|
25 | //---------------------------------------------------------------- |
---|
26 | |
---|
27 | StdString CContext::GetName(void) { return (StdString("context")); } |
---|
28 | StdString CContext::GetDefName(void){ return (CContext::GetName()); } |
---|
29 | ENodeType CContext::GetType(void) { return (eContext); } |
---|
30 | |
---|
31 | //---------------------------------------------------------------- |
---|
32 | |
---|
33 | boost::shared_ptr<CContextGroup> CContext::GetContextGroup(void) |
---|
34 | { |
---|
35 | static boost::shared_ptr<CContextGroup> group_context |
---|
36 | (new CContextGroup(xml::CXMLNode::GetRootName())); |
---|
37 | return (group_context); |
---|
38 | } |
---|
39 | |
---|
40 | //---------------------------------------------------------------- |
---|
41 | |
---|
42 | void CContext::parse(xml::CXMLNode & node) |
---|
43 | { |
---|
44 | CContext::SuperClass::parse(node); |
---|
45 | |
---|
46 | // PARSING POUR GESTION DES ENFANTS |
---|
47 | xml::THashAttributes attributes; |
---|
48 | |
---|
49 | if (node.getElementName().compare(CContext::GetName())) |
---|
50 | DEBUG("Le noeud est mal nommé mais sera traité comme un contexte !"); |
---|
51 | |
---|
52 | if (!(node.goToChildElement())) |
---|
53 | { |
---|
54 | DEBUG("Le context ne contient pas d'enfant !"); |
---|
55 | } |
---|
56 | else |
---|
57 | { |
---|
58 | do { // Parcours des contextes pour traitement. |
---|
59 | |
---|
60 | StdString name = node.getElementName(); |
---|
61 | attributes.clear(); |
---|
62 | attributes = node.getAttributes(); |
---|
63 | |
---|
64 | if (attributes.end() != attributes.find("id")) |
---|
65 | { DEBUG(<< "Le noeud de définition possÚde un identifiant," |
---|
66 | << " ce dernier ne sera pas pris en compte lors du traitement !"); } |
---|
67 | |
---|
68 | #define DECLARE_NODE(Name_, name_) \ |
---|
69 | if (name.compare(C##Name_##Definition::GetDefName()) == 0) \ |
---|
70 | { CObjectFactory::CreateObject<C##Name_##Definition>(C##Name_##Definition::GetDefName()) -> parse(node); \ |
---|
71 | continue; } |
---|
72 | #define DECLARE_NODE_PAR(Name_, name_) |
---|
73 | #include "node_type.conf" |
---|
74 | |
---|
75 | DEBUG(<< "L'élément nommé \'" << name |
---|
76 | << "\' dans le contexte \'" << CObjectFactory::GetCurrentContextId() |
---|
77 | << "\' ne représente pas une définition !"); |
---|
78 | |
---|
79 | } while (node.goToNextElement()); |
---|
80 | |
---|
81 | node.goToParentElement(); // Retour au parent |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | //---------------------------------------------------------------- |
---|
86 | |
---|
87 | void CContext::ShowTree(StdOStream & out) |
---|
88 | { |
---|
89 | StdString currentContextId = |
---|
90 | CObjectFactory::GetCurrentContextId(); |
---|
91 | std::vector<boost::shared_ptr<CContext> > def_vector = |
---|
92 | CContext::GetContextGroup()->getChildList(); |
---|
93 | std::vector<boost::shared_ptr<CContext> >::iterator |
---|
94 | it = def_vector.begin(), end = def_vector.end(); |
---|
95 | |
---|
96 | out << "<? xml version=\"1.0\" ?>" << std::endl; |
---|
97 | out << "<" << xml::CXMLNode::GetRootName() << " >" << std::endl; |
---|
98 | |
---|
99 | for (; it != end; it++) |
---|
100 | { |
---|
101 | boost::shared_ptr<CContext> context = *it; |
---|
102 | CTreeManager::SetCurrentContextId(context->getId()); |
---|
103 | out << *context << std::endl; |
---|
104 | } |
---|
105 | |
---|
106 | out << "</" << xml::CXMLNode::GetRootName() << " >" << std::endl; |
---|
107 | CTreeManager::SetCurrentContextId(currentContextId); |
---|
108 | } |
---|
109 | |
---|
110 | //---------------------------------------------------------------- |
---|
111 | |
---|
112 | void CContext::toBinary(StdOStream & os) const |
---|
113 | { |
---|
114 | SuperClass::toBinary(os); |
---|
115 | |
---|
116 | #define DECLARE_NODE(Name_, name_) \ |
---|
117 | { \ |
---|
118 | ENodeType renum = C##Name_##Definition::GetType(); \ |
---|
119 | bool val = CObjectFactory::HasObject<C##Name_##Definition> \ |
---|
120 | (C##Name_##Definition::GetDefName()); \ |
---|
121 | os.write (reinterpret_cast<const char*>(&renum), sizeof(ENodeType)); \ |
---|
122 | os.write (reinterpret_cast<const char*>(&val), sizeof(bool)); \ |
---|
123 | if (val) CObjectFactory::GetObject<C##Name_##Definition> \ |
---|
124 | (C##Name_##Definition::GetDefName())->toBinary(os); \ |
---|
125 | } |
---|
126 | #define DECLARE_NODE_PAR(Name_, name_) |
---|
127 | #include "node_type.conf" |
---|
128 | } |
---|
129 | |
---|
130 | //---------------------------------------------------------------- |
---|
131 | |
---|
132 | void CContext::fromBinary(StdIStream & is) |
---|
133 | { |
---|
134 | SuperClass::fromBinary(is); |
---|
135 | #define DECLARE_NODE(Name_, name_) \ |
---|
136 | { \ |
---|
137 | bool val = false; \ |
---|
138 | ENodeType renum = Unknown; \ |
---|
139 | is.read (reinterpret_cast<char*>(&renum), sizeof(ENodeType)); \ |
---|
140 | is.read (reinterpret_cast<char*>(&val), sizeof(bool)); \ |
---|
141 | if (renum != C##Name_##Definition::GetType()) \ |
---|
142 | ERROR("CContext::fromBinary(StdIStream & is)", \ |
---|
143 | << "[ renum = " << renum << "] Bad type !"); \ |
---|
144 | if (val) CObjectFactory::CreateObject<C##Name_##Definition> \ |
---|
145 | (C##Name_##Definition::GetDefName()) -> fromBinary(is); \ |
---|
146 | } |
---|
147 | #define DECLARE_NODE_PAR(Name_, name_) |
---|
148 | #include "node_type.conf" |
---|
149 | |
---|
150 | } |
---|
151 | |
---|
152 | |
---|
153 | //---------------------------------------------------------------- |
---|
154 | |
---|
155 | StdString CContext::toString(void) const |
---|
156 | { |
---|
157 | StdOStringStream oss; |
---|
158 | oss << "<" << CContext::GetName() |
---|
159 | << " id=\"" << this->getId() << "\" " |
---|
160 | << SuperClassAttribute::toString() << ">" << std::endl; |
---|
161 | if (!this->hasChild()) |
---|
162 | { |
---|
163 | //oss << "<!-- No definition -->" << std::endl; // fait planter l'incrémentation |
---|
164 | } |
---|
165 | else |
---|
166 | { |
---|
167 | |
---|
168 | #define DECLARE_NODE(Name_, name_) \ |
---|
169 | if (CObjectFactory::HasObject<C##Name_##Definition>(C##Name_##Definition::GetDefName())) \ |
---|
170 | oss << *CObjectFactory::GetObject<C##Name_##Definition>(C##Name_##Definition::GetDefName()) << std::endl; |
---|
171 | #define DECLARE_NODE_PAR(Name_, name_) |
---|
172 | #include "node_type.conf" |
---|
173 | |
---|
174 | } |
---|
175 | |
---|
176 | oss << "</" << CContext::GetName() << " >"; |
---|
177 | |
---|
178 | return (oss.str()); |
---|
179 | } |
---|
180 | |
---|
181 | //---------------------------------------------------------------- |
---|
182 | |
---|
183 | void CContext::solveDescInheritance(const CAttributeMap * const UNUSED(parent)) |
---|
184 | { |
---|
185 | #define DECLARE_NODE(Name_, name_) \ |
---|
186 | if (CObjectFactory::HasObject<C##Name_##Definition>(C##Name_##Definition::GetDefName())) \ |
---|
187 | CObjectFactory::GetObject<C##Name_##Definition>(C##Name_##Definition::GetDefName())->solveDescInheritance(); |
---|
188 | #define DECLARE_NODE_PAR(Name_, name_) |
---|
189 | #include "node_type.conf" |
---|
190 | } |
---|
191 | |
---|
192 | //---------------------------------------------------------------- |
---|
193 | |
---|
194 | bool CContext::hasChild(void) const |
---|
195 | { |
---|
196 | return ( |
---|
197 | #define DECLARE_NODE(Name_, name_) \ |
---|
198 | CObjectFactory::HasObject<C##Name_##Definition> (C##Name_##Definition::GetDefName()) || |
---|
199 | #define DECLARE_NODE_PAR(Name_, name_) |
---|
200 | #include "node_type.conf" |
---|
201 | false); |
---|
202 | } |
---|
203 | |
---|
204 | //---------------------------------------------------------------- |
---|
205 | |
---|
206 | void CContext::solveFieldRefInheritance(void) |
---|
207 | { |
---|
208 | if (!this->hasId()) return; |
---|
209 | std::vector<boost::shared_ptr<CField> > allField |
---|
210 | = CObjectTemplate<CField>::GetAllVectobject(this->getId()); |
---|
211 | std::vector<boost::shared_ptr<CField> >::iterator |
---|
212 | it = allField.begin(), end = allField.end(); |
---|
213 | |
---|
214 | for (; it != end; it++) |
---|
215 | { |
---|
216 | boost::shared_ptr<CField> field = *it; |
---|
217 | field->solveRefInheritance(); |
---|
218 | } |
---|
219 | } |
---|
220 | |
---|
221 | ///--------------------------------------------------------------- |
---|
222 | |
---|
223 | } // namespace tree |
---|
224 | } // namespace xmlioserver |
---|