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