LCOV - code coverage report
Current view: top level - xmloff/source/meta - xmlmetai.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 110 122 90.2 %
Date: 2014-11-03 Functions: 18 18 100.0 %
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 <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
      21             : #include <com/sun/star/xml/dom/SAXDocumentBuilder.hpp>
      22             : #include <com/sun/star/xml/dom/XSAXDocumentBuilder2.hpp>
      23             : #include <com/sun/star/xml/xpath/XPathAPI.hpp>
      24             : #include <com/sun/star/beans/XPropertySet.hpp>
      25             : #include <com/sun/star/beans/XPropertySetInfo.hpp>
      26             : #include <comphelper/processfactory.hxx>
      27             : #include <xmloff/xmlmetai.hxx>
      28             : #include <xmloff/xmlimp.hxx>
      29             : #include <xmloff/nmspmap.hxx>
      30             : #include <xmloff/xmltoken.hxx>
      31             : #include <xmloff/xmlnmspe.hxx>
      32             : 
      33             : using namespace com::sun::star;
      34             : using namespace ::xmloff::token;
      35             : 
      36             : /// builds a DOM tree from SAX events, by forwarding to SAXDocumentBuilder
      37             : class XMLDocumentBuilderContext : public SvXMLImportContext
      38             : {
      39             : private:
      40             :     ::com::sun::star::uno::Reference<
      41             :         ::com::sun::star::xml::dom::XSAXDocumentBuilder2> mxDocBuilder;
      42             : 
      43             : public:
      44             :     XMLDocumentBuilderContext(SvXMLImport& rImport, sal_uInt16 nPrfx,
      45             :         const OUString& rLName,
      46             :         const ::com::sun::star::uno::Reference<
      47             :             ::com::sun::star::xml::sax::XAttributeList>& xAttrList,
      48             :         const ::com::sun::star::uno::Reference<
      49             :             ::com::sun::star::xml::dom::XSAXDocumentBuilder2>& rDocBuilder);
      50             : 
      51             :     virtual ~XMLDocumentBuilderContext();
      52             : 
      53             :     virtual SvXMLImportContext *CreateChildContext( sal_uInt16 nPrefix,
      54             :         const OUString& rLocalName,
      55             :         const ::com::sun::star::uno::Reference<
      56             :             ::com::sun::star::xml::sax::XAttributeList>& xAttrList ) SAL_OVERRIDE;
      57             : 
      58             :     virtual void StartElement( const ::com::sun::star::uno::Reference<
      59             :             ::com::sun::star::xml::sax::XAttributeList >& xAttrList ) SAL_OVERRIDE;
      60             : 
      61             :     virtual void Characters( const OUString& rChars ) SAL_OVERRIDE;
      62             : 
      63             :     virtual void EndElement() SAL_OVERRIDE;
      64             : };
      65             : 
      66        6198 : XMLDocumentBuilderContext::XMLDocumentBuilderContext(SvXMLImport& rImport,
      67             :         sal_uInt16 nPrfx, const OUString& rLName,
      68             :         const uno::Reference<xml::sax::XAttributeList>&,
      69             :         const uno::Reference<xml::dom::XSAXDocumentBuilder2>& rDocBuilder) :
      70             :     SvXMLImportContext( rImport, nPrfx, rLName ),
      71        6198 :     mxDocBuilder(rDocBuilder)
      72             : {
      73        6198 : }
      74             : 
      75       12396 : XMLDocumentBuilderContext::~XMLDocumentBuilderContext()
      76             : {
      77       12396 : }
      78             : 
      79             : SvXMLImportContext *
      80        5340 : XMLDocumentBuilderContext::CreateChildContext( sal_uInt16 nPrefix,
      81             :     const OUString& rLocalName,
      82             :     const uno::Reference< xml::sax::XAttributeList>& rAttrs)
      83             : {
      84             :     return new XMLDocumentBuilderContext(
      85        5340 :                 GetImport(), nPrefix, rLocalName, rAttrs, mxDocBuilder);
      86             : }
      87             : 
      88        6198 : void XMLDocumentBuilderContext::StartElement(
      89             :     const uno::Reference< xml::sax::XAttributeList >& xAttrList )
      90             : {
      91        6198 :     mxDocBuilder->startElement(
      92       12396 :       GetImport().GetNamespaceMap().GetQNameByKey(GetPrefix(), GetLocalName()),
      93       18594 :       xAttrList);
      94        6198 : }
      95             : 
      96        4586 : void XMLDocumentBuilderContext::Characters( const OUString& rChars )
      97             : {
      98        4586 :     mxDocBuilder->characters(rChars);
      99        4586 : }
     100             : 
     101        6198 : void XMLDocumentBuilderContext::EndElement()
     102             : {
     103        6198 :     mxDocBuilder->endElement(
     104        6198 :       GetImport().GetNamespaceMap().GetQNameByKey(GetPrefix(), GetLocalName()));
     105        6198 : }
     106             : 
     107             : static void
     108         982 : lcl_initDocumentProperties(SvXMLImport & rImport,
     109             :         uno::Reference<xml::dom::XSAXDocumentBuilder2> const& xDocBuilder,
     110             :         uno::Reference<document::XDocumentProperties> const& xDocProps)
     111             : {
     112         982 :     uno::Sequence< uno::Any > aSeq(1);
     113         982 :     aSeq[0] <<= xDocBuilder->getDocument();
     114             :     uno::Reference< lang::XInitialization > const xInit(xDocProps,
     115        1964 :         uno::UNO_QUERY_THROW);
     116             :     try {
     117         982 :         xInit->initialize(aSeq);
     118         982 :         rImport.SetStatistics(xDocProps->getDocumentStatistics());
     119             :         // convert all URLs from relative to absolute
     120         982 :         xDocProps->setTemplateURL(rImport.GetAbsoluteReference(
     121         982 :             xDocProps->getTemplateURL()));
     122         982 :         xDocProps->setAutoloadURL(rImport.GetAbsoluteReference(
     123         982 :             xDocProps->getAutoloadURL()));
     124             :         SvXMLMetaDocumentContext::setBuildId(
     125         982 :             xDocProps->getGenerator(), rImport.getImportInfo());
     126           0 :     } catch (const uno::RuntimeException&) {
     127           0 :         throw;
     128           0 :     } catch (const uno::Exception& e) {
     129             :         throw lang::WrappedTargetRuntimeException(
     130             :             OUString(
     131             :                 "SvXMLMetaDocumentContext::initDocumentProperties: "
     132             :                 "properties init exception"),
     133           0 :             rImport, makeAny(e));
     134         982 :     }
     135         982 : }
     136             : 
     137             : static void
     138         110 : lcl_initGenerator(SvXMLImport & rImport,
     139             :         uno::Reference<xml::dom::XSAXDocumentBuilder2> const& xDocBuilder)
     140             : {
     141         220 :     uno::Reference< xml::dom::XDocument > const xDoc(xDocBuilder->getDocument(),
     142         220 :         uno::UNO_SET_THROW);
     143             :     try {
     144             :         uno::Reference< xml::xpath::XXPathAPI > const xPath = xml::xpath::XPathAPI::create(
     145         110 :             rImport.GetComponentContext() );
     146         110 :         xPath->registerNS(GetXMLToken(XML_NP_OFFICE),GetXMLToken(XML_N_OFFICE));
     147         110 :         xPath->registerNS(GetXMLToken(XML_NP_META), GetXMLToken(XML_N_META));
     148             : 
     149         220 :         OUString const expr( "string(/office:document-meta/office:meta/meta:generator)");
     150             :         uno::Reference< xml::xpath::XXPathObject > const xObj(
     151         220 :             xPath->eval(xDoc.get(), expr), uno::UNO_SET_THROW);
     152         220 :         OUString const value(xObj->getString());
     153         220 :         SvXMLMetaDocumentContext::setBuildId(value, rImport.getImportInfo());
     154           0 :     } catch (const uno::RuntimeException&) {
     155           0 :         throw;
     156           0 :     } catch (const uno::Exception& e) {
     157             :         throw lang::WrappedTargetRuntimeException(
     158             :             OUString(
     159             :                                 "SvXMLMetaDocumentContext::initGenerator: exception"),
     160           0 :             rImport, makeAny(e));
     161         110 :     }
     162         110 : }
     163             : 
     164        1092 : SvXMLMetaDocumentContext::SvXMLMetaDocumentContext(SvXMLImport& rImport,
     165             :             sal_uInt16 nPrfx, const OUString& rLName,
     166             :             const uno::Reference<document::XDocumentProperties>& xDocProps) :
     167             :     SvXMLImportContext( rImport, nPrfx, rLName ),
     168             :     mxDocProps(xDocProps),
     169             :     mxDocBuilder(
     170             :         xml::dom::SAXDocumentBuilder::create(
     171        1092 :             comphelper::getProcessComponentContext()))
     172             : {
     173             : // #i103539#: must always read meta.xml for generator, xDocProps unwanted then
     174             : //    OSL_ENSURE(xDocProps.is(), "SvXMLMetaDocumentContext: no document props");
     175        1092 : }
     176             : 
     177        1940 : SvXMLMetaDocumentContext::~SvXMLMetaDocumentContext()
     178             : {
     179        1940 : }
     180             : 
     181         858 : SvXMLImportContext *SvXMLMetaDocumentContext::CreateChildContext(
     182             :              sal_uInt16 nPrefix, const OUString& rLocalName,
     183             :              const uno::Reference<xml::sax::XAttributeList>& rAttrs)
     184             : {
     185        1716 :     if (  (XML_NAMESPACE_OFFICE == nPrefix) &&
     186         858 :          IsXMLToken(rLocalName, XML_META) )
     187             :     {
     188             :         return new XMLDocumentBuilderContext(
     189         858 :                 GetImport(), nPrefix, rLocalName, rAttrs, mxDocBuilder);
     190             :     }
     191             :     else
     192             :     {
     193           0 :         return new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
     194             :     }
     195             : }
     196             : 
     197        1092 : void SvXMLMetaDocumentContext::StartElement(
     198             :     const uno::Reference< xml::sax::XAttributeList >& xAttrList )
     199             : {
     200        1092 :     mxDocBuilder->startDocument();
     201             :     // hardcode office:document-meta (necessary in case of flat file ODF)
     202        1092 :     mxDocBuilder->startElement(
     203        2184 :         GetImport().GetNamespaceMap().GetQNameByKey(GetPrefix(),
     204        3276 :             GetXMLToken(XML_DOCUMENT_META)), xAttrList);
     205             : 
     206        1092 : }
     207             : 
     208        1092 : void SvXMLMetaDocumentContext::EndElement()
     209             : {
     210             :     // hardcode office:document-meta (necessary in case of flat file ODF)
     211        1092 :     mxDocBuilder->endElement(
     212        2184 :         GetImport().GetNamespaceMap().GetQNameByKey(GetPrefix(),
     213        3276 :             GetXMLToken(XML_DOCUMENT_META)));
     214        1092 :     mxDocBuilder->endDocument();
     215        1092 :     if (mxDocProps.is())
     216             :     {
     217         982 :         lcl_initDocumentProperties(GetImport(), mxDocBuilder, mxDocProps);
     218             :     }
     219             :     else
     220             :     {
     221         110 :         lcl_initGenerator(GetImport(), mxDocBuilder);
     222             :     }
     223        1092 : }
     224             : 
     225        1328 : void SvXMLMetaDocumentContext::setBuildId(OUString const& i_rBuildId, const uno::Reference<beans::XPropertySet>& xImportInfo )
     226             : {
     227        1328 :     OUString sBuildId;
     228             :     // skip to second product
     229        1328 :     sal_Int32 nBegin = i_rBuildId.indexOf( ' ' );
     230        1328 :     if ( nBegin != -1 )
     231             :     {
     232             :         // skip to build information
     233        1086 :         nBegin = i_rBuildId.indexOf( '/', nBegin );
     234        1086 :         if ( nBegin != -1 )
     235             :         {
     236        1070 :             sal_Int32 nEnd = i_rBuildId.indexOf( 'm', nBegin );
     237        1070 :             if ( nEnd != -1 )
     238             :             {
     239             :                 OUStringBuffer sBuffer(
     240         150 :                     i_rBuildId.copy( nBegin+1, nEnd-nBegin-1 ) );
     241             :                 const OUString sBuildCompare(
     242         300 :                      "$Build-"  );
     243         150 :                 nBegin = i_rBuildId.indexOf( sBuildCompare, nEnd );
     244         150 :                 if ( nBegin != -1 )
     245             :                 {
     246         150 :                     sBuffer.append( '$' );
     247             :                     sBuffer.append( i_rBuildId.copy(
     248         150 :                         nBegin + sBuildCompare.getLength() ) );
     249         150 :                     sBuildId = sBuffer.makeStringAndClear();
     250         150 :                 }
     251             :             }
     252             :         }
     253             :     }
     254             : 
     255        1328 :     if ( sBuildId.isEmpty() )
     256             :     {
     257        2356 :         if (    i_rBuildId.startsWith("StarOffice 7")
     258        1178 :             ||  i_rBuildId.startsWith("StarSuite 7")
     259        2356 :             ||  i_rBuildId.startsWith("OpenOffice.org 1"))
     260             :         {
     261           0 :             sBuildId = "645$8687";
     262             :         }
     263        1178 :         else if (i_rBuildId.startsWith("NeoOffice/2"))
     264             :         {
     265           0 :             sBuildId = "680$9134"; // fake NeoOffice as OpenOffice.org 2.2 release
     266             :         }
     267             :     }
     268             : 
     269        2656 :     OUString rest;
     270        3560 :     if (i_rBuildId.startsWith("LibreOffice/", &rest) ||
     271        1630 :         i_rBuildId.startsWith("LibreOfficeDev/", &rest) ||
     272         302 :         i_rBuildId.startsWith("LOdev/", &rest))
     273             :     {
     274        1026 :         OUStringBuffer sNumber;
     275        8412 :         for (sal_Int32 i = 0; i < rest.getLength(); ++i)
     276             :         {
     277        8412 :             if (isdigit(rest[i]))
     278             :             {
     279        3828 :                 sNumber.append(rest[i]);
     280             :             }
     281        4584 :             else if ('.' != rest[i])
     282             :             {
     283        1026 :                 break;
     284             :             }
     285             :         }
     286        1026 :         if (!sNumber.isEmpty())
     287             :         {
     288        1026 :             sBuildId += (";" + sNumber.makeStringAndClear());
     289        1026 :         }
     290             :     }
     291             : 
     292        1328 :     if ( !sBuildId.isEmpty() ) try
     293             :     {
     294        1068 :         if( xImportInfo.is() )
     295             :         {
     296        1066 :             const OUString aPropName("BuildId");
     297             :             uno::Reference< beans::XPropertySetInfo > xSetInfo(
     298        2132 :                 xImportInfo->getPropertySetInfo());
     299        1066 :             if( xSetInfo.is() && xSetInfo->hasPropertyByName( aPropName ) )
     300        2104 :                 xImportInfo->setPropertyValue( aPropName, uno::makeAny( sBuildId ) );
     301             :         }
     302             :     }
     303           0 :     catch(const uno::Exception&)
     304             :     {
     305        1328 :     }
     306        1328 : }
     307             : 
     308             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10