LCOV - code coverage report
Current view: top level - xmloff/source/text - XMLIndexTOCContext.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 107 117 91.5 %
Date: 2015-06-13 12:38:46 Functions: 6 11 54.5 %
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 "XMLIndexTOCContext.hxx"
      21             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      22             : #include <com/sun/star/lang/IllegalArgumentException.hpp>
      23             : #include <com/sun/star/uno/XInterface.hpp>
      24             : #include <com/sun/star/text/XTextContent.hpp>
      25             : #include <com/sun/star/text/XTextSection.hpp>
      26             : #include <com/sun/star/text/XRelativeTextContentInsert.hpp>
      27             : #include <com/sun/star/beans/XPropertySet.hpp>
      28             : #include <sax/tools/converter.hxx>
      29             : #include "XMLIndexTOCSourceContext.hxx"
      30             : #include "XMLIndexObjectSourceContext.hxx"
      31             : #include "XMLIndexAlphabeticalSourceContext.hxx"
      32             : #include "XMLIndexUserSourceContext.hxx"
      33             : #include "XMLIndexBibliographySourceContext.hxx"
      34             : #include "XMLIndexTableSourceContext.hxx"
      35             : #include "XMLIndexIllustrationSourceContext.hxx"
      36             : #include "XMLIndexBodyContext.hxx"
      37             : #include <xmloff/xmlictxt.hxx>
      38             : #include <xmloff/xmlimp.hxx>
      39             : #include <xmloff/txtimp.hxx>
      40             : #include <xmloff/nmspmap.hxx>
      41             : #include <xmloff/xmlnmspe.hxx>
      42             : #include <xmloff/xmltoken.hxx>
      43             : #include <xmloff/prstylei.hxx>
      44             : #include <xmloff/xmlerror.hxx>
      45             : #include <xmloff/xmluconv.hxx>
      46             : #include <rtl/ustring.hxx>
      47             : #include <osl/diagnose.h>
      48             : 
      49             : 
      50             : using namespace ::com::sun::star::uno;
      51             : using namespace ::com::sun::star::text;
      52             : using namespace ::xmloff::token;
      53             : 
      54             : using ::com::sun::star::beans::XPropertySet;
      55             : using ::com::sun::star::uno::Reference;
      56             : using ::com::sun::star::xml::sax::XAttributeList;
      57             : using ::com::sun::star::lang::XMultiServiceFactory;
      58             : using ::com::sun::star::lang::IllegalArgumentException;
      59             : 
      60             : 
      61           0 : TYPEINIT1(XMLIndexTOCContext, SvXMLImportContext);
      62             : 
      63             : static const sal_Char* aIndexServiceMap[] =
      64             : {
      65             :     "com.sun.star.text.ContentIndex",
      66             :     "com.sun.star.text.DocumentIndex",
      67             :     "com.sun.star.text.TableIndex",
      68             :     "com.sun.star.text.ObjectIndex",
      69             :     "com.sun.star.text.Bibliography",
      70             :     "com.sun.star.text.UserIndex",
      71             :     "com.sun.star.text.IllustrationsIndex"
      72             : };
      73             : 
      74             : static const XMLTokenEnum aIndexSourceElementMap[] =
      75             : {
      76             :     XML_TABLE_OF_CONTENT_SOURCE,
      77             :     XML_ALPHABETICAL_INDEX_SOURCE,
      78             :     XML_TABLE_INDEX_SOURCE,
      79             :     XML_OBJECT_INDEX_SOURCE,
      80             :     XML_BIBLIOGRAPHY_SOURCE,
      81             :     XML_USER_INDEX_SOURCE,
      82             :     XML_ILLUSTRATION_INDEX_SOURCE
      83             : };
      84             : 
      85             : SvXMLEnumMapEntry const aIndexTypeMap[] =
      86             : {
      87             :     { XML_TABLE_OF_CONTENT,     TEXT_INDEX_TOC },
      88             :     { XML_ALPHABETICAL_INDEX,   TEXT_INDEX_ALPHABETICAL },
      89             :     { XML_TABLE_INDEX,          TEXT_INDEX_TABLE },
      90             :     { XML_OBJECT_INDEX,         TEXT_INDEX_OBJECT },
      91             :     { XML_BIBLIOGRAPHY,         TEXT_INDEX_BIBLIOGRAPHY },
      92             :     { XML_USER_INDEX,           TEXT_INDEX_USER },
      93             :     { XML_ILLUSTRATION_INDEX,   TEXT_INDEX_ILLUSTRATION },
      94             :     { XML_TOKEN_INVALID,        0 }
      95             : };
      96             : 
      97             : 
      98          23 : XMLIndexTOCContext::XMLIndexTOCContext(SvXMLImport& rImport,
      99             :     sal_uInt16 nPrfx, const OUString& rLocalName)
     100             :     : SvXMLImportContext(rImport, nPrfx, rLocalName)
     101             :     , sIsProtected("IsProtected")
     102             :     , sName("Name")
     103             :     , eIndexType(TEXT_INDEX_UNKNOWN)
     104          23 :     , bValid(false)
     105             : {
     106          23 :     if (XML_NAMESPACE_TEXT == nPrfx)
     107             :     {
     108             :         sal_uInt16 nTmp;
     109          23 :         if (SvXMLUnitConverter::convertEnum(nTmp, rLocalName, aIndexTypeMap))
     110             :         {
     111             :             // check for array index:
     112             :             OSL_ENSURE(nTmp < (SAL_N_ELEMENTS(aIndexServiceMap)), "index out of range");
     113             :             OSL_ENSURE(SAL_N_ELEMENTS(aIndexServiceMap) ==
     114             :                        SAL_N_ELEMENTS(aIndexSourceElementMap),
     115             :                        "service and source element maps must be same size");
     116             : 
     117          23 :             eIndexType = static_cast<IndexTypeEnum>(nTmp);
     118          23 :             bValid = true;
     119             :         }
     120             :     }
     121          23 : }
     122             : 
     123          46 : XMLIndexTOCContext::~XMLIndexTOCContext()
     124             : {
     125          46 : }
     126             : 
     127          23 : void XMLIndexTOCContext::StartElement(
     128             :     const Reference<XAttributeList> & xAttrList)
     129             : {
     130          23 :     if (bValid)
     131             :     {
     132             :         // find text:style-name attribute and set section style
     133             :         // find text:protected and set value
     134             :         // find text:name and set value (if not empty)
     135          23 :         sal_Int16 nCount = xAttrList->getLength();
     136          23 :         sal_Bool bProtected = sal_False;
     137          23 :         OUString sIndexName;
     138          46 :         OUString sXmlId;
     139          23 :         XMLPropStyleContext* pStyle(NULL);
     140         105 :         for(sal_Int16 nAttr = 0; nAttr < nCount; nAttr++)
     141             :         {
     142          82 :             OUString sLocalName;
     143          82 :             sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
     144          82 :                 GetKeyByAttrName( xAttrList->getNameByIndex(nAttr),
     145         164 :                                   &sLocalName );
     146          82 :             if ( XML_NAMESPACE_TEXT == nPrefix)
     147             :             {
     148          68 :                 if ( IsXMLToken( sLocalName, XML_STYLE_NAME ) )
     149             :                 {
     150          23 :                     pStyle = GetImport().GetTextImport()->FindSectionStyle(
     151          46 :                                 xAttrList->getValueByIndex(nAttr));
     152             :                 }
     153          45 :                 else if ( IsXMLToken( sLocalName, XML_PROTECTED ) )
     154             :                 {
     155          22 :                     bool bTmp(false);
     156          44 :                     if (::sax::Converter::convertBool(
     157          44 :                          bTmp, xAttrList->getValueByIndex(nAttr)))
     158             :                     {
     159          22 :                         bProtected = bTmp;
     160             :                     }
     161             :                 }
     162          23 :                 else if ( IsXMLToken( sLocalName, XML_NAME ) )
     163             :                 {
     164          23 :                     sIndexName = xAttrList->getValueByIndex(nAttr);
     165             :                 }
     166             :             }
     167          14 :             else if ( XML_NAMESPACE_XML == nPrefix)
     168             :             {
     169          14 :                 if ( IsXMLToken( sLocalName, XML_ID ) )
     170             :                 {
     171          14 :                     sXmlId = xAttrList->getValueByIndex(nAttr);
     172             :                 }
     173             :             }
     174          82 :         }
     175             : 
     176             :         // create table of content (via MultiServiceFactory)
     177          23 :         Reference<XMultiServiceFactory> xFactory(GetImport().GetModel(),
     178          46 :                                                  UNO_QUERY);
     179          23 :         if( xFactory.is() )
     180             :         {
     181             :             Reference<XInterface> xIfc =
     182          23 :                 xFactory->createInstance(
     183          23 :                     OUString::createFromAscii(aIndexServiceMap[eIndexType]));
     184          23 :             if( xIfc.is() )
     185             :             {
     186             :                 // get Property set
     187          23 :                 Reference<XPropertySet> xPropSet(xIfc, UNO_QUERY);
     188          23 :                 xTOCPropertySet = xPropSet;
     189             : 
     190             :                 // insert section
     191             :                 // a) insert section
     192             :                 //    The inserted index consists of an empty paragraph
     193             :                 //    only, as well as an empty paragraph *after* the index
     194             :                 // b) insert marker after index, and put Cursor inside of the
     195             :                 //    index
     196             : 
     197             :                 // preliminaries
     198             : #ifndef DBG_UTIL
     199          46 :                 OUString sMarker(" ");
     200             : #else
     201             :                 OUString sMarker("Y");
     202             : #endif
     203             :                 rtl::Reference<XMLTextImportHelper> rImport =
     204          46 :                     GetImport().GetTextImport();
     205             : 
     206             :                 // a) insert index
     207          46 :                 Reference<XTextContent> xTextContent(xIfc, UNO_QUERY);
     208             :                 try
     209             :                 {
     210          23 :                     GetImport().GetTextImport()->InsertTextContent(
     211          23 :                         xTextContent);
     212             :                 }
     213           0 :                 catch(const IllegalArgumentException& e)
     214             :                 {
     215             :                     // illegal argument? Then we can't accept indices here!
     216           0 :                     Sequence<OUString> aSeq(1);
     217           0 :                     aSeq[0] = GetLocalName();
     218           0 :                     GetImport().SetError(
     219             :                         XMLERROR_FLAG_ERROR | XMLERROR_NO_INDEX_ALLOWED_HERE,
     220           0 :                         aSeq, e.Message, NULL );
     221             : 
     222             :                     // set bValid to false, and return prematurely
     223           0 :                     bValid = false;
     224          23 :                     return;
     225             :                 }
     226             : 
     227             :                 // xml:id for RDF metadata
     228          23 :                 GetImport().SetXmlId(xIfc, sXmlId);
     229             : 
     230             :                 // b) insert marker and move cursor
     231          23 :                 rImport->InsertString(sMarker);
     232          46 :                 rImport->GetCursor()->goLeft(2, sal_False);
     233          23 :             }
     234             :         }
     235             : 
     236             :         // finally, check for redlines that should start at
     237             :         // the section start node
     238          23 :         if( bValid )
     239          23 :             GetImport().GetTextImport()->RedlineAdjustStartNodeCursor(true);
     240             : 
     241          23 :         if (pStyle != NULL)
     242             :         {
     243          23 :             pStyle->FillPropertySet( xTOCPropertySet );
     244             :         }
     245             : 
     246          46 :         Any aAny;
     247          23 :         aAny.setValue( &bProtected, cppu::UnoType<bool>::get() );
     248          23 :         xTOCPropertySet->setPropertyValue( sIsProtected, aAny );
     249             : 
     250          23 :         if (!sIndexName.isEmpty())
     251             :         {
     252          23 :             aAny <<= sIndexName;
     253          23 :             xTOCPropertySet->setPropertyValue( sName, aAny );
     254          23 :         }
     255             :     }
     256             : }
     257             : 
     258          23 : void XMLIndexTOCContext::EndElement()
     259             : {
     260             :     // complete import of index by removing the markers (if the index
     261             :     // was actually inserted, that is)
     262          23 :     if( bValid )
     263             :     {
     264             :         // preliminaries
     265          23 :         OUString sEmpty;
     266          46 :         rtl::Reference<XMLTextImportHelper> rHelper= GetImport().GetTextImport();
     267             : 
     268             :         // get rid of last paragraph (unless it's the only paragraph)
     269          23 :         rHelper->GetCursor()->goRight(1, sal_False);
     270          46 :         if( xBodyContextRef.Is() &&
     271          23 :             static_cast<XMLIndexBodyContext*>(&xBodyContextRef)->HasContent() )
     272             :         {
     273          23 :             rHelper->GetCursor()->goLeft(1, sal_True);
     274          46 :             rHelper->GetText()->insertString(rHelper->GetCursorAsRange(),
     275          46 :                                              sEmpty, sal_True);
     276             :         }
     277             : 
     278             :         // and delete second marker
     279          23 :         rHelper->GetCursor()->goRight(1, sal_True);
     280          46 :         rHelper->GetText()->insertString(rHelper->GetCursorAsRange(),
     281          46 :                                          sEmpty, sal_True);
     282             : 
     283             :         // check for Redlines on our end node
     284          46 :         GetImport().GetTextImport()->RedlineAdjustStartNodeCursor(false);
     285             :     }
     286          23 : }
     287             : 
     288          46 : SvXMLImportContext* XMLIndexTOCContext::CreateChildContext(
     289             :     sal_uInt16 nPrefix,
     290             :     const OUString& rLocalName,
     291             :     const Reference<XAttributeList> & xAttrList )
     292             : {
     293          46 :     SvXMLImportContext* pContext = NULL;
     294             : 
     295          46 :     if (bValid)
     296             :     {
     297          46 :         if (XML_NAMESPACE_TEXT == nPrefix)
     298             :         {
     299          46 :             if ( IsXMLToken( rLocalName, XML_INDEX_BODY ) )
     300             :             {
     301          23 :                 pContext = new XMLIndexBodyContext(GetImport(), nPrefix,
     302          23 :                                                    rLocalName);
     303          23 :                 if ( !xBodyContextRef.Is() ||
     304           0 :                      !static_cast<XMLIndexBodyContext*>(&xBodyContextRef)->HasContent() )
     305             :                 {
     306          23 :                     xBodyContextRef = pContext;
     307             :                 }
     308             :             }
     309          23 :             else if (IsXMLToken(rLocalName, aIndexSourceElementMap[eIndexType]))
     310             :             {
     311             :                 // instantiate source context for the appropriate index type
     312          23 :                 switch (eIndexType)
     313             :                 {
     314             :                     case TEXT_INDEX_TOC:
     315             :                         pContext = new XMLIndexTOCSourceContext(
     316           8 :                             GetImport(), nPrefix, rLocalName, xTOCPropertySet);
     317           8 :                         break;
     318             : 
     319             :                     case TEXT_INDEX_OBJECT:
     320             :                         pContext = new XMLIndexObjectSourceContext(
     321           2 :                             GetImport(), nPrefix, rLocalName, xTOCPropertySet);
     322           2 :                         break;
     323             : 
     324             :                     case TEXT_INDEX_ALPHABETICAL:
     325             :                         pContext = new XMLIndexAlphabeticalSourceContext(
     326           3 :                             GetImport(), nPrefix, rLocalName, xTOCPropertySet);
     327           3 :                         break;
     328             : 
     329             :                     case TEXT_INDEX_USER:
     330             :                         pContext = new XMLIndexUserSourceContext(
     331           2 :                             GetImport(), nPrefix, rLocalName, xTOCPropertySet);
     332           2 :                         break;
     333             : 
     334             :                     case TEXT_INDEX_BIBLIOGRAPHY:
     335             :                         pContext = new XMLIndexBibliographySourceContext(
     336           3 :                             GetImport(), nPrefix, rLocalName, xTOCPropertySet);
     337           3 :                         break;
     338             : 
     339             :                     case TEXT_INDEX_TABLE:
     340             :                         pContext = new XMLIndexTableSourceContext(
     341           2 :                             GetImport(), nPrefix, rLocalName, xTOCPropertySet);
     342           2 :                         break;
     343             : 
     344             :                     case TEXT_INDEX_ILLUSTRATION:
     345             :                         pContext = new XMLIndexIllustrationSourceContext(
     346           3 :                             GetImport(), nPrefix, rLocalName, xTOCPropertySet);
     347           3 :                         break;
     348             : 
     349             :                     default:
     350             :                         OSL_FAIL("index type not implemented");
     351           0 :                         break;
     352             :                 }
     353             :             }
     354             :             // else: ignore
     355             :         }
     356             :         // else: no text: namespace -> ignore
     357             :     }
     358             :     // else: not valid -> ignore
     359             : 
     360             :     // default: ignore
     361          46 :     if (pContext == NULL)
     362             :     {
     363             :         pContext = SvXMLImportContext::CreateChildContext(nPrefix, rLocalName,
     364           0 :                                                           xAttrList);
     365             :     }
     366             : 
     367          46 :     return pContext;
     368             : }
     369             : 
     370             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11