Coverage Report

Created: 2026-08-14 10:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/xmloff/source/text/XMLAutoTextEventExport.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 "XMLAutoTextEventExport.hxx"
21
#include <com/sun/star/xml/sax/XDocumentHandler.hpp>
22
#include <com/sun/star/util/MeasureUnit.hpp>
23
#include <com/sun/star/document/XEventsSupplier.hpp>
24
#include <com/sun/star/container/XNameReplace.hpp>
25
#include <com/sun/star/uno/Reference.hxx>
26
#include <com/sun/star/uno/Sequence.hxx>
27
#include <com/sun/star/uno/Exception.hpp>
28
#include <com/sun/star/uno/XComponentContext.hpp>
29
#include <comphelper/errcode.hxx>
30
#include <osl/diagnose.h>
31
#include <xmloff/xmlnamespace.hxx>
32
#include <xmloff/namespacemap.hxx>
33
#include <xmloff/xmltoken.hxx>
34
#include <xmloff/XMLEventExport.hxx>
35
#include <tools/debug.hxx>
36
37
38
using namespace ::com::sun::star;
39
using namespace ::xmloff::token;
40
41
using ::com::sun::star::container::XNameReplace;
42
using ::com::sun::star::document::XEventsSupplier;
43
using ::com::sun::star::uno::Any;
44
using ::com::sun::star::uno::Exception;
45
using ::com::sun::star::uno::Reference;
46
using ::com::sun::star::uno::Sequence;
47
using ::com::sun::star::uno::XInterface;
48
using ::com::sun::star::uno::UNO_QUERY;
49
using ::com::sun::star::xml::sax::XDocumentHandler;
50
51
52
XMLAutoTextEventExport::XMLAutoTextEventExport(
53
    const css::uno::Reference< css::uno::XComponentContext >& xContext,
54
    OUString const & implementationName, SvXMLExportFlags nFlags
55
    )
56
0
:   SvXMLExport(xContext, implementationName, util::MeasureUnit::INCH, XML_AUTO_TEXT, nFlags)
57
0
{
58
0
}
59
60
XMLAutoTextEventExport::~XMLAutoTextEventExport()
61
0
{
62
0
}
63
64
void XMLAutoTextEventExport::initialize(
65
    const Sequence<Any> & rArguments )
66
0
{
67
0
    if (rArguments.getLength() > 1)
68
0
    {
69
0
        Reference<XEventsSupplier> xSupplier;
70
0
        rArguments[1] >>= xSupplier;
71
0
        if (xSupplier.is())
72
0
        {
73
0
            xEvents = xSupplier->getEvents();
74
0
        }
75
0
        else
76
0
        {
77
0
            Reference<XNameReplace> xReplace;
78
0
            rArguments[1] >>= xReplace;
79
0
            if (xReplace.is())
80
0
            {
81
0
                xEvents = xReplace;
82
0
            }
83
0
            else
84
0
            {
85
0
                rArguments[1] >>= xEvents;
86
0
            }
87
0
        }
88
0
    }
89
90
    // call super class (for XHandler)
91
0
    SvXMLExport::initialize(rArguments);
92
0
}
93
94
95
ErrCode XMLAutoTextEventExport::exportDoc( enum XMLTokenEnum )
96
0
{
97
0
    if( !(getExportFlags() & SvXMLExportFlags::OASIS) )
98
0
    {
99
0
        Reference< uno::XComponentContext> xContext = getComponentContext();
100
0
        try
101
0
        {
102
103
0
            Sequence<Any> aArgs{ Any(GetDocHandler()) };
104
105
            // get filter component
106
0
            Reference< xml::sax::XDocumentHandler > xTmpDocHandler(
107
0
                xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
108
0
                    u"com.sun.star.comp.Oasis2OOoTransformer"_ustr,
109
0
                    aArgs,
110
0
                    xContext),
111
0
                UNO_QUERY);
112
0
            OSL_ENSURE( xTmpDocHandler.is(),
113
0
                "can't instantiate OASIS transformer component" );
114
0
            if( xTmpDocHandler.is() )
115
0
            {
116
0
                SetDocHandler( xTmpDocHandler );
117
0
            }
118
0
        }
119
0
        catch( css::uno::Exception& )
120
0
        {
121
0
        }
122
0
    }
