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/xmlnmspe.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;
49 
50 
52  SvXMLImport& rImport,
53  sal_uInt16 nPrfx,
54  const OUString& rLName,
55  const uno::Reference< xml::sax::XAttributeList >& xAttrList,
56  SvXMLStylesContext& rStyles,
57  XmlStyleFamily nFamily)
58 : XMLPropStyleContext(rImport, nPrfx, rLName, xAttrList, rStyles, nFamily ),
59  m_bIsNumRuleAlreadyConverted( false )
60 {
61 }
62 
64 {
65 }
66 
67 void XMLShapeStyleContext::SetAttribute( sal_uInt16 nPrefixKey, const OUString& rLocalName, const OUString& rValue )
68 {
70  {
72  }
73  else if( (XML_NAMESPACE_STYLE == nPrefixKey) && IsXMLToken( rLocalName, ::xmloff::token::XML_LIST_STYLE_NAME ) )
74  {
76  }
77  else
78  {
79  XMLPropStyleContext::SetAttribute( nPrefixKey, rLocalName, rValue );
80 
81  if( (XML_NAMESPACE_STYLE == nPrefixKey) &&
82  ( IsXMLToken( rLocalName, ::xmloff::token::XML_NAME ) || IsXMLToken( rLocalName, ::xmloff::token::XML_DISPLAY_NAME ) ) )
83  {
84  if( !GetName().isEmpty() && !GetDisplayName().isEmpty() && GetName() != GetDisplayName() )
85  {
86  GetImport().
87  AddStyleDisplayName( GetFamily(), GetName(), GetDisplayName() );
88  }
89  }
90  }
91 }
92 
94  sal_uInt16 nPrefix,
95  const OUString& rLocalName,
96  const Reference< xml::sax::XAttributeList > & xAttrList )
97 {
98  SvXMLImportContextRef xContext;
99 
100  if( XML_NAMESPACE_STYLE == nPrefix || XML_NAMESPACE_LO_EXT == nPrefix )
101  {
102  sal_uInt32 nFamily = 0;
103  if( IsXMLToken( rLocalName, XML_TEXT_PROPERTIES ) )
104  nFamily = XML_TYPE_PROP_TEXT;
105  else if( IsXMLToken( rLocalName, XML_PARAGRAPH_PROPERTIES ) )
106  nFamily = XML_TYPE_PROP_PARAGRAPH;
107  else if( IsXMLToken( rLocalName, XML_GRAPHIC_PROPERTIES ) )
108  nFamily = XML_TYPE_PROP_GRAPHIC;
109  if( nFamily )
110  {
113  if( xImpPrMap.is() )
114  xContext = new XMLShapePropertySetContext( GetImport(), nPrefix,
115  rLocalName, xAttrList,
116  nFamily,
117  GetProperties(),
118  xImpPrMap );
119  }
120  }
121 
122  if (!xContext)
123  xContext = XMLPropStyleContext::CreateChildContext( nPrefix, rLocalName,
124  xAttrList );
125 
126  return xContext;
127 }
128 
130 {
132  {
134 
135  // for compatibility to beta files, search for CTF_SD_NUMBERINGRULES_NAME to
136  // import numbering rules from the style:properties element
137  const rtl::Reference< XMLPropertySetMapper >&rMapper = GetStyles()->GetImportPropertyMapper( GetFamily() )->getPropertySetMapper();
138 
139  ::std::vector< XMLPropertyState > &rProperties = GetProperties();
140  ::std::vector< XMLPropertyState >::iterator end( rProperties.end() );
141 
142  // first, look for the old format, where we had a text:list-style-name
143  // attribute in the style:properties element
144  auto property = std::find_if(rProperties.begin(), end, [&rMapper](XMLPropertyState& rProp) {
145  // find properties with context
146  return (rProp.mnIndex != -1) && (rMapper->GetEntryContextId( rProp.mnIndex ) == CTF_SD_NUMBERINGRULES_NAME); });
147 
148  // if we did not find an old list-style-name in the properties, and we need one
149  // because we got a style:list-style attribute in the style-style element
150  // we generate one
151  if( (property == end) && ( !m_sListStyleName.isEmpty() ) )
152  {
153  sal_Int32 nIndex = rMapper->FindEntryIndex( CTF_SD_NUMBERINGRULES_NAME );
154  SAL_WARN_IF( -1 == nIndex, "xmloff", "can't find numbering rules property entry, can't set numbering rule!" );
155 
156  XMLPropertyState aNewState( nIndex );
157  rProperties.push_back( aNewState );
158  end = rProperties.end();
159  property = end - 1;
160  }
161 
162  // so, if we have an old or a new list style name, we set its value to
163  // a numbering rule
164  if( property != end )
165  {
166  if( m_sListStyleName.isEmpty() )
167  {
168  property->maValue >>= m_sListStyleName;
169  }
170 
171  const SvxXMLListStyleContext *pListStyle = GetImport().GetTextImport()->FindAutoListStyle( m_sListStyleName );
172 
173  SAL_WARN_IF( !pListStyle, "xmloff", "list-style not found for shape style" );
174  if( pListStyle )
175  {
176  uno::Reference< container::XIndexReplace > xNumRule( SvxXMLListStyleContext::CreateNumRule( GetImport().GetModel() ) );
177  pListStyle->FillUnoNumRule(xNumRule);
178  property->maValue <<= xNumRule;
179  }
180  else
181  {
182  property->mnIndex = -1;
183  }
184  }
185  }
186 
187  struct ContextID_Index_Pair aContextIDs[] =
188  {
189  { CTF_DASHNAME , -1 },
190  { CTF_LINESTARTNAME , -1 },
191  { CTF_LINEENDNAME , -1 },
192  { CTF_FILLGRADIENTNAME, -1 },
193  { CTF_FILLTRANSNAME , -1 },
194  { CTF_FILLHATCHNAME , -1 },
195  { CTF_FILLBITMAPNAME , -1 },
200  { -1, -1 }
201  };
202  static const XmlStyleFamily aFamilies[] =
203  {
211  };
212 
215  SAL_WARN_IF( !xImpPrMap.is(), "xmloff", "There is the import prop mapper" );
216  if( xImpPrMap.is() )
217  xImpPrMap->FillPropertySet( GetProperties(), rPropSet, aContextIDs );
218 
219  Reference< XPropertySetInfo > xInfo;
220  // get property set mapper
221  rtl::Reference<XMLPropertySetMapper> xPropMapper( xImpPrMap->getPropertySetMapper() );
222 
223  for( sal_uInt16 i=0; aContextIDs[i].nContextID != -1; i++ )
224  {
225  sal_Int32 nIndex = aContextIDs[i].nIndex;
226  if( nIndex != -1 ) switch( aContextIDs[i].nContextID )
227  {
228  case CTF_DASHNAME:
229  case CTF_LINESTARTNAME:
230  case CTF_LINEENDNAME:
232  case CTF_FILLTRANSNAME:
233  case CTF_FILLHATCHNAME:
234  case CTF_FILLBITMAPNAME:
235  {
236  struct XMLPropertyState& rState = GetProperties()[nIndex];
237  OUString sStyleName;
238  rState.maValue >>= sStyleName;
239  sStyleName = GetImport().GetStyleDisplayName( aFamilies[i], sStyleName );
240  // All of these attributes refer to something with draw:name
241  // of type styleName = NCName which is non-empty.
242  // tdf#89802: for Writer frames there would be no exception here but
243  // it will fail later on attach() and take out the entire frame
244  if (sStyleName.isEmpty() &&
245  ( CTF_FILLGRADIENTNAME == aContextIDs[i].nContextID
246  || CTF_FILLTRANSNAME == aContextIDs[i].nContextID
247  || CTF_FILLHATCHNAME == aContextIDs[i].nContextID
248  || CTF_FILLBITMAPNAME == aContextIDs[i].nContextID))
249  {
250  Sequence<OUString> const seq{ sStyleName };
253  seq, "empty style name reference", nullptr );
254  break;
255  }
256 
257  try
258  {
259 
260  // set property
261  const OUString& rPropertyName = xPropMapper->GetEntryAPIName(rState.mnIndex);
262  if( !xInfo.is() )
263  xInfo = rPropSet->getPropertySetInfo();
264  if ( xInfo->hasPropertyByName( rPropertyName ) )
265  {
266  rPropSet->setPropertyValue( rPropertyName, Any( sStyleName ) );
267  }
268  }
269  catch ( const css::lang::IllegalArgumentException& e )
270  {
271  Sequence<OUString> aSeq { sStyleName };
274  aSeq, e.Message, nullptr );
275  }
276  break;
277  }
282  {
283  struct XMLPropertyState& rState = GetProperties()[nIndex];
284  const OUString& rPropertyName = xPropMapper->GetEntryAPIName(rState.mnIndex);
285  try
286  {
287  if( !xInfo.is() )
288  xInfo = rPropSet->getPropertySetInfo();
289  if ( xInfo->hasPropertyByName( rPropertyName ) )
290  {
291  rPropSet->setPropertyValue( rPropertyName, rState.maValue );
292  }
293  }
294  catch ( const css::lang::IllegalArgumentException& e )
295  {
299  aSeq, e.Message, nullptr );
300  }
301  break;
302  }
303  }
304  }
305 
306  if (!m_sControlDataStyleName.isEmpty())
307  { // we had a data-style-name attribute
308 
309  // set the formatting on the control model of the control shape
310  uno::Reference< drawing::XControlShape > xControlShape(rPropSet, uno::UNO_QUERY);
311  DBG_ASSERT(xControlShape.is(), "XMLShapeStyleContext::FillPropertySet: data style for a non-control shape!");
312  if (xControlShape.is())
313  {
314  uno::Reference< beans::XPropertySet > xControlModel(xControlShape->getControl(), uno::UNO_QUERY);
315  DBG_ASSERT(xControlModel.is(), "XMLShapeStyleContext::FillPropertySet: no control model for the shape!");
316  if (xControlModel.is())
317  {
318  GetImport().GetFormImport()->applyControlNumberStyle(xControlModel, m_sControlDataStyleName);
319  }
320  }
321  }
322 }
323 
324 void XMLShapeStyleContext::Finish( bool /*bOverwrite*/ )
325 {
326 }
327 
328 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int16 nContextID
Definition: xmlimppr.hxx:56
constexpr sal_uInt16 XML_NAMESPACE_LO_EXT
Definition: xmlnmspe.hxx:80
XmlStyleFamily GetFamily() const
Definition: xmlstyle.hxx:112
bool IsXMLToken(const OUString &rString, enum XMLTokenEnum eToken)
compare eToken to the string
Definition: xmltoken.cxx:3422
constexpr sal_uInt16 XML_NAMESPACE_STYLE
Definition: xmlnmspe.hxx:30
sal_Int32 nIndex
rtl::Reference< ::xmloff::OFormLayerXMLImport > const & GetFormImport()
Definition: xmlimp.hxx:618
virtual ~XMLShapeStyleContext() override
#define CTF_LINEENDNAME
Definition: xmlsdtypes.hxx:138
void FillUnoNumRule(const css::uno::Reference< css::container::XIndexReplace > &rNumRule) const
Definition: xmlnumi.cxx:1071
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:62
virtual void SetAttribute(sal_uInt16 nPrefixKey, const OUString &rLocalName, const OUString &rValue) override
Definition: prstylei.cxx:57
sal_Int32 mnIndex
Definition: maptype.hxx:106
#define CTF_FILLGRADIENTNAME
Definition: xmlsdtypes.hxx:139
#define CTF_FILLTRANSNAME
Definition: xmlsdtypes.hxx:142
rtl::Reference< XMLTextImportHelper > const & GetTextImport()
Definition: xmlimp.hxx:594
css::uno::Any const & rValue
Definition: ImageStyle.hxx:38
XmlStyleFamily
Definition: families.hxx:47
#define CTF_FILLBITMAPNAME
Definition: xmlsdtypes.hxx:141
#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:1215
#define CTF_SD_OLE_VIS_AREA_IMPORT_TOP
Definition: xmlsdtypes.hxx:170
#define CTF_SD_OLE_VIS_AREA_IMPORT_HEIGHT
Definition: xmlsdtypes.hxx:172
virtual rtl::Reference< SvXMLImportPropertyMapper > GetImportPropertyMapper(XmlStyleFamily nFamily) const
Definition: xmlstyle.cxx:580
#define XMLERROR_STYLE_PROP_VALUE
Definition: xmlerror.hxx:68
#define DBG_ASSERT(sCon, aError)
const OUString & GetDisplayName() const
Definition: xmlstyle.hxx:107
int i
#define CTF_SD_OLE_VIS_AREA_IMPORT_WIDTH
Definition: xmlsdtypes.hxx:171
::std::vector< XMLPropertyState > & GetProperties()
Definition: prstylei.hxx:79
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: prstylei.cxx:178
#define XML_TYPE_PROP_TEXT
Definition: xmltypes.hxx:98
#define CTF_FILLHATCHNAME
Definition: xmlsdtypes.hxx:140
SvXMLImportContextRef CreateChildContext(sal_uInt16 nPrefix, const OUString &rLocalName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList) override
Create a children element context.
enumrange< T >::Iterator end(enumrange< T >)
#define CTF_LINESTARTNAME
Definition: xmlsdtypes.hxx:137
virtual void SetAttribute(sal_uInt16 nPrefixKey, const OUString &rLocalName, const OUString &rValue) override
virtual void Finish(bool bOverwrite) override
css::uno::Any maValue
Definition: maptype.hxx:107
OUString GetStyleDisplayName(XmlStyleFamily nFamily, const OUString &rName) const
Definition: xmlimp.cxx:1491
#define SAL_WARN_IF(condition, area, stream)
const OUString & GetName() const
Definition: xmlstyle.hxx:106
const OUString & GetXMLToken(enum XMLTokenEnum eToken)
return the OUString representation for eToken
Definition: xmltoken.cxx:3366
#define XML_TYPE_PROP_GRAPHIC
Definition: xmltypes.hxx:94
#define CTF_DASHNAME
Definition: xmlsdtypes.hxx:136
#define CTF_SD_OLE_VIS_AREA_IMPORT_LEFT
Definition: xmlsdtypes.hxx:169
Smart struct to transport an Any with an index to the appropriate property-name.
Definition: maptype.hxx:104
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:54
virtual void FillPropertySet(const css::uno::Reference< css::beans::XPropertySet > &rPropSet) override
XMLShapeStyleContext(SvXMLImport &rImport, sal_uInt16 nPrfx, const OUString &rLName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList, SvXMLStylesContext &rStyles, XmlStyleFamily nFamily)
#define CTF_SD_NUMBERINGRULES_NAME
Definition: contextid.hxx:36