LibreOffice Module xmloff (master)  1
XMLShapeStyleContext.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 <tools/debug.hxx>
21 #include <sal/log.hxx>
24 #include <xmloff/contextid.hxx>
25 #include <com/sun/star/drawing/XControlShape.hpp>
26 #include <com/sun/star/beans/XPropertySet.hpp>
27 #include <com/sun/star/beans/XPropertySetInfo.hpp>
28 #include <com/sun/star/lang/IllegalArgumentException.hpp>
29 #include <com/sun/star/container/XIndexReplace.hpp>
30 #include <xmloff/xmlimp.hxx>
31 #include <xmloff/xmlnumi.hxx>
32 #include <xmloff/xmlnamespace.hxx>
33 #include <xmloff/xmlprmap.hxx>
34 #include <xmloff/xmltoken.hxx>
35 #include <xmloff/xmlerror.hxx>
36 #include <xmloff/maptype.hxx>
37 #include <xmloff/xmlimppr.hxx>
38 
39 #include <xmlsdtypes.hxx>
40 
41 using namespace ::com::sun::star;
42 using namespace ::com::sun::star::uno;
43 using namespace ::com::sun::star::beans;
44 using namespace ::com::sun::star::drawing;
45 using namespace ::xmloff::token;
46 
47 
49  SvXMLImport& rImport,
50  SvXMLStylesContext& rStyles,
51  XmlStyleFamily nFamily)
52 : XMLPropStyleContext(rImport, rStyles, nFamily ),
53  m_bIsNumRuleAlreadyConverted( false )
54 {
55 }
56 
58 {
59 }
60 
61 void XMLShapeStyleContext::SetAttribute( sal_Int32 nElement, const OUString& rValue )
62 {
63  if (m_sControlDataStyleName.isEmpty() && (nElement & TOKEN_MASK) == XML_DATA_STYLE_NAME)
64  {
66  }
67  else if( nElement == XML_ELEMENT(STYLE, XML_LIST_STYLE_NAME) )
68  {
70  }
71  else
72  {
73  XMLPropStyleContext::SetAttribute( nElement, rValue );
74 
75  if( nElement == XML_ELEMENT(STYLE, XML_NAME) || nElement == XML_ELEMENT(STYLE, XML_DISPLAY_NAME) )
76  {
77  if( !GetName().isEmpty() && !GetDisplayName().isEmpty() && GetName() != GetDisplayName() )
78  {
79  GetImport().
80  AddStyleDisplayName( GetFamily(), GetName(), GetDisplayName() );
81  }
82  }
83  }
84 }
85 
86 css::uno::Reference< css::xml::sax::XFastContextHandler > XMLShapeStyleContext::createFastChildContext(
87  sal_Int32 nElement,
88  const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
89 {
90  SvXMLImportContextRef xContext;
91 
92  if( IsTokenInNamespace(nElement, XML_NAMESPACE_STYLE) ||
94  {
95  sal_Int32 nLocalName = nElement & TOKEN_MASK;
96  sal_uInt32 nFamily = 0;
97  if( nLocalName == XML_TEXT_PROPERTIES )
98  nFamily = XML_TYPE_PROP_TEXT;
99  else if( nLocalName == XML_PARAGRAPH_PROPERTIES )
100  nFamily = XML_TYPE_PROP_PARAGRAPH;
101  else if( nLocalName == XML_GRAPHIC_PROPERTIES )
102  nFamily = XML_TYPE_PROP_GRAPHIC;
103  if( nFamily )
104  {
107  if( xImpPrMap.is() )
108  return new XMLShapePropertySetContext( GetImport(), nElement, xAttrList,
109  nFamily,
110  GetProperties(),
111  xImpPrMap );
112  }
113  }
114 
115  return XMLPropStyleContext::createFastChildContext( nElement, xAttrList );
116 }
117 
119 {
121  {
123 
124  // for compatibility to beta files, search for CTF_SD_NUMBERINGRULES_NAME to
125  // import numbering rules from the style:properties element
126  const rtl::Reference< XMLPropertySetMapper >&rMapper = GetStyles()->GetImportPropertyMapper( GetFamily() )->getPropertySetMapper();
127 
128  ::std::vector< XMLPropertyState > &rProperties = GetProperties();
129  ::std::vector< XMLPropertyState >::iterator end( rProperties.end() );
130 
131  // first, look for the old format, where we had a text:list-style-name
132  // attribute in the style:properties element
133  auto property = std::find_if(rProperties.begin(), end, [&rMapper](XMLPropertyState& rProp) {
134  // find properties with context
135  return (rProp.mnIndex != -1) && (rMapper->GetEntryContextId( rProp.mnIndex ) == CTF_SD_NUMBERINGRULES_NAME); });
136 
137  // if we did not find an old list-style-name in the properties, and we need one
138  // because we got a style:list-style attribute in the style-style element
139  // we generate one
140  if( (property == end) && ( !m_sListStyleName.isEmpty() ) )
141  {
142  sal_Int32 nIndex = rMapper->FindEntryIndex( CTF_SD_NUMBERINGRULES_NAME );
143  SAL_WARN_IF( -1 == nIndex, "xmloff", "can't find numbering rules property entry, can't set numbering rule!" );
144 
145  XMLPropertyState aNewState( nIndex );
146  rProperties.push_back( aNewState );
147  end = rProperties.end();
148  property = end - 1;
149  }
150 
151  // so, if we have an old or a new list style name, we set its value to
152  // a numbering rule
153  if( property != end )
154  {
155  if( m_sListStyleName.isEmpty() )
156  {
157  property->maValue >>= m_sListStyleName;
158  }
159 
160  const SvxXMLListStyleContext *pListStyle = GetImport().GetTextImport()->FindAutoListStyle( m_sListStyleName );
161 
162  SAL_WARN_IF( !pListStyle, "xmloff", "list-style not found for shape style" );
163  if( pListStyle )
164  {
165  uno::Reference< container::XIndexReplace > xNumRule( SvxXMLListStyleContext::CreateNumRule( GetImport().GetModel() ) );
166  pListStyle->FillUnoNumRule(xNumRule);
167  property->maValue <<= xNumRule;
168  }
169  else
170  {
171  property->mnIndex = -1;
172  }
173  }
174  }
175 
176  struct ContextID_Index_Pair aContextIDs[] =
177  {
178  { CTF_DASHNAME , -1 },
179  { CTF_LINESTARTNAME , -1 },
180  { CTF_LINEENDNAME , -1 },
181  { CTF_FILLGRADIENTNAME, -1 },
182  { CTF_FILLTRANSNAME , -1 },
183  { CTF_FILLHATCHNAME , -1 },
184  { CTF_FILLBITMAPNAME , -1 },
189  { -1, -1 }
190  };
191  static const XmlStyleFamily aFamilies[] =
192  {
200  };
201 
204  SAL_WARN_IF( !xImpPrMap.is(), "xmloff", "There is the import prop mapper" );
205  if( xImpPrMap.is() )
206  xImpPrMap->FillPropertySet( GetProperties(), rPropSet, aContextIDs );
207 
208  Reference< XPropertySetInfo > xInfo;
209  // get property set mapper
210  rtl::Reference<XMLPropertySetMapper> xPropMapper( xImpPrMap->getPropertySetMapper() );
211 
212  for( sal_uInt16 i=0; aContextIDs[i].nContextID != -1; i++ )
213  {
214  sal_Int32 nIndex = aContextIDs[i].nIndex;
215  if( nIndex != -1 ) switch( aContextIDs[i].nContextID )
216  {
217  case CTF_DASHNAME:
218  case CTF_LINESTARTNAME:
219  case CTF_LINEENDNAME:
221  case CTF_FILLTRANSNAME:
222  case CTF_FILLHATCHNAME:
223  case CTF_FILLBITMAPNAME:
224  {
225  struct XMLPropertyState& rState = GetProperties()[nIndex];
226  OUString sStyleName;
227  rState.maValue >>= sStyleName;
228  sStyleName = GetImport().GetStyleDisplayName( aFamilies[i], sStyleName );
229  // All of these attributes refer to something with draw:name
230  // of type styleName = NCName which is non-empty.
231  // tdf#89802: for Writer frames there would be no exception here but
232  // it will fail later on attach() and take out the entire frame
233  if (sStyleName.isEmpty() &&
234  ( CTF_FILLGRADIENTNAME == aContextIDs[i].nContextID
235  || CTF_FILLTRANSNAME == aContextIDs[i].nContextID
236  || CTF_FILLHATCHNAME == aContextIDs[i].nContextID
237  || CTF_FILLBITMAPNAME == aContextIDs[i].nContextID))
238  {
239  Sequence<OUString> const seq{ sStyleName };
242  seq, "empty style name reference", nullptr );
243  break;
244  }
245 
246  try
247  {
248 
249  // set property
250  const OUString& rPropertyName = xPropMapper->GetEntryAPIName(rState.mnIndex);
251  if( !xInfo.is() )
252  xInfo = rPropSet->getPropertySetInfo();
253  if ( xInfo->hasPropertyByName( rPropertyName ) )
254  {
255  rPropSet->setPropertyValue( rPropertyName, Any( sStyleName ) );
256  }
257  }
258  catch ( const css::lang::IllegalArgumentException& e )
259  {
260  Sequence<OUString> aSeq { sStyleName };
263  aSeq, e.Message, nullptr );
264  }
265  break;
266  }
271  {
272  struct XMLPropertyState& rState = GetProperties()[nIndex];
273  const OUString& rPropertyName = xPropMapper->GetEntryAPIName(rState.mnIndex);
274  try
275  {
276  if( !xInfo.is() )
277  xInfo = rPropSet->getPropertySetInfo();
278  if ( xInfo->hasPropertyByName( rPropertyName ) )
279  {
280  rPropSet->setPropertyValue( rPropertyName, rState.maValue );
281  }
282  }
283  catch ( const css::lang::IllegalArgumentException& e )
284  {
288  aSeq, e.Message, nullptr );
289  }
290  break;
291  }
292  }
293  }
294 
295  if (m_sControlDataStyleName.isEmpty())
296  return;
297 
298  // we had a data-style-name attribute
299 
300  // set the formatting on the control model of the control shape
301  uno::Reference< drawing::XControlShape > xControlShape(rPropSet, uno::UNO_QUERY);
302  DBG_ASSERT(xControlShape.is(), "XMLShapeStyleContext::FillPropertySet: data style for a non-control shape!");
303  if (xControlShape.is())
304  {
305  uno::Reference< beans::XPropertySet > xControlModel(xControlShape->getControl(), uno::UNO_QUERY);
306  DBG_ASSERT(xControlModel.is(), "XMLShapeStyleContext::FillPropertySet: no control model for the shape!");
307  if (xControlModel.is())
308  {
309  GetImport().GetFormImport()->applyControlNumberStyle(xControlModel, m_sControlDataStyleName);
310  }
311  }
312 }
313 
314 void XMLShapeStyleContext::Finish( bool /*bOverwrite*/ )
315 {
316 }
317 
318 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int16 nContextID
Definition: xmlimppr.hxx:58
XmlStyleFamily GetFamily() const
Definition: xmlstyle.hxx:83
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: prstylei.cxx:175
sal_Int32 nIndex
rtl::Reference< ::xmloff::OFormLayerXMLImport > const & GetFormImport()
Definition: xmlimp.hxx:623
virtual ~XMLShapeStyleContext() override
#define CTF_LINEENDNAME
Definition: xmlsdtypes.hxx:136
void FillUnoNumRule(const css::uno::Reference< css::container::XIndexReplace > &rNumRule) const
Definition: xmlnumi.cxx:891
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:59
sal_Int32 mnIndex
Definition: maptype.hxx:124
constexpr sal_uInt16 XML_NAMESPACE_LO_EXT
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &AttrList) override
#define CTF_FILLGRADIENTNAME
Definition: xmlsdtypes.hxx:137
#define CTF_FILLTRANSNAME
Definition: xmlsdtypes.hxx:140
rtl::Reference< XMLTextImportHelper > const & GetTextImport()
Definition: xmlimp.hxx:599
css::uno::Any const & rValue
Definition: ImageStyle.hxx:38
XmlStyleFamily
Definition: families.hxx:47
#define CTF_FILLBITMAPNAME
Definition: xmlsdtypes.hxx:139
#define XML_TYPE_PROP_PARAGRAPH
Definition: xmltypes.hxx:99
#define XMLERROR_FLAG_WARNING
Definition: xmlerror.hxx:35
static css::uno::Reference< css::container::XIndexReplace > CreateNumRule(const css::uno::Reference< css::frame::XModel > &rModel)
Definition: xmlnumi.cxx:1035
#define CTF_SD_OLE_VIS_AREA_IMPORT_TOP
Definition: xmlsdtypes.hxx:168
#define CTF_SD_OLE_VIS_AREA_IMPORT_HEIGHT
Definition: xmlsdtypes.hxx:170
virtual rtl::Reference< SvXMLImportPropertyMapper > GetImportPropertyMapper(XmlStyleFamily nFamily) const
Definition: xmlstyle.cxx:510
#define XMLERROR_STYLE_PROP_VALUE
Definition: xmlerror.hxx:68
#define DBG_ASSERT(sCon, aError)
const OUString & GetDisplayName() const
Definition: xmlstyle.hxx:78
int i
#define CTF_SD_OLE_VIS_AREA_IMPORT_WIDTH
Definition: xmlsdtypes.hxx:169
virtual void SetAttribute(sal_Int32 nElement, const OUString &rValue) override
::std::vector< XMLPropertyState > & GetProperties()
Definition: prstylei.hxx:79
#define XML_TYPE_PROP_TEXT
Definition: xmltypes.hxx:98
#define CTF_FILLHATCHNAME
Definition: xmlsdtypes.hxx:138
enumrange< T >::Iterator end(enumrange< T >)
#define CTF_LINESTARTNAME
Definition: xmlsdtypes.hxx:135
virtual void Finish(bool bOverwrite) override
css::uno::Any maValue
Definition: maptype.hxx:125
constexpr bool IsTokenInNamespace(sal_Int32 nToken, sal_uInt16 nNamespacePrefix)
Definition: xmlimp.hxx:103
OUString GetStyleDisplayName(XmlStyleFamily nFamily, const OUString &rName) const
Definition: xmlimp.cxx:1410
#define SAL_WARN_IF(condition, area, stream)
const OUString & GetName() const
Definition: xmlstyle.hxx:77
virtual void SetAttribute(sal_Int32 nElement, const OUString &rValue) override
Definition: prstylei.cxx:57
Handling of tokens in XML:
#define XML_TYPE_PROP_GRAPHIC
Definition: xmltypes.hxx:94
#define CTF_DASHNAME
Definition: xmlsdtypes.hxx:134
XMLShapeStyleContext(SvXMLImport &rImport, SvXMLStylesContext &rStyles, XmlStyleFamily nFamily)
#define CTF_SD_OLE_VIS_AREA_IMPORT_LEFT
Definition: xmlsdtypes.hxx:167
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:96
Smart struct to transport an Any with an index to the appropriate property-name.
Definition: maptype.hxx:122
void SetError(sal_Int32 nId, const css::uno::Sequence< OUString > &rMsgParams, const OUString &rExceptionMessage, const css::uno::Reference< css::xml::sax::XLocator > &rLocator)
Record an error condition that occurred during import.
Sequence< sal_Int8 > aSeq
SvXMLStylesContext * GetStyles()
Definition: prstylei.hxx:78
This struct is used as an optional parameter to the static _FillPropertySet() methods.
Definition: xmlimppr.hxx:56
constexpr sal_Int32 TOKEN_MASK
Definition: xmlimp.hxx:93
virtual void FillPropertySet(const css::uno::Reference< css::beans::XPropertySet > &rPropSet) override
constexpr sal_uInt16 XML_NAMESPACE_STYLE
#define CTF_SD_NUMBERINGRULES_NAME
Definition: contextid.hxx:36