Coverage Report

Created: 2025-11-16 09:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/xmloff/source/transform/MergeElemTContext.cxx
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
#include "MergeElemTContext.hxx"
21
#include "MutableAttrList.hxx"
22
#include "TransformerBase.hxx"
23
#include "TransformerActions.hxx"
24
#include "ElemTransformerAction.hxx"
25
#include "IgnoreTContext.hxx"
26
#include <xmloff/xmlnamespace.hxx>
27
#include <osl/diagnose.h>
28
29
using namespace ::com::sun::star::uno;
30
using namespace ::com::sun::star::xml::sax;
31
using namespace ::xmloff::token;
32
33
namespace {
34
35
class XMLParagraphTransformerContext : public XMLTransformerContext
36
{
37
public:
38
    XMLParagraphTransformerContext( XMLTransformerBase& rTransformer,
39
                           const OUString& rQName );
40
41
    // Create a children element context. By default, the import's
42
    // CreateContext method is called to create a new default context.
43
    virtual rtl::Reference<XMLTransformerContext> CreateChildContext( sal_uInt16 nPrefix,
44
                                   const OUString& rLocalName,
45
                                   const OUString& rQName,
46
                                   const css::uno::Reference< css::xml::sax::XAttributeList >& xAttrList ) override;
47
};
48
49
}
50
51
XMLParagraphTransformerContext::XMLParagraphTransformerContext(
52
        XMLTransformerBase& rImp,
53
        const OUString& rQName ) :
54
0
    XMLTransformerContext( rImp, rQName )
55
0
{
56
0
}
57
58
rtl::Reference<XMLTransformerContext> XMLParagraphTransformerContext::CreateChildContext(
59
        sal_uInt16 /*nPrefix*/,
60
        const OUString& /*rLocalName*/,
61
        const OUString& rQName,
62
        const Reference< XAttributeList >& )
63
0
{
64
0
    return new XMLIgnoreTransformerContext( GetTransformer(),
65
0
                                                rQName, true );
66
0
}
67
68
namespace {
69
70
class XMLPersTextContentRNGTransformTContext : public XMLPersTextContentTContext
71
{
72
public:
73
    XMLPersTextContentRNGTransformTContext(
74
        XMLTransformerBase& rTransformer,
75
        const OUString& rQName,
76
        sal_uInt16 nPrefix,
77
        ::xmloff::token::XMLTokenEnum eToken );
78
79
    virtual void Characters( const OUString& rChars ) override;
80
};
81
82
}
83
84
XMLPersTextContentRNGTransformTContext::XMLPersTextContentRNGTransformTContext(
85
    XMLTransformerBase& rTransformer,
86
    const OUString& rQName,
87
    sal_uInt16 nPrefix,
88
    ::xmloff::token::XMLTokenEnum eToken ) :
89
0
        XMLPersTextContentTContext(
90
0
            rTransformer, rQName, nPrefix, eToken )
91
0
{}
92
93
void XMLPersTextContentRNGTransformTContext::Characters( const OUString& rChars )
94
0
{
95
0
    OUString aConvChars( rChars );
96
0
    XMLTransformerBase::ConvertRNGDateTimeToISO( aConvChars );
97
0
    XMLPersTextContentTContext::Characters( aConvChars );
98
0
}
99
100
101
void XMLMergeElemTransformerContext::ExportStartElement()
102
0
{
103
0
    for( const auto& rChildContext : m_aChildContexts )
104
0
    {
105
0
        XMLPersTextContentTContext *pContext = rChildContext.get();
106
0
        m_xAttrList->AddAttribute( pContext->GetExportQName(),
107
0
                            pContext->GetTextContent() );
108
0
    }
109
0
    XMLTransformerContext::StartElement( m_xAttrList );
110
111
0
    m_bStartElementExported = true;
112
0
}
113
114
XMLMergeElemTransformerContext::XMLMergeElemTransformerContext(
115
        XMLTransformerBase& rImp,
116
        const OUString& rQName,
117
       sal_uInt16 nActionMap ) :
118
0
    XMLTransformerContext( rImp, rQName ),
119
0
    m_nActionMap( nActionMap ),
120
0
    m_bStartElementExported( false )
121
0
{
122
0
}
123
124
void XMLMergeElemTransformerContext::StartElement(
125
    const Reference< XAttributeList >& rAttrList )
126
0
{
127
0
    rtl::Reference<XMLMutableAttributeList> pMutableAttrList =
128
0
        new XMLMutableAttributeList( rAttrList, true );
129
0
    m_xAttrList = pMutableAttrList;
130
131
0
    sal_Int16 nAttrCount = m_xAttrList.is() ? m_xAttrList->getLength() : 0;
132
0
    for( sal_Int16 i=0; i < nAttrCount; i++ )
133
0
    {
134
0
        const OUString aAttrName = m_xAttrList->getNameByIndex( i );
135
0
        OUString aLocalName;
136
0
        sal_uInt16 nPrefix =
137
0
            GetTransformer().GetNamespaceMap().GetKeyByAttrName( aAttrName,
138
0
                                                                &aLocalName );
139
0
        bool bRemove = true;
140
0
        if( XML_NAMESPACE_OFFICE == nPrefix)
141
0
        {
142
0
            if (IsXMLToken( aLocalName, XML_DISPLAY ) )
143
0
                bRemove = false;
144
0
            else if (IsXMLToken( aLocalName, XML_AUTHOR ) )
145
0
                bRemove = false;
146
0
            else if (IsXMLToken( aLocalName, XML_CREATE_DATE ) )
147
0
                bRemove = false;
148
0
            else if (IsXMLToken( aLocalName, XML_CREATE_DATE_STRING ) )
149
0
                bRemove = false;
150
0
        }
151
0
        if (bRemove)
152
0
        {
153
0
            pMutableAttrList->RemoveAttributeByIndex( i );
154
0
            --i;
155
0
            --nAttrCount;
156
0
        }
157
0
    }
158
0
}
159
160
rtl::Reference<XMLTransformerContext> XMLMergeElemTransformerContext::CreateChildContext(
161
        sal_uInt16 nPrefix,
162
        const OUString& rLocalName,
163
        const OUString& rQName,
164
        const Reference< XAttributeList >& rAttrList )
