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