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

Generated by: LCOV version 1.10