165
0
{
166
0
    rtl::Reference<XMLTransformerContext> pContext;
167
168
0
    if( !m_bStartElementExported )
169
0
    {
170
0
        XMLTransformerActions *pActions =
171
0
            GetTransformer().GetUserDefinedActions( m_nActionMap );
172
0
        OSL_ENSURE( pActions, "go no actions" );
173
0
        if( pActions )
174
0
        {
175
0
            XMLTransformerActions::key_type aKey( nPrefix, rLocalName );
176
0
            XMLTransformerActions::const_iterator aIter =
177
0
                pActions->find( aKey );
178
179
0
            if( aIter != pActions->end() )
180
0
            {
181
0
                switch( (*aIter).second.m_nActionType )
182
0
                {
183
0
                case XML_ETACTION_MOVE_TO_ATTR_RNG2ISO_DATETIME:
184
0
                    {
185
0
                        rtl::Reference<XMLPersTextContentTContext> pTC(
186
0
                            new XMLPersTextContentRNGTransformTContext(
187
0
                                    GetTransformer(), rQName,
188
0
                                    (*aIter).second.GetQNamePrefixFromParam1(),
189
0
                                    (*aIter).second.GetQNameTokenFromParam1() ));
190
0
                        m_aChildContexts.push_back(pTC);
191
0
                        pContext = pTC;
192
0
                    }
193
0
                    break;
194
0
                case XML_ETACTION_MOVE_TO_ATTR:
195
0
                    {
196
0
                        rtl::Reference<XMLPersTextContentTContext> pTC(
197
0
                            new XMLPersTextContentTContext(
198
0
                                    GetTransformer(), rQName,
199
0
                                    (*aIter).second.GetQNamePrefixFromParam1(),
200
0
                                    (*aIter).second.GetQNameTokenFromParam1() ));
201
0
                        m_aChildContexts.push_back(pTC);
202
0
                        pContext = pTC;
203
0
                    }
204
0
                    break;
205
0
                case XML_ETACTION_EXTRACT_CHARACTERS:
206
0
                    {
207
0
                        if( !m_bStartElementExported )
208
0
                            ExportStartElement();
209
0
                        pContext.set(
210
0
                            new XMLParagraphTransformerContext( GetTransformer(),
211
0
                            rQName));
212
0
                    }
213
0
                    break;
214
0
                default:
215
0
                    OSL_ENSURE( false, "unknown action" );
216
0
                    break;
217
0
                }
218
0
            }
219
0
        }
220
0
    }
221
0
    else
222
0
    {
223
0
        XMLTransformerActions *pActions =
224
0
            GetTransformer().GetUserDefinedActions( m_nActionMap );
225
0
        OSL_ENSURE( pActions, "go no actions" );
226
0
        if( pActions )
227
0
        {
228
0
            XMLTransformerActions::key_type aKey( nPrefix, rLocalName );
229
0
            XMLTransformerActions::const_iterator aIter =
230
0
                pActions->find( aKey );
231
232
0
            if( aIter != pActions->end() )
233
0
            {
234
0
                switch( (*aIter).second.m_nActionType )
235
0
                {
236
0
                case XML_ETACTION_EXTRACT_CHARACTERS:
237
0
                    {
238
0
                        if( !m_bStartElementExported )
239
0
                            ExportStartElement();
240
0
                        pContext.set(
241
0
                            new XMLParagraphTransformerContext( GetTransformer(),
242
0
                            rQName));
243
0
                    }
244
0
                    break;
245
0
                default:
246
0
                    OSL_ENSURE( false, "unknown action" );
247
0
                    break;
248
0
                }
249
0
            }
250
0
        }
251
0
    }
252
253
    // default is copying
254
0
    if( !pContext.is() )
255
0
    {
256
0
        if( !m_bStartElementExported )
257
0
            ExportStartElement();
258
0
        pContext = XMLTransformerContext::CreateChildContext( nPrefix,
259
0
                                                              rLocalName,
260
0
                                                              rQName,
261
0
                                                              rAttrList );
262
0
    }
263
264
0
    return pContext;
265
0
}
266
267
void XMLMergeElemTransformerContext::EndElement()
268
0
{
269
0
    if( !m_bStartElementExported )
270
0
        ExportStartElement();
271
0
    XMLTransformerContext::EndElement();
272
0
}
273
274
void XMLMergeElemTransformerContext::Characters( const OUString& )
275
0
{
276
    // ignore
277
0
}
278
279
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */