XML.h

Go to the documentation of this file.
00001 /*
00002 www.sourceforge.net/projects/tinyxml
00003 Original code (2.0 and earlier )copyright (c) 2000-2002 Lee Thomason (www.grinninglizard.com)
00004 
00005 This software is provided 'as-is', without any express or implied
00006 warranty. In no event will the authors be held liable for any
00007 damages arising from the use of this software.
00008 
00009 Permission is granted to anyone to use this software for any
00010 purpose, including commercial applications, and to alter it and
00011 redistribute it freely, subject to the following restrictions:
00012 
00013 1. The origin of this software must not be misrepresented; you must
00014 not claim that you wrote the original software. If you use this
00015 software in a product, an acknowledgment in the product documentation
00016 would be appreciated but is not required.
00017 
00018 2. Altered source versions must be plainly marked as such, and
00019 must not be misrepresented as being the original software.
00020 
00021 3. This notice may not be removed or altered from any source
00022 distribution.
00023 */
00024 
00041 
00042 #ifndef __LDK_XML_H__
00043 #define __LDK_XML_H__
00044 
00045 #ifdef _MSC_VER
00046 #pragma warning( disable : 4530 )
00047 #pragma warning( disable : 4786 )
00048 #endif
00049 
00050 #include "LDK/VFileSystem.h"
00051 #include "LDK/String.h"
00052 
00053 #ifdef LDKXML_USE_STL
00054 #include <iostream>
00055 #define LDKXML_STRING    LDK::String
00056 #define LDKXML_ISTREAM   std::istream
00057 #define LDKXML_OSTREAM   std::ostream
00058 #else
00059 #define LDKXML_STRING    LDK::String
00060 #define LDKXML_OSTREAM   XmlOutStream
00061 #endif //LDKXML_USE_STL
00062 
00063 //#include <ctype.h>
00064 //#include <stdio.h>
00065 //#include <stdlib.h>
00066 //#include <string.h>
00067 //#include <assert.h>
00068 
00069 //TODO: make sure this code is safe to use in a thread local fashion
00070 
00071 #define LDKXML_LOG VFileSystem::stdOut()->printf
00072 
00073 namespace LDK
00074 {
00075 
00076 /*
00077    XmlOutStream is an emulation of std::ostream. It is based on String.
00078    Only the operators that we need for TinyXML have been developped.
00079 */
00080 class LDK_API XmlOutStream : public String
00081 {
00082 public :
00083     XmlOutStream () : String () {}
00084 
00085     // XmlOutStream << operator. Maps to String::append
00086     XmlOutStream & operator << (const char * in)
00087     {
00088         append (in);
00089         return (* this);
00090     }
00091 
00092     // XmlOutStream << operator. Maps to String::append
00093     XmlOutStream & operator << (const String & in)
00094     {
00095         append (in . c_str ());
00096         return (* this);
00097     }
00098 } ;
00099 
00100 
00101 class LDK_API XmlDocument;
00102 class LDK_API XmlElement;
00103 class LDK_API XmlComment;
00104 class LDK_API XmlUnknown;
00105 class LDK_API XmlAttribute;
00106 class LDK_API XmlText;
00107 class LDK_API XmlDeclaration;
00108 class LDK_API XmlParsingData;
00109 
00110 const int LDKXML_MAJOR_VERSION = 2;
00111 const int LDKXML_MINOR_VERSION = 3;
00112 const int LDKXML_PATCH_VERSION = 4;
00113 
00114 /*  Internal structure for tracking location of items
00115     in the XML file.
00116 */
00117 struct LDK_API XmlCursor
00118 {
00119     XmlCursor()       { Clear(); }
00120     void Clear()        { row = col = -1; }
00121 
00122     int row;    // 0 based.
00123     int col;    // 0 based.
00124 };
00125 
00126 
00127 // Only used by Attribute::Query functions
00128 enum
00129 {
00130     LDKXML_SUCCESS,
00131     LDKXML_NO_ATTRIBUTE,
00132     LDKXML_WRONG_TYPE
00133 };
00134 
00135 
00136 // Used by the parsing routines.
00137 enum XmlEncoding
00138 {
00139     LDKXML_ENCODING_UNKNOWN,
00140     LDKXML_ENCODING_UTF8,
00141     LDKXML_ENCODING_LEGACY
00142 };
00143 
00144 const extern LDK_API XmlEncoding LDKXML_DEFAULT_ENCODING;
00145 
00168 class LDK_API XmlBase
00169 {
00170     friend class LDK_API XmlNode;
00171     friend class LDK_API XmlElement;
00172     friend class LDK_API XmlDocument;
00173 
00174 public:
00175     XmlBase() :   userData(0) {}
00176     virtual ~XmlBase()                    {}
00177 
00183     virtual void Print( SmartFile f, int depth ) const = 0;
00184 
00191     static void SetCondenseWhiteSpace( bool condense )      { condenseWhiteSpace = condense; }
00192 
00194     static bool IsWhiteSpaceCondensed()                     { return condenseWhiteSpace; }
00195 
00214     int Row() const         { return location.row + 1; }
00215     int Column() const      { return location.col + 1; }    
00216 
00217     void  SetUserData( void* user )         { userData = user; }
00218     void* GetUserData()                     { return userData; }
00219 
00220     // Table that returs, for a given lead byte, the total number of bytes
00221     // in the UTF-8 sequence.
00222     static const int utf8ByteTable[256];
00223 
00224     virtual const char* Parse(  const char* p,
00225                                 XmlParsingData* data,
00226                                 XmlEncoding encoding /*= LDKXML_ENCODING_UNKNOWN */ ) = 0;
00227 
00228     enum
00229     {
00230         LDKXML_NO_ERROR = 0,
00231         LDKXML_ERROR,
00232         LDKXML_ERROR_OPENING_FILE,
00233         LDKXML_ERROR_OUT_OF_MEMORY,
00234         LDKXML_ERROR_PARSING_ELEMENT,
00235         LDKXML_ERROR_FAILED_TO_READ_ELEMENT_NAME,
00236         LDKXML_ERROR_READING_ELEMENT_VALUE,
00237         LDKXML_ERROR_READING_ATTRIBUTES,
00238         LDKXML_ERROR_PARSING_EMPTY,
00239         LDKXML_ERROR_READING_END_TAG,
00240         LDKXML_ERROR_PARSING_UNKNOWN,
00241         LDKXML_ERROR_PARSING_COMMENT,
00242         LDKXML_ERROR_PARSING_DECLARATION,
00243         LDKXML_ERROR_DOCUMENT_EMPTY,
00244         LDKXML_ERROR_EMBEDDED_NULL,
00245 
00246         LDKXML_ERROR_STRING_COUNT
00247     };
00248 
00249 protected:
00250 
00251     static const char*  SkipWhiteSpace( const char*, XmlEncoding encoding );
00252     inline static bool  IsWhiteSpace( char c )
00253     {
00254         return ( isspace( (unsigned char) c ) || c == '\n' || c == '\r' );
00255     }
00256 
00257     virtual void StreamOut (LDKXML_OSTREAM *) const = 0;
00258 
00259     #ifdef LDKXML_USE_STL
00260         static bool StreamWhiteSpace( LDKXML_ISTREAM * in, LDKXML_STRING * tag );
00261         static bool StreamTo( LDKXML_ISTREAM * in, int character, LDKXML_STRING * tag );
00262     #endif
00263 
00264     /*  Reads an XML name into the string provided. Returns
00265         a pointer just past the last character of the name,
00266         or 0 if the function has an error.
00267     */
00268     static const char* ReadName( const char* p, LDKXML_STRING* name, XmlEncoding encoding );
00269 
00270     /*  Reads text. Returns a pointer past the given end tag.
00271         Wickedly complex options, but it keeps the (sensitive) code in one place.
00272     */
00273     static const char* ReadText(    const char* in,             // where to start
00274                                     LDKXML_STRING* text,         // the string read
00275                                     bool ignoreWhiteSpace,      // whether to keep the white space
00276                                     const char* endTag,         // what ends this text
00277                                     bool ignoreCase,            // whether to ignore case in the end tag
00278                                     XmlEncoding encoding );   // the current encoding
00279 
00280     // If an entity has been found, transform it into a character.
00281     static const char* GetEntity( const char* in, char* value, int* length, XmlEncoding encoding );
00282 
00283     // Get a character, while interpreting entities.
00284     // The length can be from 0 to 4 bytes.
00285     inline static const char* GetChar( const char* p, char* _value, int* length, XmlEncoding encoding )
00286     {
00287         assert( p );
00288         if ( encoding == LDKXML_ENCODING_UTF8 )
00289         {
00290             *length = utf8ByteTable[ *((unsigned char*)p) ];
00291             assert( *length >= 0 && *length < 5 );
00292         }
00293         else
00294         {
00295             *length = 1;
00296         }
00297 
00298         if ( *length == 1 )
00299         {
00300             if ( *p == '&' )
00301                 return GetEntity( p, _value, length, encoding );
00302             *_value = *p;
00303             return p+1;
00304         }
00305         else if ( *length )
00306         {
00307             strncpy( _value, p, *length );
00308             return p + (*length);
00309         }
00310         else
00311         {
00312             // Not valid text.
00313             return 0;
00314         }
00315     }
00316 
00317     // Puts a string to a stream, expanding entities as it goes.
00318     // Note this should not contian the '<', '>', etc, or they will be transformed into entities!
00319     static void PutString( const LDKXML_STRING& str, LDKXML_OSTREAM* out );
00320 
00321     static void PutString( const LDKXML_STRING& str, LDKXML_STRING* out );
00322 
00323     // Return true if the next characters in the stream are any of the endTag sequences.
00324     // Ignore case only works for english, and should only be relied on when comparing
00325     // to Engilish words: StringEqual( p, "version", true ) is fine.
00326     static bool StringEqual(    const char* p,
00327                                 const char* endTag,
00328                                 bool ignoreCase,
00329                                 XmlEncoding encoding );
00330 
00331     static const char* errorString[ LDKXML_ERROR_STRING_COUNT ];
00332 
00333     XmlCursor location;
00334 
00336     void*           userData;
00337 
00338     // None of these methods are reliable for any language except English.
00339     // Good for approximation, not great for accuracy.
00340     static int IsAlpha( unsigned char anyByte, XmlEncoding encoding );
00341     static int IsAlphaNum( unsigned char anyByte, XmlEncoding encoding );
00342     inline static int ToLower( int v, XmlEncoding encoding )
00343     {
00344         if ( encoding == LDKXML_ENCODING_UTF8 )
00345         {
00346             if ( v < 128 ) return tolower( v );
00347             return v;
00348         }
00349         else
00350         {
00351             return tolower( v );
00352         }
00353     }
00354     static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
00355 
00356 private:
00357     XmlBase( const XmlBase& );              // not implemented.
00358     void operator=( const XmlBase& base );    // not allowed.
00359 
00360     struct Entity
00361     {
00362         const char*     str;
00363         unsigned int    strLength;
00364         char            chr;
00365     };
00366     enum
00367     {
00368         NUM_ENTITY = 5,
00369         MAX_ENTITY_LENGTH = 6
00370 
00371     };
00372     static Entity entity[ NUM_ENTITY ];
00373     static bool condenseWhiteSpace;
00374 };
00375 
00376 
00383 class LDK_API XmlNode : public XmlBase
00384 {
00385     friend class LDK_API XmlDocument;
00386     friend class LDK_API XmlElement;
00387 
00388 public:
00389     #ifdef LDKXML_USE_STL
00390 
00394         friend std::istream& operator >> (std::istream& in, XmlNode& base);
00395 
00412         friend std::ostream& operator<< (std::ostream& out, const XmlNode& base);
00413 
00415         friend String& operator<< (String& out, const XmlNode& base );
00416 
00417     #else
00418         // Used internally, not part of the public API.
00419         friend LDKXML_OSTREAM& operator<< (LDKXML_OSTREAM& out, const XmlNode& base);
00420     #endif
00421 
00425     enum NodeType
00426     {
00427         DOCUMENT,
00428         ELEMENT,
00429         COMMENT,
00430         UNKNOWN,
00431         TEXT,
00432         DECLARATION,
00433         TYPECOUNT
00434     };
00435 
00436     virtual ~XmlNode();
00437 
00450     const char * Value() const { return value.c_str (); }
00451 
00461     void SetValue(const char * _value) { value = _value;}
00462 
00463 
00465     void SetValue( const String& _value )
00466     {
00467         value = _value;
00468     }
00469 
00470 
00472     void Clear();
00473 
00475     XmlNode* Parent()                         { return parent; }
00476     const XmlNode* Parent() const             { return parent; }
00477 
00478     const XmlNode* FirstChild()   const   { return firstChild; }      
00479     XmlNode* FirstChild()                 { return firstChild; }
00480     const XmlNode* FirstChild( const char * value ) const;            
00481     XmlNode* FirstChild( const char * value );                        
00482 
00483     const XmlNode* LastChild() const  { return lastChild; }       
00484     XmlNode* LastChild()  { return lastChild; }
00485     const XmlNode* LastChild( const char * value ) const;         
00486     XmlNode* LastChild( const char * value );
00487 
00488     const XmlNode* FirstChild( const String& _value ) const  {   return FirstChild (_value.c_str ());    }   
00489     XmlNode* FirstChild( const String& _value )              {   return FirstChild (_value.c_str ());    }   
00490     const XmlNode* LastChild( const String& _value ) const   {   return LastChild (_value.c_str ()); }   
00491     XmlNode* LastChild( const String& _value )               {   return LastChild (_value.c_str ()); }   
00492 
00509     const XmlNode* IterateChildren( const XmlNode* previous ) const;
00510     XmlNode* IterateChildren( XmlNode* previous );
00511 
00513     const XmlNode* IterateChildren( const char * value, const XmlNode* previous ) const;
00514     XmlNode* IterateChildren( const char * value, XmlNode* previous );
00515 
00516     const XmlNode* IterateChildren( const String& _value, const XmlNode* previous ) const  {   return IterateChildren (_value.c_str (), previous); }   
00517     XmlNode* IterateChildren( const String& _value, XmlNode* previous ) {  return IterateChildren (_value.c_str (), previous); }   
00518 
00522     XmlNode* InsertEndChild( const XmlNode& addThis );
00523 
00524 
00534     XmlNode* LinkEndChild( XmlNode* addThis );
00535 
00539     XmlNode* InsertBeforeChild( XmlNode* beforeThis, const XmlNode& addThis );
00540 
00544     XmlNode* InsertAfterChild(  XmlNode* afterThis, const XmlNode& addThis );
00545 
00549     XmlNode* ReplaceChild( XmlNode* replaceThis, const XmlNode& withThis );
00550 
00552     bool RemoveChild( XmlNode* removeThis );
00553 
00555     const XmlNode* PreviousSibling() const            { return prev; }
00556     XmlNode* PreviousSibling()                        { return prev; }
00557 
00559     const XmlNode* PreviousSibling( const char * ) const;
00560     XmlNode* PreviousSibling( const char * );
00561 
00562     const XmlNode* PreviousSibling( const String& _value ) const {   return PreviousSibling (_value.c_str ());   }   
00563     XmlNode* PreviousSibling( const String& _value )             {   return PreviousSibling (_value.c_str ());   }   
00564     const XmlNode* NextSibling( const String& _value) const      {   return NextSibling (_value.c_str ());   }   
00565     XmlNode* NextSibling( const String& _value)                  {   return NextSibling (_value.c_str ());   }   
00566 
00568     const XmlNode* NextSibling() const                { return next; }
00569     XmlNode* NextSibling()                            { return next; }
00570 
00572     const XmlNode* NextSibling( const char * ) const;
00573     XmlNode* NextSibling( const char * );
00574 
00579     const XmlElement* NextSiblingElement() const;
00580     XmlElement* NextSiblingElement();
00581 
00586     const XmlElement* NextSiblingElement( const char * ) const;
00587     XmlElement* NextSiblingElement( const char * );
00588 
00589     const XmlElement* NextSiblingElement( const String& _value) const    {   return NextSiblingElement (_value.c_str ());    }   
00590     XmlElement* NextSiblingElement( const String& _value)                {   return NextSiblingElement (_value.c_str ());    }   
00591 
00593     const XmlElement* FirstChildElement() const;
00594     XmlElement* FirstChildElement();
00595 
00597     const XmlElement* FirstChildElement( const char * value ) const;
00598     XmlElement* FirstChildElement( const char * value );
00599 
00600     const XmlElement* FirstChildElement( const String& _value ) const    {   return FirstChildElement (_value.c_str ()); }   
00601     XmlElement* FirstChildElement( const String& _value )                {   return FirstChildElement (_value.c_str ()); }   
00602 
00607     virtual int Type() const    { return type; }
00608 
00612     const XmlDocument* GetDocument() const;
00613     XmlDocument* GetDocument();
00614 
00616     bool NoChildren() const                     { return !firstChild; }
00617 
00618     const XmlDocument* ToDocument()   const       { return ( this && type == DOCUMENT ) ? (const XmlDocument*) this : 0; } 
00619     const XmlElement*  ToElement() const          { return ( this && type == ELEMENT  ) ? (const XmlElement*)  this : 0; } 
00620     const XmlComment*  ToComment() const          { return ( this && type == COMMENT  ) ? (const XmlComment*)  this : 0; } 
00621     const XmlUnknown*  ToUnknown() const          { return ( this && type == UNKNOWN  ) ? (const XmlUnknown*)  this : 0; } 
00622     const XmlText*       ToText()    const        { return ( this && type == TEXT     ) ? (const XmlText*)     this : 0; } 
00623     const XmlDeclaration* ToDeclaration() const   { return ( this && type == DECLARATION ) ? (const XmlDeclaration*) this : 0; } 
00624 
00625     XmlDocument* ToDocument()         { return ( this && type == DOCUMENT ) ? (XmlDocument*) this : 0; } 
00626     XmlElement*  ToElement()          { return ( this && type == ELEMENT  ) ? (XmlElement*)  this : 0; } 
00627     XmlComment*  ToComment()          { return ( this && type == COMMENT  ) ? (XmlComment*)  this : 0; } 
00628     XmlUnknown*  ToUnknown()          { return ( this && type == UNKNOWN  ) ? (XmlUnknown*)  this : 0; } 
00629     XmlText*     ToText()             { return ( this && type == TEXT     ) ? (XmlText*)     this : 0; } 
00630     XmlDeclaration* ToDeclaration()   { return ( this && type == DECLARATION ) ? (XmlDeclaration*) this : 0; } 
00631 
00635     virtual XmlNode* Clone() const = 0;
00636 
00637 protected:
00638     XmlNode( NodeType _type );
00639 
00640     // Copy to the allocated object. Shared functionality between Clone, Copy constructor,
00641     // and the assignment operator.
00642     void CopyTo( XmlNode* target ) const;
00643 
00644     #ifdef LDKXML_USE_STL
00645         // The real work of the input operator.
00646         virtual void StreamIn( LDKXML_ISTREAM* in, LDKXML_STRING* tag ) = 0;
00647     #endif
00648 
00649     // Figure out what is at *p, and parse it. Returns null if it is not an xml node.
00650     XmlNode* Identify( const char* start, XmlEncoding encoding );
00651 
00652     // Internal Value function returning a LDKXML_STRING
00653     const LDKXML_STRING& SValue() const  { return value ; }
00654 
00655     XmlNode*      parent;
00656     NodeType        type;
00657 
00658     XmlNode*      firstChild;
00659     XmlNode*      lastChild;
00660 
00661     LDKXML_STRING    value;
00662 
00663     XmlNode*      prev;
00664     XmlNode*      next;
00665 
00666 private:
00667     XmlNode( const XmlNode& );              // not implemented.
00668     void operator=( const XmlNode& base );    // not allowed.
00669 };
00670 
00671 
00679 class LDK_API XmlAttribute : public XmlBase
00680 {
00681     friend class LDK_API XmlAttributeSet;
00682 
00683 public:
00685     XmlAttribute() : XmlBase()
00686     {
00687         document = 0;
00688         prev = next = 0;
00689     }
00690 
00692     XmlAttribute( const String& _name, const String& _value )
00693     {
00694         name = _name;
00695         value = _value;
00696         document = 0;
00697         prev = next = 0;
00698     }
00699 
00701     XmlAttribute( const char * _name, const char * _value )
00702     {
00703         name = _name;
00704         value = _value;
00705         document = 0;
00706         prev = next = 0;
00707     }
00708 
00709     const char*     Name()  const       { return name.c_str (); }       
00710     const char*     Value() const       { return value.c_str (); }      
00711     const int       IntValue() const;                                   
00712     const double    DoubleValue() const;                                
00713 
00723     int QueryIntValue( int* value ) const;
00725     int QueryDoubleValue( double* value ) const;
00726 
00727     void SetName( const char* _name )   { name = _name; }               
00728     void SetValue( const char* _value ) { value = _value; }             
00729 
00730     void SetIntValue( int value );                                      
00731     void SetDoubleValue( double value );                                
00732 
00733 
00735     void SetName( const String& _name )
00736     {
00737         name = _name;
00738     }
00740     void SetValue( const String& _value )
00741     {
00742         value = _value;
00743     }
00744 
00745 
00747     const XmlAttribute* Next() const;
00748     XmlAttribute* Next();
00750     const XmlAttribute* Previous() const;
00751     XmlAttribute* Previous();
00752 
00753     bool operator==( const XmlAttribute& rhs ) const { return rhs.name == name; }
00754     bool operator<( const XmlAttribute& rhs )  const { return name < rhs.name; }
00755     bool operator>( const XmlAttribute& rhs )  const { return name > rhs.name; }
00756 
00757     /*  Attribute parsing starts: first letter of the name
00758                          returns: the next char after the value end quote
00759     */
00760     virtual const char* Parse( const char* p, XmlParsingData* data, XmlEncoding encoding );
00761 
00762     // Prints this Attribute to a FILE stream.
00763     virtual void Print( SmartFile f, int depth ) const;
00764 
00765     virtual void StreamOut( LDKXML_OSTREAM * out ) const;
00766     // [internal use]
00767     // Set the document pointer so the attribute can report errors.
00768     void SetDocument( XmlDocument* doc )  { document = doc; }
00769 
00770 private:
00771     XmlAttribute( const XmlAttribute& );                // not implemented.
00772     void operator=( const XmlAttribute& base );   // not allowed.
00773 
00774     XmlDocument*  document;   // A pointer back to a document, for error reporting.
00775     LDKXML_STRING name;
00776     LDKXML_STRING value;
00777     XmlAttribute* prev;
00778     XmlAttribute* next;
00779 };
00780 
00781 
00782 /*  A class used to manage a group of attributes.
00783     It is only used internally, both by the ELEMENT and the DECLARATION.
00784 
00785     The set can be changed transparent to the Element and Declaration
00786     classes that use it, but NOT transparent to the Attribute
00787     which has to implement a next() and previous() method. Which makes
00788     it a bit problematic and prevents the use of STL.
00789 
00790     This version is implemented with circular lists because:
00791         - I like circular lists
00792         - it demonstrates some independence from the (typical) doubly linked list.
00793 */
00794 class LDK_API XmlAttributeSet
00795 {
00796 public:
00797     XmlAttributeSet();
00798     ~XmlAttributeSet();
00799 
00800     void Add( XmlAttribute* attribute );
00801     void Remove( XmlAttribute* attribute );
00802 
00803     const XmlAttribute* First()   const   { return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
00804     XmlAttribute* First()                 { return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
00805     const XmlAttribute* Last() const      { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
00806     XmlAttribute* Last()                  { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
00807 
00808     const XmlAttribute*   Find( const char * name ) const;
00809     XmlAttribute* Find( const char * name );
00810 
00811 private:
00812     //*ME:  Because of hidden/disabled copy-construktor in XmlAttribute (sentinel-element),
00813     //*ME:  this class must be also use a hidden/disabled copy-constructor !!!
00814     XmlAttributeSet( const XmlAttributeSet& );  // not allowed
00815     void operator=( const XmlAttributeSet& ); // not allowed (as XmlAttribute)
00816 
00817     XmlAttribute sentinel;
00818 };
00819 
00820 
00825 class LDK_API XmlElement : public XmlNode
00826 {
00827 public:
00829     XmlElement (const char * in_value);
00830 
00831     //#ifdef LDKXML_USE_STL
00833     XmlElement( const String& _value );
00834     //#endif
00835 
00836     XmlElement( const XmlElement& );
00837 
00838     void operator=( const XmlElement& base );
00839 
00840     virtual ~XmlElement();
00841 
00845     const char* Attribute( const char* name ) const;
00846 
00853     const char* Attribute( const char* name, int* i ) const;
00854 
00861     const char* Attribute( const char* name, double* d ) const;
00862 
00870     int QueryIntAttribute( const char* name, int* value ) const;
00872     int QueryDoubleAttribute( const char* name, double* value ) const;
00874     int QueryDoubleAttribute( const char* name, float* value ) const {
00875         double d;
00876         int result = QueryDoubleAttribute( name, &d );
00877         *value = (float)d;
00878         return result;
00879     }
00880 
00884     void SetAttribute( const char* name, const char * value );
00885 
00886 
00887     const char* Attribute( const String& name ) const              { return Attribute( name.c_str() ); }
00888     const char* Attribute( const String& name, int* i ) const      { return Attribute( name.c_str(), i ); }
00889     const char* Attribute( const String& name, double* d ) const   { return Attribute( name.c_str(), d ); }
00890     int QueryIntAttribute( const String& name, int* value ) const  { return QueryIntAttribute( name.c_str(), value ); }
00891     int QueryDoubleAttribute( const String& name, double* value ) const { return QueryDoubleAttribute( name.c_str(), value ); }
00892 
00894     void SetAttribute( const String& _name, const String& _value )
00895     {
00896         SetAttribute (_name.c_str(), _value.c_str());
00897     }
00899     void SetAttribute( const String& _name, int _value )
00900     {
00901             SetAttribute (_name.c_str(), _value);
00902     }
00903 
00904 
00908     void SetAttribute( const char * name, int value );
00909 
00913     void SetDoubleAttribute( const char * name, double value );
00914 
00917     void RemoveAttribute( const char * name );
00918 
00919     void RemoveAttribute( const String& name ) {   RemoveAttribute (name.c_str ());    }   
00920 
00921 
00922     const XmlAttribute* FirstAttribute() const    { return attributeSet.First(); }        
00923     XmlAttribute* FirstAttribute()                { return attributeSet.First(); }
00924     const XmlAttribute* LastAttribute()   const   { return attributeSet.Last(); }     
00925     XmlAttribute* LastAttribute()                 { return attributeSet.Last(); }
00926 
00928     virtual XmlNode* Clone() const;
00929     // Print the Element to a FILE stream.
00930     virtual void Print( SmartFile f, int depth ) const;
00931 
00932     /*  Attribtue parsing starts: next char past '<'
00933                          returns: next char past '>'
00934     */
00935     virtual const char* Parse( const char* p, XmlParsingData* data, XmlEncoding encoding );
00936 
00937 protected:
00938 
00939     void CopyTo( XmlElement* target ) const;
00940     void ClearThis();   // like clear, but initializes 'this' object as well
00941 
00942     // Used to be public [internal use]
00943     #ifdef LDKXML_USE_STL
00944         virtual void StreamIn( LDKXML_ISTREAM * in, LDKXML_STRING * tag );
00945     #endif
00946     virtual void StreamOut( LDKXML_OSTREAM * out ) const;
00947 
00948     /*  [internal use]
00949         Reads the "value" of the element -- another element, or text.
00950         This should terminate with the current end tag.
00951     */
00952     const char* ReadValue( const char* in, XmlParsingData* prevData, XmlEncoding encoding );
00953 
00954 private:
00955 
00956     XmlAttributeSet attributeSet;
00957 };
00958 
00959 
00962 class LDK_API XmlComment : public XmlNode
00963 {
00964 public:
00966     XmlComment() : XmlNode( XmlNode::COMMENT ) {}
00967     XmlComment( const XmlComment& );
00968     void operator=( const XmlComment& base );
00969 
00970     virtual ~XmlComment() {}
00971 
00973     virtual XmlNode* Clone() const;
00975     virtual void Print( SmartFile f, int depth ) const;
00976 
00977     /*  Attribtue parsing starts: at the ! of the !--
00978                          returns: next char past '>'
00979     */
00980     virtual const char* Parse( const char* p, XmlParsingData* data, XmlEncoding encoding );
00981 
00982 protected:
00983     void CopyTo( XmlComment* target ) const;
00984 
00985     // used to be public
00986     #ifdef LDKXML_USE_STL
00987         virtual void StreamIn( LDKXML_ISTREAM * in, LDKXML_STRING * tag );
00988     #endif
00989     virtual void StreamOut( LDKXML_OSTREAM * out ) const;
00990 
00991 private:
00992 
00993 };
00994 
00995 
00998 class LDK_API XmlText : public XmlNode
00999 {
01000     friend class XmlElement;
01001 public:
01003     XmlText (const char * initValue) : XmlNode (XmlNode::TEXT)
01004     {
01005         SetValue( initValue );
01006     }
01007     virtual ~XmlText() {}
01008 
01009 
01011     XmlText( const String& initValue ) : XmlNode (XmlNode::TEXT)
01012     {
01013         SetValue( initValue.c_str() );
01014     }
01015 
01016 
01017     XmlText( const XmlText& copy ) : XmlNode( XmlNode::TEXT )   { copy.CopyTo( this ); }
01018     void operator=( const XmlText& base )                             { base.CopyTo( this ); }
01019 
01021     virtual void Print( SmartFile f, int depth ) const;
01022 
01023     virtual const char* Parse( const char* p, XmlParsingData* data, XmlEncoding encoding );
01024 
01025 protected :
01027     virtual XmlNode* Clone() const;
01028     void CopyTo( XmlText* target ) const;
01029 
01030     virtual void StreamOut ( LDKXML_OSTREAM * out ) const;
01031     bool Blank() const; // returns true if all white space and new lines
01032     // [internal use]
01033     #ifdef LDKXML_USE_STL
01034         virtual void StreamIn( LDKXML_ISTREAM * in, LDKXML_STRING * tag );
01035     #endif
01036 
01037 private:
01038 };
01039 
01040 
01054 class LDK_API XmlDeclaration : public XmlNode
01055 {
01056 public:
01058     XmlDeclaration()   : XmlNode( XmlNode::DECLARATION ) {}
01059 
01060 
01062     XmlDeclaration(   const String& _version,
01063                         const String& _encoding,
01064                         const String& _standalone );
01065 
01066 
01068     XmlDeclaration(   const char* _version,
01069                         const char* _encoding,
01070                         const char* _standalone );
01071 
01072     XmlDeclaration( const XmlDeclaration& copy );
01073     void operator=( const XmlDeclaration& copy );
01074 
01075     virtual ~XmlDeclaration() {}
01076 
01078     const char *Version() const         { return version.c_str (); }
01080     const char *Encoding() const        { return encoding.c_str (); }
01082     const char *Standalone() const      { return standalone.c_str (); }
01083 
01085     virtual XmlNode* Clone() const;
01087     virtual void Print( SmartFile f, int depth ) const;
01088 
01089     virtual const char* Parse( const char* p, XmlParsingData* data, XmlEncoding encoding );
01090 
01091 protected:
01092     void CopyTo( XmlDeclaration* target ) const;
01093     // used to be public
01094     #ifdef LDKXML_USE_STL
01095         virtual void StreamIn( LDKXML_ISTREAM * in, LDKXML_STRING * tag );
01096     #endif
01097     virtual void StreamOut ( LDKXML_OSTREAM * out) const;
01098 
01099 private:
01100 
01101     LDKXML_STRING version;
01102     LDKXML_STRING encoding;
01103     LDKXML_STRING standalone;
01104 };
01105 
01106 
01114 class LDK_API XmlUnknown : public XmlNode
01115 {
01116 public:
01117     XmlUnknown() : XmlNode( XmlNode::UNKNOWN )    {}
01118     virtual ~XmlUnknown() {}
01119 
01120     XmlUnknown( const XmlUnknown& copy ) : XmlNode( XmlNode::UNKNOWN )      { copy.CopyTo( this ); }
01121     void operator=( const XmlUnknown& copy )                                      { copy.CopyTo( this ); }
01122 
01124     virtual XmlNode* Clone() const;
01126     virtual void Print( SmartFile f, int depth ) const;
01127 
01128     virtual const char* Parse( const char* p, XmlParsingData* data, XmlEncoding encoding );
01129 
01130 protected:
01131     void CopyTo( XmlUnknown* target ) const;
01132 
01133     #ifdef LDKXML_USE_STL
01134         virtual void StreamIn( LDKXML_ISTREAM * in, LDKXML_STRING * tag );
01135     #endif
01136     virtual void StreamOut ( LDKXML_OSTREAM * out ) const;
01137 
01138 private:
01139 
01140 };
01141 
01142 
01147 class LDK_API XmlDocument : public XmlNode
01148 {
01149 public:
01151     XmlDocument();
01153     XmlDocument( const char * documentName );
01154 
01155 
01157     XmlDocument( const String& documentName );
01158 
01159 
01160     XmlDocument( const XmlDocument& copy );
01161     void operator=( const XmlDocument& copy );
01162 
01163     virtual ~XmlDocument() {}
01164 
01169     bool LoadFile( XmlEncoding encoding = LDKXML_DEFAULT_ENCODING );
01171     bool SaveFile() const;
01173     bool LoadFile( const char * filename, XmlEncoding encoding = LDKXML_DEFAULT_ENCODING );
01175     bool SaveFile( const char * filename ) const;
01176 
01178     bool LoadFile( const String& filename, XmlEncoding encoding = LDKXML_DEFAULT_ENCODING )
01179     {
01180 
01181         return ( LoadFile( filename.c_str(), encoding ));
01182     }
01183     bool SaveFile( const String& filename ) const      
01184     {
01185 
01186         return ( SaveFile( filename.c_str() ));
01187     }
01188 
01189 
01194     virtual const char* Parse( const char* p, XmlParsingData* data = 0, XmlEncoding encoding = LDKXML_DEFAULT_ENCODING );
01195 
01200     const XmlElement* RootElement() const     { return FirstChildElement(); }
01201     XmlElement* RootElement()                 { return FirstChildElement(); }
01202 
01208     bool Error() const                      { return error; }
01209 
01211     const char * ErrorDesc() const  { return errorDesc.c_str (); }
01212 
01216     const int ErrorId() const               { return errorId; }
01217 
01225     int ErrorRow()  { return errorLocation.row+1; }
01226     int ErrorCol()  { return errorLocation.col+1; } 
01227 
01248     void SetTabSize( int _tabsize )     { tabsize = _tabsize; }
01249 
01250     int TabSize() const { return tabsize; }
01251 
01255     void ClearError()                       {   error = false;
01256                                                 errorId = 0;
01257                                                 errorDesc = "";
01258                                                 errorLocation.row = errorLocation.col = 0;
01259                                                 //errorLocation.last = 0;
01260                                             }
01261 
01263     void Print() const                      { Print( VFileSystem::stdOut(), 0 ); }
01264 
01266     virtual void Print( SmartFile f, int depth = 0 ) const;
01267     // [internal use]
01268     void SetError( int err, const char* errorLocation, XmlParsingData* prevData, XmlEncoding encoding );
01269 
01270 protected :
01271     virtual void StreamOut ( LDKXML_OSTREAM * out) const;
01272     // [internal use]
01273     virtual XmlNode* Clone() const;
01274     #ifdef LDKXML_USE_STL
01275         virtual void StreamIn( LDKXML_ISTREAM * in, LDKXML_STRING * tag );
01276     #endif
01277 
01278 private:
01279     void CopyTo( XmlDocument* target ) const;
01280 
01281     bool error;
01282     int  errorId;
01283     LDKXML_STRING errorDesc;
01284     int tabsize;
01285     XmlCursor errorLocation;
01286 };
01287 
01288 
01369 class LDK_API XmlHandle
01370 {
01371 public:
01373     XmlHandle( XmlNode* node )                  { this->node = node; }
01375     XmlHandle( const XmlHandle& ref )           { this->node = ref.node; }
01376     XmlHandle operator=( const XmlHandle& ref ) { this->node = ref.node; return *this; }
01377 
01379     XmlHandle FirstChild() const;
01381     XmlHandle FirstChild( const char * value ) const;
01383     XmlHandle FirstChildElement() const;
01385     XmlHandle FirstChildElement( const char * value ) const;
01386 
01390     XmlHandle Child( const char* value, int index ) const;
01394     XmlHandle Child( int index ) const;
01399     XmlHandle ChildElement( const char* value, int index ) const;
01404     XmlHandle ChildElement( int index ) const;
01405 
01406 
01407     XmlHandle FirstChild( const String& _value ) const               { return FirstChild( _value.c_str() ); }
01408     XmlHandle FirstChildElement( const String& _value ) const        { return FirstChildElement( _value.c_str() ); }
01409 
01410     XmlHandle Child( const String& _value, int index ) const         { return Child( _value.c_str(), index ); }
01411     XmlHandle ChildElement( const String& _value, int index ) const  { return ChildElement( _value.c_str(), index ); }
01412 
01413 
01415     XmlNode* Node() const         { return node; }
01417     XmlElement* Element() const   { return ( ( node && node->ToElement() ) ? node->ToElement() : 0 ); }
01419     XmlText* Text() const         { return ( ( node && node->ToText() ) ? node->ToText() : 0 ); }
01421     XmlUnknown* Unknown() const           { return ( ( node && node->ToUnknown() ) ? node->ToUnknown() : 0 ); }
01422 
01423 private:
01424     XmlNode* node;
01425 };
01426 
01427 }//namespace LDK
01428 
01429 #ifdef _MSC_VER
01430 #pragma warning( default : 4530 )
01431 #pragma warning( default : 4786 )
01432 #endif
01433 
01434 #endif
01435 

Generated on Fri Aug 17 18:32:26 2007 for LDK by  doxygen 1.5.1