LCOV - code coverage report
Current view: top level - xmloff/source/text - XMLIndexBibliographyConfigurationContext.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 110 118 93.2 %
Date: 2015-06-13 12:38:46 Functions: 7 12 58.3 %
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 "XMLIndexBibliographyConfigurationContext.hxx"
      21             : #include "XMLIndexBibliographyEntryContext.hxx"
      22             : #include <xmloff/xmlictxt.hxx>
      23             : #include <xmloff/xmlimp.hxx>
      24             : #include <xmloff/txtimp.hxx>
      25             : #include <xmloff/nmspmap.hxx>
      26             : #include <xmloff/xmlnmspe.hxx>
      27             : #include <xmloff/xmltoken.hxx>
      28             : #include <xmloff/xmluconv.hxx>
      29             : #include <sax/tools/converter.hxx>
      30             : #include <rtl/ustring.hxx>
      31             : #include <com/sun/star/beans/XPropertySet.hpp>
      32             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      33             : 
      34             : using namespace ::com::sun::star::text;
      35             : using namespace ::com::sun::star::uno;
      36             : using namespace ::xmloff::token;
      37             : 
      38             : using ::com::sun::star::xml::sax::XAttributeList;
      39             : using ::com::sun::star::beans::PropertyValue;
      40             : using ::com::sun::star::beans::XPropertySet;
      41             : using ::com::sun::star::lang::XMultiServiceFactory;
      42             : 
      43             : const sal_Char sAPI_FieldMaster_Bibliography[] =
      44             :                                 "com.sun.star.text.FieldMaster.Bibliography";
      45             : 
      46             : 
      47           0 : TYPEINIT1( XMLIndexBibliographyConfigurationContext, SvXMLStyleContext );
      48             : 
      49           5 : XMLIndexBibliographyConfigurationContext::XMLIndexBibliographyConfigurationContext(
      50             :     SvXMLImport& rImport,
      51             :     sal_uInt16 nPrfx,
      52             :     const OUString& rLocalName,
      53             :     const Reference<XAttributeList> & xAttrList) :
      54             :         SvXMLStyleContext(rImport, nPrfx, rLocalName, xAttrList, XML_STYLE_FAMILY_TEXT_BIBLIOGRAPHYCONFIG),
      55             :         sFieldMaster_Bibliography(
      56             :             sAPI_FieldMaster_Bibliography),
      57             :         sBracketBefore("BracketBefore"),
      58             :         sBracketAfter("BracketAfter"),
      59             :         sIsNumberEntries("IsNumberEntries"),
      60             :         sIsSortByPosition("IsSortByPosition"),
      61             :         sSortKeys("SortKeys"),
      62             :         sSortKey("SortKey"),
      63             :         sIsSortAscending("IsSortAscending"),
      64             :         sSortAlgorithm("SortAlgorithm"),
      65             :         sLocale("Locale"),
      66             :         sSuffix(),
      67             :         sPrefix(),
      68             :         sAlgorithm(),
      69             :         maLanguageTagODF(),
      70             :         bNumberedEntries(false),
      71           5 :         bSortByPosition(true)
      72             : {
      73           5 : }
      74             : 
      75          10 : XMLIndexBibliographyConfigurationContext::~XMLIndexBibliographyConfigurationContext()
      76             : {
      77          10 : }
      78             : 
      79           5 : void XMLIndexBibliographyConfigurationContext::StartElement(
      80             :     const Reference<XAttributeList> & xAttrList)
      81             : {
      82           5 :     sal_Int16 nLength = xAttrList->getLength();
      83          30 :     for(sal_Int16 nAttr = 0; nAttr < nLength; nAttr++)
      84             :     {
      85          25 :         OUString sLocalName;
      86          25 :         sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
      87          25 :             GetKeyByAttrName( xAttrList->getNameByIndex(nAttr),
      88          50 :                               &sLocalName );
      89             : 
      90             :         ProcessAttribute(nPrefix, sLocalName,
      91          25 :                          xAttrList->getValueByIndex(nAttr));
      92             :         // else: ignore
      93          25 :     }
      94           5 : }
      95             : 
      96          25 : void XMLIndexBibliographyConfigurationContext::ProcessAttribute(
      97             :     sal_uInt16 nPrefix,
      98             :     const OUString& sLocalName,
      99             :     const OUString& sValue)
     100             : {
     101          25 :     if( XML_NAMESPACE_TEXT == nPrefix )
     102             :     {
     103          15 :         if( IsXMLToken(sLocalName, XML_PREFIX) )
     104             :         {
     105           5 :             sPrefix = sValue;
     106             :         }
     107          10 :         else if( IsXMLToken(sLocalName, XML_SUFFIX) )
     108             :         {
     109           5 :             sSuffix = sValue;
     110             :         }
     111           5 :         else if( IsXMLToken(sLocalName, XML_NUMBERED_ENTRIES) )
     112             :         {
     113           0 :             bool bTmp(false);
     114           0 :             if (::sax::Converter::convertBool(bTmp, sValue))
     115             :             {
     116           0 :                 bNumberedEntries = bTmp;
     117             :             }
     118             :         }
     119           5 :         else if( IsXMLToken(sLocalName, XML_SORT_BY_POSITION) )
     120             :         {
     121           1 :             bool bTmp(false);
     122           1 :             if (::sax::Converter::convertBool(bTmp, sValue))
     123             :             {
     124           1 :                 bSortByPosition = bTmp;
     125             :             }
     126             :         }
     127           4 :         else if( IsXMLToken(sLocalName, XML_SORT_ALGORITHM) )
     128             :         {
     129           4 :             sAlgorithm = sValue;
     130             :         }
     131             :     }
     132          10 :     else if( XML_NAMESPACE_FO == nPrefix )
     133             :     {
     134          10 :         if( IsXMLToken(sLocalName, XML_LANGUAGE) )
     135             :         {
     136           5 :             maLanguageTagODF.maLanguage = sValue;
     137             :         }
     138           5 :         else if( IsXMLToken(sLocalName, XML_SCRIPT) )
     139             :         {
     140           0 :             maLanguageTagODF.maScript = sValue;
     141             :         }
     142           5 :         else if( IsXMLToken(sLocalName, XML_COUNTRY) )
     143             :         {
     144           5 :             maLanguageTagODF.maCountry = sValue;
     145             :         }
     146             :     }
     147           0 :     else if( XML_NAMESPACE_STYLE == nPrefix )
     148             :     {
     149           0 :         if( IsXMLToken(sLocalName, XML_RFC_LANGUAGE_TAG) )
     150             :         {
     151           0 :             maLanguageTagODF.maRfcLanguageTag = sValue;
     152             :         }
     153             :     }
     154          25 : }
     155             : 
     156             : 
     157           1 : SvXMLImportContext *XMLIndexBibliographyConfigurationContext::CreateChildContext(
     158             :     sal_uInt16 nPrefix,
     159             :     const OUString& rLocalName,
     160             :     const Reference<XAttributeList> & xAttrList )
     161             : {
     162           1 :     OUString sKey;
     163           1 :     sal_Bool bSort(sal_True);
     164             : 
     165             :     // process children here and use default context!
     166           2 :     if ( ( nPrefix == XML_NAMESPACE_TEXT ) &&
     167           1 :          IsXMLToken( rLocalName, XML_SORT_KEY ) )
     168             :     {
     169           1 :         sal_Int16 nLength = xAttrList->getLength();
     170           3 :         for(sal_Int16 nAttr = 0; nAttr < nLength; nAttr++)
     171             :         {
     172           2 :             OUString sLocalName;
     173           2 :             sal_uInt16 nPrfx = GetImport().GetNamespaceMap().
     174           2 :                 GetKeyByAttrName( xAttrList->getNameByIndex(nAttr),
     175           4 :                                   &sLocalName );
     176             : 
     177           2 :             if (nPrfx == XML_NAMESPACE_TEXT)
     178             :             {
     179           2 :                 if ( IsXMLToken( sLocalName, XML_KEY ) )
     180             :                 {
     181           1 :                     sKey = xAttrList->getValueByIndex(nAttr);
     182             :                 }
     183           1 :                 else if ( IsXMLToken( sLocalName, XML_SORT_ASCENDING ) )
     184             :                 {
     185           1 :                     bool bTmp(false);
     186           2 :                     if (::sax::Converter::convertBool(
     187           2 :                         bTmp, xAttrList->getValueByIndex(nAttr)))
     188             :                     {
     189           1 :                         bSort = bTmp;
     190             :                     }
     191             :                 }
     192             :             }
     193           2 :         }
     194             : 
     195             :         // valid data?
     196             :         sal_uInt16 nKey;
     197           1 :         if (SvXMLUnitConverter::convertEnum(nKey, sKey,
     198             :                                             aBibliographyDataFieldMap))
     199             :         {
     200             : 
     201           1 :             Any aAny;
     202           2 :             Sequence<PropertyValue> aKey(2);
     203             : 
     204           2 :             PropertyValue aNameValue;
     205           1 :             aNameValue.Name = sSortKey;
     206           1 :             aAny <<= (sal_Int16)nKey;
     207           1 :             aNameValue.Value = aAny;
     208           1 :             aKey[0] = aNameValue;
     209             : 
     210           2 :             PropertyValue aSortValue;
     211           1 :             aSortValue.Name = sIsSortAscending;
     212           1 :             aAny.setValue(&bSort, cppu::UnoType<bool>::get());
     213           1 :             aSortValue.Value = aAny;
     214           1 :             aKey[1] = aSortValue;
     215             : 
     216           2 :             aSortKeys.push_back(aKey);
     217             :         }
     218             :     }
     219             : 
     220             :     return SvXMLImportContext::CreateChildContext(nPrefix, rLocalName,
     221           1 :                                                   xAttrList);
     222             : }
     223             : 
     224           5 : void XMLIndexBibliographyConfigurationContext::CreateAndInsert(bool)
     225             : {
     226             :     // (code almost the same as export...)
     227             : 
     228             :     // insert and block mode is handled in insertStyleFamily
     229             : 
     230             :     // first: get field master
     231             :     // (we'll create one, and get the only master for this type)
     232           5 :     Reference<XMultiServiceFactory> xFactory(GetImport().GetModel(),UNO_QUERY);
     233           5 :     if( xFactory.is() )
     234             :     {
     235           5 :         Sequence<OUString> aServices = xFactory->getAvailableServiceNames();
     236           5 :         bool bFound(false);
     237           5 :         sal_Int32 i(0);
     238           5 :         sal_Int32 nServiceCount(aServices.getLength());
     239         640 :         while (i < nServiceCount && !bFound)
     240             :         {
     241         630 :             if (aServices[i].equals(sFieldMaster_Bibliography))
     242             :             // here we should use a method which compares in reverse order if available
     243             :             // #85282#
     244           5 :                 bFound = true;
     245             :             else
     246         625 :                 i++;
     247             :         }
     248           5 :         if (bFound)
     249             :         {
     250             :             Reference<XInterface> xIfc =
     251           5 :                 xFactory->createInstance(sFieldMaster_Bibliography);
     252           5 :             if( xIfc.is() )
     253             :             {
     254           5 :                 Reference<XPropertySet> xPropSet( xIfc, UNO_QUERY );
     255          10 :                 Any aAny;
     256             : 
     257           5 :                 aAny <<= sSuffix;
     258           5 :                 xPropSet->setPropertyValue(sBracketAfter, aAny);
     259             : 
     260           5 :                 aAny <<= sPrefix;
     261           5 :                 xPropSet->setPropertyValue(sBracketBefore, aAny);
     262             : 
     263           5 :                 aAny.setValue(&bNumberedEntries, cppu::UnoType<bool>::get());
     264           5 :                 xPropSet->setPropertyValue(sIsNumberEntries, aAny);
     265             : 
     266           5 :                 aAny.setValue(&bSortByPosition, cppu::UnoType<bool>::get());
     267           5 :                 xPropSet->setPropertyValue(sIsSortByPosition, aAny);
     268             : 
     269           5 :                 if( !maLanguageTagODF.isEmpty() )
     270             :                 {
     271           5 :                     aAny <<= maLanguageTagODF.getLanguageTag().getLocale( false);
     272           5 :                     xPropSet->setPropertyValue(sLocale, aAny);
     273             :                 }
     274             : 
     275           5 :                 if( !sAlgorithm.isEmpty() )
     276             :                 {
     277           4 :                     aAny <<= sAlgorithm;
     278           4 :                     xPropSet->setPropertyValue(sSortAlgorithm, aAny);
     279             :                 }
     280             : 
     281           5 :                 sal_Int32 nCount = aSortKeys.size();
     282          10 :                 Sequence<Sequence<PropertyValue> > aKeysSeq(nCount);
     283           6 :                 for(i = 0; i < nCount; i++)
     284             :                 {
     285           1 :                     aKeysSeq[i] = aSortKeys[i];
     286             :                 }
     287           5 :                 aAny <<= aKeysSeq;
     288          10 :                 xPropSet->setPropertyValue(sSortKeys, aAny);
     289           5 :             }
     290             :             // else: can't get FieldMaster -> ignore
     291           5 :         }
     292           5 :     }
     293             :     // else: can't even get Factory -> ignore
     294           5 : }
     295             : 
     296             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11