1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: DomBuilderContext.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmloff.hxx"
34 #include "DomBuilderContext.hxx"
36 #include <xmloff/nmspmap.hxx>
37 #include <xmloff/xmlimp.hxx>
38 #include "xmlerror.hxx"
40 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
41 #include <com/sun/star/uno/Reference.hxx>
42 #include <com/sun/star/uno/Sequence.hxx>
43 #include <com/sun/star/xml/dom/XAttr.hpp>
44 #include <com/sun/star/xml/dom/XDocumentBuilder.hpp>
45 #include <com/sun/star/xml/dom/XNode.hpp>
46 #include <com/sun/star/xml/dom/XElement.hpp>
47 #include <com/sun/star/xml/sax/XAttributeList.hpp>
48 #include <com/sun/star/xml/dom/NodeType.hpp>
50 #include <rtl/ustring.hxx>
51 #include <tools/debug.hxx>
53 #include <unotools/processfactory.hxx>
56 using com::sun::star::lang::XMultiServiceFactory
;
57 using com::sun::star::uno::Reference
;
58 using com::sun::star::uno::Sequence
;
59 using com::sun::star::uno::UNO_QUERY
;
60 using com::sun::star::uno::UNO_QUERY_THROW
;
61 using com::sun::star::xml::dom::XAttr
;
62 using com::sun::star::xml::dom::XDocument
;
63 using com::sun::star::xml::dom::XDocumentBuilder
;
64 using com::sun::star::xml::dom::XNode
;
65 using com::sun::star::xml::dom::XElement
;
66 using com::sun::star::xml::sax::XAttributeList
;
67 using com::sun::star::xml::dom::NodeType_ELEMENT_NODE
;
71 // helper functions; implemented below
72 Reference
<XNode
> lcl_createDomInstance();
73 Reference
<XNode
> lcl_createElement( SvXMLImport
& rImport
,
75 const OUString rLocalName
,
76 Reference
<XNode
> xParent
);
79 DomBuilderContext::DomBuilderContext( SvXMLImport
& rImport
,
81 const OUString
& rLocalName
) :
82 SvXMLImportContext( rImport
, nPrefix
, rLocalName
),
83 mxNode( lcl_createElement( rImport
, nPrefix
, rLocalName
,
84 lcl_createDomInstance() ) )
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( SvXMLImport
& rImport
,
93 const OUString
& rLocalName
,
94 Reference
<XNode
>& xParent
) :
95 SvXMLImportContext( rImport
, nPrefix
, rLocalName
),
96 mxNode( lcl_createElement( rImport
, nPrefix
, rLocalName
, xParent
) )
98 DBG_ASSERT( mxNode
.is(), "empty XNode not allowed" );
99 DBG_ASSERT( Reference
<XElement
>( mxNode
, UNO_QUERY
).is(), "need element" );
100 DBG_ASSERT( mxNode
->getNodeType() == NodeType_ELEMENT_NODE
, "need element" );
103 DomBuilderContext::~DomBuilderContext()
107 Reference
<XDocument
> DomBuilderContext::getTree()
109 DBG_ASSERT( mxNode
.is(), "empty XNode not allowed" );
110 return mxNode
->getOwnerDocument();
113 Reference
<XNode
> DomBuilderContext::getNode()
119 SvXMLImportContext
* DomBuilderContext::CreateChildContext(
121 const OUString
& rLocalName
,
122 const Reference
<XAttributeList
>& )
124 // create DomBuilder for subtree
125 return new DomBuilderContext( GetImport(), nPrefix
, rLocalName
, mxNode
);
129 void DomBuilderContext::StartElement(
130 const Reference
<XAttributeList
>& xAttrList
)
132 DBG_ASSERT( mxNode
.is(), "empty XNode not allowed" );
133 DBG_ASSERT( mxNode
->getOwnerDocument().is(), "XNode must have XDocument" );
135 // add attribute nodes to new node
136 sal_Int16 nAttributeCount
= xAttrList
->getLength();
137 for( sal_Int16 i
= 0; i
< nAttributeCount
; i
++ )
139 // get name & value for attribute
140 const OUString
& rName
= xAttrList
->getNameByIndex( i
);
141 const OUString
& rValue
= xAttrList
->getValueByIndex( i
);
143 // namespace handling: determine namespace & namespace keykey
145 sal_uInt16 nNamespaceKey
=
146 GetImport().GetNamespaceMap()._GetKeyByAttrName(
147 rName
, NULL
, NULL
, &sNamespace
);
149 // create attribute node and set value
150 Reference
<XElement
> xElement( mxNode
, UNO_QUERY_THROW
);
151 switch( nNamespaceKey
)
153 case XML_NAMESPACE_NONE
:
154 // no namespace: create a non-namespaced attribute
155 xElement
->setAttribute( rName
, rValue
);
157 case XML_NAMESPACE_XMLNS
:
158 // namespace declaration: ignore, since the DOM tree handles these
159 // declarations implicitly
161 case XML_NAMESPACE_UNKNOWN
:
162 // unknown namespace: illegal input. Raise Warning.
164 Sequence
<OUString
> aSeq(2);
167 GetImport().SetError(
168 XMLERROR_FLAG_WARNING
| XMLERROR_NAMESPACE_TROUBLE
, aSeq
);
172 // a real and proper namespace: create namespaced attribute
173 xElement
->setAttributeNS( sNamespace
, rName
, rValue
);
179 void DomBuilderContext::EndElement()
181 // nothing to be done!
184 void DomBuilderContext::Characters( const OUString
& rCharacters
)
186 DBG_ASSERT( mxNode
.is(), "empty XNode not allowed" );
188 // TODO: I assume adjacent text nodes should be joined, to preserve
189 // processinf model? (I.e., if the SAX parser breaks a string into 2
190 // Characters(..) calls, the DOM model would still see only one child.)
192 // create text node and append to parent
193 Reference
<XNode
> xNew(
194 mxNode
->getOwnerDocument()->createTextNode( rCharacters
),
196 mxNode
->appendChild( xNew
);
201 // helper function implementations
204 const sal_Char sDocumentBuilder
[] = "com.sun.star.xml.dom.DocumentBuilder";
206 Reference
<XNode
> lcl_createDomInstance()
208 Reference
<XMultiServiceFactory
> xFactory
= utl::getProcessServiceFactory();
209 DBG_ASSERT( xFactory
.is(), "can't get service factory" );
211 Reference
<XDocumentBuilder
> xBuilder(
212 xFactory
->createInstance(
213 OUString( RTL_CONSTASCII_USTRINGPARAM( sDocumentBuilder
) ) ),
216 return Reference
<XNode
>( xBuilder
->newDocument(), UNO_QUERY_THROW
);
219 Reference
<XNode
> lcl_createElement( SvXMLImport
& rImport
,
221 const OUString rLocalName
,
222 Reference
<XNode
> xParent
)
224 DBG_ASSERT( xParent
.is(), "need parent node" );
226 Reference
<XDocument
> xDocument
= xParent
->getOwnerDocument();
227 DBG_ASSERT( xDocument
.is(), "no XDocument found!" );
229 // TODO: come up with proper way of handling namespaces; re-creating the
230 // namespace from the key is NOT a good idea, and will not work for
231 // multiple prefixes for the same namespace. Fortunately, those are rare.
233 Reference
<XElement
> xElement
;
236 case XML_NAMESPACE_NONE
:
237 // no namespace: use local name
238 xElement
= xDocument
->createElement( rLocalName
);
240 case XML_NAMESPACE_XMLNS
:
241 case XML_NAMESPACE_UNKNOWN
:
242 // both cases are illegal; raise warning (and use only local name)
243 xElement
= xDocument
->createElement( rLocalName
);
245 Sequence
<OUString
> aSeq(1);
246 aSeq
[0] = rLocalName
;
248 XMLERROR_FLAG_WARNING
| XMLERROR_NAMESPACE_TROUBLE
, aSeq
);
252 // We are only given the prefix and the local name; thus we have to ask
253 // the namespace map to create a qualified name for us. Technically,
254 // this is a bug, since this will fail for multiple prefixes used for
255 // the same namespace.
256 xElement
= xDocument
->createElementNS(
257 rImport
.GetNamespaceMap().GetNameByKey( nPrefix
),
258 rImport
.GetNamespaceMap().GetQNameByKey( nPrefix
, rLocalName
) );
261 DBG_ASSERT( xElement
.is(), "can't create element" );
263 // add new element to parent and return
264 Reference
<XNode
> xNode( xElement
, UNO_QUERY_THROW
);
265 xParent
->appendChild( xNode
);