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

Generated by: LCOV version 1.10