Line data Source code
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 <set>
22 : #include "xmloff/xmlnmspe.hxx"
23 : #include <xmloff/xmltoken.hxx>
24 : #include <xmloff/xmlprcon.hxx>
25 : #include <com/sun/star/style/XStyle.hpp>
26 : #include <com/sun/star/style/XAutoStyleFamily.hpp>
27 : #include <com/sun/star/container/XNameContainer.hpp>
28 : #include <com/sun/star/beans/XPropertySet.hpp>
29 : #include <com/sun/star/beans/XPropertyState.hpp>
30 : #include <com/sun/star/beans/XMultiPropertyStates.hpp>
31 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
32 : #include <xmloff/xmlimp.hxx>
33 :
34 : #include <xmloff/prstylei.hxx>
35 : #include <xmloff/attrlist.hxx>
36 : #include "xmloff/xmlerror.hxx"
37 :
38 : using ::rtl::OUString;
39 : using ::rtl::OUStringBuffer;
40 :
41 : using namespace ::com::sun::star;
42 : using namespace ::com::sun::star::uno;
43 : using namespace ::com::sun::star::xml::sax;
44 : using namespace ::com::sun::star::style;
45 : using namespace ::com::sun::star::container;
46 : using namespace ::com::sun::star::beans;
47 : using namespace ::com::sun::star::lang;
48 : using namespace ::xmloff::token;
49 :
50 :
51 2067 : void XMLPropStyleContext::SetAttribute( sal_uInt16 nPrefixKey,
52 : const OUString& rLocalName,
53 : const OUString& rValue )
54 : {
55 2067 : if( XML_NAMESPACE_STYLE == nPrefixKey && IsXMLToken( rLocalName, XML_FAMILY ) )
56 : {
57 : DBG_ASSERT( GetFamily() == ((SvXMLStylesContext *)&mxStyles)->GetFamily( rValue ), "unexpected style family" );
58 : }
59 : else
60 : {
61 1306 : SvXMLStyleContext::SetAttribute( nPrefixKey, rLocalName, rValue );
62 : }
63 2067 : }
64 :
65 759 : TYPEINIT1( XMLPropStyleContext, SvXMLStyleContext );
66 :
67 829 : XMLPropStyleContext::XMLPropStyleContext( SvXMLImport& rImport,
68 : sal_uInt16 nPrfx, const OUString& rLName,
69 : const Reference< XAttributeList > & xAttrList,
70 : SvXMLStylesContext& rStyles, sal_uInt16 nFamily,
71 : sal_Bool bDefault )
72 : : SvXMLStyleContext( rImport, nPrfx, rLName, xAttrList, nFamily, bDefault )
73 : , msIsPhysical( "IsPhysical" )
74 : , msFollowStyle( "FollowStyle" )
75 829 : , mxStyles( &rStyles )
76 : {
77 829 : }
78 :
79 829 : XMLPropStyleContext::~XMLPropStyleContext()
80 : {
81 829 : }
82 :
83 383 : SvXMLImportContext *XMLPropStyleContext::CreateChildContext(
84 : sal_uInt16 nPrefix,
85 : const OUString& rLocalName,
86 : const Reference< XAttributeList > & xAttrList )
87 : {
88 383 : SvXMLImportContext *pContext = 0;
89 :
90 383 : sal_uInt32 nFamily = 0;
91 383 : if( XML_NAMESPACE_STYLE == nPrefix )
92 : {
93 383 : if( IsXMLToken( rLocalName, XML_GRAPHIC_PROPERTIES ) )
94 0 : nFamily = XML_TYPE_PROP_GRAPHIC;
95 383 : else if( IsXMLToken( rLocalName, XML_DRAWING_PAGE_PROPERTIES ) )
96 0 : nFamily = XML_TYPE_PROP_DRAWING_PAGE;
97 383 : else if( IsXMLToken( rLocalName, XML_TEXT_PROPERTIES ) )
98 112 : nFamily = XML_TYPE_PROP_TEXT;
99 271 : else if( IsXMLToken( rLocalName, XML_PARAGRAPH_PROPERTIES ) )
100 49 : nFamily = XML_TYPE_PROP_PARAGRAPH;
101 222 : else if( IsXMLToken( rLocalName, XML_RUBY_PROPERTIES ) )
102 0 : nFamily = XML_TYPE_PROP_RUBY;
103 222 : else if( IsXMLToken( rLocalName, XML_SECTION_PROPERTIES ) )
104 0 : nFamily = XML_TYPE_PROP_SECTION;
105 222 : else if( IsXMLToken( rLocalName, XML_TABLE_PROPERTIES ) )
106 22 : nFamily = XML_TYPE_PROP_TABLE;
107 200 : else if( IsXMLToken( rLocalName, XML_TABLE_COLUMN_PROPERTIES ) )
108 46 : nFamily = XML_TYPE_PROP_TABLE_COLUMN;
109 154 : else if( IsXMLToken( rLocalName, XML_TABLE_ROW_PROPERTIES ) )
110 39 : nFamily = XML_TYPE_PROP_TABLE_ROW;
111 115 : else if( IsXMLToken( rLocalName, XML_TABLE_CELL_PROPERTIES ) )
112 115 : nFamily = XML_TYPE_PROP_TABLE_CELL;
113 0 : else if( IsXMLToken( rLocalName, XML_CHART_PROPERTIES ) )
114 0 : nFamily = XML_TYPE_PROP_CHART;
115 : }
116 383 : if( nFamily )
117 : {
118 : UniReference < SvXMLImportPropertyMapper > xImpPrMap =
119 383 : ((SvXMLStylesContext *)&mxStyles)->GetImportPropertyMapper(
120 383 : GetFamily() );
121 383 : if( xImpPrMap.is() )
122 383 : pContext = new SvXMLPropertySetContext( GetImport(), nPrefix,
123 : rLocalName, xAttrList,
124 : nFamily,
125 : maProperties,
126 383 : xImpPrMap );
127 : }
128 :
129 383 : if( !pContext )
130 : pContext = SvXMLStyleContext::CreateChildContext( nPrefix, rLocalName,
131 0 : xAttrList );
132 :
133 383 : return pContext;
134 : }
135 :
136 1814 : void XMLPropStyleContext::FillPropertySet(
137 : const Reference< XPropertySet > & rPropSet )
138 : {
139 : UniReference < SvXMLImportPropertyMapper > xImpPrMap =
140 1814 : ((SvXMLStylesContext *)&mxStyles)->GetImportPropertyMapper(
141 1814 : GetFamily() );
142 : DBG_ASSERT( xImpPrMap.is(), "There is the import prop mapper" );
143 1814 : if( xImpPrMap.is() )
144 1814 : xImpPrMap->FillPropertySet( maProperties, rPropSet );
145 1814 : }
146 :
147 0 : void XMLPropStyleContext::SetDefaults()
148 : {
149 0 : }
150 :
151 63 : Reference < XStyle > XMLPropStyleContext::Create()
152 : {
153 63 : Reference < XStyle > xNewStyle;
154 :
155 : OUString sServiceName(
156 63 : ((SvXMLStylesContext *)&mxStyles)->GetServiceName( GetFamily() ) );
157 63 : if( !sServiceName.isEmpty() )
158 : {
159 63 : Reference< XMultiServiceFactory > xFactory( GetImport().GetModel(),
160 63 : UNO_QUERY );
161 63 : if( xFactory.is() )
162 : {
163 : Reference < XInterface > xIfc =
164 63 : xFactory->createInstance( sServiceName );
165 63 : if( xIfc.is() )
166 63 : xNewStyle = Reference < XStyle >( xIfc, UNO_QUERY );
167 63 : }
168 : }
169 :
170 63 : return xNewStyle;
171 : }
172 :
173 : typedef ::std::set < OUString, ::comphelper::UStringLess > PropertyNameSet;
174 :
175 334 : void XMLPropStyleContext::CreateAndInsert( sal_Bool bOverwrite )
176 : {
177 381 : if( ((SvXMLStylesContext *)&mxStyles)->IsAutomaticStyle()
178 47 : && ( GetFamily() == XML_STYLE_FAMILY_TEXT_TEXT || GetFamily() == XML_STYLE_FAMILY_TEXT_PARAGRAPH ) )
179 : {
180 : Reference < XAutoStyleFamily > xAutoFamily =
181 26 : ((SvXMLStylesContext *)&mxStyles)->GetAutoStyles( GetFamily() );
182 26 : if( !xAutoFamily.is() )
183 : return;
184 : UniReference < SvXMLImportPropertyMapper > xImpPrMap =
185 26 : ((SvXMLStylesContext *)&mxStyles)->GetImportPropertyMapper( GetFamily() );
186 : DBG_ASSERT( xImpPrMap.is(), "There is no import prop mapper" );
187 26 : if( xImpPrMap.is() )
188 : {
189 26 : Sequence< PropertyValue > aValues;
190 26 : xImpPrMap->FillPropertySequence( maProperties, aValues );
191 :
192 26 : sal_Int32 nLen = aValues.getLength();
193 26 : if( nLen )
194 : {
195 23 : if( GetFamily() == XML_STYLE_FAMILY_TEXT_PARAGRAPH )
196 : {
197 18 : aValues.realloc( nLen + 2 );
198 18 : PropertyValue *pProps = aValues.getArray() + nLen;
199 18 : pProps->Name = rtl::OUString("ParaStyleName");
200 18 : OUString sParent( GetParentName() );
201 18 : if( !sParent.isEmpty() )
202 16 : sParent = GetImport().GetStyleDisplayName( GetFamily(), sParent );
203 : else
204 2 : sParent = rtl::OUString("Standard");
205 18 : pProps->Value <<= sParent;
206 18 : ++pProps;
207 18 : pProps->Name = rtl::OUString("ParaConditionalStyleName");
208 18 : pProps->Value <<= sParent;
209 : }
210 :
211 23 : Reference < XAutoStyle > xAutoStyle = xAutoFamily->insertStyle( aValues );
212 23 : if( xAutoStyle.is() )
213 : {
214 23 : Sequence< OUString > aPropNames(1);
215 46 : aPropNames[0] = GetFamily() == XML_STYLE_FAMILY_TEXT_PARAGRAPH ?
216 : rtl::OUString("ParaAutoStyleName") :
217 46 : rtl::OUString("CharAutoStyleName");
218 23 : Sequence< Any > aAny = xAutoStyle->getPropertyValues( aPropNames );
219 23 : if( aAny.hasElements() )
220 : {
221 23 : OUString aName;
222 23 : aAny[0] >>= aName;
223 23 : SetAutoName( aName );
224 23 : }
225 23 : }
226 26 : }
227 26 : }
228 : }
229 : else
230 : {
231 308 : const OUString& rName = GetDisplayName();
232 308 : if( rName.isEmpty() || IsDefaultStyle() )
233 : return;
234 :
235 : Reference < XNameContainer > xFamilies =
236 308 : ((SvXMLStylesContext *)&mxStyles)->GetStylesContainer( GetFamily() );
237 308 : if( !xFamilies.is() )
238 : return;
239 :
240 308 : sal_Bool bNew = sal_False;
241 308 : if( xFamilies->hasByName( rName ) )
242 : {
243 245 : Any aAny = xFamilies->getByName( rName );
244 245 : aAny >>= mxStyle;
245 : }
246 : else
247 : {
248 63 : mxStyle = Create();
249 63 : if( !mxStyle.is() )
250 : return;
251 :
252 63 : Any aAny;
253 63 : aAny <<= mxStyle;
254 63 : xFamilies->insertByName( rName, aAny );
255 63 : bNew = sal_True;
256 : }
257 :
258 308 : Reference < XPropertySet > xPropSet( mxStyle, UNO_QUERY );
259 : Reference< XPropertySetInfo > xPropSetInfo =
260 308 : xPropSet->getPropertySetInfo();
261 308 : if( !bNew && xPropSetInfo->hasPropertyByName( msIsPhysical ) )
262 : {
263 135 : Any aAny = xPropSet->getPropertyValue( msIsPhysical );
264 135 : bNew = !*(sal_Bool *)aAny.getValue();
265 : }
266 308 : SetNew( bNew );
267 308 : if( rName != GetName() )
268 93 : GetImport().AddStyleDisplayName( GetFamily(), GetName(), rName );
269 :
270 :
271 308 : if( bOverwrite || bNew )
272 : {
273 308 : Reference< XPropertyState > xPropState( xPropSet, uno::UNO_QUERY );
274 :
275 308 : UniReference < XMLPropertySetMapper > xPrMap;
276 : UniReference < SvXMLImportPropertyMapper > xImpPrMap =
277 308 : ((SvXMLStylesContext *)&mxStyles)->GetImportPropertyMapper(
278 308 : GetFamily() );
279 : DBG_ASSERT( xImpPrMap.is(), "There is the import prop mapper" );
280 308 : if( xImpPrMap.is() )
281 308 : xPrMap = xImpPrMap->getPropertySetMapper();
282 308 : if( xPrMap.is() )
283 : {
284 : Reference < XMultiPropertyStates > xMultiStates( xPropSet,
285 308 : UNO_QUERY );
286 308 : if( xMultiStates.is() )
287 : {
288 308 : xMultiStates->setAllPropertiesToDefault();
289 : }
290 : else
291 : {
292 0 : PropertyNameSet aNameSet;
293 0 : sal_Int32 nCount = xPrMap->GetEntryCount();
294 : sal_Int32 i;
295 0 : for( i = 0; i < nCount; i++ )
296 : {
297 0 : const OUString& rPrName = xPrMap->GetEntryAPIName( i );
298 0 : if( xPropSetInfo->hasPropertyByName( rPrName ) )
299 0 : aNameSet.insert( rPrName );
300 : }
301 :
302 0 : nCount = aNameSet.size();
303 0 : Sequence < OUString > aNames( nCount );
304 0 : OUString *pNames = aNames.getArray();
305 0 : PropertyNameSet::iterator aIter = aNameSet.begin();
306 0 : while( aIter != aNameSet.end() )
307 0 : *pNames++ = *aIter++;
308 :
309 : Sequence < PropertyState > aStates(
310 0 : xPropState->getPropertyStates( aNames ) );
311 0 : const PropertyState *pStates = aStates.getConstArray();
312 0 : pNames = aNames.getArray();
313 :
314 0 : for( i = 0; i < nCount; i++ )
315 : {
316 0 : if( PropertyState_DIRECT_VALUE == *pStates++ )
317 0 : xPropState->setPropertyToDefault( pNames[i] );
318 0 : }
319 308 : }
320 : }
321 :
322 308 : if (mxStyle.is())
323 308 : mxStyle->setParentStyle(OUString());
324 :
325 308 : FillPropertySet( xPropSet );
326 : }
327 : else
328 : {
329 0 : SetValid( sal_False );
330 308 : }
331 : }
332 : }
333 :
334 342 : void XMLPropStyleContext::Finish( sal_Bool bOverwrite )
335 : {
336 342 : if( mxStyle.is() && (IsNew() || bOverwrite) )
337 : {
338 : // The families cintaner must exist
339 : Reference < XNameContainer > xFamilies =
340 308 : ((SvXMLStylesContext *)&mxStyles)->GetStylesContainer( GetFamily() );
341 : DBG_ASSERT( xFamilies.is(), "Families lost" );
342 308 : if( !xFamilies.is() )
343 342 : return;
344 :
345 : // connect parent
346 308 : OUString sParent( GetParentName() );
347 308 : if( !sParent.isEmpty() )
348 254 : sParent = GetImport().GetStyleDisplayName( GetFamily(), sParent );
349 308 : if( !sParent.isEmpty() && !xFamilies->hasByName( sParent ) )
350 0 : sParent = OUString();
351 :
352 308 : if( sParent != mxStyle->getParentStyle() )
353 : {
354 : // this may except if setting the parent style forms a
355 : // circle in the style depencies; especially if the parent
356 : // style is the same as the current style
357 : try
358 : {
359 195 : mxStyle->setParentStyle( sParent );
360 : }
361 0 : catch(const uno::Exception& e)
362 : {
363 : // according to the API definition, I would expect a
364 : // container::NoSuchElementException. But it throws an
365 : // uno::RuntimeException instead. I catch
366 : // uno::Exception in order to process both of them.
367 :
368 : // We can't set the parent style. For a proper
369 : // Error-Message, we should pass in the name of the
370 : // style, as well as the desired parent style.
371 0 : Sequence<OUString> aSequence(2);
372 :
373 : // getName() throws no non-Runtime exception:
374 0 : aSequence[0] = mxStyle->getName();
375 0 : aSequence[1] = sParent;
376 :
377 0 : GetImport().SetError(
378 : XMLERROR_FLAG_ERROR | XMLERROR_PARENT_STYLE_NOT_ALLOWED,
379 0 : aSequence, e.Message, NULL );
380 : }
381 : }
382 :
383 : // connect follow
384 308 : OUString sFollow( GetFollow() );
385 308 : if( !sFollow.isEmpty() )
386 30 : sFollow = GetImport().GetStyleDisplayName( GetFamily(), sFollow );
387 308 : if( sFollow.isEmpty() || !xFamilies->hasByName( sFollow ) )
388 278 : sFollow = mxStyle->getName();
389 :
390 308 : Reference < XPropertySet > xPropSet( mxStyle, UNO_QUERY );
391 : Reference< XPropertySetInfo > xPropSetInfo =
392 308 : xPropSet->getPropertySetInfo();
393 308 : if( xPropSetInfo->hasPropertyByName( msFollowStyle ) )
394 : {
395 151 : Any aAny = xPropSet->getPropertyValue( msFollowStyle );
396 151 : OUString sCurrFollow;
397 151 : aAny >>= sCurrFollow;
398 151 : if( sCurrFollow != sFollow )
399 : {
400 4 : aAny <<= sFollow;
401 4 : xPropSet->setPropertyValue( msFollowStyle, aAny );
402 151 : }
403 : }
404 :
405 308 : if ( xPropSetInfo->hasPropertyByName( "Hidden" ) )
406 : {
407 308 : xPropSet->setPropertyValue( "Hidden", uno::makeAny( IsHidden( ) ) );
408 308 : }
409 :
410 : }
411 : }
412 :
413 :
414 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|