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::dom::NodeType_ELEMENT_NODE;
52
53
54// helper functions; implemented below
56static Reference<XNode> lcl_createElement( SvXMLImport& rImport,
57 sal_Int32 nElement,
58 const Reference<XNode>& xParent);
60 const OUString & rNamespace, const OUString & rName,
61 const Reference<XNode>& xParent);
62
64 sal_Int32 nElement ) :
65 SvXMLImportContext( rImport ),
66 mxNode( lcl_createElement( rImport, nElement,
68{
69 SAL_WARN_IF( !mxNode.is(), "xmloff", "empty XNode not allowed" );
70 SAL_WARN_IF( !Reference<XElement>( mxNode, UNO_QUERY ).is(), "xmloff", "need element" );
71 SAL_WARN_IF( mxNode->getNodeType() != NodeType_ELEMENT_NODE, "xmloff", "need element" );
72}
73
75 const OUString & rNamespace, const OUString & rName ) :
76 SvXMLImportContext( rImport ),
77 mxNode( lcl_createElement( rNamespace, rName,
79{
80 SAL_WARN_IF( !mxNode.is(), "xmloff", "empty XNode not allowed" );
81 SAL_WARN_IF( !Reference<XElement>( mxNode, UNO_QUERY ).is(), "xmloff", "need element" );
82 SAL_WARN_IF( mxNode->getNodeType() != NodeType_ELEMENT_NODE, "xmloff", "need element" );
83}
84
85DomBuilderContext::DomBuilderContext( SvXMLImport& rImport,
86 sal_Int32 nElement,
87 Reference<XNode> const & xParent ) :
88 SvXMLImportContext( rImport ),
89 mxNode( lcl_createElement( rImport, nElement, xParent ) )
90{
91 SAL_WARN_IF( !mxNode.is(), "xmloff", "empty XNode not allowed" );
92 SAL_WARN_IF( !Reference<XElement>( mxNode, UNO_QUERY ).is(), "xmloff", "need element" );
93 SAL_WARN_IF( mxNode->getNodeType() != NodeType_ELEMENT_NODE, "xmloff", "need element" );
94}
95
96DomBuilderContext::DomBuilderContext( SvXMLImport& rImport,
97 const OUString & rNamespace, const OUString & rName,
98 Reference<XNode> const & xParent ) :
99 SvXMLImportContext( rImport ),
100 mxNode( lcl_createElement( rNamespace, rName, xParent ) )
101{
102 SAL_WARN_IF( !mxNode.is(), "xmloff", "empty XNode not allowed" );
103 SAL_WARN_IF( !Reference<XElement>( mxNode, UNO_QUERY ).is(), "xmloff", "need element" );
104 SAL_WARN_IF( mxNode->getNodeType() != NodeType_ELEMENT_NODE, "xmloff", "need element" );
105}
106
108{
109}
110
112{
113 SAL_WARN_IF( !mxNode.is(), "xmloff", "empty XNode not allowed" );
114 return mxNode->getOwnerDocument();
115}
116
117css::uno::Reference< css::xml::sax::XFastContextHandler > DomBuilderContext::createFastChildContext(
118 sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList >& )
119{
120 // create DomBuilder for subtree
121 return new DomBuilderContext( GetImport(), nElement, mxNode );
122}
123
124css::uno::Reference< css::xml::sax::XFastContextHandler > DomBuilderContext::createUnknownChildContext(
125 const OUString & rNamespace, const OUString &rName, const css::uno::Reference< css::xml::sax::XFastAttributeList >& )
126{
127 // create DomBuilder for subtree
128 return new DomBuilderContext( GetImport(), rNamespace, rName, mxNode );
129}
130
132 sal_Int32 /*nElement*/,
133 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
134{
135 SAL_WARN_IF( !mxNode.is(), "xmloff", "empty XNode not allowed" );
136 SAL_WARN_IF( !mxNode->getOwnerDocument().is(), "xmloff", "XNode must have XDocument" );
137
138 HandleAttributes(xAttrList);
139}
140
142 const OUString & /*rNamespace*/, const OUString & /*rName*/,
143 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
144{
145 SAL_WARN_IF( !mxNode.is(), "xmloff", "empty XNode not allowed" );
146 SAL_WARN_IF( !mxNode->getOwnerDocument().is(), "xmloff", "XNode must have XDocument" );
147 HandleAttributes(xAttrList);
148}
149
151 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
152{
153 // add attribute nodes to new node
154 for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
155 {
156 sal_Int32 nAttrToken = aIter.getToken();
157 // get name & value for attribute
158 sal_uInt16 nNamespace = (nAttrToken >> NMSP_SHIFT) - 1;
159 const OUString& rPrefix = SvXMLImport::getNamespacePrefixFromToken(nAttrToken, &GetImport().GetNamespaceMap());
160 const OUString& rLocalName = SvXMLImport::getNameFromToken( nAttrToken );
161 OUString aValue = aIter.toString();
162
163 // create attribute node and set value
164 Reference<XElement> xElement( mxNode, UNO_QUERY_THROW );
165 switch( nNamespace )
166 {
168 // no namespace: create a non-namespaced attribute
169 xElement->setAttribute( rLocalName, aValue );
170 break;
172 // namespace declaration: ignore, since the DOM tree handles these
173 // declarations implicitly
174 break;
176 // unknown namespace: illegal input. Raise Warning.
177 {
178 GetImport().SetError(
179 XMLERROR_FLAG_WARNING | XMLERROR_NAMESPACE_TROUBLE, { rLocalName, aValue } );
180 }
181 break;
182 default:
183 {
184 // a real and proper namespace: create namespaced attribute
185 OUString namespaceURI = SvXMLImport::getNamespaceURIFromToken(aIter.getToken());
186 OUString qualifiedName = rPrefix.isEmpty() ? rLocalName : rPrefix + SvXMLImport::aNamespaceSeparator + rLocalName;
187 xElement->setAttributeNS( namespaceURI, qualifiedName, aValue );
188 }
189 break;
190 }
191 }
192 const css::uno::Sequence< css::xml::Attribute > unknownAttribs = xAttrList->getUnknownAttributes();
193 for ( const auto& rUnknownAttrib : unknownAttribs )
194 {
195 // create attribute node and set value
196 Reference<XElement> xElement( mxNode, UNO_QUERY_THROW );
197
198 if (!rUnknownAttrib.NamespaceURL.isEmpty())
199 {
200 // unknown namespace: illegal input. Raise Warning.
201 GetImport().SetError(
202 XMLERROR_FLAG_WARNING | XMLERROR_NAMESPACE_TROUBLE, { rUnknownAttrib.Name, rUnknownAttrib.Value } );
203 }
204 else
205 {
206 // no namespace: create a non-namespaced attribute
207 xElement->setAttribute( rUnknownAttrib.Name, rUnknownAttrib.Value );
208 }
209 }
210}
211
212void DomBuilderContext::characters( const OUString& rCharacters )
213{
214 SAL_WARN_IF( !mxNode.is(), "xmloff", "empty XNode not allowed" );
215
216 // TODO: I assume adjacent text nodes should be joined, to preserve
217 // processing model? (I.e., if the SAX parser breaks a string into 2
218 // Characters(..) calls, the DOM model would still see only one child.)
219
220 // create text node and append to parent
221 Reference<XNode> xNew(
222 mxNode->getOwnerDocument()->createTextNode( rCharacters ),
223 UNO_QUERY_THROW );
224 mxNode->appendChild( xNew );
225}
226
227
228// helper function implementations
229
230
232{
234 SAL_WARN_IF( !xContext.is(), "xmloff", "can't get service factory" );
235
236 Reference<XDocumentBuilder> xBuilder( DocumentBuilder::create(xContext) );
237
238 return Reference<XNode>( xBuilder->newDocument(), UNO_QUERY_THROW );
239}
240
241static Reference<XNode> lcl_createElement( SvXMLImport& rImport,
242 sal_Int32 nElement,
243 const Reference<XNode>& xParent)
244{
245 SAL_WARN_IF( !xParent.is(), "xmloff", "need parent node" );
246
247 Reference<XDocument> xDocument = xParent->getOwnerDocument();
248 SAL_WARN_IF( !xDocument.is(), "xmloff", "no XDocument found!" );
249
250 // TODO: come up with proper way of handling namespaces; re-creating the
251 // namespace from the key is NOT a good idea, and will not work for
252 // multiple prefixes for the same namespace. Fortunately, those are rare.
253
254 Reference<XElement> xElement;
255 sal_uInt16 nNamespace = (nElement >> NMSP_SHIFT) - 1;
256 const OUString& rPrefix = SvXMLImport::getNamespacePrefixFromToken(nElement, &rImport.GetNamespaceMap());
257 const OUString& rLocalName = SvXMLImport::getNameFromToken( nElement );
258 switch( nNamespace )
259 {
261 // no namespace: use local name
262 xElement = xDocument->createElement( rLocalName );
263 break;
266 // both cases are illegal; raise warning (and use only local name)
267 xElement = xDocument->createElement( rLocalName );
268 {
269 Sequence<OUString> aSeq { rLocalName };
270 rImport.SetError(
272 }
273 break;
274 default:
275 // We are only given the prefix and the local name; thus we have to ask
276 // the namespace map to create a qualified name for us. Technically,
277 // this is a bug, since this will fail for multiple prefixes used for
278 // the same namespace.
279 OUString namespaceURI = SvXMLImport::getNamespaceURIFromToken(nElement);
280 OUString qualifiedName = rPrefix.isEmpty() ? rLocalName : rPrefix + SvXMLImport::aNamespaceSeparator + rLocalName;
281 xElement = xDocument->createElementNS(namespaceURI, qualifiedName);
282 break;
283 }
284 SAL_WARN_IF( !xElement.is(), "xmloff", "can't create element" );
285
286 // add new element to parent and return
287 xParent->appendChild( xElement );
288 return xElement;
289}
290
292 const OUString & rNamespace, const OUString & rName,
293 const Reference<XNode>& xParent)
294{
295 SAL_WARN_IF( !xParent.is(), "xmloff", "need parent node" );
296
297 Reference<XDocument> xDocument = xParent->getOwnerDocument();
298 SAL_WARN_IF( !xDocument.is(), "xmloff", "no XDocument found!" );
299
300 // TODO: come up with proper way of handling namespaces; re-creating the
301 // namespace from the key is NOT a good idea, and will not work for
302 // multiple prefixes for the same namespace. Fortunately, those are rare.
303
304 Reference<XElement> xElement;
305 if (rNamespace.isEmpty())
306 {
307 // no namespace: use local name
308 xElement = xDocument->createElement( rName );
309 }
310 else
311 {
312 xElement = xDocument->createElementNS(rNamespace, rName);
313 }
314
315 // add new element to parent and return
316 xParent->appendChild( xElement );
317 return xElement;
318}
319
320/* 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:93