nss: upgrade to release 3.73
[LibreOffice.git] / xmloff / source / core / DomBuilderContext.cxx
blobf24990c73c94335fdc82770d8c8871b06713702d
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);
62 DomBuilderContext::DomBuilderContext( SvXMLImport& rImport,
63 sal_Int32 nElement ) :
64 SvXMLImportContext( rImport ),
65 mxNode( lcl_createElement( rImport, nElement,
66 lcl_createDomInstance() ) )
68 SAL_WARN_IF( !mxNode.is(), "xmloff", "empty XNode not allowed" );
69 SAL_WARN_IF( !Reference<XElement>( mxNode, UNO_QUERY ).is(), "xmloff", "need element" );
70 SAL_WARN_IF( mxNode->getNodeType() != NodeType_ELEMENT_NODE, "xmloff", "need element" );
73 DomBuilderContext::DomBuilderContext( SvXMLImport& rImport,
74 sal_Int32 nElement,
75 Reference<XNode> const & xParent ) :
76 SvXMLImportContext( rImport ),
77 mxNode( lcl_createElement( rImport, nElement, xParent ) )
79 SAL_WARN_IF( !mxNode.is(), "xmloff", "empty XNode not allowed" );
80 SAL_WARN_IF( !Reference<XElement>( mxNode, UNO_QUERY ).is(), "xmloff", "need element" );
81 SAL_WARN_IF( mxNode->getNodeType() != NodeType_ELEMENT_NODE, "xmloff", "need element" );
84 DomBuilderContext::~DomBuilderContext()
88 Reference<XDocument> DomBuilderContext::getTree()
90 SAL_WARN_IF( !mxNode.is(), "xmloff", "empty XNode not allowed" );
91 return mxNode->getOwnerDocument();
94 css::uno::Reference< css::xml::sax::XFastContextHandler > DomBuilderContext::createFastChildContext(
95 sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList >& )
97 // create DomBuilder for subtree
98 return new DomBuilderContext( GetImport(), nElement, mxNode );
102 void SAL_CALL DomBuilderContext::startFastElement(
103 sal_Int32 nElement,
104 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
106 SAL_WARN_IF( !mxNode.is(), "xmloff", "empty XNode not allowed" );
107 SAL_WARN_IF( !mxNode->getOwnerDocument().is(), "xmloff", "XNode must have XDocument" );
109 // add attribute nodes to new node
110 for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
112 sal_Int32 nAttrToken = aIter.getToken();
113 // get name & value for attribute
114 sal_uInt16 nNamespace = (nAttrToken >> NMSP_SHIFT) - 1;
115 const OUString& rPrefix = SvXMLImport::getNamespacePrefixFromToken(nAttrToken, &GetImport().GetNamespaceMap());
116 const OUString& rLocalName = SvXMLImport::getNameFromToken( nAttrToken );
117 OUString aValue = aIter.toString();
119 // create attribute node and set value
120 Reference<XElement> xElement( mxNode, UNO_QUERY_THROW );
121 switch( nNamespace )
123 case XML_NAMESPACE_NONE:
124 // no namespace: create a non-namespaced attribute
125 xElement->setAttribute( rLocalName, aValue );
126 break;
127 case XML_NAMESPACE_XMLNS:
128 // namespace declaration: ignore, since the DOM tree handles these
129 // declarations implicitly
130 break;
131 case XML_NAMESPACE_UNKNOWN:
132 // unknown namespace: illegal input. Raise Warning.
134 Sequence<OUString> aSeq(2);
135 aSeq[0] = rLocalName;
136 aSeq[1] = aValue;
137 GetImport().SetError(
138 XMLERROR_FLAG_WARNING | XMLERROR_NAMESPACE_TROUBLE, aSeq );
140 break;
141 default:
143 // a real and proper namespace: create namespaced attribute
144 OUString namespaceURI = SvXMLImport::getNamespaceURIFromToken(nElement);
145 OUString qualifiedName = rPrefix.isEmpty() ? rLocalName : rPrefix + SvXMLImport::aNamespaceSeparator + rLocalName;
146 xElement->setAttributeNS( namespaceURI, qualifiedName, aValue );
148 break;
153 void DomBuilderContext::characters( const OUString& rCharacters )
155 SAL_WARN_IF( !mxNode.is(), "xmloff", "empty XNode not allowed" );
157 // TODO: I assume adjacent text nodes should be joined, to preserve
158 // processing model? (I.e., if the SAX parser breaks a string into 2
159 // Characters(..) calls, the DOM model would still see only one child.)
161 // create text node and append to parent
162 Reference<XNode> xNew(
163 mxNode->getOwnerDocument()->createTextNode( rCharacters ),
164 UNO_QUERY_THROW );
165 mxNode->appendChild( xNew );
169 // helper function implementations
172 static Reference<XNode> lcl_createDomInstance()
174 Reference<XComponentContext> xContext = comphelper::getProcessComponentContext();
175 SAL_WARN_IF( !xContext.is(), "xmloff", "can't get service factory" );
177 Reference<XDocumentBuilder> xBuilder( DocumentBuilder::create(xContext) );
179 return Reference<XNode>( xBuilder->newDocument(), UNO_QUERY_THROW );
182 static Reference<XNode> lcl_createElement( SvXMLImport& rImport,
183 sal_Int32 nElement,
184 const Reference<XNode>& xParent)
186 SAL_WARN_IF( !xParent.is(), "xmloff", "need parent node" );
188 Reference<XDocument> xDocument = xParent->getOwnerDocument();
189 SAL_WARN_IF( !xDocument.is(), "xmloff", "no XDocument found!" );
191 // TODO: come up with proper way of handling namespaces; re-creating the
192 // namespace from the key is NOT a good idea, and will not work for
193 // multiple prefixes for the same namespace. Fortunately, those are rare.
195 Reference<XElement> xElement;
196 sal_uInt16 nNamespace = (nElement >> NMSP_SHIFT) - 1;
197 const OUString& rPrefix = SvXMLImport::getNamespacePrefixFromToken(nElement, &rImport.GetNamespaceMap());
198 const OUString& rLocalName = SvXMLImport::getNameFromToken( nElement );
199 switch( nNamespace )
201 case XML_NAMESPACE_NONE:
202 // no namespace: use local name
203 xElement = xDocument->createElement( rLocalName );
204 break;
205 case XML_NAMESPACE_XMLNS:
206 case XML_NAMESPACE_UNKNOWN:
207 // both cases are illegal; raise warning (and use only local name)
208 xElement = xDocument->createElement( rLocalName );
210 Sequence<OUString> aSeq { rLocalName };
211 rImport.SetError(
212 XMLERROR_FLAG_WARNING | XMLERROR_NAMESPACE_TROUBLE, aSeq );
214 break;
215 default:
216 // We are only given the prefix and the local name; thus we have to ask
217 // the namespace map to create a qualified name for us. Technically,
218 // this is a bug, since this will fail for multiple prefixes used for
219 // the same namespace.
220 OUString namespaceURI = SvXMLImport::getNamespaceURIFromToken(nElement);
221 OUString qualifiedName = rPrefix.isEmpty() ? rLocalName : rPrefix + SvXMLImport::aNamespaceSeparator + rLocalName;
222 xElement = xDocument->createElementNS(namespaceURI, qualifiedName);
223 break;
225 SAL_WARN_IF( !xElement.is(), "xmloff", "can't create element" );
227 // add new element to parent and return
228 xParent->appendChild( xElement );
229 return xElement;
232 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */