xmlbase.h

Go to the documentation of this file.
00001 /*
00002  * The information in this file is
00003  * Copyright(c) 2007 Ball Aerospace & Technologies Corporation
00004  * and is subject to the terms and conditions of the
00005  * GNU Lesser General Public License Version 2.1
00006  * The license text is available from   
00007  * http://www.gnu.org/licenses/lgpl.html
00008  */
00009 
00010 #ifndef XMLBASE_H
00011 #define XMLBASE_H
00012 
00013 #include <stdio.h>
00014 
00015 #include "XercesIncludes.h"
00016 
00017 #include "Filename.h"
00018 #include "MessageLogMgr.h"
00019 #include "MessageLog.h"
00020 
00021 /** @file xmlbase.h
00022  *  @brief XML utilities and functionality common to reading and writing
00023  */
00024 /** @defgroup app_xml Application XML system */
00025 
00026 /**
00027  * This automatically handles conversions between Unicode and ASCII format.
00028  *
00029  * @ingroup app_xml
00030  *
00031  * @par requirements
00032  * Apache Xerces-C++ version 3.1.1
00033  */
00034 class OpticksXStr
00035 {
00036 public:
00037    /**
00038     * Create an %OpticksXStr object containing an ASCII string.
00039     * @param pToTranscode
00040     *        The ASCII string
00041     */
00042    OpticksXStr(const char* pToTranscode)
00043    {
00044       mpAsciiForm = const_cast<char*>(pToTranscode);
00045       mpUnicodeForm = XERCES_CPP_NAMESPACE_QUALIFIER XMLString::transcode(pToTranscode);
00046       mReleaseWhich = 1;
00047    }
00048 
00049    /**
00050     * Create an %OpticksXStr object containing a Unicode string.
00051     * @param pUnicode
00052     *        The Unicode string
00053     */
00054    OpticksXStr(const XMLCh* pUnicode)
00055    {
00056       mpUnicodeForm = const_cast<XMLCh*>(pUnicode);
00057       mpAsciiForm = XERCES_CPP_NAMESPACE_QUALIFIER XMLString::transcode(pUnicode);
00058       mReleaseWhich = 2;
00059    }
00060 
00061    /**
00062     * Destroy the %OpticksXStr, cleaning up Xerces allocated memory as needed.
00063     */
00064    ~OpticksXStr()
00065    {
00066       if (mReleaseWhich == 1)
00067       {
00068          XERCES_CPP_NAMESPACE_QUALIFIER XMLString::release(&mpUnicodeForm);
00069       }
00070       else if (mReleaseWhich == 2)
00071       {
00072          XERCES_CPP_NAMESPACE_QUALIFIER XMLString::release(&mpAsciiForm);
00073       }
00074    }
00075 
00076    /**
00077     * Obtain the Unicode version of this %OpticksXStr
00078     *
00079     * @return the Unicode form
00080     */
00081    const XMLCh* unicodeForm() const
00082    {
00083       return mpUnicodeForm;
00084    }
00085 
00086    /**
00087     * Obtain the ASCII version of this %OpticksXStr
00088     *
00089     * @return the ASCII form
00090     */
00091    const char* asciiForm() const
00092    {
00093       return mpAsciiForm;
00094    }
00095 
00096 private:
00097    XMLCh* mpUnicodeForm;
00098    char* mpAsciiForm;
00099    int mReleaseWhich;
00100 };
00101 
00102 /**
00103  * Syntactic shortcut which converts the argument to ASCII
00104  *
00105  * @ingroup app_xml
00106  *
00107  * @param str
00108  *        The ASCII or Unicode string to convert to ASCII
00109  *
00110  * @return The ASCII representation of %str.
00111  *         This pointer is invalid once the current statement completes execution.
00112  *
00113  * @warning The return value of this method is invalid after the current statement completes execution.
00114  *          If the ASCII string is to be stored in a variable, construct an OpticksXStr manually.
00115  */
00116 #define A(str) OpticksXStr(str).asciiForm()
00117 
00118 /**
00119  * Syntactic shortcut which converts the argument to Unicode
00120  *
00121  * @ingroup app_xml
00122  *
00123  * @param str
00124  *        The ASCII or Unicode string to convert to Unicode
00125  *
00126  * @return The Unicode representation of %str.
00127  *         This pointer is invalid once the current statement completes execution.
00128  *
00129  * @warning The return value of this method is invalid after the current statement completes execution.
00130  *          If the Unicode string is to be stored in a variable, construct an OpticksXStr manually.
00131  */
00132 #ifdef X
00133    // Undefine the XQilla version because it throws exceptions in some situations
00134    #undef X
00135 #endif
00136 #define X(str) OpticksXStr(str).unicodeForm()
00137 
00138 /**
00139  * Common functionality for reading and writing XML
00140  * @ingroup app_xml
00141  *
00142  * @par requirements
00143  * Apache Xerces-C++ version 3.1.1
00144  */
00145 class XmlBase
00146 {
00147 public: // static helpers
00148    /**
00149     * This function will convert a \b file:// URL to a path.
00150     *
00151     * It handles Windows drive letters reasonably;
00152     * however, you will not be able to refer to paths such
00153     * as \b /c:/ on UNIX.
00154     *
00155     * @param pUrl
00156     *        The URL to convert to a path. This is passed
00157     *        in as a Unicode string as the most often used
00158     *        pattern is to read an attribute or element value
00159     *        and immediately convert to a path.
00160     *
00161     * @return A string representation of the path
00162     */
00163    static std::string URLtoPath(const XMLCh* pUrl);
00164 
00165    /**
00166     * This function will convert a path to a \b file:// URL.
00167     *
00168     * It handles Windows drive letters reasonably;
00169     * however, you will not be able to refer to paths such
00170     * as \b /c:/ on UNIX.
00171     *
00172     * @param path
00173     *        The path to convert to a URL.
00174     *
00175     * @return The URL string.
00176     */
00177    static std::string PathToURL(const std::string& path);
00178 
00179    /**
00180     * Construct a new XML processor.
00181     *
00182     * Constructing an %XmlBase is not particularly useful as most
00183     * functionality will be in derived classes.
00184     *
00185     * @param pLog
00186     *        If this optional MessageLog is passed in, any errors
00187     *        encountered during processing will be logged.
00188     *
00189     * @throw XmlException
00190     *        When Xerces fails to initialize.
00191     *
00192     * @see XmlReader, XMLWriter
00193     */
00194    XmlBase(MessageLog* pLog = NULL);
00195 
00196    /**
00197     * Destroy and cleanup the XML processor.
00198     */
00199    virtual ~XmlBase();
00200 
00201    /**
00202     * Encode data in base 64 representation.
00203     *
00204     * @param pData
00205     *        This is the data which will be encoded.
00206     *
00207     * @param size
00208     *        The number of items in \e data
00209     *
00210     * @param pOutLen
00211     *        Optional output parameter which returns the length of the output byte array.
00212     *
00213     * @param pChecksum
00214     *        Optional output parameter which returns a CCITT checksum.
00215     *
00216     * @return The Unicode form of the base 64 encoded data. It should be freed by calling ::operator delete.
00217     */
00218    static XMLByte* encodeBase64(const unsigned int* pData, XMLSize_t size,
00219       XMLSize_t* pOutLen = NULL, std::string* pChecksum = NULL);
00220 
00221    /**
00222     * Decode data in base 64 representation.
00223     *
00224     * @param pData
00225     *        This is the Unicode representation of the base 64 encoded data.
00226     *
00227     * @param size
00228     *        The expected number of data items or 0 if size is unknown.
00229     *
00230     * @param checksum
00231     *        The CCITT checksum to verify. To skip verification, set this to an empty string.
00232     *
00233     * @return The array of decoded data. It should be freed by calling delete [].
00234     */
00235    static unsigned int* decodeBase64(const XMLByte* pData, XMLSize_t size = 0,
00236       const std::string& checksum = std::string());
00237 
00238    /**
00239     * This class represents an exception thrown by the XML classes.
00240     */
00241    class XmlException
00242    {
00243    public:
00244       /**
00245        * Create a new exception with no error message.
00246        */
00247       XmlException() {}
00248 
00249       /**
00250        * Destroy and cleanup the exception object.
00251        */
00252       virtual ~XmlException() {}
00253 
00254       /**
00255        * Create a new exception with an error message.
00256        *
00257        * @param msg
00258        *        The error message string.
00259        */
00260       XmlException(std::string msg) :
00261          mMessage(msg) {}
00262 
00263       /**
00264        * Accessor for the error message.
00265        *
00266        * @return The error message string.
00267        */
00268       virtual std::string str() const
00269       {
00270          return mMessage;
00271       }
00272 
00273    private:
00274       std::string mMessage;
00275    };
00276 
00277    /**
00278     * This is the current file format version.
00279     */
00280    static const unsigned int VERSION;
00281 
00282    /**
00283     * Log a Xerces DOM error to the message log, if one is available.
00284     * 
00285     * @param exc
00286     *        The exception to log.
00287     */
00288    void logError(const XERCES_CPP_NAMESPACE_QUALIFIER DOMError& exc);
00289 
00290 protected:
00291    /**
00292     * Log an XML exception to the message log, if one is available.
00293     *
00294     * @param pExc
00295     *        The exception to log.
00296     */
00297    void logException(const XERCES_CPP_NAMESPACE_QUALIFIER XMLException* pExc);
00298 
00299    /**
00300     * Log a Xerces DOM exception to the message log, if one is available.
00301     *
00302     * @param pExc
00303     *        The exception to log.
00304     */
00305    void logException(const XERCES_CPP_NAMESPACE_QUALIFIER DOMException* pExc);
00306 
00307    /**
00308     * Log a Xerces SAX exception to the message log, if one is available.
00309     *
00310     * @param pExc
00311     *        The exception to log.
00312     *
00313     * @param severity
00314     *        A severity which is also logged. SAX exceptions may not be fatal
00315     *        and this is a way to inform the uses of the exception severity.
00316     */
00317    void logException(const XERCES_CPP_NAMESPACE_QUALIFIER SAXParseException* pExc, std::string severity);
00318 
00319    /**
00320     * Log a string message to the message log, if one is available.
00321     *
00322     * @param msg
00323     *        The message to log.
00324     */
00325    void logSimpleMessage(std::string msg);
00326 
00327    /**
00328     * This is the ASCII namespace which XML data exists in.
00329     */
00330    static const char* sNamespaceId;
00331 
00332 private:
00333    MessageLog* mpLog;
00334 };
00335 
00336 #endif

Software Development Kit - Opticks 4.9.0 Build 16218