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