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/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>
40 #include <rtl/ustring.hxx>
41 #include <rtl/ustrbuf.hxx>
42 #include <tools/debug.hxx>
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
;
55 using namespace com::sun::star::xml::dom
;
63 virtual ~DomVisitor() {}
64 virtual void element( const Reference
<XElement
>& ) {}
65 virtual void character( const Reference
<XCharacterData
>& ) {}
66 virtual void endElement( const Reference
<XElement
>& ) {}
69 void visit( DomVisitor
&, const Reference
<XDocument
>& );
70 void visit( DomVisitor
&, const Reference
<XNode
>& );
74 void visitNode( DomVisitor
& rVisitor
, const Reference
<XNode
>& xNode
)
76 switch( xNode
->getNodeType() )
78 case NodeType_ATTRIBUTE_NODE
:
80 case NodeType_CDATA_SECTION_NODE
:
82 case NodeType_COMMENT_NODE
:
84 case NodeType_DOCUMENT_FRAGMENT_NODE
:
86 case NodeType_DOCUMENT_NODE
:
88 case NodeType_DOCUMENT_TYPE_NODE
:
90 case NodeType_ELEMENT_NODE
:
91 rVisitor
.element( Reference
<XElement
>( xNode
, UNO_QUERY_THROW
) );
93 case NodeType_ENTITY_NODE
:
95 case NodeType_ENTITY_REFERENCE_NODE
:
97 case NodeType_NOTATION_NODE
:
99 case NodeType_PROCESSING_INSTRUCTION_NODE
:
101 case NodeType_TEXT_NODE
:
102 rVisitor
.character( Reference
<XCharacterData
>( xNode
, UNO_QUERY_THROW
) );
105 OSL_FAIL( "unknown DOM node type" );
110 void visit( DomVisitor
& rVisitor
, const Reference
<XDocument
>& xDocument
)
112 visit( rVisitor
, Reference
<XNode
>( xDocument
, UNO_QUERY_THROW
) );
115 void visit( DomVisitor
& rVisitor
, const Reference
<XNode
>& xNode
)
117 visitNode( rVisitor
, xNode
);
118 for( Reference
<XNode
> xChild
= xNode
->getFirstChild();
120 xChild
= xChild
->getNextSibling() )
122 visit( rVisitor
, xChild
);
124 if( xNode
->getNodeType() == NodeType_ELEMENT_NODE
)
125 rVisitor
.endElement( Reference
<XElement
>( xNode
, UNO_QUERY_THROW
) );
130 class DomExport
: public DomVisitor
132 SvXMLExport
& mrExport
;
133 vector
<SvXMLNamespaceMap
> maNamespaces
;
135 void pushNamespace();
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
>& );
146 DomExport( SvXMLExport
& rExport
);
147 virtual ~DomExport();
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
;
154 DomExport::DomExport( SvXMLExport
& rExport
) :
157 maNamespaces
.push_back( rExport
.GetNamespaceMap() );
160 DomExport::~DomExport()
162 DBG_ASSERT( maNamespaces
.size() == 1, "namespace missing" );
163 maNamespaces
.clear();
166 void DomExport::pushNamespace()
168 SvXMLNamespaceMap
const aMap(maNamespaces
.back());
169 maNamespaces
.push_back(aMap
);
172 void DomExport::popNamespace()
174 maNamespaces
.pop_back();
177 void DomExport::addNamespace( const OUString
& sPrefix
, const OUString
& sURI
)
179 SvXMLNamespaceMap
& rMap
= maNamespaces
.back();
180 sal_uInt16 nKey
= rMap
.GetKeyByPrefix( sPrefix
);
182 // we need to register the namespace, if either the prefix isn't known or
183 // is used for a different namespace
184 if( nKey
== XML_NAMESPACE_UNKNOWN
||
185 rMap
.GetNameByKey( nKey
) != sURI
)
187 // add prefix to map, and add declaration
188 rMap
.Add( sPrefix
, sURI
);
189 mrExport
.AddAttribute( "xmlns:" + sPrefix
, sURI
);
193 OUString
DomExport::qualifiedName( const OUString
& sPrefix
,
194 const OUString
& sURI
,
195 const OUString
& sLocalName
)
197 OUStringBuffer sBuffer
;
198 if( !sPrefix
.isEmpty() && !sURI
.isEmpty() )
200 addNamespace( sPrefix
, sURI
);
201 sBuffer
.append( sPrefix
);
202 sBuffer
.append( ':' );
204 sBuffer
.append( sLocalName
);
205 return sBuffer
.makeStringAndClear();
208 OUString
DomExport::qualifiedName( const Reference
<XElement
>& xElement
)
210 return qualifiedName( xElement
->getPrefix(), xElement
->getNamespaceURI(),
211 xElement
->getNodeName() );
214 OUString
DomExport::qualifiedName( const Reference
<XAttr
>& xAttr
)
216 return qualifiedName( xAttr
->getPrefix(), xAttr
->getNamespaceURI(),
217 xAttr
->getNodeName() );
220 void DomExport::addAttribute( const Reference
<XAttr
>& xAttribute
)
222 mrExport
.AddAttribute( qualifiedName( xAttribute
),
223 xAttribute
->getNodeValue() );
226 void DomExport::element( const Reference
<XElement
>& xElement
)
231 Reference
<XNamedNodeMap
> xAttributes
= xElement
->getAttributes();
232 sal_Int32 nLength
= xAttributes
.is() ? xAttributes
->getLength() : 0;
233 for( sal_Int32 n
= 0; n
< nLength
; n
++ )
235 addAttribute( Reference
<XAttr
>( xAttributes
->item( n
), UNO_QUERY_THROW
) );
239 mrExport
.StartElement( qualifiedName( xElement
), false );
242 void DomExport::endElement( const Reference
<XElement
>& xElement
)
244 mrExport
.EndElement( qualifiedName( xElement
), false );
248 void DomExport::character( const Reference
<XCharacterData
>& xChars
)
250 mrExport
.Characters( xChars
->getNodeValue() );
254 void exportDom( SvXMLExport
& rExport
, const Reference
<XDocument
>& xDocument
)
256 DomExport
aDomExport( rExport
);
257 visit( aDomExport
, xDocument
);
260 void exportDom( SvXMLExport
& rExport
, const Reference
<XNode
>& xNode
)
262 DomExport
aDomExport( rExport
);
263 visit( aDomExport
, xNode
);
266 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */