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, sal_uInt16 nPrefix, const OUString& rLocalName, const Reference< XAttributeList >& xAttrList, const Reference< XNameAccess >& xLayerManager );
57 
58  virtual SvXMLImportContextRef CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const Reference< XAttributeList >& xAttrList ) override;
59  virtual void EndElement() override;
60 
61 private:
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 
72 SdXMLLayerContext::SdXMLLayerContext( SvXMLImport& rImport, sal_uInt16 nPrefix, const OUString& rLocalName, const Reference< XAttributeList >& xAttrList, const Reference< XNameAccess >& xLayerManager )
73 : SvXMLImportContext(rImport, nPrefix, rLocalName)
74 , mxLayerManager( xLayerManager )
75 {
76  const sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
77  for(sal_Int16 i=0; i < nAttrCount; i++)
78  {
79  OUString aLocalName;
80  if( GetImport().GetNamespaceMap().GetKeyByAttrName( xAttrList->getNameByIndex( i ), &aLocalName ) == XML_NAMESPACE_DRAW )
81  {
82  const OUString sValue( xAttrList->getValueByIndex( i ) );
83 
84  if( IsXMLToken( aLocalName, XML_NAME ) )
85  {
86  msName = sValue;
87  }
88  else if ( IsXMLToken( aLocalName, XML_DISPLAY))
89  {
90  msDisplay = sValue;
91  }
92  else if ( IsXMLToken( aLocalName, XML_PROTECTED))
93  {
94  msProtected = sValue;
95  }
96  }
97  }
98 
99 }
100 
101 SvXMLImportContextRef SdXMLLayerContext::CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const Reference< XAttributeList >& )
102 {
103  if( (XML_NAMESPACE_SVG == nPrefix) && IsXMLToken(rLocalName, XML_TITLE) )
104  {
105  return new XMLStringBufferImportContext( GetImport(), nPrefix, rLocalName, sTitleBuffer);
106  }
107  else if( (XML_NAMESPACE_SVG == nPrefix) && IsXMLToken(rLocalName, XML_DESC) )
108  {
109  return new XMLStringBufferImportContext( GetImport(), nPrefix, rLocalName, sDescriptionBuffer);
110  }
111  return nullptr;
112 }
113 
114 void SdXMLLayerContext::EndElement()
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  {
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 
175 SdXMLLayerSetContext::SdXMLLayerSetContext( SvXMLImport& rImport, sal_uInt16 nPrfx, const OUString& rLocalName,
176  const css::uno::Reference< css::xml::sax::XAttributeList>&)
177 : SvXMLImportContext(rImport, nPrfx, rLocalName)
178 {
179  Reference< XLayerSupplier > xLayerSupplier( rImport.GetModel(), UNO_QUERY );
180  SAL_WARN_IF( !xLayerSupplier.is(), "xmloff", "xmloff::SdXMLLayerSetContext::SdXMLLayerSetContext(), XModel is not supporting XLayerSupplier!" );
181  if( xLayerSupplier.is() )
182  mxLayerManager = xLayerSupplier->getLayerManager();
183 }
184 
186 : SvXMLImportContext(rImport)
187 {
188  Reference< XLayerSupplier > xLayerSupplier( rImport.GetModel(), UNO_QUERY );
189  SAL_WARN_IF( !xLayerSupplier.is(), "xmloff", "xmloff::SdXMLLayerSetContext::SdXMLLayerSetContext(), XModel is not supporting XLayerSupplier!" );
190  if( xLayerSupplier.is() )
191  mxLayerManager = xLayerSupplier->getLayerManager();
192 }
193 
195 {
196 }
197 
198 SvXMLImportContextRef SdXMLLayerSetContext::CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName,
199  const css::uno::Reference< css::xml::sax::XAttributeList>& xAttrList )
200 {
201  return new SdXMLLayerContext( GetImport(), nPrefix, rLocalName, xAttrList, mxLayerManager );
202 }
203 
204 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool IsXMLToken(const OUString &rString, enum XMLTokenEnum eToken)
compare eToken to the string
Definition: xmltoken.cxx:3434
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:62
OUString msName
css::uno::Reference< css::container::XNameAccess > mxLayerManager
Definition: layerimp.hxx:31
constexpr sal_uInt16 XML_NAMESPACE_DRAW
constexpr sal_uInt16 XML_NAMESPACE_SVG
#define TOOLS_WARN_EXCEPTION(area, stream)
int i
virtual ~SdXMLLayerSetContext() override
Definition: layerimp.cxx:194
Import all text into a string buffer.
This class deliberately does not support XWeak, to improve performance when loading large documents...
Definition: xmlictxt.hxx:44
#define SAL_WARN_IF(condition, area, stream)
Handling of tokens in XML:
const css::uno::Reference< css::frame::XModel > & GetModel() const
Definition: xmlimp.hxx:408
SdXMLLayerSetContext(SvXMLImport &rImport, sal_uInt16 nPrfx, const OUString &rLocalName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList)
Definition: layerimp.cxx:175
virtual SvXMLImportContextRef CreateChildContext(sal_uInt16 nPrefix, const OUString &rLocalName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList) override
Create a children element context.
Definition: layerimp.cxx:198