LCOV - code coverage report
Current view: top level - libreoffice/xmloff/source/core - xmlerror.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 23 29 79.3 %
Date: 2012-12-17 Functions: 8 8 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include "xmloff/xmlerror.hxx"
      21             : #include <tools/debug.hxx>
      22             : #include <rtl/ustring.hxx>
      23             : #include <com/sun/star/xml/sax/XLocator.hpp>
      24             : #include <com/sun/star/xml/sax/SAXParseException.hpp>
      25             : #include <com/sun/star/uno/Any.hxx>
      26             : #include <com/sun/star/uno/Reference.hxx>
      27             : #include <com/sun/star/uno/Sequence.hxx>
      28             : 
      29             : 
      30             : #include <rtl/ustrbuf.hxx>
      31             : 
      32             : 
      33             : 
      34             : using ::rtl::OUString;
      35             : using ::rtl::OUStringBuffer;
      36             : using ::com::sun::star::uno::Any;
      37             : using ::com::sun::star::uno::Sequence;
      38             : using ::com::sun::star::uno::Reference;
      39             : using ::com::sun::star::xml::sax::XLocator;
      40             : using ::com::sun::star::xml::sax::SAXParseException;
      41             : 
      42             : 
      43             : //
      44             : /// ErrorRecord: contains all information for one error
      45             : //
      46             : 
      47         210 : class ErrorRecord
      48             : {
      49             : public:
      50             : 
      51             :     ErrorRecord( sal_Int32 nId,
      52             :                  const Sequence<OUString>& rParams,
      53             :                  const OUString& rExceptionMessage,
      54             :                  sal_Int32 nRow,
      55             :                  sal_Int32 nColumn,
      56             :                  const OUString& rPublicId,
      57             :                  const OUString& rSystemId);
      58             :     ~ErrorRecord();
      59             : 
      60             :     sal_Int32 nId;  /// error ID
      61             : 
      62             :     OUString sExceptionMessage;/// message of original exception (if available)
      63             : 
      64             :     // XLocator information:
      65             :     sal_Int32 nRow;     /// row number where error occurred (or -1 for unknown)
      66             :     sal_Int32 nColumn;  /// column number where error occurred (or -1)
      67             :     OUString sPublicId; /// public identifier
      68             :     OUString sSystemId; /// public identifier
      69             : 
      70             :     /// message Parameters
      71             :     Sequence<OUString> aParams;
      72             : };
      73             : 
      74             : 
      75          84 : ErrorRecord::ErrorRecord( sal_Int32 nID, const Sequence<OUString>& rParams,
      76             :     const OUString& rExceptionMessage, sal_Int32 nRowNumber, sal_Int32 nCol,
      77             :     const OUString& rPublicId, const OUString& rSystemId) :
      78             :         nId(nID),
      79             :         sExceptionMessage(rExceptionMessage),
      80             :         nRow(nRowNumber),
      81             :         nColumn(nCol),
      82             :         sPublicId(rPublicId),
      83             :         sSystemId(rSystemId),
      84          84 :         aParams(rParams)
      85             : {
      86          84 : }
      87             : 
      88         294 : ErrorRecord::~ErrorRecord()
      89             : {
      90         294 : }
      91             : 
      92             : 
      93             : 
      94             : 
      95           2 : XMLErrors::XMLErrors()
      96             : {
      97           2 : }
      98             : 
      99           2 : XMLErrors::~XMLErrors()
     100             : {
     101           2 : }
     102             : 
     103          84 : void XMLErrors::AddRecord(
     104             :     sal_Int32 nId,
     105             :     const Sequence<OUString> & rParams,
     106             :     const OUString& rExceptionMessage,
     107             :     sal_Int32 nRow,
     108             :     sal_Int32 nColumn,
     109             :     const OUString& rPublicId,
     110             :     const OUString& rSystemId )
     111             : {
     112             :     aErrors.push_back( ErrorRecord( nId, rParams, rExceptionMessage,
     113          84 :                                     nRow, nColumn, rPublicId, rSystemId ) );
     114             : 
     115             : #ifdef DBG_UTIL
     116             : 
     117             :     // give detailed assertion on this message
     118             : 
     119             :     OUStringBuffer sMessage;
     120             : 
     121             :     sMessage.appendAscii( "An error or a warning has occurred during XML import/export!\n" );
     122             : 
     123             :     // ID & flags
     124             :     sMessage.appendAscii( "Error-Id: 0x");
     125             :     sMessage.append( nId, 16 );
     126             :     sMessage.appendAscii( "\n    Flags: " );
     127             :     sal_Int32 nFlags = (nId & XMLERROR_MASK_FLAG);
     128             :     sMessage.append( nFlags >> 28, 16 );
     129             :     if( (nFlags & XMLERROR_FLAG_WARNING) != 0 )
     130             :         sMessage.appendAscii( " WARNING" );
     131             :     if( (nFlags & XMLERROR_FLAG_ERROR) != 0 )
     132             :         sMessage.appendAscii( " ERRROR" );
     133             :     if( (nFlags & XMLERROR_FLAG_SEVERE) != 0 )
     134             :         sMessage.appendAscii( " SEVERE" );
     135             :     sMessage.appendAscii( "\n    Class: " );
     136             :     sal_Int32 nClass = (nId & XMLERROR_MASK_CLASS);
     137             :     sMessage.append( nClass >> 16, 16 );
     138             :     if( (nClass & XMLERROR_CLASS_IO) != 0 )
     139             :         sMessage.appendAscii( " IO" );
     140             :     if( (nClass & XMLERROR_CLASS_FORMAT) != 0 )
     141             :         sMessage.appendAscii( " FORMAT" );
     142             :     if( (nClass & XMLERROR_CLASS_API) != 0 )
     143             :         sMessage.appendAscii( " API" );
     144             :     if( (nClass & XMLERROR_CLASS_OTHER) != 0 )
     145             :         sMessage.appendAscii( " OTHER" );
     146             :     sMessage.appendAscii( "\n    Number: " );
     147             :     sal_Int32 nNumber = (nId & XMLERROR_MASK_NUMBER);
     148             :     sMessage.append( nNumber, 16 );
     149             :     sMessage.appendAscii( "\n");
     150             : 
     151             :     // the parameters
     152             :     sMessage.appendAscii( "Parameters:\n" );
     153             :     sal_Int32 nLength = rParams.getLength();
     154             :     const OUString* pParams = rParams.getConstArray();
     155             :     for( sal_Int32 i = 0; i < nLength; i++ )
     156             :     {
     157             :         sMessage.appendAscii( "    " );
     158             :         sMessage.append( i );
     159             :         sMessage.appendAscii( ": " );
     160             :         sMessage.append( pParams[i] );
     161             :         sMessage.appendAscii( "\n" );
     162             :     }
     163             : 
     164             :     // the exception message
     165             :     sMessage.appendAscii( "Exception-Message: " );
     166             :     sMessage.append( rExceptionMessage );
     167             :     sMessage.appendAscii( "\n" );
     168             : 
     169             :     // position (if given)
     170             :     if( (nRow != -1) || (nColumn != -1) )
     171             :     {
     172             :         sMessage.appendAscii( "Position:\n    Public Identifier: " );
     173             :         sMessage.append( rPublicId );
     174             :         sMessage.appendAscii( "\n    System Identifier: " );
     175             :         sMessage.append( rSystemId );
     176             :         sMessage.appendAscii( "\n    Row, Column: " );
     177             :         sMessage.append( nRow );
     178             :         sMessage.appendAscii( "," );
     179             :         sMessage.append( nColumn );
     180             :         sMessage.appendAscii( "\n" );
     181             :     }
     182             : 
     183             :     // convert to byte string and signal the error
     184             :     rtl::OString aError(rtl::OUStringToOString(sMessage.makeStringAndClear(),
     185             :         RTL_TEXTENCODING_ASCII_US));
     186             :     OSL_FAIL( aError.getStr() );
     187             : #endif
     188          84 : }
     189             : 
     190          84 : void XMLErrors::AddRecord(
     191             :     sal_Int32 nId,
     192             :     const Sequence<OUString> & rParams,
     193             :     const OUString& rExceptionMessage,
     194             :     const Reference<XLocator> & rLocator)
     195             : {
     196          84 :     if ( rLocator.is() )
     197             :     {
     198             :         AddRecord( nId, rParams, rExceptionMessage,
     199         168 :                    rLocator->getLineNumber(), rLocator->getColumnNumber(),
     200         252 :                    rLocator->getPublicId(), rLocator->getSystemId() );
     201             :     }
     202             :     else
     203             :     {
     204           0 :         OUString sEmpty;
     205             :         AddRecord( nId, rParams, rExceptionMessage,
     206           0 :                    -1, -1, sEmpty, sEmpty );
     207             :     }
     208          84 : }
     209             : 
     210           2 : void XMLErrors::ThrowErrorAsSAXException(sal_Int32 nIdMask)
     211             :     throw( SAXParseException )
     212             : {
     213             :     // search first error/warning that matches the nIdMask
     214         258 :     for( ErrorList::iterator aIter = aErrors.begin();
     215         172 :          aIter != aErrors.end();
     216             :          ++aIter )
     217             :     {
     218          84 :         if ( (aIter->nId & nIdMask) != 0 )
     219             :         {
     220             :             // we throw the error
     221           0 :             ErrorRecord& rErr = aErrors[0];
     222           0 :             Any aAny;
     223           0 :             aAny <<= rErr.aParams;
     224             :             throw SAXParseException(
     225             :                 rErr.sExceptionMessage, NULL, aAny,
     226           0 :                 rErr.sPublicId, rErr.sSystemId, rErr.nRow, rErr.nColumn );
     227             :         }
     228             :     }
     229           2 : }
     230             : 
     231             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10