LCOV - code coverage report
Current view: top level - xmloff/source/text - XMLIndexTOCContext.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 107 117 91.5 %
Date: 2014-04-11 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             : 
      48             : 
      49             : using namespace ::com::sun::star::uno;
      50             : using namespace ::com::sun::star::text;
      51             : using namespace ::xmloff::token;
      52             : 
      53             : using ::com::sun::star::beans::XPropertySet;
      54             : using ::com::sun::star::uno::Reference;
      55             : using ::com::sun::star::xml::sax::XAttributeList;
      56             : using ::com::sun::star::lang::XMultiServiceFactory;
      57             : using ::com::sun::star::lang::IllegalArgumentException;
      58             : 
      59             : 
      60           0 : TYPEINIT1(XMLIndexTOCContext, SvXMLImportContext);
      61             : 
      62             : static const sal_Char* aIndexServiceMap[] =
      63             : {
      64             :     "com.sun.star.text.ContentIndex",
      65             :     "com.sun.star.text.DocumentIndex",
      66             :     "com.sun.star.text.TableIndex",
      67             :     "com.sun.star.text.ObjectIndex",
      68             :     "com.sun.star.text.Bibliography",
      69             :     "com.sun.star.text.UserIndex",
      70             :     "com.sun.star.text.IllustrationsIndex"
      71             : };
      72             : 
      73             : static const XMLTokenEnum aIndexSourceElementMap[] =
      74             : {
      75             :     XML_TABLE_OF_CONTENT_SOURCE,
      76             :     XML_ALPHABETICAL_INDEX_SOURCE,
      77             :     XML_TABLE_INDEX_SOURCE,
      78             :     XML_OBJECT_INDEX_SOURCE,
      79             :     XML_BIBLIOGRAPHY_SOURCE,
      80             :     XML_USER_INDEX_SOURCE,
      81             :     XML_ILLUSTRATION_INDEX_SOURCE
      82             : };
      83             : 
      84             : SvXMLEnumMapEntry const aIndexTypeMap[] =
      85             : {
      86             :     { XML_TABLE_OF_CONTENT,     TEXT_INDEX_TOC },
      87             :     { XML_ALPHABETICAL_INDEX,   TEXT_INDEX_ALPHABETICAL },
      88             :     { XML_TABLE_INDEX,          TEXT_INDEX_TABLE },
      89             :     { XML_OBJECT_INDEX,         TEXT_INDEX_OBJECT },
      90             :     { XML_BIBLIOGRAPHY,         TEXT_INDEX_BIBLIOGRAPHY },
      91             :     { XML_USER_INDEX,           TEXT_INDEX_USER },
      92             :     { XML_ILLUSTRATION_INDEX,   TEXT_INDEX_ILLUSTRATION },
      93             :     { XML_TOKEN_INVALID,        0 }
      94             : };
      95             : 
      96             : 
      97          16 : XMLIndexTOCContext::XMLIndexTOCContext(SvXMLImport& rImport,
      98             :     sal_uInt16 nPrfx, const OUString& rLocalName)
      99             :     : SvXMLImportContext(rImport, nPrfx, rLocalName)
     100             :     , sTitle("Title")
     101             :     , sIsProtected("IsProtected")
     102             :     , sName("Name")
     103             :     , eIndexType(TEXT_INDEX_UNKNOWN)
     104          16 :     , bValid(sal_False)
     105             : {
     106          16 :     if (XML_NAMESPACE_TEXT == nPrfx)
     107             :     {
     108             :         sal_uInt16 nTmp;
     109          16 :         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          16 :             eIndexType = static_cast<IndexTypeEnum>(nTmp);
     118          16 :             bValid = sal_True;
     119             :         }
     120             :     }
     121          16 : }
     122             : 
     123          32 : XMLIndexTOCContext::~XMLIndexTOCContext()
     124             : {
     125          32 : }
     126             : 
     127          16 : void XMLIndexTOCContext::StartElement(
     128             :     const Reference<XAttributeList> & xAttrList)
     129             : {
     130          16 :     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          16 :         sal_Int16 nCount = xAttrList->getLength();
     136          16 :         sal_Bool bProtected = sal_False;
     137          16 :         OUString sIndexName;
     138          32 :         OUString sXmlId;
     139          16 :         XMLPropStyleContext* pStyle(NULL);
     140          78 :         for(sal_Int16 nAttr = 0; nAttr < nCount; nAttr++)
     141             :         {
     142          62 :             OUString sLocalName;
     143          62 :             sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
     144          62 :                 GetKeyByAttrName( xAttrList->getNameByIndex(nAttr),
     145         124 :                                   &sLocalName );
     146          62 :             if ( XML_NAMESPACE_TEXT == nPrefix)
     147             :             {
     148          48 :                 if ( IsXMLToken( sLocalName, XML_STYLE_NAME ) )
     149             :                 {
     150          16 :                     pStyle = GetImport().GetTextImport()->FindSectionStyle(
     151          32 :                                 xAttrList->getValueByIndex(nAttr));
     152             :                 }
     153          32 :                 else if ( IsXMLToken( sLocalName, XML_PROTECTED ) )
     154             :                 {
     155          16 :                     bool bTmp(false);
     156          32 :                     if (::sax::Converter::convertBool(
     157          32 :                          bTmp, xAttrList->getValueByIndex(nAttr)))
     158             :                     {
     159          16 :                         bProtected = bTmp;
     160             :                     }
     161             :                 }
     162          16 :                 else if ( IsXMLToken( sLocalName, XML_NAME ) )
     163             :                 {
     164          16 :                     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          62 :         }
     175             : 
     176             :         // create table of content (via MultiServiceFactory)
     177          16 :         Reference<XMultiServiceFactory> xFactory(GetImport().GetModel(),
     178          32 :                                                  UNO_QUERY);
     179          16 :         if( xFactory.is() )
     180             :         {
     181             :             Reference<XInterface> xIfc =
     182          16 :                 xFactory->createInstance(
     183          16 :                     OUString::createFromAscii(aIndexServiceMap[eIndexType]));
     184          16 :             if( xIfc.is() )
     185             :             {
     186             :                 // get Property set
     187          16 :                 Reference<XPropertySet> xPropSet(xIfc, UNO_QUERY);
     188          16 :                 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          32 :                 OUString sMarker(" ");
     200             : #else
     201             :                 OUString sMarker("Y");
     202             : #endif
     203             :                 UniReference<XMLTextImportHelper> rImport =
     204          32 :                     GetImport().GetTextImport();
     205             : 
     206             :                 // a) insert index
     207          32 :                 Reference<XTextContent> xTextContent(xIfc, UNO_QUERY);
     208             :                 try
     209             :                 {
     210          16 :                     GetImport().GetTextImport()->InsertTextContent(
     211          16 :                         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          16 :                     return;
     225             :                 }
     226             : 
     227             :                 // xml:id for RDF metadata
     228          16 :                 GetImport().SetXmlId(xIfc, sXmlId);
     229             : 
     230             :                 // b) insert marker and move cursor
     231          16 :                 rImport->InsertString(sMarker);
     232          32 :                 rImport->GetCursor()->goLeft(2, sal_False);
     233          16 :             }
     234             :         }
     235             : 
     236             :         // finally, check for redlines that should start at
     237             :         // the section start node
     238          16 :         if( bValid )
     239          16 :             GetImport().GetTextImport()->RedlineAdjustStartNodeCursor(true);
     240             : 
     241          16 :         if (pStyle != NULL)
     242             :         {
     243          16 :             pStyle->FillPropertySet( xTOCPropertySet );
     244             :         }
     245             : 
     246          32 :         Any aAny;
     247          16 :         aAny.setValue( &bProtected, ::getBooleanCppuType() );
     248          16 :         xTOCPropertySet->setPropertyValue( sIsProtected, aAny );
     249             : 
     250          16 :         if (!sIndexName.isEmpty())
     251             :         {
     252          16 :             aAny <<= sIndexName;
     253          16 :             xTOCPropertySet->setPropertyValue( sName, aAny );
     254          16 :         }
     255             :     }
     256             : }
     257             : 
     258          16 : void XMLIndexTOCContext::EndElement()
     259             : {
     260             :     // complete import of index by removing the markers (if the index
     261             :     // was actually inserted, that is)
     262          16 :     if( bValid )
     263             :     {
     264             :         // preliminaries
     265          16 :         OUString sEmpty;
     266          32 :         UniReference<XMLTextImportHelper> rHelper= GetImport().GetTextImport();
     267             : 
     268             :         // get rid of last paragraph (unless it's the only paragraph)
     269          16 :         rHelper->GetCursor()->goRight(1, sal_False);
     270          32 :         if( xBodyContextRef.Is() &&
     271          16 :             ((XMLIndexBodyContext*)&xBodyContextRef)->HasContent() )
     272             :         {
     273          16 :             rHelper->GetCursor()->goLeft(1, sal_True);
     274          32 :             rHelper->GetText()->insertString(rHelper->GetCursorAsRange(),
     275          32 :                                              sEmpty, sal_True);
     276             :         }
     277             : 
     278             :         // and delete second marker
     279          16 :         rHelper->GetCursor()->goRight(1, sal_True);
     280          32 :         rHelper->GetText()->insertString(rHelper->GetCursorAsRange(),
     281          32 :                                          sEmpty, sal_True);
     282             : 
     283             :         // check for Redlines on our end node
     284          32 :         GetImport().GetTextImport()->RedlineAdjustStartNodeCursor(false);
     285             :     }
     286          16 : }
     287             : 
     288          32 : SvXMLImportContext* XMLIndexTOCContext::CreateChildContext(
     289             :     sal_uInt16 nPrefix,
     290             :     const OUString& rLocalName,
     291             :     const Reference<XAttributeList> & xAttrList )
     292             : {
     293          32 :     SvXMLImportContext* pContext = NULL;
     294             : 
     295          32 :     if (bValid)
     296             :     {
     297          32 :         if (XML_NAMESPACE_TEXT == nPrefix)
     298             :         {
     299          32 :             if ( IsXMLToken( rLocalName, XML_INDEX_BODY ) )
     300             :             {
     301          16 :                 pContext = new XMLIndexBodyContext(GetImport(), nPrefix,
     302          16 :                                                    rLocalName);
     303          16 :                 if ( !xBodyContextRef.Is() ||
     304           0 :                      !((XMLIndexBodyContext*)&xBodyContextRef)->HasContent() )
     305             :                 {
     306          16 :                     xBodyContextRef = pContext;
     307             :                 }
     308             :             }
     309          16 :             else if (IsXMLToken(rLocalName, aIndexSourceElementMap[eIndexType]))
     310             :             {
     311             :                 // instantiate source context for the appropriate index type
     312          16 :                 switch (eIndexType)
     313             :                 {
     314             :                     case TEXT_INDEX_TOC:
     315             :                         pContext = new XMLIndexTOCSourceContext(
     316           3 :                             GetImport(), nPrefix, rLocalName, xTOCPropertySet);
     317           3 :                         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           2 :                             GetImport(), nPrefix, rLocalName, xTOCPropertySet);
     327           2 :                         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           2 :                             GetImport(), nPrefix, rLocalName, xTOCPropertySet);
     337           2 :                         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          32 :     if (pContext == NULL)
     362             :     {
     363             :         pContext = SvXMLImportContext::CreateChildContext(nPrefix, rLocalName,
     364           0 :                                                           xAttrList);
     365             :     }
     366             : 
     367          32 :     return pContext;
     368             : }
     369             : 
     370             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10