Coverage Report

Created: 2026-05-16 09:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/xmloff/inc/txtflde.hxx
Line
Count
Source
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
/** @#file
21
 *  XML export of all text fields
22
 */
23
24
#pragma once
25
26
#include <com/sun/star/uno/Reference.h>
27
#include <xmloff/xmlnamespace.hxx>
28
29
#include <rtl/ustring.hxx>
30
#include <xmloff/xmltoken.hxx>
31
32
#include <map>
33
#include <set>
34
#include <memory>
35
#include <optional>
36
#include <string_view>
37
38
class SvXMLExport;
39
struct XMLPropertyState;
40
41
namespace com::sun::star {
42
    namespace util { struct DateTime; }
43
    namespace text { class XTextField; }
44
    namespace text { class XText; }
45
    namespace beans { class XPropertySet; }
46
    namespace beans { class XPropertySetInfo; }
47
    namespace uno { template<typename A> class Sequence; }
48
}
49
50
51
/// field IDs,
52
//   including translation between UNO speak and XML speak if appropriate
53
54
enum FieldIdEnum {
55
    FIELD_ID_SENDER,        // sender == extended user
56
    FIELD_ID_AUTHOR,
57
    FIELD_ID_DATE,          // current date
58
    FIELD_ID_TIME,          // current time (+date)
59
    FIELD_ID_PAGENAME,      // page/slide name
60
    FIELD_ID_PAGENUMBER,    // page number
61
    FIELD_ID_PAGESTRING,    // page continuation string (page number string)
62
    FIELD_ID_REFPAGE_SET,   // set reference page
63
    FIELD_ID_REFPAGE_GET,   // get reference page number
64
65
    FIELD_ID_PLACEHOLDER,   // placeholder field == jump edit field
66
67
    FIELD_ID_VARIABLE_GET,  // get variable == get expression
68
    FIELD_ID_VARIABLE_SET,  // set variable == set expression
69
    FIELD_ID_VARIABLE_INPUT,    // input field (variable)
70
    FIELD_ID_USER_GET,      // user field
71
    FIELD_ID_USER_INPUT,    // input field (user field)
72
    FIELD_ID_TEXT_INPUT,    // input field (text)
73
    FIELD_ID_EXPRESSION,    // expression field = formula field
74
    FIELD_ID_SEQUENCE,      // sequence field
75
76
    FIELD_ID_DATABASE_NEXT,     // select next row
77
    FIELD_ID_DATABASE_SELECT,   // select row # (NumSet)
78
    FIELD_ID_DATABASE_DISPLAY,  // display data (form letter field)
79
    FIELD_ID_DATABASE_NAME,     // display current db name (database name)
80
    FIELD_ID_DATABASE_NUMBER,   // display row # (SetNumber)
81
82
    FIELD_ID_DOCINFO_CREATION_AUTHOR,   // docinfo fields
83
    FIELD_ID_DOCINFO_CREATION_TIME,
84
    FIELD_ID_DOCINFO_CREATION_DATE,
85
    FIELD_ID_DOCINFO_DESCRIPTION,
86
    FIELD_ID_DOCINFO_CUSTOM,
87
    FIELD_ID_DOCINFO_PRINT_TIME,
88
    FIELD_ID_DOCINFO_PRINT_DATE,
89
    FIELD_ID_DOCINFO_PRINT_AUTHOR,
90
    FIELD_ID_DOCINFO_TITLE,
91
    FIELD_ID_DOCINFO_SUBJECT,
92
    FIELD_ID_DOCINFO_KEYWORDS,
93
    FIELD_ID_DOCINFO_REVISION,
94
    FIELD_ID_DOCINFO_EDIT_DURATION,
95
    FIELD_ID_DOCINFO_SAVE_TIME,
96
    FIELD_ID_DOCINFO_SAVE_DATE,
97
    FIELD_ID_DOCINFO_SAVE_AUTHOR,
98
99
    FIELD_ID_CONDITIONAL_TEXT,          // conditionally choose between 2 texts
100
    FIELD_ID_HIDDEN_TEXT,               // conditionally hide a text
101
    FIELD_ID_HIDDEN_PARAGRAPH,          // conditionally hide a paragraph
102
103
    FIELD_ID_TEMPLATE_NAME,     // display name of template
104
    FIELD_ID_CHAPTER,           // display name/number of current chapter
105
    FIELD_ID_FILE_NAME,         // display name of current file
106
107
    FIELD_ID_COUNT_PARAGRAPHS,      // statistics fields: - paragraphs
108
    FIELD_ID_COUNT_WORDS,           //                    - words
109
    FIELD_ID_COUNT_CHARACTERS,      //                    - chars
110
    FIELD_ID_COUNT_PAGES,           //                    - pages
111
    FIELD_ID_COUNT_PAGES_RANGE,     //                    - pages in section/range
112
113
    FIELD_ID_COUNT_TABLES,          //                    - tables
114
    FIELD_ID_COUNT_GRAPHICS,        //                    - graphics
115
    FIELD_ID_COUNT_OBJECTS,         //                    - objects
116
117
    FIELD_ID_MACRO,                 // macro fields
118
    FIELD_ID_REF_REFERENCE,         // get reference field (reference)
119
    FIELD_ID_REF_SEQUENCE,          // get reference field (sequence)
120
    FIELD_ID_REF_BOOKMARK,          // get reference field (bookmark)
121
    FIELD_ID_REF_FOOTNOTE,          // get reference field (footnote)
122
    FIELD_ID_REF_ENDNOTE,           // get reference field (endnote)
123
    FIELD_ID_REF_STYLE,             // styleref field
124
    FIELD_ID_DDE,                   // DDE field
125
126
    FIELD_ID_BIBLIOGRAPHY,          // bibliography index entry
127
128
    FIELD_ID_SHEET_NAME,            // name of current (spread-)sheet
129
    FIELD_ID_URL,                   // URL field (only Calc, Draw, Impress)
130
131
    FIELD_ID_SCRIPT,                // script fields (for HTML pages, mostly)
132
    FIELD_ID_ANNOTATION,            // annotation (notice) field
133
134
    FIELD_ID_COMBINED_CHARACTERS,   // combined characters (asian typography)
135
136
    FIELD_ID_META,                  // text:meta-field (RDF metadata)
137
138
    FIELD_ID_MEASURE,               // for measure shapes
139
140
    FIELD_ID_TABLE_FORMULA,         // DEPRECATED: table formulas (Writer 2.0)
141
    FIELD_ID_DROP_DOWN,             // DEPRECATED: dropdown fields (WW8)
142
143
    FIELD_ID_DRAW_HEADER,
144
    FIELD_ID_DRAW_FOOTER,
145
    FIELD_ID_DRAW_DATE_TIME,
146
147
    FIELD_ID_UNKNOWN        // invalid or unknown field type!
148
};
149
150
151
class XMLTextFieldExport final
152
{
153
    SvXMLExport& rExport;
154
155
    /// store used text field master names (NULL means: don't collect)
156
    std::optional< ::std::map<
157
            css::uno::Reference< css::text::XText >,
158
            ::std::set< OUString > > >
159
        moUsedMasters;
160
161
public:
162
163
    XMLTextFieldExport( SvXMLExport& rExp,
164
                        /// XMLPropertyState for the combined characters field
165
                        std::unique_ptr<XMLPropertyState> pCombinedCharState );
166
    ~XMLTextFieldExport();
167
168
    /// Export this field and the surrounding span element with the formatting.
169
    /// To be called for every field in the document body.
170
    void ExportField(const css::uno::Reference < css::text::XTextField > & rTextField,
171
                     bool bProgress, bool & rPrevCharIsSpace);
172
173
    /// collect styles (character styles, data styles, ...) for this field
174
    /// (if appropriate).
175
    /// Also collect used field masters (if pUsedMasters is set)
176
    /// to be called for every field during style export.
177
    void ExportFieldAutoStyle(const css::uno::Reference < css::text::XTextField > & rTextField,
178
                 const bool bProgress );
179
180
    /// export field declarations.
181
    /// to be called once at beginning of document body.
182
    void ExportFieldDeclarations();
183
184
    /// export field declarations for fields used in the particular XText.
185
    /// (Requires that a list of used field declarations has previously been
186
    ///  built-up in ExportFieldAutoStyle() )
187
    void ExportFieldDeclarations(
188
        const css::uno::Reference < css::text::XText > & rText);
189
190
    /// export all field declarations, or only those that have been used?
191
    /// Calling this method will reset the list of used field declarations.
192
    void SetExportOnlyUsedFieldDeclarations(
193
        bool bExportOnlyUsed = true);
194
195
    // determine element or attribute names
196
    // (public, because they may be useful in related XML export classes)
197
    static enum ::xmloff::token::XMLTokenEnum MapPlaceholderType(sal_uInt16 nType);
198
    static enum ::xmloff::token::XMLTokenEnum MapTemplateDisplayFormat(sal_Int16 nType);
199
    static enum ::xmloff::token::XMLTokenEnum MapChapterDisplayFormat(sal_Int16 nType);
200
    static enum ::xmloff::token::XMLTokenEnum MapFilenameDisplayFormat(sal_Int16 nType);
201
    static enum ::xmloff::token::XMLTokenEnum MapDocInfoFieldName(enum FieldIdEnum nToken);
202
    static enum ::xmloff::token::XMLTokenEnum MapReferenceSource(sal_Int16 nType);
203
    static enum ::xmloff::token::XMLTokenEnum MapReferenceType(sal_Int16 nType);
204
    static enum ::xmloff::token::XMLTokenEnum MapCountFieldName(FieldIdEnum nToken);
205
    static enum ::xmloff::token::XMLTokenEnum MapBibliographyFieldName(std::u16string_view sName);
206
    static enum ::xmloff::token::XMLTokenEnum MapMeasureKind(sal_Int16 nKind);
207
    static enum ::xmloff::token::XMLTokenEnum MapPageNumberName(const css::uno::Reference< css::beans::XPropertySet> & xPropSet,
208
                      sal_Int32& nOffset);  /// also adjust page offset
209
    static enum ::xmloff::token::XMLTokenEnum MapAuthorFieldName(const css::uno::Reference< css::beans::XPropertySet > & xPropSet);
210
    static enum ::xmloff::token::XMLTokenEnum MapSenderFieldName(const css::uno::Reference< css::beans::XPropertySet > & xPropSet);
211
212
private:
213
214
0
    SvXMLExport& GetExport() { return rExport; }
215
216
    /// export a field after <text:span> is already written
217
    void ExportFieldHelper(
218
        const css::uno::Reference< css::text::XTextField> & rTextField,
219
        const css::uno::Reference< css::beans::XPropertySet> & rPropSet,
220
        const css::uno::Reference< css::beans::XPropertySet> & rRangePropSet,
221
        enum FieldIdEnum nToken,
222
        bool bProgress,
223
        bool & rPrevCharIsSpace);
224
225
    /// export an empty element
226
    void ExportElement(enum ::xmloff::token::XMLTokenEnum eElement, /// element token
227
                       bool bAddSpace = false); /// add blanks around
228
                                                        /// element?
229
230
    /// export an element with string content
231
    void ExportElement(enum ::xmloff::token::XMLTokenEnum eElement, /// element token
232
                       const OUString& sContent,
233
                       sal_uInt16 nNamespace = XML_NAMESPACE_TEXT); /// element content
234
235
    /// export a macro (as used in the macro field)
236
    void ExportMacro( const css::uno::Reference< css::beans::XPropertySet> & rPropSet,
237
                      const OUString& rContent);
238
239
    /// export text:meta-field (RDF metadata)
240
    void ExportMetaField( const css::uno::Reference< css::beans::XPropertySet> & i_xMeta,
241
                          bool i_bAutoStyles, bool i_bProgress,
242
                          bool & rPrevCharIsSpace);
243
244
245
    void ProcessBoolean(
246
        enum ::xmloff::token::XMLTokenEnum eXmlName,    /// attribute token
247
        bool bBool,     /// attribute value
248
        bool bDefault,
249
        sal_uInt16 nPrefix); /// namespace
250
251
    /// export a boolean attribute
252
    void ProcessBoolean(
253
        enum ::xmloff::token::XMLTokenEnum eXmlName,    /// attribute token (namespace text)
254
        bool bBool,     /// attribute value
255
        bool bDefault); /// attribute default; omit, if attribute differs
256
257
    /// export an integer attribute
258
    void ProcessInteger(
259
        enum ::xmloff::token::XMLTokenEnum eXmlName,    /// attribute token (namespace text)
260
        sal_Int32 nNum);            /// attribute value
261
262
    /// export an integer attribute, omit if default
263
    void ProcessIntegerDef(
264
        enum ::xmloff::token::XMLTokenEnum eXmlName,    /// attribute token (namespace text)
265
        sal_Int32 nNum,             /// attribute value
266
        sal_Int32 nDefault);        /// default value
267
268
    /// export a string attribute
269
    void ProcessString(
270
        enum ::xmloff::token::XMLTokenEnum eXmlName,        /// attribute token (namespace text)
271
        const OUString& sValue,  /// attribute value
272
        bool bOmitEmpty = false, /// omit attribute, if value is empty
273
        sal_uInt16 nPrefix = XML_NAMESPACE_TEXT);   /// attribute name prefix
274
275
    /// export a string attribute that gets a QName value
276
    void ProcessString(
277
        enum ::xmloff::token::XMLTokenEnum eXmlName,        /// attribute token (namespace text)
278
        sal_uInt16 nValuePrefix,
279
        const OUString& sValue);  /// attribute value
280
281
282
    /// export a string attribute, omit if default
283
    void ProcessString(
284
        enum ::xmloff::token::XMLTokenEnum eXmlName,        /// attribute token (namespace text)
285
        const OUString& sValue,  /// attribute value
286
        std::u16string_view sDefault); /// default value; omit if equal
287
288
    /// export a string attribute, omit if default
289
    void ProcessString(
290
        enum ::xmloff::token::XMLTokenEnum eXmlName,        /// attribute token (namespace text)
291
        sal_uInt16 nValuePrefix,
292
        const OUString& sValue,  /// attribute value
293
        std::u16string_view sDefault); /// default value; omit if equal
294
295
    /// export a string attribute
296
    void ProcessString(
297
        enum ::xmloff::token::XMLTokenEnum eXmlName,        /// attribute token (namespace text)
298
        enum ::xmloff::token::XMLTokenEnum eValue,          /// attribute token
299
        sal_uInt16 nPrefix = XML_NAMESPACE_TEXT);   /// attribute name prefix
300
301
    /// export a string attribute, omit if default
302
    void ProcessString(
303
        enum ::xmloff::token::XMLTokenEnum eXmlName,        /// attribute token (namespace text)
304
        enum ::xmloff::token::XMLTokenEnum eValue,          /// attribute value token
305
        enum ::xmloff::token::XMLTokenEnum eDefault);       /// default value token
306
307
    /// export a string as a sequence of paragraphs
308
    void ProcessParagraphSequence(
309
        /// string containing the paragraphs
310
        std::u16string_view sParagraphSequence);
311
312
    /// export a numbering format (numeric, roman, alphabetic, etc.)
313
    void ProcessNumberingType(
314
        sal_Int16 nNumberingType);      /// numbering type key
315
316
    /// export display attribute (value, formula, none)
317
    void ProcessDisplay(bool bIsVisible,    /// is visible?
318
                        bool bIsCommand);    /// is show command/show name?
319
320
    /// export all data-style related attributes
321
    void ProcessValueAndType(
322
        bool bIsString,     /// do we process a string or a number?
323
        sal_Int32 nFormatKey,   /// format key for NumberFormatter; possibly -1
324
        const OUString& sContent, /// string content; possibly invalid
325
        std::u16string_view sDefault, /// default string
326
        double fValue,          /// float content; possibly invalid
327
        bool bExportValue,  /// export value attribute?
328
        bool bExportValueType,  /// export value-type attribute?
329
        bool bExportStyle,  /// export style-attribute?
330
        bool bForceSystemLanguage, /// no style language export
331
        bool bTimeStyle = false); /// exporting a time style?
332
333
    /// export times, dates and durations according to ISO 8601
334
    void ProcessDateTime(
335
        enum ::xmloff::token::XMLTokenEnum eXMLName,    /// attribute token
336
        double dValue,              /// date/time value
337
        bool bIsDate,           /// export as date (rather than date/time)?
338
        bool bIsDuration,           /// export as duration
339
        bool bOmitDurationIfZero = true,    /// omit zero-length durat.
340
        sal_uInt16 nPrefix = XML_NAMESPACE_TEXT);   /// attribute name prefix
341
342
    /// export a date, time, or duration
343
    void ProcessDateTime(
344
        enum ::xmloff::token::XMLTokenEnum eXMLName,    /// attribute token
345
        sal_Int32 nMinutes,             /// date/time value in minutes
346
        bool bIsDate,           /// export as date?
347
        bool bIsDuration);       /// export as duration?
348
349
    /// export times, dates and durations according to ISO 8601
350
    void ProcessDateTime(
351
        enum ::xmloff::token::XMLTokenEnum eXMLName,    /// attribute token
352
        const css::util::DateTime& rTime);      /// date/time value
353
354
    /// export time or dateTime
355
    void ProcessTimeOrDateTime(
356
        enum ::xmloff::token::XMLTokenEnum eXMLName,    /// attribute token
357
        const css::util::DateTime& rTime);  /// date/time value
358
359
    /// export all attributes for bibliography data fields
360
    void ProcessBibliographyData(
361
        const css::uno::Reference <
362
            css::beans::XPropertySet > & rPropertySet);
363
364
    /// export CommandTypeAttribute
365
    void ProcessCommandType(
366
        sal_Int32 nCommandType);        /// css::sdb::CommandType
367
368
    void ProcessStringSequence(
369
        const css::uno::Sequence<OUString>& rSequence,
370
        const OUString& sSelected );
371
372
    void ProcessStringSequence(
373
        const css::uno::Sequence<OUString>& rSequence,
374
        sal_Int32 nSelected );
375
376
    /// export attributes that describe a data source
377
    void ExportDataBaseElement(
378
        enum ::xmloff::token::XMLTokenEnum eElement,
379
        const OUString& sContent,
380
        const css::uno::Reference < css::beans::XPropertySet > & rPropertySet,
381
        const css::uno::Reference < css::beans::XPropertySetInfo > & rPropertySetInfo );
382
383
    /// for XDependentTextFields, get PropertySet of FieldMaster
384
    static css::uno::Reference < css::beans::XPropertySet >
385
    GetMasterPropertySet(const css::uno::Reference < css::text::XTextField > & rTextField);
386
387
    /// get PropertySet of (any) DependentTextField for this FieldMaster
388
    static bool GetDependentFieldPropertySet(
389
        const css::uno::Reference< css::beans::XPropertySet> & xmaster,
390
        css::uno::Reference< css::beans::XPropertySet> & xField);
391
392
393
    /// get field ID from XTextField (and it's Property-Set)
394
    static enum FieldIdEnum GetFieldID(const css::uno::Reference < css::text::XTextField > & rTextField,
395
                          const css::uno::Reference < css::beans::XPropertySet > & xPropSet);
396
397
    /// get field ID from XTextField service name (and it's PropertySet)
398
    static enum FieldIdEnum MapFieldName(std::u16string_view sFieldName,
399
                            const css::uno::Reference < css::beans::XPropertySet> & xPropSet);
400
401
    /// determine, whether field has string or numeric content
402
    static bool IsStringField(FieldIdEnum nFieldType,  /// field ID
403
                           const css::uno::Reference < css::beans::XPropertySet > & xPropSet);
404
405
406
    /// explode a field master name into field type and field name
407
    static void ExplodeFieldMasterName(
408
        std::u16string_view sMasterName, /// name as returned by SO API
409
        OUString& sFieldType,        /// out: field type
410
        OUString& sVarName);         /// out: variable name
411
412
    /// make reference name for a foot- or endnote
413
    static OUString MakeFootnoteRefName(sal_Int16 nSeqNo);
414
415
    /// make reference name for a sequence field
416
    static OUString MakeSequenceRefName(sal_Int16 nSeqNo,
417
                                              std::u16string_view rSeqName);
418
419
    std::unique_ptr<XMLPropertyState> pCombinedCharactersPropertyState;
420
421
};
422
423
424
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */