1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "XMLNumberStylesExport.hxx"
21 #include <XMLNumberStylesImport.hxx>
23 #include <xmloff/xmlnamespace.hxx>
24 #include <xmloff/xmlimp.hxx>
25 #include <xmloff/namespacemap.hxx>
26 #include <xmloff/xmltoken.hxx>
28 #include <o3tl/string_view.hxx>
29 #include <sal/log.hxx>
31 #include "sdxmlexp_impl.hxx"
32 #include "sdxmlimp_impl.hxx"
34 using namespace ::xmloff::token
;
38 struct SdXMLDataStyleNumber
40 enum XMLTokenEnum meNumberStyle
;
49 SdXMLDataStyleNumber
const aSdXMLDataStyleNumbers
[] =
51 { XML_DAY
, false, false, false, nullptr },
52 { XML_DAY
, true, false, false, nullptr },
53 { XML_MONTH
, true, false, false, nullptr },
54 { XML_MONTH
, false, true, false, nullptr },
55 { XML_MONTH
, true, true, false, nullptr },
56 { XML_YEAR
, false, false, false, nullptr },
57 { XML_YEAR
, true, false, false, nullptr },
58 { XML_DAY_OF_WEEK
, false, false, false, nullptr },
59 { XML_DAY_OF_WEEK
, true, false, false, nullptr },
60 { XML_TEXT
, false, false, false, "." },
61 { XML_TEXT
, false, false, false, " " },
62 { XML_TEXT
, false, false, false, ", " },
63 { XML_TEXT
, false, false, false, ". " },
64 { XML_HOURS
, false, false, false, nullptr },
65 { XML_MINUTES
, false, false, false, nullptr },
66 { XML_TEXT
, false, false, false, ":" },
67 { XML_AM_PM
, false, false, false, nullptr },
68 { XML_SECONDS
, false, false, false, nullptr },
69 { XML_SECONDS
, false, false, true, nullptr },
70 { XML_TOKEN_INVALID
, false, false, false, nullptr }
74 enum class DataStyleNumber
: sal_uInt8
77 Day
= 1, // <number:day/>
78 DayLong
= 2, // <number:day number:style="long"/>
79 MonthLong
= 3, // <number:month number:style="long"/>
80 MonthText
= 4, // <number:month number:textual="true"/>
81 MonthLongText
= 5, // <number:month number:style="long" number:textual="true"/>
82 Year
= 6, // <number:year/>
83 YearLong
= 7, // <number:year number:style="long"/>
84 DayOfWeek
= 8, // <number:day-of-week/>
85 DayOfWeekLong
= 9, // <number:day-of-week number:style="long"/>
86 TextPoint
= 10, // <number:text>.</number:text>
87 TextSpace
= 11, // <number:text> </number:text>
88 TextCommaSpace
= 12, // <number:text>, </number:text>
89 TextPointSpace
= 13, // <number:text>. </number:text>
90 Hours
= 14, // <number:hours/>
91 Minutes
= 15, // <number:minutes/>
92 TextColon
= 16, // <number:text>:</number:text>
93 AmPm
= 17, // <number:am-pm/>
94 Seconds
= 18, // <number:seconds/>
95 Seconds_02
= 19, // <number:seconds number:/>
98 struct SdXMLFixedDataStyle
103 DataStyleNumber mpFormat
[8];
106 const SdXMLFixedDataStyle aSdXML_Standard_Short
=
110 DataStyleNumber::DayLong
,
111 DataStyleNumber::TextPoint
,
112 DataStyleNumber::MonthLong
,
113 DataStyleNumber::TextPoint
,
114 DataStyleNumber::YearLong
,
115 DataStyleNumber::NONE
, DataStyleNumber::NONE
, DataStyleNumber::NONE
119 const SdXMLFixedDataStyle aSdXML_Standard_Long
=
123 DataStyleNumber::DayOfWeekLong
,
124 DataStyleNumber::TextCommaSpace
,
125 DataStyleNumber::Day
,
126 DataStyleNumber::TextPointSpace
,
127 DataStyleNumber::MonthLongText
,
128 DataStyleNumber::TextSpace
,
129 DataStyleNumber::YearLong
,
130 DataStyleNumber::NONE
134 const SdXMLFixedDataStyle aSdXML_DateStyle_1
=
138 DataStyleNumber::DayLong
,
139 DataStyleNumber::TextPoint
,
140 DataStyleNumber::MonthLong
,
141 DataStyleNumber::TextPoint
,
142 DataStyleNumber::Year
,
143 DataStyleNumber::NONE
, DataStyleNumber::NONE
, DataStyleNumber::NONE
147 const SdXMLFixedDataStyle aSdXML_DateStyle_2
=
151 DataStyleNumber::DayLong
,
152 DataStyleNumber::TextPoint
,
153 DataStyleNumber::MonthLong
,
154 DataStyleNumber::TextPoint
,
155 DataStyleNumber::YearLong
,
156 DataStyleNumber::NONE
, DataStyleNumber::NONE
, DataStyleNumber::NONE
160 const SdXMLFixedDataStyle aSdXML_DateStyle_3
=
164 DataStyleNumber::Day
,
165 DataStyleNumber::TextPointSpace
,
166 DataStyleNumber::MonthText
,
167 DataStyleNumber::TextSpace
,
168 DataStyleNumber::YearLong
,
169 DataStyleNumber::NONE
, DataStyleNumber::NONE
, DataStyleNumber::NONE
173 const SdXMLFixedDataStyle aSdXML_DateStyle_4
=
177 DataStyleNumber::Day
,
178 DataStyleNumber::TextPointSpace
,
179 DataStyleNumber::MonthLongText
,
180 DataStyleNumber::TextSpace
,
181 DataStyleNumber::YearLong
,
182 DataStyleNumber::NONE
, DataStyleNumber::NONE
, DataStyleNumber::NONE
186 const SdXMLFixedDataStyle aSdXML_DateStyle_5
=
190 DataStyleNumber::DayOfWeek
,
191 DataStyleNumber::TextCommaSpace
,
192 DataStyleNumber::Day
,
193 DataStyleNumber::TextPointSpace
,
194 DataStyleNumber::MonthLongText
,
195 DataStyleNumber::TextSpace
,
196 DataStyleNumber::YearLong
,
197 DataStyleNumber::NONE
201 const SdXMLFixedDataStyle aSdXML_DateStyle_6
=
205 DataStyleNumber::DayOfWeekLong
,
206 DataStyleNumber::TextCommaSpace
,
207 DataStyleNumber::Day
,
208 DataStyleNumber::TextPointSpace
,
209 DataStyleNumber::MonthLongText
,
210 DataStyleNumber::TextSpace
,
211 DataStyleNumber::YearLong
,
212 DataStyleNumber::NONE
216 const SdXMLFixedDataStyle aSdXML_TimeStyle_1
=
219 DataStyleNumber::Hours
,
220 DataStyleNumber::TextColon
,
221 DataStyleNumber::Minutes
,
222 DataStyleNumber::TextColon
,
223 DataStyleNumber::Seconds
,
224 DataStyleNumber::AmPm
,
225 DataStyleNumber::NONE
, DataStyleNumber::NONE
229 const SdXMLFixedDataStyle aSdXML_TimeStyle_2
=
230 { "T2", false, false,
232 DataStyleNumber::Hours
,
233 DataStyleNumber::TextColon
,
234 DataStyleNumber::Minutes
,
235 DataStyleNumber::NONE
, DataStyleNumber::NONE
, DataStyleNumber::NONE
, DataStyleNumber::NONE
, DataStyleNumber::NONE
239 const SdXMLFixedDataStyle aSdXML_TimeStyle_3
=
240 { "T3", false, false,
242 DataStyleNumber::Hours
,
243 DataStyleNumber::TextColon
,
244 DataStyleNumber::Minutes
,
245 DataStyleNumber::TextColon
,
246 DataStyleNumber::Seconds
,
247 DataStyleNumber::NONE
, DataStyleNumber::NONE
, DataStyleNumber::NONE
251 const SdXMLFixedDataStyle aSdXML_TimeStyle_4
=
252 { "T4", false, false,
254 DataStyleNumber::Hours
,
255 DataStyleNumber::TextColon
,
256 DataStyleNumber::Minutes
,
257 DataStyleNumber::TextColon
,
258 DataStyleNumber::Seconds_02
,
259 DataStyleNumber::NONE
, DataStyleNumber::NONE
, DataStyleNumber::NONE
263 const SdXMLFixedDataStyle aSdXML_TimeStyle_5
=
264 { "T5", false, false,
266 DataStyleNumber::Hours
,
267 DataStyleNumber::TextColon
,
268 DataStyleNumber::Minutes
,
269 DataStyleNumber::AmPm
,
270 DataStyleNumber::NONE
, DataStyleNumber::NONE
, DataStyleNumber::NONE
, DataStyleNumber::NONE
274 const SdXMLFixedDataStyle aSdXML_TimeStyle_6
=
275 { "T6", false, false,
277 DataStyleNumber::Hours
,
278 DataStyleNumber::TextColon
,
279 DataStyleNumber::Minutes
,
280 DataStyleNumber::TextColon
,
281 DataStyleNumber::Seconds
,
282 DataStyleNumber::AmPm
,
283 DataStyleNumber::NONE
, DataStyleNumber::NONE
287 const SdXMLFixedDataStyle aSdXML_TimeStyle_7
=
288 { "T7", false, false,
290 DataStyleNumber::Hours
,
291 DataStyleNumber::TextColon
,
292 DataStyleNumber::Minutes
,
293 DataStyleNumber::TextColon
,
294 DataStyleNumber::Seconds_02
,
295 DataStyleNumber::AmPm
,
296 DataStyleNumber::NONE
, DataStyleNumber::NONE
300 const SdXMLFixedDataStyle
* const aSdXMLFixedDateFormats
[SdXMLDateFormatCount
] =
302 &aSdXML_Standard_Short
,
303 &aSdXML_Standard_Long
,
312 const SdXMLFixedDataStyle
* const aSdXMLFixedTimeFormats
[SdXMLTimeFormatCount
] =
325 static void SdXMLExportDataStyleNumber( SdXMLExport
& rExport
, SdXMLDataStyleNumber
const & rElement
)
327 if( rElement
.mbDecimal02
)
329 rExport
.AddAttribute( XML_NAMESPACE_NUMBER
, XML_DECIMAL_PLACES
, XML_2
);
332 if( rElement
.mbLong
)
334 rExport
.AddAttribute( XML_NAMESPACE_NUMBER
, XML_STYLE
, XML_LONG
);
337 if( rElement
.mbTextual
)
339 rExport
.AddAttribute( XML_NAMESPACE_NUMBER
, XML_TEXTUAL
, XML_TRUE
);
342 SvXMLElementExport
aNumberStyle( rExport
, XML_NAMESPACE_NUMBER
, rElement
.meNumberStyle
, true, false );
343 if( rElement
.mpText
)
345 OUString
sAttrValue( OUString::createFromAscii( rElement
.mpText
) );
346 rExport
.GetDocHandler()->characters( sAttrValue
);
350 static void SdXMLExportStyle( SdXMLExport
& rExport
, const SdXMLFixedDataStyle
* pStyle
, const SdXMLFixedDataStyle
* pStyle2
= nullptr )
353 OUString sAttrValue
= OUString::createFromAscii( pStyle
->mpName
);
355 sAttrValue
+= OUString::createFromAscii( pStyle2
->mpName
);
357 rExport
.AddAttribute( XML_NAMESPACE_STYLE
, XML_NAME
, sAttrValue
);
359 if( pStyle
->mbAutomatic
)
361 rExport
.AddAttribute( XML_NAMESPACE_NUMBER
, XML_AUTOMATIC_ORDER
, XML_TRUE
);
364 SvXMLElementExport
aElement( rExport
, XML_NAMESPACE_NUMBER
, pStyle
->mbDateStyle
? XML_DATE_STYLE
: XML_TIME_STYLE
, true, true );
369 const DataStyleNumber
* pElements
= &pStyle
->mpFormat
[0];
371 while( *pElements
!= DataStyleNumber::NONE
)
373 SdXMLDataStyleNumber
const & rElement
= aSdXMLDataStyleNumbers
[ static_cast<int>(*pElements
++) - 1 ];
374 SdXMLExportDataStyleNumber( rExport
, rElement
);
379 SdXMLDataStyleNumber
const & rElement
= aSdXMLDataStyleNumbers
[ static_cast<int>(DataStyleNumber::TextSpace
) - 1 ];
380 SdXMLExportDataStyleNumber( rExport
, rElement
);
389 void SdXMLNumberStylesExporter::exportTimeStyle( SdXMLExport
& rExport
, sal_Int32 nStyle
)
391 SAL_WARN_IF( (nStyle
< 0) || (nStyle
>= SdXMLTimeFormatCount
), "xmloff", "Unknown time style!" );
392 if( (nStyle
>= 0) && (nStyle
< SdXMLTimeFormatCount
) )
393 SdXMLExportStyle( rExport
, aSdXMLFixedTimeFormats
[ nStyle
] );
396 void SdXMLNumberStylesExporter::exportDateStyle( SdXMLExport
& rExport
, sal_Int32 nStyle
)
400 int nDateStyle
= nStyle
& 0x0f;
401 bool bHasDate
= nDateStyle
!= 0;
406 SAL_WARN_IF(nDateStyle
>= SdXMLDateFormatCount
, "xmloff", "unknown date style!");
408 int nTimeStyle
= (nStyle
>> 4) & 0x0f;
409 bool bHasTime
= nTimeStyle
!= 0;
414 SAL_WARN_IF(nTimeStyle
>= SdXMLTimeFormatCount
, "xmloff", "Unknown time style!");
416 if ((nDateStyle
< SdXMLDateFormatCount
) && (nTimeStyle
< SdXMLTimeFormatCount
))
422 SdXMLExportStyle( rExport
, aSdXMLFixedDateFormats
[ nDateStyle
], aSdXMLFixedTimeFormats
[ nTimeStyle
] );
426 SdXMLExportStyle( rExport
, aSdXMLFixedDateFormats
[ nDateStyle
] );
431 SdXMLExportStyle( rExport
, aSdXMLFixedTimeFormats
[ nTimeStyle
] );
437 SAL_WARN_IF( (nStyle
< 0) || (nStyle
>= SdXMLDateFormatCount
), "xmloff", "unknown date style!" );
438 if( (nStyle
>= 0) && (nStyle
< SdXMLDateFormatCount
) )
439 SdXMLExportStyle( rExport
, aSdXMLFixedDateFormats
[ nStyle
] );
443 OUString
SdXMLNumberStylesExporter::getTimeStyleName(const sal_Int32 nTimeFormat
)
445 sal_Int32 nFormat
= nTimeFormat
;
449 if( (nFormat
>= 0) && (nFormat
< SdXMLTimeFormatCount
) )
451 return OUString::createFromAscii(aSdXMLFixedTimeFormats
[nFormat
]->mpName
);
459 OUString
SdXMLNumberStylesExporter::getDateStyleName(const sal_Int32 nDateFormat
)
461 sal_Int32 nFormat
= nDateFormat
;
467 aStr
= getDateStyleName( nFormat
& 0x0f );
468 aStr
+= getTimeStyleName( (nFormat
>> 4) & 0x0f );
475 if( (nFormat
>= 0) && (nFormat
< SdXMLDateFormatCount
) )
477 return OUString::createFromAscii(aSdXMLFixedDateFormats
[nFormat
]->mpName
);
487 class SdXMLNumberFormatMemberImportContext
: public SvXMLImportContext
490 SdXMLNumberFormatImportContext
* mpParent
;
492 OUString maNumberStyle
;
497 SvXMLImportContextRef mxSlaveContext
;
501 SdXMLNumberFormatMemberImportContext( SvXMLImport
& rImport
,
503 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& xAttrList
,
504 SdXMLNumberFormatImportContext
* pParent
,
505 SvXMLImportContextRef xSlaveContext
);
507 virtual css::uno::Reference
< css::xml::sax::XFastContextHandler
> SAL_CALL
createFastChildContext(
508 sal_Int32 nElement
, const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& AttrList
) override
;
510 virtual void SAL_CALL
startFastElement( sal_Int32 nElement
,
511 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& ) override
;
513 virtual void SAL_CALL
endFastElement(sal_Int32 nElement
) override
;
515 virtual void SAL_CALL
characters( const OUString
& rChars
) override
;
519 SdXMLNumberFormatMemberImportContext::SdXMLNumberFormatMemberImportContext(
520 SvXMLImport
& rImport
,
522 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& xAttrList
,
523 SdXMLNumberFormatImportContext
* pParent
,
524 SvXMLImportContextRef xSlaveContext
)
525 : SvXMLImportContext(rImport
),
527 maNumberStyle( SvXMLImport::getNameFromToken(nElement
) ),
528 mxSlaveContext(std::move( xSlaveContext
))
534 for (auto &aIter
: sax_fastparser::castToFastAttributeList( xAttrList
))
536 switch (aIter
.getToken())
538 case XML_ELEMENT(NUMBER
, XML_DECIMAL_PLACES
):
539 mbDecimal02
= IsXMLToken( aIter
, XML_2
);
541 case XML_ELEMENT(NUMBER
, XML_STYLE
):
542 mbLong
= IsXMLToken( aIter
, XML_LONG
);
544 case XML_ELEMENT(NUMBER
, XML_TEXTUAL
):
545 mbTextual
= IsXMLToken( aIter
, XML_TRUE
);
548 XMLOFF_WARN_UNKNOWN("xmloff", aIter
);
554 css::uno::Reference
< css::xml::sax::XFastContextHandler
> SdXMLNumberFormatMemberImportContext::createFastChildContext(
556 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& xAttrList
)
558 return mxSlaveContext
->createFastChildContext( nElement
, xAttrList
);
561 void SdXMLNumberFormatMemberImportContext::startFastElement(
563 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& xAttrList
)
565 mxSlaveContext
->startFastElement( nElement
, xAttrList
);
568 void SdXMLNumberFormatMemberImportContext::endFastElement(sal_Int32 nElement
)
570 mxSlaveContext
->endFastElement(nElement
);
573 mpParent
->add( maNumberStyle
, mbLong
, mbTextual
, mbDecimal02
, maText
);
576 void SdXMLNumberFormatMemberImportContext::characters( const OUString
& rChars
)
578 mxSlaveContext
->characters( rChars
);
583 SdXMLNumberFormatImportContext::SdXMLNumberFormatImportContext( SdXMLImport
& rImport
, sal_Int32 nElement
, SvXMLNumImpData
* pNewData
, SvXMLStylesTokens nNewType
, const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& xAttrList
, SvXMLStylesContext
& rStyles
)
584 : SvXMLNumFormatContext(rImport
, nElement
, pNewData
, nNewType
, xAttrList
, rStyles
),
585 mbAutomatic( false ),
590 mbTimeStyle
= (nElement
& TOKEN_MASK
) == XML_TIME_STYLE
;
592 for (auto &aIter
: sax_fastparser::castToFastAttributeList( xAttrList
))
594 if( aIter
.getToken() == XML_ELEMENT(NUMBER
, XML_AUTOMATIC_ORDER
) )
595 mbAutomatic
= IsXMLToken( aIter
, XML_TRUE
);
597 XMLOFF_WARN_UNKNOWN("xmloff", aIter
);
601 SdXMLNumberFormatImportContext::~SdXMLNumberFormatImportContext()
605 void SdXMLNumberFormatImportContext::add( std::u16string_view rNumberStyle
, bool bLong
, bool bTextual
, bool bDecimal02
, std::u16string_view rText
)
610 const SdXMLDataStyleNumber
* pStyleMember
= aSdXMLDataStyleNumbers
;
611 for( sal_uInt8 nIndex
= 0; pStyleMember
->meNumberStyle
!= XML_TOKEN_INVALID
; nIndex
++, pStyleMember
++ )
613 if( IsXMLToken(rNumberStyle
, pStyleMember
->meNumberStyle
) &&
614 (pStyleMember
->mbLong
== bLong
) &&
615 (pStyleMember
->mbTextual
== bTextual
) &&
616 (pStyleMember
->mbDecimal02
== bDecimal02
) &&
617 ( ( (pStyleMember
->mpText
== nullptr) && (rText
.empty()) ) ||
618 ( pStyleMember
->mpText
&& (o3tl::equalsAscii( rText
, pStyleMember
->mpText
) ) ) ) )
620 mnElements
[mnIndex
++] = static_cast<DataStyleNumber
>(nIndex
+ 1);
626 bool SdXMLNumberFormatImportContext::compareStyle( const SdXMLFixedDataStyle
* pStyle
, sal_Int16
& nIndex
) const
628 if( (pStyle
->mbAutomatic
!= mbAutomatic
) && (nIndex
== 0))
631 sal_Int16 nCompareIndex
;
632 for( nCompareIndex
= 0; nCompareIndex
< 8; nIndex
++, nCompareIndex
++ )
634 if( pStyle
->mpFormat
[nCompareIndex
] != mnElements
[nIndex
] )
641 void SdXMLNumberFormatImportContext::endFastElement(sal_Int32
)
643 for( ; mnIndex
< 16; mnIndex
++ )
645 mnElements
[mnIndex
] = DataStyleNumber::NONE
;
650 // compare import with all time styles
651 for( sal_Int16 nFormat
= 0; nFormat
< SdXMLTimeFormatCount
; nFormat
++ )
653 sal_Int16 nIndex
= 0;
654 if( compareStyle( aSdXMLFixedTimeFormats
[nFormat
], nIndex
) )
663 // compare import with all date styles
664 for( sal_Int16 nFormat
= 0; nFormat
< SdXMLDateFormatCount
; nFormat
++ )
666 sal_Int16 nIndex
= 0;
667 if( compareStyle( aSdXMLFixedDateFormats
[nFormat
], nIndex
) )
672 else if( mnElements
[nIndex
] == DataStyleNumber::TextSpace
)
674 // if it's a valid date ending with a space, see if a time style follows
675 for( sal_Int16 nTimeFormat
= 0; nTimeFormat
< SdXMLTimeFormatCount
; nTimeFormat
++ )
677 sal_Int16 nIndex2
= nIndex
+ 1;
678 if( compareStyle( aSdXMLFixedTimeFormats
[nTimeFormat
], nIndex2
) )
680 mnKey
= (nFormat
+ 2) | ((nTimeFormat
+ 2) << 4);
687 // no date style found? maybe it's an extended time style
690 // compare import with all time styles
691 for( sal_Int16 nFormat
= 0; nFormat
< SdXMLTimeFormatCount
; nFormat
++ )
693 sal_Int16 nIndex
= 0;
694 if( compareStyle( aSdXMLFixedTimeFormats
[nFormat
], nIndex
) )
696 mnKey
= (nFormat
+ 2) << 4;
704 css::uno::Reference
< css::xml::sax::XFastContextHandler
> SdXMLNumberFormatImportContext::createFastChildContext(
706 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& xAttrList
)
708 return new SdXMLNumberFormatMemberImportContext( GetImport(), nElement
, xAttrList
,
709 this, static_cast<SvXMLImportContext
*>(SvXMLNumFormatContext::createFastChildContext( nElement
, xAttrList
).get()) );
712 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */