LCOV - code coverage report
Current view: top level - xmloff/source/core - DomExport.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 0 97 0.0 %
Date: 2015-06-13 12:38:46 Functions: 0 24 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 endElement( const Reference<XElement>& ) {}
      67             : };
      68             : 
      69             : void visit( DomVisitor&, const Reference<XDocument>& );
      70             : void visit( DomVisitor&, const Reference<XNode>& );
      71             : 
      72             : 
      73             : 
      74           0 : void visitNode( DomVisitor& rVisitor, const Reference<XNode>& xNode )
      75             : {
      76           0 :     switch( xNode->getNodeType() )
      77             :     {
      78             :     case NodeType_ATTRIBUTE_NODE:
      79           0 :         break;
      80             :     case NodeType_CDATA_SECTION_NODE:
      81           0 :         break;
      82             :     case NodeType_COMMENT_NODE:
      83           0 :         break;
      84             :     case NodeType_DOCUMENT_FRAGMENT_NODE:
      85           0 :         break;
      86             :     case NodeType_DOCUMENT_NODE:
      87           0 :         break;
      88             :     case NodeType_DOCUMENT_TYPE_NODE:
      89           0 :         break;
      90             :     case NodeType_ELEMENT_NODE:
      91           0 :         rVisitor.element( Reference<XElement>( xNode, UNO_QUERY_THROW ) );
      92           0 :         break;
      93             :     case NodeType_ENTITY_NODE:
      94           0 :         break;
      95             :     case NodeType_ENTITY_REFERENCE_NODE:
      96           0 :         break;
      97             :     case NodeType_NOTATION_NODE:
      98           0 :         break;
      99             :     case NodeType_PROCESSING_INSTRUCTION_NODE:
     100           0 :         break;
     101             :     case NodeType_TEXT_NODE:
     102           0 :         rVisitor.character( Reference<XCharacterData>( xNode, UNO_QUERY_THROW ) );
     103           0 :         break;
     104             :     default:
     105             :         OSL_FAIL( "unknown DOM node type" );
     106           0 :         break;
     107             :     }
     108           0 : }
     109             : 
     110           0 : void visit( DomVisitor& rVisitor, const Reference<XDocument>& xDocument )
     111             : {
     112           0 :     visit( rVisitor, Reference<XNode>( xDocument, UNO_QUERY_THROW ) );
     113           0 : }
     114             : 
     115           0 : void visit( DomVisitor& rVisitor, const Reference<XNode>& xNode )
     116             : {
     117           0 :     visitNode( rVisitor, xNode );
     118           0 :     for( Reference<XNode> xChild = xNode->getFirstChild();
     119             :          xChild.is();
     120           0 :          xChild = xChild->getNextSibling() )
     121             :     {
     122           0 :         visit( rVisitor, xChild );
     123           0 :     }
     124           0 :     if( xNode->getNodeType() == NodeType_ELEMENT_NODE )
     125           0 :         rVisitor.endElement( Reference<XElement>( xNode, UNO_QUERY_THROW ) );
     126           0 : }
     127             : 
     128             : 
     129             : 
     130             : class DomExport: public DomVisitor
     131             : {
     132             :     SvXMLExport& mrExport;
     133             :     vector<SvXMLNamespaceMap> maNamespaces;
     134             : 
     135             :     void pushNamespace();
     136             :     void popNamespace();
     137             :     void addNamespace( const OUString& sPrefix, const OUString& sURI );
     138             :     OUString qualifiedName( const OUString& sPrefix, const OUString& sURI,
     139             :                             const OUString& sLocalName );
     140             :     OUString qualifiedName( const Reference<XElement>&  );
     141             :     OUString qualifiedName( const Reference<XAttr>&  );
     142             :     void addAttribute( const Reference<XAttr>& );
     143             : 
     144             : public:
     145             : 
     146             :     explicit DomExport( SvXMLExport& rExport );
     147             :     virtual ~DomExport();
     148             : 
     149             :     virtual void element( const Reference<XElement>& ) SAL_OVERRIDE;
     150             :     virtual void endElement( const Reference<XElement>& ) SAL_OVERRIDE;
     151             :     virtual void character( const Reference<XCharacterData>& ) SAL_OVERRIDE;
     152             : };
     153             : 
     154           0 : DomExport::DomExport( SvXMLExport& rExport ) :
     155           0 :     mrExport( rExport )
     156             : {
     157           0 :     maNamespaces.push_back( rExport.GetNamespaceMap() );
     158           0 : }
     159             : 
     160           0 : DomExport::~DomExport()
     161             : {
     162             :     DBG_ASSERT( maNamespaces.size() == 1, "namespace missing" );
     163           0 :     maNamespaces.clear();
     164           0 : }
     165             : 
     166           0 : void DomExport::pushNamespace()
     167             : {
     168           0 :     SvXMLNamespaceMap const aMap(maNamespaces.back());
     169           0 :     maNamespaces.push_back(aMap);
     170           0 : }
     171             : 
     172           0 : void DomExport::popNamespace()
     173             : {
     174           0 :     maNamespaces.pop_back();
     175           0 : }
     176             : 
     177           0 : void DomExport::addNamespace( const OUString& sPrefix, const OUString& sURI )
     178             : {
     179           0 :     SvXMLNamespaceMap& rMap = maNamespaces.back();
     180           0 :     sal_uInt16 nKey = rMap.GetKeyByPrefix( sPrefix );
     181             : 
     182             :     // we need to register the namespace, if either the prefix isn't known or
     183             :     // is used for a different namespace
     184           0 :     if( nKey == XML_NAMESPACE_UNKNOWN  ||
     185           0 :         rMap.GetNameByKey( nKey ) != sURI )
     186             :     {
     187             :         // add prefix to map, and add declaration
     188           0 :         rMap.Add( sPrefix, sURI );
     189           0 :         mrExport.AddAttribute( "xmlns:" + sPrefix, sURI );
     190             :     }
     191           0 : }
     192             : 
     193           0 : OUString DomExport::qualifiedName( const OUString& sPrefix,
     194             :                                    const OUString& sURI,
     195             :                                    const OUString& sLocalName )
     196             : {
     197           0 :     OUStringBuffer sBuffer;
     198           0 :     if( !sPrefix.isEmpty() && !sURI.isEmpty() )
     199             :     {
     200           0 :         addNamespace( sPrefix, sURI );
     201           0 :         sBuffer.append( sPrefix );
     202           0 :         sBuffer.append( ':' );
     203             :     }
     204           0 :     sBuffer.append( sLocalName );
     205           0 :     return sBuffer.makeStringAndClear();
     206             : }
     207             : 
     208           0 : OUString DomExport::qualifiedName( const Reference<XElement>& xElement )
     209             : {
     210           0 :     return qualifiedName( xElement->getPrefix(), xElement->getNamespaceURI(),
     211           0 :                           xElement->getNodeName() );
     212             : }
     213             : 
     214           0 : OUString DomExport::qualifiedName( const Reference<XAttr>& xAttr )
     215             : {
     216           0 :     return qualifiedName( xAttr->getPrefix(), xAttr->getNamespaceURI(),
     217           0 :                           xAttr->getNodeName() );
     218             : }
     219             : 
     220           0 : void DomExport::addAttribute( const Reference<XAttr>& xAttribute )
     221             : {
     222             :     mrExport.AddAttribute( qualifiedName( xAttribute ),
     223           0 :                            xAttribute->getNodeValue() );
     224           0 : }
     225             : 
     226           0 : void DomExport::element( const Reference<XElement>& xElement )
     227             : {
     228           0 :     pushNamespace();
     229             : 
     230             :     // write attributes
     231           0 :     Reference<XNamedNodeMap> xAttributes = xElement->getAttributes();
     232           0 :     sal_Int32 nLength = xAttributes.is() ? xAttributes->getLength() : 0;
     233           0 :     for( sal_Int32 n = 0; n < nLength; n++ )
     234             :     {
     235           0 :         addAttribute( Reference<XAttr>( xAttributes->item( n ), UNO_QUERY_THROW ) );
     236             :     }
     237             : 
     238             :     // write name
     239           0 :     mrExport.StartElement( qualifiedName( xElement ), false );
     240           0 : }
     241             : 
     242           0 : void DomExport::endElement( const Reference<XElement>& xElement )
     243             : {
     244           0 :     mrExport.EndElement( qualifiedName( xElement ), false );
     245           0 :     popNamespace();
     246           0 : }
     247             : 
     248           0 : void DomExport::character( const Reference<XCharacterData>& xChars )
     249             : {
     250           0 :     mrExport.Characters( xChars->getNodeValue() );
     251           0 : }
     252             : 
     253             : 
     254           0 : void exportDom( SvXMLExport& rExport, const Reference<XDocument>& xDocument )
     255             : {
     256           0 :     DomExport aDomExport( rExport );
     257           0 :     visit( aDomExport, xDocument );
     258           0 : }
     259             : 
     260           0 : void exportDom( SvXMLExport& rExport, const Reference<XNode>& xNode )
     261             : {
     262           0 :     DomExport aDomExport( rExport );
     263           0 :     visit( aDomExport, xNode );
     264           0 : }
     265             : 
     266             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11