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 <comphelper/string.hxx>
21 : #include <unotools/syslocale.hxx>
22 :
23 : #include <svl/zforlist.hxx>
24 :
25 : #include <svl/zformat.hxx>
26 : #include <svl/numuno.hxx>
27 : #include <rtl/math.hxx>
28 : #include <i18nlangtag/languagetag.hxx>
29 : #include <tools/color.hxx>
30 : #include <tools/debug.hxx>
31 : #include <rtl/ustrbuf.hxx>
32 :
33 : #include <sax/tools/converter.hxx>
34 :
35 : #include <xmloff/xmlnumfi.hxx>
36 : #include <xmloff/xmltkmap.hxx>
37 : #include <xmloff/xmlnmspe.hxx>
38 : #include <xmloff/xmlictxt.hxx>
39 : #include <xmloff/xmlimp.hxx>
40 : #include <xmloff/xmluconv.hxx>
41 : #include <xmloff/nmspmap.hxx>
42 : #include <xmloff/families.hxx>
43 : #include <xmloff/xmltoken.hxx>
44 : #include <xmloff/languagetagodf.hxx>
45 :
46 : #include <boost/ptr_container/ptr_vector.hpp>
47 : #include <boost/ptr_container/ptr_set.hpp>
48 :
49 : using namespace ::com::sun::star;
50 : using namespace ::xmloff::token;
51 :
52 1085 : struct SvXMLNumFmtEntry
53 : {
54 : OUString aName;
55 : sal_uInt32 nKey;
56 : bool bRemoveAfterUse;
57 :
58 1085 : SvXMLNumFmtEntry( const OUString& rN, sal_uInt32 nK, bool bR ) :
59 1085 : aName(rN), nKey(nK), bRemoveAfterUse(bR) {}
60 : };
61 :
62 : typedef ::boost::ptr_vector<SvXMLNumFmtEntry> SvXMLNumFmtEntryArr;
63 :
64 0 : struct SvXMLEmbeddedElement
65 : {
66 : sal_Int32 nFormatPos;
67 : OUString aText;
68 :
69 0 : SvXMLEmbeddedElement( sal_Int32 nFP, const OUString& rT ) :
70 0 : nFormatPos(nFP), aText(rT) {}
71 :
72 0 : bool operator < ( const SvXMLEmbeddedElement& r ) const { return nFormatPos < r.nFormatPos; }
73 : };
74 :
75 : typedef boost::ptr_set<SvXMLEmbeddedElement> SvXMLEmbeddedElementArr;
76 :
77 : class SvXMLNumImpData
78 : {
79 : SvNumberFormatter* pFormatter;
80 : SvXMLTokenMap* pStylesElemTokenMap;
81 : SvXMLTokenMap* pStyleElemTokenMap;
82 : SvXMLTokenMap* pStyleAttrTokenMap;
83 : SvXMLTokenMap* pStyleElemAttrTokenMap;
84 : LocaleDataWrapper* pLocaleData;
85 : SvXMLNumFmtEntryArr aNameEntries;
86 :
87 : uno::Reference< uno::XComponentContext > m_xContext;
88 :
89 : public:
90 : SvXMLNumImpData(
91 : SvNumberFormatter* pFmt,
92 : const uno::Reference<uno::XComponentContext>& rxContext );
93 : ~SvXMLNumImpData();
94 :
95 2963 : SvNumberFormatter* GetNumberFormatter() const { return pFormatter; }
96 : const SvXMLTokenMap& GetStylesElemTokenMap();
97 : const SvXMLTokenMap& GetStyleElemTokenMap();
98 : const SvXMLTokenMap& GetStyleAttrTokenMap();
99 : const SvXMLTokenMap& GetStyleElemAttrTokenMap();
100 : const LocaleDataWrapper& GetLocaleData( LanguageType nLang );
101 : sal_uInt32 GetKeyForName( const OUString& rName );
102 : void AddKey( sal_uInt32 nKey, const OUString& rName, bool bRemoveAfterUse );
103 : void SetUsed( sal_uInt32 nKey );
104 : void RemoveVolatileFormats();
105 : };
106 :
107 2815 : struct SvXMLNumberInfo
108 : {
109 : sal_Int32 nDecimals;
110 : sal_Int32 nInteger;
111 : sal_Int32 nExpDigits;
112 : sal_Int32 nNumerDigits;
113 : sal_Int32 nDenomDigits;
114 : sal_Int32 nFracDenominator;
115 : bool bGrouping;
116 : bool bDecReplace;
117 : bool bVarDecimals;
118 : double fDisplayFactor;
119 : SvXMLEmbeddedElementArr aEmbeddedElements;
120 :
121 2815 : SvXMLNumberInfo()
122 2815 : {
123 2815 : nDecimals = nInteger = nExpDigits = nNumerDigits = nDenomDigits = nFracDenominator = -1;
124 2815 : bGrouping = bDecReplace = bVarDecimals = false;
125 2815 : fDisplayFactor = 1.0;
126 2815 : }
127 : };
128 :
129 : class SvXMLNumFmtElementContext : public SvXMLImportContext
130 : {
131 : SvXMLNumFormatContext& rParent;
132 : sal_uInt16 nType;
133 : OUStringBuffer aContent;
134 : SvXMLNumberInfo aNumInfo;
135 : LanguageType nElementLang;
136 : bool bLong;
137 : bool bTextual;
138 : OUString sCalendar;
139 :
140 : public:
141 : SvXMLNumFmtElementContext( SvXMLImport& rImport, sal_uInt16 nPrfx,
142 : const OUString& rLName,
143 : SvXMLNumFormatContext& rParentContext, sal_uInt16 nNewType,
144 : const ::com::sun::star::uno::Reference<
145 : ::com::sun::star::xml::sax::XAttributeList>& xAttrList );
146 : virtual ~SvXMLNumFmtElementContext();
147 :
148 : virtual SvXMLImportContext *CreateChildContext( sal_uInt16 nPrefix,
149 : const OUString& rLocalName,
150 : const ::com::sun::star::uno::Reference<
151 : ::com::sun::star::xml::sax::XAttributeList>& xAttrList ) SAL_OVERRIDE;
152 : virtual void Characters( const OUString& rChars ) SAL_OVERRIDE;
153 : virtual void EndElement() SAL_OVERRIDE;
154 :
155 : void AddEmbeddedElement( sal_Int32 nFormatPos, const OUString& rContent );
156 : };
157 :
158 : class SvXMLNumFmtEmbeddedTextContext : public SvXMLImportContext
159 : {
160 : SvXMLNumFmtElementContext& rParent;
161 : OUStringBuffer aContent;
162 : sal_Int32 nTextPosition;
163 :
164 : public:
165 : SvXMLNumFmtEmbeddedTextContext( SvXMLImport& rImport, sal_uInt16 nPrfx,
166 : const OUString& rLName,
167 : SvXMLNumFmtElementContext& rParentContext,
168 : const ::com::sun::star::uno::Reference<
169 : ::com::sun::star::xml::sax::XAttributeList>& xAttrList );
170 : virtual ~SvXMLNumFmtEmbeddedTextContext();
171 :
172 : virtual SvXMLImportContext *CreateChildContext( sal_uInt16 nPrefix,
173 : const OUString& rLocalName,
174 : const ::com::sun::star::uno::Reference<
175 : ::com::sun::star::xml::sax::XAttributeList>& xAttrList ) SAL_OVERRIDE;
176 : virtual void Characters( const OUString& rChars ) SAL_OVERRIDE;
177 : virtual void EndElement() SAL_OVERRIDE;
178 : };
179 :
180 : class SvXMLNumFmtMapContext : public SvXMLImportContext
181 : {
182 : SvXMLNumFormatContext& rParent;
183 : OUString sCondition;
184 : OUString sName;
185 :
186 : public:
187 : SvXMLNumFmtMapContext( SvXMLImport& rImport, sal_uInt16 nPrfx,
188 : const OUString& rLName,
189 : SvXMLNumFormatContext& rParentContext,
190 : const ::com::sun::star::uno::Reference<
191 : ::com::sun::star::xml::sax::XAttributeList>& xAttrList );
192 : virtual ~SvXMLNumFmtMapContext();
193 :
194 : virtual SvXMLImportContext *CreateChildContext( sal_uInt16 nPrefix,
195 : const OUString& rLocalName,
196 : const ::com::sun::star::uno::Reference<
197 : ::com::sun::star::xml::sax::XAttributeList>& xAttrList ) SAL_OVERRIDE;
198 : virtual void Characters( const OUString& rChars ) SAL_OVERRIDE;
199 : virtual void EndElement() SAL_OVERRIDE;
200 : };
201 :
202 : class SvXMLNumFmtPropContext : public SvXMLImportContext
203 : {
204 : SvXMLNumFormatContext& rParent;
205 : sal_Int32 m_nColor;
206 : bool bColSet;
207 :
208 : public:
209 : SvXMLNumFmtPropContext( SvXMLImport& rImport, sal_uInt16 nPrfx,
210 : const OUString& rLName,
211 : SvXMLNumFormatContext& rParentContext,
212 : const ::com::sun::star::uno::Reference<
213 : ::com::sun::star::xml::sax::XAttributeList>& xAttrList );
214 : virtual ~SvXMLNumFmtPropContext();
215 :
216 : virtual SvXMLImportContext *CreateChildContext( sal_uInt16 nPrefix,
217 : const OUString& rLocalName,
218 : const ::com::sun::star::uno::Reference<
219 : ::com::sun::star::xml::sax::XAttributeList>& xAttrList ) SAL_OVERRIDE;
220 : virtual void Characters( const OUString& rChars ) SAL_OVERRIDE;
221 : virtual void EndElement() SAL_OVERRIDE;
222 : };
223 :
224 : enum SvXMLStyleTokens
225 : {
226 : XML_TOK_STYLE_TEXT,
227 : XML_TOK_STYLE_FILL_CHARACTER,
228 : XML_TOK_STYLE_NUMBER,
229 : XML_TOK_STYLE_SCIENTIFIC_NUMBER,
230 : XML_TOK_STYLE_FRACTION,
231 : XML_TOK_STYLE_CURRENCY_SYMBOL,
232 : XML_TOK_STYLE_DAY,
233 : XML_TOK_STYLE_MONTH,
234 : XML_TOK_STYLE_YEAR,
235 : XML_TOK_STYLE_ERA,
236 : XML_TOK_STYLE_DAY_OF_WEEK,
237 : XML_TOK_STYLE_WEEK_OF_YEAR,
238 : XML_TOK_STYLE_QUARTER,
239 : XML_TOK_STYLE_HOURS,
240 : XML_TOK_STYLE_AM_PM,
241 : XML_TOK_STYLE_MINUTES,
242 : XML_TOK_STYLE_SECONDS,
243 : XML_TOK_STYLE_BOOLEAN,
244 : XML_TOK_STYLE_TEXT_CONTENT,
245 : XML_TOK_STYLE_PROPERTIES,
246 : XML_TOK_STYLE_MAP
247 : };
248 :
249 : enum SvXMLStyleAttrTokens
250 : {
251 : XML_TOK_STYLE_ATTR_NAME,
252 : XML_TOK_STYLE_ATTR_RFC_LANGUAGE_TAG,
253 : XML_TOK_STYLE_ATTR_LANGUAGE,
254 : XML_TOK_STYLE_ATTR_SCRIPT,
255 : XML_TOK_STYLE_ATTR_COUNTRY,
256 : XML_TOK_STYLE_ATTR_TITLE,
257 : XML_TOK_STYLE_ATTR_AUTOMATIC_ORDER,
258 : XML_TOK_STYLE_ATTR_FORMAT_SOURCE,
259 : XML_TOK_STYLE_ATTR_TRUNCATE_ON_OVERFLOW,
260 : XML_TOK_STYLE_ATTR_VOLATILE,
261 : XML_TOK_STYLE_ATTR_TRANSL_FORMAT,
262 : XML_TOK_STYLE_ATTR_TRANSL_RFC_LANGUAGE_TAG,
263 : XML_TOK_STYLE_ATTR_TRANSL_LANGUAGE,
264 : XML_TOK_STYLE_ATTR_TRANSL_SCRIPT,
265 : XML_TOK_STYLE_ATTR_TRANSL_COUNTRY,
266 : XML_TOK_STYLE_ATTR_TRANSL_STYLE
267 : };
268 :
269 : enum SvXMLStyleElemAttrTokens
270 : {
271 : XML_TOK_ELEM_ATTR_DECIMAL_PLACES,
272 : XML_TOK_ELEM_ATTR_MIN_INTEGER_DIGITS,
273 : XML_TOK_ELEM_ATTR_GROUPING,
274 : XML_TOK_ELEM_ATTR_DISPLAY_FACTOR,
275 : XML_TOK_ELEM_ATTR_DECIMAL_REPLACEMENT,
276 : XML_TOK_ELEM_ATTR_DENOMINATOR_VALUE,
277 : XML_TOK_ELEM_ATTR_MIN_EXPONENT_DIGITS,
278 : XML_TOK_ELEM_ATTR_MIN_NUMERATOR_DIGITS,
279 : XML_TOK_ELEM_ATTR_MIN_DENOMINATOR_DIGITS,
280 : XML_TOK_ELEM_ATTR_RFC_LANGUAGE_TAG,
281 : XML_TOK_ELEM_ATTR_LANGUAGE,
282 : XML_TOK_ELEM_ATTR_SCRIPT,
283 : XML_TOK_ELEM_ATTR_COUNTRY,
284 : XML_TOK_ELEM_ATTR_STYLE,
285 : XML_TOK_ELEM_ATTR_TEXTUAL,
286 : XML_TOK_ELEM_ATTR_CALENDAR
287 : };
288 :
289 :
290 : // standard colors
291 :
292 :
293 : #define XML_NUMF_COLORCOUNT 10
294 :
295 : static const ColorData aNumFmtStdColors[XML_NUMF_COLORCOUNT] =
296 : {
297 : COL_BLACK,
298 : COL_LIGHTBLUE,
299 : COL_LIGHTGREEN,
300 : COL_LIGHTCYAN,
301 : COL_LIGHTRED,
302 : COL_LIGHTMAGENTA,
303 : COL_BROWN,
304 : COL_GRAY,
305 : COL_YELLOW,
306 : COL_WHITE
307 : };
308 :
309 :
310 : // token maps
311 :
312 :
313 : // maps for SvXMLUnitConverter::convertEnum
314 :
315 : static const SvXMLEnumMapEntry aStyleValueMap[] =
316 : {
317 : { XML_SHORT, sal_False },
318 : { XML_LONG, sal_True },
319 : { XML_TOKEN_INVALID, 0 }
320 : };
321 :
322 : static const SvXMLEnumMapEntry aFormatSourceMap[] =
323 : {
324 : { XML_FIXED, sal_False },
325 : { XML_LANGUAGE, sal_True },
326 : { XML_TOKEN_INVALID, 0 }
327 : };
328 :
329 : struct SvXMLDefaultDateFormat
330 : {
331 : NfIndexTableOffset eFormat;
332 : SvXMLDateElementAttributes eDOW;
333 : SvXMLDateElementAttributes eDay;
334 : SvXMLDateElementAttributes eMonth;
335 : SvXMLDateElementAttributes eYear;
336 : SvXMLDateElementAttributes eHours;
337 : SvXMLDateElementAttributes eMins;
338 : SvXMLDateElementAttributes eSecs;
339 : bool bSystem;
340 : };
341 :
342 : static const SvXMLDefaultDateFormat aDefaultDateFormats[] =
343 : {
344 : // format day-of-week day month year hours minutes seconds format-source
345 :
346 : { NF_DATE_SYSTEM_SHORT, XML_DEA_NONE, XML_DEA_ANY, XML_DEA_ANY, XML_DEA_ANY, XML_DEA_NONE, XML_DEA_NONE, XML_DEA_NONE, true },
347 : { NF_DATE_SYSTEM_LONG, XML_DEA_ANY, XML_DEA_ANY, XML_DEA_ANY, XML_DEA_ANY, XML_DEA_NONE, XML_DEA_NONE, XML_DEA_NONE, true },
348 : { NF_DATE_SYS_MMYY, XML_DEA_NONE, XML_DEA_NONE, XML_DEA_LONG, XML_DEA_SHORT, XML_DEA_NONE, XML_DEA_NONE, XML_DEA_NONE, false },
349 : { NF_DATE_SYS_DDMMM, XML_DEA_NONE, XML_DEA_LONG, XML_DEA_TEXTSHORT, XML_DEA_NONE, XML_DEA_NONE, XML_DEA_NONE, XML_DEA_NONE, false },
350 : { NF_DATE_SYS_DDMMYYYY, XML_DEA_NONE, XML_DEA_LONG, XML_DEA_LONG, XML_DEA_LONG, XML_DEA_NONE, XML_DEA_NONE, XML_DEA_NONE, false },
351 : { NF_DATE_SYS_DDMMYY, XML_DEA_NONE, XML_DEA_LONG, XML_DEA_LONG, XML_DEA_SHORT, XML_DEA_NONE, XML_DEA_NONE, XML_DEA_NONE, false },
352 : { NF_DATE_SYS_DMMMYY, XML_DEA_NONE, XML_DEA_SHORT, XML_DEA_TEXTSHORT, XML_DEA_SHORT, XML_DEA_NONE, XML_DEA_NONE, XML_DEA_NONE, false },
353 : { NF_DATE_SYS_DMMMYYYY, XML_DEA_NONE, XML_DEA_SHORT, XML_DEA_TEXTSHORT, XML_DEA_LONG, XML_DEA_NONE, XML_DEA_NONE, XML_DEA_NONE, false },
354 : { NF_DATE_SYS_DMMMMYYYY, XML_DEA_NONE, XML_DEA_SHORT, XML_DEA_TEXTLONG, XML_DEA_LONG, XML_DEA_NONE, XML_DEA_NONE, XML_DEA_NONE, false },
355 : { NF_DATE_SYS_NNDMMMYY, XML_DEA_SHORT, XML_DEA_SHORT, XML_DEA_TEXTSHORT, XML_DEA_SHORT, XML_DEA_NONE, XML_DEA_NONE, XML_DEA_NONE, false },
356 : { NF_DATE_SYS_NNDMMMMYYYY, XML_DEA_SHORT, XML_DEA_SHORT, XML_DEA_TEXTLONG, XML_DEA_LONG, XML_DEA_NONE, XML_DEA_NONE, XML_DEA_NONE, false },
357 : { NF_DATE_SYS_NNNNDMMMMYYYY, XML_DEA_LONG, XML_DEA_SHORT, XML_DEA_TEXTLONG, XML_DEA_LONG, XML_DEA_NONE, XML_DEA_NONE, XML_DEA_NONE, false },
358 : { NF_DATETIME_SYSTEM_SHORT_HHMM, XML_DEA_NONE, XML_DEA_ANY, XML_DEA_ANY, XML_DEA_ANY, XML_DEA_ANY, XML_DEA_ANY, XML_DEA_NONE, true },
359 : { NF_DATETIME_SYS_DDMMYYYY_HHMMSS, XML_DEA_NONE, XML_DEA_ANY, XML_DEA_ANY, XML_DEA_ANY, XML_DEA_ANY, XML_DEA_ANY, XML_DEA_ANY, false }
360 : };
361 :
362 :
363 : // SvXMLNumImpData
364 :
365 :
366 646 : SvXMLNumImpData::SvXMLNumImpData(
367 : SvNumberFormatter* pFmt,
368 : const uno::Reference<uno::XComponentContext>& rxContext )
369 : : pFormatter(pFmt),
370 : pStylesElemTokenMap(NULL),
371 : pStyleElemTokenMap(NULL),
372 : pStyleAttrTokenMap(NULL),
373 : pStyleElemAttrTokenMap(NULL),
374 : pLocaleData(NULL),
375 646 : m_xContext(rxContext)
376 : {
377 : DBG_ASSERT( rxContext.is(), "got no service manager" );
378 646 : }
379 :
380 1292 : SvXMLNumImpData::~SvXMLNumImpData()
381 : {
382 646 : delete pStylesElemTokenMap;
383 646 : delete pStyleElemTokenMap;
384 646 : delete pStyleAttrTokenMap;
385 646 : delete pStyleElemAttrTokenMap;
386 646 : delete pLocaleData;
387 646 : }
388 :
389 346 : sal_uInt32 SvXMLNumImpData::GetKeyForName( const OUString& rName )
390 : {
391 346 : sal_uInt16 nCount = aNameEntries.size();
392 10451 : for (sal_uInt16 i=0; i<nCount; i++)
393 : {
394 10451 : const SvXMLNumFmtEntry* pObj = &aNameEntries[i];
395 10451 : if ( pObj->aName == rName )
396 346 : return pObj->nKey; // found
397 : }
398 0 : return NUMBERFORMAT_ENTRY_NOT_FOUND;
399 : }
400 :
401 1085 : void SvXMLNumImpData::AddKey( sal_uInt32 nKey, const OUString& rName, bool bRemoveAfterUse )
402 : {
403 1085 : if ( bRemoveAfterUse )
404 : {
405 : // if there is already an entry for this key without the bRemoveAfterUse flag,
406 : // clear the flag for this entry, too
407 :
408 346 : sal_uInt16 nCount = aNameEntries.size();
409 10451 : for (sal_uInt16 i=0; i<nCount; i++)
410 : {
411 10105 : SvXMLNumFmtEntry* pObj = &aNameEntries[i];
412 10105 : if ( pObj->nKey == nKey && !pObj->bRemoveAfterUse )
413 : {
414 0 : bRemoveAfterUse = false; // clear flag for new entry
415 0 : break;
416 : }
417 : }
418 : }
419 : else
420 : {
421 : // call SetUsed to clear the bRemoveAfterUse flag for other entries for this key
422 739 : SetUsed( nKey );
423 : }
424 :
425 1085 : SvXMLNumFmtEntry* pObj = new SvXMLNumFmtEntry( rName, nKey, bRemoveAfterUse );
426 1085 : aNameEntries.push_back( pObj );
427 1085 : }
428 :
429 739 : void SvXMLNumImpData::SetUsed( sal_uInt32 nKey )
430 : {
431 739 : sal_uInt16 nCount = aNameEntries.size();
432 12162 : for (sal_uInt16 i=0; i<nCount; i++)
433 : {
434 11423 : SvXMLNumFmtEntry* pObj = &aNameEntries[i];
435 11423 : if ( pObj->nKey == nKey )
436 : {
437 4 : pObj->bRemoveAfterUse = false; // used -> don't remove
438 :
439 : // continue searching - there may be several entries for the same key
440 : // (with different names), the format must not be deleted if any one of
441 : // them is used
442 : }
443 : }
444 739 : }
445 :
446 646 : void SvXMLNumImpData::RemoveVolatileFormats()
447 : {
448 : // remove temporary (volatile) formats from NumberFormatter
449 : // called at the end of each import (styles and content), so volatile formats
450 : // from styles can't be used in content
451 :
452 646 : if ( !pFormatter )
453 646 : return;
454 :
455 646 : sal_uInt16 nCount = aNameEntries.size();
456 1731 : for (sal_uInt16 i=0; i<nCount; i++)
457 : {
458 1085 : const SvXMLNumFmtEntry* pObj = &aNameEntries[i];
459 1085 : if ( pObj->bRemoveAfterUse )
460 : {
461 346 : const SvNumberformat* pFormat = pFormatter->GetEntry(pObj->nKey);
462 346 : if (pFormat && (pFormat->GetType() & NUMBERFORMAT_DEFINED))
463 296 : pFormatter->DeleteEntry( pObj->nKey );
464 : }
465 : }
466 : }
467 :
468 9712 : const SvXMLTokenMap& SvXMLNumImpData::GetStylesElemTokenMap()
469 : {
470 9712 : if( !pStylesElemTokenMap )
471 : {
472 : static const SvXMLTokenMapEntry aStylesElemMap[] =
473 : {
474 : // style elements
475 : { XML_NAMESPACE_NUMBER, XML_NUMBER_STYLE, XML_TOK_STYLES_NUMBER_STYLE },
476 : { XML_NAMESPACE_NUMBER, XML_CURRENCY_STYLE, XML_TOK_STYLES_CURRENCY_STYLE },
477 : { XML_NAMESPACE_NUMBER, XML_PERCENTAGE_STYLE, XML_TOK_STYLES_PERCENTAGE_STYLE },
478 : { XML_NAMESPACE_NUMBER, XML_DATE_STYLE, XML_TOK_STYLES_DATE_STYLE },
479 : { XML_NAMESPACE_NUMBER, XML_TIME_STYLE, XML_TOK_STYLES_TIME_STYLE },
480 : { XML_NAMESPACE_NUMBER, XML_BOOLEAN_STYLE, XML_TOK_STYLES_BOOLEAN_STYLE },
481 : { XML_NAMESPACE_NUMBER, XML_TEXT_STYLE, XML_TOK_STYLES_TEXT_STYLE },
482 : XML_TOKEN_MAP_END
483 : };
484 :
485 628 : pStylesElemTokenMap = new SvXMLTokenMap( aStylesElemMap );
486 : }
487 9712 : return *pStylesElemTokenMap;
488 : }
489 :
490 3340 : const SvXMLTokenMap& SvXMLNumImpData::GetStyleElemTokenMap()
491 : {
492 3340 : if( !pStyleElemTokenMap )
493 : {
494 : static const SvXMLTokenMapEntry aStyleElemMap[] =
495 : {
496 : // elements in a style
497 : { XML_NAMESPACE_NUMBER, XML_TEXT, XML_TOK_STYLE_TEXT },
498 : { XML_NAMESPACE_LO_EXT, XML_FILL_CHARACTER, XML_TOK_STYLE_FILL_CHARACTER },
499 : { XML_NAMESPACE_NUMBER, XML_FILL_CHARACTER, XML_TOK_STYLE_FILL_CHARACTER },
500 : { XML_NAMESPACE_NUMBER, XML_NUMBER, XML_TOK_STYLE_NUMBER },
501 : { XML_NAMESPACE_NUMBER, XML_SCIENTIFIC_NUMBER, XML_TOK_STYLE_SCIENTIFIC_NUMBER },
502 : { XML_NAMESPACE_NUMBER, XML_FRACTION, XML_TOK_STYLE_FRACTION },
503 : { XML_NAMESPACE_NUMBER, XML_CURRENCY_SYMBOL, XML_TOK_STYLE_CURRENCY_SYMBOL },
504 : { XML_NAMESPACE_NUMBER, XML_DAY, XML_TOK_STYLE_DAY },
505 : { XML_NAMESPACE_NUMBER, XML_MONTH, XML_TOK_STYLE_MONTH },
506 : { XML_NAMESPACE_NUMBER, XML_YEAR, XML_TOK_STYLE_YEAR },
507 : { XML_NAMESPACE_NUMBER, XML_ERA, XML_TOK_STYLE_ERA },
508 : { XML_NAMESPACE_NUMBER, XML_DAY_OF_WEEK, XML_TOK_STYLE_DAY_OF_WEEK },
509 : { XML_NAMESPACE_NUMBER, XML_WEEK_OF_YEAR, XML_TOK_STYLE_WEEK_OF_YEAR },
510 : { XML_NAMESPACE_NUMBER, XML_QUARTER, XML_TOK_STYLE_QUARTER },
511 : { XML_NAMESPACE_NUMBER, XML_HOURS, XML_TOK_STYLE_HOURS },
512 : { XML_NAMESPACE_NUMBER, XML_AM_PM, XML_TOK_STYLE_AM_PM },
513 : { XML_NAMESPACE_NUMBER, XML_MINUTES, XML_TOK_STYLE_MINUTES },
514 : { XML_NAMESPACE_NUMBER, XML_SECONDS, XML_TOK_STYLE_SECONDS },
515 : { XML_NAMESPACE_NUMBER, XML_BOOLEAN, XML_TOK_STYLE_BOOLEAN },
516 : { XML_NAMESPACE_NUMBER, XML_TEXT_CONTENT, XML_TOK_STYLE_TEXT_CONTENT },
517 : { XML_NAMESPACE_STYLE, XML_TEXT_PROPERTIES, XML_TOK_STYLE_PROPERTIES },
518 : { XML_NAMESPACE_STYLE, XML_MAP, XML_TOK_STYLE_MAP },
519 : XML_TOKEN_MAP_END
520 : };
521 :
522 209 : pStyleElemTokenMap = new SvXMLTokenMap( aStyleElemMap );
523 : }
524 3340 : return *pStyleElemTokenMap;
525 : }
526 :
527 2104 : const SvXMLTokenMap& SvXMLNumImpData::GetStyleAttrTokenMap()
528 : {
529 2104 : if( !pStyleAttrTokenMap )
530 : {
531 : static const SvXMLTokenMapEntry aStyleAttrMap[] =
532 : {
533 : // attributes for a style
534 : { XML_NAMESPACE_STYLE, XML_NAME, XML_TOK_STYLE_ATTR_NAME },
535 : { XML_NAMESPACE_NUMBER, XML_RFC_LANGUAGE_TAG, XML_TOK_STYLE_ATTR_RFC_LANGUAGE_TAG, },
536 : { XML_NAMESPACE_NUMBER, XML_LANGUAGE, XML_TOK_STYLE_ATTR_LANGUAGE },
537 : { XML_NAMESPACE_NUMBER, XML_SCRIPT, XML_TOK_STYLE_ATTR_SCRIPT },
538 : { XML_NAMESPACE_NUMBER, XML_COUNTRY, XML_TOK_STYLE_ATTR_COUNTRY },
539 : { XML_NAMESPACE_NUMBER, XML_TITLE, XML_TOK_STYLE_ATTR_TITLE },
540 : { XML_NAMESPACE_NUMBER, XML_AUTOMATIC_ORDER, XML_TOK_STYLE_ATTR_AUTOMATIC_ORDER },
541 : { XML_NAMESPACE_NUMBER, XML_FORMAT_SOURCE, XML_TOK_STYLE_ATTR_FORMAT_SOURCE },
542 : { XML_NAMESPACE_NUMBER, XML_TRUNCATE_ON_OVERFLOW, XML_TOK_STYLE_ATTR_TRUNCATE_ON_OVERFLOW },
543 : { XML_NAMESPACE_STYLE, XML_VOLATILE, XML_TOK_STYLE_ATTR_VOLATILE },
544 : { XML_NAMESPACE_NUMBER, XML_TRANSLITERATION_FORMAT, XML_TOK_STYLE_ATTR_TRANSL_FORMAT },
545 : // not defined in ODF { XML_NAMESPACE_NUMBER, XML_TRANSLITERATION_RFC_LANGUAGE_TAG, XML_TOK_STYLE_ATTR_TRANSL_RFC_LANGUAGE_TAG },
546 : { XML_NAMESPACE_NUMBER, XML_TRANSLITERATION_LANGUAGE, XML_TOK_STYLE_ATTR_TRANSL_LANGUAGE },
547 : // not defined in ODF { XML_NAMESPACE_NUMBER, XML_TRANSLITERATION_SCRIPT, XML_TOK_STYLE_ATTR_TRANSL_SCRIPT },
548 : { XML_NAMESPACE_NUMBER, XML_TRANSLITERATION_COUNTRY, XML_TOK_STYLE_ATTR_TRANSL_COUNTRY },
549 : { XML_NAMESPACE_NUMBER, XML_TRANSLITERATION_STYLE, XML_TOK_STYLE_ATTR_TRANSL_STYLE },
550 : XML_TOKEN_MAP_END
551 : };
552 :
553 209 : pStyleAttrTokenMap = new SvXMLTokenMap( aStyleAttrMap );
554 : }
555 2104 : return *pStyleAttrTokenMap;
556 : }
557 :
558 2860 : const SvXMLTokenMap& SvXMLNumImpData::GetStyleElemAttrTokenMap()
559 : {
560 2860 : if( !pStyleElemAttrTokenMap )
561 : {
562 : static const SvXMLTokenMapEntry aStyleElemAttrMap[] =
563 : {
564 : // attributes for an element within a style
565 : { XML_NAMESPACE_NUMBER, XML_DECIMAL_PLACES, XML_TOK_ELEM_ATTR_DECIMAL_PLACES },
566 : { XML_NAMESPACE_NUMBER, XML_MIN_INTEGER_DIGITS, XML_TOK_ELEM_ATTR_MIN_INTEGER_DIGITS },
567 : { XML_NAMESPACE_NUMBER, XML_GROUPING, XML_TOK_ELEM_ATTR_GROUPING },
568 : { XML_NAMESPACE_NUMBER, XML_DISPLAY_FACTOR, XML_TOK_ELEM_ATTR_DISPLAY_FACTOR },
569 : { XML_NAMESPACE_NUMBER, XML_DECIMAL_REPLACEMENT, XML_TOK_ELEM_ATTR_DECIMAL_REPLACEMENT },
570 : { XML_NAMESPACE_NUMBER, XML_DENOMINATOR_VALUE, XML_TOK_ELEM_ATTR_DENOMINATOR_VALUE },
571 : { XML_NAMESPACE_NUMBER, XML_MIN_EXPONENT_DIGITS, XML_TOK_ELEM_ATTR_MIN_EXPONENT_DIGITS },
572 : { XML_NAMESPACE_NUMBER, XML_MIN_NUMERATOR_DIGITS, XML_TOK_ELEM_ATTR_MIN_NUMERATOR_DIGITS },
573 : { XML_NAMESPACE_NUMBER, XML_MIN_DENOMINATOR_DIGITS, XML_TOK_ELEM_ATTR_MIN_DENOMINATOR_DIGITS },
574 : { XML_NAMESPACE_NUMBER, XML_RFC_LANGUAGE_TAG, XML_TOK_ELEM_ATTR_RFC_LANGUAGE_TAG },
575 : { XML_NAMESPACE_NUMBER, XML_LANGUAGE, XML_TOK_ELEM_ATTR_LANGUAGE },
576 : { XML_NAMESPACE_NUMBER, XML_SCRIPT, XML_TOK_ELEM_ATTR_SCRIPT },
577 : { XML_NAMESPACE_NUMBER, XML_COUNTRY, XML_TOK_ELEM_ATTR_COUNTRY },
578 : { XML_NAMESPACE_NUMBER, XML_STYLE, XML_TOK_ELEM_ATTR_STYLE },
579 : { XML_NAMESPACE_NUMBER, XML_TEXTUAL, XML_TOK_ELEM_ATTR_TEXTUAL },
580 : { XML_NAMESPACE_NUMBER, XML_CALENDAR, XML_TOK_ELEM_ATTR_CALENDAR },
581 : XML_TOKEN_MAP_END
582 : };
583 :
584 207 : pStyleElemAttrTokenMap = new SvXMLTokenMap( aStyleElemAttrMap );
585 : }
586 2860 : return *pStyleElemAttrTokenMap;
587 : }
588 :
589 652 : const LocaleDataWrapper& SvXMLNumImpData::GetLocaleData( LanguageType nLang )
590 : {
591 652 : if ( !pLocaleData )
592 : pLocaleData = new LocaleDataWrapper(
593 : pFormatter ? pFormatter->GetComponentContext() : m_xContext,
594 121 : LanguageTag( nLang ) );
595 : else
596 531 : pLocaleData->setLanguageTag( LanguageTag( nLang ) );
597 652 : return *pLocaleData;
598 : }
599 :
600 :
601 : // SvXMLNumFmtMapContext
602 :
603 :
604 346 : SvXMLNumFmtMapContext::SvXMLNumFmtMapContext( SvXMLImport& rImport,
605 : sal_uInt16 nPrfx, const OUString& rLName,
606 : SvXMLNumFormatContext& rParentContext,
607 : const uno::Reference<xml::sax::XAttributeList>& xAttrList ) :
608 : SvXMLImportContext( rImport, nPrfx, rLName ),
609 346 : rParent( rParentContext )
610 : {
611 346 : sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
612 1038 : for( sal_Int16 i=0; i < nAttrCount; i++ )
613 : {
614 692 : OUString sAttrName = xAttrList->getNameByIndex( i );
615 1384 : OUString sValue = xAttrList->getValueByIndex( i );
616 1384 : OUString aLocalName;
617 692 : sal_uInt16 nPrefix = rImport.GetNamespaceMap().GetKeyByAttrName( sAttrName, &aLocalName );
618 692 : if ( nPrefix == XML_NAMESPACE_STYLE )
619 : {
620 692 : if ( IsXMLToken( aLocalName, XML_CONDITION) )
621 346 : sCondition = sValue;
622 346 : else if ( IsXMLToken( aLocalName, XML_APPLY_STYLE_NAME) )
623 346 : sName = sValue;
624 : }
625 692 : }
626 346 : }
627 :
628 692 : SvXMLNumFmtMapContext::~SvXMLNumFmtMapContext()
629 : {
630 692 : }
631 :
632 0 : SvXMLImportContext* SvXMLNumFmtMapContext::CreateChildContext(
633 : sal_uInt16 nPrfx, const OUString& rLName,
634 : const uno::Reference<xml::sax::XAttributeList>& )
635 : {
636 : // no elements supported - use default context
637 0 : return new SvXMLImportContext( GetImport(), nPrfx, rLName );
638 : }
639 :
640 0 : void SvXMLNumFmtMapContext::Characters( const OUString& )
641 : {
642 0 : }
643 :
644 346 : void SvXMLNumFmtMapContext::EndElement()
645 : {
646 346 : rParent.AddCondition( sCondition, sName );
647 346 : }
648 :
649 :
650 : // SvXMLNumFmtPropContext
651 :
652 :
653 179 : SvXMLNumFmtPropContext::SvXMLNumFmtPropContext( SvXMLImport& rImport,
654 : sal_uInt16 nPrfx, const OUString& rLName,
655 : SvXMLNumFormatContext& rParentContext,
656 : const uno::Reference<xml::sax::XAttributeList>& xAttrList ) :
657 : SvXMLImportContext( rImport, nPrfx, rLName ),
658 : rParent( rParentContext ),
659 179 : bColSet( false )
660 : {
661 179 : sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
662 358 : for( sal_Int16 i=0; i < nAttrCount; i++ )
663 : {
664 179 : OUString sAttrName = xAttrList->getNameByIndex( i );
665 358 : OUString sValue = xAttrList->getValueByIndex( i );
666 358 : OUString aLocalName;
667 179 : sal_uInt16 nPrefix = rImport.GetNamespaceMap().GetKeyByAttrName( sAttrName, &aLocalName );
668 179 : if ( nPrefix == XML_NAMESPACE_FO && IsXMLToken( aLocalName, XML_COLOR ) )
669 : {
670 179 : bColSet = ::sax::Converter::convertColor( m_nColor, sValue );
671 : }
672 179 : }
673 179 : }
674 :
675 358 : SvXMLNumFmtPropContext::~SvXMLNumFmtPropContext()
676 : {
677 358 : }
678 :
679 0 : SvXMLImportContext* SvXMLNumFmtPropContext::CreateChildContext(
680 : sal_uInt16 nPrfx, const OUString& rLName,
681 : const uno::Reference<xml::sax::XAttributeList>& )
682 : {
683 : // no elements supported - use default context
684 0 : return new SvXMLImportContext( GetImport(), nPrfx, rLName );
685 : }
686 :
687 0 : void SvXMLNumFmtPropContext::Characters( const OUString& )
688 : {
689 0 : }
690 :
691 179 : void SvXMLNumFmtPropContext::EndElement()
692 : {
693 179 : if (bColSet)
694 179 : rParent.AddColor( m_nColor );
695 179 : }
696 :
697 :
698 : // SvXMLNumFmtEmbeddedTextContext
699 :
700 :
701 0 : SvXMLNumFmtEmbeddedTextContext::SvXMLNumFmtEmbeddedTextContext( SvXMLImport& rImport,
702 : sal_uInt16 nPrfx, const OUString& rLName,
703 : SvXMLNumFmtElementContext& rParentContext,
704 : const uno::Reference<xml::sax::XAttributeList>& xAttrList ) :
705 : SvXMLImportContext( rImport, nPrfx, rLName ),
706 : rParent( rParentContext ),
707 0 : nTextPosition( 0 )
708 : {
709 : sal_Int32 nAttrVal;
710 :
711 0 : sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
712 0 : for( sal_Int16 i=0; i < nAttrCount; i++ )
713 : {
714 0 : OUString sAttrName = xAttrList->getNameByIndex( i );
715 0 : OUString sValue = xAttrList->getValueByIndex( i );
716 0 : OUString aLocalName;
717 0 : sal_uInt16 nPrefix = rImport.GetNamespaceMap().GetKeyByAttrName( sAttrName, &aLocalName );
718 0 : if ( nPrefix == XML_NAMESPACE_NUMBER && IsXMLToken( aLocalName, XML_POSITION ) )
719 : {
720 0 : if (::sax::Converter::convertNumber( nAttrVal, sValue, 0 ))
721 0 : nTextPosition = nAttrVal;
722 : }
723 0 : }
724 0 : }
725 :
726 0 : SvXMLNumFmtEmbeddedTextContext::~SvXMLNumFmtEmbeddedTextContext()
727 : {
728 0 : }
729 :
730 0 : SvXMLImportContext* SvXMLNumFmtEmbeddedTextContext::CreateChildContext(
731 : sal_uInt16 nPrfx, const OUString& rLName,
732 : const uno::Reference<xml::sax::XAttributeList>& )
733 : {
734 : // no elements supported - use default context
735 0 : return new SvXMLImportContext( GetImport(), nPrfx, rLName );
736 : }
737 :
738 0 : void SvXMLNumFmtEmbeddedTextContext::Characters( const OUString& rChars )
739 : {
740 0 : aContent.append( rChars );
741 0 : }
742 :
743 0 : void SvXMLNumFmtEmbeddedTextContext::EndElement()
744 : {
745 0 : rParent.AddEmbeddedElement( nTextPosition, aContent.makeStringAndClear() );
746 0 : }
747 :
748 981 : static bool lcl_ValidChar( sal_Unicode cChar, const SvXMLNumFormatContext& rParent )
749 : {
750 981 : sal_uInt16 nFormatType = rParent.GetType();
751 :
752 : // Treat space equal to non-breaking space separator.
753 981 : const sal_Unicode cNBSP = 0x00A0;
754 : sal_Unicode cTS;
755 1666 : if ( ( nFormatType == XML_TOK_STYLES_NUMBER_STYLE ||
756 358 : nFormatType == XML_TOK_STYLES_CURRENCY_STYLE ||
757 1625 : nFormatType == XML_TOK_STYLES_PERCENTAGE_STYLE ) &&
758 1288 : (cChar == (cTS = rParent.GetLocaleData().getNumThousandSep()[0]) ||
759 243 : (cChar == ' ' && cTS == cNBSP)) )
760 : {
761 : // #i22394# Extra occurrences of thousands separator must be quoted, so they
762 : // aren't mis-interpreted as display-factor.
763 : // This must be limited to the format types that can contain a number element,
764 : // because the same character can be a date separator that should not be quoted
765 : // in date formats.
766 :
767 0 : return false; // force quotes
768 : }
769 :
770 : // see ImpSvNumberformatScan::Next_Symbol
771 981 : if ( cChar == ' ' ||
772 431 : cChar == '-' ||
773 300 : cChar == '/' ||
774 296 : cChar == '.' ||
775 296 : cChar == ',' ||
776 205 : cChar == ':' ||
777 : cChar == '\'' )
778 776 : return true; // for all format types
779 :
780 : // percent sign must be used without quotes for percentage styles only
781 205 : if ( nFormatType == XML_TOK_STYLES_PERCENTAGE_STYLE && cChar == '%' )
782 21 : return true;
783 :
784 : // don't put quotes around single parentheses (often used for negative numbers)
785 184 : if ( ( nFormatType == XML_TOK_STYLES_NUMBER_STYLE ||
786 0 : nFormatType == XML_TOK_STYLES_CURRENCY_STYLE ||
787 184 : nFormatType == XML_TOK_STYLES_PERCENTAGE_STYLE ) &&
788 107 : ( cChar == '(' || cChar == ')' ) )
789 154 : return true;
790 :
791 30 : return false;
792 : }
793 :
794 1055 : static void lcl_EnquoteIfNecessary( OUStringBuffer& rContent, const SvXMLNumFormatContext& rParent )
795 : {
796 1055 : bool bQuote = true;
797 1055 : sal_Int32 nLength = rContent.getLength();
798 :
799 1949 : if ( ( nLength == 1 &&
800 2124 : lcl_ValidChar( rContent[0], rParent ) ) ||
801 87 : ( nLength == 2 &&
802 160 : lcl_ValidChar( rContent[0], rParent ) &&
803 73 : rContent[1] == ' ' ) )
804 : {
805 : // don't quote single separator characters like space or percent,
806 : // or separator characters followed by space (used in date formats)
807 892 : bQuote = false;
808 : }
809 163 : else if ( rParent.GetType() == XML_TOK_STYLES_PERCENTAGE_STYLE && nLength > 1 )
810 : {
811 : // the percent character in percentage styles must be left out of quoting
812 : // (one occurrence is enough even if there are several percent characters in the string)
813 :
814 0 : OUString aString( rContent.getStr() );
815 0 : sal_Int32 nPos = aString.indexOf( '%' );
816 0 : if ( nPos >= 0 )
817 : {
818 0 : if ( nPos + 1 < nLength )
819 : {
820 0 : if ( nPos + 2 == nLength && lcl_ValidChar( rContent[nPos + 1], rParent ) )
821 : {
822 : // single character that doesn't need quoting
823 : }
824 : else
825 : {
826 : // quote text behind percent character
827 0 : rContent.insert( nPos + 1, '"' );
828 0 : rContent.append( '"' );
829 : }
830 : }
831 0 : if ( nPos > 0 )
832 : {
833 0 : if ( nPos == 1 && lcl_ValidChar( rContent[0], rParent ) )
834 : {
835 : // single character that doesn't need quoting
836 : }
837 : else
838 : {
839 : // quote text before percent character
840 0 : rContent.insert( nPos, '"' );
841 0 : rContent.insert( 0, '"' );
842 : }
843 : }
844 0 : bQuote = false;
845 0 : }
846 : // else: normal quoting (below)
847 : }
848 :
849 1055 : if ( bQuote )
850 : {
851 : // #i55469# quotes in the string itself have to be escaped
852 163 : bool bEscape = ( rContent.indexOf( '"' ) >= 0 );
853 163 : if ( bEscape )
854 : {
855 : // A quote is turned into "\"" - a quote to end quoted text, an escaped quote,
856 : // and a quote to resume quoting.
857 44 : OUString aInsert( "\"\\\"" );
858 :
859 44 : sal_Int32 nPos = 0;
860 244 : while ( nPos < rContent.getLength() )
861 : {
862 156 : if ( rContent[nPos] == '"' )
863 : {
864 44 : rContent.insert( nPos, aInsert );
865 44 : nPos += aInsert.getLength();
866 : }
867 156 : ++nPos;
868 44 : }
869 : }
870 :
871 : // quote string literals
872 163 : rContent.insert( 0, '"' );
873 163 : rContent.append( '"' );
874 :
875 : // remove redundant double quotes at start or end
876 163 : if ( bEscape )
877 : {
878 132 : if ( rContent.getLength() > 2 &&
879 88 : rContent[0] == '"' &&
880 44 : rContent[1] == '"' )
881 : {
882 0 : rContent.remove(0, 2);
883 : }
884 :
885 44 : sal_Int32 nLen = rContent.getLength();
886 88 : if ( nLen > 2 &&
887 88 : rContent[nLen - 1] == '"' &&
888 44 : rContent[nLen - 2] == '"' )
889 : {
890 44 : rContent.truncate(nLen - 2);
891 : }
892 : }
893 : }
894 1055 : }
895 :
896 :
897 : // SvXMLNumFmtElementContext
898 :
899 :
900 : const sal_Int32 MAX_SECOND_DIGITS = 20; // fdo#58539 & gnome#627420: limit number of digits during import
901 :
902 2815 : SvXMLNumFmtElementContext::SvXMLNumFmtElementContext( SvXMLImport& rImport,
903 : sal_uInt16 nPrfx, const OUString& rLName,
904 : SvXMLNumFormatContext& rParentContext, sal_uInt16 nNewType,
905 : const uno::Reference<xml::sax::XAttributeList>& xAttrList ) :
906 : SvXMLImportContext( rImport, nPrfx, rLName ),
907 : rParent( rParentContext ),
908 : nType( nNewType ),
909 : nElementLang( LANGUAGE_SYSTEM ),
910 : bLong( false ),
911 2815 : bTextual( false )
912 : {
913 2815 : LanguageTagODF aLanguageTagODF;
914 : sal_Int32 nAttrVal;
915 2815 : bool bAttrBool(false);
916 : sal_uInt16 nAttrEnum;
917 : double fAttrDouble;
918 :
919 2815 : sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
920 5675 : for( sal_Int16 i=0; i < nAttrCount; i++ )
921 : {
922 2860 : OUString sAttrName = xAttrList->getNameByIndex( i );
923 5720 : OUString sValue = xAttrList->getValueByIndex( i );
924 5720 : OUString aLocalName;
925 2860 : sal_uInt16 nPrefix = rImport.GetNamespaceMap().GetKeyByAttrName( sAttrName, &aLocalName );
926 :
927 2860 : const SvXMLTokenMap& rTokenMap = rParent.GetData()->GetStyleElemAttrTokenMap();
928 2860 : sal_uInt16 nToken = rTokenMap.Get( nPrefix, aLocalName );
929 :
930 2860 : switch (nToken)
931 : {
932 : case XML_TOK_ELEM_ATTR_DECIMAL_PLACES:
933 649 : if (::sax::Converter::convertNumber( nAttrVal, sValue, 0 ))
934 649 : aNumInfo.nDecimals = std::min<sal_Int32>(nAttrVal, MAX_SECOND_DIGITS);
935 649 : break;
936 : case XML_TOK_ELEM_ATTR_MIN_INTEGER_DIGITS:
937 837 : if (::sax::Converter::convertNumber( nAttrVal, sValue, 0 ))
938 837 : aNumInfo.nInteger = nAttrVal;
939 837 : break;
940 : case XML_TOK_ELEM_ATTR_GROUPING:
941 520 : if (::sax::Converter::convertBool( bAttrBool, sValue ))
942 520 : aNumInfo.bGrouping = bAttrBool;
943 520 : break;
944 : case XML_TOK_ELEM_ATTR_DISPLAY_FACTOR:
945 0 : if (::sax::Converter::convertDouble( fAttrDouble, sValue ))
946 0 : aNumInfo.fDisplayFactor = fAttrDouble;
947 0 : break;
948 : case XML_TOK_ELEM_ATTR_DECIMAL_REPLACEMENT:
949 0 : if ( !sValue.isEmpty() )
950 0 : aNumInfo.bDecReplace = true; // only a default string is supported
951 : else
952 0 : aNumInfo.bVarDecimals = true; // empty replacement string: variable decimals
953 0 : break;
954 : case XML_TOK_ELEM_ATTR_MIN_EXPONENT_DIGITS:
955 17 : if (::sax::Converter::convertNumber( nAttrVal, sValue, 0 ))
956 17 : aNumInfo.nExpDigits = nAttrVal;
957 17 : break;
958 : case XML_TOK_ELEM_ATTR_MIN_NUMERATOR_DIGITS:
959 7 : if (::sax::Converter::convertNumber( nAttrVal, sValue, 0 ))
960 7 : aNumInfo.nNumerDigits = nAttrVal;
961 7 : break;
962 : case XML_TOK_ELEM_ATTR_MIN_DENOMINATOR_DIGITS:
963 7 : if (::sax::Converter::convertNumber( nAttrVal, sValue, 0 ))
964 7 : aNumInfo.nDenomDigits = nAttrVal;
965 7 : break;
966 : case XML_TOK_ELEM_ATTR_DENOMINATOR_VALUE:
967 0 : if (::sax::Converter::convertNumber( nAttrVal, sValue, 0 ))
968 0 : aNumInfo.nFracDenominator = nAttrVal;
969 0 : break;
970 : case XML_TOK_ELEM_ATTR_RFC_LANGUAGE_TAG:
971 0 : aLanguageTagODF.maRfcLanguageTag = sValue;
972 0 : break;
973 : case XML_TOK_ELEM_ATTR_LANGUAGE:
974 256 : aLanguageTagODF.maLanguage = sValue;
975 256 : break;
976 : case XML_TOK_ELEM_ATTR_SCRIPT:
977 0 : aLanguageTagODF.maScript = sValue;
978 0 : break;
979 : case XML_TOK_ELEM_ATTR_COUNTRY:
980 256 : aLanguageTagODF.maCountry = sValue;
981 256 : break;
982 : case XML_TOK_ELEM_ATTR_STYLE:
983 285 : if ( SvXMLUnitConverter::convertEnum( nAttrEnum, sValue, aStyleValueMap ) )
984 285 : bLong = (bool) nAttrEnum;
985 285 : break;
986 : case XML_TOK_ELEM_ATTR_TEXTUAL:
987 26 : if (::sax::Converter::convertBool( bAttrBool, sValue ))
988 26 : bTextual = bAttrBool;
989 26 : break;
990 : case XML_TOK_ELEM_ATTR_CALENDAR:
991 0 : sCalendar = sValue;
992 0 : break;
993 : }
994 2860 : }
995 :
996 2815 : if ( !aLanguageTagODF.isEmpty() )
997 : {
998 256 : nElementLang = aLanguageTagODF.getLanguageTag().getLanguageType( false);
999 256 : if ( nElementLang == LANGUAGE_DONTKNOW )
1000 0 : nElementLang = LANGUAGE_SYSTEM; //! error handling for unknown locales?
1001 2815 : }
1002 2815 : }
1003 :
1004 5630 : SvXMLNumFmtElementContext::~SvXMLNumFmtElementContext()
1005 : {
1006 5630 : }
1007 :
1008 0 : SvXMLImportContext* SvXMLNumFmtElementContext::CreateChildContext(
1009 : sal_uInt16 nPrfx, const OUString& rLName,
1010 : const uno::Reference<xml::sax::XAttributeList>& xAttrList )
1011 : {
1012 : // only number:number supports number:embedded-text child element
1013 :
1014 0 : if ( nType == XML_TOK_STYLE_NUMBER &&
1015 0 : nPrfx == XML_NAMESPACE_NUMBER && IsXMLToken( rLName, XML_EMBEDDED_TEXT ) )
1016 : {
1017 0 : return new SvXMLNumFmtEmbeddedTextContext( GetImport(), nPrfx, rLName, *this, xAttrList );
1018 : }
1019 : else
1020 0 : return new SvXMLImportContext( GetImport(), nPrfx, rLName );
1021 : }
1022 :
1023 1449 : void SvXMLNumFmtElementContext::Characters( const OUString& rChars )
1024 : {
1025 1449 : aContent.append( rChars );
1026 1449 : }
1027 :
1028 0 : void SvXMLNumFmtElementContext::AddEmbeddedElement( sal_Int32 nFormatPos, const OUString& rContent )
1029 : {
1030 0 : if ( !rContent.isEmpty() )
1031 : {
1032 0 : SvXMLEmbeddedElement* pObj = new SvXMLEmbeddedElement( nFormatPos, rContent );
1033 0 : if ( !aNumInfo.aEmbeddedElements.insert( pObj ).second )
1034 : {
1035 : // there's already an element at this position - append text to existing element
1036 :
1037 0 : delete pObj;
1038 0 : for (SvXMLEmbeddedElementArr::iterator it = aNumInfo.aEmbeddedElements.begin();
1039 0 : it != aNumInfo.aEmbeddedElements.end(); ++it)
1040 : {
1041 0 : pObj = &*it;
1042 0 : if ( pObj->nFormatPos == nFormatPos )
1043 : {
1044 0 : pObj->aText += rContent;
1045 0 : break;
1046 : }
1047 : }
1048 : }
1049 : }
1050 0 : }
1051 :
1052 2815 : void SvXMLNumFmtElementContext::EndElement()
1053 : {
1054 2815 : bool bEffLong = bLong;
1055 2815 : switch (nType)
1056 : {
1057 : case XML_TOK_STYLE_TEXT:
1058 3165 : if ( rParent.HasLongDoW() &&
1059 1055 : aContent.toString().equals(rParent.GetLocaleData().getLongDateDayOfWeekSep()) )
1060 : {
1061 : // skip separator constant after long day of week
1062 : // (NF_KEY_NNNN contains the separator)
1063 :
1064 0 : if ( rParent.ReplaceNfKeyword( NF_KEY_NNN, NF_KEY_NNNN ) )
1065 : {
1066 0 : aContent = OUStringBuffer();
1067 : }
1068 :
1069 0 : rParent.SetHasLongDoW( false ); // only once
1070 : }
1071 1055 : if ( !aContent.isEmpty() )
1072 : {
1073 1055 : lcl_EnquoteIfNecessary( aContent, rParent );
1074 1055 : rParent.AddToCode( aContent.makeStringAndClear() );
1075 : }
1076 1055 : break;
1077 :
1078 : case XML_TOK_STYLE_NUMBER:
1079 813 : rParent.AddNumber( aNumInfo );
1080 813 : break;
1081 :
1082 : case XML_TOK_STYLE_CURRENCY_SYMBOL:
1083 340 : rParent.AddCurrency( aContent.makeStringAndClear(), nElementLang );
1084 340 : break;
1085 :
1086 : case XML_TOK_STYLE_TEXT_CONTENT:
1087 47 : rParent.AddToCode( '@');
1088 47 : break;
1089 : case XML_TOK_STYLE_FILL_CHARACTER:
1090 84 : if ( !aContent.isEmpty() )
1091 : {
1092 84 : rParent.AddToCode( '*' );
1093 84 : rParent.AddToCode( aContent[0] );
1094 : }
1095 84 : break;
1096 : case XML_TOK_STYLE_BOOLEAN:
1097 : // ignored - only default boolean format is supported
1098 11 : break;
1099 :
1100 : case XML_TOK_STYLE_DAY:
1101 86 : rParent.UpdateCalendar( sCalendar );
1102 : //! I18N doesn't provide SYSTEM or extended date information yet
1103 :
1104 : rParent.AddNfKeyword(
1105 : sal::static_int_cast< sal_uInt16 >(
1106 86 : bEffLong ? NF_KEY_DD : NF_KEY_D ) );
1107 86 : break;
1108 : case XML_TOK_STYLE_MONTH:
1109 96 : rParent.UpdateCalendar( sCalendar );
1110 : //! I18N doesn't provide SYSTEM or extended date information yet
1111 :
1112 : rParent.AddNfKeyword(
1113 : sal::static_int_cast< sal_uInt16 >(
1114 : bTextual
1115 : ? ( bEffLong ? NF_KEY_MMMM : NF_KEY_MMM )
1116 96 : : ( bEffLong ? NF_KEY_MM : NF_KEY_M ) ) );
1117 96 : break;
1118 : case XML_TOK_STYLE_YEAR:
1119 85 : rParent.UpdateCalendar( sCalendar );
1120 : //! I18N doesn't provide SYSTEM or extended date information yet
1121 : // Y after G (era) is replaced by E
1122 85 : if ( rParent.HasEra() )
1123 : rParent.AddNfKeyword(
1124 : sal::static_int_cast< sal_uInt16 >(
1125 0 : bEffLong ? NF_KEY_EEC : NF_KEY_EC ) );
1126 : else
1127 : rParent.AddNfKeyword(
1128 : sal::static_int_cast< sal_uInt16 >(
1129 85 : bEffLong ? NF_KEY_YYYY : NF_KEY_YY ) );
1130 85 : break;
1131 : case XML_TOK_STYLE_ERA:
1132 0 : rParent.UpdateCalendar( sCalendar );
1133 : //! I18N doesn't provide SYSTEM or extended date information yet
1134 : rParent.AddNfKeyword(
1135 : sal::static_int_cast< sal_uInt16 >(
1136 0 : bEffLong ? NF_KEY_GGG : NF_KEY_G ) );
1137 : // HasEra flag is set
1138 0 : break;
1139 : case XML_TOK_STYLE_DAY_OF_WEEK:
1140 0 : rParent.UpdateCalendar( sCalendar );
1141 : //! I18N doesn't provide SYSTEM or extended date information yet
1142 : rParent.AddNfKeyword(
1143 : sal::static_int_cast< sal_uInt16 >(
1144 0 : bEffLong ? NF_KEY_NNNN : NF_KEY_NN ) );
1145 0 : break;
1146 : case XML_TOK_STYLE_WEEK_OF_YEAR:
1147 0 : rParent.UpdateCalendar( sCalendar );
1148 0 : rParent.AddNfKeyword( NF_KEY_WW );
1149 0 : break;
1150 : case XML_TOK_STYLE_QUARTER:
1151 0 : rParent.UpdateCalendar( sCalendar );
1152 : rParent.AddNfKeyword(
1153 : sal::static_int_cast< sal_uInt16 >(
1154 0 : bEffLong ? NF_KEY_QQ : NF_KEY_Q ) );
1155 0 : break;
1156 : case XML_TOK_STYLE_HOURS:
1157 : rParent.AddNfKeyword(
1158 : sal::static_int_cast< sal_uInt16 >(
1159 51 : bEffLong ? NF_KEY_HH : NF_KEY_H ) );
1160 51 : break;
1161 : case XML_TOK_STYLE_AM_PM:
1162 : //! short/long?
1163 16 : rParent.AddNfKeyword( NF_KEY_AMPM );
1164 16 : break;
1165 : case XML_TOK_STYLE_MINUTES:
1166 : rParent.AddNfKeyword(
1167 : sal::static_int_cast< sal_uInt16 >(
1168 67 : bEffLong ? NF_KEY_MMI : NF_KEY_MI ) );
1169 67 : break;
1170 : case XML_TOK_STYLE_SECONDS:
1171 : rParent.AddNfKeyword(
1172 : sal::static_int_cast< sal_uInt16 >(
1173 40 : bEffLong ? NF_KEY_SS : NF_KEY_S ) );
1174 40 : if ( aNumInfo.nDecimals > 0 )
1175 : {
1176 : // manually add the decimal places
1177 8 : rParent.AddToCode(rParent.GetLocaleData().getNumDecimalSep());
1178 16 : for (sal_Int32 i=0; i<aNumInfo.nDecimals; i++)
1179 : {
1180 8 : rParent.AddToCode( '0');
1181 : }
1182 : }
1183 40 : break;
1184 :
1185 : case XML_TOK_STYLE_FRACTION:
1186 : {
1187 7 : if ( aNumInfo.nInteger >= 0 )
1188 : {
1189 : // add integer part only if min-integer-digits attribute is there
1190 7 : aNumInfo.nDecimals = 0;
1191 7 : rParent.AddNumber( aNumInfo ); // number without decimals
1192 7 : rParent.AddToCode( ' ' );
1193 : }
1194 :
1195 : //! build string and add at once
1196 :
1197 : sal_Int32 i;
1198 17 : for (i=0; i<aNumInfo.nNumerDigits; i++)
1199 : {
1200 10 : rParent.AddToCode( '?' );
1201 : }
1202 7 : rParent.AddToCode( '/' );
1203 7 : if ( aNumInfo.nFracDenominator > 0 )
1204 : {
1205 0 : rParent.AddToCode( OUString::number( aNumInfo.nFracDenominator ) );
1206 : }
1207 : else
1208 : {
1209 17 : for (i=0; i<aNumInfo.nDenomDigits; i++)
1210 : {
1211 10 : rParent.AddToCode( '?');
1212 : }
1213 : }
1214 : }
1215 7 : break;
1216 :
1217 : case XML_TOK_STYLE_SCIENTIFIC_NUMBER:
1218 : {
1219 17 : rParent.AddNumber( aNumInfo ); // simple number
1220 :
1221 17 : rParent.AddToCode( OUString("E+") );
1222 47 : for (sal_Int32 i=0; i<aNumInfo.nExpDigits; i++)
1223 : {
1224 30 : rParent.AddToCode( '0' );
1225 : }
1226 : }
1227 17 : break;
1228 :
1229 : default:
1230 : OSL_FAIL("invalid element ID");
1231 : }
1232 2815 : }
1233 :
1234 177 : sal_uInt16 SvXMLNumFmtDefaults::GetDefaultDateFormat( SvXMLDateElementAttributes eDOW,
1235 : SvXMLDateElementAttributes eDay, SvXMLDateElementAttributes eMonth,
1236 : SvXMLDateElementAttributes eYear, SvXMLDateElementAttributes eHours,
1237 : SvXMLDateElementAttributes eMins, SvXMLDateElementAttributes eSecs,
1238 : bool bSystem )
1239 : {
1240 : static const sal_uInt16 nCount = sizeof(aDefaultDateFormats) / sizeof(SvXMLDefaultDateFormat);
1241 885 : for (sal_uInt16 nPos=0; nPos<nCount; nPos++)
1242 : {
1243 885 : const SvXMLDefaultDateFormat& rEntry = aDefaultDateFormats[nPos];
1244 1420 : if ( bSystem == rEntry.bSystem &&
1245 1070 : ( eDOW == rEntry.eDOW || ( rEntry.eDOW == XML_DEA_ANY && eDOW != XML_DEA_NONE ) ) &&
1246 1072 : ( eDay == rEntry.eDay || ( rEntry.eDay == XML_DEA_ANY && eDay != XML_DEA_NONE ) ) &&
1247 722 : ( eMonth == rEntry.eMonth || ( rEntry.eMonth == XML_DEA_ANY && eMonth != XML_DEA_NONE ) ) &&
1248 372 : ( eYear == rEntry.eYear || ( rEntry.eYear == XML_DEA_ANY && eYear != XML_DEA_NONE ) ) &&
1249 354 : ( eHours == rEntry.eHours || ( rEntry.eHours == XML_DEA_ANY && eHours != XML_DEA_NONE ) ) &&
1250 354 : ( eMins == rEntry.eMins || ( rEntry.eMins == XML_DEA_ANY && eMins != XML_DEA_NONE ) ) &&
1251 177 : ( eSecs == rEntry.eSecs || ( rEntry.eSecs == XML_DEA_ANY && eSecs != XML_DEA_NONE ) ) )
1252 : {
1253 177 : return sal::static_int_cast< sal_uInt16 >(rEntry.eFormat);
1254 : }
1255 : }
1256 :
1257 0 : return NF_INDEX_TABLE_ENTRIES; // invalid
1258 : }
1259 :
1260 :
1261 : // SvXMLNumFormatContext
1262 :
1263 :
1264 1088 : SvXMLNumFormatContext::SvXMLNumFormatContext( SvXMLImport& rImport,
1265 : sal_uInt16 nPrfx, const OUString& rLName,
1266 : SvXMLNumImpData* pNewData, sal_uInt16 nNewType,
1267 : const uno::Reference<xml::sax::XAttributeList>& xAttrList,
1268 : SvXMLStylesContext& rStyles ) :
1269 : SvXMLStyleContext( rImport, nPrfx, rLName, xAttrList ),
1270 : pData( pNewData ),
1271 : pStyles( &rStyles ),
1272 : aMyConditions(),
1273 : nType( nNewType ),
1274 : nKey(-1),
1275 : nFormatLang( LANGUAGE_SYSTEM ),
1276 : bAutoOrder( false ),
1277 : bFromSystem( false ),
1278 : bTruncate( true ),
1279 : bAutoDec( false ),
1280 : bAutoInt( false ),
1281 : bHasExtraText( false ),
1282 : bHasLongDoW( false ),
1283 : bHasEra( false ),
1284 : bHasDateTime( false ),
1285 : bRemoveAfterUse( false ),
1286 : eDateDOW( XML_DEA_NONE ),
1287 : eDateDay( XML_DEA_NONE ),
1288 : eDateMonth( XML_DEA_NONE ),
1289 : eDateYear( XML_DEA_NONE ),
1290 : eDateHours( XML_DEA_NONE ),
1291 : eDateMins( XML_DEA_NONE ),
1292 : eDateSecs( XML_DEA_NONE ),
1293 1088 : bDateNoDefault( false )
1294 : {
1295 1088 : LanguageTagODF aLanguageTagODF;
1296 2176 : OUString sNatNumAttrScript, sNatNumAttrRfcLanguageTag;
1297 2176 : ::com::sun::star::i18n::NativeNumberXmlAttributes aNatNumAttr;
1298 1088 : bool bAttrBool(false);
1299 : sal_uInt16 nAttrEnum;
1300 :
1301 1088 : sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
1302 3192 : for( sal_Int16 i=0; i < nAttrCount; i++ )
1303 : {
1304 2104 : OUString sAttrName = xAttrList->getNameByIndex( i );
1305 4208 : OUString sValue = xAttrList->getValueByIndex( i );
1306 4208 : OUString aLocalName;
1307 2104 : sal_uInt16 nPrefix = rImport.GetNamespaceMap().GetKeyByAttrName( sAttrName, &aLocalName );
1308 :
1309 2104 : const SvXMLTokenMap& rTokenMap = pData->GetStyleAttrTokenMap();
1310 2104 : sal_uInt16 nToken = rTokenMap.Get( nPrefix, aLocalName );
1311 2104 : switch (nToken)
1312 : {
1313 : case XML_TOK_STYLE_ATTR_NAME:
1314 1088 : break;
1315 : case XML_TOK_STYLE_ATTR_RFC_LANGUAGE_TAG:
1316 0 : aLanguageTagODF.maRfcLanguageTag = sValue;
1317 0 : break;
1318 : case XML_TOK_STYLE_ATTR_LANGUAGE:
1319 293 : aLanguageTagODF.maLanguage = sValue;
1320 293 : break;
1321 : case XML_TOK_STYLE_ATTR_SCRIPT:
1322 0 : aLanguageTagODF.maScript = sValue;
1323 0 : break;
1324 : case XML_TOK_STYLE_ATTR_COUNTRY:
1325 293 : aLanguageTagODF.maCountry = sValue;
1326 293 : break;
1327 : case XML_TOK_STYLE_ATTR_TITLE:
1328 0 : sFormatTitle = sValue;
1329 0 : break;
1330 : case XML_TOK_STYLE_ATTR_AUTOMATIC_ORDER:
1331 52 : if (::sax::Converter::convertBool( bAttrBool, sValue ))
1332 52 : bAutoOrder = bAttrBool;
1333 52 : break;
1334 : case XML_TOK_STYLE_ATTR_FORMAT_SOURCE:
1335 2 : if ( SvXMLUnitConverter::convertEnum( nAttrEnum, sValue, aFormatSourceMap ) )
1336 2 : bFromSystem = (bool) nAttrEnum;
1337 2 : break;
1338 : case XML_TOK_STYLE_ATTR_TRUNCATE_ON_OVERFLOW:
1339 8 : if (::sax::Converter::convertBool( bAttrBool, sValue ))
1340 8 : bTruncate = bAttrBool;
1341 8 : break;
1342 : case XML_TOK_STYLE_ATTR_VOLATILE:
1343 : // volatile formats can be removed after importing
1344 : // if not used in other styles
1345 346 : if (::sax::Converter::convertBool( bAttrBool, sValue ))
1346 346 : bRemoveAfterUse = bAttrBool;
1347 346 : break;
1348 : case XML_TOK_STYLE_ATTR_TRANSL_FORMAT:
1349 1 : aNatNumAttr.Format = sValue;
1350 1 : break;
1351 : case XML_TOK_STYLE_ATTR_TRANSL_RFC_LANGUAGE_TAG:
1352 0 : sNatNumAttrRfcLanguageTag = sValue;
1353 0 : break;
1354 : case XML_TOK_STYLE_ATTR_TRANSL_LANGUAGE:
1355 1 : aNatNumAttr.Locale.Language = sValue;
1356 1 : break;
1357 : case XML_TOK_STYLE_ATTR_TRANSL_SCRIPT:
1358 0 : sNatNumAttrScript = sValue;
1359 0 : break;
1360 : case XML_TOK_STYLE_ATTR_TRANSL_COUNTRY:
1361 1 : aNatNumAttr.Locale.Country = sValue;
1362 1 : break;
1363 : case XML_TOK_STYLE_ATTR_TRANSL_STYLE:
1364 1 : aNatNumAttr.Style = sValue;
1365 1 : break;
1366 : }
1367 2104 : }
1368 :
1369 1088 : if (!aLanguageTagODF.isEmpty())
1370 : {
1371 293 : nFormatLang = aLanguageTagODF.getLanguageTag().getLanguageType( false);
1372 293 : if ( nFormatLang == LANGUAGE_DONTKNOW )
1373 0 : nFormatLang = LANGUAGE_SYSTEM; //! error handling for unknown locales?
1374 : }
1375 :
1376 1088 : if ( !aNatNumAttr.Format.isEmpty() )
1377 : {
1378 1 : SvNumberFormatter* pFormatter = pData->GetNumberFormatter();
1379 1 : if ( pFormatter )
1380 : {
1381 : LanguageTag aLanguageTag( sNatNumAttrRfcLanguageTag, aNatNumAttr.Locale.Language,
1382 1 : sNatNumAttrScript, aNatNumAttr.Locale.Country);
1383 1 : aNatNumAttr.Locale = aLanguageTag.getLocale( false);
1384 :
1385 1 : sal_Int32 nNatNum = pFormatter->GetNatNum()->convertFromXmlAttributes( aNatNumAttr );
1386 1 : aFormatCode.append( "[NatNum" );
1387 1 : aFormatCode.append( nNatNum, 10 );
1388 :
1389 1 : LanguageType eLang = aLanguageTag.getLanguageType( false);
1390 1 : if ( eLang == LANGUAGE_DONTKNOW )
1391 0 : eLang = LANGUAGE_SYSTEM; //! error handling for unknown locales?
1392 1 : if ( eLang != nFormatLang && eLang != LANGUAGE_SYSTEM )
1393 : {
1394 0 : aFormatCode.append( "][$-" );
1395 : // language code in upper hex:
1396 0 : aFormatCode.append(OUString::number(eLang, 16).toAsciiUpperCase());
1397 : }
1398 1 : aFormatCode.append( ']' );
1399 : }
1400 1088 : }
1401 1088 : }
1402 :
1403 539 : SvXMLNumFormatContext::SvXMLNumFormatContext( SvXMLImport& rImport,
1404 : sal_uInt16 nPrfx, const OUString& rLName,
1405 : const uno::Reference<xml::sax::XAttributeList>& xAttrList,
1406 : const sal_Int32 nTempKey,
1407 : SvXMLStylesContext& rStyles ) :
1408 : SvXMLStyleContext( rImport, nPrfx, rLName, xAttrList, XML_STYLE_FAMILY_DATA_STYLE ),
1409 : pData( NULL ),
1410 : pStyles( &rStyles ),
1411 : aMyConditions(),
1412 : nType( 0 ),
1413 : nKey(nTempKey),
1414 : nFormatLang( LANGUAGE_SYSTEM ),
1415 : bAutoOrder( false ),
1416 : bFromSystem( false ),
1417 : bTruncate( true ),
1418 : bAutoDec( false ),
1419 : bAutoInt( false ),
1420 : bHasExtraText( false ),
1421 : bHasLongDoW( false ),
1422 : bHasEra( false ),
1423 : bHasDateTime( false ),
1424 : bRemoveAfterUse( false ),
1425 : eDateDOW( XML_DEA_NONE ),
1426 : eDateDay( XML_DEA_NONE ),
1427 : eDateMonth( XML_DEA_NONE ),
1428 : eDateYear( XML_DEA_NONE ),
1429 : eDateHours( XML_DEA_NONE ),
1430 : eDateMins( XML_DEA_NONE ),
1431 : eDateSecs( XML_DEA_NONE ),
1432 539 : bDateNoDefault( false )
1433 : {
1434 539 : SetAttribute(XML_NAMESPACE_STYLE, GetXMLToken(XML_NAME), rLName);
1435 539 : }
1436 :
1437 3253 : SvXMLNumFormatContext::~SvXMLNumFormatContext()
1438 : {
1439 3253 : }
1440 :
1441 3340 : SvXMLImportContext* SvXMLNumFormatContext::CreateChildContext(
1442 : sal_uInt16 nPrfx, const OUString& rLName,
1443 : const uno::Reference<xml::sax::XAttributeList>& xAttrList )
1444 : {
1445 3340 : SvXMLImportContext* pContext = NULL;
1446 :
1447 3340 : const SvXMLTokenMap& rTokenMap = pData->GetStyleElemTokenMap();
1448 3340 : sal_uInt16 nToken = rTokenMap.Get( nPrfx, rLName );
1449 3340 : switch (nToken)
1450 : {
1451 : case XML_TOK_STYLE_TEXT:
1452 : case XML_TOK_STYLE_FILL_CHARACTER:
1453 : case XML_TOK_STYLE_NUMBER:
1454 : case XML_TOK_STYLE_SCIENTIFIC_NUMBER:
1455 : case XML_TOK_STYLE_FRACTION:
1456 : case XML_TOK_STYLE_CURRENCY_SYMBOL:
1457 : case XML_TOK_STYLE_DAY:
1458 : case XML_TOK_STYLE_MONTH:
1459 : case XML_TOK_STYLE_YEAR:
1460 : case XML_TOK_STYLE_ERA:
1461 : case XML_TOK_STYLE_DAY_OF_WEEK:
1462 : case XML_TOK_STYLE_WEEK_OF_YEAR:
1463 : case XML_TOK_STYLE_QUARTER:
1464 : case XML_TOK_STYLE_HOURS:
1465 : case XML_TOK_STYLE_AM_PM:
1466 : case XML_TOK_STYLE_MINUTES:
1467 : case XML_TOK_STYLE_SECONDS:
1468 : case XML_TOK_STYLE_BOOLEAN:
1469 : case XML_TOK_STYLE_TEXT_CONTENT:
1470 2815 : pContext = new SvXMLNumFmtElementContext( GetImport(), nPrfx, rLName,
1471 2815 : *this, nToken, xAttrList );
1472 2815 : break;
1473 :
1474 : case XML_TOK_STYLE_PROPERTIES:
1475 179 : pContext = new SvXMLNumFmtPropContext( GetImport(), nPrfx, rLName,
1476 179 : *this, xAttrList );
1477 179 : break;
1478 : case XML_TOK_STYLE_MAP:
1479 : {
1480 : // SvXMLNumFmtMapContext::EndElement adds to aMyConditions,
1481 : // so there's no need for an extra flag
1482 346 : pContext = new SvXMLNumFmtMapContext( GetImport(), nPrfx, rLName,
1483 346 : *this, xAttrList );
1484 : }
1485 346 : break;
1486 : }
1487 :
1488 3340 : if( !pContext )
1489 0 : pContext = new SvXMLImportContext( GetImport(), nPrfx, rLName );
1490 3340 : return pContext;
1491 : }
1492 :
1493 650 : sal_Int32 SvXMLNumFormatContext::GetKey()
1494 : {
1495 650 : if (nKey > -1)
1496 : {
1497 450 : if (bRemoveAfterUse)
1498 : {
1499 : // format is used -> don't remove
1500 0 : bRemoveAfterUse = false;
1501 0 : if (pData)
1502 0 : pData->SetUsed(nKey);
1503 :
1504 : // Add to import's list of keys now - CreateAndInsert didn't add
1505 : // the style if bRemoveAfterUse was set.
1506 0 : GetImport().AddNumberStyle( nKey, GetName() );
1507 : }
1508 450 : return nKey;
1509 : }
1510 : else
1511 : {
1512 : // reset bRemoveAfterUse before CreateAndInsert, so AddKey is called without bRemoveAfterUse set
1513 200 : bRemoveAfterUse = false;
1514 200 : CreateAndInsert(true);
1515 200 : return nKey;
1516 : }
1517 : }
1518 :
1519 346 : sal_Int32 SvXMLNumFormatContext::PrivateGetKey()
1520 : {
1521 : // used for map elements in CreateAndInsert - don't reset bRemoveAfterUse flag
1522 :
1523 346 : if (nKey > -1)
1524 345 : return nKey;
1525 : else
1526 : {
1527 1 : CreateAndInsert(true);
1528 1 : return nKey;
1529 : }
1530 : }
1531 :
1532 0 : sal_Int32 SvXMLNumFormatContext::CreateAndInsert( com::sun::star::uno::Reference< com::sun::star::util::XNumberFormatsSupplier >& xFormatsSupplier )
1533 : {
1534 0 : if (nKey <= -1)
1535 : {
1536 0 : SvNumberFormatter* pFormatter = NULL;
1537 : SvNumberFormatsSupplierObj* pObj =
1538 0 : SvNumberFormatsSupplierObj::getImplementation( xFormatsSupplier );
1539 0 : if (pObj)
1540 0 : pFormatter = pObj->GetNumberFormatter();
1541 :
1542 0 : if ( pFormatter )
1543 0 : return CreateAndInsert( pFormatter );
1544 : else
1545 0 : return -1;
1546 : }
1547 : else
1548 0 : return nKey;
1549 : }
1550 :
1551 1085 : void SvXMLNumFormatContext::CreateAndInsert(bool /*bOverwrite*/)
1552 : {
1553 1085 : if (!(nKey > -1))
1554 1085 : CreateAndInsert(pData->GetNumberFormatter());
1555 1085 : }
1556 :
1557 1085 : sal_Int32 SvXMLNumFormatContext::CreateAndInsert(SvNumberFormatter* pFormatter)
1558 : {
1559 1085 : if (!pFormatter)
1560 : {
1561 : OSL_FAIL("no number formatter");
1562 0 : return -1;
1563 : }
1564 :
1565 1085 : sal_uInt32 nIndex = NUMBERFORMAT_ENTRY_NOT_FOUND;
1566 :
1567 1431 : for (sal_uInt32 i = 0; i < aMyConditions.size(); i++)
1568 : {
1569 : SvXMLNumFormatContext* pStyle = (SvXMLNumFormatContext *)pStyles->FindStyleChildContext(
1570 346 : XML_STYLE_FAMILY_DATA_STYLE, aMyConditions[i].sMapName, false);
1571 346 : if (pStyle)
1572 : {
1573 346 : if ((pStyle->PrivateGetKey() > -1)) // don't reset pStyle's bRemoveAfterUse flag
1574 346 : AddCondition(i);
1575 : }
1576 : }
1577 :
1578 1085 : if ( aFormatCode.isEmpty() )
1579 : {
1580 : // insert empty format as empty string (with quotes)
1581 : // #93901# this check has to be done before inserting the conditions
1582 11 : aFormatCode.appendAscii("\"\""); // ""
1583 : }
1584 :
1585 1085 : aFormatCode.insert( 0, aConditions.makeStringAndClear() );
1586 1085 : OUString sFormat = aFormatCode.makeStringAndClear();
1587 :
1588 : // test special cases
1589 :
1590 1085 : if ( bAutoDec ) // automatic decimal places
1591 : {
1592 : // #99391# adjust only if the format contains no text elements, no conditions
1593 : // and no color definition (detected by the '[' at the start)
1594 :
1595 756 : if ( nType == XML_TOK_STYLES_NUMBER_STYLE && !bHasExtraText &&
1596 567 : aMyConditions.empty() && sFormat.toChar() != '[' )
1597 189 : nIndex = pFormatter->GetStandardIndex( nFormatLang );
1598 : }
1599 1085 : if ( bAutoInt ) // automatic integer digits
1600 : {
1601 : //! only if two decimal places was set?
1602 :
1603 0 : if ( nType == XML_TOK_STYLES_NUMBER_STYLE && !bHasExtraText &&
1604 0 : aMyConditions.empty() && sFormat.toChar() != '[' )
1605 0 : nIndex = pFormatter->GetFormatIndex( NF_NUMBER_SYSTEM, nFormatLang );
1606 : }
1607 :
1608 : // boolean is always the builtin boolean format
1609 : // (no other boolean formats are implemented)
1610 1085 : if ( nType == XML_TOK_STYLES_BOOLEAN_STYLE )
1611 11 : nIndex = pFormatter->GetFormatIndex( NF_BOOLEAN, nFormatLang );
1612 :
1613 : // check for default date formats
1614 1085 : if ( nType == XML_TOK_STYLES_DATE_STYLE && bAutoOrder && !bDateNoDefault )
1615 : {
1616 : NfIndexTableOffset eFormat = (NfIndexTableOffset) SvXMLNumFmtDefaults::GetDefaultDateFormat(
1617 : eDateDOW, eDateDay, eDateMonth, eDateYear,
1618 51 : eDateHours, eDateMins, eDateSecs, bFromSystem );
1619 51 : if ( eFormat < NF_INDEX_TABLE_LOCALE_DATA_DEFAULTS )
1620 : {
1621 : // #109651# if a date format has the automatic-order attribute and
1622 : // contains exactly the elements of one of the default date formats,
1623 : // use that default format, with the element order and separators
1624 : // from the current locale settings
1625 :
1626 51 : nIndex = pFormatter->GetFormatIndex( eFormat, nFormatLang );
1627 : }
1628 : }
1629 :
1630 1085 : if ( nIndex == NUMBERFORMAT_ENTRY_NOT_FOUND && !sFormat.isEmpty() )
1631 : {
1632 : // insert by format string
1633 :
1634 834 : OUString aFormatStr( sFormat );
1635 834 : nIndex = pFormatter->GetEntryKey( aFormatStr, nFormatLang );
1636 834 : if ( nIndex == NUMBERFORMAT_ENTRY_NOT_FOUND )
1637 : {
1638 630 : sal_Int32 nErrPos = 0;
1639 630 : short l_nType = 0;
1640 630 : bool bOk = pFormatter->PutEntry( aFormatStr, nErrPos, l_nType, nIndex, nFormatLang );
1641 630 : if ( !bOk && nErrPos == 0 && aFormatStr != sFormat )
1642 : {
1643 : // if the string was modified by PutEntry, look for an existing format
1644 : // with the modified string
1645 1 : nIndex = pFormatter->GetEntryKey( aFormatStr, nFormatLang );
1646 1 : if ( nIndex != NUMBERFORMAT_ENTRY_NOT_FOUND )
1647 1 : bOk = true;
1648 : }
1649 630 : if (!bOk)
1650 0 : nIndex = NUMBERFORMAT_ENTRY_NOT_FOUND;
1651 834 : }
1652 : }
1653 :
1654 : //! I18N doesn't provide SYSTEM or extended date information yet
1655 1085 : if ( nIndex != NUMBERFORMAT_ENTRY_NOT_FOUND && !bAutoOrder )
1656 : {
1657 : // use fixed-order formats instead of SYS... if bAutoOrder is false
1658 : // (only if the format strings are equal for the locale)
1659 :
1660 1034 : NfIndexTableOffset eOffset = pFormatter->GetIndexTableOffset( nIndex );
1661 1034 : if ( eOffset == NF_DATE_SYS_DMMMYYYY )
1662 : {
1663 0 : sal_uInt32 nNewIndex = pFormatter->GetFormatIndex( NF_DATE_DIN_DMMMYYYY, nFormatLang );
1664 0 : const SvNumberformat* pOldEntry = pFormatter->GetEntry( nIndex );
1665 0 : const SvNumberformat* pNewEntry = pFormatter->GetEntry( nNewIndex );
1666 0 : if ( pOldEntry && pNewEntry && pOldEntry->GetFormatstring() == pNewEntry->GetFormatstring() )
1667 0 : nIndex = nNewIndex;
1668 : }
1669 1034 : else if ( eOffset == NF_DATE_SYS_DMMMMYYYY )
1670 : {
1671 0 : sal_uInt32 nNewIndex = pFormatter->GetFormatIndex( NF_DATE_DIN_DMMMMYYYY, nFormatLang );
1672 0 : const SvNumberformat* pOldEntry = pFormatter->GetEntry( nIndex );
1673 0 : const SvNumberformat* pNewEntry = pFormatter->GetEntry( nNewIndex );
1674 0 : if ( pOldEntry && pNewEntry && pOldEntry->GetFormatstring() == pNewEntry->GetFormatstring() )
1675 0 : nIndex = nNewIndex;
1676 : }
1677 : }
1678 :
1679 1085 : if ((nIndex != NUMBERFORMAT_ENTRY_NOT_FOUND) && !sFormatTitle.isEmpty())
1680 : {
1681 0 : SvNumberformat* pFormat = const_cast<SvNumberformat*>(pFormatter->GetEntry( nIndex ));
1682 0 : if (pFormat)
1683 : {
1684 0 : pFormat->SetComment(sFormatTitle);
1685 : }
1686 : }
1687 :
1688 1085 : if ( nIndex == NUMBERFORMAT_ENTRY_NOT_FOUND )
1689 : {
1690 : OSL_FAIL("invalid number format");
1691 0 : nIndex = pFormatter->GetStandardIndex( nFormatLang );
1692 : }
1693 :
1694 1085 : pData->AddKey( nIndex, GetName(), bRemoveAfterUse );
1695 1085 : nKey = nIndex;
1696 :
1697 : // Add to import's list of keys (shared between styles and content import)
1698 : // only if not volatile - formats are removed from NumberFormatter at the
1699 : // end of each import (in SvXMLNumFmtHelper dtor).
1700 : // If bRemoveAfterUse is reset later in GetKey, AddNumberStyle is called there.
1701 :
1702 1085 : if (!bRemoveAfterUse)
1703 739 : GetImport().AddNumberStyle( nKey, GetName() );
1704 :
1705 1085 : return nKey;
1706 : }
1707 :
1708 885 : void SvXMLNumFormatContext::Finish( bool bOverwrite )
1709 : {
1710 885 : SvXMLStyleContext::Finish( bOverwrite );
1711 885 : }
1712 :
1713 652 : const LocaleDataWrapper& SvXMLNumFormatContext::GetLocaleData() const
1714 : {
1715 652 : return pData->GetLocaleData( nFormatLang );
1716 : }
1717 :
1718 287 : void SvXMLNumFormatContext::AddToCode( sal_Unicode c )
1719 : {
1720 287 : aFormatCode.append( c );
1721 287 : bHasExtraText = true;
1722 287 : }
1723 :
1724 1080 : void SvXMLNumFormatContext::AddToCode( const OUString& rString )
1725 : {
1726 1080 : aFormatCode.append( rString );
1727 1080 : bHasExtraText = true;
1728 1080 : }
1729 :
1730 837 : void SvXMLNumFormatContext::AddNumber( const SvXMLNumberInfo& rInfo )
1731 : {
1732 837 : SvNumberFormatter* pFormatter = pData->GetNumberFormatter();
1733 837 : if (!pFormatter)
1734 189 : return;
1735 :
1736 : // store special conditions
1737 837 : bAutoDec = ( rInfo.nDecimals < 0 );
1738 837 : bAutoInt = ( rInfo.nInteger < 0 );
1739 :
1740 837 : sal_uInt16 nPrec = 0;
1741 837 : sal_uInt16 nLeading = 0;
1742 837 : if ( rInfo.nDecimals >= 0 ) // < 0 : Default
1743 648 : nPrec = (sal_uInt16) rInfo.nDecimals;
1744 837 : if ( rInfo.nInteger >= 0 ) // < 0 : Default
1745 837 : nLeading = (sal_uInt16) rInfo.nInteger;
1746 :
1747 837 : if ( bAutoDec )
1748 : {
1749 189 : if ( nType == XML_TOK_STYLES_CURRENCY_STYLE )
1750 : {
1751 : // for currency formats, "automatic decimals" is used for the automatic
1752 : // currency format with (fixed) decimals from the locale settings
1753 :
1754 0 : const LocaleDataWrapper& rLoc = pData->GetLocaleData( nFormatLang );
1755 0 : nPrec = rLoc.getCurrDigits();
1756 : }
1757 : else
1758 : {
1759 : // for other types, "automatic decimals" means dynamic determination of
1760 : // decimals, as achieved with the "general" keyword
1761 :
1762 189 : aFormatCode.append( pFormatter->GetStandardName( nFormatLang ) );
1763 189 : return;
1764 : }
1765 : }
1766 648 : if ( bAutoInt )
1767 : {
1768 : //!...
1769 : }
1770 :
1771 648 : sal_uInt16 nGenPrec = nPrec;
1772 648 : if ( rInfo.bDecReplace || rInfo.bVarDecimals )
1773 0 : nGenPrec = 0; // generate format without decimals...
1774 :
1775 648 : bool bGrouping = rInfo.bGrouping;
1776 648 : sal_uInt16 nEmbeddedCount = rInfo.aEmbeddedElements.size();
1777 648 : if ( nEmbeddedCount )
1778 0 : bGrouping = false; // grouping and embedded characters can't be used together
1779 :
1780 648 : sal_uInt32 nStdIndex = pFormatter->GetStandardIndex( nFormatLang );
1781 : OUStringBuffer aNumStr = pFormatter->GenerateFormat( nStdIndex, nFormatLang,
1782 648 : bGrouping, false, nGenPrec, nLeading );
1783 :
1784 648 : if ( rInfo.nExpDigits >= 0 && nLeading == 0 && !bGrouping && nEmbeddedCount == 0 )
1785 : {
1786 : // #i43959# For scientific numbers, "#" in the integer part forces a digit,
1787 : // so it has to be removed if nLeading is 0 (".00E+0", not "#.00E+0").
1788 :
1789 0 : aNumStr.stripStart('#');
1790 : }
1791 :
1792 648 : if ( nEmbeddedCount )
1793 : {
1794 : // insert embedded strings into number string
1795 : // only the integer part is supported
1796 : // nZeroPos is the string position where format position 0 is inserted
1797 :
1798 0 : sal_Int32 nZeroPos = aNumStr.indexOf( pData->GetLocaleData( nFormatLang ).getNumDecimalSep() );
1799 0 : if ( nZeroPos < 0 )
1800 : {
1801 0 : nZeroPos = aNumStr.getLength();
1802 : }
1803 :
1804 : // aEmbeddedElements is sorted - last entry has the largest position (leftmost)
1805 0 : const SvXMLEmbeddedElement* pLastObj = &*rInfo.aEmbeddedElements.rbegin();
1806 0 : sal_Int32 nLastFormatPos = pLastObj->nFormatPos;
1807 0 : if ( nLastFormatPos >= nZeroPos )
1808 : {
1809 : // add '#' characters so all embedded texts are really embedded in digits
1810 : // (there always has to be a digit before the leftmost embedded text)
1811 :
1812 0 : sal_Int32 nAddCount = nLastFormatPos + 1 - nZeroPos;
1813 0 : for(sal_Int32 index = 0; index < nAddCount; ++index)
1814 : {
1815 0 : aNumStr.insert(0, '#');
1816 : }
1817 0 : nZeroPos = nZeroPos + nAddCount;
1818 : }
1819 :
1820 : // aEmbeddedElements is sorted with ascending positions - loop is from right to left
1821 0 : for (SvXMLEmbeddedElementArr::const_iterator it = rInfo.aEmbeddedElements.begin();
1822 0 : it != rInfo.aEmbeddedElements.end(); ++it)
1823 : {
1824 0 : const SvXMLEmbeddedElement* pObj = &*it;
1825 0 : sal_Int32 nFormatPos = pObj->nFormatPos;
1826 0 : sal_Int32 nInsertPos = nZeroPos - nFormatPos;
1827 0 : if ( nFormatPos >= 0 && nInsertPos >= 0 )
1828 : {
1829 : // #107805# always quote embedded strings - even space would otherwise
1830 : // be recognized as thousands separator in French.
1831 :
1832 0 : aNumStr.insert(nInsertPos, '"');
1833 0 : aNumStr.insert(nInsertPos, pObj->aText);
1834 0 : aNumStr.insert(nInsertPos, '"');
1835 : }
1836 : }
1837 : }
1838 :
1839 648 : aFormatCode.append( aNumStr.makeStringAndClear() );
1840 :
1841 648 : if ( ( rInfo.bDecReplace || rInfo.bVarDecimals ) && nPrec ) // add decimal replacement (dashes)
1842 : {
1843 : // add dashes for explicit decimal replacement, # for variable decimals
1844 0 : sal_Unicode cAdd = rInfo.bDecReplace ? '-' : '#';
1845 :
1846 0 : aFormatCode.append( pData->GetLocaleData( nFormatLang ).getNumDecimalSep() );
1847 0 : for ( sal_uInt16 i=0; i<nPrec; i++)
1848 0 : aFormatCode.append( cAdd );
1849 : }
1850 :
1851 : // add extra thousands separators for display factor
1852 :
1853 648 : if ( rInfo.fDisplayFactor != 1.0 && rInfo.fDisplayFactor > 0.0 )
1854 : {
1855 : // test for 1.0 is just for optimization - nSepCount would be 0
1856 :
1857 : // one separator for each factor of 1000
1858 0 : sal_Int32 nSepCount = (sal_Int32) ::rtl::math::round( log10(rInfo.fDisplayFactor) / 3.0 );
1859 0 : if ( nSepCount > 0 )
1860 : {
1861 0 : OUString aSep = pData->GetLocaleData( nFormatLang ).getNumThousandSep();
1862 0 : for ( sal_Int32 i=0; i<nSepCount; i++ )
1863 0 : aFormatCode.append( aSep );
1864 : }
1865 648 : }
1866 : }
1867 :
1868 340 : void SvXMLNumFormatContext::AddCurrency( const OUString& rContent, LanguageType nLang )
1869 : {
1870 340 : bool bAutomatic = false;
1871 340 : OUString aSymbol = rContent;
1872 340 : if ( aSymbol.isEmpty())
1873 : {
1874 74 : SvNumberFormatter* pFormatter = pData->GetNumberFormatter();
1875 74 : if ( pFormatter )
1876 : {
1877 74 : pFormatter->ChangeIntl( nFormatLang );
1878 148 : OUString sCurString, sDummy;
1879 74 : pFormatter->GetCompatibilityCurrency( sCurString, sDummy );
1880 74 : aSymbol = sCurString;
1881 :
1882 148 : bAutomatic = true;
1883 : }
1884 : }
1885 266 : else if ( nLang == LANGUAGE_SYSTEM && aSymbol.equalsAscii("CCC") )
1886 : {
1887 : // "CCC" is used for automatic long symbol
1888 0 : bAutomatic = true;
1889 : }
1890 :
1891 340 : if ( bAutomatic )
1892 : {
1893 : // remove unnecessary quotes before automatic symbol (formats like "-(0DM)")
1894 : // otherwise the currency symbol isn't recognized (#94048#)
1895 :
1896 74 : sal_Int32 nLength = aFormatCode.getLength();
1897 74 : if ( nLength > 1 && aFormatCode[nLength - 1] == '"' )
1898 : {
1899 : // find start of quoted string
1900 : // When SvXMLNumFmtElementContext::EndElement creates escaped quotes,
1901 : // they must be handled here, too.
1902 :
1903 0 : sal_Int32 nFirst = nLength - 2;
1904 0 : while ( nFirst >= 0 && aFormatCode[nFirst] != '"' )
1905 0 : --nFirst;
1906 0 : if ( nFirst >= 0 )
1907 : {
1908 : // remove both quotes from aFormatCode
1909 0 : OUString aOld = aFormatCode.makeStringAndClear();
1910 0 : if ( nFirst > 0 )
1911 0 : aFormatCode.append( aOld.copy( 0, nFirst ) );
1912 0 : if ( nLength > nFirst + 2 )
1913 0 : aFormatCode.append( aOld.copy( nFirst + 1, nLength - nFirst - 2 ) );
1914 : }
1915 : }
1916 : }
1917 :
1918 340 : if (!bAutomatic)
1919 266 : aFormatCode.appendAscii( "[$" ); // intro for "new" currency symbols
1920 :
1921 340 : aFormatCode.append( aSymbol );
1922 :
1923 340 : if (!bAutomatic)
1924 : {
1925 266 : if ( nLang != LANGUAGE_SYSTEM )
1926 : {
1927 : // '-' sign and language code in hex:
1928 256 : aFormatCode.append("-" + OUString::number(sal_Int32(nLang), 16).toAsciiUpperCase());
1929 : }
1930 :
1931 266 : aFormatCode.append( ']' ); // end of "new" currency symbol
1932 340 : }
1933 340 : }
1934 :
1935 441 : void SvXMLNumFormatContext::AddNfKeyword( sal_uInt16 nIndex )
1936 : {
1937 441 : SvNumberFormatter* pFormatter = pData->GetNumberFormatter();
1938 441 : if (!pFormatter)
1939 441 : return;
1940 :
1941 441 : if ( nIndex == NF_KEY_G || nIndex == NF_KEY_GG || nIndex == NF_KEY_GGG )
1942 0 : bHasEra = true;
1943 :
1944 441 : if ( nIndex == NF_KEY_NNNN )
1945 : {
1946 0 : nIndex = NF_KEY_NNN;
1947 0 : bHasLongDoW = true; // to remove string constant with separator
1948 : }
1949 :
1950 441 : OUString sKeyword = pFormatter->GetKeyword( nFormatLang, nIndex );
1951 :
1952 441 : if ( nIndex == NF_KEY_H || nIndex == NF_KEY_HH ||
1953 390 : nIndex == NF_KEY_MI || nIndex == NF_KEY_MMI ||
1954 323 : nIndex == NF_KEY_S || nIndex == NF_KEY_SS )
1955 : {
1956 158 : if ( !bTruncate && !bHasDateTime )
1957 : {
1958 : // with truncate-on-overflow = false, add "[]" to first time part
1959 8 : aFormatCode.append("[" + sKeyword + "]");
1960 : }
1961 : else
1962 : {
1963 150 : aFormatCode.append( sKeyword );
1964 : }
1965 158 : bHasDateTime = true;
1966 : }
1967 : else
1968 : {
1969 283 : aFormatCode.append( sKeyword );
1970 : }
1971 : // collect the date elements that the format contains, to recognize default date formats
1972 441 : switch ( nIndex )
1973 : {
1974 0 : case NF_KEY_NN: eDateDOW = XML_DEA_SHORT; break;
1975 : case NF_KEY_NNN:
1976 0 : case NF_KEY_NNNN: eDateDOW = XML_DEA_LONG; break;
1977 30 : case NF_KEY_D: eDateDay = XML_DEA_SHORT; break;
1978 56 : case NF_KEY_DD: eDateDay = XML_DEA_LONG; break;
1979 16 : case NF_KEY_M: eDateMonth = XML_DEA_SHORT; break;
1980 54 : case NF_KEY_MM: eDateMonth = XML_DEA_LONG; break;
1981 24 : case NF_KEY_MMM: eDateMonth = XML_DEA_TEXTSHORT; break;
1982 2 : case NF_KEY_MMMM: eDateMonth = XML_DEA_TEXTLONG; break;
1983 25 : case NF_KEY_YY: eDateYear = XML_DEA_SHORT; break;
1984 60 : case NF_KEY_YYYY: eDateYear = XML_DEA_LONG; break;
1985 45 : case NF_KEY_H: eDateHours = XML_DEA_SHORT; break;
1986 6 : case NF_KEY_HH: eDateHours = XML_DEA_LONG; break;
1987 0 : case NF_KEY_MI: eDateMins = XML_DEA_SHORT; break;
1988 67 : case NF_KEY_MMI: eDateMins = XML_DEA_LONG; break;
1989 0 : case NF_KEY_S: eDateSecs = XML_DEA_SHORT; break;
1990 40 : case NF_KEY_SS: eDateSecs = XML_DEA_LONG; break;
1991 : case NF_KEY_AP:
1992 16 : case NF_KEY_AMPM: break; // AM/PM may or may not be in date/time formats -> ignore by itself
1993 : default:
1994 0 : bDateNoDefault = true; // any other element -> no default format
1995 441 : }
1996 : }
1997 :
1998 0 : static bool lcl_IsAtEnd( OUStringBuffer& rBuffer, const OUString& rToken )
1999 : {
2000 0 : sal_Int32 nBufLen = rBuffer.getLength();
2001 0 : sal_Int32 nTokLen = rToken.getLength();
2002 :
2003 0 : if ( nTokLen > nBufLen )
2004 0 : return false;
2005 :
2006 0 : sal_Int32 nStartPos = nBufLen - nTokLen;
2007 0 : for ( sal_Int32 nTokPos = 0; nTokPos < nTokLen; nTokPos++ )
2008 0 : if ( rToken[ nTokPos ] != rBuffer[nStartPos + nTokPos] )
2009 0 : return false;
2010 :
2011 0 : return true;
2012 : }
2013 :
2014 0 : bool SvXMLNumFormatContext::ReplaceNfKeyword( sal_uInt16 nOld, sal_uInt16 nNew )
2015 : {
2016 : // replaces one keyword with another if it is found at the end of the code
2017 :
2018 0 : SvNumberFormatter* pFormatter = pData->GetNumberFormatter();
2019 0 : if (!pFormatter)
2020 0 : return false;
2021 :
2022 0 : OUString sOldStr = pFormatter->GetKeyword( nFormatLang, nOld );
2023 0 : if ( lcl_IsAtEnd( aFormatCode, sOldStr ) )
2024 : {
2025 : // remove old keyword
2026 0 : aFormatCode.setLength( aFormatCode.getLength() - sOldStr.getLength() );
2027 :
2028 : // add new keyword
2029 0 : OUString sNewStr = pFormatter->GetKeyword( nFormatLang, nNew );
2030 0 : aFormatCode.append( sNewStr );
2031 :
2032 0 : return true; // changed
2033 : }
2034 0 : return false; // not found
2035 : }
2036 :
2037 346 : void SvXMLNumFormatContext::AddCondition( const sal_Int32 nIndex )
2038 : {
2039 346 : OUString rApplyName = aMyConditions[nIndex].sMapName;
2040 692 : OUString rCondition = aMyConditions[nIndex].sCondition;
2041 346 : SvNumberFormatter* pFormatter = pData->GetNumberFormatter();
2042 346 : sal_uInt32 l_nKey = pData->GetKeyForName( rApplyName );
2043 692 : OUString sValue("value()"); //! define constant
2044 346 : sal_Int32 nValLen = sValue.getLength();
2045 :
2046 1384 : if ( pFormatter && l_nKey != NUMBERFORMAT_ENTRY_NOT_FOUND &&
2047 1384 : rCondition.copy( 0, nValLen ) == sValue )
2048 : {
2049 : //! test for valid conditions
2050 : //! test for default conditions
2051 :
2052 346 : OUString sRealCond = rCondition.copy( nValLen, rCondition.getLength() - nValLen );
2053 346 : bool bDefaultCond = false;
2054 :
2055 : //! collect all conditions first and adjust default to >=0, >0 or <0 depending on count
2056 : //! allow blanks in conditions
2057 346 : if ( aConditions.isEmpty() && aMyConditions.size() == 1 && sRealCond.equalsAscii( ">=0" ) )
2058 214 : bDefaultCond = true;
2059 :
2060 346 : if ( nType == XML_TOK_STYLES_TEXT_STYLE && nIndex == 2 )
2061 : {
2062 : // The third condition in a number format with a text part can only be
2063 : // "all other numbers", the condition string must be empty.
2064 40 : bDefaultCond = true;
2065 : }
2066 :
2067 346 : if (!bDefaultCond)
2068 : {
2069 : // Convert != to <>
2070 92 : sal_Int32 nPos = sRealCond.indexOf( "!=" );
2071 92 : if ( nPos >= 0 )
2072 : {
2073 0 : sRealCond = sRealCond.replaceAt( nPos, 2, "<>" );
2074 : }
2075 :
2076 92 : nPos = sRealCond.indexOf( '.' );
2077 92 : if ( nPos >= 0 )
2078 : {
2079 : // #i8026# #103991# localize decimal separator
2080 0 : const OUString& rDecSep = GetLocaleData().getNumDecimalSep();
2081 0 : if ( rDecSep.getLength() > 1 || rDecSep[0] != '.' )
2082 : {
2083 0 : sRealCond = sRealCond.replaceAt( nPos, 1, rDecSep );
2084 : }
2085 : }
2086 92 : aConditions.append("[" + sRealCond + "]");
2087 : }
2088 :
2089 346 : const SvNumberformat* pFormat = pFormatter->GetEntry(l_nKey);
2090 346 : if ( pFormat )
2091 346 : aConditions.append( OUString( pFormat->GetFormatstring() ) );
2092 :
2093 346 : aConditions.append( ';' );
2094 346 : }
2095 346 : }
2096 :
2097 346 : void SvXMLNumFormatContext::AddCondition( const OUString& rCondition, const OUString& rApplyName )
2098 : {
2099 346 : MyCondition aCondition;
2100 346 : aCondition.sCondition = rCondition;
2101 346 : aCondition.sMapName = rApplyName;
2102 346 : aMyConditions.push_back(aCondition);
2103 346 : }
2104 :
2105 179 : void SvXMLNumFormatContext::AddColor( sal_uInt32 const nColor )
2106 : {
2107 179 : SvNumberFormatter* pFormatter = pData->GetNumberFormatter();
2108 179 : if (!pFormatter)
2109 179 : return;
2110 :
2111 179 : OUStringBuffer aColName;
2112 895 : for ( sal_uInt16 i=0; i<XML_NUMF_COLORCOUNT; i++ )
2113 895 : if (nColor == aNumFmtStdColors[i])
2114 : {
2115 179 : aColName = OUString( pFormatter->GetKeyword( nFormatLang, sal::static_int_cast< sal_uInt16 >(NF_KEY_FIRSTCOLOR + i) ) );
2116 179 : break;
2117 : }
2118 :
2119 179 : if ( !aColName.isEmpty() )
2120 : {
2121 179 : aColName.insert( 0, '[' );
2122 179 : aColName.append( ']' );
2123 179 : aFormatCode.insert( 0, aColName.makeStringAndClear() );
2124 179 : }
2125 : }
2126 :
2127 267 : void SvXMLNumFormatContext::UpdateCalendar( const OUString& rNewCalendar )
2128 : {
2129 267 : if ( rNewCalendar != sCalendar )
2130 : {
2131 0 : sCalendar = rNewCalendar;
2132 0 : if ( !sCalendar.isEmpty() )
2133 : {
2134 0 : aFormatCode.appendAscii( "[~" ); // intro for calendar code
2135 0 : aFormatCode.append( sCalendar );
2136 0 : aFormatCode.append( ']' ); // end of "new" currency symbolcalendar code
2137 : }
2138 : }
2139 267 : }
2140 :
2141 6 : bool SvXMLNumFormatContext::IsSystemLanguage()
2142 : {
2143 6 : return nFormatLang == LANGUAGE_SYSTEM;
2144 : }
2145 :
2146 :
2147 : // SvXMLNumFmtHelper
2148 :
2149 :
2150 564 : SvXMLNumFmtHelper::SvXMLNumFmtHelper(
2151 : const uno::Reference<util::XNumberFormatsSupplier>& rSupp,
2152 : const uno::Reference<uno::XComponentContext>& rxContext )
2153 : {
2154 : DBG_ASSERT( rxContext.is(), "got no service manager" );
2155 :
2156 564 : SvNumberFormatter* pFormatter = NULL;
2157 : SvNumberFormatsSupplierObj* pObj =
2158 564 : SvNumberFormatsSupplierObj::getImplementation( rSupp );
2159 564 : if (pObj)
2160 564 : pFormatter = pObj->GetNumberFormatter();
2161 :
2162 564 : pData = new SvXMLNumImpData( pFormatter, rxContext );
2163 564 : }
2164 :
2165 82 : SvXMLNumFmtHelper::SvXMLNumFmtHelper(
2166 : SvNumberFormatter* pNumberFormatter,
2167 : const uno::Reference<uno::XComponentContext>& rxContext )
2168 : {
2169 : DBG_ASSERT( rxContext.is(), "got no service manager" );
2170 :
2171 82 : pData = new SvXMLNumImpData( pNumberFormatter, rxContext );
2172 82 : }
2173 :
2174 646 : SvXMLNumFmtHelper::~SvXMLNumFmtHelper()
2175 : {
2176 : // remove temporary (volatile) formats from NumberFormatter
2177 646 : pData->RemoveVolatileFormats();
2178 :
2179 646 : delete pData;
2180 646 : }
2181 :
2182 8902 : SvXMLStyleContext* SvXMLNumFmtHelper::CreateChildContext( SvXMLImport& rImport,
2183 : sal_uInt16 nPrefix, const OUString& rLocalName,
2184 : const uno::Reference<xml::sax::XAttributeList>& xAttrList,
2185 : SvXMLStylesContext& rStyles )
2186 : {
2187 8902 : SvXMLStyleContext* pContext = NULL;
2188 :
2189 8902 : const SvXMLTokenMap& rTokenMap = pData->GetStylesElemTokenMap();
2190 8902 : sal_uInt16 nToken = rTokenMap.Get( nPrefix, rLocalName );
2191 8902 : switch (nToken)
2192 : {
2193 : case XML_TOK_STYLES_NUMBER_STYLE:
2194 : case XML_TOK_STYLES_CURRENCY_STYLE:
2195 : case XML_TOK_STYLES_PERCENTAGE_STYLE:
2196 : case XML_TOK_STYLES_DATE_STYLE:
2197 : case XML_TOK_STYLES_TIME_STYLE:
2198 : case XML_TOK_STYLES_BOOLEAN_STYLE:
2199 : case XML_TOK_STYLES_TEXT_STYLE:
2200 : pContext = new SvXMLNumFormatContext( rImport, nPrefix, rLocalName,
2201 1087 : pData, nToken, xAttrList, rStyles );
2202 1087 : break;
2203 : }
2204 :
2205 : // return NULL if not a data style, caller must handle other elements
2206 8902 : return pContext;
2207 : }
2208 :
2209 810 : const SvXMLTokenMap& SvXMLNumFmtHelper::GetStylesElemTokenMap()
2210 : {
2211 810 : return pData->GetStylesElemTokenMap();
2212 : }
2213 :
2214 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|