1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/namespacemap.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/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>
34 #include <rtl/ustring.hxx>
35 #include <rtl/ustrbuf.hxx>
36 #include <sal/log.hxx>
37 #include <osl/diagnose.h>
43 using com::sun::star::uno::Reference
;
44 using com::sun::star::uno::UNO_QUERY_THROW
;
47 using namespace com::sun::star::xml::dom
;
55 virtual ~DomVisitor() {}
56 virtual void element( const Reference
<XElement
>& ) {}
57 virtual void character( const Reference
<XCharacterData
>& ) {}
58 virtual void endElement( const Reference
<XElement
>& ) {}
63 static void visit( DomVisitor
&, const Reference
<XDocument
>& );
64 static void visit( DomVisitor
&, const Reference
<XNode
>& );
67 static void visitNode( DomVisitor
& rVisitor
, const Reference
<XNode
>& xNode
)
69 switch( xNode
->getNodeType() )
71 case NodeType_ATTRIBUTE_NODE
:
73 case NodeType_CDATA_SECTION_NODE
:
75 case NodeType_COMMENT_NODE
:
77 case NodeType_DOCUMENT_FRAGMENT_NODE
:
79 case NodeType_DOCUMENT_NODE
:
81 case NodeType_DOCUMENT_TYPE_NODE
:
83 case NodeType_ELEMENT_NODE
:
84 rVisitor
.element( Reference
<XElement
>( xNode
, UNO_QUERY_THROW
) );
86 case NodeType_ENTITY_NODE
:
88 case NodeType_ENTITY_REFERENCE_NODE
:
90 case NodeType_NOTATION_NODE
:
92 case NodeType_PROCESSING_INSTRUCTION_NODE
:
94 case NodeType_TEXT_NODE
:
95 rVisitor
.character( Reference
<XCharacterData
>( xNode
, UNO_QUERY_THROW
) );
98 OSL_FAIL( "unknown DOM node type" );
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();
113 xChild
= xChild
->getNextSibling() )
115 visit( rVisitor
, xChild
);
117 if( xNode
->getNodeType() == NodeType_ELEMENT_NODE
)
118 rVisitor
.endElement( Reference
<XElement
>( xNode
, UNO_QUERY_THROW
) );
123 class DomExport
: public DomVisitor
125 SvXMLExport
& mrExport
;
126 vector
<SvXMLNamespaceMap
> maNamespaces
;
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
>& );
138 explicit DomExport( SvXMLExport
& rExport
);
139 virtual ~DomExport() override
;
141 virtual void element( const Reference
<XElement
>& ) override
;
142 virtual void endElement( const Reference
<XElement
>& ) override
;
143 virtual void character( const Reference
<XCharacterData
>& ) override
;
148 DomExport::DomExport( SvXMLExport
& rExport
) :
151 maNamespaces
.push_back( rExport
.GetNamespaceMap() );
154 DomExport::~DomExport()
156 SAL_WARN_IF( maNamespaces
.size() != 1, "xmloff", "namespace missing" );
157 maNamespaces
.clear();
160 void DomExport::pushNamespace()
162 SvXMLNamespaceMap
const aMap(maNamespaces
.back());
163 maNamespaces
.push_back(aMap
);
166 void DomExport::addNamespace( const OUString
& sPrefix
, const OUString
& sURI
)
168 SvXMLNamespaceMap
& rMap
= maNamespaces
.back();
169 sal_uInt16 nKey
= rMap
.GetKeyByPrefix( sPrefix
);
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
)
176 // add prefix to map, and add declaration
177 rMap
.Add( sPrefix
, sURI
);
178 mrExport
.AddAttribute( "xmlns:" + sPrefix
, sURI
);
182 OUString
DomExport::qualifiedName( const OUString
& sPrefix
,
183 const OUString
& sURI
,
184 const OUString
& sLocalName
)
186 if( !sPrefix
.isEmpty() && !sURI
.isEmpty() )
188 addNamespace( sPrefix
, sURI
);
189 return sPrefix
+ ":" + sLocalName
;
195 OUString
DomExport::qualifiedName( const Reference
<XElement
>& xElement
)
197 return qualifiedName( xElement
->getPrefix(), xElement
->getNamespaceURI(),
198 xElement
->getNodeName() );
201 OUString
DomExport::qualifiedName( const Reference
<XAttr
>& xAttr
)
203 return qualifiedName( xAttr
->getPrefix(), xAttr
->getNamespaceURI(),
204 xAttr
->getNodeName() );
207 void DomExport::addAttribute( const Reference
<XAttr
>& xAttribute
)
209 mrExport
.AddAttribute( qualifiedName( xAttribute
),
210 xAttribute
->getNodeValue() );
213 void DomExport::element( const Reference
<XElement
>& xElement
)
218 Reference
<XNamedNodeMap
> xAttributes
= xElement
->getAttributes();
219 sal_Int32 nLength
= xAttributes
.is() ? xAttributes
->getLength() : 0;
220 for( sal_Int32 n
= 0; n
< nLength
; n
++ )
222 addAttribute( Reference
<XAttr
>( xAttributes
->item( n
), UNO_QUERY_THROW
) );
226 mrExport
.StartElement( qualifiedName( xElement
), false );
229 void DomExport::endElement( const Reference
<XElement
>& xElement
)
231 mrExport
.EndElement( qualifiedName( xElement
), false );
232 maNamespaces
.pop_back();
235 void DomExport::character( const Reference
<XCharacterData
>& xChars
)
237 mrExport
.Characters( xChars
->getNodeValue() );
241 void exportDom( SvXMLExport
& rExport
, const Reference
<XDocument
>& xDocument
)
243 DomExport
aDomExport( rExport
);
244 visit( aDomExport
, xDocument
);
247 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */