Bump version to 6.4.0.12
[LibreOffice.git] / xmloff / source / core / DomExport.cxx
blobd437f2617c6d0a5cd46eb262d3fee9e8efb61b76
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
40 #include <vector>
43 using com::sun::star::uno::Reference;
44 using com::sun::star::uno::UNO_QUERY_THROW;
45 using std::vector;
47 using namespace com::sun::star::xml::dom;
50 class DomVisitor
52 public:
53 DomVisitor() {}
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:
69 break;
70 case NodeType_CDATA_SECTION_NODE:
71 break;
72 case NodeType_COMMENT_NODE:
73 break;
74 case NodeType_DOCUMENT_FRAGMENT_NODE:
75 break;
76 case NodeType_DOCUMENT_NODE:
77 break;
78 case NodeType_DOCUMENT_TYPE_NODE:
79 break;
80 case NodeType_ELEMENT_NODE:
81 rVisitor.element( Reference<XElement>( xNode, UNO_QUERY_THROW ) );
82 break;
83 case NodeType_ENTITY_NODE:
84 break;
85 case NodeType_ENTITY_REFERENCE_NODE:
86 break;
87 case NodeType_NOTATION_NODE:
88 break;
89 case NodeType_PROCESSING_INSTRUCTION_NODE:
90 break;
91 case NodeType_TEXT_NODE:
92 rVisitor.character( Reference<XCharacterData>( xNode, UNO_QUERY_THROW ) );
93 break;
94 default:
95 OSL_FAIL( "unknown DOM node type" );
96 break;
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();
109 xChild.is();
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>& );
132 public:
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 ) :
143 mrExport( 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 )
211 pushNamespace();
213 // write attributes
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 ) );
221 // write name
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: */