1 // Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "DF_definitions.hxx"
21 #include "DF_Label.hxx"
22 #include "DF_Document.hxx"
23 #include "DF_Attribute.hxx"
24 #include "DF_ChildIterator.hxx"
28 //Class DF_Label defines a persistence reference in DF_Document that contains a tree of Labels.
29 //This reference is named "entry" and is a sequence of tags divided by ":". The root entry is "0:".
30 //For example "0:1:1" corresponds the following structure
37 DF_Label DF_Label::Label(const DF_Label& theLabel, const std::string& theEntry, bool isCreated)
39 if(theLabel.IsNull()) return DF_Label();
41 DF_Label aLabel = theLabel.Root();
42 if(theEntry == "0:") return aLabel;
43 if(theEntry == "0:1") return theLabel.GetDocument()->Main();
45 char* cc = (char*)theEntry.c_str();
50 while ( *cc >= '0' && *cc <= '9') {
51 n = 10*n + (*cc - '0');
54 if (*cc == ':' || *cc == '\0') {
57 if(aLabel.IsNull())break;
58 aLabel = aLabel.FindChild(n, isCreated);
62 if (*cc != '\0') ++cc;
72 DF_Label::DF_Label(DF_LabelNode* theNode)
84 DF_Label::DF_Label(const DF_Label& theLabel)
86 _node = theLabel._node;
89 DF_Label& DF_Label::operator=(const DF_Label& theLabel)
91 _node = theLabel._node;
101 //Returns a smart pointer to Document which contains this Label
102 DF_Document* DF_Label::GetDocument() const
104 if(!_node) return NULL;
105 return _node->_document;
108 //Returns true if theLabel equals to this label
109 bool DF_Label::operator==(const DF_Label& theLabel)
111 if(IsNull() || theLabel.IsNull()) return false;
112 return (theLabel.Entry() == Entry());
115 //Returns true if theLabel doesn't equals to this label
116 bool DF_Label::operator!=(const DF_Label& theLabel)
118 if(IsNull() || theLabel.IsNull()) return true;
119 return (theLabel.Entry() != Entry());
123 //Returns a tag of this Label
124 int DF_Label::Tag() const
126 if(!_node) return -1;
130 //Returns true if this Label is attached to the tree in the Document.
131 bool DF_Label::IsAttached()
133 if(!_node) return false;
134 return _node->_document != 0;
137 //Searches an Attribute with given ID located on this Label.
138 //Returns true if the Attribute is found.
139 DF_Attribute* DF_Label::FindAttribute(const std::string& theID) const
141 if(!_node) return NULL;
143 std::map< std::string, DF_Attribute* >::iterator it=_node->_attributes.find(theID);
144 if(it == _node->_attributes.end()) return NULL;
149 //Returns true if there is an Attribute with given ID on this Label.
150 bool DF_Label::IsAttribute(const std::string& theID) const
152 if(!_node) return false;
154 return (_node->_attributes.find(theID) != _node->_attributes.end());
157 //Adds theAttribute to the Label where this Attribute is located.
158 //Returns true if theAttribute was added.
159 bool DF_Label::AddAttribute(DF_Attribute* theAttribute) const
161 if(!_node) return false;
163 if(_node->_attributes.find(theAttribute->ID()) != _node->_attributes.end()) return false;
164 theAttribute->_node = _node;
165 _node->_attributes[theAttribute->ID()] = theAttribute;
166 theAttribute->AfterAddition();
171 //Forgets an Attribute with given ID located on the this Label.
172 bool DF_Label::ForgetAttribute(const std::string& theID) const
174 if(!_node) return false;
176 if(_node->_attributes.find(theID) == _node->_attributes.end()) return false;
177 DF_Attribute* attr = _node->_attributes[theID];
178 attr->BeforeForget();
179 _node->_attributes.erase(theID);
185 //Forgets all Attributes located on this Label.
186 bool DF_Label::ForgetAllAttributes(bool clearChildren) const
188 if(!_node) return false;
190 std::vector<DF_Attribute*> va = GetAttributes();
191 _node->_attributes.clear();
193 for(int i = 0, len = va.size(); i<len; i++) {
194 va[i]->BeforeForget();
199 DF_ChildIterator CI(*this, true);
200 for(; CI.More(); CI.Next())
201 CI.Value().ForgetAllAttributes(true);
207 //Returns Father of this Label.
208 DF_Label DF_Label::Father() const
210 if(!_node) return DF_Label();
212 return _node->_father;
215 //Returns is this Label is not initialized
216 bool DF_Label::IsNull() const
218 return (!_node || (_node->_document == NULL));
221 //Returns is this Label is a Root label
222 bool DF_Label::IsRoot() const
224 if(IsNull() || Father().IsNull()) return true;
229 //Returns true if this Label has Attributes.
230 bool DF_Label::HasAttributes() const
232 if(!_node) return false;
234 return !(_node->_attributes.empty());
237 //Returns a list of Attributes of this Label.
238 std::vector<DF_Attribute*> DF_Label::GetAttributes() const
240 std::vector<DF_Attribute*> attributes;
241 if(!_node) return attributes;
243 typedef std::map<std::string, DF_Attribute*>::const_iterator AI;
244 std::vector<std::string> sorted;
245 for(AI p = _node->_attributes.begin(); p!=_node->_attributes.end(); p++)
246 sorted.push_back(p->first);
248 sort(sorted.begin(), sorted.end());
249 int len = sorted.size();
250 for(int i = 0; i<len; i++)
251 attributes.push_back(_node->_attributes[sorted[i]]);
256 //Returns true if this Label has a child Label.
257 bool DF_Label::HasChild() const
259 if(!_node) return false;
261 return _node->_firstChild != 0;
264 //Returns a number of child Labels.
265 int DF_Label::NbChildren() const
267 if(!_node) return -1;
269 if(!_node->_firstChild) return 0;
271 DF_LabelNode* next = _node->_firstChild->_next;
280 //Returns the depth (a number of fathers required to identify the Label) of this Label in the tree.
281 int DF_Label::Depth() const
283 if(!_node) return -1;
285 return _node->_depth;
288 //Returns true if this Label is a descendant of theLabel.
289 bool DF_Label::IsDescendant(const DF_Label& theLabel)
291 if(!_node) return false;
293 DF_LabelNode* father = _node->_father;
294 if(!father) return false;
297 if(father == theLabel._node) return true;
298 father = father->_father;
304 //Returns the root Label of a Label tree to which this Label belongs.
305 DF_Label DF_Label::Root() const
307 if(!_node) return DF_Label();
309 return _node->_document->Main().Father();
312 //Finds a child Label of this Label with a given tag. If isCreate = true and there is no child
313 //Label with the given tag, the child Label is created.
314 DF_Label DF_Label::FindChild(int theTag, bool isCreate)
316 if(!_node || IsNull()) return DF_Label();
318 DF_LabelNode *aLabel = NULL, *aPrevious = NULL, *aNext = NULL;
319 if(!_node->_firstChild && !isCreate) return DF_Label();
321 if(_node->_firstChild && _node->_firstChild->_tag == theTag)
322 return DF_Label(_node->_firstChild);
324 if(_node->_lastChild) {
325 if(_node->_lastChild->_tag == theTag) return DF_Label(_node->_lastChild);
326 if(_node->_lastChild->_tag < theTag) aPrevious = _node->_lastChild;
330 aLabel = _node->_firstChild;
332 if(aLabel->_tag == theTag) return DF_Label(aLabel);
333 if(aLabel->_tag > theTag) {
337 if(aLabel->_tag < theTag) aPrevious = aLabel;
338 aLabel = aLabel->_next;
342 if(!isCreate) return DF_Label();
344 DF_LabelNode* aChild = new DF_LabelNode();
345 aChild->_father = this->_node;
346 aChild->_document = _node->_document;
347 aChild->_tag = theTag;
348 aChild->_depth = _node->_depth+1;
350 aChild->_previous = aNext->_previous;
351 aChild->_next = aNext;
352 aNext->_previous = aChild;
355 aChild->_previous = aPrevious;
356 aChild->_next = aPrevious->_next;
357 aPrevious->_next = aChild;
360 if(!_node->_firstChild || (aNext && aNext == _node->_firstChild) ) _node->_firstChild = aChild;
361 if(!_node->_lastChild || !aNext) _node->_lastChild = aChild;
366 //Creates a new child Label of this Label.
367 DF_Label DF_Label::NewChild()
369 if(!_node || IsNull()) return DF_Label();
372 if(_node->_lastChild) tag = _node->_lastChild->_tag+1;
374 return FindChild(tag, true);
377 //Returns a string entry of this Label
378 std::string DF_Label::Entry() const
380 DF_LabelNode* father = this->_node;
381 if(!father->_father)return "0:";
391 // Conversion. Number is reversed.
392 *wstr++ = '0' + (tag % 10);
394 father = father->_father;
395 if(father)*wstr++ = ':';
402 aux = *wstr, *wstr-- = *str, *str++ = aux;
407 bool DF_Label::IsEqual(const DF_Label& theLabel)
409 if(theLabel.IsNull() || IsNull()) return false;
410 DF_Label L(theLabel);
411 return (L.Entry() == Entry());
415 void DF_Label::Nullify()
421 void DF_Label::dump()
423 if(!_node) std::cout << "DF_Label addr : " << this << " NULL " << std::endl;
425 std::cout << "DF_Label addr : " << this->_node << " entry : " << Entry() << std::endl;
426 if(_node->_father) std::cout << " Father : " << _node->_father << " entry : " << Father().Entry() << std::endl;
427 else std::cout << " Father : NULL " << std::endl;
429 if(_node->_firstChild) std::cout << " FirstChild : " << _node->_firstChild << " entry : " << DF_Label(_node->_firstChild).Entry() << std::endl;
430 else std::cout << " FirstChild : NULL " << std::endl;
432 if(_node->_lastChild) std::cout << " LastChild : " << _node->_lastChild << " entry : " << DF_Label(_node->_lastChild).Entry() << std::endl;
433 else std::cout << " LastChild : NULL " << std::endl;
435 if(_node->_previous) std::cout << " Previous : " << _node->_previous << " entry : " << DF_Label(_node->_previous).Entry() << std::endl;
436 else std::cout << " Previous : NULL " << std::endl;
438 if(_node->_next) std::cout << " Next : " << _node->_next << " entry : " << DF_Label(_node->_next).Entry() << std::endl;
439 else std::cout << " Next : NULL " << std::endl;
445 ###############################################
447 ###############################################
450 DF_LabelNode::DF_LabelNode()
463 DF_LabelNode::~DF_LabelNode()
465 std::vector<DF_Attribute*> va;
466 typedef std::map<std::string, DF_Attribute*>::const_iterator AI;
467 for(AI p = _attributes.begin(); p!=_attributes.end(); p++)
468 va.push_back(p->second);
470 for(int i = 0, len = va.size(); i<len; i++)
477 void DF_LabelNode::Reset()
482 std::vector<DF_Attribute*> va;
483 typedef std::map<std::string, DF_Attribute*>::const_iterator AI;
484 for(AI p = _attributes.begin(); p!=_attributes.end(); p++)
485 va.push_back(p->second);
487 for(int i = 0, len = va.size(); i<len; i++)