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
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <XMLRangeHelper.hxx>
#include <rtl/character.hxx>
#include <rtl/ustrbuf.hxx>
#include <osl/diagnose.h>
#include <o3tl/string_view.hxx>
#include <algorithm>
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:
explicit lcl_Escape( OUStringBuffer & aResultBuffer ) : m_aResultBuffer( aResultBuffer ) {}
void operator() ( sal_Unicode aChar )
{
static const sal_Unicode s_aQuote( '\'' );
static const sal_Unicode s_aBackslash( '\\' );
if( aChar == s_aQuote ||
aChar == s_aBackslash )
m_aResultBuffer.append( s_aBackslash );
m_aResultBuffer.append( aChar );
}
private:
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:
explicit lcl_UnEscape( OUStringBuffer & aResultBuffer ) : m_aResultBuffer( aResultBuffer ) {}
void operator() ( sal_Unicode aChar )
{
static const sal_Unicode s_aBackslash( '\\' );
if( aChar != s_aBackslash )
m_aResultBuffer.append( aChar );
}
private:
OUStringBuffer & m_aResultBuffer;
};
void lcl_getXMLStringForCell( const ::chart::XMLRangeHelper::Cell & rCell, OUStringBuffer * output )
{
OSL_ASSERT(output != nullptr);
if( rCell.empty())
return;
sal_Int32 nCol = rCell.nColumn;
output->append( '.' );
if( ! rCell.bRelativeColumn )
output->append( '$' );
// get A, B, C, ..., AA, AB, ... representation of column number
if( nCol < 26 )
output->append( static_cast<sal_Unicode>('A' + nCol) );
else if( nCol < 702 )
{
output->append( static_cast<sal_Unicode>('A' + nCol / 26 - 1 ));
output->append( static_cast<sal_Unicode>('A' + nCol % 26) );
}
else // works for nCol <= 18,278
{
output->append( static_cast<sal_Unicode>('A' + nCol / 702 - 1 ));
output->append( static_cast<sal_Unicode>('A' + (nCol % 702) / 26 ));
output->append( static_cast<sal_Unicode>('A' + nCol % 26) );
}
// write row number as number
if( ! rCell.bRelativeRow )
output->append( '$' );
output->append( rCell.nRow + sal_Int32(1) );
}
void lcl_getSingleCellAddressFromXMLString(
std::u16string_view 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' );
OUString aCellStr = OUString(rXMLString.substr( 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( rtl::isAsciiDigit( pStrArray[ i ] ) && i >= 0 )
i--;
rOutCell.nRow = (o3tl::toInt32(aCellStr.subView( i + 1 ))) - 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( rtl::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 OUString& rXMLString,
sal_Int32 nStartPos, sal_Int32 nEndPos,
::chart::XMLRangeHelper::Cell & rOutCell,
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
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 )
{
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 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.isEmpty() )
bResult = false;
}
else
{
// range (separated by a colon)
bResult = lcl_getCellAddressFromXMLString( rXMLString, nStartPos, nDelimiterPos - 1,
rOutRange.aUpperLeft,
rOutRange.aTableName );
if( rOutRange.aTableName.isEmpty() )
bResult = false;
OUString sTableSecondName;
if( bResult )
{
bResult = lcl_getCellAddressFromXMLString( rXMLString, nDelimiterPos + 1, nEndPos,
rOutRange.aLowerRight,
sTableSecondName );
}
if( bResult &&
!sTableSecondName.isEmpty() &&
sTableSecondName != rOutRange.aTableName )
bResult = false;
}
return bResult;
}
} // anonymous namespace
namespace chart::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( '\\' );
const sal_Int32 nLength = rXMLString.getLength();
// reset
CellRange aResult;
// iterate over different ranges
for( sal_Int32 nStartPos = 0, nEndPos = nStartPos;
nEndPos < nLength;
nStartPos = ++nEndPos )
{
// 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 occurred, bail out
return CellRange();
}
}
return aResult;
}
OUString getXMLStringFromCellRange( const CellRange & rRange )
{
static const sal_Unicode aSpace( ' ' );
static const sal_Unicode aQuote( '\'' );
OUStringBuffer aBuffer;
if( !rRange.aTableName.isEmpty())
{
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 );
}
lcl_getXMLStringForCell( rRange.aUpperLeft, &aBuffer );
if( ! rRange.aLowerRight.empty())
{
// we have a range (not a single cell)
aBuffer.append( u':');
lcl_getXMLStringForCell( rRange.aLowerRight, &aBuffer );
}
return aBuffer.makeStringAndClear();
}
} // namespace chart::XMLRangeHelper
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|