123
0
    if (hasEvents())
124
0
    {
125
0
        GetDocHandler()->startDocument();
126
127
0
        addChaffWhenEncryptedStorage();
128
129
0
        addNamespaces();
130
131
0
        {
132
            // container element
133
0
            SvXMLElementExport aContainerElement(
134
0
                *this, XML_NAMESPACE_OOO, XML_AUTO_TEXT_EVENTS,
135
0
                true, true);
136
137
0
            exportEvents();
138
0
        }
139
140
        // and close document again
141
0
        GetDocHandler()->endDocument();
142
0
    }
143
144
0
    return ERRCODE_NONE;
145
0
}
146
147
bool XMLAutoTextEventExport::hasEvents() const
148
0
{
149
    // TODO: provide full implementation that check for presence of events
150
0
    return xEvents.is();
151
0
}
152
153
void XMLAutoTextEventExport::addNamespaces()
154
0
{
155
    // namespaces for office:, text: and script:
156
0
    GetAttrList().AddAttribute(
157
0
        GetNamespaceMap().GetAttrNameByIndex( XML_NAMESPACE_OFFICE ),
158
0
        GetNamespaceMap().GetNameByIndex( XML_NAMESPACE_OFFICE ) );
159
0
    GetAttrList().AddAttribute(
160
0
        GetNamespaceMap().GetAttrNameByIndex( XML_NAMESPACE_TEXT ),
161
0
        GetNamespaceMap().GetNameByIndex( XML_NAMESPACE_TEXT ) );
162
0
    GetAttrList().AddAttribute(
163
0
        GetNamespaceMap().GetAttrNameByIndex( XML_NAMESPACE_SCRIPT ),
164
0
        GetNamespaceMap().GetNameByIndex( XML_NAMESPACE_SCRIPT ) );
165
0
    GetAttrList().AddAttribute(
166
0
        GetNamespaceMap().GetAttrNameByIndex( XML_NAMESPACE_DOM ),
167
0
        GetNamespaceMap().GetNameByIndex( XML_NAMESPACE_DOM ) );
168
0
    GetAttrList().AddAttribute(
169
0
        GetNamespaceMap().GetAttrNameByIndex( XML_NAMESPACE_OOO ),
170
0
        GetNamespaceMap().GetNameByIndex( XML_NAMESPACE_OOO ) );
171
0
    GetAttrList().AddAttribute(
172
0
        GetNamespaceMap().GetAttrNameByIndex( XML_NAMESPACE_XLINK ),
173
0
        GetNamespaceMap().GetNameByIndex( XML_NAMESPACE_XLINK ) );
174
0
}
175
176
void XMLAutoTextEventExport::exportEvents()
177
0
{
178
0
    DBG_ASSERT(hasEvents(), "no events to export!");
179
180
0
    GetEventExport().Export(xEvents);
181
0
}
182
183
184
// methods without content:
185
186
0
void XMLAutoTextEventExport::ExportMeta_() {}
187
0
void XMLAutoTextEventExport::ExportScripts_() {}
188
0
void XMLAutoTextEventExport::ExportFontDecls_() {}
189
0
void XMLAutoTextEventExport::ExportStyles_( bool ) {}
190
0
void XMLAutoTextEventExport::ExportAutoStyles_() {}
191
0
void XMLAutoTextEventExport::ExportMasterStyles_() {}
192
0
void XMLAutoTextEventExport::ExportContent_() {}
193
194
195
// methods to support the component registration
196
197
extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
198
com_sun_star_comp_Writer_XMLOasisAutotextEventsExporter_get_implementation(
199
    css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
200
0
{
201
0
    return cppu::acquire(new XMLAutoTextEventExport(
202
0
        context, u"com.sun.star.comp.Writer.XMLOasisAutotextEventsExporter"_ustr,
203
0
        SvXMLExportFlags::ALL | SvXMLExportFlags::OASIS));
204
0
}
205
206
extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
207
com_sun_star_comp_Writer_XMLAutotextEventsExporter_get_implementation(
208
    css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
209
0
{
210
0
    return cppu::acquire(new XMLAutoTextEventExport(
211
0
        context, u"com.sun.star.comp.Writer.XMLAutotextEventsExporter"_ustr,
212
0
        SvXMLExportFlags::ALL));
213
0
}
214
215
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */