1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <tools/debug.hxx>
21 #include <xmloff/XMLShapeStyleContext.hxx>
22 #include "XMLShapePropertySetContext.hxx"
23 #include <xmloff/contextid.hxx>
24 #include <com/sun/star/drawing/XControlShape.hpp>
25 #include <com/sun/star/beans/XPropertySetInfo.hpp>
26 #include <com/sun/star/lang/IllegalArgumentException.hpp>
27 #include <com/sun/star/drawing/FillStyle.hpp>
28 #include <xmloff/xmlimp.hxx>
29 #include <xmloff/xmlnumi.hxx>
30 #include <xmloff/xmlnmspe.hxx>
31 #include <xmloff/xmltoken.hxx>
32 #include <xmloff/xmlerror.hxx>
33 #include <xmloff/maptype.hxx>
35 #include "sdpropls.hxx"
37 using namespace ::com::sun::star
;
38 using namespace ::com::sun::star::uno
;
39 using namespace ::com::sun::star::beans
;
40 using namespace ::com::sun::star::drawing
;
41 using ::xmloff::token::IsXMLToken
;
42 using ::xmloff::token::XML_TEXT_PROPERTIES
;
43 using ::xmloff::token::XML_GRAPHIC_PROPERTIES
;
44 using ::xmloff::token::XML_PARAGRAPH_PROPERTIES
;
46 TYPEINIT1( XMLShapeStyleContext
, XMLPropStyleContext
);
48 XMLShapeStyleContext::XMLShapeStyleContext(
51 const OUString
& rLName
,
52 const uno::Reference
< xml::sax::XAttributeList
>& xAttrList
,
53 SvXMLStylesContext
& rStyles
,
55 : XMLPropStyleContext(rImport
, nPrfx
, rLName
, xAttrList
, rStyles
, nFamily
),
56 m_bIsNumRuleAlreadyConverted( false )
60 XMLShapeStyleContext::~XMLShapeStyleContext()
64 void XMLShapeStyleContext::SetAttribute( sal_uInt16 nPrefixKey
, const OUString
& rLocalName
, const OUString
& rValue
)
66 if (m_sControlDataStyleName
.isEmpty() && (::xmloff::token::GetXMLToken(::xmloff::token::XML_DATA_STYLE_NAME
) == rLocalName
))
68 m_sControlDataStyleName
= rValue
;
70 else if( (XML_NAMESPACE_STYLE
== nPrefixKey
) && IsXMLToken( rLocalName
, ::xmloff::token::XML_LIST_STYLE_NAME
) )
72 m_sListStyleName
= rValue
;
76 XMLPropStyleContext::SetAttribute( nPrefixKey
, rLocalName
, rValue
);
78 if( (XML_NAMESPACE_STYLE
== nPrefixKey
) &&
79 ( IsXMLToken( rLocalName
, ::xmloff::token::XML_NAME
) || IsXMLToken( rLocalName
, ::xmloff::token::XML_DISPLAY_NAME
) ) )
81 if( !GetName().isEmpty() && !GetDisplayName().isEmpty() && GetName() != GetDisplayName() )
83 const_cast< SvXMLImport
&>( GetImport() ).
84 AddStyleDisplayName( GetFamily(), GetName(), GetDisplayName() );
90 SvXMLImportContext
*XMLShapeStyleContext::CreateChildContext(
92 const OUString
& rLocalName
,
93 const Reference
< xml::sax::XAttributeList
> & xAttrList
)
95 SvXMLImportContext
*pContext
= 0;
97 if( XML_NAMESPACE_STYLE
== nPrefix
|| XML_NAMESPACE_LO_EXT
== nPrefix
)
99 sal_uInt32 nFamily
= 0;
100 if( IsXMLToken( rLocalName
, XML_TEXT_PROPERTIES
) )
101 nFamily
= XML_TYPE_PROP_TEXT
;
102 else if( IsXMLToken( rLocalName
, XML_PARAGRAPH_PROPERTIES
) )
103 nFamily
= XML_TYPE_PROP_PARAGRAPH
;
104 else if( IsXMLToken( rLocalName
, XML_GRAPHIC_PROPERTIES
) )
105 nFamily
= XML_TYPE_PROP_GRAPHIC
;
108 rtl::Reference
< SvXMLImportPropertyMapper
> xImpPrMap
=
109 GetStyles()->GetImportPropertyMapper( GetFamily() );
111 pContext
= new XMLShapePropertySetContext( GetImport(), nPrefix
,
112 rLocalName
, xAttrList
,
120 pContext
= XMLPropStyleContext::CreateChildContext( nPrefix
, rLocalName
,
126 void XMLShapeStyleContext::FillPropertySet( const Reference
< beans::XPropertySet
> & rPropSet
)
128 if( !m_bIsNumRuleAlreadyConverted
)
130 m_bIsNumRuleAlreadyConverted
= true;
132 // for compatibility to beta files, search for CTF_SD_NUMBERINGRULES_NAME to
133 // import numbering rules from the style:properties element
134 const rtl::Reference
< XMLPropertySetMapper
>&rMapper
= GetStyles()->GetImportPropertyMapper( GetFamily() )->getPropertySetMapper();
136 ::std::vector
< XMLPropertyState
> &rProperties
= GetProperties();
137 ::std::vector
< XMLPropertyState
>::iterator
end( rProperties
.end() );
138 ::std::vector
< XMLPropertyState
>::iterator property
;
140 // first, look for the old format, where we had a text:list-style-name
141 // attribute in the style:properties element
142 for( property
= rProperties
.begin(); property
!= end
; ++property
)
144 // find properties with context
145 if( (property
->mnIndex
!= -1) && (rMapper
->GetEntryContextId( property
->mnIndex
) == CTF_SD_NUMBERINGRULES_NAME
) )
149 // if we did not find an old list-style-name in the properties, and we need one
150 // because we got a style:list-style attribute in the style-style element
152 if( (property
== end
) && ( !m_sListStyleName
.isEmpty() ) )
154 sal_Int32 nIndex
= rMapper
->FindEntryIndex( CTF_SD_NUMBERINGRULES_NAME
);
155 DBG_ASSERT( -1 != nIndex
, "can't find numbering rules property entry, can't set numbering rule!" );
157 XMLPropertyState
aNewState( nIndex
);
158 rProperties
.push_back( aNewState
);
159 end
= rProperties
.end();
163 // so, if we have an old or a new list style name, we set its value to
165 if( property
!= end
)
167 if( m_sListStyleName
.isEmpty() )
169 property
->maValue
>>= m_sListStyleName
;
172 const SvxXMLListStyleContext
*pListStyle
= GetImport().GetTextImport()->FindAutoListStyle( m_sListStyleName
);
174 DBG_ASSERT( pListStyle
, "list-style not found for shape style" );
177 uno::Reference
< container::XIndexReplace
> xNumRule( SvxXMLListStyleContext::CreateNumRule( GetImport().GetModel() ) );
178 pListStyle
->FillUnoNumRule(xNumRule
);
179 property
->maValue
<<= xNumRule
;
183 property
->mnIndex
= -1;
188 struct _ContextID_Index_Pair aContextIDs
[] =
190 { CTF_DASHNAME
, -1 },
191 { CTF_LINESTARTNAME
, -1 },
192 { CTF_LINEENDNAME
, -1 },
193 { CTF_FILLGRADIENTNAME
, -1 },
194 { CTF_FILLTRANSNAME
, -1 },
195 { CTF_FILLHATCHNAME
, -1 },
196 { CTF_FILLBITMAPNAME
, -1 },
197 { CTF_SD_OLE_VIS_AREA_IMPORT_LEFT
, -1 },
198 { CTF_SD_OLE_VIS_AREA_IMPORT_TOP
, -1 },
199 { CTF_SD_OLE_VIS_AREA_IMPORT_WIDTH
, -1 },
200 { CTF_SD_OLE_VIS_AREA_IMPORT_HEIGHT
, -1 },
203 static const sal_uInt16 aFamilies
[] =
205 XML_STYLE_FAMILY_SD_STROKE_DASH_ID
,
206 XML_STYLE_FAMILY_SD_MARKER_ID
,
207 XML_STYLE_FAMILY_SD_MARKER_ID
,
208 XML_STYLE_FAMILY_SD_GRADIENT_ID
,
209 XML_STYLE_FAMILY_SD_GRADIENT_ID
,
210 XML_STYLE_FAMILY_SD_HATCH_ID
,
211 XML_STYLE_FAMILY_SD_FILL_IMAGE_ID
214 rtl::Reference
< SvXMLImportPropertyMapper
> xImpPrMap
=
215 GetStyles()->GetImportPropertyMapper( GetFamily() );
216 DBG_ASSERT( xImpPrMap
.is(), "There is the import prop mapper" );
218 xImpPrMap
->FillPropertySet( GetProperties(), rPropSet
, aContextIDs
);
220 Reference
< XPropertySetInfo
> xInfo
;
221 // get property set mapper
222 rtl::Reference
<XMLPropertySetMapper
> xPropMapper( xImpPrMap
->getPropertySetMapper() );
224 for( sal_uInt16 i
=0; aContextIDs
[i
].nContextID
!= -1; i
++ )
226 sal_Int32 nIndex
= aContextIDs
[i
].nIndex
;
227 if( nIndex
!= -1 ) switch( aContextIDs
[i
].nContextID
)
230 case CTF_LINESTARTNAME
:
231 case CTF_LINEENDNAME
:
232 case CTF_FILLGRADIENTNAME
:
233 case CTF_FILLTRANSNAME
:
234 case CTF_FILLHATCHNAME
:
235 case CTF_FILLBITMAPNAME
:
237 struct XMLPropertyState
& rState
= GetProperties()[nIndex
];
239 rState
.maValue
>>= sStyleName
;
240 sStyleName
= GetImport().GetStyleDisplayName( aFamilies
[i
], sStyleName
);
241 // All of these attributes refer to something with draw:name
242 // of type styleName = NCName which is non-empty.
243 // tdf#89802: for Writer frames there would be no exception here but
244 // it will fail later on attach() and take out the entire frame
245 if (sStyleName
.isEmpty())
247 Sequence
<OUString
> const seq
{ sStyleName
};
248 GetImport().SetError(
249 XMLERROR_STYLE_PROP_VALUE
| XMLERROR_FLAG_WARNING
,
250 seq
, "empty style name reference", NULL
);
258 const OUString
& rPropertyName
= xPropMapper
->GetEntryAPIName(rState
.mnIndex
);
260 xInfo
= rPropSet
->getPropertySetInfo();
261 if ( xInfo
->hasPropertyByName( rPropertyName
) )
263 rPropSet
->setPropertyValue( rPropertyName
, Any( sStyleName
) );
266 catch ( const ::com::sun::star::lang::IllegalArgumentException
& e
)
268 Sequence
<OUString
> aSeq(1);
269 aSeq
[0] = sStyleName
;
270 GetImport().SetError(
271 XMLERROR_STYLE_PROP_VALUE
| XMLERROR_FLAG_WARNING
,
272 aSeq
, e
.Message
, NULL
);
276 case CTF_SD_OLE_VIS_AREA_IMPORT_LEFT
:
277 case CTF_SD_OLE_VIS_AREA_IMPORT_TOP
:
278 case CTF_SD_OLE_VIS_AREA_IMPORT_WIDTH
:
279 case CTF_SD_OLE_VIS_AREA_IMPORT_HEIGHT
:
281 struct XMLPropertyState
& rState
= GetProperties()[nIndex
];
282 const OUString
& rPropertyName
= xPropMapper
->GetEntryAPIName(rState
.mnIndex
);
286 xInfo
= rPropSet
->getPropertySetInfo();
287 if ( xInfo
->hasPropertyByName( rPropertyName
) )
289 rPropSet
->setPropertyValue( rPropertyName
, rState
.maValue
);
292 catch ( const ::com::sun::star::lang::IllegalArgumentException
& e
)
294 Sequence
<OUString
> aSeq
;
295 GetImport().SetError(
296 XMLERROR_STYLE_PROP_VALUE
| XMLERROR_FLAG_WARNING
,
297 aSeq
, e
.Message
, NULL
);
304 if (!m_sControlDataStyleName
.isEmpty())
305 { // we had a data-style-name attribute
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())
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())
316 GetImport().GetFormImport()->applyControlNumberStyle(xControlModel
, m_sControlDataStyleName
);
322 void XMLShapeStyleContext::Finish( bool /*bOverwrite*/ )
326 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */