bump product version to 5.0.4.1
[LibreOffice.git] / xmloff / source / text / XMLFootnoteConfigurationImportContext.cxx
blob8d010e54a8ee7bfda8a8f14ece7de220b5e0f8b2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
21 #include "XMLFootnoteConfigurationImportContext.hxx"
23 #include <rtl/ustring.hxx>
24 #include <rtl/ustrbuf.hxx>
26 #include <sax/tools/converter.hxx>
28 #include <xmloff/nmspmap.hxx>
29 #include <xmloff/xmlnmspe.hxx>
30 #include <xmloff/xmltoken.hxx>
32 #include <xmloff/families.hxx>
33 #include <xmloff/xmluconv.hxx>
34 #include <xmloff/xmlimp.hxx>
35 #include <xmloff/xmlnumi.hxx>
36 #include <com/sun/star/xml/sax/XAttributeList.hpp>
37 #include <com/sun/star/beans/XPropertySet.hpp>
38 #include <com/sun/star/text/XFootnote.hpp>
39 #include <com/sun/star/text/XFootnotesSupplier.hpp>
40 #include <com/sun/star/text/XEndnotesSupplier.hpp>
41 #include <com/sun/star/text/FootnoteNumbering.hpp>
42 #include <com/sun/star/style/NumberingType.hpp>
46 using namespace ::com::sun::star::text;
47 using namespace ::com::sun::star::beans;
48 using namespace ::com::sun::star::uno;
49 using namespace ::com::sun::star::style;
50 using namespace ::com::sun::star::xml::sax;
51 using namespace ::xmloff::token;
54 // XMLFootnoteConfigHelper
57 /// local helper class for import of quo-vadis and ergo-sum elements
58 class XMLFootnoteConfigHelper : public SvXMLImportContext
60 OUStringBuffer sBuffer;
61 XMLFootnoteConfigurationImportContext& rConfig;
62 bool bIsBegin;
64 public:
65 TYPEINFO_OVERRIDE();
67 XMLFootnoteConfigHelper(
68 SvXMLImport& rImport,
69 sal_uInt16 nPrfx,
70 const OUString& rLName,
71 XMLFootnoteConfigurationImportContext& rConfigImport,
72 bool bBegin);
74 virtual void EndElement() SAL_OVERRIDE;
76 virtual void Characters( const OUString& rChars ) SAL_OVERRIDE;
79 TYPEINIT1( XMLFootnoteConfigHelper, SvXMLImportContext );
81 XMLFootnoteConfigHelper::XMLFootnoteConfigHelper(
82 SvXMLImport& rImport,
83 sal_uInt16 nPrfx,
84 const OUString& rLName,
85 XMLFootnoteConfigurationImportContext& rConfigImport,
86 bool bBegin)
87 : SvXMLImportContext(rImport, nPrfx, rLName)
88 , sBuffer()
89 , rConfig(rConfigImport)
90 , bIsBegin(bBegin)
94 void XMLFootnoteConfigHelper::EndElement()
96 if (bIsBegin)
98 rConfig.SetBeginNotice(sBuffer.makeStringAndClear());
100 else
102 rConfig.SetEndNotice(sBuffer.makeStringAndClear());
104 // rConfig = NULL; // import contexts are ref-counted
107 void XMLFootnoteConfigHelper::Characters( const OUString& rChars )
109 sBuffer.append(rChars);
114 // XMLFootnoteConfigurationImportContext
118 TYPEINIT1( XMLFootnoteConfigurationImportContext, SvXMLStyleContext );
120 XMLFootnoteConfigurationImportContext::XMLFootnoteConfigurationImportContext(
121 SvXMLImport& rImport,
122 sal_uInt16 nPrfx,
123 const OUString& rLocalName,
124 const Reference<XAttributeList> & xAttrList)
125 : SvXMLStyleContext(rImport, nPrfx, rLocalName, xAttrList, XML_STYLE_FAMILY_TEXT_FOOTNOTECONFIG)
126 , sPropertyAnchorCharStyleName("AnchorCharStyleName")
127 , sPropertyCharStyleName("CharStyleName")
128 , sPropertyNumberingType("NumberingType")
129 , sPropertyPageStyleName("PageStyleName")
130 , sPropertyParagraphStyleName("ParaStyleName")
131 , sPropertyPrefix("Prefix")
132 , sPropertyStartAt("StartAt")
133 , sPropertySuffix("Suffix")
134 , sPropertyPositionEndOfDoc("PositionEndOfDoc")
135 , sPropertyFootnoteCounting("FootnoteCounting")
136 , sPropertyEndNotice("EndNotice")
137 , sPropertyBeginNotice("BeginNotice")
138 , sNumFormat("1")
139 , sNumSync("false")
140 , pAttrTokenMap(NULL)
141 , nOffset(0)
142 , nNumbering(FootnoteNumbering::PER_PAGE)
143 , bPosition(false)
144 , bIsEndnote(false)
146 sal_Int16 nLength = xAttrList->getLength();
147 for(sal_Int16 nAttr = 0; nAttr < nLength; nAttr++)
149 OUString sLocalName;
150 sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
151 GetKeyByAttrName( xAttrList->getNameByIndex(nAttr),
152 &sLocalName );
153 if( XML_NAMESPACE_TEXT == nPrefix && IsXMLToken( sLocalName,
154 XML_NOTE_CLASS ) )
156 const OUString& rValue = xAttrList->getValueByIndex( nAttr );
157 if( IsXMLToken( rValue, XML_ENDNOTE ) )
159 bIsEndnote = true;
160 SetFamily( XML_STYLE_FAMILY_TEXT_FOOTNOTECONFIG );
162 break;
167 XMLFootnoteConfigurationImportContext::~XMLFootnoteConfigurationImportContext()
169 delete pAttrTokenMap;
172 enum XMLFtnConfigToken
174 XML_TOK_FTNCONFIG_CITATION_STYLENAME,
175 XML_TOK_FTNCONFIG_ANCHOR_STYLENAME,
176 XML_TOK_FTNCONFIG_DEFAULT_STYLENAME,
177 XML_TOK_FTNCONFIG_PAGE_STYLENAME,
178 XML_TOK_FTNCONFIG_OFFSET,
179 XML_TOK_FTNCONFIG_NUM_PREFIX,
180 XML_TOK_FTNCONFIG_NUM_SUFFIX,
181 XML_TOK_FTNCONFIG_NUM_FORMAT,
182 XML_TOK_FTNCONFIG_NUM_SYNC,
183 XML_TOK_FTNCONFIG_START_AT,
184 XML_TOK_FTNCONFIG_POSITION
187 static const SvXMLTokenMapEntry aTextFieldAttrTokenMap[] =
189 { XML_NAMESPACE_TEXT, XML_CITATION_STYLE_NAME, XML_TOK_FTNCONFIG_CITATION_STYLENAME },
190 { XML_NAMESPACE_TEXT, XML_CITATION_BODY_STYLE_NAME, XML_TOK_FTNCONFIG_ANCHOR_STYLENAME },
191 { XML_NAMESPACE_TEXT, XML_DEFAULT_STYLE_NAME, XML_TOK_FTNCONFIG_DEFAULT_STYLENAME },
192 { XML_NAMESPACE_TEXT, XML_MASTER_PAGE_NAME, XML_TOK_FTNCONFIG_PAGE_STYLENAME },
193 { XML_NAMESPACE_TEXT, XML_START_VALUE, XML_TOK_FTNCONFIG_OFFSET },
194 { XML_NAMESPACE_STYLE, XML_NUM_PREFIX, XML_TOK_FTNCONFIG_NUM_PREFIX },
195 { XML_NAMESPACE_STYLE, XML_NUM_SUFFIX, XML_TOK_FTNCONFIG_NUM_SUFFIX },
196 { XML_NAMESPACE_STYLE, XML_NUM_FORMAT, XML_TOK_FTNCONFIG_NUM_FORMAT },
197 { XML_NAMESPACE_STYLE, XML_NUM_LETTER_SYNC, XML_TOK_FTNCONFIG_NUM_SYNC },
198 { XML_NAMESPACE_TEXT, XML_START_NUMBERING_AT, XML_TOK_FTNCONFIG_START_AT},
199 { XML_NAMESPACE_TEXT, XML_FOOTNOTES_POSITION, XML_TOK_FTNCONFIG_POSITION},
201 // for backwards compatibility with SRC630 & earlier
202 { XML_NAMESPACE_TEXT, XML_NUM_PREFIX, XML_TOK_FTNCONFIG_NUM_PREFIX },
203 { XML_NAMESPACE_TEXT, XML_NUM_SUFFIX, XML_TOK_FTNCONFIG_NUM_SUFFIX },
204 { XML_NAMESPACE_TEXT, XML_OFFSET, XML_TOK_FTNCONFIG_OFFSET },
205 XML_TOKEN_MAP_END
208 const SvXMLTokenMap&
209 XMLFootnoteConfigurationImportContext::GetFtnConfigAttrTokenMap()
211 if (NULL == pAttrTokenMap)
213 pAttrTokenMap = new SvXMLTokenMap(aTextFieldAttrTokenMap);
216 return *pAttrTokenMap;
219 static SvXMLEnumMapEntry const aFootnoteNumberingMap[] =
221 { XML_PAGE, FootnoteNumbering::PER_PAGE },
222 { XML_CHAPTER, FootnoteNumbering::PER_CHAPTER },
223 { XML_DOCUMENT, FootnoteNumbering::PER_DOCUMENT },
224 { XML_TOKEN_INVALID, 0 },
227 void XMLFootnoteConfigurationImportContext::StartElement(
228 const Reference<XAttributeList> & xAttrList )
230 sal_Int16 nLength = xAttrList->getLength();
231 for(sal_Int16 nAttr = 0; nAttr < nLength; nAttr++)
233 OUString sLocalName;
234 sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
235 GetKeyByAttrName( xAttrList->getNameByIndex(nAttr),
236 &sLocalName );
237 OUString sValue = xAttrList->getValueByIndex(nAttr);
238 switch (GetFtnConfigAttrTokenMap().Get(nPrefix, sLocalName))
240 case XML_TOK_FTNCONFIG_CITATION_STYLENAME:
241 sCitationStyle = sValue;
242 break;
243 case XML_TOK_FTNCONFIG_ANCHOR_STYLENAME:
244 sAnchorStyle = sValue;
245 break;
246 case XML_TOK_FTNCONFIG_DEFAULT_STYLENAME:
247 sDefaultStyle = sValue;
248 break;
249 case XML_TOK_FTNCONFIG_PAGE_STYLENAME:
250 sPageStyle = sValue;
251 break;
252 case XML_TOK_FTNCONFIG_OFFSET:
254 sal_Int32 nTmp;
255 if (::sax::Converter::convertNumber(nTmp, sValue))
257 nOffset = (sal_uInt16)nTmp;
259 break;
261 case XML_TOK_FTNCONFIG_NUM_PREFIX:
262 sPrefix = sValue;
263 break;
264 case XML_TOK_FTNCONFIG_NUM_SUFFIX:
265 sSuffix = sValue;
266 break;
267 case XML_TOK_FTNCONFIG_NUM_FORMAT:
268 sNumFormat = sValue;
269 break;
270 case XML_TOK_FTNCONFIG_NUM_SYNC:
271 sNumSync = sValue;
272 break;
273 case XML_TOK_FTNCONFIG_START_AT:
275 sal_uInt16 nTmp;
276 if (SvXMLUnitConverter::convertEnum(nTmp, sValue,
277 aFootnoteNumberingMap))
279 nNumbering = nTmp;
281 break;
283 case XML_TOK_FTNCONFIG_POSITION:
284 bPosition = IsXMLToken( sValue, XML_DOCUMENT );
285 break;
286 default:
287 ; // ignore
292 SvXMLImportContext *XMLFootnoteConfigurationImportContext::CreateChildContext(
293 sal_uInt16 nPrefix,
294 const OUString& rLocalName,
295 const Reference<XAttributeList> & xAttrList )
297 SvXMLImportContext* pContext = NULL;
299 if (!bIsEndnote)
301 if (XML_NAMESPACE_TEXT == nPrefix)
303 if ( IsXMLToken( rLocalName,
304 XML_FOOTNOTE_CONTINUATION_NOTICE_FORWARD ) )
306 pContext = new XMLFootnoteConfigHelper(GetImport(),
307 nPrefix, rLocalName,
308 *this, false);
310 else if ( IsXMLToken( rLocalName,
311 XML_FOOTNOTE_CONTINUATION_NOTICE_BACKWARD ) )
313 pContext = new XMLFootnoteConfigHelper(GetImport(),
314 nPrefix, rLocalName,
315 *this, true);
317 // else: default context
319 // else: unknown namespace -> default context
321 // else: endnote -> default context
323 if (pContext == NULL)
325 // default: delegate to super class
326 pContext = SvXMLStyleContext::CreateChildContext(nPrefix,
327 rLocalName,
328 xAttrList);
331 return pContext;
334 // Rename method <CreateAndInsertLate(..)> to <Finish(..)> (#i40597#)
335 void XMLFootnoteConfigurationImportContext::Finish( bool bOverwrite )
338 if (bOverwrite)
340 if (bIsEndnote)
342 Reference<XEndnotesSupplier> xSupplier(
343 GetImport().GetModel(), UNO_QUERY);
344 if (xSupplier.is())
346 ProcessSettings(xSupplier->getEndnoteSettings());
349 else
351 Reference<XFootnotesSupplier> xSupplier(
352 GetImport().GetModel(), UNO_QUERY);
353 if (xSupplier.is())
355 ProcessSettings(xSupplier->getFootnoteSettings());
359 // else: ignore (there's only one configuration, so we can only overwrite)
362 void XMLFootnoteConfigurationImportContext::ProcessSettings(
363 const Reference<XPropertySet> & rConfig)
365 Any aAny;
367 if (!sCitationStyle.isEmpty())
369 aAny <<= GetImport().GetStyleDisplayName(
370 XML_STYLE_FAMILY_TEXT_TEXT, sCitationStyle );
371 rConfig->setPropertyValue(sPropertyCharStyleName, aAny);
374 if (!sAnchorStyle.isEmpty())
376 aAny <<= GetImport().GetStyleDisplayName(
377 XML_STYLE_FAMILY_TEXT_TEXT, sAnchorStyle );
378 rConfig->setPropertyValue(sPropertyAnchorCharStyleName, aAny);
381 if (!sPageStyle.isEmpty())
383 aAny <<= GetImport().GetStyleDisplayName(
384 XML_STYLE_FAMILY_MASTER_PAGE, sPageStyle );
385 rConfig->setPropertyValue(sPropertyPageStyleName, aAny);
388 if (!sDefaultStyle.isEmpty())
390 aAny <<= GetImport().GetStyleDisplayName(
391 XML_STYLE_FAMILY_TEXT_PARAGRAPH, sDefaultStyle );
392 rConfig->setPropertyValue(sPropertyParagraphStyleName, aAny);
395 aAny <<= sPrefix;
396 rConfig->setPropertyValue(sPropertyPrefix, aAny);
398 aAny <<= sSuffix;
399 rConfig->setPropertyValue(sPropertySuffix, aAny);
401 sal_Int16 nNumType = NumberingType::ARABIC;
402 GetImport().GetMM100UnitConverter().convertNumFormat( nNumType, sNumFormat,
403 sNumSync );
404 // #i61399: Corrupt file? It contains "Bullet" as numbering style for footnotes.
405 // Okay, even it seems to be corrupt, we will oversee this and set the style to ARABIC
406 if( NumberingType::CHAR_SPECIAL == nNumType )
407 nNumType = NumberingType::ARABIC;
409 aAny <<= nNumType;
410 rConfig->setPropertyValue(sPropertyNumberingType, aAny);
412 aAny <<= nOffset;
413 rConfig->setPropertyValue(sPropertyStartAt, aAny);
415 if (!bIsEndnote)
417 aAny.setValue(&bPosition, cppu::UnoType<bool>::get());
418 rConfig->setPropertyValue(sPropertyPositionEndOfDoc, aAny);
420 aAny <<= nNumbering;
421 rConfig->setPropertyValue(sPropertyFootnoteCounting, aAny);
423 aAny <<= sEndNotice;
424 rConfig->setPropertyValue(sPropertyEndNotice, aAny);
426 aAny <<= sBeginNotice;
427 rConfig->setPropertyValue(sPropertyBeginNotice, aAny);
431 void XMLFootnoteConfigurationImportContext::SetBeginNotice(
432 const OUString& sText)
434 sBeginNotice = sText;
437 void XMLFootnoteConfigurationImportContext::SetEndNotice(
438 const OUString& sText)
440 sEndNotice = sText;
443 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */