Coverage Report

Created: 2026-03-31 11:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/xmloff/source/meta/MetaExportComponent.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 <MetaExportComponent.hxx>
21
#include <com/sun/star/xml/sax/XDocumentHandler.hpp>
22
#include <com/sun/star/uno/Sequence.hxx>
23
#include <com/sun/star/uno/Reference.hxx>
24
#include <com/sun/star/uno/Exception.hpp>
25
#include <com/sun/star/util/MeasureUnit.hpp>
26
#include <com/sun/star/beans/PropertyAttribute.hpp>
27
#include <com/sun/star/frame/XModel.hpp>
28
#include <com/sun/star/uno/XComponentContext.hpp>
29
#include <comphelper/errcode.hxx>
30
#include <comphelper/genericpropertyset.hxx>
31
#include <comphelper/propertysetinfo.hxx>
32
#include <osl/diagnose.h>
33
#include <xmloff/xmlnamespace.hxx>
34
#include <xmloff/namespacemap.hxx>
35
#include <xmloff/xmltoken.hxx>
36
#include <xmloff/xmlmetae.hxx>
37
#include <PropertySetMerger.hxx>
38
39
#include <unotools/docinfohelper.hxx>
40
41
42
using namespace ::com::sun::star;
43
using namespace ::xmloff::token;
44
45
XMLMetaExportComponent::XMLMetaExportComponent(
46
    const css::uno::Reference< css::uno::XComponentContext >& xContext,
47
    OUString const & implementationName, SvXMLExportFlags nFlags )
48
0
:   SvXMLExport( xContext, implementationName, util::MeasureUnit::CM, XML_TEXT, nFlags )
49
0
{
50
0
}
51
52
XMLMetaExportComponent::~XMLMetaExportComponent()
53
0
{
54
0
}
55
56
void SAL_CALL XMLMetaExportComponent::setSourceDocument( const css::uno::Reference< css::lang::XComponent >& xDoc )
57
0
{
58
0
    try
59
0
    {
60
0
        SvXMLExport::setSourceDocument( xDoc );
61
0
    }
62
0
    catch( lang::IllegalArgumentException& )
63
0
    {
64
        // allow to use document properties service without model access
65
        // this is required for document properties exporter
66
0
        mxDocProps =
67
0
            uno::Reference< document::XDocumentProperties >::query( xDoc );
68
0
        if( !mxDocProps.is() )
69
0
            throw lang::IllegalArgumentException();
70
0
    }
71
0
}
72
73
ErrCode XMLMetaExportComponent::exportDoc( enum XMLTokenEnum )
74
0
{
75
0
    uno::Reference< xml::sax::XDocumentHandler > xDocHandler = GetDocHandler();
76
77
0
    if( !(getExportFlags() & SvXMLExportFlags::OASIS) )
78
0
    {
79
0
        uno::Reference< uno::XComponentContext > xContext = getComponentContext();
80
0
        try
81
0
        {
82
0
            static const ::comphelper::PropertyMapEntry aInfoMap[] =
83
0
            {
84
0
                { u"Class"_ustr, 0,
85
0
                    ::cppu::UnoType<OUString>::get(),
86
0
                    beans::PropertyAttribute::MAYBEVOID, 0},
87
0
            };
88
0
            uno::Reference< beans::XPropertySet > xConvPropSet(
89
0
                ::comphelper::GenericPropertySet_CreateInstance(
90
0
                        new ::comphelper::PropertySetInfo( aInfoMap ) ) );
91
92
0
            xConvPropSet->setPropertyValue(u"Class"_ustr, uno::Any(GetXMLToken( XML_TEXT )) );
93
94
0
            uno::Reference< beans::XPropertySet > xPropSet =
95
0
                getExportInfo().is()
96
0
                    ?  PropertySetMerger_CreateInstance( getExportInfo(),
97
0
                                                      xConvPropSet )
98
0
                    : getExportInfo();
99
100
0
            uno::Sequence< uno::Any > aArgs{ uno::Any(xDocHandler), uno::Any(xPropSet),
101
0
                                             uno::Any(GetModel()) };
102
103
            // get filter component
104
0
            xDocHandler.set(
105
0
                xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
106
0
                    u"com.sun.star.comp.Oasis2OOoTransformer"_ustr, aArgs, xContext),
107
0
                uno::UNO_QUERY_THROW );
108
109
0
            SetDocHandler( xDocHandler );
110
0
        }
111
0
        catch( css::uno::Exception& )
112
0
        {
113
0
            OSL_FAIL( "Cannot instantiate com.sun.star.comp.Oasis2OOoTransformer!");
114
0
        }
115
0
    }
116
117
118
0
    xDocHandler->startDocument();
119
120
0
    addChaffWhenEncryptedStorage();
121
122
0
    {
123
124
0
        const SvXMLNamespaceMap& rMap = GetNamespaceMap();
125
0
        sal_uInt16 nPos = rMap.GetFirstKey();
126
0
        while( USHRT_MAX != nPos )
127
0
        {
128
0
            GetAttrList().AddAttribute( rMap.GetAttrNameByKey( nPos ), rMap.GetNameByKey( nPos ) );
129
0
            nPos = GetNamespaceMap().GetNextKey( nPos );
130
0
        }
131
132
0
        const char*const pVersion = GetODFVersionAttributeValue();
133
134
0
        if( pVersion )
135
0
            AddAttribute( XML_NAMESPACE_OFFICE, XML_VERSION,
136
0
                            OUString::createFromAscii(pVersion) );
137
138
0
        SvXMLElementExport aDocElem( *this, XML_NAMESPACE_OFFICE, XML_DOCUMENT_META,
139
0
                    true, true );
140
141
        // NB: office:meta is now written by _ExportMeta
142
0
        ExportMeta_();
143
0
    }
144
0
    xDocHandler->endDocument();
145
0
    return ERRCODE_NONE;
146
0
}
147
148
void XMLMetaExportComponent::ExportMeta_()
149
0
{
150
0
    if (mxDocProps.is()) {
151
0
        OUString generator( ::utl::DocInfoHelper::GetGeneratorString() );
152
        // update generator here
153
0
        mxDocProps->setGenerator(generator);
154
0
        rtl::Reference<SvXMLMetaExport> pMeta = new SvXMLMetaExport(*this, mxDocProps);
155
0
        pMeta->Export();
156
0
    } else {
157
0
        SvXMLExport::ExportMeta_();
158
0
    }
159
0
}
160
161
// methods without content:
162
0
void XMLMetaExportComponent::ExportAutoStyles_() {}
163
0
void XMLMetaExportComponent::ExportMasterStyles_() {}
164
0
void XMLMetaExportComponent::ExportContent_() {}
165
166
extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
167
XMLMetaExportComponent_get_implementation(
168
    css::uno::XComponentContext *context,
169
    css::uno::Sequence<css::uno::Any> const &)
170
0
{
171
0
    return cppu::acquire(new XMLMetaExportComponent(context, u"XMLMetaExportComponent"_ustr, SvXMLExportFlags::META|SvXMLExportFlags::OASIS));
172
0
}
173
174
extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
175
XMLMetaExportOOo_get_implementation(css::uno::XComponentContext* context,
176
                                    css::uno::Sequence<css::uno::Any> const&)
177
0
{
178
0
    return cppu::acquire(
179
0
        new XMLMetaExportComponent(context, u"XMLMetaExportOOo"_ustr, SvXMLExportFlags::META));
180
0
}
181
182
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */