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 attribute( const Reference
<XAttr
>& ) {}
67 virtual void cdata( const Reference
<XCDATASection
>& ) {}
68 virtual void comment( const Reference
<XComment
>& ) {}
69 virtual void documentFragment( const Reference
<XDocumentFragment
>& ) {}
70 virtual void document( const Reference
<XDocument
>& ) {}
71 virtual void documentType( const Reference
<XDocumentType
>& ) {}
72 virtual void entity( const Reference
<XEntity
>& ) {}
73 virtual void entityReference( const Reference
<XEntityReference
>& ) {}
74 virtual void notation( const Reference
<XNotation
>& ) {}
75 virtual void processingInstruction( const Reference
<XProcessingInstruction
>& ) {}
76 virtual void endElement( const Reference
<XElement
>& ) {}
79 void visit( DomVisitor
&, const Reference
<XDocument
>& );
80 void visit( DomVisitor
&, const Reference
<XNode
>& );
84 void visitNode( DomVisitor
& rVisitor
, const Reference
<XNode
>& xNode
)
86 switch( xNode
->getNodeType() )
88 case NodeType_ATTRIBUTE_NODE
:
89 rVisitor
.attribute( Reference
<XAttr
>( xNode
, UNO_QUERY_THROW
) );
91 case NodeType_CDATA_SECTION_NODE
:
92 rVisitor
.cdata( Reference
<XCDATASection
>( xNode
, UNO_QUERY_THROW
) );
94 case NodeType_COMMENT_NODE
:
95 rVisitor
.comment( Reference
<XComment
>( xNode
, UNO_QUERY_THROW
) );
97 case NodeType_DOCUMENT_FRAGMENT_NODE
:
98 rVisitor
.documentFragment( Reference
<XDocumentFragment
>( xNode
, UNO_QUERY_THROW
) );
100 case NodeType_DOCUMENT_NODE
:
101 rVisitor
.document( Reference
<XDocument
>( xNode
, UNO_QUERY_THROW
) );
103 case NodeType_DOCUMENT_TYPE_NODE
:
104 rVisitor
.documentType( Reference
<XDocumentType
>( xNode
, UNO_QUERY_THROW
) );
106 case NodeType_ELEMENT_NODE
:
107 rVisitor
.element( Reference
<XElement
>( xNode
, UNO_QUERY_THROW
) );
109 case NodeType_ENTITY_NODE
:
110 rVisitor
.entity( Reference
<XEntity
>( xNode
, UNO_QUERY_THROW
) );
112 case NodeType_ENTITY_REFERENCE_NODE
:
113 rVisitor
.entityReference( Reference
<XEntityReference
>( xNode
, UNO_QUERY_THROW
) );
115 case NodeType_NOTATION_NODE
:
116 rVisitor
.notation( Reference
<XNotation
>( xNode
, UNO_QUERY_THROW
) );
118 case NodeType_PROCESSING_INSTRUCTION_NODE
:
119 rVisitor
.processingInstruction( Reference
<XProcessingInstruction
>( xNode
, UNO_QUERY_THROW
) );
121 case NodeType_TEXT_NODE
:
122 rVisitor
.character( Reference
<XCharacterData
>( xNode
, UNO_QUERY_THROW
) );
125 OSL_FAIL( "unknown DOM node type" );
130 void visit( DomVisitor
& rVisitor
, const Reference
<XDocument
>& xDocument
)
132 visit( rVisitor
, Reference
<XNode
>( xDocument
, UNO_QUERY_THROW
) );
135 void visit( DomVisitor
& rVisitor
, const Reference
<XNode
>& xNode
)
137 visitNode( rVisitor
, xNode
);
138 for( Reference
<XNode
> xChild
= xNode
->getFirstChild();
140 xChild
= xChild
->getNextSibling() )
142 visit( rVisitor
, xChild
);
144 if( xNode
->getNodeType() == NodeType_ELEMENT_NODE
)
145 rVisitor
.endElement( Reference
<XElement
>( xNode
, UNO_QUERY_THROW
) );
150 class DomExport
: public DomVisitor
152 SvXMLExport
& mrExport
;
153 vector
<SvXMLNamespaceMap
> maNamespaces
;
155 void pushNamespace();
157 void addNamespace( const OUString
& sPrefix
, const OUString
& sURI
);
158 OUString
qualifiedName( const OUString
& sPrefix
, const OUString
& sURI
,
159 const OUString
& sLocalName
);
160 OUString
qualifiedName( const Reference
<XElement
>& );
161 OUString
qualifiedName( const Reference
<XAttr
>& );
162 void addAttribute( const Reference
<XAttr
>& );
166 DomExport( SvXMLExport
& rExport
);
167 virtual ~DomExport();
169 virtual void element( const Reference
<XElement
>& );
170 virtual void endElement( const Reference
<XElement
>& );
171 virtual void character( const Reference
<XCharacterData
>& );
174 DomExport::DomExport( SvXMLExport
& rExport
) :
177 maNamespaces
.push_back( rExport
.GetNamespaceMap() );
180 DomExport::~DomExport()
182 DBG_ASSERT( maNamespaces
.size() == 1, "namespace missing" );
183 maNamespaces
.clear();
186 void DomExport::pushNamespace()
188 SvXMLNamespaceMap
const aMap(maNamespaces
.back());
189 maNamespaces
.push_back(aMap
);
192 void DomExport::popNamespace()
194 maNamespaces
.pop_back();
197 void DomExport::addNamespace( const OUString
& sPrefix
, const OUString
& sURI
)
199 SvXMLNamespaceMap
& rMap
= maNamespaces
.back();
200 sal_uInt16 nKey
= rMap
.GetKeyByPrefix( sPrefix
);
202 // we need to register the namespace, if either the prefix isn't known or
203 // is used for a different namespace
204 if( nKey
== XML_NAMESPACE_UNKNOWN
||
205 rMap
.GetNameByKey( nKey
) != sURI
)
207 // add prefix to map, and add declaration
208 rMap
.Add( sPrefix
, sURI
);
209 mrExport
.AddAttribute( "xmlns:" + sPrefix
, sURI
);
213 OUString
DomExport::qualifiedName( const OUString
& sPrefix
,
214 const OUString
& sURI
,
215 const OUString
& sLocalName
)
217 OUStringBuffer sBuffer
;
218 if( !sPrefix
.isEmpty() && !sURI
.isEmpty() )
220 addNamespace( sPrefix
, sURI
);
221 sBuffer
.append( sPrefix
);
222 sBuffer
.append( sal_Unicode( ':' ) );
224 sBuffer
.append( sLocalName
);
225 return sBuffer
.makeStringAndClear();
228 OUString
DomExport::qualifiedName( const Reference
<XElement
>& xElement
)
230 return qualifiedName( xElement
->getPrefix(), xElement
->getNamespaceURI(),
231 xElement
->getNodeName() );
234 OUString
DomExport::qualifiedName( const Reference
<XAttr
>& xAttr
)
236 return qualifiedName( xAttr
->getPrefix(), xAttr
->getNamespaceURI(),
237 xAttr
->getNodeName() );
240 void DomExport::addAttribute( const Reference
<XAttr
>& xAttribute
)
242 mrExport
.AddAttribute( qualifiedName( xAttribute
),
243 xAttribute
->getNodeValue() );
246 void DomExport::element( const Reference
<XElement
>& xElement
)
251 Reference
<XNamedNodeMap
> xAttributes
= xElement
->getAttributes();
252 sal_Int32 nLength
= xAttributes
.is() ? xAttributes
->getLength() : 0;
253 for( sal_Int32 n
= 0; n
< nLength
; n
++ )
255 addAttribute( Reference
<XAttr
>( xAttributes
->item( n
), UNO_QUERY_THROW
) );
259 mrExport
.StartElement( qualifiedName( xElement
), sal_False
);
262 void DomExport::endElement( const Reference
<XElement
>& xElement
)
264 mrExport
.EndElement( qualifiedName( xElement
), sal_False
);
268 void DomExport::character( const Reference
<XCharacterData
>& xChars
)
270 mrExport
.Characters( xChars
->getNodeValue() );
274 void exportDom( SvXMLExport
& rExport
, const Reference
<XDocument
>& xDocument
)
276 DomExport
aDomExport( rExport
);
277 visit( aDomExport
, xDocument
);
280 void exportDom( SvXMLExport
& rExport
, const Reference
<XNode
>& xNode
)
282 DomExport
aDomExport( rExport
);
283 visit( aDomExport
, xNode
);
286 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */