1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/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 <tools/debug.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
,
59 const OUString
& rLocalName
,
60 const Reference
<XNode
>& xParent
);
63 DomBuilderContext::DomBuilderContext( SvXMLImport
& rImport
,
65 const OUString
& rLocalName
) :
66 SvXMLImportContext( rImport
, nPrefix
, rLocalName
),
67 mxNode( lcl_createElement( rImport
, nPrefix
, rLocalName
,
68 lcl_createDomInstance() ) )
70 DBG_ASSERT( mxNode
.is(), "empty XNode not allowed" );
71 DBG_ASSERT( Reference
<XElement
>( mxNode
, UNO_QUERY
).is(), "need element" );
72 DBG_ASSERT( mxNode
->getNodeType() == NodeType_ELEMENT_NODE
, "need element" );
75 DomBuilderContext::DomBuilderContext( SvXMLImport
& rImport
,
77 const OUString
& rLocalName
,
78 Reference
<XNode
>& xParent
) :
79 SvXMLImportContext( rImport
, nPrefix
, rLocalName
),
80 mxNode( lcl_createElement( rImport
, nPrefix
, rLocalName
, xParent
) )
82 DBG_ASSERT( mxNode
.is(), "empty XNode not allowed" );
83 DBG_ASSERT( Reference
<XElement
>( mxNode
, UNO_QUERY
).is(), "need element" );
84 DBG_ASSERT( mxNode
->getNodeType() == NodeType_ELEMENT_NODE
, "need element" );
87 DomBuilderContext::~DomBuilderContext()
91 Reference
<XDocument
> DomBuilderContext::getTree()
93 DBG_ASSERT( mxNode
.is(), "empty XNode not allowed" );
94 return mxNode
->getOwnerDocument();
97 SvXMLImportContext
* DomBuilderContext::CreateChildContext(
99 const OUString
& rLocalName
,
100 const Reference
<XAttributeList
>& )
102 // create DomBuilder for subtree
103 return new DomBuilderContext( GetImport(), nPrefix
, rLocalName
, mxNode
);
107 void DomBuilderContext::StartElement(
108 const Reference
<XAttributeList
>& xAttrList
)
110 DBG_ASSERT( mxNode
.is(), "empty XNode not allowed" );
111 DBG_ASSERT( mxNode
->getOwnerDocument().is(), "XNode must have XDocument" );
113 // add attribute nodes to new node
114 sal_Int16 nAttributeCount
= xAttrList
->getLength();
115 for( sal_Int16 i
= 0; i
< nAttributeCount
; i
++ )
117 // get name & value for attribute
118 const OUString
& rName
= xAttrList
->getNameByIndex( i
);
119 const OUString
& rValue
= xAttrList
->getValueByIndex( i
);
121 // namespace handling: determine namespace & namespace key
123 sal_uInt16 nNamespaceKey
=
124 GetImport().GetNamespaceMap().GetKeyByAttrName_(
125 rName
, nullptr, nullptr, &sNamespace
);
127 // create attribute node and set value
128 Reference
<XElement
> xElement( mxNode
, UNO_QUERY_THROW
);
129 switch( nNamespaceKey
)
131 case XML_NAMESPACE_NONE
:
132 // no namespace: create a non-namespaced attribute
133 xElement
->setAttribute( rName
, rValue
);
135 case XML_NAMESPACE_XMLNS
:
136 // namespace declaration: ignore, since the DOM tree handles these
137 // declarations implicitly
139 case XML_NAMESPACE_UNKNOWN
:
140 // unknown namespace: illegal input. Raise Warning.
142 Sequence
<OUString
> aSeq(2);
145 GetImport().SetError(
146 XMLERROR_FLAG_WARNING
| XMLERROR_NAMESPACE_TROUBLE
, aSeq
);
150 // a real and proper namespace: create namespaced attribute
151 xElement
->setAttributeNS( sNamespace
, rName
, rValue
);
157 void DomBuilderContext::EndElement()
159 // nothing to be done!
162 void DomBuilderContext::Characters( const OUString
& rCharacters
)
164 DBG_ASSERT( mxNode
.is(), "empty XNode not allowed" );
166 // TODO: I assume adjacent text nodes should be joined, to preserve
167 // processinf model? (I.e., if the SAX parser breaks a string into 2
168 // Characters(..) calls, the DOM model would still see only one child.)
170 // create text node and append to parent
171 Reference
<XNode
> xNew(
172 mxNode
->getOwnerDocument()->createTextNode( rCharacters
),
174 mxNode
->appendChild( xNew
);
178 // helper function implementations
181 static Reference
<XNode
> lcl_createDomInstance()
183 Reference
<XComponentContext
> xContext
= comphelper::getProcessComponentContext();
184 DBG_ASSERT( xContext
.is(), "can't get service factory" );
186 Reference
<XDocumentBuilder
> xBuilder( DocumentBuilder::create(xContext
) );
188 return Reference
<XNode
>( xBuilder
->newDocument(), UNO_QUERY_THROW
);
191 static Reference
<XNode
> lcl_createElement( SvXMLImport
& rImport
,
193 const OUString
& rLocalName
,
194 const Reference
<XNode
>& xParent
)
196 DBG_ASSERT( xParent
.is(), "need parent node" );
198 Reference
<XDocument
> xDocument
= xParent
->getOwnerDocument();
199 DBG_ASSERT( xDocument
.is(), "no XDocument found!" );
201 // TODO: come up with proper way of handling namespaces; re-creating the
202 // namespace from the key is NOT a good idea, and will not work for
203 // multiple prefixes for the same namespace. Fortunately, those are rare.
205 Reference
<XElement
> xElement
;
208 case XML_NAMESPACE_NONE
:
209 // no namespace: use local name
210 xElement
= xDocument
->createElement( rLocalName
);
212 case XML_NAMESPACE_XMLNS
:
213 case XML_NAMESPACE_UNKNOWN
:
214 // both cases are illegal; raise warning (and use only local name)
215 xElement
= xDocument
->createElement( rLocalName
);
217 Sequence
<OUString
> aSeq
{ rLocalName
};
219 XMLERROR_FLAG_WARNING
| XMLERROR_NAMESPACE_TROUBLE
, aSeq
);
223 // We are only given the prefix and the local name; thus we have to ask
224 // the namespace map to create a qualified name for us. Technically,
225 // this is a bug, since this will fail for multiple prefixes used for
226 // the same namespace.
227 xElement
= xDocument
->createElementNS(
228 rImport
.GetNamespaceMap().GetNameByKey( nPrefix
),
229 rImport
.GetNamespaceMap().GetQNameByKey( nPrefix
, rLocalName
) );
232 DBG_ASSERT( xElement
.is(), "can't create element" );
234 // add new element to parent and return
235 xParent
->appendChild( xElement
);
239 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */