LibreOffice Module xmloff (master)  1
DomExport.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 
21 #include <DomExport.hxx>
22 
23 #include <xmloff/namespacemap.hxx>
24 #include <xmloff/xmlexp.hxx>
25 #include <xmloff/xmlerror.hxx>
26 
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>
33 
34 #include <rtl/ustring.hxx>
35 #include <rtl/ustrbuf.hxx>
36 #include <sal/log.hxx>
37 #include <osl/diagnose.h>
38 
39 
40 #include <vector>
41 
42 
44 using com::sun::star::uno::UNO_QUERY_THROW;
45 using std::vector;
46 
47 using namespace com::sun::star::xml::dom;
48 
49 namespace {
50 
51 class DomVisitor
52 {
53 public:
54  DomVisitor() {}
55  virtual ~DomVisitor() {}
56  virtual void element( const Reference<XElement>& ) {}
57  virtual void character( const Reference<XCharacterData>& ) {}
58  virtual void endElement( const Reference<XElement>& ) {}
59 };
60 
61 }
62 
63 static void visit( DomVisitor&, const Reference<XDocument>& );
64 static void visit( DomVisitor&, const Reference<XNode>& );
65 
66 
67 static void visitNode( DomVisitor& rVisitor, const Reference<XNode>& xNode )
68 {
69  switch( xNode->getNodeType() )
70  {
71  case NodeType_ATTRIBUTE_NODE:
72  break;
73  case NodeType_CDATA_SECTION_NODE:
74  break;
75  case NodeType_COMMENT_NODE:
76  break;
77  case NodeType_DOCUMENT_FRAGMENT_NODE:
78  break;
79  case NodeType_DOCUMENT_NODE:
80  break;
81  case NodeType_DOCUMENT_TYPE_NODE:
82  break;
83  case NodeType_ELEMENT_NODE:
84  rVisitor.element( Reference<XElement>( xNode, UNO_QUERY_THROW ) );
85  break;
86  case NodeType_ENTITY_NODE:
87  break;
88  case NodeType_ENTITY_REFERENCE_NODE:
89  break;
90  case NodeType_NOTATION_NODE:
91  break;
92  case NodeType_PROCESSING_INSTRUCTION_NODE:
93  break;
94  case NodeType_TEXT_NODE:
95  rVisitor.character( Reference<XCharacterData>( xNode, UNO_QUERY_THROW ) );
96  break;
97  default:
98  OSL_FAIL( "unknown DOM node type" );
99  break;
100  }
101 }
102 
103 void visit( DomVisitor& rVisitor, const Reference<XDocument>& xDocument )
104 {
105  visit( rVisitor, Reference<XNode>( xDocument, UNO_QUERY_THROW ) );
106 }
107 
108 void visit( DomVisitor& rVisitor, const Reference<XNode>& xNode )
109 {
110  visitNode( rVisitor, xNode );
111  for( Reference<XNode> xChild = xNode->getFirstChild();
112  xChild.is();
113  xChild = xChild->getNextSibling() )
114  {
115  visit( rVisitor, xChild );
116  }
117  if( xNode->getNodeType() == NodeType_ELEMENT_NODE )
118  rVisitor.endElement( Reference<XElement>( xNode, UNO_QUERY_THROW ) );
119 }
120 
121 namespace {
122 
123 class DomExport: public DomVisitor
124 {
125  SvXMLExport& mrExport;
126  vector<SvXMLNamespaceMap> maNamespaces;
127 
128  void pushNamespace();
129  void addNamespace( const OUString& sPrefix, const OUString& sURI );
130  OUString qualifiedName( const OUString& sPrefix, const OUString& sURI,
131  std::u16string_view sLocalName );
132  OUString qualifiedName( const Reference<XElement>& );
133  OUString qualifiedName( const Reference<XAttr>& );
134  void addAttribute( const Reference<XAttr>& );
135 
136 public:
137 
138  explicit DomExport( SvXMLExport& rExport );
139  virtual ~DomExport() override;
140 
141  virtual void element( const Reference<XElement>& ) override;
142  virtual void endElement( const Reference<XElement>& ) override;
143  virtual void character( const Reference<XCharacterData>& ) override;
144 };
145 
146 }
147 
148 DomExport::DomExport( SvXMLExport& rExport ) :
149  mrExport( rExport )
150 {
151  maNamespaces.push_back( rExport.GetNamespaceMap() );
152 }
153 
154 DomExport::~DomExport()
155 {
156  SAL_WARN_IF( maNamespaces.size() != 1, "xmloff", "namespace missing" );
157  maNamespaces.clear();
158 }
159 
160 void DomExport::pushNamespace()
161 {
162  SvXMLNamespaceMap const aMap(maNamespaces.back());
163  maNamespaces.push_back(aMap);
164 }
165 
166 void DomExport::addNamespace( const OUString& sPrefix, const OUString& sURI )
167 {
168  SvXMLNamespaceMap& rMap = maNamespaces.back();
169  sal_uInt16 nKey = rMap.GetKeyByPrefix( sPrefix );
170 
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 )
175  {
176  // add prefix to map, and add declaration
177  rMap.Add( sPrefix, sURI );
178  mrExport.AddAttribute( "xmlns:" + sPrefix, sURI );
179  }
180 }
181 
182 OUString DomExport::qualifiedName( const OUString& sPrefix,
183  const OUString& sURI,
184  std::u16string_view sLocalName )
185 {
186  OUStringBuffer sBuffer;
187  if( !sPrefix.isEmpty() && !sURI.isEmpty() )
188  {
189  addNamespace( sPrefix, sURI );
190  sBuffer.append( sPrefix );
191  sBuffer.append( ':' );
192  }
193  sBuffer.append( sLocalName );
194  return sBuffer.makeStringAndClear();
195 }
196 
197 OUString DomExport::qualifiedName( const Reference<XElement>& xElement )
198 {
199  return qualifiedName( xElement->getPrefix(), xElement->getNamespaceURI(),
200  xElement->getNodeName() );
201 }
202 
203 OUString DomExport::qualifiedName( const Reference<XAttr>& xAttr )
204 {
205  return qualifiedName( xAttr->getPrefix(), xAttr->getNamespaceURI(),
206  xAttr->getNodeName() );
207 }
208 
209 void DomExport::addAttribute( const Reference<XAttr>& xAttribute )
210 {
211  mrExport.AddAttribute( qualifiedName( xAttribute ),
212  xAttribute->getNodeValue() );
213 }
214 
215 void DomExport::element( const Reference<XElement>& xElement )
216 {
217  pushNamespace();
218 
219  // write attributes
220  Reference<XNamedNodeMap> xAttributes = xElement->getAttributes();
221  sal_Int32 nLength = xAttributes.is() ? xAttributes->getLength() : 0;
222  for( sal_Int32 n = 0; n < nLength; n++ )
223  {
224  addAttribute( Reference<XAttr>( xAttributes->item( n ), UNO_QUERY_THROW ) );
225  }
226 
227  // write name
228  mrExport.StartElement( qualifiedName( xElement ), false );
229 }
230 
231 void DomExport::endElement( const Reference<XElement>& xElement )
232 {
233  mrExport.EndElement( qualifiedName( xElement ), false );
234  maNamespaces.pop_back();
235 }
236 
237 void DomExport::character( const Reference<XCharacterData>& xChars )
238 {
239  mrExport.Characters( xChars->getNodeValue() );
240 }
241 
242 
243 void exportDom( SvXMLExport& rExport, const Reference<XDocument>& xDocument )
244 {
245  DomExport aDomExport( rExport );
246  visit( aDomExport, xDocument );
247 }
248 
249 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt16 GetKeyByPrefix(const OUString &rPrefix) const
sal_Int64 n
const sal_uInt16 XML_NAMESPACE_UNKNOWN
HashMap_OWString_Interface aMap
OUString sPrefix
static void visit(DomVisitor &, const Reference< XDocument > &)
Definition: DomExport.cxx:103
static void visitNode(DomVisitor &rVisitor, const Reference< XNode > &xNode)
Definition: DomExport.cxx:67
const SvXMLNamespaceMap & GetNamespaceMap() const
Definition: xmlexp.hxx:390
const OUString & GetNameByKey(sal_uInt16 nKey) const
#define SAL_WARN_IF(condition, area, stream)
void exportDom(SvXMLExport &rExport, const Reference< XDocument > &xDocument)
Definition: DomExport.cxx:243
sal_uInt16 Add(const OUString &rPrefix, const OUString &rName, sal_uInt16 nKey=XML_NAMESPACE_UNKNOWN)
sal_Int32 nLength
Definition: xmltoken.cxx:38