PixelLightAPI  .
XmlDocument.h
Go to the documentation of this file.
00001 /*********************************************************\
00002  *  File: XmlDocument.h                                  *
00003  *
00004  *  Copyright (C) 2002-2012 The PixelLight Team (http://www.pixellight.org/)
00005  *
00006  *  This file is part of PixelLight.
00007  *
00008  *  PixelLight is free software: you can redistribute it and/or modify
00009  *  it under the terms of the GNU Lesser General Public License as published by
00010  *  the Free Software Foundation, either version 3 of the License, or
00011  *  (at your option) any later version.
00012  *
00013  *  PixelLight is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  *  GNU Lesser General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU Lesser General Public License
00019  *  along with PixelLight. If not, see <http://www.gnu.org/licenses/>.
00020 \*********************************************************/
00021 
00022 
00023 #ifndef __PLCORE_XML_DOCUMENT_H__
00024 #define __PLCORE_XML_DOCUMENT_H__
00025 #pragma once
00026 
00027 
00028 //[-------------------------------------------------------]
00029 //[ Includes                                              ]
00030 //[-------------------------------------------------------]
00031 #include "PLCore/Xml/XmlNode.h"
00032 
00033 
00034 //[-------------------------------------------------------]
00035 //[ Namespace                                             ]
00036 //[-------------------------------------------------------]
00037 namespace PLCore {
00038 
00039 
00040 //[-------------------------------------------------------]
00041 //[ Forward declarations                                  ]
00042 //[-------------------------------------------------------]
00043 class XmlParsingData;
00044 
00045 
00046 //[-------------------------------------------------------]
00047 //[ Classes                                               ]
00048 //[-------------------------------------------------------]
00049 /**
00050 *  @brief
00051 *    XML (Extensible Markup Language) document node
00052 *
00053 *  @remarks
00054 *    Always the top level node. A document binds together all the
00055 *    XML pieces. It can be saved, loaded, and printed to the screen.
00056 *    The 'value' of a document node is the XML filename.
00057 *
00058 *    The XML DOM parser is basing on TinyXML (http://www.sourceforge.net/projects/tinyxml).
00059 */
00060 class XmlDocument : public XmlNode {
00061 
00062 
00063     //[-------------------------------------------------------]
00064     //[ Friends                                               ]
00065     //[-------------------------------------------------------]
00066     friend class XmlText;
00067     friend class XmlNode;
00068     friend class XmlUnknown;
00069     friend class XmlElement;
00070     friend class XmlComment;
00071     friend class XmlAttribute;
00072     friend class XmlDeclaration;
00073 
00074 
00075     //[-------------------------------------------------------]
00076     //[ Public functions                                      ]
00077     //[-------------------------------------------------------]
00078     public:
00079         /**
00080         *  @brief
00081         *    Default constructor
00082         */
00083         PLCORE_API XmlDocument();
00084 
00085         /**
00086         *  @brief
00087         *    Constructor
00088         *
00089         *  @param[in] sName
00090         *    Document name. The name of the document is also the filename of the XML.
00091         */
00092         PLCORE_API XmlDocument(const String &sName);
00093 
00094         /**
00095         *  @brief
00096         *    Copy constructor
00097         *
00098         *  @param[in] cSource
00099         *    Source to copy from
00100         */
00101         PLCORE_API XmlDocument(const XmlDocument &cSource);
00102 
00103         /**
00104         *  @brief
00105         *    Destructor
00106         */
00107         PLCORE_API virtual ~XmlDocument();
00108 
00109         /**
00110         *  @brief
00111         *    Copy operator
00112         *
00113         *  @param[in] cSource
00114         *    Source to copy from
00115         *
00116         *  @return
00117         *    Reference to this instance
00118         */
00119         PLCORE_API XmlDocument &operator =(const XmlDocument &cSource);
00120 
00121         /**
00122         *  @brief
00123         *    Load a file using the current document value
00124         *
00125         *  @param[in] nEncoding
00126         *    Encoding
00127         *
00128         *  @return
00129         *    Returns 'true' if successful, else 'false'
00130         *
00131         *  @note
00132         *    - Will delete any existing document data before loading
00133         */
00134         inline bool Load(EEncoding nEncoding = EncodingUnknown);
00135 
00136         /**
00137         *  @brief
00138         *    Load a file using the given filename
00139         *
00140         *  @param[in] sFilename
00141         *    Filename
00142         *  @param[in] nEncoding
00143         *    Encoding
00144         *
00145         *  @return
00146         *    Returns 'true' if successful, else 'false'
00147         *
00148         *  @note
00149         *    - The document value is set to 'sFilename'
00150         */
00151         PLCORE_API bool Load(const String &sFilename, EEncoding nEncoding = EncodingUnknown);
00152 
00153         /**
00154         *  @brief
00155         *    Loads from a given file
00156         *
00157         *  @param[in] cFile
00158         *    File to read from, must be opened and readable
00159         *  @param[in] nEncoding
00160         *    Encoding
00161         *
00162         *  @return
00163         *    'true' if all went fine, else 'false'
00164         */
00165         PLCORE_API bool Load(File &cFile, EEncoding nEncoding = EncodingUnknown);
00166 
00167         /**
00168         *  @brief
00169         *    Save a file using the current document value
00170         *
00171         *  @return
00172         *    Returns 'true' if successful, else 'false'
00173         */
00174         inline bool Save();
00175 
00176         /**
00177         *  @brief
00178         *    Save a file using the given filename
00179         *
00180         *  @param[in] sFilename
00181         *    Filename
00182         *
00183         *  @return
00184         *    Returns 'true' if successful, else 'false'
00185         *
00186         *  @note
00187         *    - The document value is set to 'sFilename'
00188         */
00189         PLCORE_API bool Save(const String &sFilename);
00190 
00191         /**
00192         *  @brief
00193         *    Get the root element -- the only top level element -- of the document
00194         *
00195         *  @return
00196         *    The root element, a null pointer on error
00197         *
00198         *  @note
00199         *    - In well formed XML, there should only be one. This parser is tolerant of
00200         *      multiple elements at the document level
00201         */
00202         inline XmlElement *GetRootElement();
00203         inline const XmlElement *GetRootElement() const;
00204 
00205         /**
00206         *  @brief
00207         *    If an error occurs, error will be set to true
00208         *
00209         *  @return
00210         *    'true' if an error occurs, else 'false'
00211         *
00212         *  @note
00213         *    - 'GetErrorID()' will contain the integer identifier of the error (not generally useful)
00214         *    - 'GetErrorDesc()' will return the name of the error. (very useful)
00215         *    - 'GetErrorRow()' and 'GetErrorColumn()' will return the location of the error (if known)
00216         */
00217         inline bool Error() const;
00218 
00219         /**
00220         *  @brief
00221         *    Contains a textual (english) description of the error if one occurs
00222         *
00223         *  @param[in] bLocation
00224         *    Do also add the location (if known) of the error?
00225         *
00226         *  @return
00227         *    Error description
00228         */
00229         PLCORE_API String GetErrorDesc(bool bLocation = true) const;
00230 
00231         /**
00232         *  @brief
00233         *    Generally, you probably want the error string ('GetErrorDesc()') - but if you
00234         *    prefer the error ID, this function will fetch it
00235         *
00236         *  @return
00237         *    Error ID
00238         */
00239         inline int GetErrorID() const;
00240 
00241         /**
00242         *  @brief
00243         *    Returns the location (if known) of the error
00244         *
00245         *  @remarks
00246         *    The first column is column 1, and the first row is row 1. A value of 0 means
00247         *    the row and column wasn't applicable (memory errors, for example, have no
00248         *    row/column) or the parser lost the error. (An error in the error reporting,
00249         *    in that case.)
00250         *
00251         *  @return
00252         *    Row the error occurred
00253         *
00254         *  @see
00255         *    - SetTabSize(), GetRow() and GetColumn()
00256         */
00257         inline int GetErrorRow() const;
00258 
00259         /**
00260         *  @brief
00261         *    The column where the error occurred
00262         *
00263         *  @return
00264         *    Column the error occurred
00265         *
00266         *  @see
00267         *    - GetErrorRow()
00268         */
00269         inline int GetErrorColumn() const;
00270 
00271         /**
00272         *  @brief
00273         *    Returns the tab size
00274         *
00275         *  @return
00276         *    Tab size
00277         */
00278         inline uint32 GetTabSize() const;
00279 
00280         /**
00281         *  @brief
00282         *    Sets the tab size
00283         *
00284         *  @param[in] nTabSize
00285         *    New tab size
00286         *
00287         *  @remarks
00288         *    By calling this method, with a tab size greater than 0, the row and column
00289         *    of each node and attribute is stored when the file is loaded. Very useful
00290         *    for tracking the DOM back in to the source file.
00291         *    The tab size is required for calculating the location of nodes. If not
00292         *    set, the default of 4 is used. The tab size is set per document. Setting
00293         *    the tab size to 0 disables row/column tracking.
00294         *    Note that row and column tracking is not supported when using operator '>>'
00295         *    The tab size needs to be enabled before the parse or load. Correct usage:
00296         *    @verbatim
00297         *    XmlDocument cDocument;
00298         *    cDocument.SetTabSize(8);
00299         *    cDocument.Load("myfile.xml");
00300         *    @endverbatim
00301         *
00302         *    @see
00303         *      - GetRow() and GetColumn()
00304         */
00305         inline void SetTabSize(uint32 nTabSize = 4);
00306 
00307         /**
00308         *  @brief
00309         *    If you have handled the error, it can be reset with this call
00310         *
00311         *  @note
00312         *    - The error state is automatically cleared if you parse a new XML block
00313         */
00314         PLCORE_API void ClearError();
00315 
00316 
00317     //[-------------------------------------------------------]
00318     //[ Public virtual XmlBase functions                      ]
00319     //[-------------------------------------------------------]
00320     public:
00321         PLCORE_API virtual bool Save(File &cFile, uint32 nDepth = 0) override;
00322         PLCORE_API virtual String ToString(uint32 nDepth = 0) const override;
00323         PLCORE_API virtual const char *Parse(const char *pszData, XmlParsingData *pData = nullptr, EEncoding nEncoding = EncodingUnknown) override;
00324 
00325 
00326     //[-------------------------------------------------------]
00327     //[ Public virtual XmlNode functions                      ]
00328     //[-------------------------------------------------------]
00329     public:
00330         PLCORE_API virtual XmlNode *Clone() const override;
00331 
00332 
00333     //[-------------------------------------------------------]
00334     //[ Private static data                                   ]
00335     //[-------------------------------------------------------]
00336     private:
00337         static const String sErrorString[ErrorStringCount]; /**< Human readable error messages */
00338 
00339 
00340     //[-------------------------------------------------------]
00341     //[ Private functions                                     ]
00342     //[-------------------------------------------------------]
00343     private:
00344         /**
00345         *  @brief
00346         *    Sets an error
00347         *
00348         *  @param[in] nError
00349         *    Error code
00350         *  @param[in] pszErrorLocation
00351         *    Error location
00352         *  @param[in] pData
00353         *    Data
00354         *  @param[in] nEncoding
00355         *    Encoding
00356         */
00357         void SetError(int nError, const char *pszErrorLocation, XmlParsingData *pData, EEncoding nEncoding);
00358 
00359 
00360     //[-------------------------------------------------------]
00361     //[ Private data                                          ]
00362     //[-------------------------------------------------------]
00363     private:
00364         bool   m_bError;            /**< Error detected? */
00365         int    m_nErrorID;          /**< Error ID */
00366         String m_sErrorDescription; /**< Human readable error description */
00367         Cursor m_cErrorCursor;      /**< Error cursor */
00368         int    m_nTabSize;          /**< Tab size */
00369         bool   m_bUseMicrosoftBOM;  /**< The UTF-8 BOM were found when read. Note this, and try to write. */
00370 
00371 
00372 };
00373 
00374 
00375 //[-------------------------------------------------------]
00376 //[ Namespace                                             ]
00377 //[-------------------------------------------------------]
00378 } // PLCore
00379 
00380 
00381 //[-------------------------------------------------------]
00382 //[ Implementation                                        ]
00383 //[-------------------------------------------------------]
00384 #include "PLCore/Xml/XmlDocument.inl"
00385 
00386 
00387 #endif // __PLCORE_XML_DOCUMENT_H__


PixelLight PixelLight 0.9.11-R1
Copyright (C) 2002-2012 by The PixelLight Team
Last modified Thu Feb 23 2012 14:09:02
The content of this PixelLight document is published under the
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported