bump product version to 5.0.4.1
[LibreOffice.git] / xmloff / source / text / txtftne.cxx
blob57900bd8efefbca66876e7802241533f2661b37b
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 .
22 /** @#file
24 * This file implements XMLTextParagraphExport methods to export
25 * - footnotes
26 * - endnotes
27 * - footnote configuration elements
28 * - endnote configuration elements
30 #include <tools/debug.hxx>
31 #include <rtl/ustrbuf.hxx>
32 #include <com/sun/star/lang/XServiceInfo.hpp>
33 #include <com/sun/star/beans/XPropertySet.hpp>
34 #include <com/sun/star/beans/XPropertyState.hpp>
35 #include <com/sun/star/text/XTextDocument.hpp>
36 #include <com/sun/star/text/XText.hpp>
37 #include <com/sun/star/text/XTextContent.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/container/XNameReplace.hpp>
43 #include <sax/tools/converter.hxx>
44 #include <xmloff/xmltoken.hxx>
45 #include <xmloff/xmlnmspe.hxx>
46 #include <xmloff/xmlnumfe.hxx>
47 #include <xmloff/xmluconv.hxx>
48 #include <xmloff/nmspmap.hxx>
49 #include <xmloff/xmlexp.hxx>
50 #include <xmloff/families.hxx>
51 #include <xmloff/xmlnume.hxx>
52 #include "XMLTextCharStyleNamesElementExport.hxx"
53 #include <xmloff/XMLEventExport.hxx>
54 #include <xmloff/txtparae.hxx>
57 using namespace ::com::sun::star;
58 using namespace ::com::sun::star::uno;
59 using namespace ::com::sun::star::lang;
60 using namespace ::com::sun::star::beans;
61 using namespace ::com::sun::star::text;
62 using namespace ::com::sun::star::container;
63 using namespace ::xmloff::token;
66 void XMLTextParagraphExport::exportTextFootnote(
67 const Reference<XPropertySet> & rPropSet,
68 const OUString& rText,
69 bool bAutoStyles, bool bIsProgress )
71 // get footnote and associated text
72 Any aAny;
73 aAny = rPropSet->getPropertyValue(sFootnote);
74 Reference<XFootnote> xFootnote;
75 aAny >>= xFootnote;
76 Reference<XText> xText(xFootnote, UNO_QUERY);
78 // are we an endnote?
79 Reference<XServiceInfo> xServiceInfo( xFootnote, UNO_QUERY );
80 bool bIsEndnote = xServiceInfo->supportsService(sTextEndnoteService);
82 if (bAutoStyles)
84 // handle formatting of citation mark
85 Add( XML_STYLE_FAMILY_TEXT_TEXT, rPropSet );
87 // handle formatting within footnote
88 exportTextFootnoteHelper(xFootnote, xText, rText,
89 bAutoStyles, bIsEndnote, bIsProgress );
91 else
93 // create span (for citation mark) if necessary; footnote content
94 // wil be handled via exportTextFootnoteHelper, exportText
95 bool bHasHyperlink;
96 bool bIsUICharStyle = false;
97 bool bHasAutoStyle = false;
99 OUString sStyle = FindTextStyleAndHyperlink( rPropSet, bHasHyperlink,
100 bIsUICharStyle, bHasAutoStyle );
102 // export hyperlink (if we have one)
103 Reference < XPropertySetInfo > xPropSetInfo;
104 if( bHasHyperlink )
106 Reference<XPropertyState> xPropState( rPropSet, UNO_QUERY );
107 xPropSetInfo = rPropSet->getPropertySetInfo();
108 bHasHyperlink =
109 addHyperlinkAttributes( rPropSet, xPropState, xPropSetInfo );
111 SvXMLElementExport aHyperlink( GetExport(), bHasHyperlink,
112 XML_NAMESPACE_TEXT, XML_A,
113 false, false );
115 if( bHasHyperlink )
117 // export events (if supported)
118 OUString sHyperLinkEvents("HyperLinkEvents");
119 if (xPropSetInfo->hasPropertyByName(sHyperLinkEvents))
121 Any a = rPropSet->getPropertyValue(sHyperLinkEvents);
122 Reference<XNameReplace> xName;
123 a >>= xName;
124 GetExport().GetEventExport().Export(xName, false);
129 XMLTextCharStyleNamesElementExport aCharStylesExport(
130 GetExport(), bIsUICharStyle &&
131 aCharStyleNamesPropInfoCache.hasProperty(
132 rPropSet ), bHasAutoStyle,
133 rPropSet, sCharStyleNames );
134 if( !sStyle.isEmpty() )
136 GetExport().AddAttribute( XML_NAMESPACE_TEXT, XML_STYLE_NAME,
137 GetExport().EncodeStyleName( sStyle ) );
138 SvXMLElementExport aElem( GetExport(), XML_NAMESPACE_TEXT,
139 XML_SPAN, false, false );
140 exportTextFootnoteHelper(xFootnote, xText, rText,
141 bAutoStyles, bIsEndnote, bIsProgress );
143 else
145 exportTextFootnoteHelper(xFootnote, xText, rText,
146 bAutoStyles, bIsEndnote, bIsProgress );
153 void XMLTextParagraphExport::exportTextFootnoteHelper(
154 const Reference<XFootnote> & rFootnote,
155 const Reference<XText> & rText,
156 const OUString& rTextString,
157 bool bAutoStyles,
158 bool bIsEndnote,
159 bool bIsProgress )
161 if (bAutoStyles)
163 exportText(rText, bAutoStyles, bIsProgress, true );
165 else
167 // export reference Id (for reference fields)
168 Reference<XPropertySet> xPropSet(rFootnote, UNO_QUERY);
169 Any aAny = xPropSet->getPropertyValue(sReferenceId);
170 sal_Int32 nNumber = 0;
171 aAny >>= nNumber;
172 OUStringBuffer aBuf;
173 aBuf.appendAscii("ftn");
174 aBuf.append(nNumber);
175 GetExport().AddAttribute(XML_NAMESPACE_TEXT, XML_ID,
176 aBuf.makeStringAndClear());
177 GetExport().AddAttribute(XML_NAMESPACE_TEXT, XML_NOTE_CLASS,
178 GetXMLToken( bIsEndnote ? XML_ENDNOTE
179 : XML_FOOTNOTE ) );
181 SvXMLElementExport aNote(GetExport(), XML_NAMESPACE_TEXT,
182 XML_NOTE, false, false);
184 // handle label vs. automatic numbering
185 OUString sLabel = rFootnote->getLabel();
186 if (!sLabel.isEmpty())
188 GetExport().AddAttribute(XML_NAMESPACE_TEXT, XML_LABEL,
189 sLabel);
191 // else: automatic numbering -> no attribute
193 SvXMLElementExport aCite(GetExport(), XML_NAMESPACE_TEXT,
194 XML_NOTE_CITATION, false, false);
195 GetExport().Characters(rTextString);
199 SvXMLElementExport aBody(GetExport(), XML_NAMESPACE_TEXT,
200 XML_NOTE_BODY, false, false);
201 exportText(rText, bAutoStyles, bIsProgress, true );
207 void XMLTextParagraphExport::exportTextFootnoteConfiguration()
209 // footnote settings
210 Reference<XFootnotesSupplier> aFootnotesSupplier(GetExport().GetModel(),
211 UNO_QUERY);
212 Reference<XPropertySet> aFootnoteConfiguration(
213 aFootnotesSupplier->getFootnoteSettings());
214 exportTextFootnoteConfigurationHelper(aFootnoteConfiguration, false);
216 // endnote settings
217 Reference<XEndnotesSupplier> aEndnotesSupplier(GetExport().GetModel(),
218 UNO_QUERY);
219 Reference<XPropertySet> aEndnoteConfiguration(
220 aEndnotesSupplier->getEndnoteSettings());
221 exportTextFootnoteConfigurationHelper(aEndnoteConfiguration, true);
225 static void lcl_exportString(
226 SvXMLExport& rExport,
227 const Reference<XPropertySet> & rPropSet,
228 const OUString& sProperty,
229 sal_uInt16 nPrefix,
230 enum XMLTokenEnum eElement,
231 bool bEncodeName,
232 bool bOmitIfEmpty)
234 DBG_ASSERT( eElement != XML_TOKEN_INVALID, "need element token");
236 Any aAny = rPropSet->getPropertyValue(sProperty);
237 OUString sTmp;
238 aAny >>= sTmp;
239 if (!bOmitIfEmpty || !sTmp.isEmpty())
241 if( bEncodeName )
242 sTmp = rExport.EncodeStyleName( sTmp );
243 rExport.AddAttribute(nPrefix, eElement, sTmp);
247 void XMLTextParagraphExport::exportTextFootnoteConfigurationHelper(
248 const Reference<XPropertySet> & rFootnoteConfig,
249 bool bIsEndnote)
251 GetExport().AddAttribute(XML_NAMESPACE_TEXT, XML_NOTE_CLASS,
252 GetXMLToken( bIsEndnote ? XML_ENDNOTE
253 : XML_FOOTNOTE ) );
254 // default/paragraph style
255 lcl_exportString( GetExport(), rFootnoteConfig,
256 sParaStyleName,
257 XML_NAMESPACE_TEXT, XML_DEFAULT_STYLE_NAME,
258 true, true);
260 // citation style
261 lcl_exportString( GetExport(), rFootnoteConfig,
262 sCharStyleName,
263 XML_NAMESPACE_TEXT, XML_CITATION_STYLE_NAME,
264 true, true);
266 // citation body style
267 lcl_exportString( GetExport(), rFootnoteConfig,
268 sAnchorCharStyleName,
269 XML_NAMESPACE_TEXT, XML_CITATION_BODY_STYLE_NAME,
270 true, true);
272 // page style
273 lcl_exportString( GetExport(), rFootnoteConfig,
274 sPageStyleName,
275 XML_NAMESPACE_TEXT, XML_MASTER_PAGE_NAME,
276 true, true );
278 // prefix
279 lcl_exportString( GetExport(), rFootnoteConfig, sPrefix,
280 XML_NAMESPACE_STYLE, XML_NUM_PREFIX, false, true);
282 // suffix
283 lcl_exportString( GetExport(), rFootnoteConfig, sSuffix,
284 XML_NAMESPACE_STYLE, XML_NUM_SUFFIX, false, true);
288 Any aAny;
290 // numbering style
291 OUStringBuffer sBuffer;
292 aAny = rFootnoteConfig->getPropertyValue(sNumberingType);
293 sal_Int16 nNumbering = 0;
294 aAny >>= nNumbering;
295 GetExport().GetMM100UnitConverter().convertNumFormat( sBuffer, nNumbering);
296 GetExport().AddAttribute(XML_NAMESPACE_STYLE, XML_NUM_FORMAT,
297 sBuffer.makeStringAndClear() );
298 SvXMLUnitConverter::convertNumLetterSync( sBuffer, nNumbering);
299 if (!sBuffer.isEmpty() )
301 GetExport().AddAttribute(XML_NAMESPACE_STYLE, XML_NUM_LETTER_SYNC,
302 sBuffer.makeStringAndClear());
305 // StartAt / start-value
306 aAny = rFootnoteConfig->getPropertyValue(sStartAt);
307 sal_Int16 nOffset = 0;
308 aAny >>= nOffset;
309 ::sax::Converter::convertNumber(sBuffer, (sal_Int32)nOffset);
310 GetExport().AddAttribute(XML_NAMESPACE_TEXT, XML_START_VALUE,
311 sBuffer.makeStringAndClear());
313 // some properties are for footnotes only
314 if (!bIsEndnote)
316 // footnotes position
317 aAny = rFootnoteConfig->getPropertyValue(
318 sPositionEndOfDoc);
319 GetExport().AddAttribute(XML_NAMESPACE_TEXT, XML_FOOTNOTES_POSITION,
320 ( (*static_cast<sal_Bool const *>(aAny.getValue())) ?
321 XML_DOCUMENT : XML_PAGE ) );
323 aAny = rFootnoteConfig->getPropertyValue(sFootnoteCounting);
324 sal_Int16 nTmp = 0;
325 aAny >>= nTmp;
326 enum XMLTokenEnum eElement;
327 switch (nTmp)
329 case FootnoteNumbering::PER_PAGE:
330 eElement = XML_PAGE;
331 break;
332 case FootnoteNumbering::PER_CHAPTER:
333 eElement = XML_CHAPTER;
334 break;
335 case FootnoteNumbering::PER_DOCUMENT:
336 default:
337 eElement = XML_DOCUMENT;
338 break;
340 GetExport().AddAttribute(XML_NAMESPACE_TEXT,
341 XML_START_NUMBERING_AT, eElement);
344 // element
345 SvXMLElementExport aFootnoteConfigElement(
346 GetExport(), XML_NAMESPACE_TEXT,
347 XML_NOTES_CONFIGURATION,
348 true, true);
350 // two element for footnote content
351 if (!bIsEndnote)
353 OUString sTmp;
355 // end notice / quo vadis
356 aAny = rFootnoteConfig->getPropertyValue(sEndNotice);
357 aAny >>= sTmp;
359 if (!sTmp.isEmpty())
361 SvXMLElementExport aElem(GetExport(), XML_NAMESPACE_TEXT,
362 XML_FOOTNOTE_CONTINUATION_NOTICE_FORWARD,
363 true, false);
364 GetExport().Characters(sTmp);
367 // begin notice / ergo sum
368 aAny = rFootnoteConfig->getPropertyValue(sBeginNotice);
369 aAny >>= sTmp;
371 if (!sTmp.isEmpty())
373 SvXMLElementExport aElem(GetExport(), XML_NAMESPACE_TEXT,
374 XML_FOOTNOTE_CONTINUATION_NOTICE_BACKWARD,
375 true, false);
376 GetExport().Characters(sTmp);
381 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */