GDBMS 1.0
GDBMS_for_documentation/xmlParser.h
Go to the documentation of this file.
00001 /****************************************************************************/
00023 #ifndef __INCLUDE_XML_NODE__
00024 #define __INCLUDE_XML_NODE__
00025 
00026 #include <stdlib.h>
00027 
00028 #ifdef _UNICODE
00029 // If you comment the next "define" line then the library will never "switch to" _UNICODE (wchar_t*) mode (16/32 bits per characters).
00030 // This is useful when you get error messages like:
00031 //    'XMLNode::openFileHelper' : cannot convert parameter 2 from 'const char [5]' to 'const wchar_t *'
00032 // The _XMLWIDECHAR preprocessor variable force the XMLParser library into either utf16/32-mode (the proprocessor variable
00033 // must be defined) or utf8-mode(the pre-processor variable must be undefined).
00034 #define _XMLWIDECHAR
00035 #endif
00036 
00037 #if defined(WIN32) || defined(UNDER_CE) || defined(_WIN32) || defined(WIN64) || defined(__BORLANDC__)
00038 // comment the next line if you are under windows and the compiler is not Microsoft Visual Studio (6.0 or .NET) or Borland
00039 #define _XMLWINDOWS
00040 #endif
00041 
00042 #ifdef XMLDLLENTRY
00043 #undef XMLDLLENTRY
00044 #endif
00045 #ifdef _USE_XMLPARSER_DLL
00046 #ifdef _DLL_EXPORTS_
00047 #define XMLDLLENTRY __declspec(dllexport)
00048 #else
00049 #define XMLDLLENTRY __declspec(dllimport)
00050 #endif
00051 #else
00052 #define XMLDLLENTRY
00053 #endif
00054 
00055 // uncomment the next line if you want no support for wchar_t* (no need for the <wchar.h> or <tchar.h> libraries anymore to compile)
00056 //#define XML_NO_WIDE_CHAR
00057 
00058 #ifdef XML_NO_WIDE_CHAR
00059 #undef _XMLWINDOWS
00060 #undef _XMLWIDECHAR
00061 #endif
00062 
00063 #ifdef _XMLWINDOWS
00064 #include <tchar.h>
00065 #else
00066 #define XMLDLLENTRY
00067 #ifndef XML_NO_WIDE_CHAR
00068 #include <wchar.h> // to have 'wcsrtombs' for ANSI version
00069                    // to have 'mbsrtowcs' for WIDECHAR version
00070 #endif
00071 #endif
00072 
00073 // Some common types for char set portable code
00074 #ifdef _XMLWIDECHAR
00075     #define _CXML(c) L ## c
00076     #define XMLCSTR const wchar_t *
00077     #define XMLSTR  wchar_t *
00078     #define XMLCHAR wchar_t
00079 #else
00080     #define _CXML(c) c
00081     #define XMLCSTR const char *
00082     #define XMLSTR  char *
00083     #define XMLCHAR char
00084 #endif
00085 #ifndef FALSE
00086     #define FALSE 0
00087 #endif /* FALSE */
00088 #ifndef TRUE
00089     #define TRUE 1
00090 #endif /* TRUE */
00091 
00092 
00094 typedef enum XMLError
00095 {
00096     eXMLErrorNone = 0,
00097     eXMLErrorMissingEndTag,
00098     eXMLErrorNoXMLTagFound,
00099     eXMLErrorEmpty,
00100     eXMLErrorMissingTagName,
00101     eXMLErrorMissingEndTagName,
00102     eXMLErrorUnmatchedEndTag,
00103     eXMLErrorUnmatchedEndClearTag,
00104     eXMLErrorUnexpectedToken,
00105     eXMLErrorNoElements,
00106     eXMLErrorFileNotFound,
00107     eXMLErrorFirstTagNotFound,
00108     eXMLErrorUnknownCharacterEntity,
00109     eXMLErrorCharacterCodeAbove255,
00110     eXMLErrorCharConversionError,
00111     eXMLErrorCannotOpenWriteFile,
00112     eXMLErrorCannotWriteFile,
00113 
00114     eXMLErrorBase64DataSizeIsNotMultipleOf4,
00115     eXMLErrorBase64DecodeIllegalCharacter,
00116     eXMLErrorBase64DecodeTruncatedData,
00117     eXMLErrorBase64DecodeBufferTooSmall
00118 } XMLError;
00119 
00120 
00122 typedef enum XMLElementType
00123 {
00124     eNodeChild=0,
00125     eNodeAttribute=1,
00126     eNodeText=2,
00127     eNodeClear=3,
00128     eNodeNULL=4
00129 } XMLElementType;
00130 
00132 typedef struct XMLResults
00133 {
00134     enum XMLError error;
00135     int  nLine,nColumn;
00136 } XMLResults;
00137 
00139 typedef struct XMLClear {
00140     XMLCSTR lpszValue; XMLCSTR lpszOpenTag; XMLCSTR lpszCloseTag;
00141 } XMLClear;
00142 
00144 typedef struct XMLAttribute {
00145     XMLCSTR lpszName; XMLCSTR lpszValue;
00146 } XMLAttribute;
00147 
00149 typedef int XMLElementPosition;
00150 
00151 struct XMLNodeContents;
00152 
00155 
00156 
00165 typedef struct XMLDLLENTRY XMLNode
00166 {
00167   private:
00168 
00169     struct XMLNodeDataTag;
00170 
00172     XMLNode(struct XMLNodeDataTag *pParent, XMLSTR lpszName, char isDeclaration);
00174     XMLNode(struct XMLNodeDataTag *p);
00175 
00176   public:
00177     static XMLCSTR getVersion();
00178 
00183 
00184     static XMLNode parseString   (XMLCSTR  lpXMLString, XMLCSTR tag=NULL, XMLResults *pResults=NULL);
00196 
00197     static XMLNode parseFile     (XMLCSTR     filename, XMLCSTR tag=NULL, XMLResults *pResults=NULL);
00209 
00210     static XMLNode openFileHelper(XMLCSTR     filename, XMLCSTR tag=NULL);
00228     static XMLCSTR getError(XMLError error); 
00229 
00231     XMLSTR createXMLString(int nFormat=1, int *pnSize=NULL) const;
00237 
00238     XMLError writeToFile(XMLCSTR filename,
00239                          const char *encoding=NULL,
00240                          char nFormat=1) const;
00251     XMLCSTR getName() const;                                       
00252     XMLCSTR getText(int i=0) const;                                
00253     int nText() const;                                             
00254     XMLNode getParentNode() const;                                 
00255     XMLNode getChildNode(int i=0) const;                           
00256     XMLNode getChildNode(XMLCSTR name, int i)  const;              
00257     XMLNode getChildNode(XMLCSTR name, int *i=NULL) const;         
00258     XMLNode getChildNodeWithAttribute(XMLCSTR tagName,
00259                                       XMLCSTR attributeName,
00260                                       XMLCSTR attributeValue=NULL,
00261                                       int *i=NULL)  const;         
00262     XMLNode getChildNodeByPath(XMLCSTR path, char createNodeIfMissing=0, XMLCHAR sep='/');
00264     XMLNode getChildNodeByPathNonConst(XMLSTR  path, char createNodeIfMissing=0, XMLCHAR sep='/');
00266 
00267     int nChildNode(XMLCSTR name) const;                            
00268     int nChildNode() const;                                        
00269     XMLAttribute getAttribute(int i=0) const;                      
00270     XMLCSTR      getAttributeName(int i=0) const;                  
00271     XMLCSTR      getAttributeValue(int i=0) const;                 
00272     char  isAttributeSet(XMLCSTR name) const;                      
00273     XMLCSTR getAttribute(XMLCSTR name, int i) const;               
00274     XMLCSTR getAttribute(XMLCSTR name, int *i=NULL) const;         
00275     int nAttribute() const;                                        
00276     XMLClear getClear(int i=0) const;                              
00277     int nClear() const;                                            
00278     XMLNodeContents enumContents(XMLElementPosition i) const;      
00279     int nElement() const;                                          
00280     char isEmpty() const;                                          
00281     char isDeclaration() const;                                    
00282     XMLNode deepCopy() const;                                      
00283     static XMLNode emptyNode();                                    
00284 
00286     ~XMLNode();
00287     XMLNode(const XMLNode &A);                                     
00288     XMLNode& operator=( const XMLNode& A );                        
00289 
00290     XMLNode(): d(NULL){};
00291     static XMLNode emptyXMLNode;
00292     static XMLClear emptyXMLClear;
00293     static XMLAttribute emptyXMLAttribute;
00294 
00308     static XMLNode createXMLTopNode(XMLCSTR lpszName, char isDeclaration=FALSE);                    
00309     XMLNode        addChild(XMLCSTR lpszName, char isDeclaration=FALSE, XMLElementPosition pos=-1); 
00310     XMLNode        addChild(XMLNode nodeToAdd, XMLElementPosition pos=-1);                          
00311     XMLAttribute  *addAttribute(XMLCSTR lpszName, XMLCSTR lpszValuev);                              
00312     XMLCSTR        addText(XMLCSTR lpszValue, XMLElementPosition pos=-1);                           
00313     XMLClear      *addClear(XMLCSTR lpszValue, XMLCSTR lpszOpen=NULL, XMLCSTR lpszClose=NULL, XMLElementPosition pos=-1);
00325     XMLCSTR       updateName(XMLCSTR lpszName);                                                  
00326     XMLAttribute *updateAttribute(XMLAttribute *newAttribute, XMLAttribute *oldAttribute);       
00327     XMLAttribute *updateAttribute(XMLCSTR lpszNewValue, XMLCSTR lpszNewName=NULL,int i=0);       
00328     XMLAttribute *updateAttribute(XMLCSTR lpszNewValue, XMLCSTR lpszNewName,XMLCSTR lpszOldName);
00329     XMLCSTR       updateText(XMLCSTR lpszNewValue, int i=0);                                     
00330     XMLCSTR       updateText(XMLCSTR lpszNewValue, XMLCSTR lpszOldValue);                        
00331     XMLClear     *updateClear(XMLCSTR lpszNewContent, int i=0);                                  
00332     XMLClear     *updateClear(XMLClear *newP,XMLClear *oldP);                                    
00333     XMLClear     *updateClear(XMLCSTR lpszNewValue, XMLCSTR lpszOldValue);                       
00334 
00341 
00342     void deleteNodeContent();
00344     void deleteAttribute(int i=0);                   
00345     void deleteAttribute(XMLCSTR lpszName);          
00346     void deleteAttribute(XMLAttribute *anAttribute); 
00347     void deleteText(int i=0);                        
00348     void deleteText(XMLCSTR lpszValue);              
00349     void deleteClear(int i=0);                       
00350     void deleteClear(XMLCSTR lpszValue);             
00351     void deleteClear(XMLClear *p);                   
00352 
00383     static XMLNode createXMLTopNode_WOSD(XMLSTR lpszName, char isDeclaration=FALSE);                     
00384     XMLNode        addChild_WOSD(XMLSTR lpszName, char isDeclaration=FALSE, XMLElementPosition pos=-1);  
00385     XMLAttribute  *addAttribute_WOSD(XMLSTR lpszName, XMLSTR lpszValue);                                 
00386     XMLCSTR        addText_WOSD(XMLSTR lpszValue, XMLElementPosition pos=-1);                            
00387     XMLClear      *addClear_WOSD(XMLSTR lpszValue, XMLCSTR lpszOpen=NULL, XMLCSTR lpszClose=NULL, XMLElementPosition pos=-1); 
00388 
00389     XMLCSTR        updateName_WOSD(XMLSTR lpszName);                                                  
00390     XMLAttribute  *updateAttribute_WOSD(XMLAttribute *newAttribute, XMLAttribute *oldAttribute);      
00391     XMLAttribute  *updateAttribute_WOSD(XMLSTR lpszNewValue, XMLSTR lpszNewName=NULL,int i=0);        
00392     XMLAttribute  *updateAttribute_WOSD(XMLSTR lpszNewValue, XMLSTR lpszNewName,XMLCSTR lpszOldName); 
00393     XMLCSTR        updateText_WOSD(XMLSTR lpszNewValue, int i=0);                                     
00394     XMLCSTR        updateText_WOSD(XMLSTR lpszNewValue, XMLCSTR lpszOldValue);                        
00395     XMLClear      *updateClear_WOSD(XMLSTR lpszNewContent, int i=0);                                  
00396     XMLClear      *updateClear_WOSD(XMLClear *newP,XMLClear *oldP);                                   
00397     XMLClear      *updateClear_WOSD(XMLSTR lpszNewValue, XMLCSTR lpszOldValue);                       
00398 
00406     XMLElementPosition positionOfText(int i=0) const;
00407     XMLElementPosition positionOfText(XMLCSTR lpszValue) const;
00408     XMLElementPosition positionOfClear(int i=0) const;
00409     XMLElementPosition positionOfClear(XMLCSTR lpszValue) const;
00410     XMLElementPosition positionOfClear(XMLClear *a) const;
00411     XMLElementPosition positionOfChildNode(int i=0) const;
00412     XMLElementPosition positionOfChildNode(XMLNode x) const;
00413     XMLElementPosition positionOfChildNode(XMLCSTR name, int i=0) const; 
00414 
00416 
00417     typedef enum XMLCharEncoding
00418     {
00419         char_encoding_error=0,
00420         char_encoding_UTF8=1,
00421         char_encoding_legacy=2,
00422         char_encoding_ShiftJIS=3,
00423         char_encoding_GB2312=4,
00424         char_encoding_Big5=5,
00425         char_encoding_GBK=6     // this is actually the same as Big5
00426     } XMLCharEncoding;
00427 
00431 
00432     static char setGlobalOptions(XMLCharEncoding characterEncoding=XMLNode::char_encoding_UTF8, char guessWideCharChars=1,
00433                                  char dropWhiteSpace=1, char removeCommentsInMiddleOfText=1);
00484 
00485     static XMLCharEncoding guessCharEncoding(void *buffer, int bufLen, char useXMLEncodingAttribute=1);
00498   private:
00499       // these are functions and structures used internally by the XMLNode class (don't bother about them):
00500 
00501       typedef struct XMLNodeDataTag // to allow shallow copy and "intelligent/smart" pointers (automatic delete):
00502       {
00503           XMLCSTR                lpszName;        // Element name (=NULL if root)
00504           int                    nChild,          // Number of child nodes
00505                                  nText,           // Number of text fields
00506                                  nClear,          // Number of Clear fields (comments)
00507                                  nAttribute;      // Number of attributes
00508           char                   isDeclaration;   // Whether node is an XML declaration - '<?xml ?>'
00509           struct XMLNodeDataTag  *pParent;        // Pointer to parent element (=NULL if root)
00510           XMLNode                *pChild;         // Array of child nodes
00511           XMLCSTR                *pText;          // Array of text fields
00512           XMLClear               *pClear;         // Array of clear fields
00513           XMLAttribute           *pAttribute;     // Array of attributes
00514           int                    *pOrder;         // order of the child_nodes,text_fields,clear_fields
00515           int                    ref_count;       // for garbage collection (smart pointers)
00516       } XMLNodeData;
00517       XMLNodeData *d;
00518 
00519       char parseClearTag(void *px, void *pa);
00520       char maybeAddTxT(void *pa, XMLCSTR tokenPStr);
00521       int ParseXMLElement(void *pXML);
00522       void *addToOrder(int memInc, int *_pos, int nc, void *p, int size, XMLElementType xtype);
00523       int indexText(XMLCSTR lpszValue) const;
00524       int indexClear(XMLCSTR lpszValue) const;
00525       XMLNode addChild_priv(int,XMLSTR,char,int);
00526       XMLAttribute *addAttribute_priv(int,XMLSTR,XMLSTR);
00527       XMLCSTR addText_priv(int,XMLSTR,int);
00528       XMLClear *addClear_priv(int,XMLSTR,XMLCSTR,XMLCSTR,int);
00529       void emptyTheNode(char force);
00530       static inline XMLElementPosition findPosition(XMLNodeData *d, int index, XMLElementType xtype);
00531       static int CreateXMLStringR(XMLNodeData *pEntry, XMLSTR lpszMarker, int nFormat);
00532       static int removeOrderElement(XMLNodeData *d, XMLElementType t, int index);
00533       static void exactMemory(XMLNodeData *d);
00534       static int detachFromParent(XMLNodeData *d);
00535 } XMLNode;
00536 
00538 typedef struct XMLNodeContents
00539 {
00541     enum XMLElementType etype;
00543     XMLNode child;
00544     XMLAttribute attrib;
00545     XMLCSTR text;
00546     XMLClear clear;
00547 
00548 } XMLNodeContents;
00549 
00553 
00554 XMLDLLENTRY XMLSTR stringDup(XMLCSTR source, int cbData=-1);
00560 
00561 XMLDLLENTRY void freeXMLString(XMLSTR t); // {free(t);}
00573 XMLDLLENTRY char    xmltob(XMLCSTR xmlString,char   defautValue=0);
00574 XMLDLLENTRY int     xmltoi(XMLCSTR xmlString,int    defautValue=0);
00575 XMLDLLENTRY long    xmltol(XMLCSTR xmlString,long   defautValue=0);
00576 XMLDLLENTRY double  xmltof(XMLCSTR xmlString,double defautValue=.0);
00577 XMLDLLENTRY XMLCSTR xmltoa(XMLCSTR xmlString,XMLCSTR defautValue=_CXML(""));
00578 XMLDLLENTRY XMLCHAR xmltoc(XMLCSTR xmlString,const XMLCHAR defautValue=_CXML('\0'));
00584 
00585 
00594 typedef struct XMLDLLENTRY ToXMLStringTool
00595 {
00596 public:
00597     ToXMLStringTool(): buf(NULL),buflen(0){}
00598     ~ToXMLStringTool();
00599     void freeBuffer();
00600 
00601     XMLSTR toXML(XMLCSTR source);
00602 
00606     static XMLSTR toXMLUnSafe(XMLSTR dest,XMLCSTR source); 
00607     static int lengthXMLString(XMLCSTR source);            
00608 
00609 private:
00610     XMLSTR buf;
00611     int buflen;
00612 } ToXMLStringTool;
00618 
00619 
00627 typedef struct XMLDLLENTRY XMLParserBase64Tool
00628 {
00629 public:
00630     XMLParserBase64Tool(): buf(NULL),buflen(0){}
00631     ~XMLParserBase64Tool();
00632     void freeBuffer();
00633 
00636     static int encodeLength(int inBufLen, char formatted=0); 
00637 
00643     XMLSTR encode(unsigned char *inByteBuf, unsigned int inByteLen, char formatted=0); 
00644 
00646     static unsigned int decodeSize(XMLCSTR inString, XMLError *xe=NULL);
00647 
00653     unsigned char* decode(XMLCSTR inString, int *outByteLen=NULL, XMLError *xe=NULL); 
00654 
00659     static unsigned char decode(XMLCSTR inString, unsigned char *outByteBuf, int inMaxByteOutBuflen, XMLError *xe=NULL); 
00660 
00661 private:
00662     void *buf;
00663     int buflen;
00664     void alloc(int newsize);
00665 }XMLParserBase64Tool;
00668 #undef XMLDLLENTRY
00669 
00670 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines