LibreOffice Module xmloff (master)  1
DomBuilderContext.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 <DomBuilderContext.hxx>
22 
23 #include <xmloff/namespacemap.hxx>
24 #include <xmloff/xmlimp.hxx>
25 #include <xmloff/xmlerror.hxx>
26 
27 #include <com/sun/star/uno/Reference.hxx>
28 #include <com/sun/star/uno/Sequence.hxx>
29 #include <com/sun/star/xml/dom/DocumentBuilder.hpp>
30 #include <com/sun/star/xml/dom/XNode.hpp>
31 #include <com/sun/star/xml/dom/XElement.hpp>
32 #include <com/sun/star/xml/sax/XAttributeList.hpp>
33 #include <com/sun/star/xml/dom/NodeType.hpp>
34 
35 #include <rtl/ustring.hxx>
36 #include <sal/log.hxx>
37 
39 
40 
41 using com::sun::star::uno::XComponentContext;
44 using com::sun::star::uno::UNO_QUERY;
45 using com::sun::star::uno::UNO_QUERY_THROW;
46 using com::sun::star::xml::dom::DocumentBuilder;
47 using com::sun::star::xml::dom::XDocument;
48 using com::sun::star::xml::dom::XDocumentBuilder;
49 using com::sun::star::xml::dom::XNode;
50 using com::sun::star::xml::dom::XElement;
51 using com::sun::star::xml::sax::XAttributeList;
52 using com::sun::star::xml::dom::NodeType_ELEMENT_NODE;
53 
54 
55 // helper functions; implemented below
58  sal_Int32 nElement,
59  const Reference<XNode>& xParent);
61  const OUString & rNamespace, const OUString & rName,
62  const Reference<XNode>& xParent);
63 
65  sal_Int32 nElement ) :
66  SvXMLImportContext( rImport ),
67  mxNode( lcl_createElement( rImport, nElement,
69 {
70  SAL_WARN_IF( !mxNode.is(), "xmloff", "empty XNode not allowed" );
71  SAL_WARN_IF( !Reference<XElement>( mxNode, UNO_QUERY ).is(), "xmloff", "need element" );
72  SAL_WARN_IF( mxNode->getNodeType() != NodeType_ELEMENT_NODE, "xmloff", "need element" );
73 }
74 
76  const OUString & rNamespace, const OUString & rName ) :
77  SvXMLImportContext( rImport ),
78  mxNode( lcl_createElement( rNamespace, rName,
80 {
81  SAL_WARN_IF( !mxNode.is(), "xmloff", "empty XNode not allowed" );
82  SAL_WARN_IF( !Reference<XElement>( mxNode, UNO_QUERY ).is(), "xmloff", "need element" );
83  SAL_WARN_IF( mxNode->getNodeType() != NodeType_ELEMENT_NODE, "xmloff", "need element" );
84 }
85 
87  sal_Int32 nElement,
88  Reference<XNode> const & xParent ) :
89  SvXMLImportContext( rImport ),
90  mxNode( lcl_createElement( rImport, nElement, xParent ) )
91 {
92  SAL_WARN_IF( !mxNode.is(), "xmloff", "empty XNode not allowed" );
93  SAL_WARN_IF( !Reference<XElement>( mxNode, UNO_QUERY ).is(), "xmloff", "need element" );
94  SAL_WARN_IF( mxNode->getNodeType() != NodeType_ELEMENT_NODE, "xmloff", "need element" );
95 }
96 
98  const OUString & rNamespace, const OUString & rName,
99  Reference<XNode> const & xParent ) :
100  SvXMLImportContext( rImport ),
101  mxNode( lcl_createElement( rNamespace, rName, xParent ) )
102 {
103  SAL_WARN_IF( !mxNode.is(), "xmloff", "empty XNode not allowed" );
104  SAL_WARN_IF( !Reference<XElement>( mxNode, UNO_QUERY ).is(), "xmloff", "need element" );
105  SAL_WARN_IF( mxNode->getNodeType() != NodeType_ELEMENT_NODE, "xmloff", "need element" );
106 }
107 
109 {
110 }
111 
113 {
114  SAL_WARN_IF( !mxNode.is(), "xmloff", "empty XNode not allowed" );
115  return mxNode->getOwnerDocument();
116 }
117 
118 css::uno::Reference< css::xml::sax::XFastContextHandler > DomBuilderContext::createFastChildContext(
119  sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList >& )
120 {
121  // create DomBuilder for subtree
122  return new DomBuilderContext( GetImport(), nElement, mxNode );
123 }
124 
125 css::uno::Reference< css::xml::sax::XFastContextHandler > DomBuilderContext::createUnknownChildContext(
126  const OUString & rNamespace, const OUString &rName, const css::uno::Reference< css::xml::sax::XFastAttributeList >& )
127 {
128  // create DomBuilder for subtree
129  return new DomBuilderContext( GetImport(), rNamespace, rName, mxNode );
130 }
131 
133  sal_Int32 /*nElement*/,
134  const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
135 {
136  SAL_WARN_IF( !mxNode.is(), "xmloff", "empty XNode not allowed" );
137  SAL_WARN_IF( !mxNode->getOwnerDocument().is(), "xmloff", "XNode must have XDocument" );
138 
139  HandleAttributes(xAttrList);
140 }
141 
143  const OUString & /*rNamespace*/, const OUString & /*rName*/,
144  const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
145 {
146  SAL_WARN_IF( !mxNode.is(), "xmloff", "empty XNode not allowed" );
147  SAL_WARN_IF( !mxNode->getOwnerDocument().is(), "xmloff", "XNode must have XDocument" );
148  HandleAttributes(xAttrList);
149 }
150 
152  const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
153 {
154  // add attribute nodes to new node
155  for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
156  {
157  sal_Int32 nAttrToken = aIter.getToken();
158  // get name & value for attribute
159  sal_uInt16 nNamespace = (nAttrToken >> NMSP_SHIFT) - 1;
160  const OUString& rPrefix = SvXMLImport::getNamespacePrefixFromToken(nAttrToken, &GetImport().GetNamespaceMap());
161  const OUString& rLocalName = SvXMLImport::getNameFromToken( nAttrToken );
162  OUString aValue = aIter.toString();
163 
164  // create attribute node and set value
165  Reference<XElement> xElement( mxNode, UNO_QUERY_THROW );
166  switch( nNamespace )
167  {
168  case XML_NAMESPACE_NONE:
169  // no namespace: create a non-namespaced attribute
170  xElement->setAttribute( rLocalName, aValue );
171  break;
172  case XML_NAMESPACE_XMLNS:
173  // namespace declaration: ignore, since the DOM tree handles these
174  // declarations implicitly
175  break;
177  // unknown namespace: illegal input. Raise Warning.
178  {
180  aSeq[0] = rLocalName;
181  aSeq[1] = aValue;
184  }
185  break;
186  default:
187  {
188  // a real and proper namespace: create namespaced attribute
189  OUString namespaceURI = SvXMLImport::getNamespaceURIFromToken(aIter.getToken());
190  OUString qualifiedName = rPrefix.isEmpty() ? rLocalName : rPrefix + SvXMLImport::aNamespaceSeparator + rLocalName;
191  xElement->setAttributeNS( namespaceURI, qualifiedName, aValue );
192  }
193  break;
194  }
195  }
196  const css::uno::Sequence< css::xml::Attribute > unknownAttribs = xAttrList->getUnknownAttributes();
197  for ( const auto& rUnknownAttrib : unknownAttribs )
198  {
199  // create attribute node and set value
200  Reference<XElement> xElement( mxNode, UNO_QUERY_THROW );
201 
202  if (!rUnknownAttrib.NamespaceURL.isEmpty())
203  {
204  // unknown namespace: illegal input. Raise Warning.
206  aSeq[0] = rUnknownAttrib.Name;
207  aSeq[1] = rUnknownAttrib.Value;
210  }
211  else
212  {
213  // no namespace: create a non-namespaced attribute
214  xElement->setAttribute( rUnknownAttrib.Name, rUnknownAttrib.Value );
215  }
216  }
217 }
218 
219 void DomBuilderContext::characters( const OUString& rCharacters )
220 {
221  SAL_WARN_IF( !mxNode.is(), "xmloff", "empty XNode not allowed" );
222 
223  // TODO: I assume adjacent text nodes should be joined, to preserve
224  // processing model? (I.e., if the SAX parser breaks a string into 2
225  // Characters(..) calls, the DOM model would still see only one child.)
226 
227  // create text node and append to parent
228  Reference<XNode> xNew(
229  mxNode->getOwnerDocument()->createTextNode( rCharacters ),
230  UNO_QUERY_THROW );
231  mxNode->appendChild( xNew );
232 }
233 
234 
235 // helper function implementations
236 
237 
239 {
241  SAL_WARN_IF( !xContext.is(), "xmloff", "can't get service factory" );
242 
243  Reference<XDocumentBuilder> xBuilder( DocumentBuilder::create(xContext) );
244 
245  return Reference<XNode>( xBuilder->newDocument(), UNO_QUERY_THROW );
246 }
247 
249  sal_Int32 nElement,
250  const Reference<XNode>& xParent)
251 {
252  SAL_WARN_IF( !xParent.is(), "xmloff", "need parent node" );
253 
254  Reference<XDocument> xDocument = xParent->getOwnerDocument();
255  SAL_WARN_IF( !xDocument.is(), "xmloff", "no XDocument found!" );
256 
257  // TODO: come up with proper way of handling namespaces; re-creating the
258  // namespace from the key is NOT a good idea, and will not work for
259  // multiple prefixes for the same namespace. Fortunately, those are rare.
260 
261  Reference<XElement> xElement;
262  sal_uInt16 nNamespace = (nElement >> NMSP_SHIFT) - 1;
263  const OUString& rPrefix = SvXMLImport::getNamespacePrefixFromToken(nElement, &rImport.GetNamespaceMap());
264  const OUString& rLocalName = SvXMLImport::getNameFromToken( nElement );
265  switch( nNamespace )
266  {
267  case XML_NAMESPACE_NONE:
268  // no namespace: use local name
269  xElement = xDocument->createElement( rLocalName );
270  break;
271  case XML_NAMESPACE_XMLNS:
273  // both cases are illegal; raise warning (and use only local name)
274  xElement = xDocument->createElement( rLocalName );
275  {
276  Sequence<OUString> aSeq { rLocalName };
277  rImport.SetError(
279  }
280  break;
281  default:
282  // We are only given the prefix and the local name; thus we have to ask
283  // the namespace map to create a qualified name for us. Technically,
284  // this is a bug, since this will fail for multiple prefixes used for
285  // the same namespace.
286  OUString namespaceURI = SvXMLImport::getNamespaceURIFromToken(nElement);
287  OUString qualifiedName = rPrefix.isEmpty() ? rLocalName : rPrefix + SvXMLImport::aNamespaceSeparator + rLocalName;
288  xElement = xDocument->createElementNS(namespaceURI, qualifiedName);
289  break;
290  }
291  SAL_WARN_IF( !xElement.is(), "xmloff", "can't create element" );
292 
293  // add new element to parent and return
294  xParent->appendChild( xElement );
295  return xElement;
296 }
297 
299  const OUString & rNamespace, const OUString & rName,
300  const Reference<XNode>& xParent)
301 {
302  SAL_WARN_IF( !xParent.is(), "xmloff", "need parent node" );
303 
304  Reference<XDocument> xDocument = xParent->getOwnerDocument();
305  SAL_WARN_IF( !xDocument.is(), "xmloff", "no XDocument found!" );
306 
307  // TODO: come up with proper way of handling namespaces; re-creating the
308  // namespace from the key is NOT a good idea, and will not work for
309  // multiple prefixes for the same namespace. Fortunately, those are rare.
310 
311  Reference<XElement> xElement;
312  if (rNamespace.isEmpty())
313  {
314  // no namespace: use local name
315  xElement = xDocument->createElement( rName );
316  }
317  else
318  {
319  xElement = xDocument->createElementNS(rNamespace, rName);
320  }
321 
322  // add new element to parent and return
323  xParent->appendChild( xElement );
324  return xElement;
325 }
326 
327 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static Reference< XNode > lcl_createElement(SvXMLImport &rImport, sal_Int32 nElement, const Reference< XNode > &xParent)
void HandleAttributes(const css::uno::Reference< css::xml::sax::XFastAttributeList > &Attribs)
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:60
SvXMLNamespaceMap & GetNamespaceMap()
Definition: xmlimp.hxx:393
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
const sal_uInt16 XML_NAMESPACE_UNKNOWN
const sal_uInt16 XML_NAMESPACE_XMLNS
static OUString getNamespaceURIFromToken(sal_Int32 nToken)
Definition: xmlimp.cxx:1949
DomBuilderContext(SvXMLImport &rImport, sal_Int32 nElement)
default constructor: create new DOM tree
static constexpr OUStringLiteral aNamespaceSeparator
Definition: xmlimp.hxx:528
#define XMLERROR_FLAG_WARNING
Definition: xmlerror.hxx:35
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createUnknownChildContext(const OUString &Namespace, const OUString &Name, const css::uno::Reference< css::xml::sax::XFastAttributeList > &Attribs) override
#define XMLERROR_NAMESPACE_TROUBLE
Definition: xmlerror.hxx:57
constexpr size_t NMSP_SHIFT
Definition: xmlimp.hxx:92
static OUString getNamespacePrefixFromToken(sal_Int32 nToken, const SvXMLNamespaceMap *pMap)
Definition: xmlimp.cxx:1931
const sal_uInt16 XML_NAMESPACE_NONE
static const OUString & getNameFromToken(sal_Int32 nToken)
Definition: xmlimp.cxx:1916
This class deliberately does not support XWeak, to improve performance when loading large documents...
Definition: xmlictxt.hxx:45
virtual ~DomBuilderContext() override
#define SAL_WARN_IF(condition, area, stream)
OReadImagesDocumentHandler::Image_XML_Namespace nNamespace
Reference< XComponentContext > getProcessComponentContext()
void SetError(sal_Int32 nId, const css::uno::Sequence< OUString > &rMsgParams, const OUString &rExceptionMessage, const css::uno::Reference< css::xml::sax::XLocator > &rLocator)
Record an error condition that occurred during import.
Sequence< sal_Int8 > aSeq
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &AttrList) override
virtual void SAL_CALL startFastElement(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
virtual void SAL_CALL startUnknownElement(const OUString &Namespace, const OUString &Name, const css::uno::Reference< css::xml::sax::XFastAttributeList > &Attribs) override
css::uno::Reference< css::xml::dom::XDocument > getTree()
access the DOM tree
static Reference< XNode > lcl_createDomInstance()
css::uno::Reference< css::xml::dom::XNode > mxNode
virtual void SAL_CALL characters(const OUString &rChars) override
This method is called for all characters that are contained in the current element.