Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FICN/src/tinyxml/ticpp.cc @ 739

Last change on this file since 739 was 739, checked in by landauf, 16 years ago

added TiCPP:

"'TiCPP' is short for the official name TinyXML++. It is a completely new interface to TinyXML that uses MANY of the C++ strengths. Templates, exceptions, and much better error handling."

website: http://code.google.com/p/ticpp/

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