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