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>
23 #include <tools/diagnose_ex.h>
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>
31 #include <xmloff/xmlnamespace.hxx>
32 #include <xmloff/namespacemap.hxx>
33 #include "layerimp.hxx"
34 
35 
37 
38 using namespace ::std;
39 using namespace ::cppu;
40 using namespace ::xmloff::token;
41 using namespace ::com::sun::star;
42 using namespace ::com::sun::star::xml;
43 using namespace ::com::sun::star::xml::sax;
44 using namespace ::com::sun::star::uno;
45 using namespace ::com::sun::star::drawing;
46 using namespace ::com::sun::star::beans;
47 using namespace ::com::sun::star::lang;
48 using namespace ::com::sun::star::container;
50 
51 namespace {
52 
53 class SdXMLLayerContext : public SvXMLImportContext
54 {
55 public:
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 
63 private:
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 
74 SdXMLLayerContext::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 
99 css::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 
116 void 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  {
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 
190 css::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: */
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:56
OUString msName
bool IsXMLToken(std::u16string_view rString, enum XMLTokenEnum eToken)
compare eToken to the string
Definition: xmltoken.cxx:3462
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
css::uno::Reference< css::container::XNameAccess > mxLayerManager
Definition: layerimp.hxx:31
SdXMLLayerSetContext(SvXMLImport &rImport)
Definition: layerimp.cxx:177
#define XMLOFF_WARN_UNKNOWN(area, rIter)
Definition: xmlictxt.hxx:110
#define TOOLS_WARN_EXCEPTION(area, stream)
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
Import all text into a string buffer.
This class deliberately does not support XWeak, to improve performance when loading large documents...
Definition: xmlictxt.hxx:45
#define SAL_WARN_IF(condition, area, stream)
Handling of tokens in XML:
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:96
const css::uno::Reference< css::frame::XModel > & GetModel() const
Definition: xmlimp.hxx:399
#define XMLOFF_WARN_UNKNOWN_ELEMENT(area, token)
Definition: xmlictxt.hxx:116