LCOV - code coverage report
Current view: top level - xmloff/source/core - xmlerror.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 25 29 86.2 %
Date: 2015-06-13 12:38:46 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 <rtl/ustring.hxx>
      22             : #include <osl/diagnose.h>
      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             : #include <rtl/ustrbuf.hxx>
      30             : 
      31             : using ::com::sun::star::uno::Any;
      32             : using ::com::sun::star::uno::Sequence;
      33             : using ::com::sun::star::uno::Reference;
      34             : using ::com::sun::star::xml::sax::XLocator;
      35             : using ::com::sun::star::xml::sax::SAXParseException;
      36             : 
      37             : 
      38             : 
      39             : /// ErrorRecord: contains all information for one error
      40             : 
      41             : 
      42         588 : class ErrorRecord
      43             : {
      44             : public:
      45             : 
      46             :     ErrorRecord( sal_Int32 nId,
      47             :                  const Sequence<OUString>& rParams,
      48             :                  const OUString& rExceptionMessage,
      49             :                  sal_Int32 nRow,
      50             :                  sal_Int32 nColumn,
      51             :                  const OUString& rPublicId,
      52             :                  const OUString& rSystemId);
      53             :     ~ErrorRecord();
      54             : 
      55             :     sal_Int32 nId;  /// error ID
      56             : 
      57             :     OUString sExceptionMessage;/// message of original exception (if available)
      58             : 
      59             :     // XLocator information:
      60             :     sal_Int32 nRow;     /// row number where error occurred (or -1 for unknown)
      61             :     sal_Int32 nColumn;  /// column number where error occurred (or -1)
      62             :     OUString sPublicId; /// public identifier
      63             :     OUString sSystemId; /// public identifier
      64             : 
      65             :     /// message Parameters
      66             :     Sequence<OUString> aParams;
      67             : };
      68             : 
      69             : 
      70         278 : ErrorRecord::ErrorRecord( sal_Int32 nID, const Sequence<OUString>& rParams,
      71             :     const OUString& rExceptionMessage, sal_Int32 nRowNumber, sal_Int32 nCol,
      72             :     const OUString& rPublicId, const OUString& rSystemId) :
      73             :         nId(nID),
      74             :         sExceptionMessage(rExceptionMessage),
      75             :         nRow(nRowNumber),
      76             :         nColumn(nCol),
      77             :         sPublicId(rPublicId),
      78             :         sSystemId(rSystemId),
      79         278 :         aParams(rParams)
      80             : {
      81         278 : }
      82             : 
      83         866 : ErrorRecord::~ErrorRecord()
      84             : {
      85         866 : }
      86             : 
      87             : 
      88             : 
      89             : 
      90          48 : XMLErrors::XMLErrors()
      91             : {
      92          48 : }
      93             : 
      94          48 : XMLErrors::~XMLErrors()
      95             : {
      96          48 : }
      97             : 
      98         278 : void XMLErrors::AddRecord(
      99             :     sal_Int32 nId,
     100             :     const Sequence<OUString> & rParams,
     101             :     const OUString& rExceptionMessage,
     102             :     sal_Int32 nRow,
     103             :     sal_Int32 nColumn,
     104             :     const OUString& rPublicId,
     105             :     const OUString& rSystemId )
     106             : {
     107             :     aErrors.push_back( ErrorRecord( nId, rParams, rExceptionMessage,
     108         278 :                                     nRow, nColumn, rPublicId, rSystemId ) );
     109             : 
     110             : #ifdef DBG_UTIL
     111             : 
     112             :     // give detailed assertion on this message
     113             : 
     114             :     OUStringBuffer sMessage;
     115             : 
     116             :     sMessage.appendAscii( "An error or a warning has occurred during XML import/export!\n" );
     117             : 
     118             :     // ID & flags
     119             :     sMessage.appendAscii( "Error-Id: 0x");
     120             :     sMessage.append( nId, 16 );
     121             :     sMessage.appendAscii( "\n    Flags: " );
     122             :     sal_Int32 nFlags = (nId & XMLERROR_MASK_FLAG);
     123             :     sMessage.append( nFlags >> 28, 16 );
     124             :     if( (nFlags & XMLERROR_FLAG_WARNING) != 0 )
     125             :         sMessage.appendAscii( " WARNING" );
     126             :     if( (nFlags & XMLERROR_FLAG_ERROR) != 0 )
     127             :         sMessage.appendAscii( " ERROR" );
     128             :     if( (nFlags & XMLERROR_FLAG_SEVERE) != 0 )
     129             :         sMessage.appendAscii( " SEVERE" );
     130             :     sMessage.appendAscii( "\n    Class: " );
     131             :     sal_Int32 nClass = (nId & XMLERROR_MASK_CLASS);
     132             :     sMessage.append( nClass >> 16, 16 );
     133             :     if( (nClass & XMLERROR_CLASS_IO) != 0 )
     134             :         sMessage.appendAscii( " IO" );
     135             :     if( (nClass & XMLERROR_CLASS_FORMAT) != 0 )
     136             :         sMessage.appendAscii( " FORMAT" );
     137             :     if( (nClass & XMLERROR_CLASS_API) != 0 )
     138             :         sMessage.appendAscii( " API" );
     139             :     if( (nClass & XMLERROR_CLASS_OTHER) != 0 )
     140             :         sMessage.appendAscii( " OTHER" );
     141             :     sMessage.appendAscii( "\n    Number: " );
     142             :     sal_Int32 nNumber = (nId & XMLERROR_MASK_NUMBER);
     143             :     sMessage.append( nNumber, 16 );
     144             :     sMessage.appendAscii( "\n");
     145             : 
     146             :     // the parameters
     147             :     sMessage.appendAscii( "Parameters:\n" );
     148             :     sal_Int32 nLength = rParams.getLength();
     149             :     const OUString* pParams = rParams.getConstArray();
     150             :     for( sal_Int32 i = 0; i < nLength; i++ )
     151             :     {
     152             :         sMessage.appendAscii( "    " );
     153             :         sMessage.append( i );
     154             :         sMessage.appendAscii( ": " );
     155             :         sMessage.append( pParams[i] );
     156             :         sMessage.appendAscii( "\n" );
     157             :     }
     158             : 
     159             :     // the exception message
     160             :     sMessage.appendAscii( "Exception-Message: " );
     161             :     sMessage.append( rExceptionMessage );
     162             :     sMessage.appendAscii( "\n" );
     163             : 
     164             :     // position (if given)
     165             :     if( (nRow != -1) || (nColumn != -1) )
     166             :     {
     167             :         sMessage.appendAscii( "Position:\n    Public Identifier: " );
     168             :         sMessage.append( rPublicId );
     169             :         sMessage.appendAscii( "\n    System Identifier: " );
     170             :         sMessage.append( rSystemId );
     171             :         sMessage.appendAscii( "\n    Row, Column: " );
     172             :         sMessage.append( nRow );
     173             :         sMessage.appendAscii( "," );
     174             :         sMessage.append( nColumn );
     175             :         sMessage.appendAscii( "\n" );
     176             :     }
     177             : 
     178             :     // convert to byte string and signal the error
     179             :     OString aError(OUStringToOString(sMessage.makeStringAndClear(),
     180             :         RTL_TEXTENCODING_ASCII_US));
     181             :     OSL_FAIL( aError.getStr() );
     182             : #endif
     183         278 : }
     184             : 
     185         278 : void XMLErrors::AddRecord(
     186             :     sal_Int32 nId,
     187             :     const Sequence<OUString> & rParams,
     188             :     const OUString& rExceptionMessage,
     189             :     const Reference<XLocator> & rLocator)
     190             : {
     191         278 :     if ( rLocator.is() )
     192             :     {
     193             :         AddRecord( nId, rParams, rExceptionMessage,
     194         298 :                    rLocator->getLineNumber(), rLocator->getColumnNumber(),
     195         447 :                    rLocator->getPublicId(), rLocator->getSystemId() );
     196             :     }
     197             :     else
     198             :     {
     199         129 :         OUString sEmpty;
     200             :         AddRecord( nId, rParams, rExceptionMessage,
     201         129 :                    -1, -1, sEmpty, sEmpty );
     202             :     }
     203         278 : }
     204             : 
     205          47 : void XMLErrors::ThrowErrorAsSAXException(sal_Int32 nIdMask)
     206             :     throw( SAXParseException )
     207             : {
     208             :     // search first error/warning that matches the nIdMask
     209         972 :     for( ErrorList::iterator aIter = aErrors.begin();
     210         648 :          aIter != aErrors.end();
     211             :          ++aIter )
     212             :     {
     213         277 :         if ( (aIter->nId & nIdMask) != 0 )
     214             :         {
     215             :             // we throw the error
     216           0 :             ErrorRecord& rErr = aErrors[0];
     217           0 :             Any aAny;
     218           0 :             aAny <<= rErr.aParams;
     219             :             throw SAXParseException(
     220             :                 rErr.sExceptionMessage, NULL, aAny,
     221           0 :                 rErr.sPublicId, rErr.sSystemId, rErr.nRow, rErr.nColumn );
     222             :         }
     223             :     }
     224          47 : }
     225             : 
     226             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11