Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / xmloff / source / core / DomExport.cxx
blob7fa61dc80a05181c360e4583d0993f4c3b2bf760
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include "DomExport.hxx"
23 #include <xmloff/nmspmap.hxx>
24 #include <xmloff/xmlexp.hxx>
25 #include <xmloff/xmlerror.hxx>
27 #include <com/sun/star/uno/Reference.hxx>
28 #include <com/sun/star/xml/dom/XAttr.hpp>
29 #include <com/sun/star/xml/dom/XDocumentBuilder.hpp>
30 #include <com/sun/star/xml/dom/XNode.hpp>
31 #include <com/sun/star/xml/dom/XElement.hpp>
32 #include <com/sun/star/xml/dom/XEntity.hpp>
33 #include <com/sun/star/xml/dom/XNotation.hpp>
34 #include <com/sun/star/xml/sax/XAttributeList.hpp>
35 #include <com/sun/star/xml/dom/NodeType.hpp>
36 #include <com/sun/star/xml/dom/XNamedNodeMap.hpp>
38 #include <rtl/ustring.hxx>
39 #include <rtl/ustrbuf.hxx>
40 #include <tools/debug.hxx>
43 #include <vector>
46 using com::sun::star::uno::Reference;
47 using com::sun::star::uno::UNO_QUERY_THROW;
48 using std::vector;
50 using namespace com::sun::star::xml::dom;
53 class DomVisitor
55 public:
56 DomVisitor() {}
57 virtual ~DomVisitor() {}
58 virtual void element( const Reference<XElement>& ) {}
59 virtual void character( const Reference<XCharacterData>& ) {}
60 virtual void endElement( const Reference<XElement>& ) {}
63 void visit( DomVisitor&, const Reference<XDocument>& );
64 void visit( DomVisitor&, const Reference<XNode>& );
67 void visitNode( DomVisitor& rVisitor, const Reference<XNode>& xNode )
69 switch( xNode->getNodeType() )
71 case NodeType_ATTRIBUTE_NODE:
72 break;
73 case NodeType_CDATA_SECTION_NODE:
74 break;
75 case NodeType_COMMENT_NODE:
76 break;
77 case NodeType_DOCUMENT_FRAGMENT_NODE:
78 break;
79 case NodeType_DOCUMENT_NODE:
80 break;
81 case NodeType_DOCUMENT_TYPE_NODE:
82 break;
83 case NodeType_ELEMENT_NODE:
84 rVisitor.element( Reference<XElement>( xNode, UNO_QUERY_THROW ) );
85 break;
86 case NodeType_ENTITY_NODE:
87 break;
88 case NodeType_ENTITY_REFERENCE_NODE:
89 break;
90 case NodeType_NOTATION_NODE:
91 break;
92 case NodeType_PROCESSING_INSTRUCTION_NODE:
93 break;
94 case NodeType_TEXT_NODE:
95 rVisitor.character( Reference<XCharacterData>( xNode, UNO_QUERY_THROW ) );
96 break;
97 default:
98 OSL_FAIL( "unknown DOM node type" );
99 break;
103 void visit( DomVisitor& rVisitor, const Reference<XDocument>& xDocument )
105 visit( rVisitor, Reference<XNode>( xDocument, UNO_QUERY_THROW ) );
108 void visit( DomVisitor& rVisitor, const Reference<XNode>& xNode )
110 visitNode( rVisitor, xNode );
111 for( Reference<XNode> xChild = xNode->getFirstChild();
112 xChild.is();
113 xChild = xChild->getNextSibling() )
115 visit( rVisitor, xChild );
117 if( xNode->getNodeType() == NodeType_ELEMENT_NODE )
118 rVisitor.endElement( Reference<XElement>( xNode, UNO_QUERY_THROW ) );
122 class DomExport: public DomVisitor
124 SvXMLExport& mrExport;
125 vector<SvXMLNamespaceMap> maNamespaces;
127 void pushNamespace();
128 void popNamespace();
129 void addNamespace( const OUString& sPrefix, const OUString& sURI );
130 OUString qualifiedName( const OUString& sPrefix, const OUString& sURI,
131 const OUString& sLocalName );
132 OUString qualifiedName( const Reference<XElement>& );
133 OUString qualifiedName( const Reference<XAttr>& );
134 void addAttribute( const Reference<XAttr>& );
136 public:
138 explicit DomExport( SvXMLExport& rExport );
139 virtual ~DomExport();
141 virtual void element( const Reference<XElement>& ) override;
142 virtual void endElement( const Reference<XElement>& ) override;
143 virtual void character( const Reference<XCharacterData>& ) override;
146 DomExport::DomExport( SvXMLExport& rExport ) :
147 mrExport( rExport )
149 maNamespaces.push_back( rExport.GetNamespaceMap() );
152 DomExport::~DomExport()
154 DBG_ASSERT( maNamespaces.size() == 1, "namespace missing" );
155 maNamespaces.clear();
158 void DomExport::pushNamespace()
160 SvXMLNamespaceMap const aMap(maNamespaces.back());
161 maNamespaces.push_back(aMap);
164 void DomExport::popNamespace()
166 maNamespaces.pop_back();
169 void DomExport::addNamespace( const OUString& sPrefix, const OUString& sURI )
171 SvXMLNamespaceMap& rMap = maNamespaces.back();
172 sal_uInt16 nKey = rMap.GetKeyByPrefix( sPrefix );
174 // we need to register the namespace, if either the prefix isn't known or
175 // is used for a different namespace
176 if( nKey == XML_NAMESPACE_UNKNOWN ||
177 rMap.GetNameByKey( nKey ) != sURI )
179 // add prefix to map, and add declaration
180 rMap.Add( sPrefix, sURI );
181 mrExport.AddAttribute( "xmlns:" + sPrefix, sURI );
185 OUString DomExport::qualifiedName( const OUString& sPrefix,
186 const OUString& sURI,
187 const OUString& sLocalName )
189 OUStringBuffer sBuffer;
190 if( !sPrefix.isEmpty() && !sURI.isEmpty() )
192 addNamespace( sPrefix, sURI );
193 sBuffer.append( sPrefix );
194 sBuffer.append( ':' );
196 sBuffer.append( sLocalName );
197 return sBuffer.makeStringAndClear();
200 OUString DomExport::qualifiedName( const Reference<XElement>& xElement )
202 return qualifiedName( xElement->getPrefix(), xElement->getNamespaceURI(),
203 xElement->getNodeName() );
206 OUString DomExport::qualifiedName( const Reference<XAttr>& xAttr )
208 return qualifiedName( xAttr->getPrefix(), xAttr->getNamespaceURI(),
209 xAttr->getNodeName() );
212 void DomExport::addAttribute( const Reference<XAttr>& xAttribute )
214 mrExport.AddAttribute( qualifiedName( xAttribute ),
215 xAttribute->getNodeValue() );
218 void DomExport::element( const Reference<XElement>& xElement )
220 pushNamespace();
222 // write attributes
223 Reference<XNamedNodeMap> xAttributes = xElement->getAttributes();
224 sal_Int32 nLength = xAttributes.is() ? xAttributes->getLength() : 0;
225 for( sal_Int32 n = 0; n < nLength; n++ )
227 addAttribute( Reference<XAttr>( xAttributes->item( n ), UNO_QUERY_THROW ) );
230 // write name
231 mrExport.StartElement( qualifiedName( xElement ), false );
234 void DomExport::endElement( const Reference<XElement>& xElement )
236 mrExport.EndElement( qualifiedName( xElement ), false );
237 popNamespace();
240 void DomExport::character( const Reference<XCharacterData>& xChars )
242 mrExport.Characters( xChars->getNodeValue() );
246 void exportDom( SvXMLExport& rExport, const Reference<XDocument>& xDocument )
248 DomExport aDomExport( rExport );
249 visit( aDomExport, xDocument );
252 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */