LCOV - code coverage report
Current view: top level - xmloff/source/text - XMLSectionImportContext.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 121 139 87.1 %
Date: 2014-11-03 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          92 : 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          92 : ,   bHasContent(false)
     113             : {
     114          92 : }
     115             : 
     116         184 : XMLSectionImportContext::~XMLSectionImportContext()
     117             : {
     118         184 : }
     119             : 
     120          92 : void XMLSectionImportContext::StartElement(
     121             :     const Reference<XAttributeList> & xAttrList)
     122             : {
     123             :     // process attributes
     124          92 :     ProcessAttributes(xAttrList);
     125             : 
     126             :     // process index headers:
     127          92 :     bool bIsIndexHeader = IsXMLToken( GetLocalName(), XML_INDEX_TITLE );
     128          92 :     if (bIsIndexHeader)
     129             :     {
     130          36 :         bValid = true;
     131             :     }
     132             : 
     133          92 :     rtl::Reference<XMLTextImportHelper> rHelper = GetImport().GetTextImport();
     134             : 
     135             :     // valid?
     136          92 :     if (bValid)
     137             :     {
     138             :         // create text section (as XPropertySet)
     139             :         Reference<XMultiServiceFactory> xFactory(
     140          92 :             GetImport().GetModel(),UNO_QUERY);
     141          92 :         if (xFactory.is())
     142             :         {
     143             :             Reference<XInterface> xIfc =
     144          92 :                 xFactory->createInstance( bIsIndexHeader ? sIndexHeaderSection
     145          92 :                                                         : sTextSection );
     146          92 :             if (xIfc.is())
     147             :             {
     148          92 :                 Reference<XPropertySet> xPropSet(xIfc, UNO_QUERY);
     149             : 
     150             :                 // save PropertySet (for CreateChildContext)
     151          92 :                 xSectionPropertySet = xPropSet;
     152             : 
     153             :                 // name
     154         184 :                 Reference<XNamed> xNamed(xPropSet, UNO_QUERY);
     155          92 :                 xNamed->setName(sName);
     156             : 
     157             :                 // stylename?
     158          92 :                 if (!sStyleName.isEmpty())
     159             :                 {
     160             :                     XMLPropStyleContext* pStyle = rHelper->
     161          92 :                         FindSectionStyle(sStyleName);
     162             : 
     163          92 :                     if (pStyle != NULL)
     164             :                     {
     165          92 :                         pStyle->FillPropertySet( xPropSet );
     166             :                     }
     167             :                 }
     168             : 
     169             :                 // IsVisible and condition (not for index headers)
     170          92 :                 if (! bIsIndexHeader)
     171             :                 {
     172          56 :                     Any aAny;
     173          56 :                     aAny.setValue( &bIsVisible, ::getBooleanCppuType() );
     174          56 :                     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          56 :                     if( bIsCurrentlyVisibleOK )
     180             :                     {
     181             :                         aAny.setValue( &bIsCurrentlyVisible,
     182           0 :                                        ::getBooleanCppuType() );
     183           0 :                         xPropSet->setPropertyValue( sIsCurrentlyVisible, aAny);
     184             :                     }
     185             : 
     186          56 :                     if (bCondOK)
     187             :                     {
     188           4 :                         aAny <<= sCond;
     189           4 :                         xPropSet->setPropertyValue( sCondition, aAny );
     190          56 :                     }
     191             :                 }
     192             : 
     193             :                 // password (only for regular sections)
     194          96 :                 if ( bSequenceOK &&
     195           4 :                      IsXMLToken(GetLocalName(), XML_SECTION) )
     196             :                 {
     197           4 :                     Any aAny;
     198           4 :                     aAny <<= aSequence;
     199           4 :                     xPropSet->setPropertyValue(sProtectionKey, aAny);
     200             :                 }
     201             : 
     202             :                 // protection
     203         184 :                 Any aAny;
     204          92 :                 aAny.setValue( &bProtect, ::getBooleanCppuType() );
     205          92 :                 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         184 :                     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         184 :                 OUString sMarkerString(sMarker);
     218          92 :                 rHelper->InsertString(sMarkerString);
     219             :                 rHelper->InsertControlCharacter(
     220          92 :                     ControlCharacter::APPEND_PARAGRAPH );
     221          92 :                 rHelper->InsertString(sMarkerString);
     222             : 
     223             :                 // select first marker
     224          92 :                 rHelper->GetCursor()->gotoRange(xStart, sal_False);
     225          92 :                 rHelper->GetCursor()->goRight(1, sal_True);
     226             : 
     227             :                 // convert section to XTextContent
     228             :                 Reference<XTextContent> xTextContent(xSectionPropertySet,
     229         184 :                                                      UNO_QUERY);
     230             : 
     231             :                 // and insert (over marker)
     232          92 :                 rHelper->GetText()->insertTextContent(
     233          92 :                     rHelper->GetCursorAsRange(), xTextContent, sal_True );
     234             : 
     235             :                 // and delete first marker (in section)
     236          92 :                 rHelper->GetText()->insertString(
     237          92 :                     rHelper->GetCursorAsRange(), sEmpty, sal_True);
     238             : 
     239             :                 // finally, check for redlines that should start at
     240             :                 // the section start node
     241          92 :                 rHelper->RedlineAdjustStartNodeCursor(true); // start ???
     242             : 
     243             :                 // xml:id for RDF metadata
     244         184 :                 GetImport().SetXmlId(xIfc, sXmlId);
     245          92 :             }
     246          92 :         }
     247          92 :     }
     248          92 : }
     249             : 
     250          92 : void XMLSectionImportContext::ProcessAttributes(
     251             :     const Reference<XAttributeList> & xAttrList )
     252             : {
     253          92 :     SvXMLTokenMap aTokenMap(aSectionTokenMap);
     254             : 
     255          92 :     sal_Int16 nLength = xAttrList->getLength();
     256         348 :     for(sal_Int16 nAttr = 0; nAttr < nLength; nAttr++)
     257             :     {
     258         256 :         OUString sLocalName;
     259         256 :         sal_uInt16 nNamePrefix = GetImport().GetNamespaceMap().
     260         256 :             GetKeyByAttrName( xAttrList->getNameByIndex(nAttr),
     261         512 :                               &sLocalName );
     262         512 :         OUString sAttr = xAttrList->getValueByIndex(nAttr);
     263             : 
     264         256 :         switch (aTokenMap.Get(nNamePrefix, sLocalName))
     265             :         {
     266             :             case XML_TOK_SECTION_XMLID:
     267          36 :                 sXmlId = sAttr;
     268          36 :                 break;
     269             :             case XML_TOK_SECTION_STYLE_NAME:
     270          92 :                 sStyleName = sAttr;
     271          92 :                 break;
     272             :             case XML_TOK_SECTION_NAME:
     273          92 :                 sName = sAttr;
     274          92 :                 bValid = true;
     275          92 :                 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           4 :                 if (IsXMLToken(sAttr, XML_TRUE))
     292             :                 {
     293           0 :                     bIsVisible = true;
     294             :                 }
     295           8 :                 else if ( IsXMLToken(sAttr, XML_NONE) ||
     296           4 :                           IsXMLToken(sAttr, XML_CONDITION) )
     297             :                 {
     298           4 :                     bIsVisible = false;
     299             :                 }
     300             :                 // else: ignore
     301           4 :                 break;
     302             :             case XML_TOK_SECTION_IS_HIDDEN:
     303             :                 {
     304           0 :                     bool bTmp(false);
     305           0 :                     if (::sax::Converter::convertBool(bTmp, sAttr))
     306             :                     {
     307           0 :                         bIsCurrentlyVisible = !bTmp;
     308           0 :                         bIsCurrentlyVisibleOK = true;
     309             :                     }
     310             :                 }
     311           0 :                 break;
     312             :             case XML_TOK_SECTION_PROTECTION_KEY:
     313           4 :                 ::sax::Converter::decodeBase64(aSequence, sAttr);
     314           4 :                 bSequenceOK = true;
     315           4 :                 break;
     316             :             case XML_TOK_SECTION_PROTECT:
     317             :             {
     318          24 :                 bool bTmp(false);
     319          24 :                 if (::sax::Converter::convertBool(bTmp, sAttr))
     320             :                 {
     321          24 :                     bProtect = bTmp;
     322             :                 }
     323          24 :                 break;
     324             :             }
     325             :             default:
     326             :                 ; // ignore
     327           0 :                 break;
     328             :         }
     329         348 :     }
     330          92 : }
     331             : 
     332          92 : void XMLSectionImportContext::EndElement()
     333             : {
     334             :     // get rid of last paragraph
     335             :     // (unless it's the only paragraph in the section)
     336          92 :     rtl::Reference<XMLTextImportHelper> rHelper = GetImport().GetTextImport();
     337          92 :     rHelper->GetCursor()->goRight(1, sal_False);
     338          92 :     if (bHasContent)
     339             :     {
     340          92 :         rHelper->GetCursor()->goLeft(1, sal_True);
     341         184 :         rHelper->GetText()->insertString(rHelper->GetCursorAsRange(),
     342         184 :                                          sEmpty, sal_True);
     343             :     }
     344             : 
     345             :     // and delete second marker
     346          92 :     rHelper->GetCursor()->goRight(1, sal_True);
     347         184 :     rHelper->GetText()->insertString(rHelper->GetCursorAsRange(),
     348         184 :                                      sEmpty, sal_True);
     349             : 
     350             :     // check for redlines to our endnode
     351          92 :     rHelper->RedlineAdjustStartNodeCursor(false);
     352          92 : }
     353             : 
     354         452 : SvXMLImportContext* XMLSectionImportContext::CreateChildContext(
     355             :     sal_uInt16 nPrefix,
     356             :     const OUString& rLocalName,
     357             :     const Reference<XAttributeList> & xAttrList )
     358             : {
     359         452 :     SvXMLImportContext* pContext = NULL;
     360             : 
     361             :     // section-source (-dde) elements
     362         888 :     if ( (XML_NAMESPACE_TEXT == nPrefix) &&
     363         436 :          IsXMLToken(rLocalName, XML_SECTION_SOURCE) )
     364             :     {
     365           0 :         pContext = new XMLSectionSourceImportContext(GetImport(),
     366             :                                                      nPrefix, rLocalName,
     367           0 :                                                      xSectionPropertySet);
     368             :     }
     369         452 :     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         452 :         pContext = GetImport().GetTextImport()->CreateTextChildContext(
     380         452 :             GetImport(), nPrefix, rLocalName, xAttrList,
     381         904 :             XML_TEXT_TYPE_SECTION );
     382             : 
     383             :         // if that fails, default context
     384         452 :         if (NULL == pContext)
     385             :         {
     386           0 :             pContext = new SvXMLImportContext( GetImport(),
     387           0 :                                                nPrefix, rLocalName );
     388             :         }
     389             :         else
     390         452 :             bHasContent = true;
     391             :     }
     392             : 
     393         452 :     return pContext;
     394             : }
     395             : 
     396             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10