LibreOffice Module xmloff (master) 1
DomExport.cxx
Go to the documentation of this file.
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
24#include <xmloff/xmlexp.hxx>
25#include <xmloff/xmlerror.hxx>
26
27#include <com/sun/star/uno/Reference.hxx>
28#include <com/sun/star/xml/dom/XAttr.hpp>
29#include <com/sun/star/xml/dom/XNode.hpp>
30#include <com/sun/star/xml/dom/XElement.hpp>
31#include <com/sun/star/xml/dom/NodeType.hpp>
32#include <com/sun/star/xml/dom/XNamedNodeMap.hpp>
33
34#include <rtl/ustring.hxx>
35#include <rtl/ustrbuf.hxx>
36#include <sal/log.hxx>
37#include <osl/diagnose.h>
38
39
40#include <vector>
41
42
44using com::sun::star::uno::UNO_QUERY_THROW;
45using std::vector;
46
47using namespace com::sun::star::xml::dom;
48
49namespace {
50
51class DomVisitor
52{
53public:
54 DomVisitor() {}
55 virtual ~DomVisitor() {}
56 virtual void element( const Reference<XElement>& ) {}
57 virtual void character( const Reference<XCharacterData>& ) {}
58 virtual void endElement( const Reference<XElement>& ) {}
59};
60
61}
62
63static void visit( DomVisitor&, const Reference<XDocument>& );
64static void visit( DomVisitor&, const Reference<XNode>& );
65
66
67static void visitNode( DomVisitor& rVisitor, const Reference<XNode>& xNode )
68{
69 switch( xNode->getNodeType() )
70 {
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;
100 }
101}
102
103void visit( DomVisitor& rVisitor, const Reference<XDocument>& xDocument )
104{
105 visit( rVisitor, Reference<XNode>( xDocument, UNO_QUERY_THROW ) );
106}
107
108void visit( DomVisitor& rVisitor, const Reference<XNode>& xNode )
109{
110 visitNode( rVisitor, xNode );
111 for( Reference<XNode> xChild = xNode->getFirstChild();
112 xChild.is();
113 xChild = xChild->getNextSibling() )
114 {
115 visit( rVisitor, xChild );
116 }
117 if( xNode->getNodeType() == NodeType_ELEMENT_NODE )
118 rVisitor.endElement( Reference<XElement>( xNode, UNO_QUERY_THROW ) );
119}
120
121namespace {
122
123class DomExport: public DomVisitor
124{
125 SvXMLExport& mrExport;
126 vector<SvXMLNamespaceMap> maNamespaces;
127
128 void pushNamespace();
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>& );
135
136public:
137
138 explicit DomExport( SvXMLExport& rExport );
139 virtual ~DomExport() override;
140
141 virtual void element( const Reference<XElement>& ) override;
142 virtual void endElement( const Reference<XElement>& ) override;
143 virtual void character( const Reference<XCharacterData>& ) override;
144};
145
146}
147
148DomExport::DomExport( SvXMLExport& rExport ) :
149 mrExport( rExport )
150{
151 maNamespaces.push_back( rExport.GetNamespaceMap() );
152}
153
154DomExport::~DomExport()
155{
156 SAL_WARN_IF( maNamespaces.size() != 1, "xmloff", "namespace missing" );
157 maNamespaces.clear();
158}
159
160void DomExport::pushNamespace()
161{
162 SvXMLNamespaceMap const aMap(maNamespaces.back());
163 maNamespaces.push_back(aMap);
164}
165
166void DomExport::addNamespace( const OUString& sPrefix, const OUString& sURI )
167{
168 SvXMLNamespaceMap& rMap = maNamespaces.back();
169 sal_uInt16 nKey = rMap.GetKeyByPrefix( sPrefix );
170
171 // we need to register the namespace, if either the prefix isn't known or
172 // is used for a different namespace
173 if( nKey == XML_NAMESPACE_UNKNOWN ||
174 rMap.GetNameByKey( nKey ) != sURI )
175 {
176 // add prefix to map, and add declaration
177 rMap.Add( sPrefix, sURI );
178 mrExport.AddAttribute( "xmlns:" + sPrefix, sURI );
179 }
180}
181
182OUString DomExport::qualifiedName( const OUString& sPrefix,
183 const OUString& sURI,
184 const OUString& sLocalName )
185{
186 if( !sPrefix.isEmpty() && !sURI.isEmpty() )
187 {
188 addNamespace( sPrefix, sURI );
189 return sPrefix + ":" + sLocalName;
190 }
191 else
192 return sLocalName;
193}
194
195OUString DomExport::qualifiedName( const Reference<XElement>& xElement )
196{
197 return qualifiedName( xElement->getPrefix(), xElement->getNamespaceURI(),
198 xElement->getNodeName() );
199}
200
201OUString DomExport::qualifiedName( const Reference<XAttr>& xAttr )
202{
203 return qualifiedName( xAttr->getPrefix(), xAttr->getNamespaceURI(),
204 xAttr->getNodeName() );
205}
206
207void DomExport::addAttribute( const Reference<XAttr>& xAttribute )
208{
209 mrExport.AddAttribute( qualifiedName( xAttribute ),
210 xAttribute->getNodeValue() );
211}
212
213void DomExport::element( const Reference<XElement>& xElement )
214{
215 pushNamespace();
216
217 // write attributes
218 Reference<XNamedNodeMap> xAttributes = xElement->getAttributes();
219 sal_Int32 nLength = xAttributes.is() ? xAttributes->getLength() : 0;
220 for( sal_Int32 n = 0; n < nLength; n++ )
221 {
222 addAttribute( Reference<XAttr>( xAttributes->item( n ), UNO_QUERY_THROW ) );
223 }
224
225 // write name
226 mrExport.StartElement( qualifiedName( xElement ), false );
227}
228
229void DomExport::endElement( const Reference<XElement>& xElement )
230{
231 mrExport.EndElement( qualifiedName( xElement ), false );
232 maNamespaces.pop_back();
233}
234
235void DomExport::character( const Reference<XCharacterData>& xChars )
236{
237 mrExport.Characters( xChars->getNodeValue() );
238}
239
240
241void exportDom( SvXMLExport& rExport, const Reference<XDocument>& xDocument )
242{
243 DomExport aDomExport( rExport );
244 visit( aDomExport, xDocument );
245}
246
247/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void exportDom(SvXMLExport &rExport, const Reference< XDocument > &xDocument)
Definition: DomExport.cxx:241
static void visit(DomVisitor &, const Reference< XDocument > &)
Definition: DomExport.cxx:103
static void visitNode(DomVisitor &rVisitor, const Reference< XNode > &xNode)
Definition: DomExport.cxx:67
const SvXMLNamespaceMap & GetNamespaceMap() const
Definition: xmlexp.hxx:385
const OUString & GetNameByKey(sal_uInt16 nKey) const
sal_uInt16 GetKeyByPrefix(const OUString &rPrefix) const
sal_uInt16 Add(const OUString &rPrefix, const OUString &rName, sal_uInt16 nKey=XML_NAMESPACE_UNKNOWN)
OUString sPrefix
sal_Int64 n
#define SAL_WARN_IF(condition, area, stream)
@ character
const sal_uInt16 XML_NAMESPACE_UNKNOWN
HashMap_OWString_Interface aMap
sal_Int32 nLength
Definition: xmltoken.cxx:38