LCOV - code coverage report
Current view: top level - libreoffice/sw/source/core/unocore - XMLRangeHelper.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 144 0.0 %
Date: 2012-12-27 Functions: 0 10 0.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             : 
      21             : #include "XMLRangeHelper.hxx"
      22             : #include <unotools/charclass.hxx>
      23             : #include <rtl/ustrbuf.hxx>
      24             : 
      25             : #include <algorithm>
      26             : #include <functional>
      27             : 
      28             : using ::rtl::OUString;
      29             : using ::rtl::OUStringBuffer;
      30             : 
      31             : // ================================================================================
      32             : 
      33             : namespace
      34             : {
      35             : /** unary function that escapes backslashes and single quotes in a sal_Unicode
      36             :     array (which you can get from an OUString with getStr()) and puts the result
      37             :     into the OUStringBuffer given in the CTOR
      38             :  */
      39             : class lcl_Escape : public ::std::unary_function< sal_Unicode, void >
      40             : {
      41             : public:
      42           0 :     lcl_Escape( ::rtl::OUStringBuffer & aResultBuffer ) : m_aResultBuffer( aResultBuffer ) {}
      43           0 :     void operator() ( sal_Unicode aChar )
      44             :     {
      45             :         static const sal_Unicode m_aQuote( '\'' );
      46             :         static const sal_Unicode m_aBackslash( '\\' );
      47             : 
      48           0 :         if( aChar == m_aQuote ||
      49             :             aChar == m_aBackslash )
      50           0 :             m_aResultBuffer.append( m_aBackslash );
      51           0 :         m_aResultBuffer.append( aChar );
      52           0 :     }
      53             : 
      54             : private:
      55             :     ::rtl::OUStringBuffer & m_aResultBuffer;
      56             : };
      57             : 
      58             : // ----------------------------------------
      59             : 
      60             : /** unary function that removes backslash escapes in a sal_Unicode array (which
      61             :     you can get from an OUString with getStr()) and puts the result into the
      62             :     OUStringBuffer given in the CTOR
      63             :  */
      64             : class lcl_UnEscape : public ::std::unary_function< sal_Unicode, void >
      65             : {
      66             : public:
      67           0 :     lcl_UnEscape( ::rtl::OUStringBuffer & aResultBuffer ) : m_aResultBuffer( aResultBuffer ) {}
      68           0 :     void operator() ( sal_Unicode aChar )
      69             :     {
      70             :         static const sal_Unicode m_aBackslash( '\\' );
      71             : 
      72           0 :         if( aChar != m_aBackslash )
      73           0 :             m_aResultBuffer.append( aChar );
      74           0 :     }
      75             : 
      76             : private:
      77             :     ::rtl::OUStringBuffer & m_aResultBuffer;
      78             : };
      79             : 
      80             : // ----------------------------------------
      81             : 
      82           0 : void lcl_getXMLStringForCell( const /*::chart::*/XMLRangeHelper::Cell & rCell, rtl::OUStringBuffer * output )
      83             : {
      84             :     OSL_ASSERT(output != 0);
      85             : 
      86           0 :     if( rCell.empty())
      87           0 :         return;
      88             : 
      89           0 :     sal_Int32 nCol = rCell.nColumn;
      90           0 :     output->append( (sal_Unicode)'.' );
      91           0 :     if( ! rCell.bRelativeColumn )
      92           0 :         output->append( (sal_Unicode)'$' );
      93             : 
      94             :     // get A, B, C, ..., AA, AB, ... representation of column number
      95           0 :     if( nCol < 26 )
      96           0 :         output->append( (sal_Unicode)('A' + nCol) );
      97           0 :     else if( nCol < 702 )
      98             :     {
      99           0 :         output->append( (sal_Unicode)('A' + nCol / 26 - 1 ));
     100           0 :         output->append( (sal_Unicode)('A' + nCol % 26) );
     101             :     }
     102             :     else    // works for nCol <= 18,278
     103             :     {
     104           0 :         output->append( (sal_Unicode)('A' + nCol / 702 - 1 ));
     105           0 :         output->append( (sal_Unicode)('A' + (nCol % 702) / 26 ));
     106           0 :         output->append( (sal_Unicode)('A' + nCol % 26) );
     107             :     }
     108             : 
     109             :     // write row number as number
     110           0 :     if( ! rCell.bRelativeRow )
     111           0 :         output->append( (sal_Unicode)'$' );
     112           0 :     output->append( rCell.nRow + (sal_Int32)1 );
     113             : }
     114             : 
     115           0 : void lcl_getSingleCellAddressFromXMLString(
     116             :     const ::rtl::OUString& rXMLString,
     117             :     sal_Int32 nStartPos, sal_Int32 nEndPos,
     118             :     /*::chart::*/XMLRangeHelper::Cell & rOutCell )
     119             : {
     120             :     // expect "\$?[a-zA-Z]+\$?[1-9][0-9]*"
     121             :     static const sal_Unicode aDollar( '$' );
     122             :     static const sal_Unicode aLetterA( 'A' );
     123             : 
     124           0 :     ::rtl::OUString aCellStr = rXMLString.copy( nStartPos, nEndPos - nStartPos + 1 ).toAsciiUpperCase();
     125           0 :     const sal_Unicode* pStrArray = aCellStr.getStr();
     126           0 :     sal_Int32 nLength = aCellStr.getLength();
     127           0 :     sal_Int32 i = nLength - 1, nColumn = 0;
     128             : 
     129             :     // parse number for row
     130           0 :     while( CharClass::isAsciiDigit( pStrArray[ i ] ) && i >= 0 )
     131           0 :         i--;
     132           0 :     rOutCell.nRow = (aCellStr.copy( i + 1 )).toInt32() - 1;
     133             :     // a dollar in XML means absolute (whereas in UI it means relative)
     134           0 :     if( pStrArray[ i ] == aDollar )
     135             :     {
     136           0 :         i--;
     137           0 :         rOutCell.bRelativeRow = false;
     138             :     }
     139             :     else
     140           0 :         rOutCell.bRelativeRow = true;
     141             : 
     142             :     // parse rest for column
     143           0 :     sal_Int32 nPower = 1;
     144           0 :     while( CharClass::isAsciiAlpha( pStrArray[ i ] ))
     145             :     {
     146           0 :         nColumn += (pStrArray[ i ] - aLetterA + 1) * nPower;
     147           0 :         i--;
     148           0 :         nPower *= 26;
     149             :     }
     150           0 :     rOutCell.nColumn = nColumn - 1;
     151             : 
     152           0 :     rOutCell.bRelativeColumn = true;
     153           0 :     if( i >= 0 &&
     154           0 :         pStrArray[ i ] == aDollar )
     155           0 :         rOutCell.bRelativeColumn = false;
     156           0 :     rOutCell.bIsEmpty = false;
     157           0 : }
     158             : 
     159           0 : bool lcl_getCellAddressFromXMLString(
     160             :     const ::rtl::OUString& rXMLString,
     161             :     sal_Int32 nStartPos, sal_Int32 nEndPos,
     162             :     /*::chart::*/XMLRangeHelper::Cell & rOutCell,
     163             :     ::rtl::OUString& rOutTableName )
     164             : {
     165             :     static const sal_Unicode aDot( '.' );
     166             :     static const sal_Unicode aQuote( '\'' );
     167             :     static const sal_Unicode aBackslash( '\\' );
     168             : 
     169           0 :     sal_Int32 nNextDelimiterPos = nStartPos;
     170             : 
     171           0 :     sal_Int32 nDelimiterPos = nStartPos;
     172           0 :     bool bInQuotation = false;
     173             :     // parse table name
     174           0 :     while( nDelimiterPos < nEndPos &&
     175           0 :            ( bInQuotation || rXMLString[ nDelimiterPos ] != aDot ))
     176             :     {
     177             :         // skip escaped characters (with backslash)
     178           0 :         if( rXMLString[ nDelimiterPos ] == aBackslash )
     179           0 :             ++nDelimiterPos;
     180             :         // toggle quotation mode when finding single quotes
     181           0 :         else if( rXMLString[ nDelimiterPos ] == aQuote )
     182           0 :             bInQuotation = ! bInQuotation;
     183             : 
     184           0 :         ++nDelimiterPos;
     185             :     }
     186             : 
     187           0 :     if( nDelimiterPos == -1 ||
     188             :         nDelimiterPos >= nEndPos )
     189             :     {
     190           0 :         return false;
     191             :     }
     192           0 :     if( nDelimiterPos > nStartPos )
     193             :     {
     194             :         // there is a table name before the address
     195             : 
     196           0 :         ::rtl::OUStringBuffer aTableNameBuffer;
     197           0 :         const sal_Unicode * pTableName = rXMLString.getStr();
     198             : 
     199             :         // remove escapes from table name
     200             :         ::std::for_each( pTableName + nStartPos,
     201             :                          pTableName + nDelimiterPos,
     202           0 :                          lcl_UnEscape( aTableNameBuffer ));
     203             : 
     204             :         // unquote quoted table name
     205           0 :         const sal_Unicode * pBuf = aTableNameBuffer.getStr();
     206           0 :         if( pBuf[ 0 ] == aQuote &&
     207           0 :             pBuf[ aTableNameBuffer.getLength() - 1 ] == aQuote )
     208             :         {
     209           0 :             ::rtl::OUString aName = aTableNameBuffer.makeStringAndClear();
     210           0 :             rOutTableName = aName.copy( 1, aName.getLength() - 2 );
     211             :         }
     212             :         else
     213           0 :             rOutTableName = aTableNameBuffer.makeStringAndClear();
     214             :     }
     215             : 
     216           0 :     for( sal_Int32 i = 0;
     217             :          nNextDelimiterPos < nEndPos;
     218             :          nDelimiterPos = nNextDelimiterPos, i++ )
     219             :     {
     220           0 :         nNextDelimiterPos = rXMLString.indexOf( aDot, nDelimiterPos + 1 );
     221           0 :         if( nNextDelimiterPos == -1 ||
     222             :             nNextDelimiterPos > nEndPos )
     223           0 :             nNextDelimiterPos = nEndPos + 1;
     224             : 
     225           0 :         if( i==0 )
     226             :             // only take first cell
     227             :             lcl_getSingleCellAddressFromXMLString(
     228           0 :                 rXMLString, nDelimiterPos + 1, nNextDelimiterPos - 1, rOutCell );
     229             :     }
     230             : 
     231           0 :     return true;
     232             : }
     233             : 
     234           0 : bool lcl_getCellRangeAddressFromXMLString(
     235             :     const ::rtl::OUString& rXMLString,
     236             :     sal_Int32 nStartPos, sal_Int32 nEndPos,
     237             :     /*::chart::*/XMLRangeHelper::CellRange & rOutRange )
     238             : {
     239           0 :     bool bResult = true;
     240             :     static const sal_Unicode aColon( ':' );
     241             :     static const sal_Unicode aQuote( '\'' );
     242             :     static const sal_Unicode aBackslash( '\\' );
     243             : 
     244           0 :     sal_Int32 nDelimiterPos = nStartPos;
     245           0 :     bool bInQuotation = false;
     246             :     // parse table name
     247           0 :     while( nDelimiterPos < nEndPos &&
     248           0 :            ( bInQuotation || rXMLString[ nDelimiterPos ] != aColon ))
     249             :     {
     250             :         // skip escaped characters (with backslash)
     251           0 :         if( rXMLString[ nDelimiterPos ] == aBackslash )
     252           0 :             ++nDelimiterPos;
     253             :         // toggle quotation mode when finding single quotes
     254           0 :         else if( rXMLString[ nDelimiterPos ] == aQuote )
     255           0 :             bInQuotation = ! bInQuotation;
     256             : 
     257           0 :         ++nDelimiterPos;
     258             :     }
     259             : 
     260           0 :     if( nDelimiterPos == nEndPos )
     261             :     {
     262             :         // only one cell
     263             :         bResult = lcl_getCellAddressFromXMLString( rXMLString, nStartPos, nEndPos,
     264             :                                                    rOutRange.aUpperLeft,
     265           0 :                                                    rOutRange.aTableName );
     266             :     }
     267             :     else
     268             :     {
     269             :         // range (separated by a colon)
     270             :         bResult = lcl_getCellAddressFromXMLString( rXMLString, nStartPos, nDelimiterPos - 1,
     271             :                                                    rOutRange.aUpperLeft,
     272           0 :                                                    rOutRange.aTableName );
     273           0 :         ::rtl::OUString sTableSecondName;
     274           0 :         if( bResult )
     275             :         {
     276             :             bResult = lcl_getCellAddressFromXMLString( rXMLString, nDelimiterPos + 1, nEndPos,
     277             :                                                        rOutRange.aLowerRight,
     278           0 :                                                        sTableSecondName );
     279             :         }
     280           0 :         if( bResult &&
     281           0 :             !sTableSecondName.isEmpty() &&
     282           0 :             ! sTableSecondName.equals( rOutRange.aTableName ))
     283           0 :             bResult = false;
     284             :     }
     285             : 
     286           0 :     return bResult;
     287             : }
     288             : 
     289             : } // anonymous namespace
     290             : 
     291             : // ================================================================================
     292             : 
     293             : //namespace chart
     294             : //{
     295             : namespace XMLRangeHelper
     296             : {
     297             : 
     298           0 : CellRange getCellRangeFromXMLString( const OUString & rXMLString )
     299             : {
     300             :     static const sal_Unicode aSpace( ' ' );
     301             :     static const sal_Unicode aQuote( '\'' );
     302             :     static const sal_Unicode aDollar( '$' );
     303             :     static const sal_Unicode aBackslash( '\\' );
     304             : 
     305           0 :     sal_Int32 nStartPos = 0;
     306           0 :     sal_Int32 nEndPos = nStartPos;
     307           0 :     const sal_Int32 nLength = rXMLString.getLength();
     308             : 
     309             :     // reset
     310           0 :     CellRange aResult;
     311             : 
     312             :     // iterate over different ranges
     313           0 :     for( sal_Int32 i = 0;
     314             :          nEndPos < nLength;
     315             :          nStartPos = ++nEndPos, i++ )
     316             :     {
     317             :         // find start point of next range
     318             : 
     319             :         // ignore leading '$'
     320           0 :         if( rXMLString[ nEndPos ] == aDollar)
     321           0 :             nEndPos++;
     322             : 
     323           0 :         bool bInQuotation = false;
     324             :         // parse range
     325           0 :         while( nEndPos < nLength &&
     326           0 :                ( bInQuotation || rXMLString[ nEndPos ] != aSpace ))
     327             :         {
     328             :             // skip escaped characters (with backslash)
     329           0 :             if( rXMLString[ nEndPos ] == aBackslash )
     330           0 :                 ++nEndPos;
     331             :             // toggle quotation mode when finding single quotes
     332           0 :             else if( rXMLString[ nEndPos ] == aQuote )
     333           0 :                 bInQuotation = ! bInQuotation;
     334             : 
     335           0 :             ++nEndPos;
     336             :         }
     337             : 
     338           0 :         if( ! lcl_getCellRangeAddressFromXMLString(
     339             :                 rXMLString,
     340             :                 nStartPos, nEndPos - 1,
     341           0 :                 aResult ))
     342             :         {
     343             :             // if an error occurred, bail out
     344           0 :             return CellRange();
     345             :         }
     346             :     }
     347             : 
     348           0 :     return aResult;
     349             : }
     350             : 
     351           0 : OUString getXMLStringFromCellRange( const CellRange & rRange )
     352             : {
     353             :     static const sal_Unicode aSpace( ' ' );
     354             :     static const sal_Unicode aQuote( '\'' );
     355             : 
     356           0 :     ::rtl::OUStringBuffer aBuffer;
     357             : 
     358           0 :     if( !rRange.aTableName.isEmpty())
     359             :     {
     360           0 :         bool bNeedsEscaping = ( rRange.aTableName.indexOf( aQuote ) > -1 );
     361           0 :         bool bNeedsQuoting = bNeedsEscaping || ( rRange.aTableName.indexOf( aSpace ) > -1 );
     362             : 
     363             :         // quote table name if it contains spaces or quotes
     364           0 :         if( bNeedsQuoting )
     365             :         {
     366             :             // leading quote
     367           0 :             aBuffer.append( aQuote );
     368             : 
     369             :             // escape existing quotes
     370           0 :             if( bNeedsEscaping )
     371             :             {
     372           0 :                 const sal_Unicode * pTableNameBeg = rRange.aTableName.getStr();
     373             : 
     374             :                 // append the quoted string at the buffer
     375             :                 ::std::for_each( pTableNameBeg,
     376           0 :                                  pTableNameBeg + rRange.aTableName.getLength(),
     377           0 :                                  lcl_Escape( aBuffer ) );
     378             :             }
     379             :             else
     380           0 :                 aBuffer.append( rRange.aTableName );
     381             : 
     382             :             // final quote
     383           0 :             aBuffer.append( aQuote );
     384             :         }
     385             :         else
     386           0 :             aBuffer.append( rRange.aTableName );
     387             :     }
     388           0 :     lcl_getXMLStringForCell( rRange.aUpperLeft, &aBuffer );
     389             : 
     390           0 :     if( ! rRange.aLowerRight.empty())
     391             :     {
     392             :         // we have a range (not a single cell)
     393           0 :         aBuffer.append( sal_Unicode( ':' ));
     394           0 :         lcl_getXMLStringForCell( rRange.aLowerRight, &aBuffer );
     395             :     }
     396             : 
     397           0 :     return aBuffer.makeStringAndClear();
     398             : }
     399             : 
     400             : } //  namespace XMLRangeHelper
     401             : //} //  namespace chart
     402             : 
     403             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10