LibreOffice Module xmloff (master) 1
layerimp.cxx
Go to the documentation of this file.
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>
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/xml/sax/XAttributeList.hpp>
28#include <com/sun/star/drawing/XLayerSupplier.hpp>
29#include <xmloff/xmltoken.hxx>
30#include <xmloff/xmlimp.hxx>
33#include "layerimp.hxx"
34
35
37
38using namespace ::std;
39using namespace ::cppu;
40using namespace ::xmloff::token;
41using namespace ::com::sun::star;
42using namespace ::com::sun::star::xml;
43using namespace ::com::sun::star::xml::sax;
44using namespace ::com::sun::star::uno;
45using namespace ::com::sun::star::drawing;
46using namespace ::com::sun::star::beans;
47using namespace ::com::sun::star::lang;
48using namespace ::com::sun::star::container;
50
51namespace {
52
53class SdXMLLayerContext : public SvXMLImportContext
54{
55public:
56 SdXMLLayerContext( SvXMLImport& rImport, const Reference< XFastAttributeList >& xAttrList, const Reference< XNameAccess >& xLayerManager );
57
58 virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(
59 sal_Int32 nElement,
60 const css::uno::Reference< css::xml::sax::XFastAttributeList >& AttrList ) override;
61 virtual void SAL_CALL endFastElement(sal_Int32 nElement) override;
62
63private:
64 css::uno::Reference< css::container::XNameAccess > mxLayerManager;
65 OUString msName;
66 OUStringBuffer sDescriptionBuffer;
67 OUStringBuffer sTitleBuffer;
68 OUString msDisplay;
69 OUString msProtected;
70};
71
72}
73
74SdXMLLayerContext::SdXMLLayerContext( SvXMLImport& rImport, const Reference< XFastAttributeList >& xAttrList, const Reference< XNameAccess >& xLayerManager )
75: SvXMLImportContext(rImport)
76, mxLayerManager( xLayerManager )
77{
78 for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
79 {
80 OUString sValue = aIter.toString();
81 switch(aIter.getToken())
82 {
83 case XML_ELEMENT(DRAW, XML_NAME):
84 msName = sValue;
85 break;
86 case XML_ELEMENT(DRAW, XML_DISPLAY):
87 msDisplay = sValue;
88 break;
89 case XML_ELEMENT(DRAW, XML_PROTECTED):
90 msProtected = sValue;
91 break;
92 default:
93 XMLOFF_WARN_UNKNOWN("xmloff", aIter);
94 }
95 }
96
97}
98
99css::uno::Reference< css::xml::sax::XFastContextHandler > SdXMLLayerContext::createFastChildContext(
100 sal_Int32 nElement,
101 const css::uno::Reference< css::xml::sax::XFastAttributeList >& )
102{
103 if( nElement == XML_ELEMENT(SVG, XML_TITLE) )
104 {
105 return new XMLStringBufferImportContext( GetImport(), sTitleBuffer);
106 }
107 else if( nElement == XML_ELEMENT(SVG, XML_DESC) )
108 {
109 return new XMLStringBufferImportContext( GetImport(), sDescriptionBuffer);
110 }
111 else
112 XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement);
113 return nullptr;
114}
115
116void SdXMLLayerContext::endFastElement(sal_Int32 )
117{
118 SAL_WARN_IF( msName.isEmpty(), "xmloff", "xmloff::SdXMLLayerContext::EndElement(), draw:layer element without draw:name!" );
119 if( msName.isEmpty() )
120 return;
121
122 try
123 {
124 Reference< XPropertySet > xLayer;
125
126 if( mxLayerManager->hasByName( msName ) )
127 {
128 mxLayerManager->getByName( msName ) >>= xLayer;
129 SAL_WARN_IF( !xLayer.is(), "xmloff", "xmloff::SdXMLLayerContext::EndElement(), failed to get existing XLayer!" );
130 }
131 else
132 {
133 Reference< XLayerManager > xLayerManager( mxLayerManager, UNO_QUERY );
134 if( xLayerManager.is() )
135 xLayer = xLayerManager->insertNewByIndex( xLayerManager->getCount() );
136 SAL_WARN_IF( !xLayer.is(), "xmloff", "xmloff::SdXMLLayerContext::EndElement(), failed to create new XLayer!" );
137
138 if( xLayer.is() )
139 xLayer->setPropertyValue("Name", Any( msName ) );
140 }
141
142 if( xLayer.is() )
143 {
144 xLayer->setPropertyValue("Title", Any( sTitleBuffer.makeStringAndClear() ) );
145 xLayer->setPropertyValue("Description", Any( sDescriptionBuffer.makeStringAndClear() ) );
146 bool bIsVisible( true );
147 bool bIsPrintable( true );
148 if ( !msDisplay.isEmpty() )
149 {
150 bIsVisible = (msDisplay == "always") || (msDisplay == "screen");
151 bIsPrintable = (msDisplay == "always") || (msDisplay == "printer");
152 }
153 xLayer->setPropertyValue("IsVisible", Any( bIsVisible ) );
154 xLayer->setPropertyValue("IsPrintable", Any( bIsPrintable ) );
155 bool bIsLocked( false );
156 if ( !msProtected.isEmpty() )
157 bIsLocked = (msProtected == "true");
158 xLayer->setPropertyValue("IsLocked", Any( bIsLocked ) );
159
160 // tdf#129898 repair layer "DrawnInSlideshow", which was wrongly written
161 // in LO 6.2 to 6.4. It should always have ODF defaults.
162 if (msName == "DrawnInSlideshow")
163 {
164 xLayer->setPropertyValue("IsVisible", Any(true));
165 xLayer->setPropertyValue("IsPrintable", Any(true));
166 xLayer->setPropertyValue("IsLocked", Any(false));
167 }
168 }
169 }
170 catch( Exception& )
171 {
172 TOOLS_WARN_EXCEPTION("xmloff.draw", "");
173 }
174}
175
176
178: SvXMLImportContext(rImport)
179{
180 Reference< XLayerSupplier > xLayerSupplier( rImport.GetModel(), UNO_QUERY );
181 SAL_WARN_IF( !xLayerSupplier.is(), "xmloff", "xmloff::SdXMLLayerSetContext::SdXMLLayerSetContext(), XModel is not supporting XLayerSupplier!" );
182 if( xLayerSupplier.is() )
183 mxLayerManager = xLayerSupplier->getLayerManager();
184}
185
187{
188}
189
190css::uno::Reference< css::xml::sax::XFastContextHandler > SdXMLLayerSetContext::createFastChildContext(
191 sal_Int32 /*nElement*/,
192 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
193{
194 return new SdXMLLayerContext( GetImport(), xAttrList, mxLayerManager );
195}
196
197/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SdXMLLayerSetContext(SvXMLImport &rImport)
Definition: layerimp.cxx:177
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &AttrList) override
Definition: layerimp.cxx:190
virtual ~SdXMLLayerSetContext() override
Definition: layerimp.cxx:186
css::uno::Reference< css::container::XNameAccess > mxLayerManager
Definition: layerimp.hxx:30
This class deliberately does not support XWeak, to improve performance when loading large documents.
Definition: xmlictxt.hxx:48
virtual void SAL_CALL endFastElement(sal_Int32 Element) override
endFastElement is called before a context will be destructed, but after an elements context has been ...
Definition: xmlictxt.cxx:40
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:60
virtual css::uno::Reference< XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > &Attribs) override
Definition: xmlictxt.cxx:59
Import all text into a string buffer.
#define TOOLS_WARN_EXCEPTION(area, stream)
#define SAL_WARN_IF(condition, area, stream)
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
Handling of tokens in XML:
bool IsXMLToken(std::u16string_view rString, enum XMLTokenEnum eToken)
compare eToken to the string
Definition: xmltoken.cxx:3583
OUString msName
#define XMLOFF_WARN_UNKNOWN_ELEMENT(area, token)
Definition: xmlictxt.hxx:120
#define XMLOFF_WARN_UNKNOWN(area, rIter)
Definition: xmlictxt.hxx:114
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:98