Version 7.5.1.1, tag libreoffice-7.5.1.1
[LibreOffice.git] / xmloff / source / core / DomBuilderContext.cxx
blob066d8e6038c4138fc7acd5a17442a155e889c256
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 <DomBuilderContext.hxx>
23 #include <xmloff/namespacemap.hxx>
24 #include <xmloff/xmlimp.hxx>
25 #include <xmloff/xmlerror.hxx>
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>
35 #include <rtl/ustring.hxx>
36 #include <sal/log.hxx>
38 #include <comphelper/processfactory.hxx>
41 using com::sun::star::uno::XComponentContext;
42 using com::sun::star::uno::Reference;
43 using com::sun::star::uno::Sequence;
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;
55 // helper functions; implemented below
56 static Reference<XNode> lcl_createDomInstance();
57 static Reference<XNode> lcl_createElement( SvXMLImport& rImport,
58 sal_Int32 nElement,
59 const Reference<XNode>& xParent);
60 static Reference<XNode> lcl_createElement(
61 const OUString & rNamespace, const OUString & rName,
62 const Reference<XNode>& xParent);
64 DomBuilderContext::DomBuilderContext( SvXMLImport& rImport,
65 sal_Int32 nElement ) :
66 SvXMLImportContext( rImport ),
67 mxNode( lcl_createElement( rImport, nElement,
68 lcl_createDomInstance() ) )
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" );
75 DomBuilderContext::DomBuilderContext( SvXMLImport& rImport,
76 const OUString & rNamespace, const OUString & rName ) :
77 SvXMLImportContext( rImport ),
78 mxNode( lcl_createElement( rNamespace, rName,
79 lcl_createDomInstance() ) )
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" );
86 DomBuilderContext::DomBuilderContext( SvXMLImport& rImport,
87 sal_Int32 nElement,
88 Reference<XNode> const & xParent ) :
89 SvXMLImportContext( rImport ),
90 mxNode( lcl_createElement( rImport, nElement, xParent ) )
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" );
97 DomBuilderContext::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 ) )
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" );
108 DomBuilderContext::~DomBuilderContext()
112 Reference<XDocument> DomBuilderContext::getTree()
114 SAL_WARN_IF( !mxNode.is(), "xmloff", "empty XNode not allowed" );
115 return mxNode->getOwnerDocument();
118 css::uno::Reference< css::xml::sax::XFastContextHandler > DomBuilderContext::createFastChildContext(
119 sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList >& )
121 // create DomBuilder for subtree
122 return new DomBuilderContext( GetImport(), nElement, mxNode );
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 >& )
128 // create DomBuilder for subtree
129 return new DomBuilderContext( GetImport(), rNamespace, rName, mxNode );
132 void SAL_CALL DomBuilderContext::startFastElement(
133 sal_Int32 /*nElement*/,
134 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
136 SAL_WARN_IF( !mxNode.is(), "xmloff", "empty XNode not allowed" );
137 SAL_WARN_IF( !mxNode->getOwnerDocument().is(), "xmloff", "XNode must have XDocument" );
139 HandleAttributes(xAttrList);
142 void SAL_CALL DomBuilderContext::startUnknownElement(
143 const OUString & /*rNamespace*/, const OUString & /*rName*/,
144 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
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);
151 void DomBuilderContext::HandleAttributes(
152 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
154 // add attribute nodes to new node
155 for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
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();
164 // create attribute node and set value
165 Reference<XElement> xElement( mxNode, UNO_QUERY_THROW );
166 switch( nNamespace )
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;
176 case XML_NAMESPACE_UNKNOWN:
177 // unknown namespace: illegal input. Raise Warning.
179 GetImport().SetError(
180 XMLERROR_FLAG_WARNING | XMLERROR_NAMESPACE_TROUBLE, { rLocalName, aValue } );
182 break;
183 default:
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 );
190 break;
193 const css::uno::Sequence< css::xml::Attribute > unknownAttribs = xAttrList->getUnknownAttributes();
194 for ( const auto& rUnknownAttrib : unknownAttribs )
196 // create attribute node and set value
197 Reference<XElement> xElement( mxNode, UNO_QUERY_THROW );
199 if (!rUnknownAttrib.NamespaceURL.isEmpty())
201 // unknown namespace: illegal input. Raise Warning.
202 GetImport().SetError(
203 XMLERROR_FLAG_WARNING | XMLERROR_NAMESPACE_TROUBLE, { rUnknownAttrib.Name, rUnknownAttrib.Value } );
205 else
207 // no namespace: create a non-namespaced attribute
208 xElement->setAttribute( rUnknownAttrib.Name, rUnknownAttrib.Value );
213 void DomBuilderContext::characters( const OUString& rCharacters )
215 SAL_WARN_IF( !mxNode.is(), "xmloff", "empty XNode not allowed" );
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.)
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 );
229 // helper function implementations
232 static Reference<XNode> lcl_createDomInstance()
234 Reference<XComponentContext> xContext = comphelper::getProcessComponentContext();
235 SAL_WARN_IF( !xContext.is(), "xmloff", "can't get service factory" );
237 Reference<XDocumentBuilder> xBuilder( DocumentBuilder::create(xContext) );
239 return Reference<XNode>( xBuilder->newDocument(), UNO_QUERY_THROW );
242 static Reference<XNode> lcl_createElement( SvXMLImport& rImport,
243 sal_Int32 nElement,
244 const Reference<XNode>& xParent)
246 SAL_WARN_IF( !xParent.is(), "xmloff", "need parent node" );
248 Reference<XDocument> xDocument = xParent->getOwnerDocument();
249 SAL_WARN_IF( !xDocument.is(), "xmloff", "no XDocument found!" );
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.
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 )
261 case XML_NAMESPACE_NONE:
262 // no namespace: use local name
263 xElement = xDocument->createElement( rLocalName );
264 break;
265 case XML_NAMESPACE_XMLNS:
266 case XML_NAMESPACE_UNKNOWN:
267 // both cases are illegal; raise warning (and use only local name)
268 xElement = xDocument->createElement( rLocalName );
270 Sequence<OUString> aSeq { rLocalName };
271 rImport.SetError(
272 XMLERROR_FLAG_WARNING | XMLERROR_NAMESPACE_TROUBLE, aSeq );
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;
285 SAL_WARN_IF( !xElement.is(), "xmloff", "can't create element" );
287 // add new element to parent and return
288 xParent->appendChild( xElement );
289 return xElement;
292 static Reference<XNode> lcl_createElement(
293 const OUString & rNamespace, const OUString & rName,
294 const Reference<XNode>& xParent)
296 SAL_WARN_IF( !xParent.is(), "xmloff", "need parent node" );
298 Reference<XDocument> xDocument = xParent->getOwnerDocument();
299 SAL_WARN_IF( !xDocument.is(), "xmloff", "no XDocument found!" );
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.
305 Reference<XElement> xElement;
306 if (rNamespace.isEmpty())
308 // no namespace: use local name
309 xElement = xDocument->createElement( rName );
311 else
313 xElement = xDocument->createElementNS(rNamespace, rName);
316 // add new element to parent and return
317 xParent->appendChild( xElement );
318 return xElement;
321 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */