Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/tinyxml/ticpp.cc @ 1625

Last change on this file since 1625 was 1625, checked in by rgrieder, 16 years ago

merged hud branch back to trunk

  • Property svn:eol-style set to native
File size: 24.4 KB
Line 
1#define TIXML_USE_TICPP
2
3/*
4http://code.google.com/p/ticpp/
5Copyright (c) 2006 Ryan Pusztai, Ryan Mulder
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of
8this software and associated documentation files (the "Software"), to deal in
9the Software without restriction, including without limitation the rights to
10use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11the Software, and to permit persons to whom the Software is furnished to do so,
12subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23*/
24
25/*
26Modifications by the orxonox team:
27In function: void Document::Parse( const std::string& xml, bool throwIfParseError, TiXmlEncoding encoding )
28     change: Added row and column number to the error description.
29     author: Reto Grieder
30*/
31
32#ifdef TIXML_USE_TICPP
33
34#include "ticpp.h"
35#include "ticpprc.h"
36#include "tinyxml.h"
37#include <sstream>
38
39using namespace ticpp;
40
41// In the following Visitor functions, casting away const should be safe, as the object can only be referred to by a const &
42bool Visitor::VisitEnter( const TiXmlDocument& doc )
43{
44        return VisitEnter( Document( const_cast< TiXmlDocument* >( &doc ) ) );
45}
46
47bool Visitor::VisitExit( const TiXmlDocument& doc )
48{
49        return VisitEnter( Document( const_cast< TiXmlDocument* >( &doc ) ) );
50}
51
52bool Visitor::VisitEnter( const TiXmlElement& element, const TiXmlAttribute* firstAttribute )
53{
54        if ( 0 != firstAttribute )
55        {
56                Attribute attribute( const_cast< TiXmlAttribute* >( firstAttribute ) );
57                return VisitEnter( Element( const_cast< TiXmlElement* >( &element ) ), &attribute );
58        }
59        else
60        {
61                return VisitEnter( Element( const_cast< TiXmlElement* >( &element ) ), 0 );
62        }
63}
64
65bool Visitor::VisitExit( const TiXmlElement& element )
66{
67        return VisitExit( Element( const_cast< TiXmlElement* >( &element ) ) );
68}
69
70bool Visitor::Visit( const TiXmlDeclaration& declaration )
71{
72        return Visit( Declaration( const_cast< TiXmlDeclaration* >( &declaration ) ) );
73}
74
75bool Visitor::Visit( const TiXmlStylesheetReference& stylesheet )
76{
77        return Visit( StylesheetReference( const_cast< TiXmlStylesheetReference* >( &stylesheet ) ) );
78}
79
80bool Visitor::Visit( const TiXmlText& text )
81{
82        return Visit( Text( const_cast< TiXmlText* >( &text ) ) );
83}
84
85bool Visitor::Visit( const TiXmlComment& comment )
86{
87        return Visit( Comment( const_cast< TiXmlComment* >( &comment ) ) );
88}
89
90Attribute::Attribute()
91{
92        SetTiXmlPointer( new TiXmlAttribute() );
93        m_impRC->InitRef();
94}
95
96Attribute::Attribute( TiXmlAttribute* attribute )
97{
98        SetTiXmlPointer( attribute );
99        m_impRC->IncRef();
100}
101
102Attribute::Attribute( const std::string& name, const std::string& value )
103{
104        SetTiXmlPointer( new TiXmlAttribute( name, value ) );
105        m_impRC->InitRef();
106}
107
108void Attribute::operator=( const Attribute& copy )
109{
110        // Dropping the reference to the old object
111        this->m_impRC->DecRef();
112
113        // Pointing to the new Object
114        SetTiXmlPointer( copy.m_tiXmlPointer );
115
116        // The internal tixml pointer changed in the above line
117        this->m_impRC->IncRef();
118}
119
120Attribute::Attribute( const Attribute& copy ) : Base()
121{
122        // Dropping the reference to the old object
123        this->m_impRC->DecRef();
124
125        // Pointing to the new Object
126        SetTiXmlPointer( copy.m_tiXmlPointer );
127
128        // The internal tixml pointer changed in the above line
129        this->m_impRC->IncRef();
130}
131
132Attribute::~Attribute()
133{
134        m_impRC->DecRef();
135}
136
137std::string Attribute::Value() const
138{
139        ValidatePointer();
140        return m_tiXmlPointer->ValueStr();
141}
142
143std::string Attribute::Name() const
144{
145        ValidatePointer();
146        return m_tiXmlPointer->Name();
147}
148
149Attribute* Attribute::Next( bool throwIfNoAttribute ) const
150{
151        ValidatePointer();
152        TiXmlAttribute* attribute = m_tiXmlPointer->Next();
153        if ( 0 == attribute )
154        {
155                if ( throwIfNoAttribute )
156                {
157                        TICPPTHROW( "No more attributes found" )
158                }
159                else
160                {
161                        return 0;
162                }
163        }
164
165        Attribute* temp = new Attribute( attribute );
166        m_spawnedWrappers.push_back( temp );
167
168        return temp;
169}
170
171Attribute* Attribute::Previous( bool throwIfNoAttribute ) const
172{
173        ValidatePointer();
174        TiXmlAttribute* attribute = m_tiXmlPointer->Previous();
175        if ( 0 == attribute )
176        {
177                if ( throwIfNoAttribute )
178                {
179                        TICPPTHROW( "No more attributes found" )
180                }
181                else
182                {
183                        return 0;
184                }
185        }
186
187        Attribute* temp = new Attribute( attribute );
188        m_spawnedWrappers.push_back( temp );
189
190        return temp;
191}
192
193void Attribute::IterateNext( const std::string&, Attribute** next ) const
194{
195        *next = Next( false );
196}
197
198void Attribute::IteratePrevious( const std::string&, Attribute** previous ) const
199{
200        *previous = Previous( false );
201}
202
203void Attribute::Print( FILE* file, int depth ) const
204{
205        ValidatePointer();
206        m_tiXmlPointer->Print( file, depth );
207}
208
209void Attribute::SetTiXmlPointer( TiXmlAttribute* newPointer )
210{
211        m_tiXmlPointer = newPointer;
212        SetImpRC( newPointer );
213}
214
215//*****************************************************************************
216
217Node* Node::NodeFactory( TiXmlNode* tiXmlNode, bool throwIfNull, bool rememberSpawnedWrapper ) const
218{
219        if ( 0 == tiXmlNode )
220        {
221                if ( throwIfNull )
222                {
223                        TICPPTHROW( "tiXmlNode is NULL" )
224                }
225                else
226                {
227                        return 0;
228                }
229        }
230
231        Node* temp;
232        switch ( tiXmlNode->Type() )
233        {
234                case TiXmlNode::DOCUMENT:
235                        temp = new Document( tiXmlNode->ToDocument() );
236                        break;
237
238                case TiXmlNode::ELEMENT:
239                        temp = new Element( tiXmlNode->ToElement() );
240                        break;
241
242                case TiXmlNode::COMMENT:
243                        temp = new Comment( tiXmlNode->ToComment() );
244                        break;
245
246                case TiXmlNode::TEXT:
247                        temp = new Text( tiXmlNode->ToText() );
248                        break;
249
250                case TiXmlNode::DECLARATION:
251                        temp = new Declaration( tiXmlNode->ToDeclaration() );
252                        break;
253
254                case TiXmlNode::STYLESHEETREFERENCE:
255                        temp = new StylesheetReference( tiXmlNode->ToStylesheetReference() );
256                        break;
257
258                default:
259                        TICPPTHROW( "Type is unsupported" )
260        }
261
262        if ( rememberSpawnedWrapper )
263        {
264                m_spawnedWrappers.push_back( temp );
265        }
266        return temp;
267}
268
269
270std::string Node::Value() const
271{
272        return GetTiXmlPointer()->ValueStr();
273}
274
275void Node::Clear()
276{
277        GetTiXmlPointer()->Clear();
278}
279
280Node* Node::Parent( bool throwIfNoParent ) const
281{
282        TiXmlNode* parent = GetTiXmlPointer()->Parent();
283        if ( ( 0 == parent ) && throwIfNoParent )
284        {
285                TICPPTHROW( "No parent exists" );
286        }
287
288        return NodeFactory( parent, false );
289}
290
291Node* Node::FirstChild( bool throwIfNoChildren ) const
292{
293        return FirstChild( "", throwIfNoChildren );
294}
295
296Node* Node::FirstChild( const std::string& value, bool throwIfNoChildren ) const
297{
298        return FirstChild( value.c_str(), throwIfNoChildren );
299}
300
301Node* Node::FirstChild( const char* value, bool throwIfNoChildren ) const
302{
303        TiXmlNode* childNode;
304        if ( 0 == strlen( value ) )
305        {
306                childNode = GetTiXmlPointer()->FirstChild();
307        }
308        else
309        {
310                childNode = GetTiXmlPointer()->FirstChild( value );
311        }
312
313        if ( ( 0 == childNode ) && throwIfNoChildren )
314        {
315                TICPPTHROW( "Child with the value of \"" << value << "\" not found" );
316        }
317
318        return NodeFactory( childNode, false );
319}
320
321Node* Node::LastChild( bool throwIfNoChildren ) const
322{
323        return LastChild( "", throwIfNoChildren );
324}
325
326Node* Node::LastChild( const std::string& value, bool throwIfNoChildren ) const
327{
328        return LastChild( value.c_str(), throwIfNoChildren );
329}
330
331Node* Node::LastChild( const char* value, bool throwIfNoChildren ) const
332{
333        TiXmlNode* childNode;
334        if ( 0 == strlen( value ) )
335        {
336                childNode = GetTiXmlPointer()->LastChild();
337        }
338        else
339        {
340                childNode = GetTiXmlPointer()->LastChild( value );
341        }
342
343        if ( ( 0 == childNode ) && throwIfNoChildren )
344        {
345                TICPPTHROW( "Child with the value of \"" << value << "\" not found" );
346        }
347
348        return NodeFactory( childNode, false );
349}
350
351Node* Node::IterateChildren ( Node* previous ) const
352{
353        TiXmlNode* pointer;
354        if ( 0 == previous )
355        {
356                pointer = GetTiXmlPointer()->IterateChildren( 0 );
357        }
358        else
359        {
360                pointer = GetTiXmlPointer()->IterateChildren( previous->GetTiXmlPointer() );
361        }
362
363        return NodeFactory( pointer, false );
364}
365
366Node* Node::IterateChildren( const std::string& value, Node* previous ) const
367{
368        TiXmlNode* pointer;
369        if ( 0 == previous )
370        {
371                pointer = GetTiXmlPointer()->IterateChildren( value, 0 );
372        }
373        else
374        {
375                pointer = GetTiXmlPointer()->IterateChildren( value, previous->GetTiXmlPointer() );
376        }
377
378        return NodeFactory( pointer, false );
379}
380
381Node* Node::InsertEndChild( Node& addThis )
382{
383        if ( addThis.Type() == TiXmlNode::DOCUMENT )
384        {
385                TICPPTHROW( "Node is a Document and can't be inserted" );
386        }
387
388        // Increment reference count when adding to the tree
389        addThis.m_impRC->IncRef();
390
391        TiXmlNode* pointer = GetTiXmlPointer()->InsertEndChild( *addThis.GetTiXmlPointer() );
392        if ( 0 == pointer )
393        {
394                TICPPTHROW( "Node can't be inserted" );
395        }
396
397        return NodeFactory( pointer );
398}
399
400Node* Node::LinkEndChild( Node* childNode )
401{
402        if ( childNode->Type() == TiXmlNode::DOCUMENT )
403        {
404                TICPPTHROW( "Node is a Document and can't be linked" );
405        }
406
407        // Increment reference count when adding to the tree
408        childNode->m_impRC->IncRef();
409
410        if ( 0 == GetTiXmlPointer()->LinkEndChild( childNode->GetTiXmlPointer() ) )
411        {
412                TICPPTHROW( "Node can't be linked" );
413        }
414
415        return childNode;
416}
417
418Node* Node::InsertBeforeChild( Node* beforeThis, Node& addThis )
419{
420        if ( addThis.Type() == TiXmlNode::DOCUMENT )
421        {
422                TICPPTHROW( "Node is a Document and can't be inserted" );
423        }
424
425        // Increment reference count when adding to the tree
426        addThis.m_impRC->IncRef();
427
428        TiXmlNode* pointer = GetTiXmlPointer()->InsertBeforeChild( beforeThis->GetTiXmlPointer(), *addThis.GetTiXmlPointer() );
429        if ( 0 == pointer )
430        {
431                TICPPTHROW( "Node can't be inserted" );
432        }
433
434        return NodeFactory( pointer );
435}
436
437Node* Node::InsertAfterChild( Node* afterThis, Node& addThis )
438{
439        if ( addThis.Type() == TiXmlNode::DOCUMENT )
440        {
441                TICPPTHROW( "Node is a Document and can't be inserted" );
442        }
443
444        // Increment reference count when adding to the tree
445        addThis.m_impRC->IncRef();
446
447        TiXmlNode* pointer = GetTiXmlPointer()->InsertAfterChild( afterThis->GetTiXmlPointer(), *addThis.GetTiXmlPointer() );
448        if ( 0 == pointer )
449        {
450                TICPPTHROW( "Node can't be inserted" );
451        }
452
453        return NodeFactory( pointer );
454}
455
456Node* Node::ReplaceChild( Node* replaceThis, Node& withThis )
457{
458        if ( withThis.Type() == TiXmlNode::DOCUMENT )
459        {
460                TICPPTHROW( "Node is a Document and can't be inserted" );
461        }
462
463        // Increment reference count when adding to the tree
464        withThis.m_impRC->IncRef();
465
466        TiXmlNode* pointer = GetTiXmlPointer()->ReplaceChild( replaceThis->GetTiXmlPointer(), *withThis.GetTiXmlPointer() );
467        if ( 0 == pointer )
468        {
469                TICPPTHROW( "Node can't be inserted" );
470        }
471
472        return NodeFactory( pointer );
473}
474
475void Node::RemoveChild( Node* removeThis )
476{
477        if  ( !GetTiXmlPointer()->RemoveChild( removeThis->GetTiXmlPointer() ) )
478        {
479                TICPPTHROW( "Node to remove (" << removeThis->Value() << ") is not a child of this Node (" << Value() << ")" )
480        }
481}
482
483Node* Node::PreviousSibling( bool throwIfNoSiblings ) const
484{
485        return PreviousSibling( "", throwIfNoSiblings );
486}
487
488Node* Node::PreviousSibling( const std::string& value, bool throwIfNoSiblings ) const
489{
490        return PreviousSibling( value.c_str(), throwIfNoSiblings );
491}
492
493Node* Node::PreviousSibling( const char* value, bool throwIfNoSiblings ) const
494{
495        TiXmlNode* sibling;
496        if ( 0 == strlen( value ) )
497        {
498                sibling = GetTiXmlPointer()->PreviousSibling();
499        }
500        else
501        {
502                sibling = GetTiXmlPointer()->PreviousSibling( value );
503        }
504
505        if ( ( 0 == sibling ) && throwIfNoSiblings )
506        {
507                TICPPTHROW( "No Siblings found with value, '" << value << "', Prior to this Node (" << Value() << ")" )
508        }
509
510        return NodeFactory( sibling, false );
511}
512
513Node* Node::NextSibling( bool throwIfNoSiblings ) const
514{
515        return NextSibling( "", throwIfNoSiblings );
516}
517
518Node* Node::NextSibling( const std::string& value, bool throwIfNoSiblings ) const
519{
520        return NextSibling( value.c_str(), throwIfNoSiblings );
521}
522
523Node* Node::NextSibling( const char* value, bool throwIfNoSiblings ) const
524{
525        TiXmlNode* sibling;
526        if ( 0 == strlen( value ) )
527        {
528                sibling = GetTiXmlPointer()->NextSibling();
529        }
530        else
531        {
532                sibling = GetTiXmlPointer()->NextSibling( value );
533        }
534
535        if ( ( 0 == sibling ) && throwIfNoSiblings )
536        {
537                TICPPTHROW( "No Siblings found with value, '" << value << "', After this Node (" << Value() << ")" )
538        }
539
540        return NodeFactory( sibling, false );
541}
542
543Element* Node::NextSiblingElement( bool throwIfNoSiblings ) const
544{
545        return NextSiblingElement( "", throwIfNoSiblings );
546}
547
548Element* Node::NextSiblingElement( const std::string& value, bool throwIfNoSiblings ) const
549{
550        return NextSiblingElement( value.c_str(), throwIfNoSiblings );
551}
552
553Element* Node::NextSiblingElement( const char* value, bool throwIfNoSiblings ) const
554{
555        TiXmlElement* sibling;
556        if ( 0 == strlen( value ) )
557        {
558                sibling = GetTiXmlPointer()->NextSiblingElement();
559        }
560        else
561        {
562                sibling = GetTiXmlPointer()->NextSiblingElement( value );
563        }
564
565        if ( 0 == sibling )
566        {
567                if ( throwIfNoSiblings )
568                {
569                        TICPPTHROW( "No Element Siblings found with value, '" << value << "', After this Node (" << Value() << ")" )
570                }
571                else
572                {
573                        return 0;
574                }
575        }
576
577        Element* temp = new Element( sibling );
578        m_spawnedWrappers.push_back( temp );
579
580        return temp;
581}
582
583Element* Node::FirstChildElement( bool throwIfNoChildren ) const
584{
585        return FirstChildElement( "", throwIfNoChildren );
586}
587
588Element* Node::FirstChildElement( const std::string& value, bool throwIfNoChildren ) const
589{
590        return FirstChildElement( value.c_str(), throwIfNoChildren );
591}
592
593Element* Node::FirstChildElement( const char* value, bool throwIfNoChildren ) const
594{
595        TiXmlElement* element;
596        if ( 0 == strlen( value ) )
597        {
598                element = GetTiXmlPointer()->FirstChildElement();
599        }
600        else
601        {
602                element = GetTiXmlPointer()->FirstChildElement( value );
603        }
604
605        if ( 0 == element )
606        {
607                if( throwIfNoChildren )
608                {
609                        TICPPTHROW( "Element (" << Value() << ") does NOT contain a child with the value of '" << value << "'" )
610                }
611                else
612                {
613                        return 0;
614                }
615        }
616
617        Element* temp = new Element( element );
618        m_spawnedWrappers.push_back( temp );
619
620        return temp;
621}
622
623int Node::Type() const
624{
625        return GetTiXmlPointer()->Type();
626}
627
628Document* Node::GetDocument( bool throwIfNoDocument ) const
629{
630        TiXmlDocument* doc = GetTiXmlPointer()->GetDocument();
631        if ( 0 == doc )
632        {
633                if( throwIfNoDocument )
634                {
635                        TICPPTHROW( "This node (" << Value() << ") is not linked under a document" )
636                }
637                else
638                {
639                        return 0;
640                }
641        }
642        Document* temp = new Document( doc );
643        m_spawnedWrappers.push_back( temp );
644
645        return temp;
646}
647
648bool Node::NoChildren() const
649{
650        return GetTiXmlPointer()->NoChildren();
651}
652
653Document* Node::ToDocument() const
654{
655        TiXmlDocument* doc = GetTiXmlPointer()->ToDocument();
656        if ( 0 == doc )
657        {
658                TICPPTHROW( "This node (" << Value() << ") is not a Document" )
659        }
660        Document* temp = new Document( doc );
661        m_spawnedWrappers.push_back( temp );
662
663        return temp;
664}
665
666Element* Node::ToElement() const
667{
668        TiXmlElement* doc = GetTiXmlPointer()->ToElement();
669        if ( 0 == doc )
670        {
671                TICPPTHROW( "This node (" << Value() << ") is not a Element" )
672        }
673        Element* temp = new Element( doc );
674        m_spawnedWrappers.push_back( temp );
675
676        return temp;
677}
678
679Comment* Node::ToComment() const
680{
681        TiXmlComment* doc = GetTiXmlPointer()->ToComment();
682        if ( 0 == doc )
683        {
684                TICPPTHROW( "This node (" << Value() << ") is not a Comment" )
685        }
686        Comment* temp = new Comment( doc );
687        m_spawnedWrappers.push_back( temp );
688
689        return temp;
690}
691
692Text* Node::ToText() const
693{
694        TiXmlText* doc = GetTiXmlPointer()->ToText();
695        if ( 0 == doc )
696        {
697                TICPPTHROW( "This node (" << Value() << ") is not a Text" )
698        }
699        Text* temp = new Text( doc );
700        m_spawnedWrappers.push_back( temp );
701
702        return temp;
703}
704
705Declaration* Node::ToDeclaration() const
706{
707        TiXmlDeclaration* doc = GetTiXmlPointer()->ToDeclaration();
708        if ( 0 == doc )
709        {
710                TICPPTHROW( "This node (" << Value() << ") is not a Declaration" )
711        }
712        Declaration* temp = new Declaration( doc );
713        m_spawnedWrappers.push_back( temp );
714
715        return temp;
716}
717
718StylesheetReference* Node::ToStylesheetReference() const
719{
720        TiXmlStylesheetReference* doc = GetTiXmlPointer()->ToStylesheetReference();
721        if ( 0 == doc )
722        {
723                TICPPTHROW( "This node (" << Value() << ") is not a StylesheetReference" )
724        }
725        StylesheetReference* temp = new StylesheetReference( doc );
726        m_spawnedWrappers.push_back( temp );
727
728        return temp;
729}
730
731std::auto_ptr< Node > Node::Clone() const
732{
733        TiXmlNode* node = GetTiXmlPointer()->Clone();
734        if ( 0 == node )
735        {
736                TICPPTHROW( "Node could not be cloned" );
737        }
738        std::auto_ptr< Node > temp( NodeFactory( node, false, false ) );
739
740        // Take ownership of the memory from TiXml
741        temp->m_impRC->InitRef();
742
743        return temp;
744}
745
746bool Node::Accept( TiXmlVisitor* visitor ) const
747{
748        return GetTiXmlPointer()->Accept( visitor );
749}
750
751//*****************************************************************************
752
753Comment::Comment()
754: NodeImp< TiXmlComment >( new TiXmlComment() )
755{
756        m_impRC->InitRef();
757}
758
759Comment::Comment( TiXmlComment* comment )
760: NodeImp< TiXmlComment >( comment )
761{
762}
763
764Comment::Comment( const std::string& comment )
765: NodeImp< TiXmlComment >( new TiXmlComment() )
766{
767        m_impRC->InitRef();
768        m_tiXmlPointer->SetValue( comment );
769}
770
771//*****************************************************************************
772
773Text::Text()
774: NodeImp< TiXmlText >( new TiXmlText("") )
775{
776        m_impRC->InitRef();
777}
778
779
780Text::Text( const std::string& value )
781: NodeImp< TiXmlText >( new TiXmlText( value ) )
782{
783        m_impRC->InitRef();
784}
785
786Text::Text( TiXmlText* text )
787: NodeImp< TiXmlText >( text )
788{
789}
790
791
792//*****************************************************************************
793
794Document::Document()
795: NodeImp< TiXmlDocument >( new TiXmlDocument() )
796{
797        m_impRC->InitRef();
798}
799
800Document::Document( TiXmlDocument* document )
801: NodeImp< TiXmlDocument >( document )
802{
803}
804
805Document::Document( const char* documentName )
806: NodeImp< TiXmlDocument >( new TiXmlDocument( documentName ) )
807{
808        m_impRC->InitRef();
809}
810
811Document::Document( const std::string& documentName )
812: NodeImp< TiXmlDocument >( new TiXmlDocument( documentName ) )
813{
814        m_impRC->InitRef();
815}
816
817void Document::LoadFile( TiXmlEncoding encoding )
818{
819        if ( !m_tiXmlPointer->LoadFile( encoding ) )
820        {
821                TICPPTHROW( "Couldn't load " << m_tiXmlPointer->Value() );
822        }
823}
824
825void Document::SaveFile( void ) const
826{
827        if ( !m_tiXmlPointer->SaveFile() )
828        {
829                TICPPTHROW( "Couldn't save " << m_tiXmlPointer->Value() );
830        }
831}
832
833void Document::LoadFile( const std::string& filename, TiXmlEncoding encoding )
834{
835        if ( !m_tiXmlPointer->LoadFile( filename.c_str(), encoding ) )
836        {
837                TICPPTHROW( "Couldn't load " << filename );
838        }
839}
840
841void Document::LoadFile( const char* filename, TiXmlEncoding encoding )
842{
843        if ( !m_tiXmlPointer->LoadFile( filename, encoding ) )
844        {
845                TICPPTHROW( "Couldn't load " << filename );
846        }
847}
848
849void Document::SaveFile( const std::string& filename ) const
850{
851        if ( !m_tiXmlPointer->SaveFile( filename.c_str() ) )
852        {
853                TICPPTHROW( "Couldn't save " << filename );
854        }
855}
856
857void Document::Parse( const std::string& xml, bool throwIfParseError, TiXmlEncoding encoding )
858{
859        m_tiXmlPointer->Parse( xml.c_str(), 0, encoding );
860        if( throwIfParseError && m_tiXmlPointer->Error() )
861        {
862                TICPPTHROW( "Error parsing xml: " << m_tiXmlPointer->ErrorDesc()
863            << " In row " << m_tiXmlPointer->ErrorRow() << ", column " << m_tiXmlPointer->ErrorCol() << ".");
864        }
865}
866
867//*****************************************************************************
868
869Element::Element()
870: NodeImp< TiXmlElement >( new TiXmlElement( "DefaultValueCausedByCreatingAnElementWithNoParameters" ) )
871{
872        m_impRC->InitRef();
873}
874
875Element::Element( const std::string& value )
876: NodeImp< TiXmlElement >( new TiXmlElement( value ) )
877{
878        m_impRC->InitRef();
879}
880
881Element::Element( const char* value )
882: NodeImp< TiXmlElement >( new TiXmlElement( value ) )
883{
884        m_impRC->InitRef();
885}
886
887Element::Element( TiXmlElement* element )
888: NodeImp< TiXmlElement >( element )
889{
890}
891
892Attribute* Element::FirstAttribute( bool throwIfNoAttributes ) const
893{
894        ValidatePointer();
895        TiXmlAttribute* attribute = m_tiXmlPointer->FirstAttribute();
896        if ( ( 0 == attribute ) && throwIfNoAttributes )
897        {
898                TICPPTHROW( "This Element (" << Value() << ") has no attributes" )
899        }
900
901        if ( 0 == attribute )
902        {
903                if( throwIfNoAttributes )
904                {
905                        TICPPTHROW( "Element (" << Value() << ") has no attributes" )
906                }
907                else
908                {
909                        return 0;
910                }
911        }
912
913        Attribute* temp = new Attribute( attribute );
914        m_spawnedWrappers.push_back( temp );
915
916        return temp;
917}
918
919Attribute* Element::LastAttribute( bool throwIfNoAttributes ) const
920{
921        ValidatePointer();
922        TiXmlAttribute* attribute = m_tiXmlPointer->LastAttribute();
923        if ( ( 0 == attribute ) && throwIfNoAttributes )
924        {
925                TICPPTHROW( "This Element (" << Value() << ") has no attributes" )
926        }
927
928        if ( 0 == attribute )
929        {
930                if( throwIfNoAttributes )
931                {
932                        TICPPTHROW( "Element (" << Value() << ") has no attributes" )
933                }
934                else
935                {
936                        return 0;
937                }
938        }
939
940        Attribute* temp = new Attribute( attribute );
941        m_spawnedWrappers.push_back( temp );
942
943        return temp;
944}
945
946std::string Element::GetAttributeOrDefault( const std::string& name, const std::string& defaultValue ) const
947{
948        std::string value;
949        if ( !GetAttributeImp( name, &value ) )
950        {
951                return defaultValue;
952        }
953        return value;
954}
955
956std::string Element::GetAttribute( const std::string& name ) const
957{
958        return GetAttributeOrDefault( name, std::string() );
959}
960
961bool Element::GetAttributeImp( const std::string& name, std::string* value ) const
962{
963        ValidatePointer();
964
965        // Get value from TinyXML, if the attribute exists
966        const char* retVal = m_tiXmlPointer->Attribute( name.c_str() );
967
968        // TinyXML returns NULL if the attribute doesn't exist
969        if ( 0 == retVal )
970        {
971                return false;
972        }
973        else
974        {
975                *value = retVal;
976                return true;
977        }
978}
979
980bool Element::GetTextImp( std::string* value ) const
981{
982        ValidatePointer();
983
984        // Get value from TinyXML, if the attribute exists
985        const char* retVal = m_tiXmlPointer->GetText();
986
987        // TinyXML returns NULL if the attribute doesn't exist
988        if ( 0 == retVal )
989        {
990                return false;
991        }
992        else
993        {
994                *value = retVal;
995                return true;
996        }
997}
998
999//*****************************************************************************
1000
1001Declaration::Declaration()
1002: NodeImp< TiXmlDeclaration >( new TiXmlDeclaration() )
1003{
1004        m_impRC->InitRef();
1005}
1006
1007Declaration::Declaration( TiXmlDeclaration* declaration )
1008: NodeImp< TiXmlDeclaration >( declaration )
1009{
1010}
1011
1012Declaration::Declaration( const std::string& version, const std::string& encoding, const std::string& standalone )
1013: NodeImp< TiXmlDeclaration >( new TiXmlDeclaration( version, encoding, standalone ) )
1014{
1015        m_impRC->InitRef();
1016}
1017
1018std::string Declaration::Version() const
1019{
1020        return m_tiXmlPointer->Version();
1021}
1022
1023std::string Declaration::Encoding() const
1024{
1025        return m_tiXmlPointer->Encoding();
1026}
1027
1028std::string Declaration::Standalone() const
1029{
1030        return m_tiXmlPointer->Standalone();
1031}
1032
1033//*****************************************************************************
1034
1035StylesheetReference::StylesheetReference()
1036: NodeImp< TiXmlStylesheetReference >( new TiXmlStylesheetReference() )
1037{
1038        m_impRC->InitRef();
1039}
1040
1041StylesheetReference::StylesheetReference( TiXmlStylesheetReference* stylesheetReference )
1042: NodeImp< TiXmlStylesheetReference >( stylesheetReference )
1043{
1044}
1045
1046StylesheetReference::StylesheetReference( const std::string& type, const std::string& href )
1047: NodeImp< TiXmlStylesheetReference >( new TiXmlStylesheetReference( type, href ) )
1048{
1049        m_impRC->InitRef();
1050}
1051
1052std::string StylesheetReference::Type() const
1053{
1054        return m_tiXmlPointer->Type();
1055}
1056
1057std::string StylesheetReference::Href() const
1058{
1059        return m_tiXmlPointer->Href();
1060}
1061
1062//*****************************************************************************
1063
1064Exception::Exception(const std::string &details)
1065:
1066m_details( details )
1067{
1068
1069}
1070
1071Exception::~Exception() throw()
1072{
1073}
1074
1075const char* Exception::what() const throw()
1076{
1077        return m_details.c_str();
1078}
1079
1080//*****************************************************************************
1081
1082TiCppRC::TiCppRC()
1083{
1084        // Spawn reference counter for this object
1085        m_tiRC = new TiCppRCImp( this );
1086}
1087
1088TiCppRC::~TiCppRC()
1089{
1090        // Set pointer held by reference counter to NULL
1091        this->m_tiRC->Nullify();
1092
1093        // Decrement reference - so reference counter will delete itself if necessary
1094        this->m_tiRC->DecRef();
1095}
1096
1097//*****************************************************************************
1098
1099TiCppRCImp::TiCppRCImp( TiCppRC* tiCppRC )
1100        : m_count( 1 ), m_tiCppRC ( tiCppRC )
1101{
1102}
1103
1104void TiCppRCImp::IncRef()
1105{
1106        m_count++;
1107}
1108
1109void TiCppRCImp::DecRef()
1110{
1111        m_count--;
1112        if ( 0 == m_count )
1113        {
1114                delete m_tiCppRC;
1115                delete this;
1116        }
1117}
1118
1119void TiCppRCImp::InitRef()
1120{
1121        m_count = 1;
1122}
1123
1124void TiCppRCImp::Nullify()
1125{
1126        m_tiCppRC = 0;
1127}
1128
1129TiCppRC* TiCppRCImp::Get()
1130{
1131        return m_tiCppRC;
1132}
1133
1134bool TiCppRCImp::IsNull()
1135{
1136        return 0 == m_tiCppRC;
1137}
1138
1139#endif // TIXML_USE_TICPP
Note: See TracBrowser for help on using the repository browser.