bump product version to 5.0.4.1
[LibreOffice.git] / xmloff / source / core / DomBuilderContext.cxx
blobd3a577af35f02d031013c510908e21ffc0922380
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/nmspmap.hxx>
24 #include <xmloff/xmlimp.hxx>
25 #include <xmloff/xmlerror.hxx>
27 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
28 #include <com/sun/star/uno/Reference.hxx>
29 #include <com/sun/star/uno/Sequence.hxx>
30 #include <com/sun/star/xml/dom/XAttr.hpp>
31 #include <com/sun/star/xml/dom/DocumentBuilder.hpp>
32 #include <com/sun/star/xml/dom/XNode.hpp>
33 #include <com/sun/star/xml/dom/XElement.hpp>
34 #include <com/sun/star/xml/sax/XAttributeList.hpp>
35 #include <com/sun/star/xml/dom/NodeType.hpp>
37 #include <rtl/ustring.hxx>
38 #include <tools/debug.hxx>
40 #include <comphelper/processfactory.hxx>
43 using com::sun::star::lang::XMultiServiceFactory;
44 using com::sun::star::uno::XComponentContext;
45 using com::sun::star::uno::Reference;
46 using com::sun::star::uno::Sequence;
47 using com::sun::star::uno::UNO_QUERY;
48 using com::sun::star::uno::UNO_QUERY_THROW;
49 using com::sun::star::xml::dom::DocumentBuilder;
50 using com::sun::star::xml::dom::XAttr;
51 using com::sun::star::xml::dom::XDocument;
52 using com::sun::star::xml::dom::XDocumentBuilder;
53 using com::sun::star::xml::dom::XNode;
54 using com::sun::star::xml::dom::XElement;
55 using com::sun::star::xml::sax::XAttributeList;
56 using com::sun::star::xml::dom::NodeType_ELEMENT_NODE;
59 // helper functions; implemented below
60 static Reference<XNode> lcl_createDomInstance();
61 static Reference<XNode> lcl_createElement( SvXMLImport& rImport,
62 sal_uInt16 nPrefix,
63 const OUString& rLocalName,
64 Reference<XNode> xParent);
67 DomBuilderContext::DomBuilderContext( SvXMLImport& rImport,
68 sal_uInt16 nPrefix,
69 const OUString& rLocalName ) :
70 SvXMLImportContext( rImport, nPrefix, rLocalName ),
71 mxNode( lcl_createElement( rImport, nPrefix, rLocalName,
72 lcl_createDomInstance() ) )
74 DBG_ASSERT( mxNode.is(), "empty XNode not allowed" );
75 DBG_ASSERT( Reference<XElement>( mxNode, UNO_QUERY ).is(), "need element" );
76 DBG_ASSERT( mxNode->getNodeType() == NodeType_ELEMENT_NODE, "need element" );
79 DomBuilderContext::DomBuilderContext( SvXMLImport& rImport,
80 sal_uInt16 nPrefix,
81 const OUString& rLocalName,
82 Reference<XNode>& xParent ) :
83 SvXMLImportContext( rImport, nPrefix, rLocalName ),
84 mxNode( lcl_createElement( rImport, nPrefix, rLocalName, xParent ) )
86 DBG_ASSERT( mxNode.is(), "empty XNode not allowed" );
87 DBG_ASSERT( Reference<XElement>( mxNode, UNO_QUERY ).is(), "need element" );
88 DBG_ASSERT( mxNode->getNodeType() == NodeType_ELEMENT_NODE, "need element" );
91 DomBuilderContext::~DomBuilderContext()
95 Reference<XDocument> DomBuilderContext::getTree()
97 DBG_ASSERT( mxNode.is(), "empty XNode not allowed" );
98 return mxNode->getOwnerDocument();
101 SvXMLImportContext* DomBuilderContext::CreateChildContext(
102 sal_uInt16 nPrefix,
103 const OUString& rLocalName,
104 const Reference<XAttributeList>& )
106 // create DomBuilder for subtree
107 return new DomBuilderContext( GetImport(), nPrefix, rLocalName, mxNode );
111 void DomBuilderContext::StartElement(
112 const Reference<XAttributeList>& xAttrList )
114 DBG_ASSERT( mxNode.is(), "empty XNode not allowed" );
115 DBG_ASSERT( mxNode->getOwnerDocument().is(), "XNode must have XDocument" );
117 // add attribute nodes to new node
118 sal_Int16 nAttributeCount = xAttrList->getLength();
119 for( sal_Int16 i = 0; i < nAttributeCount; i++ )
121 // get name & value for attribute
122 const OUString& rName = xAttrList->getNameByIndex( i );
123 const OUString& rValue = xAttrList->getValueByIndex( i );
125 // namespace handling: determine namespace & namespace keykey
126 OUString sNamespace;
127 sal_uInt16 nNamespaceKey =
128 GetImport().GetNamespaceMap()._GetKeyByAttrName(
129 rName, NULL, NULL, &sNamespace );
131 // create attribute node and set value
132 Reference<XElement> xElement( mxNode, UNO_QUERY_THROW );
133 switch( nNamespaceKey )
135 case XML_NAMESPACE_NONE:
136 // no namespace: create a non-namespaced attribute
137 xElement->setAttribute( rName, rValue );
138 break;
139 case XML_NAMESPACE_XMLNS:
140 // namespace declaration: ignore, since the DOM tree handles these
141 // declarations implicitly
142 break;
143 case XML_NAMESPACE_UNKNOWN:
144 // unknown namespace: illegal input. Raise Warning.
146 Sequence<OUString> aSeq(2);
147 aSeq[0] = rName;
148 aSeq[1] = rValue;
149 GetImport().SetError(
150 XMLERROR_FLAG_WARNING | XMLERROR_NAMESPACE_TROUBLE, aSeq );
152 break;
153 default:
154 // a real and proper namespace: create namespaced attribute
155 xElement->setAttributeNS( sNamespace, rName, rValue );
156 break;
161 void DomBuilderContext::EndElement()
163 // nothing to be done!
166 void DomBuilderContext::Characters( const OUString& rCharacters )
168 DBG_ASSERT( mxNode.is(), "empty XNode not allowed" );
170 // TODO: I assume adjacent text nodes should be joined, to preserve
171 // processinf model? (I.e., if the SAX parser breaks a string into 2
172 // Characters(..) calls, the DOM model would still see only one child.)
174 // create text node and append to parent
175 Reference<XNode> xNew(
176 mxNode->getOwnerDocument()->createTextNode( rCharacters ),
177 UNO_QUERY_THROW );
178 mxNode->appendChild( xNew );
183 // helper function implementations
186 static Reference<XNode> lcl_createDomInstance()
188 Reference<XComponentContext> xContext = comphelper::getProcessComponentContext();
189 DBG_ASSERT( xContext.is(), "can't get service factory" );
191 Reference<XDocumentBuilder> xBuilder( DocumentBuilder::create(xContext) );
193 return Reference<XNode>( xBuilder->newDocument(), UNO_QUERY_THROW );
196 static Reference<XNode> lcl_createElement( SvXMLImport& rImport,
197 sal_uInt16 nPrefix,
198 const OUString& rLocalName,
199 Reference<XNode> xParent)
201 DBG_ASSERT( xParent.is(), "need parent node" );
203 Reference<XDocument> xDocument = xParent->getOwnerDocument();
204 DBG_ASSERT( xDocument.is(), "no XDocument found!" );
206 // TODO: come up with proper way of handling namespaces; re-creating the
207 // namespace from the key is NOT a good idea, and will not work for
208 // multiple prefixes for the same namespace. Fortunately, those are rare.
210 Reference<XElement> xElement;
211 switch( nPrefix )
213 case XML_NAMESPACE_NONE:
214 // no namespace: use local name
215 xElement = xDocument->createElement( rLocalName );
216 break;
217 case XML_NAMESPACE_XMLNS:
218 case XML_NAMESPACE_UNKNOWN:
219 // both cases are illegal; raise warning (and use only local name)
220 xElement = xDocument->createElement( rLocalName );
222 Sequence<OUString> aSeq(1);
223 aSeq[0] = rLocalName;
224 rImport.SetError(
225 XMLERROR_FLAG_WARNING | XMLERROR_NAMESPACE_TROUBLE, aSeq );
227 break;
228 default:
229 // We are only given the prefix and the local name; thus we have to ask
230 // the namespace map to create a qualified name for us. Technically,
231 // this is a bug, since this will fail for multiple prefixes used for
232 // the same namespace.
233 xElement = xDocument->createElementNS(
234 rImport.GetNamespaceMap().GetNameByKey( nPrefix ),
235 rImport.GetNamespaceMap().GetQNameByKey( nPrefix, rLocalName ) );
236 break;
238 DBG_ASSERT( xElement.is(), "can't create element" );
240 // add new element to parent and return
241 xParent->appendChild( xElement );
242 return xElement;
245 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */