LCOV - code coverage report
Current view: top level - comphelper/source/xml - ofopxmlhelper.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 216 0.0 %
Date: 2014-04-14 Functions: 0 17 0.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             : 
      21             : #include <com/sun/star/beans/StringPair.hpp>
      22             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      23             : #include <com/sun/star/io/XActiveDataSource.hpp>
      24             : #include <com/sun/star/xml/sax/Parser.hpp>
      25             : #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
      26             : #include <com/sun/star/xml/sax/Writer.hpp>
      27             : #include <com/sun/star/lang/IllegalArgumentException.hpp>
      28             : 
      29             : #include <comphelper/ofopxmlhelper.hxx>
      30             : #include <comphelper/attributelist.hxx>
      31             : 
      32             : #define RELATIONINFO_FORMAT 0
      33             : #define CONTENTTYPE_FORMAT  1
      34             : #define FORMAT_MAX_ID CONTENTTYPE_FORMAT
      35             : 
      36             : using namespace ::com::sun::star;
      37             : 
      38             : namespace comphelper {
      39             : 
      40             : 
      41           0 : uno::Sequence< uno::Sequence< beans::StringPair > > SAL_CALL OFOPXMLHelper::ReadRelationsInfoSequence( const uno::Reference< io::XInputStream >& xInStream, const OUString & aStreamName, const uno::Reference< uno::XComponentContext > xContext )
      42             :     throw( uno::Exception )
      43             : {
      44           0 :     OUString aStringID = "_rels/";
      45           0 :     aStringID += aStreamName;
      46           0 :     return ReadSequence_Impl( xInStream, aStringID, RELATIONINFO_FORMAT, xContext );
      47             : }
      48             : 
      49             : 
      50           0 : uno::Sequence< uno::Sequence< beans::StringPair > > SAL_CALL OFOPXMLHelper::ReadContentTypeSequence( const uno::Reference< io::XInputStream >& xInStream, const uno::Reference< uno::XComponentContext > xContext )
      51             :     throw( uno::Exception )
      52             : {
      53           0 :     OUString aStringID = "[Content_Types].xml";
      54           0 :     return ReadSequence_Impl( xInStream, aStringID, CONTENTTYPE_FORMAT, xContext );
      55             : }
      56             : 
      57             : 
      58           0 : void SAL_CALL OFOPXMLHelper::WriteRelationsInfoSequence( const uno::Reference< io::XOutputStream >& xOutStream, const uno::Sequence< uno::Sequence< beans::StringPair > >& aSequence, const uno::Reference< uno::XComponentContext > xContext )
      59             :     throw( uno::Exception )
      60             : {
      61           0 :     if ( !xOutStream.is() )
      62           0 :         throw uno::RuntimeException();
      63             : 
      64           0 :     uno::Reference< xml::sax::XWriter > xWriter = xml::sax::Writer::create(xContext);
      65             : 
      66           0 :     xWriter->setOutputStream( xOutStream );
      67             : 
      68           0 :     OUString aRelListElement( "Relationships" );
      69           0 :     OUString aRelElement( "Relationship" );
      70           0 :     OUString aIDAttr( "Id" );
      71           0 :     OUString aTypeAttr( "Type" );
      72           0 :     OUString aTargetModeAttr( "TargetMode" );
      73           0 :     OUString aTargetAttr( "Target" );
      74           0 :     OUString aCDATAString( "CDATA" );
      75           0 :     OUString aWhiteSpace( " " );
      76             : 
      77             :     // write the namespace
      78           0 :     AttributeList* pRootAttrList = new AttributeList;
      79           0 :     uno::Reference< xml::sax::XAttributeList > xRootAttrList( pRootAttrList );
      80             :     pRootAttrList->AddAttribute(
      81             :         OUString( "xmlns" ),
      82             :         aCDATAString,
      83           0 :         OUString( "http://schemas.openxmlformats.org/package/2006/relationships" ) );
      84             : 
      85           0 :     xWriter->startDocument();
      86           0 :     xWriter->startElement( aRelListElement, xRootAttrList );
      87             : 
      88           0 :     for ( sal_Int32 nInd = 0; nInd < aSequence.getLength(); nInd++ )
      89             :     {
      90           0 :         AttributeList *pAttrList = new AttributeList;
      91           0 :         uno::Reference< xml::sax::XAttributeList > xAttrList( pAttrList );
      92           0 :         for( sal_Int32 nSecInd = 0; nSecInd < aSequence[nInd].getLength(); nSecInd++ )
      93             :         {
      94           0 :             if ( aSequence[nInd][nSecInd].First.equals( aIDAttr )
      95           0 :               || aSequence[nInd][nSecInd].First.equals( aTypeAttr )
      96           0 :               || aSequence[nInd][nSecInd].First.equals( aTargetModeAttr )
      97           0 :               || aSequence[nInd][nSecInd].First.equals( aTargetAttr ) )
      98             :             {
      99           0 :                 pAttrList->AddAttribute( aSequence[nInd][nSecInd].First, aCDATAString, aSequence[nInd][nSecInd].Second );
     100             :             }
     101             :             else
     102             :             {
     103             :                 // TODO/LATER: should the extensions be allowed?
     104           0 :                 throw lang::IllegalArgumentException();
     105             :             }
     106             :         }
     107             : 
     108           0 :         xWriter->startElement( aRelElement, xAttrList );
     109           0 :         xWriter->ignorableWhitespace( aWhiteSpace );
     110           0 :         xWriter->endElement( aRelElement );
     111           0 :     }
     112             : 
     113           0 :     xWriter->ignorableWhitespace( aWhiteSpace );
     114           0 :     xWriter->endElement( aRelListElement );
     115           0 :     xWriter->endDocument();
     116           0 : }
     117             : 
     118             : 
     119           0 : void SAL_CALL OFOPXMLHelper::WriteContentSequence( const uno::Reference< io::XOutputStream >& xOutStream, const uno::Sequence< beans::StringPair >& aDefaultsSequence, const uno::Sequence< beans::StringPair >& aOverridesSequence, const uno::Reference< uno::XComponentContext > xContext )
     120             :     throw( uno::Exception )
     121             : {
     122           0 :     if ( !xOutStream.is() )
     123           0 :         throw uno::RuntimeException();
     124             : 
     125           0 :     uno::Reference< xml::sax::XWriter > xWriter = xml::sax::Writer::create(xContext);
     126             : 
     127           0 :     xWriter->setOutputStream( xOutStream );
     128             : 
     129           0 :     OUString aTypesElement( "Types" );
     130           0 :     OUString aDefaultElement( "Default" );
     131           0 :     OUString aOverrideElement( "Override" );
     132           0 :     OUString aExtensionAttr( "Extension" );
     133           0 :     OUString aPartNameAttr( "PartName" );
     134           0 :     OUString aContentTypeAttr( "ContentType" );
     135           0 :     OUString aCDATAString( "CDATA" );
     136           0 :     OUString aWhiteSpace( " " );
     137             : 
     138             :     // write the namespace
     139           0 :     AttributeList* pRootAttrList = new AttributeList;
     140           0 :     uno::Reference< xml::sax::XAttributeList > xRootAttrList( pRootAttrList );
     141             :     pRootAttrList->AddAttribute(
     142             :         OUString( "xmlns" ),
     143             :         aCDATAString,
     144           0 :         OUString( "http://schemas.openxmlformats.org/package/2006/content-types" ) );
     145             : 
     146           0 :     xWriter->startDocument();
     147           0 :     xWriter->startElement( aTypesElement, xRootAttrList );
     148             : 
     149           0 :     for ( sal_Int32 nInd = 0; nInd < aDefaultsSequence.getLength(); nInd++ )
     150             :     {
     151           0 :         AttributeList *pAttrList = new AttributeList;
     152           0 :         uno::Reference< xml::sax::XAttributeList > xAttrList( pAttrList );
     153           0 :         pAttrList->AddAttribute( aExtensionAttr, aCDATAString, aDefaultsSequence[nInd].First );
     154           0 :         pAttrList->AddAttribute( aContentTypeAttr, aCDATAString, aDefaultsSequence[nInd].Second );
     155             : 
     156           0 :         xWriter->startElement( aDefaultElement, xAttrList );
     157           0 :         xWriter->ignorableWhitespace( aWhiteSpace );
     158           0 :         xWriter->endElement( aDefaultElement );
     159           0 :     }
     160             : 
     161           0 :     for ( sal_Int32 nInd = 0; nInd < aOverridesSequence.getLength(); nInd++ )
     162             :     {
     163           0 :         AttributeList *pAttrList = new AttributeList;
     164           0 :         uno::Reference< xml::sax::XAttributeList > xAttrList( pAttrList );
     165           0 :         pAttrList->AddAttribute( aPartNameAttr, aCDATAString, aOverridesSequence[nInd].First );
     166           0 :         pAttrList->AddAttribute( aContentTypeAttr, aCDATAString, aOverridesSequence[nInd].Second );
     167             : 
     168           0 :         xWriter->startElement( aOverrideElement, xAttrList );
     169           0 :         xWriter->ignorableWhitespace( aWhiteSpace );
     170           0 :         xWriter->endElement( aOverrideElement );
     171           0 :     }
     172             : 
     173           0 :     xWriter->ignorableWhitespace( aWhiteSpace );
     174           0 :     xWriter->endElement( aTypesElement );
     175           0 :     xWriter->endDocument();
     176             : 
     177           0 : }
     178             : 
     179             : 
     180             : 
     181             : 
     182           0 : uno::Sequence< uno::Sequence< beans::StringPair > > SAL_CALL OFOPXMLHelper::ReadSequence_Impl( const uno::Reference< io::XInputStream >& xInStream, const OUString& aStringID, sal_uInt16 nFormat, const uno::Reference< uno::XComponentContext > xContext )
     183             :     throw( uno::Exception )
     184             : {
     185           0 :     if ( !xContext.is() || !xInStream.is() || nFormat > FORMAT_MAX_ID )
     186           0 :         throw uno::RuntimeException();
     187             : 
     188           0 :     uno::Reference< xml::sax::XParser > xParser = xml::sax::Parser::create( xContext );
     189             : 
     190           0 :     OFOPXMLHelper* pHelper = new OFOPXMLHelper( nFormat );
     191           0 :     uno::Reference< xml::sax::XDocumentHandler > xHelper( static_cast< xml::sax::XDocumentHandler* >( pHelper ) );
     192           0 :     xml::sax::InputSource aParserInput;
     193           0 :     aParserInput.aInputStream = xInStream;
     194           0 :     aParserInput.sSystemId = aStringID;
     195           0 :     xParser->setDocumentHandler( xHelper );
     196           0 :     xParser->parseStream( aParserInput );
     197           0 :     xParser->setDocumentHandler( uno::Reference < xml::sax::XDocumentHandler > () );
     198             : 
     199           0 :     return pHelper->GetParsingResult();
     200             : }
     201             : 
     202             : 
     203           0 : OFOPXMLHelper::OFOPXMLHelper( sal_uInt16 nFormat )
     204             : : m_nFormat( nFormat )
     205             : , m_aRelListElement( "Relationships" )
     206             : , m_aRelElement( "Relationship" )
     207             : , m_aIDAttr( "Id" )
     208             : , m_aTypeAttr( "Type" )
     209             : , m_aTargetModeAttr( "TargetMode" )
     210             : , m_aTargetAttr( "Target" )
     211             : , m_aTypesElement( "Types" )
     212             : , m_aDefaultElement( "Default" )
     213             : , m_aOverrideElement( "Override" )
     214             : , m_aExtensionAttr( "Extension" )
     215             : , m_aPartNameAttr( "PartName" )
     216           0 : , m_aContentTypeAttr( "ContentType" )
     217             : {
     218           0 : }
     219             : 
     220             : 
     221           0 : OFOPXMLHelper::~OFOPXMLHelper()
     222             : {
     223           0 : }
     224             : 
     225             : 
     226           0 : uno::Sequence< uno::Sequence< beans::StringPair > > OFOPXMLHelper::GetParsingResult()
     227             : {
     228           0 :     if ( m_aElementsSeq.getLength() )
     229           0 :         throw uno::RuntimeException(); // the parsing has still not finished!
     230             : 
     231           0 :     return m_aResultSeq;
     232             : }
     233             : 
     234             : 
     235           0 : void SAL_CALL OFOPXMLHelper::startDocument()
     236             :         throw(xml::sax::SAXException, uno::RuntimeException, std::exception)
     237             : {
     238           0 : }
     239             : 
     240             : 
     241           0 : void SAL_CALL OFOPXMLHelper::endDocument()
     242             :         throw(xml::sax::SAXException, uno::RuntimeException, std::exception)
     243             : {
     244           0 : }
     245             : 
     246             : 
     247           0 : void SAL_CALL OFOPXMLHelper::startElement( const OUString& aName, const uno::Reference< xml::sax::XAttributeList >& xAttribs )
     248             :         throw( xml::sax::SAXException, uno::RuntimeException, std::exception )
     249             : {
     250           0 :     if ( m_nFormat == RELATIONINFO_FORMAT )
     251             :     {
     252           0 :         if ( aName == m_aRelListElement )
     253             :         {
     254           0 :             sal_Int32 nNewLength = m_aElementsSeq.getLength() + 1;
     255             : 
     256           0 :             if ( nNewLength != 1 )
     257           0 :                 throw xml::sax::SAXException(); // TODO: this element must be the first level element
     258             : 
     259           0 :             m_aElementsSeq.realloc( nNewLength );
     260           0 :             m_aElementsSeq[nNewLength-1] = aName;
     261             : 
     262           0 :             return; // nothing to do
     263             :         }
     264           0 :         else if ( aName == m_aRelElement )
     265             :         {
     266           0 :             sal_Int32 nNewLength = m_aElementsSeq.getLength() + 1;
     267           0 :             if ( nNewLength != 2 )
     268           0 :                 throw xml::sax::SAXException(); // TODO: this element must be the second level element
     269             : 
     270           0 :             m_aElementsSeq.realloc( nNewLength );
     271           0 :             m_aElementsSeq[nNewLength-1] = aName;
     272             : 
     273           0 :             sal_Int32 nNewEntryNum = m_aResultSeq.getLength() + 1;
     274           0 :             m_aResultSeq.realloc( nNewEntryNum );
     275           0 :             sal_Int32 nAttrNum = 0;
     276           0 :             m_aResultSeq[nNewEntryNum-1].realloc( 4 ); // the maximal expected number of arguments is 4
     277             : 
     278           0 :             OUString aIDValue = xAttribs->getValueByName( m_aIDAttr );
     279           0 :             if ( aIDValue.isEmpty() )
     280           0 :                 throw xml::sax::SAXException(); // TODO: the ID value must present
     281             : 
     282           0 :             OUString aTypeValue = xAttribs->getValueByName( m_aTypeAttr );
     283           0 :             OUString aTargetValue = xAttribs->getValueByName( m_aTargetAttr );
     284           0 :             OUString aTargetModeValue = xAttribs->getValueByName( m_aTargetModeAttr );
     285             : 
     286           0 :             m_aResultSeq[nNewEntryNum-1][++nAttrNum - 1].First = m_aIDAttr;
     287           0 :             m_aResultSeq[nNewEntryNum-1][nAttrNum - 1].Second = aIDValue;
     288             : 
     289           0 :             if ( !aTypeValue.isEmpty() )
     290             :             {
     291           0 :                 m_aResultSeq[nNewEntryNum-1][++nAttrNum - 1].First = m_aTypeAttr;
     292           0 :                 m_aResultSeq[nNewEntryNum-1][nAttrNum - 1].Second = aTypeValue;
     293             :             }
     294             : 
     295           0 :             if ( !aTargetValue.isEmpty() )
     296             :             {
     297           0 :                 m_aResultSeq[nNewEntryNum-1][++nAttrNum - 1].First = m_aTargetAttr;
     298           0 :                 m_aResultSeq[nNewEntryNum-1][nAttrNum - 1].Second = aTargetValue;
     299             :             }
     300             : 
     301           0 :             if ( !aTargetModeValue.isEmpty() )
     302             :             {
     303           0 :                 m_aResultSeq[nNewEntryNum-1][++nAttrNum - 1].First = m_aTargetModeAttr;
     304           0 :                 m_aResultSeq[nNewEntryNum-1][nAttrNum - 1].Second = aTargetModeValue;
     305             :             }
     306             : 
     307           0 :             m_aResultSeq[nNewEntryNum-1].realloc( nAttrNum );
     308             :         }
     309             :         else
     310           0 :             throw xml::sax::SAXException(); // TODO: no other elements expected!
     311             :     }
     312           0 :     else if ( m_nFormat == CONTENTTYPE_FORMAT )
     313             :     {
     314           0 :         if ( aName == m_aTypesElement )
     315             :         {
     316           0 :             sal_Int32 nNewLength = m_aElementsSeq.getLength() + 1;
     317             : 
     318           0 :             if ( nNewLength != 1 )
     319           0 :                 throw xml::sax::SAXException(); // TODO: this element must be the first level element
     320             : 
     321           0 :             m_aElementsSeq.realloc( nNewLength );
     322           0 :             m_aElementsSeq[nNewLength-1] = aName;
     323             : 
     324           0 :             if ( !m_aResultSeq.getLength() )
     325           0 :                 m_aResultSeq.realloc( 2 );
     326             : 
     327           0 :             return; // nothing to do
     328             :         }
     329           0 :         else if ( aName == m_aDefaultElement )
     330             :         {
     331           0 :             sal_Int32 nNewLength = m_aElementsSeq.getLength() + 1;
     332           0 :             if ( nNewLength != 2 )
     333           0 :                 throw xml::sax::SAXException(); // TODO: this element must be the second level element
     334             : 
     335           0 :             m_aElementsSeq.realloc( nNewLength );
     336           0 :             m_aElementsSeq[nNewLength-1] = aName;
     337             : 
     338           0 :             if ( !m_aResultSeq.getLength() )
     339           0 :                 m_aResultSeq.realloc( 2 );
     340             : 
     341           0 :             if ( m_aResultSeq.getLength() != 2 )
     342           0 :                 throw uno::RuntimeException();
     343             : 
     344           0 :             OUString aExtensionValue = xAttribs->getValueByName( m_aExtensionAttr );
     345           0 :             if ( aExtensionValue.isEmpty() )
     346           0 :                 throw xml::sax::SAXException(); // TODO: the Extension value must present
     347             : 
     348           0 :             OUString aContentTypeValue = xAttribs->getValueByName( m_aContentTypeAttr );
     349           0 :             if ( aContentTypeValue.isEmpty() )
     350           0 :                 throw xml::sax::SAXException(); // TODO: the ContentType value must present
     351             : 
     352           0 :             sal_Int32 nNewResultLen = m_aResultSeq[0].getLength() + 1;
     353           0 :             m_aResultSeq[0].realloc( nNewResultLen );
     354             : 
     355           0 :             m_aResultSeq[0][nNewResultLen-1].First = aExtensionValue;
     356           0 :             m_aResultSeq[0][nNewResultLen-1].Second = aContentTypeValue;
     357             :         }
     358           0 :         else if ( aName == m_aOverrideElement )
     359             :         {
     360           0 :             sal_Int32 nNewLength = m_aElementsSeq.getLength() + 1;
     361           0 :             if ( nNewLength != 2 )
     362           0 :                 throw xml::sax::SAXException(); // TODO: this element must be the second level element
     363             : 
     364           0 :             m_aElementsSeq.realloc( nNewLength );
     365           0 :             m_aElementsSeq[nNewLength-1] = aName;
     366             : 
     367           0 :             if ( !m_aResultSeq.getLength() )
     368           0 :                 m_aResultSeq.realloc( 2 );
     369             : 
     370           0 :             if ( m_aResultSeq.getLength() != 2 )
     371           0 :                 throw uno::RuntimeException();
     372             : 
     373           0 :             OUString aPartNameValue = xAttribs->getValueByName( m_aPartNameAttr );
     374           0 :             if ( aPartNameValue.isEmpty() )
     375           0 :                 throw xml::sax::SAXException(); // TODO: the PartName value must present
     376             : 
     377           0 :             OUString aContentTypeValue = xAttribs->getValueByName( m_aContentTypeAttr );
     378           0 :             if ( aContentTypeValue.isEmpty() )
     379           0 :                 throw xml::sax::SAXException(); // TODO: the ContentType value must present
     380             : 
     381           0 :             sal_Int32 nNewResultLen = m_aResultSeq[1].getLength() + 1;
     382           0 :             m_aResultSeq[1].realloc( nNewResultLen );
     383             : 
     384           0 :             m_aResultSeq[1][nNewResultLen-1].First = aPartNameValue;
     385           0 :             m_aResultSeq[1][nNewResultLen-1].Second = aContentTypeValue;
     386             :         }
     387             :         else
     388           0 :             throw xml::sax::SAXException(); // TODO: no other elements expected!
     389             :     }
     390             :     else
     391           0 :         throw xml::sax::SAXException(); // TODO: no other elements expected!
     392             : }
     393             : 
     394             : 
     395           0 : void SAL_CALL OFOPXMLHelper::endElement( const OUString& aName )
     396             :     throw( xml::sax::SAXException, uno::RuntimeException, std::exception )
     397             : {
     398           0 :     if ( m_nFormat == RELATIONINFO_FORMAT || m_nFormat == CONTENTTYPE_FORMAT )
     399             :     {
     400           0 :         sal_Int32 nLength = m_aElementsSeq.getLength();
     401           0 :         if ( nLength <= 0 )
     402           0 :             throw xml::sax::SAXException(); // TODO: no other end elements expected!
     403             : 
     404           0 :         if ( !m_aElementsSeq[nLength-1].equals( aName ) )
     405           0 :             throw xml::sax::SAXException(); // TODO: unexpected element ended
     406             : 
     407           0 :         m_aElementsSeq.realloc( nLength - 1 );
     408             :     }
     409           0 : }
     410             : 
     411             : 
     412           0 : void SAL_CALL OFOPXMLHelper::characters( const OUString& /*aChars*/ )
     413             :         throw(xml::sax::SAXException, uno::RuntimeException, std::exception)
     414             : {
     415           0 : }
     416             : 
     417             : 
     418           0 : void SAL_CALL OFOPXMLHelper::ignorableWhitespace( const OUString& /*aWhitespaces*/ )
     419             :         throw(xml::sax::SAXException, uno::RuntimeException, std::exception)
     420             : {
     421           0 : }
     422             : 
     423             : 
     424           0 : void SAL_CALL OFOPXMLHelper::processingInstruction( const OUString& /*aTarget*/, const OUString& /*aData*/ )
     425             :         throw(xml::sax::SAXException, uno::RuntimeException, std::exception)
     426             : {
     427           0 : }
     428             : 
     429             : 
     430           0 : void SAL_CALL OFOPXMLHelper::setDocumentLocator( const uno::Reference< xml::sax::XLocator >& /*xLocator*/ )
     431             :         throw(xml::sax::SAXException, uno::RuntimeException, std::exception)
     432             : {
     433           0 : }
     434             : 
     435             : } // namespace comphelper
     436             : 
     437             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10