LCOV - code coverage report
Current view: top level - xmloff/source/style - prstylei.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 163 200 81.5 %
Date: 2014-04-11 Functions: 12 15 80.0 %
Legend: Lines: hit not hit

          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       17750 : void XMLPropStyleContext::SetAttribute( sal_uInt16 nPrefixKey,
      50             :                                         const OUString& rLocalName,
      51             :                                         const OUString& rValue )
      52             : {
      53       17750 :     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       10777 :         SvXMLStyleContext::SetAttribute( nPrefixKey, rLocalName, rValue );
      60             :     }
      61       17750 : }
      62             : 
      63        7419 : TYPEINIT1( XMLPropStyleContext, SvXMLStyleContext );
      64             : 
      65        7428 : 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        7428 : ,   mxStyles( &rStyles )
      74             : {
      75        7428 : }
      76             : 
      77        7404 : XMLPropStyleContext::~XMLPropStyleContext()
      78             : {
      79        7404 : }
      80             : 
      81        1419 : SvXMLImportContext *XMLPropStyleContext::CreateChildContext(
      82             :         sal_uInt16 nPrefix,
      83             :         const OUString& rLocalName,
      84             :         const Reference< XAttributeList > & xAttrList )
      85             : {
      86        1419 :     SvXMLImportContext *pContext = 0;
      87             : 
      88        1419 :     sal_uInt32 nFamily = 0;
      89        1419 :     if( XML_NAMESPACE_STYLE == nPrefix )
      90             :     {
      91        1419 :         if( IsXMLToken( rLocalName, XML_GRAPHIC_PROPERTIES ) )
      92           0 :             nFamily = XML_TYPE_PROP_GRAPHIC;
      93        1419 :         else if( IsXMLToken( rLocalName, XML_DRAWING_PAGE_PROPERTIES )  )
      94           0 :             nFamily = XML_TYPE_PROP_DRAWING_PAGE;
      95        1419 :         else if( IsXMLToken( rLocalName, XML_TEXT_PROPERTIES )  )
      96         540 :             nFamily = XML_TYPE_PROP_TEXT;
      97         879 :         else if( IsXMLToken( rLocalName, XML_PARAGRAPH_PROPERTIES )  )
      98         278 :             nFamily = XML_TYPE_PROP_PARAGRAPH;
      99         601 :         else if( IsXMLToken( rLocalName, XML_RUBY_PROPERTIES )  )
     100           7 :             nFamily = XML_TYPE_PROP_RUBY;
     101         594 :         else if( IsXMLToken( rLocalName, XML_SECTION_PROPERTIES )  )
     102           0 :             nFamily = XML_TYPE_PROP_SECTION;
     103         594 :         else if( IsXMLToken( rLocalName, XML_TABLE_PROPERTIES )  )
     104         112 :             nFamily = XML_TYPE_PROP_TABLE;
     105         482 :         else if( IsXMLToken( rLocalName, XML_TABLE_COLUMN_PROPERTIES )  )
     106         273 :             nFamily = XML_TYPE_PROP_TABLE_COLUMN;
     107         209 :         else if( IsXMLToken( rLocalName, XML_TABLE_ROW_PROPERTIES )  )
     108         204 :             nFamily = XML_TYPE_PROP_TABLE_ROW;
     109           5 :         else if( IsXMLToken( rLocalName, XML_TABLE_CELL_PROPERTIES )  )
     110           0 :             nFamily = XML_TYPE_PROP_TABLE_CELL;
     111           5 :         else if( IsXMLToken( rLocalName, XML_CHART_PROPERTIES ) )
     112           0 :             nFamily = XML_TYPE_PROP_CHART;
     113             :     }
     114        1419 :     if( nFamily )
     115             :     {
     116             :         UniReference < SvXMLImportPropertyMapper > xImpPrMap =
     117        1414 :             ((SvXMLStylesContext *)&mxStyles)->GetImportPropertyMapper(
     118        1414 :                                                         GetFamily() );
     119        1414 :         if( xImpPrMap.is() )
     120        1414 :             pContext = new SvXMLPropertySetContext( GetImport(), nPrefix,
     121             :                                                     rLocalName, xAttrList,
     122             :                                                     nFamily,
     123             :                                                     maProperties,
     124        1414 :                                                     xImpPrMap );
     125             :     }
     126             : 
     127        1419 :     if( !pContext )
     128             :         pContext = SvXMLStyleContext::CreateChildContext( nPrefix, rLocalName,
     129           5 :                                                           xAttrList );
     130             : 
     131        1419 :     return pContext;
     132             : }
     133             : 
     134        5880 : void XMLPropStyleContext::FillPropertySet(
     135             :             const Reference< XPropertySet > & rPropSet )
     136             : {
     137             :     UniReference < SvXMLImportPropertyMapper > xImpPrMap =
     138        5880 :         ((SvXMLStylesContext *)&mxStyles)->GetImportPropertyMapper(
     139        5880 :                                                                 GetFamily() );
     140             :     DBG_ASSERT( xImpPrMap.is(), "There is the import prop mapper" );
     141        5880 :     if( xImpPrMap.is() )
     142        5880 :         xImpPrMap->FillPropertySet( maProperties, rPropSet );
     143        5880 : }
     144             : 
     145           0 : void XMLPropStyleContext::SetDefaults()
     146             : {
     147           0 : }
     148             : 
     149         493 : Reference < XStyle > XMLPropStyleContext::Create()
     150             : {
     151         493 :     Reference < XStyle > xNewStyle;
     152             : 
     153             :     OUString sServiceName(
     154         986 :         ((SvXMLStylesContext *)&mxStyles)->GetServiceName( GetFamily() ) );
     155         493 :     if( !sServiceName.isEmpty() )
     156             :     {
     157         493 :         Reference< XMultiServiceFactory > xFactory( GetImport().GetModel(),
     158         493 :                                                     UNO_QUERY );
     159         493 :         if( xFactory.is() )
     160             :         {
     161             :             Reference < XInterface > xIfc =
     162         493 :                 xFactory->createInstance( sServiceName );
     163         493 :             if( xIfc.is() )
     164         493 :                 xNewStyle = Reference < XStyle >( xIfc, UNO_QUERY );
     165         493 :         }
     166             :     }
     167             : 
     168         986 :     return xNewStyle;
     169             : }
     170             : 
     171             : typedef ::std::set < OUString > PropertyNameSet;
     172             : 
     173        3270 : void XMLPropStyleContext::CreateAndInsert( bool bOverwrite )
     174             : {
     175        6540 :     if( ((SvXMLStylesContext *)&mxStyles)->IsAutomaticStyle()
     176        3270 :         && ( GetFamily() == XML_STYLE_FAMILY_TEXT_TEXT || GetFamily() == XML_STYLE_FAMILY_TEXT_PARAGRAPH ) )
     177             :     {
     178             :         Reference < XAutoStyleFamily > xAutoFamily =
     179        1111 :                 ((SvXMLStylesContext *)&mxStyles)->GetAutoStyles( GetFamily() );
     180        1111 :         if( !xAutoFamily.is() )
     181           0 :             return;
     182             :         UniReference < SvXMLImportPropertyMapper > xImpPrMap =
     183        2222 :             ((SvXMLStylesContext *)&mxStyles)->GetImportPropertyMapper( GetFamily() );
     184             :         DBG_ASSERT( xImpPrMap.is(), "There is no import prop mapper" );
     185        1111 :         if( xImpPrMap.is() )
     186             :         {
     187        1111 :             Sequence< PropertyValue > aValues;
     188        1111 :             xImpPrMap->FillPropertySequence( maProperties, aValues );
     189             : 
     190        1111 :             sal_Int32 nLen = aValues.getLength();
     191        1111 :             if( nLen )
     192             :             {
     193        1099 :                 if( GetFamily() == XML_STYLE_FAMILY_TEXT_PARAGRAPH )
     194             :                 {
     195         613 :                     aValues.realloc( nLen + 2 );
     196         613 :                     PropertyValue *pProps = aValues.getArray() + nLen;
     197         613 :                     pProps->Name = "ParaStyleName";
     198         613 :                     OUString sParent( GetParentName() );
     199         613 :                     if( !sParent.isEmpty() )
     200         567 :                         sParent = GetImport().GetStyleDisplayName( GetFamily(), sParent );
     201             :                     else
     202          46 :                         sParent = "Standard";
     203         613 :                     pProps->Value <<= sParent;
     204         613 :                     ++pProps;
     205         613 :                     pProps->Name = "ParaConditionalStyleName";
     206         613 :                     pProps->Value <<= sParent;
     207             :                 }
     208             : 
     209        1099 :                 Reference < XAutoStyle > xAutoStyle = xAutoFamily->insertStyle( aValues );
     210        1099 :                 if( xAutoStyle.is() )
     211             :                 {
     212        1099 :                     Sequence< OUString > aPropNames(1);
     213        2811 :                     aPropNames[0] = GetFamily() == XML_STYLE_FAMILY_TEXT_PARAGRAPH ?
     214             :                         OUString("ParaAutoStyleName") :
     215        1585 :                         OUString("CharAutoStyleName");
     216        2198 :                     Sequence< Any > aAny = xAutoStyle->getPropertyValues( aPropNames );
     217        1099 :                     if( aAny.hasElements() )
     218             :                     {
     219        1099 :                         OUString aName;
     220        1099 :                         aAny[0] >>= aName;
     221        1099 :                         SetAutoName( aName );
     222        1099 :                     }
     223        1099 :                 }
     224        1111 :             }
     225        1111 :         }
     226             :     }
     227             :     else
     228             :     {
     229        2159 :         const OUString& rName = GetDisplayName();
     230        2159 :         if( rName.isEmpty() || IsDefaultStyle() )
     231           0 :             return;
     232             : 
     233             :         Reference < XNameContainer > xFamilies =
     234        2159 :                 ((SvXMLStylesContext *)&mxStyles)->GetStylesContainer( GetFamily() );
     235        2159 :         if( !xFamilies.is() )
     236           0 :             return;
     237             : 
     238        2159 :         sal_Bool bNew = sal_False;
     239        2159 :         if( xFamilies->hasByName( rName ) )
     240             :         {
     241        1666 :             Any aAny = xFamilies->getByName( rName );
     242        1666 :             aAny >>= mxStyle;
     243             :         }
     244             :         else
     245             :         {
     246         493 :             mxStyle = Create();
     247         493 :             if( !mxStyle.is() )
     248           0 :                 return;
     249             : 
     250         493 :             Any aAny;
     251         493 :             aAny <<= mxStyle;
     252         493 :             xFamilies->insertByName( rName, aAny );
     253         493 :             bNew = sal_True;
     254             :         }
     255             : 
     256        4318 :         Reference < XPropertySet > xPropSet( mxStyle, UNO_QUERY );
     257             :         Reference< XPropertySetInfo > xPropSetInfo =
     258        4318 :                     xPropSet->getPropertySetInfo();
     259        2159 :         if( !bNew && xPropSetInfo->hasPropertyByName( msIsPhysical ) )
     260             :         {
     261        1131 :             Any aAny = xPropSet->getPropertyValue( msIsPhysical );
     262        1131 :             bNew = !*(sal_Bool *)aAny.getValue();
     263             :         }
     264        2159 :         SetNew( bNew );
     265        2159 :         if( rName != GetName() )
     266         688 :             GetImport().AddStyleDisplayName( GetFamily(), GetName(), rName );
     267             : 
     268             : 
     269        2159 :         if( bOverwrite || bNew )
     270             :         {
     271        2087 :             Reference< XPropertyState > xPropState( xPropSet, uno::UNO_QUERY );
     272             : 
     273        4174 :             UniReference < XMLPropertySetMapper > xPrMap;
     274             :             UniReference < SvXMLImportPropertyMapper > xImpPrMap =
     275        2087 :                 ((SvXMLStylesContext *)&mxStyles)->GetImportPropertyMapper(
     276        4174 :                                                                     GetFamily() );
     277             :             DBG_ASSERT( xImpPrMap.is(), "There is the import prop mapper" );
     278        2087 :             if( xImpPrMap.is() )
     279        2087 :                 xPrMap = xImpPrMap->getPropertySetMapper();
     280        2087 :             if( xPrMap.is() )
     281             :             {
     282             :                 Reference < XMultiPropertyStates > xMultiStates( xPropSet,
     283        2087 :                                                                  UNO_QUERY );
     284        2087 :                 if( xMultiStates.is() )
     285             :                 {
     286        2087 :                     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        2087 :                 }
     318             :             }
     319             : 
     320        2087 :             if (mxStyle.is())
     321        2087 :                 mxStyle->setParentStyle(OUString());
     322             : 
     323        4174 :             FillPropertySet( xPropSet );
     324             :         }
     325             :         else
     326             :         {
     327          72 :             SetValid( false );
     328        2159 :         }
     329             :     }
     330             : }
     331             : 
     332        2255 : void XMLPropStyleContext::Finish( bool bOverwrite )
     333             : {
     334        2255 :     if( mxStyle.is() && (IsNew() || bOverwrite) )
     335             :     {
     336             :         // The families cintaner must exist
     337             :         Reference < XNameContainer > xFamilies =
     338        2087 :             ((SvXMLStylesContext *)&mxStyles)->GetStylesContainer( GetFamily() );
     339             :         DBG_ASSERT( xFamilies.is(), "Families lost" );
     340        2087 :         if( !xFamilies.is() )
     341        2255 :             return;
     342             : 
     343             :         // connect parent
     344        4174 :         OUString sParent( GetParentName() );
     345        2087 :         if( !sParent.isEmpty() )
     346        1485 :             sParent = GetImport().GetStyleDisplayName( GetFamily(), sParent );
     347        2087 :         if( !sParent.isEmpty() && !xFamilies->hasByName( sParent ) )
     348           0 :             sParent = OUString();
     349             : 
     350        2087 :         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        1187 :                 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        4174 :         OUString sFollow( GetFollow() );
     383        2087 :         if( !sFollow.isEmpty() )
     384         166 :             sFollow = GetImport().GetStyleDisplayName( GetFamily(), sFollow );
     385        2087 :         if( sFollow.isEmpty() || !xFamilies->hasByName( sFollow ) )
     386        1921 :             sFollow = mxStyle->getName();
     387             : 
     388        4174 :         Reference < XPropertySet > xPropSet( mxStyle, UNO_QUERY );
     389             :         Reference< XPropertySetInfo > xPropSetInfo =
     390        4174 :             xPropSet->getPropertySetInfo();
     391        2087 :         if( xPropSetInfo->hasPropertyByName( msFollowStyle ) )
     392             :         {
     393        1013 :             Any aAny = xPropSet->getPropertyValue( msFollowStyle );
     394        2026 :             OUString sCurrFollow;
     395        1013 :             aAny >>= sCurrFollow;
     396        1013 :             if( sCurrFollow != sFollow )
     397             :             {
     398          41 :                 aAny <<= sFollow;
     399          41 :                 xPropSet->setPropertyValue( msFollowStyle, aAny );
     400        1013 :             }
     401             :         }
     402             : 
     403        2087 :         if ( xPropSetInfo->hasPropertyByName( "Hidden" ) )
     404             :         {
     405        2087 :             xPropSet->setPropertyValue( "Hidden", uno::makeAny( IsHidden( ) ) );
     406        2087 :         }
     407             : 
     408             :     }
     409             : }
     410             : 
     411             : 
     412             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10