LCOV - code coverage report
Current view: top level - xmloff/source/text - XMLSectionImportContext.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 130 139 93.5 %
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 "XMLSectionImportContext.hxx"
      21             : #include "XMLSectionSourceImportContext.hxx"
      22             : #include "XMLSectionSourceDDEImportContext.hxx"
      23             : #include <xmloff/xmlictxt.hxx>
      24             : #include <xmloff/xmlimp.hxx>
      25             : #include <xmloff/txtimp.hxx>
      26             : #include <xmloff/nmspmap.hxx>
      27             : #include <xmloff/xmlnmspe.hxx>
      28             : #include <xmloff/xmltoken.hxx>
      29             : #include <xmloff/prstylei.hxx>
      30             : #include <sax/tools/converter.hxx>
      31             : #include <com/sun/star/container/XNamed.hpp>
      32             : #include <com/sun/star/uno/Reference.h>
      33             : #include <com/sun/star/text/XTextContent.hpp>
      34             : #include <com/sun/star/beans/XPropertySet.hpp>
      35             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      36             : #include <com/sun/star/text/ControlCharacter.hpp>
      37             : 
      38             : 
      39             : using ::com::sun::star::beans::XPropertySet;
      40             : using ::com::sun::star::uno::Reference;
      41             : using ::com::sun::star::xml::sax::XAttributeList;
      42             : using ::com::sun::star::lang::XMultiServiceFactory;
      43             : using ::com::sun::star::container::XNamed;
      44             : 
      45             : using namespace ::com::sun::star::uno;
      46             : using namespace ::com::sun::star::text;
      47             : using namespace ::xmloff::token;
      48             : 
      49             : 
      50           0 : TYPEINIT1( XMLSectionImportContext, SvXMLImportContext );
      51             : 
      52             : const sal_Char sAPI_TextSection[] = "com.sun.star.text.TextSection";
      53             : const sal_Char sAPI_IndexHeaderSection[] = "com.sun.star.text.IndexHeaderSection";
      54             : const sal_Char sAPI_IsProtected[] = "IsProtected";
      55             : const sal_Char sAPI_Condition[] = "Condition";
      56             : const sal_Char sAPI_IsVisible[] = "IsVisible";
      57             : const sal_Char sAPI_IsCurrentlyVisible[] = "IsCurrentlyVisible";
      58             : const sal_Char sAPI_ProtectionKey[] = "ProtectionKey";
      59             : 
      60             : enum XMLSectionToken
      61             : {
      62             :     XML_TOK_SECTION_XMLID,
      63             :     XML_TOK_SECTION_STYLE_NAME,
      64             :     XML_TOK_SECTION_NAME,
      65             :     XML_TOK_SECTION_CONDITION,
      66             :     XML_TOK_SECTION_DISPLAY,
      67             :     XML_TOK_SECTION_PROTECT,
      68             :     XML_TOK_SECTION_PROTECTION_KEY,
      69             :     XML_TOK_SECTION_IS_HIDDEN
      70             : };
      71             : 
      72             : static const SvXMLTokenMapEntry aSectionTokenMap[] =
      73             : {
      74             :     { XML_NAMESPACE_XML , XML_ID, XML_TOK_SECTION_XMLID },
      75             :     { XML_NAMESPACE_TEXT, XML_STYLE_NAME, XML_TOK_SECTION_STYLE_NAME },
      76             :     { XML_NAMESPACE_TEXT, XML_NAME, XML_TOK_SECTION_NAME },
      77             :     { XML_NAMESPACE_TEXT, XML_CONDITION, XML_TOK_SECTION_CONDITION },
      78             :     { XML_NAMESPACE_TEXT, XML_DISPLAY, XML_TOK_SECTION_DISPLAY },
      79             :     { XML_NAMESPACE_TEXT, XML_PROTECTED, XML_TOK_SECTION_PROTECT },
      80             :     { XML_NAMESPACE_TEXT, XML_PROTECTION_KEY, XML_TOK_SECTION_PROTECTION_KEY},
      81             :     { XML_NAMESPACE_TEXT, XML_IS_HIDDEN, XML_TOK_SECTION_IS_HIDDEN },
      82             :     // compatibility with SRC629 (or earlier) versions
      83             :     { XML_NAMESPACE_TEXT, XML_PROTECT, XML_TOK_SECTION_PROTECT },
      84             :     XML_TOKEN_MAP_END
      85             : };
      86             : 
      87             : 
      88             : // section import: This one is fairly tricky due to a variety of
      89             : // limits of the core or the API. The main problem is that if you
      90             : // insert a section within another section, you can't move the cursor
      91             : // between the ends of the inner and the enclosing section. To avoid
      92             : // these problems, additional markers are first inserted and later deleted.
      93          81 : XMLSectionImportContext::XMLSectionImportContext(
      94             :     SvXMLImport& rImport,
      95             :     sal_uInt16 nPrfx,
      96             :     const OUString& rLocalName )
      97             : :   SvXMLImportContext(rImport, nPrfx, rLocalName)
      98             : ,   sTextSection(sAPI_TextSection)
      99             : ,   sIndexHeaderSection(sAPI_IndexHeaderSection)
     100             : ,   sCondition(sAPI_Condition)
     101             : ,   sIsVisible(sAPI_IsVisible)
     102             : ,   sProtectionKey(sAPI_ProtectionKey)
     103             : ,   sIsProtected(sAPI_IsProtected)
     104             : ,   sIsCurrentlyVisible(sAPI_IsCurrentlyVisible)
     105             : ,   bProtect(false)
     106             : ,   bCondOK(false)
     107             : ,   bIsVisible(true)
     108             : ,   bValid(false)
     109             : ,   bSequenceOK(false)
     110             : ,   bIsCurrentlyVisible(true)
     111             : ,   bIsCurrentlyVisibleOK(false)
     112          81 : ,   bHasContent(false)
     113             : {
     114          81 : }
     115             : 
     116         162 : XMLSectionImportContext::~XMLSectionImportContext()
     117             : {
     118         162 : }
     119             : 
     120          81 : void XMLSectionImportContext::StartElement(
     121             :     const Reference<XAttributeList> & xAttrList)
     122             : {
     123             :     // process attributes
     124          81 :     ProcessAttributes(xAttrList);
     125             : 
     126             :     // process index headers:
     127          81 :     bool bIsIndexHeader = IsXMLToken( GetLocalName(), XML_INDEX_TITLE );
     128          81 :     if (bIsIndexHeader)
     129             :     {
     130          22 :         bValid = true;
     131             :     }
     132             : 
     133          81 :     rtl::Reference<XMLTextImportHelper> rHelper = GetImport().GetTextImport();
     134             : 
     135             :     // valid?
     136          81 :     if (bValid)
     137             :     {
     138             :         // create text section (as XPropertySet)
     139             :         Reference<XMultiServiceFactory> xFactory(
     140          81 :             GetImport().GetModel(),UNO_QUERY);
     141          81 :         if (xFactory.is())
     142             :         {
     143             :             Reference<XInterface> xIfc =
     144          81 :                 xFactory->createInstance( bIsIndexHeader ? sIndexHeaderSection
     145          81 :                                                         : sTextSection );
     146          81 :             if (xIfc.is())
     147             :             {
     148          81 :                 Reference<XPropertySet> xPropSet(xIfc, UNO_QUERY);
     149             : 
     150             :                 // save PropertySet (for CreateChildContext)
     151          81 :                 xSectionPropertySet = xPropSet;
     152             : 
     153             :                 // name
     154         162 :                 Reference<XNamed> xNamed(xPropSet, UNO_QUERY);
     155          81 :                 xNamed->setName(sName);
     156             : 
     157             :                 // stylename?
     158          81 :                 if (!sStyleName.isEmpty())
     159             :                 {
     160             :                     XMLPropStyleContext* pStyle = rHelper->
     161          81 :                         FindSectionStyle(sStyleName);
     162             : 
     163          81 :                     if (pStyle != NULL)
     164             :                     {
     165          81 :                         pStyle->FillPropertySet( xPropSet );
     166             :                     }
     167             :                 }
     168             : 
     169             :                 // IsVisible and condition (not for index headers)
     170          81 :                 if (! bIsIndexHeader)
     171             :                 {
     172          59 :                     Any aAny;
     173          59 :                     aAny.setValue( &bIsVisible, cppu::UnoType<bool>::get() );
     174          59 :                     xPropSet->setPropertyValue( sIsVisible, aAny );
     175             : 
     176             :                     // #97450# hidden sections must be hidden on reload
     177             :                     // For backwards compatibility, set flag only if it is
     178             :                     // present
     179          59 :                     if( bIsCurrentlyVisibleOK )
     180             :                     {
     181             :                         aAny.setValue( &bIsCurrentlyVisible,
     182           2 :                                        cppu::UnoType<bool>::get() );
     183           2 :                         xPropSet->setPropertyValue( sIsCurrentlyVisible, aAny);
     184             :                     }
     185             : 
     186          59 :                     if (bCondOK)
     187             :                     {
     188           4 :                         aAny <<= sCond;
     189           4 :                         xPropSet->setPropertyValue( sCondition, aAny );
     190          59 :                     }
     191             :                 }
     192             : 
     193             :                 // password (only for regular sections)
     194          83 :                 if ( bSequenceOK &&
     195           2 :                      IsXMLToken(GetLocalName(), XML_SECTION) )
     196             :                 {
     197           2 :                     Any aAny;
     198           2 :                     aAny <<= aSequence;
     199           2 :                     xPropSet->setPropertyValue(sProtectionKey, aAny);
     200             :                 }
     201             : 
     202             :                 // protection
     203         162 :                 Any aAny;
     204          81 :                 aAny.setValue( &bProtect, cppu::UnoType<bool>::get() );
     205          81 :                 xPropSet->setPropertyValue( sIsProtected, aAny );
     206             : 
     207             :                 // insert marker, <paragraph>, marker; then insert
     208             :                 // section over the first marker character, and delete the
     209             :                 // last paragraph (and marker) when closing a section.
     210             :                 Reference<XTextRange> xStart =
     211         162 :                     rHelper->GetCursor()->getStart();
     212             : #ifndef DBG_UTIL
     213             :                 static const sal_Char sMarker[] = " ";
     214             : #else
     215             :                 static const sal_Char sMarker[] = "X";
     216             : #endif
     217         162 :                 OUString sMarkerString(sMarker);
     218          81 :                 rHelper->InsertString(sMarkerString);
     219             :                 rHelper->InsertControlCharacter(
     220          81 :                     ControlCharacter::APPEND_PARAGRAPH );
     221          81 :                 rHelper->InsertString(sMarkerString);
     222             : 
     223             :                 // select first marker
     224          81 :                 rHelper->GetCursor()->gotoRange(xStart, sal_False);
     225          81 :                 rHelper->GetCursor()->goRight(1, sal_True);
     226             : 
     227             :                 // convert section to XTextContent
     228             :                 Reference<XTextContent> xTextContent(xSectionPropertySet,
     229         162 :                                                      UNO_QUERY);
     230             : 
     231             :                 // and insert (over marker)
     232          81 :                 rHelper->GetText()->insertTextContent(
     233          81 :                     rHelper->GetCursorAsRange(), xTextContent, sal_True );
     234             : 
     235             :                 // and delete first marker (in section)
     236          81 :                 rHelper->GetText()->insertString(
     237          81 :                     rHelper->GetCursorAsRange(), sEmpty, sal_True);
     238             : 
     239             :                 // finally, check for redlines that should start at
     240             :                 // the section start node
     241          81 :                 rHelper->RedlineAdjustStartNodeCursor(true); // start ???
     242             : 
     243             :                 // xml:id for RDF metadata
     244         162 :                 GetImport().SetXmlId(xIfc, sXmlId);
     245          81 :             }
     246          81 :         }
     247          81 :     }
     248          81 : }
     249             : 
     250          81 : void XMLSectionImportContext::ProcessAttributes(
     251             :     const Reference<XAttributeList> & xAttrList )
     252             : {
     253          81 :     SvXMLTokenMap aTokenMap(aSectionTokenMap);
     254             : 
     255          81 :     sal_Int16 nLength = xAttrList->getLength();
     256         307 :     for(sal_Int16 nAttr = 0; nAttr < nLength; nAttr++)
     257             :     {
     258         226 :         OUString sLocalName;
     259         226 :         sal_uInt16 nNamePrefix = GetImport().GetNamespaceMap().
     260         226 :             GetKeyByAttrName( xAttrList->getNameByIndex(nAttr),
     261         452 :                               &sLocalName );
     262         452 :         OUString sAttr = xAttrList->getValueByIndex(nAttr);
     263             : 
     264         226 :         switch (aTokenMap.Get(nNamePrefix, sLocalName))
     265             :         {
     266             :             case XML_TOK_SECTION_XMLID:
     267          18 :                 sXmlId = sAttr;
     268          18 :                 break;
     269             :             case XML_TOK_SECTION_STYLE_NAME:
     270          81 :                 sStyleName = sAttr;
     271          81 :                 break;
     272             :             case XML_TOK_SECTION_NAME:
     273          81 :                 sName = sAttr;
     274          81 :                 bValid = true;
     275          81 :                 break;
     276             :             case XML_TOK_SECTION_CONDITION:
     277             :                 {
     278           4 :                     OUString sTmp;
     279           4 :                     sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
     280           4 :                                     _GetKeyByAttrName( sAttr, &sTmp, false );
     281           4 :                     if( XML_NAMESPACE_OOOW == nPrefix )
     282             :                     {
     283           4 :                         sCond = sTmp;
     284           4 :                         bCondOK = true;
     285             :                     }
     286             :                     else
     287           0 :                         sCond = sAttr;
     288             :                 }
     289           4 :                 break;
     290             :             case XML_TOK_SECTION_DISPLAY:
     291          17 :                 if (IsXMLToken(sAttr, XML_TRUE))
     292             :                 {
     293           0 :                     bIsVisible = true;
     294             :                 }
     295          21 :                 else if ( IsXMLToken(sAttr, XML_NONE) ||
     296           4 :                           IsXMLToken(sAttr, XML_CONDITION) )
     297             :                 {
     298          17 :                     bIsVisible = false;
     299             :                 }
     300             :                 // else: ignore
     301          17 :                 break;
     302             :             case XML_TOK_SECTION_IS_HIDDEN:
     303             :                 {
     304           2 :                     bool bTmp(false);
     305           2 :                     if (::sax::Converter::convertBool(bTmp, sAttr))
     306             :                     {
     307           2 :                         bIsCurrentlyVisible = !bTmp;
     308           2 :                         bIsCurrentlyVisibleOK = true;
     309             :                     }
     310             :                 }
     311           2 :                 break;
     312             :             case XML_TOK_SECTION_PROTECTION_KEY:
     313           2 :                 ::sax::Converter::decodeBase64(aSequence, sAttr);
     314           2 :                 bSequenceOK = true;
     315           2 :                 break;
     316             :             case XML_TOK_SECTION_PROTECT:
     317             :             {
     318          21 :                 bool bTmp(false);
     319          21 :                 if (::sax::Converter::convertBool(bTmp, sAttr))
     320             :                 {
     321          21 :                     bProtect = bTmp;
     322             :                 }
     323          21 :                 break;
     324             :             }
     325             :             default:
     326             :                 ; // ignore
     327           0 :                 break;
     328             :         }
     329         307 :     }
     330          81 : }
     331             : 
     332          81 : void XMLSectionImportContext::EndElement()
     333             : {
     334             :     // get rid of last paragraph
     335             :     // (unless it's the only paragraph in the section)
     336          81 :     rtl::Reference<XMLTextImportHelper> rHelper = GetImport().GetTextImport();
     337          81 :     rHelper->GetCursor()->goRight(1, sal_False);
     338          81 :     if (bHasContent)
     339             :     {
     340          81 :         rHelper->GetCursor()->goLeft(1, sal_True);
     341         162 :         rHelper->GetText()->insertString(rHelper->GetCursorAsRange(),
     342         162 :                                          sEmpty, sal_True);
     343             :     }
     344             : 
     345             :     // and delete second marker
     346          81 :     rHelper->GetCursor()->goRight(1, sal_True);
     347         162 :     rHelper->GetText()->insertString(rHelper->GetCursorAsRange(),
     348         162 :                                      sEmpty, sal_True);
     349             : 
     350             :     // check for redlines to our endnode
     351          81 :     rHelper->RedlineAdjustStartNodeCursor(false);
     352          81 : }
     353             : 
     354         332 : SvXMLImportContext* XMLSectionImportContext::CreateChildContext(
     355             :     sal_uInt16 nPrefix,
     356             :     const OUString& rLocalName,
     357             :     const Reference<XAttributeList> & xAttrList )
     358             : {
     359         332 :     SvXMLImportContext* pContext = NULL;
     360             : 
     361             :     // section-source (-dde) elements
     362         645 :     if ( (XML_NAMESPACE_TEXT == nPrefix) &&
     363         313 :          IsXMLToken(rLocalName, XML_SECTION_SOURCE) )
     364             :     {
     365           2 :         pContext = new XMLSectionSourceImportContext(GetImport(),
     366             :                                                      nPrefix, rLocalName,
     367           2 :                                                      xSectionPropertySet);
     368             :     }
     369         330 :     else if ( (XML_NAMESPACE_OFFICE == nPrefix) &&
     370           0 :               IsXMLToken(rLocalName, XML_DDE_SOURCE) )
     371             :     {
     372           0 :         pContext = new XMLSectionSourceDDEImportContext(GetImport(),
     373             :                                                         nPrefix, rLocalName,
     374           0 :                                                         xSectionPropertySet);
     375             :     }
     376             :     else
     377             :     {
     378             :         // otherwise: text context
     379         330 :         pContext = GetImport().GetTextImport()->CreateTextChildContext(
     380         330 :             GetImport(), nPrefix, rLocalName, xAttrList,
     381         660 :             XML_TEXT_TYPE_SECTION );
     382             : 
     383             :         // if that fails, default context
     384         330 :         if (NULL == pContext)
     385             :         {
     386           0 :             pContext = new SvXMLImportContext( GetImport(),
     387           0 :                                                nPrefix, rLocalName );
     388             :         }
     389             :         else
     390         330 :             bHasContent = true;
     391             :     }
     392             : 
     393         332 :     return pContext;
     394             : }
     395             : 
     396             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11