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