Coverage Report

Created: 2026-02-14 09:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/xmloff/source/draw/layerimp.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 <sal/config.h>
21
22
#include <sal/log.hxx>
23
#include <comphelper/diagnose_ex.hxx>
24
#include <com/sun/star/drawing/XLayerManager.hpp>
25
#include <com/sun/star/beans/XPropertySet.hpp>
26
#include <com/sun/star/frame/XModel.hpp>
27
#include <com/sun/star/drawing/XLayerSupplier.hpp>
28
#include <xmloff/xmltoken.hxx>
29
#include <xmloff/xmlimp.hxx>
30
#include <xmloff/xmlnamespace.hxx>
31
#include "layerimp.hxx"
32
33
34
#include <XMLStringBufferImportContext.hxx>
35
36
using namespace ::xmloff::token;
37
using namespace ::com::sun::star;
38
using namespace ::com::sun::star::xml;
39
using namespace ::com::sun::star::xml::sax;
40
using namespace ::com::sun::star::uno;
41
using namespace ::com::sun::star::drawing;
42
using namespace ::com::sun::star::beans;
43
using namespace ::com::sun::star::container;
44
45
namespace {
46
47
class SdXMLLayerContext : public SvXMLImportContext
48
{
49
public:
50
    SdXMLLayerContext( SvXMLImport& rImport, const Reference< XFastAttributeList >& xAttrList, const Reference< XNameAccess >& xLayerManager );
51
52
    virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(
53
        sal_Int32 nElement,
54
        const css::uno::Reference< css::xml::sax::XFastAttributeList >& AttrList ) override;
55
    virtual void SAL_CALL endFastElement(sal_Int32 nElement) override;
56
57
private:
58
    css::uno::Reference< css::container::XNameAccess > mxLayerManager;
59
    OUString msName;
60
    OUStringBuffer sDescriptionBuffer;
61
    OUStringBuffer sTitleBuffer;
62
    OUString msDisplay;
63
    OUString msProtected;
64
};
65
66
}
67
68
SdXMLLayerContext::SdXMLLayerContext( SvXMLImport& rImport, const Reference< XFastAttributeList >& xAttrList, const Reference< XNameAccess >& xLayerManager )
69
1.48k
: SvXMLImportContext(rImport)
70
1.48k
, mxLayerManager( xLayerManager )
71
1.48k
{
72
1.48k
    for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
73
1.50k
    {
74
1.50k
        OUString sValue = aIter.toString();
75
1.50k
        switch(aIter.getToken())
76
1.50k
        {
77
1.46k
            case XML_ELEMENT(DRAW, XML_NAME):
78
1.46k
                msName = sValue;
79
1.46k
                break;
80
0
            case XML_ELEMENT(DRAW, XML_DISPLAY):
81
0
                msDisplay = sValue;
82
0
                break;
83
0
            case XML_ELEMENT(DRAW, XML_PROTECTED):
84
0
                msProtected = sValue;
85
0
                break;
86
40
            default:
87
40
                XMLOFF_WARN_UNKNOWN("xmloff", aIter);
88
1.50k
        }
89
1.50k
    }
90
91
1.48k
}
92
93
css::uno::Reference< css::xml::sax::XFastContextHandler > SdXMLLayerContext::createFastChildContext(
94
    sal_Int32 nElement,
95
    const css::uno::Reference< css::xml::sax::XFastAttributeList >&  )
96
31
{
97
31
    if( nElement == XML_ELEMENT(SVG, XML_TITLE) || nElement == XML_ELEMENT(SVG_COMPAT, XML_TITLE) )
98
0
    {
99
0
        return new XMLStringBufferImportContext( GetImport(), sTitleBuffer);
100
0
    }
101
31
    else if( nElement == XML_ELEMENT(SVG, XML_DESC) || nElement == XML_ELEMENT(SVG_COMPAT, XML_DESC) )
102
0
    {
103
0
        return new XMLStringBufferImportContext( GetImport(), sDescriptionBuffer);
104
0
    }
105
31
    else
106
31
        XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement);
107
31
    return nullptr;
