LCOV - code coverage report
Current view: top level - xmloff/source/core - DomExport.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 117 0.0 %
Date: 2014-04-14 Functions: 0 34 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 "DomExport.hxx"
      22             : 
      23             : #include <xmloff/nmspmap.hxx>
      24             : #include <xmloff/xmlexp.hxx>
      25             : #include <xmloff/xmlerror.hxx>
      26             : 
      27             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      28             : #include <com/sun/star/uno/Reference.hxx>
      29             : #include <com/sun/star/uno/Sequence.hxx>
      30             : #include <com/sun/star/xml/dom/XAttr.hpp>
      31             : #include <com/sun/star/xml/dom/XDocumentBuilder.hpp>
      32             : #include <com/sun/star/xml/dom/XNode.hpp>
      33             : #include <com/sun/star/xml/dom/XElement.hpp>
      34             : #include <com/sun/star/xml/dom/XEntity.hpp>
      35             : #include <com/sun/star/xml/dom/XNotation.hpp>
      36             : #include <com/sun/star/xml/sax/XAttributeList.hpp>
      37             : #include <com/sun/star/xml/dom/NodeType.hpp>
      38             : #include <com/sun/star/xml/dom/XNamedNodeMap.hpp>
      39             : 
      40             : #include <rtl/ustring.hxx>
      41             : #include <rtl/ustrbuf.hxx>
      42             : #include <tools/debug.hxx>
      43             : 
      44             : 
      45             : #include <vector>
      46             : 
      47             : 
      48             : using com::sun::star::lang::XMultiServiceFactory;
      49             : using com::sun::star::uno::Reference;
      50             : using com::sun::star::uno::Sequence;
      51             : using com::sun::star::uno::UNO_QUERY;
      52             : using com::sun::star::uno::UNO_QUERY_THROW;
      53             : using std::vector;
      54             : 
      55             : using namespace com::sun::star::xml::dom;
      56             : 
      57             : 
      58             : 
      59             : class DomVisitor
      60             : {
      61             : public:
      62           0 :     DomVisitor() {}
      63           0 :     virtual ~DomVisitor() {}
      64           0 :     virtual void element( const Reference<XElement>& ) {}
      65           0 :     virtual void character( const Reference<XCharacterData>& ) {}
      66           0 :     virtual void attribute( const Reference<XAttr>& ) {}
      67           0 :     virtual void cdata( const Reference<XCDATASection>& ) {}
      68           0 :     virtual void comment( const Reference<XComment>& ) {}
      69           0 :     virtual void documentFragment( const Reference<XDocumentFragment>& ) {}
      70           0 :     virtual void document( const Reference<XDocument>& ) {}
      71           0 :     virtual void documentType( const Reference<XDocumentType>& ) {}
      72           0 :     virtual void entity( const Reference<XEntity>& ) {}
      73           0 :     virtual void entityReference( const Reference<XEntityReference>& ) {}
      74           0 :     virtual void notation( const Reference<XNotation>& ) {}
      75           0 :     virtual void processingInstruction( const Reference<XProcessingInstruction>& ) {}
      76           0 :     virtual void endElement( const Reference<XElement>& ) {}
      77             : };
      78             : 
      79             : void visit( DomVisitor&, const Reference<XDocument>& );
      80             : void visit( DomVisitor&, const Reference<XNode>& );
      81             : 
      82             : 
      83             : 
      84           0 : void visitNode( DomVisitor& rVisitor, const Reference<XNode>& xNode )
      85             : {
      86           0 :     switch( xNode->getNodeType() )
      87             :     {
      88             :     case NodeType_ATTRIBUTE_NODE:
      89           0 :         rVisitor.attribute( Reference<XAttr>( xNode, UNO_QUERY_THROW ) );
      90           0 :         break;
      91             :     case NodeType_CDATA_SECTION_NODE:
      92           0 :         rVisitor.cdata( Reference<XCDATASection>( xNode, UNO_QUERY_THROW ) );
      93           0 :         break;
      94             :     case NodeType_COMMENT_NODE:
      95           0 :         rVisitor.comment( Reference<XComment>( xNode, UNO_QUERY_THROW ) );
      96           0 :         break;
      97             :     case NodeType_DOCUMENT_FRAGMENT_NODE:
      98           0 :         rVisitor.documentFragment( Reference<XDocumentFragment>( xNode, UNO_QUERY_THROW ) );
      99           0 :         break;
     100             :     case NodeType_DOCUMENT_NODE:
     101           0 :         rVisitor.document( Reference<XDocument>( xNode, UNO_QUERY_THROW ) );
     102           0 :         break;
     103             :     case NodeType_DOCUMENT_TYPE_NODE:
     104           0 :         rVisitor.documentType( Reference<XDocumentType>( xNode, UNO_QUERY_THROW ) );
     105           0 :         break;
     106             :     case NodeType_ELEMENT_NODE:
     107           0 :         rVisitor.element( Reference<XElement>( xNode, UNO_QUERY_THROW ) );
     108           0 :         break;
     109             :     case NodeType_ENTITY_NODE:
     110           0 :         rVisitor.entity( Reference<XEntity>( xNode, UNO_QUERY_THROW ) );
     111           0 :         break;
     112             :     case NodeType_ENTITY_REFERENCE_NODE:
     113           0 :         rVisitor.entityReference( Reference<XEntityReference>( xNode, UNO_QUERY_THROW ) );
     114           0 :         break;
     115             :     case NodeType_NOTATION_NODE:
     116           0 :         rVisitor.notation( Reference<XNotation>( xNode, UNO_QUERY_THROW ) );
     117           0 :         break;
     118             :     case NodeType_PROCESSING_INSTRUCTION_NODE:
     119           0 :         rVisitor.processingInstruction( Reference<XProcessingInstruction>( xNode, UNO_QUERY_THROW ) );
     120           0 :         break;
     121             :     case NodeType_TEXT_NODE:
     122           0 :         rVisitor.character( Reference<XCharacterData>( xNode, UNO_QUERY_THROW ) );
     123           0 :         break;
     124             :     default:
     125             :         OSL_FAIL( "unknown DOM node type" );
     126           0 :         break;
     127             :     }
     128           0 : }
     129             : 
     130           0 : void visit( DomVisitor& rVisitor, const Reference<XDocument>& xDocument )
     131             : {
     132           0 :     visit( rVisitor, Reference<XNode>( xDocument, UNO_QUERY_THROW ) );
     133           0 : }
     134             : 
     135           0 : void visit( DomVisitor& rVisitor, const Reference<XNode>& xNode )
     136             : {
     137           0 :     visitNode( rVisitor, xNode );
     138           0 :     for( Reference<XNode> xChild = xNode->getFirstChild();
     139             :          xChild.is();
     140           0 :          xChild = xChild->getNextSibling() )
     141             :     {
     142           0 :         visit( rVisitor, xChild );
     143           0 :     }
     144           0 :     if( xNode->getNodeType() == NodeType_ELEMENT_NODE )
     145           0 :         rVisitor.endElement( Reference<XElement>( xNode, UNO_QUERY_THROW ) );
     146           0 : }
     147             : 
     148             : 
     149             : 
     150             : class DomExport: public DomVisitor
     151             : {
     152             :     SvXMLExport& mrExport;
     153             :     vector<SvXMLNamespaceMap> maNamespaces;
     154             : 
     155             :     void pushNamespace();
     156             :     void popNamespace();
     157             :     void addNamespace( const OUString& sPrefix, const OUString& sURI );
     158             :     OUString qualifiedName( const OUString& sPrefix, const OUString& sURI,
     159             :                             const OUString& sLocalName );
     160             :     OUString qualifiedName( const Reference<XElement>&  );
     161             :     OUString qualifiedName( const Reference<XAttr>&  );
     162             :     void addAttribute( const Reference<XAttr>& );
     163             : 
     164             : public:
     165             : 
     166             :     DomExport( SvXMLExport& rExport );
     167             :     virtual ~DomExport();
     168             : 
     169             :     virtual void element( const Reference<XElement>& ) SAL_OVERRIDE;
     170             :     virtual void endElement( const Reference<XElement>& ) SAL_OVERRIDE;
     171             :     virtual void character( const Reference<XCharacterData>& ) SAL_OVERRIDE;
     172             : };
     173             : 
     174           0 : DomExport::DomExport( SvXMLExport& rExport ) :
     175           0 :     mrExport( rExport )
     176             : {
     177           0 :     maNamespaces.push_back( rExport.GetNamespaceMap() );
     178           0 : }
     179             : 
     180           0 : DomExport::~DomExport()
     181             : {
     182             :     DBG_ASSERT( maNamespaces.size() == 1, "namespace missing" );
     183           0 :     maNamespaces.clear();
     184           0 : }
     185             : 
     186           0 : void DomExport::pushNamespace()
     187             : {
     188           0 :     SvXMLNamespaceMap const aMap(maNamespaces.back());
     189           0 :     maNamespaces.push_back(aMap);
     190           0 : }
     191             : 
     192           0 : void DomExport::popNamespace()
     193             : {
     194           0 :     maNamespaces.pop_back();
     195           0 : }
     196             : 
     197           0 : void DomExport::addNamespace( const OUString& sPrefix, const OUString& sURI )
     198             : {
     199           0 :     SvXMLNamespaceMap& rMap = maNamespaces.back();
     200           0 :     sal_uInt16 nKey = rMap.GetKeyByPrefix( sPrefix );
     201             : 
     202             :     // we need to register the namespace, if either the prefix isn't known or
     203             :     // is used for a different namespace
     204           0 :     if( nKey == XML_NAMESPACE_UNKNOWN  ||
     205           0 :         rMap.GetNameByKey( nKey ) != sURI )
     206             :     {
     207             :         // add prefix to map, and add declaration
     208           0 :         rMap.Add( sPrefix, sURI );
     209           0 :         mrExport.AddAttribute( "xmlns:" + sPrefix, sURI );
     210             :     }
     211           0 : }
     212             : 
     213           0 : OUString DomExport::qualifiedName( const OUString& sPrefix,
     214             :                                    const OUString& sURI,
     215             :                                    const OUString& sLocalName )
     216             : {
     217           0 :     OUStringBuffer sBuffer;
     218           0 :     if( !sPrefix.isEmpty() && !sURI.isEmpty() )
     219             :     {
     220           0 :         addNamespace( sPrefix, sURI );
     221           0 :         sBuffer.append( sPrefix );
     222           0 :         sBuffer.append( ':' );
     223             :     }
     224           0 :     sBuffer.append( sLocalName );
     225           0 :     return sBuffer.makeStringAndClear();
     226             : }
     227             : 
     228           0 : OUString DomExport::qualifiedName( const Reference<XElement>& xElement )
     229             : {
     230           0 :     return qualifiedName( xElement->getPrefix(), xElement->getNamespaceURI(),
     231           0 :                           xElement->getNodeName() );
     232             : }
     233             : 
     234           0 : OUString DomExport::qualifiedName( const Reference<XAttr>& xAttr )
     235             : {
     236           0 :     return qualifiedName( xAttr->getPrefix(), xAttr->getNamespaceURI(),
     237           0 :                           xAttr->getNodeName() );
     238             : }
     239             : 
     240           0 : void DomExport::addAttribute( const Reference<XAttr>& xAttribute )
     241             : {
     242             :     mrExport.AddAttribute( qualifiedName( xAttribute ),
     243           0 :                            xAttribute->getNodeValue() );
     244           0 : }
     245             : 
     246           0 : void DomExport::element( const Reference<XElement>& xElement )
     247             : {
     248           0 :     pushNamespace();
     249             : 
     250             :     // write attributes
     251           0 :     Reference<XNamedNodeMap> xAttributes = xElement->getAttributes();
     252           0 :     sal_Int32 nLength = xAttributes.is() ? xAttributes->getLength() : 0;
     253           0 :     for( sal_Int32 n = 0; n < nLength; n++ )
     254             :     {
     255           0 :         addAttribute( Reference<XAttr>( xAttributes->item( n ), UNO_QUERY_THROW ) );
     256             :     }
     257             : 
     258             :     // write name
     259           0 :     mrExport.StartElement( qualifiedName( xElement ), false );
     260           0 : }
     261             : 
     262           0 : void DomExport::endElement( const Reference<XElement>& xElement )
     263             : {
     264           0 :     mrExport.EndElement( qualifiedName( xElement ), false );
     265           0 :     popNamespace();
     266           0 : }
     267             : 
     268           0 : void DomExport::character( const Reference<XCharacterData>& xChars )
     269             : {
     270           0 :     mrExport.Characters( xChars->getNodeValue() );
     271           0 : }
     272             : 
     273             : 
     274           0 : void exportDom( SvXMLExport& rExport, const Reference<XDocument>& xDocument )
     275             : {
     276           0 :     DomExport aDomExport( rExport );
     277           0 :     visit( aDomExport, xDocument );
     278           0 : }
     279             : 
     280           0 : void exportDom( SvXMLExport& rExport, const Reference<XNode>& xNode )
     281             : {
     282           0 :     DomExport aDomExport( rExport );
     283           0 :     visit( aDomExport, xNode );
     284           0 : }
     285             : 
     286             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10