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