108
31
}
109
110
void SdXMLLayerContext::endFastElement(sal_Int32 )
111
1.47k
{
112
1.47k
    SAL_WARN_IF( msName.isEmpty(), "xmloff", "xmloff::SdXMLLayerContext::EndElement(), draw:layer element without draw:name!" );
113
1.47k
    if( msName.isEmpty() )
114
13
        return;
115
116
1.46k
    try
117
1.46k
    {
118
1.46k
        Reference< XPropertySet > xLayer;
119
120
1.46k
        if( mxLayerManager->hasByName( msName ) )
121
1.37k
        {
122
1.37k
            mxLayerManager->getByName( msName ) >>= xLayer;
123
1.37k
            SAL_WARN_IF( !xLayer.is(), "xmloff", "xmloff::SdXMLLayerContext::EndElement(), failed to get existing XLayer!" );
124
1.37k
        }
125
90
        else
126
90
        {
127
90
            Reference< XLayerManager > xLayerManager( mxLayerManager, UNO_QUERY );
128
90
            if( xLayerManager.is() )
129
90
                xLayer = xLayerManager->insertNewByIndex( xLayerManager->getCount() );
130
90
            SAL_WARN_IF( !xLayer.is(), "xmloff", "xmloff::SdXMLLayerContext::EndElement(), failed to create new XLayer!" );
131
132
90
            if( xLayer.is() )
133
90
                xLayer->setPropertyValue(u"Name"_ustr, Any( msName ) );
134
90
        }
135
136
1.46k
        if( xLayer.is() )
137
1.46k
        {
138
1.46k
            xLayer->setPropertyValue(u"Title"_ustr, Any( sTitleBuffer.makeStringAndClear() ) );
139
1.46k
            xLayer->setPropertyValue(u"Description"_ustr, Any( sDescriptionBuffer.makeStringAndClear() ) );
140
1.46k
            bool bIsVisible( true );
141
1.46k
            bool bIsPrintable( true );
142
1.46k
            if ( !msDisplay.isEmpty() )
143
0
            {
144
0
                bIsVisible = (msDisplay == "always") || (msDisplay == "screen");
145
0
                bIsPrintable = (msDisplay == "always") || (msDisplay == "printer");
146
0
            }
147
1.46k
            xLayer->setPropertyValue(u"IsVisible"_ustr, Any( bIsVisible ) );
148
1.46k
            xLayer->setPropertyValue(u"IsPrintable"_ustr, Any( bIsPrintable ) );
149
1.46k
            bool bIsLocked( false );
150
1.46k
            if ( !msProtected.isEmpty() )
151
0
                bIsLocked = (msProtected == "true");
152
1.46k
            xLayer->setPropertyValue(u"IsLocked"_ustr, Any( bIsLocked ) );
153
154
            // tdf#129898 repair layer "DrawnInSlideshow", which was wrongly written
155
            // in LO 6.2 to 6.4. It should always have ODF defaults.
156
1.46k
            if (msName == "DrawnInSlideshow")
157
0
            {
158
0
                xLayer->setPropertyValue(u"IsVisible"_ustr, Any(true));
159
0
                xLayer->setPropertyValue(u"IsPrintable"_ustr, Any(true));
160
0
                xLayer->setPropertyValue(u"IsLocked"_ustr, Any(false));
161
0
            }
162
1.46k
        }
163
1.46k
    }
164
1.46k
    catch( Exception& )
165
1.46k
    {
166
0
        TOOLS_WARN_EXCEPTION("xmloff.draw", "");
167
0
    }
168
1.46k
}
169
170
171
SdXMLLayerSetContext::SdXMLLayerSetContext( SvXMLImport& rImport )
172
303
: SvXMLImportContext(rImport)
173
303
{
174
303
    Reference< XLayerSupplier > xLayerSupplier( rImport.GetModel(), UNO_QUERY );
175
303
    SAL_WARN_IF( !xLayerSupplier.is(), "xmloff", "xmloff::SdXMLLayerSetContext::SdXMLLayerSetContext(), XModel is not supporting XLayerSupplier!" );
176
303
    if( xLayerSupplier.is() )
177
303
        mxLayerManager = xLayerSupplier->getLayerManager();
178
303
}
179
180
SdXMLLayerSetContext::~SdXMLLayerSetContext()
181
303
{
182
303
}
183
184
css::uno::Reference< css::xml::sax::XFastContextHandler > SdXMLLayerSetContext::createFastChildContext(
185
    sal_Int32 /*nElement*/,
186
    const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
187
1.48k
{
188
1.48k
    return new SdXMLLayerContext( GetImport(), xAttrList, mxLayerManager );
189
1.48k
}
190
191
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */