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
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
41using com::sun::star::uno::XComponentContext;
44using com::sun::star::uno::UNO_QUERY;
45using com::sun::star::uno::UNO_QUERY_THROW;
46using com::sun::star::xml::dom::DocumentBuilder;
47using com::sun::star::xml::dom::XDocument;
48using com::sun::star::xml::dom::XDocumentBuilder;
49using com::sun::star::xml::dom::XNode;
50using com::sun::star::xml::dom::XElement;
51using com::sun::star::xml::sax::XAttributeList;
52using com::sun::star::xml::dom::NodeType_ELEMENT_NODE;
53
54
55// helper functions; implemented below
57static Reference<XNode> lcl_createElement( SvXMLImport& rImport,
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
86DomBuilderContext::DomBuilderContext( SvXMLImport& rImport,
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
97DomBuilderContext::DomBuilderContext( SvXMLImport& rImport,
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
118css::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
125css::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 {
169 // no namespace: create a non-namespaced attribute
170 xElement->setAttribute( rLocalName, aValue );
171 break;
173 // namespace declaration: ignore, since the DOM tree handles these
174 // declarations implicitly
175 break;
177 // unknown namespace: illegal input. Raise Warning.
178 {
179 GetImport().SetError(
180 XMLERROR_FLAG_WARNING | XMLERROR_NAMESPACE_TROUBLE, { rLocalName, aValue } );
181 }
182 break;
183 default:
184 {
185 // a real and proper namespace: create namespaced attribute
186 OUString namespaceURI = SvXMLImport::getNamespaceURIFromToken(aIter.getToken());
187 OUString qualifiedName = rPrefix.isEmpty() ? rLocalName : rPrefix + SvXMLImport::aNamespaceSeparator + rLocalName;
188 xElement->setAttributeNS( namespaceURI, qualifiedName, aValue );
189 }
190 break;
191 }
192 }
193 const css::uno::Sequence< css::xml::Attribute > unknownAttribs = xAttrList->getUnknownAttributes();
194 for ( const auto& rUnknownAttrib : unknownAttribs )
195 {
196 // create attribute node and set value
197 Reference<XElement> xElement( mxNode, UNO_QUERY_THROW );
198
199 if (!rUnknownAttrib.NamespaceURL.isEmpty())
200 {
201 // unknown namespace: illegal input. Raise Warning.
202 GetImport().SetError(
203 XMLERROR_FLAG_WARNING | XMLERROR_NAMESPACE_TROUBLE, { rUnknownAttrib.Name, rUnknownAttrib.Value } );
204 }
205 else
206 {
207 // no namespace: create a non-namespaced attribute
208 xElement->setAttribute( rUnknownAttrib.Name, rUnknownAttrib.Value );
209 }
210 }
211}
212
213void DomBuilderContext::characters( const OUString& rCharacters )
214{
215 SAL_WARN_IF( !mxNode.is(), "xmloff", "empty XNode not allowed" );
216
217 // TODO: I assume adjacent text nodes should be joined, to preserve
218 // processing model? (I.e., if the SAX parser breaks a string into 2
219 // Characters(..) calls, the DOM model would still see only one child.)
220
221 // create text node and append to parent
222 Reference<XNode> xNew(
223 mxNode->getOwnerDocument()->createTextNode( rCharacters ),
224 UNO_QUERY_THROW );
225 mxNode->appendChild( xNew );
226}
227
228
229// helper function implementations
230
231
233{
235 SAL_WARN_IF( !xContext.is(), "xmloff", "can't get service factory" );
236
237 Reference<XDocumentBuilder> xBuilder( DocumentBuilder::create(xContext) );
238
239 return Reference<XNode>( xBuilder->newDocument(), UNO_QUERY_THROW );
240}
241
242static Reference<XNode> lcl_createElement( SvXMLImport& rImport,
243 sal_Int32 nElement,
244 const Reference<XNode>& xParent)
245{
246 SAL_WARN_IF( !xParent.is(), "xmloff", "need parent node" );
247
248 Reference<XDocument> xDocument = xParent->getOwnerDocument();
249 SAL_WARN_IF( !xDocument.is(), "xmloff", "no XDocument found!" );
250
251 // TODO: come up with proper way of handling namespaces; re-creating the
252 // namespace from the key is NOT a good idea, and will not work for
253 // multiple prefixes for the same namespace. Fortunately, those are rare.
254
255 Reference<XElement> xElement;
256 sal_uInt16 nNamespace = (nElement >> NMSP_SHIFT) - 1;
257 const OUString& rPrefix = SvXMLImport::getNamespacePrefixFromToken(nElement, &rImport.GetNamespaceMap());
258 const OUString& rLocalName = SvXMLImport::getNameFromToken( nElement );
259 switch( nNamespace )
260 {
262 // no namespace: use local name
263 xElement = xDocument->createElement( rLocalName );
264 break;
267 // both cases are illegal; raise warning (and use only local name)
268 xElement = xDocument->createElement( rLocalName );
269 {
270 Sequence<OUString> aSeq { rLocalName };
271 rImport.SetError(
273 }
274 break;
275 default:
276 // We are only given the prefix and the local name; thus we have to ask
277 // the namespace map to create a qualified name for us. Technically,
278 // this is a bug, since this will fail for multiple prefixes used for
279 // the same namespace.
280 OUString namespaceURI = SvXMLImport::getNamespaceURIFromToken(nElement);
281 OUString qualifiedName = rPrefix.isEmpty() ? rLocalName : rPrefix + SvXMLImport::aNamespaceSeparator + rLocalName;
282 xElement = xDocument->createElementNS(namespaceURI, qualifiedName);
283 break;
284 }
285 SAL_WARN_IF( !xElement.is(), "xmloff", "can't create element" );
286
287 // add new element to parent and return
288 xParent->appendChild( xElement );
289 return xElement;
290}
291
293 const OUString & rNamespace, const OUString & rName,
294 const Reference<XNode>& xParent)
295{
296 SAL_WARN_IF( !xParent.is(), "xmloff", "need parent node" );
297
298 Reference<XDocument> xDocument = xParent->getOwnerDocument();
299 SAL_WARN_IF( !xDocument.is(), "xmloff", "no XDocument found!" );
300
301 // TODO: come up with proper way of handling namespaces; re-creating the
302 // namespace from the key is NOT a good idea, and will not work for
303 // multiple prefixes for the same namespace. Fortunately, those are rare.
304
305 Reference<XElement> xElement;
306 if (rNamespace.isEmpty())
307 {
308 // no namespace: use local name
309 xElement = xDocument->createElement( rName );
310 }
311 else
312 {
313 xElement = xDocument->createElementNS(rNamespace, rName);
314 }
315
316 // add new element to parent and return
317 xParent->appendChild( xElement );
318 return xElement;
319}
320
321/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static Reference< XNode > lcl_createDomInstance()
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)
virtual void SAL_CALL startUnknownElement(const OUString &Namespace, const OUString &Name, const css::uno::Reference< css::xml::sax::XFastAttributeList > &Attribs) override
virtual void SAL_CALL startFastElement(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
virtual void SAL_CALL characters(const OUString &rChars) override
This method is called for all characters that are contained in the current element.
css::uno::Reference< css::xml::dom::XDocument > getTree()
access the DOM tree
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
css::uno::Reference< css::xml::dom::XNode > mxNode
virtual ~DomBuilderContext() override
DomBuilderContext(SvXMLImport &rImport, sal_Int32 nElement)
default constructor: create new DOM tree
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &AttrList) override
This class deliberately does not support XWeak, to improve performance when loading large documents.
Definition: xmlictxt.hxx:48
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:60
Sequence< sal_Int8 > aSeq
#define SAL_WARN_IF(condition, area, stream)
Reference< XComponentContext > getProcessComponentContext()
const sal_uInt16 XML_NAMESPACE_NONE
const sal_uInt16 XML_NAMESPACE_UNKNOWN
const sal_uInt16 XML_NAMESPACE_XMLNS
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
OReadStatusBarDocumentHandler::StatusBar_XML_Namespace nNamespace
#define XMLERROR_NAMESPACE_TROUBLE
Definition: xmlerror.hxx:57
#define XMLERROR_FLAG_WARNING
Definition: xmlerror.hxx:35
constexpr size_t NMSP_SHIFT
Definition: xmlimp.hxx:94