VirtualBox

Changeset 14922 in vbox for trunk/src


Ignore:
Timestamp:
Dec 2, 2008 5:39:34 PM (16 years ago)
Author:
vboxsync
Message:

Main: more XML reader implemenation.

Location:
trunk/src/VBox/Main
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/VirtualBoxImpl.cpp

    r14904 r14922  
    38123812                         aFile.uri(), err.rc());
    38133813    }
    3814     catch (const XmlTreeBackend::Error &err)
     3814    catch (const xml::RuntimeError &err)
    38153815    {
    38163816        Assert (err.what() != NULL);
  • trunk/src/VBox/Main/xml/Settings.cpp

    r14904 r14922  
    518518//////////////////////////////////////////////////////////////////////////////
    519519
    520 class XmlTreeBackend::XmlError : public XmlTreeBackend::Error
    521 {
    522 public:
    523 
    524     XmlError (xmlErrorPtr aErr)
    525     {
    526         if (!aErr)
    527             throw xml::EInvalidArg (RT_SRC_POS);
    528 
    529         char *msg = Format (aErr);
    530         setWhat (msg);
    531         RTStrFree (msg);
    532     }
    533 
    534     /**
    535      * Composes a single message for the given error. The caller must free the
    536      * returned string using RTStrFree() when no more necessary.
    537      */
    538     static char *Format (xmlErrorPtr aErr)
    539     {
    540         const char *msg = aErr->message ? aErr->message : "<none>";
    541         size_t msgLen = strlen (msg);
    542         /* strip spaces, trailing EOLs and dot-like char */
    543         while (msgLen && strchr (" \n.?!", msg [msgLen - 1]))
    544             -- msgLen;
    545 
    546         char *finalMsg = NULL;
    547         RTStrAPrintf (&finalMsg, "%.*s.\nLocation: '%s', line %d (%d), column %d",
    548                       msgLen, msg, aErr->file, aErr->line, aErr->int1, aErr->int2);
    549 
    550         return finalMsg;
    551     }
    552 };
    553 
    554520struct XmlTreeBackend::Data
    555521{
     
    693659                m->trappedErr->rethrow();
    694660
    695             throw XmlError (xmlCtxtGetLastError (m->ctxt));
     661            throw xml::XmlError(xmlCtxtGetLastError (m->ctxt));
    696662        }
    697663
     
    734700                        m->trappedErr->rethrow();
    735701
    736                     throw XmlError (xmlCtxtGetLastError (m->ctxt));
     702                    throw xml::XmlError(xmlCtxtGetLastError (m->ctxt));
    737703                }
    738704
     
    767733                    {
    768734                        xmlFreeDoc (newDoc);
    769                         throw Error (errorStr);
     735                        throw xml::RuntimeError(errorStr);
    770736                        /* errorStr is freed in catch(...) below */
    771737                    }
     
    861827                        throw xml::LogicError (RT_SRC_POS);
    862828
    863                     throw Error (errorStr);
     829                    throw xml::RuntimeError(errorStr);
    864830                    /* errorStr is freed in catch(...) below */
    865831                }
     
    11071073    char * &str = *(char * *) aCtxt;
    11081074
    1109     char *newMsg = XmlError::Format (aErr);
     1075    char *newMsg = xml::XmlError::Format (aErr);
    11101076    AssertReturnVoid (newMsg != NULL);
    11111077
  • trunk/src/VBox/Main/xml/xml.cpp

    r14904 r14922  
    9595
    9696LogicError::LogicError(RT_SRC_POS_DECL)
     97    : Error(NULL)
    9798{
    9899    char *msg = NULL;
     
    103104}
    104105
     106XmlError::XmlError(xmlErrorPtr aErr)
     107{
     108    if (!aErr)
     109        throw EInvalidArg (RT_SRC_POS);
     110
     111    char *msg = Format (aErr);
     112    setWhat (msg);
     113    RTStrFree (msg);
     114}
     115
     116/**
     117 * Composes a single message for the given error. The caller must free the
     118 * returned string using RTStrFree() when no more necessary.
     119 */
     120// static
     121char *XmlError::Format(xmlErrorPtr aErr)
     122{
     123    const char *msg = aErr->message ? aErr->message : "<none>";
     124    size_t msgLen = strlen (msg);
     125    /* strip spaces, trailing EOLs and dot-like char */
     126    while (msgLen && strchr (" \n.?!", msg [msgLen - 1]))
     127        -- msgLen;
     128
     129    char *finalMsg = NULL;
     130    RTStrAPrintf (&finalMsg, "%.*s.\nLocation: '%s', line %d (%d), column %d",
     131                    msgLen, msg, aErr->file, aErr->line, aErr->int1, aErr->int2);
     132
     133    return finalMsg;
     134}
     135
     136EIPRTFailure::EIPRTFailure(int aRC)
     137    : RuntimeError(NULL),
     138      mRC(aRC)
     139{
     140    char *newMsg = NULL;
     141    RTStrAPrintf(&newMsg, "Runtime error: %d (%s)", aRC, RTErrGetShort(aRC));
     142    setWhat(newMsg);
     143    RTStrFree(newMsg);
     144}
    105145
    106146//////////////////////////////////////////////////////////////////////////////
     
    118158};
    119159
    120 File::File (Mode aMode, const char *aFileName)
     160File::File(Mode aMode, const char *aFileName)
    121161    : m (new Data())
    122162{
     
    401441}
    402442
     443struct ReadContext
     444{
     445    File file;
     446    std::string error;
     447
     448    ReadContext(const char *pcszFilename)
     449        : file(File::Mode_Read, pcszFilename)
     450    {
     451    }
     452
     453    void setError(const xml::Error &x)
     454    {
     455        error = x.what();
     456    }
     457
     458    void setError(const std::exception &x)
     459    {
     460        error = x.what();
     461    }
     462};
     463
    403464void XmlFileParser::read(const char *pcszFilename)
    404465{
    405466    GlobalLock lock();
     467//     global.setExternalEntityLoader(ExternalEntityLoader);
    406468
    407469    xmlDocPtr doc = NULL;
     470    ReadContext *pContext = NULL;
    408471
    409472    m->strXmlFilename = pcszFilename;
    410473
    411     /* Note: when parsing we use XML_PARSE_NOBLANKS to instruct libxml2 to
    412         * remove text nodes that contain only blanks. This is important because
    413         * otherwise xmlSaveDoc() won't be able to do proper indentation on
    414         * output. */
    415 
    416     /* parse the stream */
    417     /* NOTE: new InputCtxt instance will be deleted when the stream is closed by
    418         * the libxml2 API (e.g. when calling xmlFreeParserCtxt()) */
    419 //     doc = xmlCtxtReadIO(m->ctxt,
    420 //                         ReadCallback,
    421 //                         CloseCallback,
    422 //                         new Data::InputCtxt (&aInput, m->trappedErr),
    423 //                         aInput.uri(), NULL,
    424 //                         XML_PARSE_NOBLANKS);
    425     if (doc == NULL)
    426     {
    427         /* look if there was a forwared exception from the lower level */
    428 //         if (m->trappedErr.get() != NULL)
    429 //             m->trappedErr->rethrow();
    430 
    431 //         throw XmlError (xmlCtxtGetLastError (m->ctxt));
    432     }
    433 }
     474    try
     475    {
     476        pContext = new ReadContext(pcszFilename);
     477        doc = xmlCtxtReadIO(m->ctxt,
     478                            ReadCallback,
     479                            CloseCallback,
     480                            pContext,
     481                            pcszFilename,
     482                            NULL,       // encoding
     483                            XML_PARSE_NOBLANKS);
     484        if (doc == NULL)
     485        {
     486            throw XmlError(xmlCtxtGetLastError(m->ctxt));
     487        }
     488
     489        xmlFreeDoc(doc);
     490        doc = NULL;
     491
     492        delete pContext;
     493        pContext = NULL;
     494    }
     495    catch (...)
     496    {
     497        if (doc != NULL)
     498            xmlFreeDoc(doc);
     499
     500        if (pContext)
     501            delete pContext;
     502
     503        throw;
     504    }
     505}
     506
     507// static
     508int XmlFileParser::ReadCallback(void *aCtxt, char *aBuf, int aLen)
     509{
     510    ReadContext *pContext = static_cast<ReadContext*>(aCtxt);
     511
     512    /* To prevent throwing exceptions while inside libxml2 code, we catch
     513     * them and forward to our level using a couple of variables. */
     514
     515    try
     516    {
     517        return pContext->file.read(aBuf, aLen);
     518    }
     519    catch (const xml::EIPRTFailure &err) { pContext->setError(err); }
     520    catch (const xml::Error &err) { pContext->setError(err); }
     521    catch (const std::exception &err) { pContext->setError(err); }
     522    catch (...) { pContext->setError(xml::LogicError(RT_SRC_POS)); }
     523
     524    return -1 /* failure */;
     525}
     526
     527int XmlFileParser::CloseCallback(void *aCtxt)
     528{
     529    /// @todo to be written
     530
     531    return -1;
     532}
     533
    434534
    435535} // end namespace xml
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette