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 0 : struct SvXMLNumFmtEntry
53 : {
54 : OUString aName;
55 : sal_uInt32 nKey;
56 : bool bRemoveAfterUse;
57 :
58 0 : SvXMLNumFmtEntry( const OUString& rN, sal_uInt32 nK, bool bR ) :
59 0 : 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 0 : 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 0 : 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 0 : SvXMLNumberInfo()
122 0 : {
123 0 : nDecimals = nInteger = nExpDigits = nNumerDigits = nDenomDigits = nFracDenominator = -1;
124 0 : bGrouping = bDecReplace = bVarDecimals = false;
125 0 : fDisplayFactor = 1.0;
126 0 : }
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 0 : 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 0 : m_xContext(rxContext)
376 : {
377 : DBG_ASSERT( rxContext.is(), "got no service manager" );
378 0 : }
379 :
380 0 : SvXMLNumImpData::~SvXMLNumImpData()
381 : {
382 0 : delete pStylesElemTokenMap;
383 0 : delete pStyleElemTokenMap;
384 0 : delete pStyleAttrTokenMap;
385 0 : delete pStyleElemAttrTokenMap;
386 0 : delete pLocaleData;
387 0 : }
388 :
389 0 : sal_uInt32 SvXMLNumImpData::GetKeyForName( const OUString& rName )
390 : {
391 0 : sal_uInt16 nCount = aNameEntries.size();
392 0 : for (sal_uInt16 i=0; i<nCount; i++)
393 : {
394 0 : const SvXMLNumFmtEntry* pObj = &aNameEntries[i];
395 0 : if ( pObj->aName == rName )
396 0 : return pObj->nKey; // found
397 : }
398 0 : return NUMBERFORMAT_ENTRY_NOT_FOUND;
399 : }
400 :
401 0 : void SvXMLNumImpData::AddKey( sal_uInt32 nKey, const OUString& rName, bool bRemoveAfterUse )
402 : {
403 0 : 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 0 : sal_uInt16 nCount = aNameEntries.size();
409 0 : for (sal_uInt16 i=0; i<nCount; i++)
410 : {
411 0 : SvXMLNumFmtEntry* pObj = &aNameEntries[i];
412 0 : 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 0 : SetUsed( nKey );
423 : }
424 :
425 0 : SvXMLNumFmtEntry* pObj = new SvXMLNumFmtEntry( rName, nKey, bRemoveAfterUse );
426 0 : aNameEntries.push_back( pObj );
427 0 : }
428 :
429 0 : void SvXMLNumImpData::SetUsed( sal_uInt32 nKey )
430 : {
431 0 : sal_uInt16 nCount = aNameEntries.size();
432 0 : for (sal_uInt16 i=0; i<nCount; i++)
433 : {
434 0 : SvXMLNumFmtEntry* pObj = &aNameEntries[i];
435 0 : if ( pObj->nKey == nKey )
436 : {
437 0 : 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 0 : }
445 :
446 0 : 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 0 : if ( !pFormatter )
453 0 : return;
454 :
455 0 : sal_uInt16 nCount = aNameEntries.size();
456 0 : for (sal_uInt16 i=0; i<nCount; i++)
457 : {
458 0 : const SvXMLNumFmtEntry* pObj = &aNameEntries[i];
459 0 : if ( pObj->bRemoveAfterUse )
460 : {
461 0 : const SvNumberformat* pFormat = pFormatter->GetEntry(pObj->nKey);
462 0 : if (pFormat && (pFormat->GetType() & NUMBERFORMAT_DEFINED))
463 0 : pFormatter->DeleteEntry( pObj->nKey );
464 : }
465 : }
466 : }
467 :
468 0 : const SvXMLTokenMap& SvXMLNumImpData::GetStylesElemTokenMap()
469 : {
470 0 : 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 0 : pStylesElemTokenMap = new SvXMLTokenMap( aStylesElemMap );
486 : }
487 0 : return *pStylesElemTokenMap;
488 : }
489 :
490 0 : const SvXMLTokenMap& SvXMLNumImpData::GetStyleElemTokenMap()
491 : {
492 0 : 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 0 : pStyleElemTokenMap = new SvXMLTokenMap( aStyleElemMap );
523 : }
524 0 : return *pStyleElemTokenMap;
525 : }
526 :
527 0 : const SvXMLTokenMap& SvXMLNumImpData::GetStyleAttrTokenMap()
528 : {
529 0 : 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 0 : pStyleAttrTokenMap = new SvXMLTokenMap( aStyleAttrMap );
554 : }
555 0 : return *pStyleAttrTokenMap;
556 : }
557 :
558 0 : const SvXMLTokenMap& SvXMLNumImpData::GetStyleElemAttrTokenMap()
559 : {
560 0 : 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 0 : pStyleElemAttrTokenMap = new SvXMLTokenMap( aStyleElemAttrMap );
585 : }
586 0 : return *pStyleElemAttrTokenMap;
587 : }
588 :
589 0 : const LocaleDataWrapper& SvXMLNumImpData::GetLocaleData( LanguageType nLang )
590 : {
591 0 : if ( !pLocaleData )
592 : pLocaleData = new LocaleDataWrapper(
593 : pFormatter ? pFormatter->GetComponentContext() : m_xContext,
594 0 : LanguageTag( nLang ) );
595 : else
596 0 : pLocaleData->setLanguageTag( LanguageTag( nLang ) );
597 0 : return *pLocaleData;
598 : }
599 :
600 :
601 : // SvXMLNumFmtMapContext
602 :
603 :
604 0 : 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 0 : rParent( rParentContext )
610 : {
611 0 : sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
612 0 : for( sal_Int16 i=0; i < nAttrCount; i++ )
613 : {
614 0 : OUString sAttrName = xAttrList->getNameByIndex( i );
615 0 : OUString sValue = xAttrList->getValueByIndex( i );
616 0 : OUString aLocalName;
617 0 : sal_uInt16 nPrefix = rImport.GetNamespaceMap().GetKeyByAttrName( sAttrName, &aLocalName );
618 0 : if ( nPrefix == XML_NAMESPACE_STYLE )
619 : {
620 0 : if ( IsXMLToken( aLocalName, XML_CONDITION) )
621 0 : sCondition = sValue;
622 0 : else if ( IsXMLToken( aLocalName, XML_APPLY_STYLE_NAME) )
623 0 : sName = sValue;
624 : }
625 0 : }
626 0 : }
627 :
628 0 : SvXMLNumFmtMapContext::~SvXMLNumFmtMapContext()
629 : {
630 0 : }
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 0 : void SvXMLNumFmtMapContext::EndElement()
645 : {
646 0 : rParent.AddCondition( sCondition, sName );
647 0 : }
648 :
649 :
650 : // SvXMLNumFmtPropContext
651 :
652 :
653 0 : 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 0 : bColSet( false )
660 : {
661 0 : sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
662 0 : for( sal_Int16 i=0; i < nAttrCount; i++ )
663 : {
664 0 : OUString sAttrName = xAttrList->getNameByIndex( i );
665 0 : OUString sValue = xAttrList->getValueByIndex( i );
666 0 : OUString aLocalName;
667 0 : sal_uInt16 nPrefix = rImport.GetNamespaceMap().GetKeyByAttrName( sAttrName, &aLocalName );
668 0 : if ( nPrefix == XML_NAMESPACE_FO && IsXMLToken( aLocalName, XML_COLOR ) )
669 : {
670 0 : bColSet = ::sax::Converter::convertColor( m_nColor, sValue );
671 : }
672 0 : }
673 0 : }
674 :
675 0 : SvXMLNumFmtPropContext::~SvXMLNumFmtPropContext()
676 : {
677 0 : }
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 0 : void SvXMLNumFmtPropContext::EndElement()
692 : {
693 0 : if (bColSet)
694 0 : rParent.AddColor( m_nColor );
695 0 : }
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 0 : static bool lcl_ValidChar( sal_Unicode cChar, const SvXMLNumFormatContext& rParent )
749 : {
750 0 : sal_uInt16 nFormatType = rParent.GetType();
751 :
752 : // Treat space equal to non-breaking space separator.
753 0 : const sal_Unicode cNBSP = 0x00A0;
754 : sal_Unicode cTS;
755 0 : if ( ( nFormatType == XML_TOK_STYLES_NUMBER_STYLE ||
756 0 : nFormatType == XML_TOK_STYLES_CURRENCY_STYLE ||
757 0 : nFormatType == XML_TOK_STYLES_PERCENTAGE_STYLE ) &&
758 0 : (cChar == (cTS = rParent.GetLocaleData().getNumThousandSep()[0]) ||
759 0 : (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 0 : if ( cChar == ' ' ||
772 0 : cChar == '-' ||
773 0 : cChar == '/' ||
774 0 : cChar == '.' ||
775 0 : cChar == ',' ||
776 0 : cChar == ':' ||
777 : cChar == '\'' )
778 0 : return true; // for all format types
779 :
780 : // percent sign must be used without quotes for percentage styles only
781 0 : if ( nFormatType == XML_TOK_STYLES_PERCENTAGE_STYLE && cChar == '%' )
782 0 : return true;
783 :
784 : // don't put quotes around single parentheses (often used for negative numbers)
785 0 : if ( ( nFormatType == XML_TOK_STYLES_NUMBER_STYLE ||
786 0 : nFormatType == XML_TOK_STYLES_CURRENCY_STYLE ||
787 0 : nFormatType == XML_TOK_STYLES_PERCENTAGE_STYLE ) &&
788 0 : ( cChar == '(' || cChar == ')' ) )
789 0 : return true;
790 :
791 0 : return false;
792 : }
793 :
794 0 : static void lcl_EnquoteIfNecessary( OUStringBuffer& rContent, const SvXMLNumFormatContext& rParent )
795 : {
796 0 : bool bQuote = true;
797 0 : sal_Int32 nLength = rContent.getLength();
798 :
799 0 : if ( ( nLength == 1 &&
800 0 : lcl_ValidChar( rContent[0], rParent ) ) ||
801 0 : ( nLength == 2 &&
802 0 : lcl_ValidChar( rContent[0], rParent ) &&
803 0 : 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 0 : bQuote = false;
808 : }
809 0 : 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 0 : if ( bQuote )
850 : {
851 : // #i55469# quotes in the string itself have to be escaped
852 0 : bool bEscape = ( rContent.indexOf( '"' ) >= 0 );
853 0 : 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 0 : OUString aInsert( "\"\\\"" );
858 :
859 0 : sal_Int32 nPos = 0;
860 0 : while ( nPos < rContent.getLength() )
861 : {
862 0 : if ( rContent[nPos] == '"' )
863 : {
864 0 : rContent.insert( nPos, aInsert );
865 0 : nPos += aInsert.getLength();
866 : }
867 0 : ++nPos;
868 0 : }
869 : }
870 :
871 : // quote string literals
872 0 : rContent.insert( 0, '"' );
873 0 : rContent.append( '"' );
874 :
875 : // remove redundant double quotes at start or end
876 0 : if ( bEscape )
877 : {
878 0 : if ( rContent.getLength() > 2 &&
879 0 : rContent[0] == '"' &&
880 0 : rContent[1] == '"' )
881 : {
882 0 : rContent.remove(0, 2);
883 : }
884 :
885 0 : sal_Int32 nLen = rContent.getLength();
886 0 : if ( nLen > 2 &&
887 0 : rContent[nLen - 1] == '"' &&
888 0 : rContent[nLen - 2] == '"' )
889 : {
890 0 : rContent.truncate(nLen - 2);
891 : }
892 : }
893 : }
894 0 : }
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 0 : 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 0 : bTextual( false )
912 : {
913 0 : LanguageTagODF aLanguageTagODF;
914 : sal_Int32 nAttrVal;
915 0 : bool bAttrBool(false);
916 : sal_uInt16 nAttrEnum;
917 : double fAttrDouble;
918 :
919 0 : sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
920 0 : for( sal_Int16 i=0; i < nAttrCount; i++ )
921 : {
922 0 : OUString sAttrName = xAttrList->getNameByIndex( i );
923 0 : OUString sValue = xAttrList->getValueByIndex( i );
924 0 : OUString aLocalName;
925 0 : sal_uInt16 nPrefix = rImport.GetNamespaceMap().GetKeyByAttrName( sAttrName, &aLocalName );
926 :
927 0 : const SvXMLTokenMap& rTokenMap = rParent.GetData()->GetStyleElemAttrTokenMap();
928 0 : sal_uInt16 nToken = rTokenMap.Get( nPrefix, aLocalName );
929 :
930 0 : switch (nToken)
931 : {
932 : case XML_TOK_ELEM_ATTR_DECIMAL_PLACES:
933 0 : if (::sax::Converter::convertNumber( nAttrVal, sValue, 0 ))
934 0 : aNumInfo.nDecimals = std::min<sal_Int32>(nAttrVal, MAX_SECOND_DIGITS);
935 0 : break;
936 : case XML_TOK_ELEM_ATTR_MIN_INTEGER_DIGITS:
937 0 : if (::sax::Converter::convertNumber( nAttrVal, sValue, 0 ))
938 0 : aNumInfo.nInteger = nAttrVal;
939 0 : break;
940 : case XML_TOK_ELEM_ATTR_GROUPING:
941 0 : if (::sax::Converter::convertBool( bAttrBool, sValue ))
942 0 : aNumInfo.bGrouping = bAttrBool;
943 0 : 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 0 : if (::sax::Converter::convertNumber( nAttrVal, sValue, 0 ))
956 0 : aNumInfo.nExpDigits = nAttrVal;
957 0 : break;
958 : case XML_TOK_ELEM_ATTR_MIN_NUMERATOR_DIGITS:
959 0 : if (::sax::Converter::convertNumber( nAttrVal, sValue, 0 ))
960 0 : aNumInfo.nNumerDigits = nAttrVal;
961 0 : break;
962 : case XML_TOK_ELEM_ATTR_MIN_DENOMINATOR_DIGITS:
963 0 : if (::sax::Converter::convertNumber( nAttrVal, sValue, 0 ))
964 0 : aNumInfo.nDenomDigits = nAttrVal;
965 0 : 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 0 : aLanguageTagODF.maLanguage = sValue;
975 0 : break;
976 : case XML_TOK_ELEM_ATTR_SCRIPT:
977 0 : aLanguageTagODF.maScript = sValue;
978 0 : break;
979 : case XML_TOK_ELEM_ATTR_COUNTRY:
980 0 : aLanguageTagODF.maCountry = sValue;
981 0 : break;
982 : case XML_TOK_ELEM_ATTR_STYLE:
983 0 : if ( SvXMLUnitConverter::convertEnum( nAttrEnum, sValue, aStyleValueMap ) )
984 0 : bLong = (bool) nAttrEnum;
985 0 : break;
986 : case XML_TOK_ELEM_ATTR_TEXTUAL:
987 0 : if (::sax::Converter::convertBool( bAttrBool, sValue ))
988 0 : bTextual = bAttrBool;
989 0 : break;
990 : case XML_TOK_ELEM_ATTR_CALENDAR:
991 0 : sCalendar = sValue;
992 0 : break;
993 : }
994 0 : }
995 :
996 0 : if ( !aLanguageTagODF.isEmpty() )
997 : {
998 0 : nElementLang = aLanguageTagODF.getLanguageTag().getLanguageType( false);
999 0 : if ( nElementLang == LANGUAGE_DONTKNOW )
1000 0 : nElementLang = LANGUAGE_SYSTEM; //! error handling for unknown locales?
1001 0 : }
1002 0 : }
1003 :
1004 0 : SvXMLNumFmtElementContext::~SvXMLNumFmtElementContext()
1005 : {
1006 0 : }
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 0 : void SvXMLNumFmtElementContext::Characters( const OUString& rChars )
1024 : {
1025 0 : aContent.append( rChars );
1026 0 : }
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 0 : void SvXMLNumFmtElementContext::EndElement()
1053 : {
1054 0 : bool bEffLong = bLong;
1055 0 : switch (nType)
1056 : {
1057 : case XML_TOK_STYLE_TEXT:
1058 0 : if ( rParent.HasLongDoW() &&
1059 0 : 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 0 : if ( !aContent.isEmpty() )
1072 : {
1073 0 : lcl_EnquoteIfNecessary( aContent, rParent );
1074 0 : rParent.AddToCode( aContent.makeStringAndClear() );
1075 : }
1076 0 : break;
1077 :
1078 : case XML_TOK_STYLE_NUMBER:
1079 0 : rParent.AddNumber( aNumInfo );
1080 0 : break;
1081 :
1082 : case XML_TOK_STYLE_CURRENCY_SYMBOL:
1083 0 : rParent.AddCurrency( aContent.makeStringAndClear(), nElementLang );
1084 0 : break;
1085 :
1086 : case XML_TOK_STYLE_TEXT_CONTENT:
1087 0 : rParent.AddToCode( '@');
1088 0 : break;
1089 : case XML_TOK_STYLE_FILL_CHARACTER:
1090 0 : if ( !aContent.isEmpty() )
1091 : {
1092 0 : rParent.AddToCode( '*' );
1093 0 : rParent.AddToCode( aContent[0] );
1094 : }
1095 0 : break;
1096 : case XML_TOK_STYLE_BOOLEAN:
1097 : // ignored - only default boolean format is supported
1098 0 : break;
1099 :
1100 : case XML_TOK_STYLE_DAY:
1101 0 : 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 0 : bEffLong ? NF_KEY_DD : NF_KEY_D ) );
1107 0 : break;
1108 : case XML_TOK_STYLE_MONTH:
1109 0 : 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 0 : : ( bEffLong ? NF_KEY_MM : NF_KEY_M ) ) );
1117 0 : break;
1118 : case XML_TOK_STYLE_YEAR:
1119 0 : rParent.UpdateCalendar( sCalendar );
1120 : //! I18N doesn't provide SYSTEM or extended date information yet
1121 : // Y after G (era) is replaced by E
1122 0 : 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 0 : bEffLong ? NF_KEY_YYYY : NF_KEY_YY ) );
1130 0 : 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 0 : bEffLong ? NF_KEY_HH : NF_KEY_H ) );
1160 0 : break;
1161 : case XML_TOK_STYLE_AM_PM:
1162 : //! short/long?
1163 0 : rParent.AddNfKeyword( NF_KEY_AMPM );
1164 0 : break;
1165 : case XML_TOK_STYLE_MINUTES:
1166 : rParent.AddNfKeyword(
1167 : sal::static_int_cast< sal_uInt16 >(
1168 0 : bEffLong ? NF_KEY_MMI : NF_KEY_MI ) );
1169 0 : break;
1170 : case XML_TOK_STYLE_SECONDS:
1171 : rParent.AddNfKeyword(
1172 : sal::static_int_cast< sal_uInt16 >(
1173 0 : bEffLong ? NF_KEY_SS : NF_KEY_S ) );
1174 0 : if ( aNumInfo.nDecimals > 0 )
1175 : {
1176 : // manually add the decimal places
1177 0 : rParent.AddToCode(rParent.GetLocaleData().getNumDecimalSep());
1178 0 : for (sal_Int32 i=0; i<aNumInfo.nDecimals; i++)
1179 : {
1180 0 : rParent.AddToCode( '0');
1181 : }
1182 : }
1183 0 : break;
1184 :
1185 : case XML_TOK_STYLE_FRACTION:
1186 : {
1187 0 : if ( aNumInfo.nInteger >= 0 )
1188 : {
1189 : // add integer part only if min-integer-digits attribute is there
1190 0 : aNumInfo.nDecimals = 0;
1191 0 : rParent.AddNumber( aNumInfo ); // number without decimals
1192 0 : rParent.AddToCode( ' ' );
1193 : }
1194 :
1195 : //! build string and add at once
1196 :
1197 : sal_Int32 i;
1198 0 : for (i=0; i<aNumInfo.nNumerDigits; i++)
1199 : {
1200 0 : rParent.AddToCode( '?' );
1201 : }
1202 0 : rParent.AddToCode( '/' );
1203 0 : if ( aNumInfo.nFracDenominator > 0 )
1204 : {
1205 0 : rParent.AddToCode( OUString::number( aNumInfo.nFracDenominator ) );
1206 : }
1207 : else
1208 : {
1209 0 : for (i=0; i<aNumInfo.nDenomDigits; i++)
1210 : {
1211 0 : rParent.AddToCode( '?');
1212 : }
1213 : }
1214 : }
1215 0 : break;
1216 :
1217 : case XML_TOK_STYLE_SCIENTIFIC_NUMBER:
1218 : {
1219 0 : rParent.AddNumber( aNumInfo ); // simple number
1220 :
1221 0 : rParent.AddToCode( OUString("E+") );
1222 0 : for (sal_Int32 i=0; i<aNumInfo.nExpDigits; i++)
1223 : {
1224 0 : rParent.AddToCode( '0' );
1225 : }
1226 : }
1227 0 : break;
1228 :
1229 : default:
1230 : OSL_FAIL("invalid element ID");
1231 : }
1232 0 : }
1233 :
1234 0 : 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 0 : for (sal_uInt16 nPos=0; nPos<nCount; nPos++)
1242 : {
1243 0 : const SvXMLDefaultDateFormat& rEntry = aDefaultDateFormats[nPos];
1244 0 : if ( bSystem == rEntry.bSystem &&
1245 0 : ( eDOW == rEntry.eDOW || ( rEntry.eDOW == XML_DEA_ANY && eDOW != XML_DEA_NONE ) ) &&
1246 0 : ( eDay == rEntry.eDay || ( rEntry.eDay == XML_DEA_ANY && eDay != XML_DEA_NONE ) ) &&
1247 0 : ( eMonth == rEntry.eMonth || ( rEntry.eMonth == XML_DEA_ANY && eMonth != XML_DEA_NONE ) ) &&
1248 0 : ( eYear == rEntry.eYear || ( rEntry.eYear == XML_DEA_ANY && eYear != XML_DEA_NONE ) ) &&
1249 0 : ( eHours == rEntry.eHours || ( rEntry.eHours == XML_DEA_ANY && eHours != XML_DEA_NONE ) ) &&
1250 0 : ( eMins == rEntry.eMins || ( rEntry.eMins == XML_DEA_ANY && eMins != XML_DEA_NONE ) ) &&
1251 0 : ( eSecs == rEntry.eSecs || ( rEntry.eSecs == XML_DEA_ANY && eSecs != XML_DEA_NONE ) ) )
1252 : {
1253 0 : 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 0 : 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 0 : bDateNoDefault( false )
1294 : {
1295 0 : LanguageTagODF aLanguageTagODF;
1296 0 : OUString sNatNumAttrScript, sNatNumAttrRfcLanguageTag;
1297 0 : ::com::sun::star::i18n::NativeNumberXmlAttributes aNatNumAttr;
1298 0 : bool bAttrBool(false);
1299 : sal_uInt16 nAttrEnum;
1300 :
1301 0 : sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
1302 0 : for( sal_Int16 i=0; i < nAttrCount; i++ )
1303 : {
1304 0 : OUString sAttrName = xAttrList->getNameByIndex( i );
1305 0 : OUString sValue = xAttrList->getValueByIndex( i );
1306 0 : OUString aLocalName;
1307 0 : sal_uInt16 nPrefix = rImport.GetNamespaceMap().GetKeyByAttrName( sAttrName, &aLocalName );
1308 :
1309 0 : const SvXMLTokenMap& rTokenMap = pData->GetStyleAttrTokenMap();
1310 0 : sal_uInt16 nToken = rTokenMap.Get( nPrefix, aLocalName );
1311 0 : switch (nToken)
1312 : {
1313 : case XML_TOK_STYLE_ATTR_NAME:
1314 0 : 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 0 : aLanguageTagODF.maLanguage = sValue;
1320 0 : break;
1321 : case XML_TOK_STYLE_ATTR_SCRIPT:
1322 0 : aLanguageTagODF.maScript = sValue;
1323 0 : break;
1324 : case XML_TOK_STYLE_ATTR_COUNTRY:
1325 0 : aLanguageTagODF.maCountry = sValue;
1326 0 : break;
1327 : case XML_TOK_STYLE_ATTR_TITLE:
1328 0 : sFormatTitle = sValue;
1329 0 : break;
1330 : case XML_TOK_STYLE_ATTR_AUTOMATIC_ORDER:
1331 0 : if (::sax::Converter::convertBool( bAttrBool, sValue ))
1332 0 : bAutoOrder = bAttrBool;
1333 0 : break;
1334 : case XML_TOK_STYLE_ATTR_FORMAT_SOURCE:
1335 0 : if ( SvXMLUnitConverter::convertEnum( nAttrEnum, sValue, aFormatSourceMap ) )
1336 0 : bFromSystem = (bool) nAttrEnum;
1337 0 : break;
1338 : case XML_TOK_STYLE_ATTR_TRUNCATE_ON_OVERFLOW:
1339 0 : if (::sax::Converter::convertBool( bAttrBool, sValue ))
1340 0 : bTruncate = bAttrBool;
1341 0 : break;
1342 : case XML_TOK_STYLE_ATTR_VOLATILE:
1343 : // volatile formats can be removed after importing
1344 : // if not used in other styles
1345 0 : if (::sax::Converter::convertBool( bAttrBool, sValue ))
1346 0 : bRemoveAfterUse = bAttrBool;
1347 0 : break;
1348 : case XML_TOK_STYLE_ATTR_TRANSL_FORMAT:
1349 0 : aNatNumAttr.Format = sValue;
1350 0 : 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 0 : aNatNumAttr.Locale.Language = sValue;
1356 0 : 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 0 : aNatNumAttr.Locale.Country = sValue;
1362 0 : break;
1363 : case XML_TOK_STYLE_ATTR_TRANSL_STYLE:
1364 0 : aNatNumAttr.Style = sValue;
1365 0 : break;
1366 : }
1367 0 : }
1368 :
1369 0 : if (!aLanguageTagODF.isEmpty())
1370 : {
1371 0 : nFormatLang = aLanguageTagODF.getLanguageTag().getLanguageType( false);
1372 0 : if ( nFormatLang == LANGUAGE_DONTKNOW )
1373 0 : nFormatLang = LANGUAGE_SYSTEM; //! error handling for unknown locales?
1374 : }
1375 :
1376 0 : if ( !aNatNumAttr.Format.isEmpty() )
1377 : {
1378 0 : SvNumberFormatter* pFormatter = pData->GetNumberFormatter();
1379 0 : if ( pFormatter )
1380 : {
1381 : LanguageTag aLanguageTag( sNatNumAttrRfcLanguageTag, aNatNumAttr.Locale.Language,
1382 0 : sNatNumAttrScript, aNatNumAttr.Locale.Country);
1383 0 : aNatNumAttr.Locale = aLanguageTag.getLocale( false);
1384 :
1385 0 : sal_Int32 nNatNum = pFormatter->GetNatNum()->convertFromXmlAttributes( aNatNumAttr );
1386 0 : aFormatCode.append( "[NatNum" );
1387 0 : aFormatCode.append( nNatNum, 10 );
1388 :
1389 0 : LanguageType eLang = aLanguageTag.getLanguageType( false);
1390 0 : if ( eLang == LANGUAGE_DONTKNOW )
1391 0 : eLang = LANGUAGE_SYSTEM; //! error handling for unknown locales?
1392 0 : 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 0 : aFormatCode.append( ']' );
1399 : }
1400 0 : }
1401 0 : }
1402 :
1403 0 : 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 0 : bDateNoDefault( false )
1433 : {
1434 0 : SetAttribute(XML_NAMESPACE_STYLE, GetXMLToken(XML_NAME), rLName);
1435 0 : }
1436 :
1437 0 : SvXMLNumFormatContext::~SvXMLNumFormatContext()
1438 : {
1439 0 : }
1440 :
1441 0 : SvXMLImportContext* SvXMLNumFormatContext::CreateChildContext(
1442 : sal_uInt16 nPrfx, const OUString& rLName,
1443 : const uno::Reference<xml::sax::XAttributeList>& xAttrList )
1444 : {
1445 0 : SvXMLImportContext* pContext = NULL;
1446 :
1447 0 : const SvXMLTokenMap& rTokenMap = pData->GetStyleElemTokenMap();
1448 0 : sal_uInt16 nToken = rTokenMap.Get( nPrfx, rLName );
1449 0 : 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 0 : pContext = new SvXMLNumFmtElementContext( GetImport(), nPrfx, rLName,
1471 0 : *this, nToken, xAttrList );
1472 0 : break;
1473 :
1474 : case XML_TOK_STYLE_PROPERTIES:
1475 0 : pContext = new SvXMLNumFmtPropContext( GetImport(), nPrfx, rLName,
1476 0 : *this, xAttrList );
1477 0 : 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 0 : pContext = new SvXMLNumFmtMapContext( GetImport(), nPrfx, rLName,
1483 0 : *this, xAttrList );
1484 : }
1485 0 : break;
1486 : }
1487 :
1488 0 : if( !pContext )
1489 0 : pContext = new SvXMLImportContext( GetImport(), nPrfx, rLName );
1490 0 : return pContext;
1491 : }
1492 :
1493 0 : sal_Int32 SvXMLNumFormatContext::GetKey()
1494 : {
1495 0 : if (nKey > -1)
1496 : {
1497 0 : 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 0 : return nKey;
1509 : }
1510 : else
1511 : {
1512 : // reset bRemoveAfterUse before CreateAndInsert, so AddKey is called without bRemoveAfterUse set
1513 0 : bRemoveAfterUse = false;
1514 0 : CreateAndInsert(true);
1515 0 : return nKey;
1516 : }
1517 : }
1518 :
1519 0 : sal_Int32 SvXMLNumFormatContext::PrivateGetKey()
1520 : {
1521 : // used for map elements in CreateAndInsert - don't reset bRemoveAfterUse flag
1522 :
1523 0 : if (nKey > -1)
1524 0 : return nKey;
1525 : else
1526 : {
1527 0 : CreateAndInsert(true);
1528 0 : 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 0 : void SvXMLNumFormatContext::CreateAndInsert(bool /*bOverwrite*/)
1552 : {
1553 0 : if (!(nKey > -1))
1554 0 : CreateAndInsert(pData->GetNumberFormatter());
1555 0 : }
1556 :
1557 0 : sal_Int32 SvXMLNumFormatContext::CreateAndInsert(SvNumberFormatter* pFormatter)
1558 : {
1559 0 : if (!pFormatter)
1560 : {
1561 : OSL_FAIL("no number formatter");
1562 0 : return -1;
1563 : }
1564 :
1565 0 : sal_uInt32 nIndex = NUMBERFORMAT_ENTRY_NOT_FOUND;
1566 :
1567 0 : for (sal_uInt32 i = 0; i < aMyConditions.size(); i++)
1568 : {
1569 : SvXMLNumFormatContext* pStyle = (SvXMLNumFormatContext *)pStyles->FindStyleChildContext(
1570 0 : XML_STYLE_FAMILY_DATA_STYLE, aMyConditions[i].sMapName, false);
1571 0 : if (pStyle)
1572 : {
1573 0 : if ((pStyle->PrivateGetKey() > -1)) // don't reset pStyle's bRemoveAfterUse flag
1574 0 : AddCondition(i);
1575 : }
1576 : }
1577 :
1578 0 : 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 0 : aFormatCode.appendAscii("\"\""); // ""
1583 : }
1584 :
1585 0 : aFormatCode.insert( 0, aConditions.makeStringAndClear() );
1586 0 : OUString sFormat = aFormatCode.makeStringAndClear();
1587 :
1588 : // test special cases
1589 :
1590 0 : 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 0 : if ( nType == XML_TOK_STYLES_NUMBER_STYLE && !bHasExtraText &&
1596 0 : aMyConditions.empty() && sFormat.toChar() != '[' )
1597 0 : nIndex = pFormatter->GetStandardIndex( nFormatLang );
1598 : }
1599 0 : 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 0 : if ( nType == XML_TOK_STYLES_BOOLEAN_STYLE )
1611 0 : nIndex = pFormatter->GetFormatIndex( NF_BOOLEAN, nFormatLang );
1612 :
1613 : // check for default date formats
1614 0 : if ( nType == XML_TOK_STYLES_DATE_STYLE && bAutoOrder && !bDateNoDefault )
1615 : {
1616 : NfIndexTableOffset eFormat = (NfIndexTableOffset) SvXMLNumFmtDefaults::GetDefaultDateFormat(
1617 : eDateDOW, eDateDay, eDateMonth, eDateYear,
1618 0 : eDateHours, eDateMins, eDateSecs, bFromSystem );
1619 0 : 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 0 : nIndex = pFormatter->GetFormatIndex( eFormat, nFormatLang );
1627 : }
1628 : }
1629 :
1630 0 : if ( nIndex == NUMBERFORMAT_ENTRY_NOT_FOUND && !sFormat.isEmpty() )
1631 : {
1632 : // insert by format string
1633 :
1634 0 : OUString aFormatStr( sFormat );
1635 0 : nIndex = pFormatter->GetEntryKey( aFormatStr, nFormatLang );
1636 0 : if ( nIndex == NUMBERFORMAT_ENTRY_NOT_FOUND )
1637 : {
1638 0 : sal_Int32 nErrPos = 0;
1639 0 : short l_nType = 0;
1640 0 : bool bOk = pFormatter->PutEntry( aFormatStr, nErrPos, l_nType, nIndex, nFormatLang );
1641 0 : 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 0 : nIndex = pFormatter->GetEntryKey( aFormatStr, nFormatLang );
1646 0 : if ( nIndex != NUMBERFORMAT_ENTRY_NOT_FOUND )
1647 0 : bOk = true;
1648 : }
1649 0 : if (!bOk)
1650 0 : nIndex = NUMBERFORMAT_ENTRY_NOT_FOUND;
1651 0 : }
1652 : }
1653 :
1654 : //! I18N doesn't provide SYSTEM or extended date information yet
1655 0 : 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 0 : NfIndexTableOffset eOffset = pFormatter->GetIndexTableOffset( nIndex );
1661 0 : 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 0 : 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 0 : 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 0 : if ( nIndex == NUMBERFORMAT_ENTRY_NOT_FOUND )
1689 : {
1690 : OSL_FAIL("invalid number format");
1691 0 : nIndex = pFormatter->GetStandardIndex( nFormatLang );
1692 : }
1693 :
1694 0 : pData->AddKey( nIndex, GetName(), bRemoveAfterUse );
1695 0 : 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 0 : if (!bRemoveAfterUse)
1703 0 : GetImport().AddNumberStyle( nKey, GetName() );
1704 :
1705 0 : return nKey;
1706 : }
1707 :
1708 0 : void SvXMLNumFormatContext::Finish( bool bOverwrite )
1709 : {
1710 0 : SvXMLStyleContext::Finish( bOverwrite );
1711 0 : }
1712 :
1713 0 : const LocaleDataWrapper& SvXMLNumFormatContext::GetLocaleData() const
1714 : {
1715 0 : return pData->GetLocaleData( nFormatLang );
1716 : }
1717 :
1718 0 : void SvXMLNumFormatContext::AddToCode( sal_Unicode c )
1719 : {
1720 0 : aFormatCode.append( c );
1721 0 : bHasExtraText = true;
1722 0 : }
1723 :
1724 0 : void SvXMLNumFormatContext::AddToCode( const OUString& rString )
1725 : {
1726 0 : aFormatCode.append( rString );
1727 0 : bHasExtraText = true;
1728 0 : }
1729 :
1730 0 : void SvXMLNumFormatContext::AddNumber( const SvXMLNumberInfo& rInfo )
1731 : {
1732 0 : SvNumberFormatter* pFormatter = pData->GetNumberFormatter();
1733 0 : if (!pFormatter)
1734 0 : return;
1735 :
1736 : // store special conditions
1737 0 : bAutoDec = ( rInfo.nDecimals < 0 );
1738 0 : bAutoInt = ( rInfo.nInteger < 0 );
1739 :
1740 0 : sal_uInt16 nPrec = 0;
1741 0 : sal_uInt16 nLeading = 0;
1742 0 : if ( rInfo.nDecimals >= 0 ) // < 0 : Default
1743 0 : nPrec = (sal_uInt16) rInfo.nDecimals;
1744 0 : if ( rInfo.nInteger >= 0 ) // < 0 : Default
1745 0 : nLeading = (sal_uInt16) rInfo.nInteger;
1746 :
1747 0 : if ( bAutoDec )
1748 : {
1749 0 : 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 0 : aFormatCode.append( pFormatter->GetStandardName( nFormatLang ) );
1763 0 : return;
1764 : }
1765 : }
1766 0 : if ( bAutoInt )
1767 : {
1768 : //!...
1769 : }
1770 :
1771 0 : sal_uInt16 nGenPrec = nPrec;
1772 0 : if ( rInfo.bDecReplace || rInfo.bVarDecimals )
1773 0 : nGenPrec = 0; // generate format without decimals...
1774 :
1775 0 : bool bGrouping = rInfo.bGrouping;
1776 0 : sal_uInt16 nEmbeddedCount = rInfo.aEmbeddedElements.size();
1777 0 : if ( nEmbeddedCount )
1778 0 : bGrouping = false; // grouping and embedded characters can't be used together
1779 :
1780 0 : sal_uInt32 nStdIndex = pFormatter->GetStandardIndex( nFormatLang );
1781 : OUStringBuffer aNumStr = pFormatter->GenerateFormat( nStdIndex, nFormatLang,
1782 0 : bGrouping, false, nGenPrec, nLeading );
1783 :
1784 0 : 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 0 : 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 0 : aFormatCode.append( aNumStr.makeStringAndClear() );
1840 :
1841 0 : 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 0 : 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 0 : }
1866 : }
1867 :
1868 0 : void SvXMLNumFormatContext::AddCurrency( const OUString& rContent, LanguageType nLang )
1869 : {
1870 0 : bool bAutomatic = false;
1871 0 : OUString aSymbol = rContent;
1872 0 : if ( aSymbol.isEmpty())
1873 : {
1874 0 : SvNumberFormatter* pFormatter = pData->GetNumberFormatter();
1875 0 : if ( pFormatter )
1876 : {
1877 0 : pFormatter->ChangeIntl( nFormatLang );
1878 0 : OUString sCurString, sDummy;
1879 0 : pFormatter->GetCompatibilityCurrency( sCurString, sDummy );
1880 0 : aSymbol = sCurString;
1881 :
1882 0 : bAutomatic = true;
1883 : }
1884 : }
1885 0 : else if ( nLang == LANGUAGE_SYSTEM && aSymbol.equalsAscii("CCC") )
1886 : {
1887 : // "CCC" is used for automatic long symbol
1888 0 : bAutomatic = true;
1889 : }
1890 :
1891 0 : if ( bAutomatic )
1892 : {
1893 : // remove unnecessary quotes before automatic symbol (formats like "-(0DM)")
1894 : // otherwise the currency symbol isn't recognized (#94048#)
1895 :
1896 0 : sal_Int32 nLength = aFormatCode.getLength();
1897 0 : 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 0 : if (!bAutomatic)
1919 0 : aFormatCode.appendAscii( "[$" ); // intro for "new" currency symbols
1920 :
1921 0 : aFormatCode.append( aSymbol );
1922 :
1923 0 : if (!bAutomatic)
1924 : {
1925 0 : if ( nLang != LANGUAGE_SYSTEM )
1926 : {
1927 : // '-' sign and language code in hex:
1928 0 : aFormatCode.append("-" + OUString::number(sal_Int32(nLang), 16).toAsciiUpperCase());
1929 : }
1930 :
1931 0 : aFormatCode.append( ']' ); // end of "new" currency symbol
1932 0 : }
1933 0 : }
1934 :
1935 0 : void SvXMLNumFormatContext::AddNfKeyword( sal_uInt16 nIndex )
1936 : {
1937 0 : SvNumberFormatter* pFormatter = pData->GetNumberFormatter();
1938 0 : if (!pFormatter)
1939 0 : return;
1940 :
1941 0 : if ( nIndex == NF_KEY_G || nIndex == NF_KEY_GG || nIndex == NF_KEY_GGG )
1942 0 : bHasEra = true;
1943 :
1944 0 : 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 0 : OUString sKeyword = pFormatter->GetKeyword( nFormatLang, nIndex );
1951 :
1952 0 : if ( nIndex == NF_KEY_H || nIndex == NF_KEY_HH ||
1953 0 : nIndex == NF_KEY_MI || nIndex == NF_KEY_MMI ||
1954 0 : nIndex == NF_KEY_S || nIndex == NF_KEY_SS )
1955 : {
1956 0 : if ( !bTruncate && !bHasDateTime )
1957 : {
1958 : // with truncate-on-overflow = false, add "[]" to first time part
1959 0 : aFormatCode.append("[" + sKeyword + "]");
1960 : }
1961 : else
1962 : {
1963 0 : aFormatCode.append( sKeyword );
1964 : }
1965 0 : bHasDateTime = true;
1966 : }
1967 : else
1968 : {
1969 0 : aFormatCode.append( sKeyword );
1970 : }
1971 : // collect the date elements that the format contains, to recognize default date formats
1972 0 : 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 0 : case NF_KEY_D: eDateDay = XML_DEA_SHORT; break;
1978 0 : case NF_KEY_DD: eDateDay = XML_DEA_LONG; break;
1979 0 : case NF_KEY_M: eDateMonth = XML_DEA_SHORT; break;
1980 0 : case NF_KEY_MM: eDateMonth = XML_DEA_LONG; break;
1981 0 : case NF_KEY_MMM: eDateMonth = XML_DEA_TEXTSHORT; break;
1982 0 : case NF_KEY_MMMM: eDateMonth = XML_DEA_TEXTLONG; break;
1983 0 : case NF_KEY_YY: eDateYear = XML_DEA_SHORT; break;
1984 0 : case NF_KEY_YYYY: eDateYear = XML_DEA_LONG; break;
1985 0 : case NF_KEY_H: eDateHours = XML_DEA_SHORT; break;
1986 0 : case NF_KEY_HH: eDateHours = XML_DEA_LONG; break;
1987 0 : case NF_KEY_MI: eDateMins = XML_DEA_SHORT; break;
1988 0 : case NF_KEY_MMI: eDateMins = XML_DEA_LONG; break;
1989 0 : case NF_KEY_S: eDateSecs = XML_DEA_SHORT; break;
1990 0 : case NF_KEY_SS: eDateSecs = XML_DEA_LONG; break;
1991 : case NF_KEY_AP:
1992 0 : 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 0 : }
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 0 : void SvXMLNumFormatContext::AddCondition( const sal_Int32 nIndex )
2038 : {
2039 0 : OUString rApplyName = aMyConditions[nIndex].sMapName;
2040 0 : OUString rCondition = aMyConditions[nIndex].sCondition;
2041 0 : SvNumberFormatter* pFormatter = pData->GetNumberFormatter();
2042 0 : sal_uInt32 l_nKey = pData->GetKeyForName( rApplyName );
2043 0 : OUString sValue("value()"); //! define constant
2044 0 : sal_Int32 nValLen = sValue.getLength();
2045 :
2046 0 : if ( pFormatter && l_nKey != NUMBERFORMAT_ENTRY_NOT_FOUND &&
2047 0 : rCondition.copy( 0, nValLen ) == sValue )
2048 : {
2049 : //! test for valid conditions
2050 : //! test for default conditions
2051 :
2052 0 : OUString sRealCond = rCondition.copy( nValLen, rCondition.getLength() - nValLen );
2053 0 : 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 0 : if ( aConditions.isEmpty() && aMyConditions.size() == 1 && sRealCond.equalsAscii( ">=0" ) )
2058 0 : bDefaultCond = true;
2059 :
2060 0 : 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 0 : bDefaultCond = true;
2065 : }
2066 :
2067 0 : if (!bDefaultCond)
2068 : {
2069 : // Convert != to <>
2070 0 : sal_Int32 nPos = sRealCond.indexOf( "!=" );
2071 0 : if ( nPos >= 0 )
2072 : {
2073 0 : sRealCond = sRealCond.replaceAt( nPos, 2, "<>" );
2074 : }
2075 :
2076 0 : nPos = sRealCond.indexOf( '.' );
2077 0 : 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 0 : aConditions.append("[" + sRealCond + "]");
2087 : }
2088 :
2089 0 : const SvNumberformat* pFormat = pFormatter->GetEntry(l_nKey);
2090 0 : if ( pFormat )
2091 0 : aConditions.append( OUString( pFormat->GetFormatstring() ) );
2092 :
2093 0 : aConditions.append( ';' );
2094 0 : }
2095 0 : }
2096 :
2097 0 : void SvXMLNumFormatContext::AddCondition( const OUString& rCondition, const OUString& rApplyName )
2098 : {
2099 0 : MyCondition aCondition;
2100 0 : aCondition.sCondition = rCondition;
2101 0 : aCondition.sMapName = rApplyName;
2102 0 : aMyConditions.push_back(aCondition);
2103 0 : }
2104 :
2105 0 : void SvXMLNumFormatContext::AddColor( sal_uInt32 const nColor )
2106 : {
2107 0 : SvNumberFormatter* pFormatter = pData->GetNumberFormatter();
2108 0 : if (!pFormatter)
2109 0 : return;
2110 :
2111 0 : OUStringBuffer aColName;
2112 0 : for ( sal_uInt16 i=0; i<XML_NUMF_COLORCOUNT; i++ )
2113 0 : if (nColor == aNumFmtStdColors[i])
2114 : {
2115 0 : aColName = OUString( pFormatter->GetKeyword( nFormatLang, sal::static_int_cast< sal_uInt16 >(NF_KEY_FIRSTCOLOR + i) ) );
2116 0 : break;
2117 : }
2118 :
2119 0 : if ( !aColName.isEmpty() )
2120 : {
2121 0 : aColName.insert( 0, '[' );
2122 0 : aColName.append( ']' );
2123 0 : aFormatCode.insert( 0, aColName.makeStringAndClear() );
2124 0 : }
2125 : }
2126 :
2127 0 : void SvXMLNumFormatContext::UpdateCalendar( const OUString& rNewCalendar )
2128 : {
2129 0 : 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 0 : }
2140 :
2141 0 : bool SvXMLNumFormatContext::IsSystemLanguage()
2142 : {
2143 0 : return nFormatLang == LANGUAGE_SYSTEM;
2144 : }
2145 :
2146 :
2147 : // SvXMLNumFmtHelper
2148 :
2149 :
2150 0 : 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 0 : SvNumberFormatter* pFormatter = NULL;
2157 : SvNumberFormatsSupplierObj* pObj =
2158 0 : SvNumberFormatsSupplierObj::getImplementation( rSupp );
2159 0 : if (pObj)
2160 0 : pFormatter = pObj->GetNumberFormatter();
2161 :
2162 0 : pData = new SvXMLNumImpData( pFormatter, rxContext );
2163 0 : }
2164 :
2165 0 : SvXMLNumFmtHelper::SvXMLNumFmtHelper(
2166 : SvNumberFormatter* pNumberFormatter,
2167 : const uno::Reference<uno::XComponentContext>& rxContext )
2168 : {
2169 : DBG_ASSERT( rxContext.is(), "got no service manager" );
2170 :
2171 0 : pData = new SvXMLNumImpData( pNumberFormatter, rxContext );
2172 0 : }
2173 :
2174 0 : SvXMLNumFmtHelper::~SvXMLNumFmtHelper()
2175 : {
2176 : // remove temporary (volatile) formats from NumberFormatter
2177 0 : pData->RemoveVolatileFormats();
2178 :
2179 0 : delete pData;
2180 0 : }
2181 :
2182 0 : 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 0 : SvXMLStyleContext* pContext = NULL;
2188 :
2189 0 : const SvXMLTokenMap& rTokenMap = pData->GetStylesElemTokenMap();
2190 0 : sal_uInt16 nToken = rTokenMap.Get( nPrefix, rLocalName );
2191 0 : 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 0 : pData, nToken, xAttrList, rStyles );
2202 0 : break;
2203 : }
2204 :
2205 : // return NULL if not a data style, caller must handle other elements
2206 0 : return pContext;
2207 : }
2208 :
2209 0 : const SvXMLTokenMap& SvXMLNumFmtHelper::GetStylesElemTokenMap()
2210 : {
2211 0 : return pData->GetStylesElemTokenMap();
2212 : }
2213 :
2214 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|