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/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/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
;
54 virtual ~DomVisitor() {}
55 virtual void element( const Reference
<XElement
>& ) {}
56 virtual void character( const Reference
<XCharacterData
>& ) {}
57 virtual void endElement( const Reference
<XElement
>& ) {}
60 static void visit( DomVisitor
&, const Reference
<XDocument
>& );
61 static void visit( DomVisitor
&, const Reference
<XNode
>& );
64 static void visitNode( DomVisitor
& rVisitor
, const Reference
<XNode
>& xNode
)
66 switch( xNode
->getNodeType() )
68 case NodeType_ATTRIBUTE_NODE
:
70 case NodeType_CDATA_SECTION_NODE
:
72 case NodeType_COMMENT_NODE
:
74 case NodeType_DOCUMENT_FRAGMENT_NODE
:
76 case NodeType_DOCUMENT_NODE
:
78 case NodeType_DOCUMENT_TYPE_NODE
:
80 case NodeType_ELEMENT_NODE
:
81 rVisitor
.element( Reference
<XElement
>( xNode
, UNO_QUERY_THROW
) );
83 case NodeType_ENTITY_NODE
:
85 case NodeType_ENTITY_REFERENCE_NODE
:
87 case NodeType_NOTATION_NODE
:
89 case NodeType_PROCESSING_INSTRUCTION_NODE
:
91 case NodeType_TEXT_NODE
:
92 rVisitor
.character( Reference
<XCharacterData
>( xNode
, UNO_QUERY_THROW
) );
95 OSL_FAIL( "unknown DOM node type" );
100 void visit( DomVisitor
& rVisitor
, const Reference
<XDocument
>& xDocument
)
102 visit( rVisitor
, Reference
<XNode
>( xDocument
, UNO_QUERY_THROW
) );
105 void visit( DomVisitor
& rVisitor
, const Reference
<XNode
>& xNode
)
107 visitNode( rVisitor
, xNode
);
108 for( Reference
<XNode
> xChild
= xNode
->getFirstChild();
110 xChild
= xChild
->getNextSibling() )
112 visit( rVisitor
, xChild
);
114 if( xNode
->getNodeType() == NodeType_ELEMENT_NODE
)
115 rVisitor
.endElement( Reference
<XElement
>( xNode
, UNO_QUERY_THROW
) );
119 class DomExport
: public DomVisitor
121 SvXMLExport
& mrExport
;
122 vector
<SvXMLNamespaceMap
> maNamespaces
;
124 void pushNamespace();
125 void addNamespace( const OUString
& sPrefix
, const OUString
& sURI
);
126 OUString
qualifiedName( const OUString
& sPrefix
, const OUString
& sURI
,
127 const OUString
& sLocalName
);
128 OUString
qualifiedName( const Reference
<XElement
>& );
129 OUString
qualifiedName( const Reference
<XAttr
>& );
130 void addAttribute( const Reference
<XAttr
>& );
134 explicit DomExport( SvXMLExport
& rExport
);
135 virtual ~DomExport() override
;
137 virtual void element( const Reference
<XElement
>& ) override
;
138 virtual void endElement( const Reference
<XElement
>& ) override
;
139 virtual void character( const Reference
<XCharacterData
>& ) override
;
142 DomExport::DomExport( SvXMLExport
& rExport
) :
145 maNamespaces
.push_back( rExport
.GetNamespaceMap() );
148 DomExport::~DomExport()
150 SAL_WARN_IF( maNamespaces
.size() != 1, "xmloff", "namespace missing" );
151 maNamespaces
.clear();
154 void DomExport::pushNamespace()
156 SvXMLNamespaceMap
const aMap(maNamespaces
.back());
157 maNamespaces
.push_back(aMap
);
160 void DomExport::addNamespace( const OUString
& sPrefix
, const OUString
& sURI
)
162 SvXMLNamespaceMap
& rMap
= maNamespaces
.back();
163 sal_uInt16 nKey
= rMap
.GetKeyByPrefix( sPrefix
);
165 // we need to register the namespace, if either the prefix isn't known or
166 // is used for a different namespace
167 if( nKey
== XML_NAMESPACE_UNKNOWN
||
168 rMap
.GetNameByKey( nKey
) != sURI
)
170 // add prefix to map, and add declaration
171 rMap
.Add( sPrefix
, sURI
);
172 mrExport
.AddAttribute( "xmlns:" + sPrefix
, sURI
);
176 OUString
DomExport::qualifiedName( const OUString
& sPrefix
,
177 const OUString
& sURI
,
178 const OUString
& sLocalName
)
180 OUStringBuffer sBuffer
;
181 if( !sPrefix
.isEmpty() && !sURI
.isEmpty() )
183 addNamespace( sPrefix
, sURI
);
184 sBuffer
.append( sPrefix
);
185 sBuffer
.append( ':' );
187 sBuffer
.append( sLocalName
);
188 return sBuffer
.makeStringAndClear();
191 OUString
DomExport::qualifiedName( const Reference
<XElement
>& xElement
)
193 return qualifiedName( xElement
->getPrefix(), xElement
->getNamespaceURI(),
194 xElement
->getNodeName() );
197 OUString
DomExport::qualifiedName( const Reference
<XAttr
>& xAttr
)
199 return qualifiedName( xAttr
->getPrefix(), xAttr
->getNamespaceURI(),
200 xAttr
->getNodeName() );
203 void DomExport::addAttribute( const Reference
<XAttr
>& xAttribute
)
205 mrExport
.AddAttribute( qualifiedName( xAttribute
),
206 xAttribute
->getNodeValue() );
209 void DomExport::element( const Reference
<XElement
>& xElement
)
214 Reference
<XNamedNodeMap
> xAttributes
= xElement
->getAttributes();
215 sal_Int32 nLength
= xAttributes
.is() ? xAttributes
->getLength() : 0;
216 for( sal_Int32 n
= 0; n
< nLength
; n
++ )
218 addAttribute( Reference
<XAttr
>( xAttributes
->item( n
), UNO_QUERY_THROW
) );
222 mrExport
.StartElement( qualifiedName( xElement
), false );
225 void DomExport::endElement( const Reference
<XElement
>& xElement
)
227 mrExport
.EndElement( qualifiedName( xElement
), false );
228 maNamespaces
.pop_back();
231 void DomExport::character( const Reference
<XCharacterData
>& xChars
)
233 mrExport
.Characters( xChars
->getNodeValue() );
237 void exportDom( SvXMLExport
& rExport
, const Reference
<XDocument
>& xDocument
)
239 DomExport
aDomExport( rExport
);
240 visit( aDomExport
, xDocument
);
243 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */