xref: /AOO41X/main/xmloff/source/core/DomBuilderContext.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_xmloff.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "DomBuilderContext.hxx"
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include <xmloff/nmspmap.hxx>
34*cdf0e10cSrcweir #include <xmloff/xmlimp.hxx>
35*cdf0e10cSrcweir #include "xmloff/xmlerror.hxx"
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/uno/Reference.hxx>
39*cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.hxx>
40*cdf0e10cSrcweir #include <com/sun/star/xml/dom/XAttr.hpp>
41*cdf0e10cSrcweir #include <com/sun/star/xml/dom/XDocumentBuilder.hpp>
42*cdf0e10cSrcweir #include <com/sun/star/xml/dom/XNode.hpp>
43*cdf0e10cSrcweir #include <com/sun/star/xml/dom/XElement.hpp>
44*cdf0e10cSrcweir #include <com/sun/star/xml/sax/XAttributeList.hpp>
45*cdf0e10cSrcweir #include <com/sun/star/xml/dom/NodeType.hpp>
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir #include <rtl/ustring.hxx>
48*cdf0e10cSrcweir #include <tools/debug.hxx>
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir #include <unotools/processfactory.hxx>
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir using com::sun::star::lang::XMultiServiceFactory;
54*cdf0e10cSrcweir using com::sun::star::uno::Reference;
55*cdf0e10cSrcweir using com::sun::star::uno::Sequence;
56*cdf0e10cSrcweir using com::sun::star::uno::UNO_QUERY;
57*cdf0e10cSrcweir using com::sun::star::uno::UNO_QUERY_THROW;
58*cdf0e10cSrcweir using com::sun::star::xml::dom::XAttr;
59*cdf0e10cSrcweir using com::sun::star::xml::dom::XDocument;
60*cdf0e10cSrcweir using com::sun::star::xml::dom::XDocumentBuilder;
61*cdf0e10cSrcweir using com::sun::star::xml::dom::XNode;
62*cdf0e10cSrcweir using com::sun::star::xml::dom::XElement;
63*cdf0e10cSrcweir using com::sun::star::xml::sax::XAttributeList;
64*cdf0e10cSrcweir using com::sun::star::xml::dom::NodeType_ELEMENT_NODE;
65*cdf0e10cSrcweir using rtl::OUString;
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir // helper functions; implemented below
69*cdf0e10cSrcweir Reference<XNode> lcl_createDomInstance();
70*cdf0e10cSrcweir Reference<XNode> lcl_createElement( SvXMLImport& rImport,
71*cdf0e10cSrcweir                                     sal_uInt16 nPrefix,
72*cdf0e10cSrcweir                                     const OUString rLocalName,
73*cdf0e10cSrcweir                                     Reference<XNode> xParent);
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir DomBuilderContext::DomBuilderContext( SvXMLImport& rImport,
77*cdf0e10cSrcweir                                       sal_uInt16 nPrefix,
78*cdf0e10cSrcweir                                       const OUString& rLocalName ) :
79*cdf0e10cSrcweir     SvXMLImportContext( rImport, nPrefix, rLocalName ),
80*cdf0e10cSrcweir     mxNode( lcl_createElement( rImport, nPrefix, rLocalName,
81*cdf0e10cSrcweir                                lcl_createDomInstance() ) )
82*cdf0e10cSrcweir {
83*cdf0e10cSrcweir     DBG_ASSERT( mxNode.is(), "empty XNode not allowed" );
84*cdf0e10cSrcweir     DBG_ASSERT( Reference<XElement>( mxNode, UNO_QUERY ).is(), "need element" );
85*cdf0e10cSrcweir     DBG_ASSERT( mxNode->getNodeType() == NodeType_ELEMENT_NODE, "need element" );
86*cdf0e10cSrcweir }
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir DomBuilderContext::DomBuilderContext( SvXMLImport& rImport,
89*cdf0e10cSrcweir                                       sal_uInt16 nPrefix,
90*cdf0e10cSrcweir                                       const OUString& rLocalName,
91*cdf0e10cSrcweir                                       Reference<XNode>& xParent ) :
92*cdf0e10cSrcweir     SvXMLImportContext( rImport, nPrefix, rLocalName ),
93*cdf0e10cSrcweir     mxNode( lcl_createElement( rImport, nPrefix, rLocalName, xParent ) )
94*cdf0e10cSrcweir {
95*cdf0e10cSrcweir     DBG_ASSERT( mxNode.is(), "empty XNode not allowed" );
96*cdf0e10cSrcweir     DBG_ASSERT( Reference<XElement>( mxNode, UNO_QUERY ).is(), "need element" );
97*cdf0e10cSrcweir     DBG_ASSERT( mxNode->getNodeType() == NodeType_ELEMENT_NODE, "need element" );
98*cdf0e10cSrcweir }
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir DomBuilderContext::~DomBuilderContext()
101*cdf0e10cSrcweir {
102*cdf0e10cSrcweir }
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir Reference<XDocument> DomBuilderContext::getTree()
105*cdf0e10cSrcweir {
106*cdf0e10cSrcweir     DBG_ASSERT( mxNode.is(), "empty XNode not allowed" );
107*cdf0e10cSrcweir     return mxNode->getOwnerDocument();
108*cdf0e10cSrcweir }
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir Reference<XNode> DomBuilderContext::getNode()
111*cdf0e10cSrcweir {
112*cdf0e10cSrcweir     return mxNode;
113*cdf0e10cSrcweir }
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir SvXMLImportContext* DomBuilderContext::CreateChildContext(
117*cdf0e10cSrcweir     sal_uInt16 nPrefix,
118*cdf0e10cSrcweir     const OUString& rLocalName,
119*cdf0e10cSrcweir     const Reference<XAttributeList>& )
120*cdf0e10cSrcweir {
121*cdf0e10cSrcweir     // create DomBuilder for subtree
122*cdf0e10cSrcweir     return new DomBuilderContext( GetImport(), nPrefix, rLocalName, mxNode );
123*cdf0e10cSrcweir }
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir void DomBuilderContext::StartElement(
127*cdf0e10cSrcweir     const Reference<XAttributeList>& xAttrList )
128*cdf0e10cSrcweir {
129*cdf0e10cSrcweir     DBG_ASSERT( mxNode.is(), "empty XNode not allowed" );
130*cdf0e10cSrcweir     DBG_ASSERT( mxNode->getOwnerDocument().is(), "XNode must have XDocument" );
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir     // add attribute nodes to new node
133*cdf0e10cSrcweir     sal_Int16 nAttributeCount = xAttrList->getLength();
134*cdf0e10cSrcweir     for( sal_Int16 i = 0; i < nAttributeCount; i++ )
135*cdf0e10cSrcweir     {
136*cdf0e10cSrcweir         // get name & value for attribute
137*cdf0e10cSrcweir         const OUString& rName = xAttrList->getNameByIndex( i );
138*cdf0e10cSrcweir         const OUString& rValue = xAttrList->getValueByIndex( i );
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir         // namespace handling: determine namespace & namespace keykey
141*cdf0e10cSrcweir         OUString sNamespace;
142*cdf0e10cSrcweir         sal_uInt16 nNamespaceKey =
143*cdf0e10cSrcweir             GetImport().GetNamespaceMap()._GetKeyByAttrName(
144*cdf0e10cSrcweir                 rName, NULL, NULL, &sNamespace );
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir         // create attribute node and set value
147*cdf0e10cSrcweir         Reference<XElement> xElement( mxNode, UNO_QUERY_THROW );
148*cdf0e10cSrcweir         switch( nNamespaceKey )
149*cdf0e10cSrcweir         {
150*cdf0e10cSrcweir         case XML_NAMESPACE_NONE:
151*cdf0e10cSrcweir             // no namespace: create a non-namespaced attribute
152*cdf0e10cSrcweir             xElement->setAttribute( rName, rValue );
153*cdf0e10cSrcweir             break;
154*cdf0e10cSrcweir         case XML_NAMESPACE_XMLNS:
155*cdf0e10cSrcweir             // namespace declaration: ignore, since the DOM tree handles these
156*cdf0e10cSrcweir             // declarations implicitly
157*cdf0e10cSrcweir             break;
158*cdf0e10cSrcweir         case XML_NAMESPACE_UNKNOWN:
159*cdf0e10cSrcweir             // unknown namespace: illegal input. Raise Warning.
160*cdf0e10cSrcweir             {
161*cdf0e10cSrcweir                 Sequence<OUString> aSeq(2);
162*cdf0e10cSrcweir                 aSeq[0] = rName;
163*cdf0e10cSrcweir                 aSeq[1] = rValue;
164*cdf0e10cSrcweir                 GetImport().SetError(
165*cdf0e10cSrcweir                     XMLERROR_FLAG_WARNING | XMLERROR_NAMESPACE_TROUBLE, aSeq );
166*cdf0e10cSrcweir             }
167*cdf0e10cSrcweir             break;
168*cdf0e10cSrcweir         default:
169*cdf0e10cSrcweir             // a real and proper namespace: create namespaced attribute
170*cdf0e10cSrcweir             xElement->setAttributeNS( sNamespace, rName, rValue );
171*cdf0e10cSrcweir             break;
172*cdf0e10cSrcweir         }
173*cdf0e10cSrcweir     }
174*cdf0e10cSrcweir }
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir void DomBuilderContext::EndElement()
177*cdf0e10cSrcweir {
178*cdf0e10cSrcweir     // nothing to be done!
179*cdf0e10cSrcweir }
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir void DomBuilderContext::Characters( const OUString& rCharacters )
182*cdf0e10cSrcweir {
183*cdf0e10cSrcweir     DBG_ASSERT( mxNode.is(), "empty XNode not allowed" );
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir     // TODO: I assume adjacent text nodes should be joined, to preserve
186*cdf0e10cSrcweir     // processinf model? (I.e., if the SAX parser breaks a string into 2
187*cdf0e10cSrcweir     // Characters(..) calls, the DOM model would still see only one child.)
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir     // create text node and append to parent
190*cdf0e10cSrcweir     Reference<XNode> xNew(
191*cdf0e10cSrcweir         mxNode->getOwnerDocument()->createTextNode( rCharacters ),
192*cdf0e10cSrcweir         UNO_QUERY_THROW );
193*cdf0e10cSrcweir     mxNode->appendChild( xNew );
194*cdf0e10cSrcweir }
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir //
198*cdf0e10cSrcweir // helper function implementations
199*cdf0e10cSrcweir //
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir const sal_Char sDocumentBuilder[] = "com.sun.star.xml.dom.DocumentBuilder";
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir Reference<XNode> lcl_createDomInstance()
204*cdf0e10cSrcweir {
205*cdf0e10cSrcweir     Reference<XMultiServiceFactory> xFactory = utl::getProcessServiceFactory();
206*cdf0e10cSrcweir     DBG_ASSERT( xFactory.is(), "can't get service factory" );
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir     Reference<XDocumentBuilder> xBuilder(
209*cdf0e10cSrcweir         xFactory->createInstance(
210*cdf0e10cSrcweir             OUString( RTL_CONSTASCII_USTRINGPARAM( sDocumentBuilder ) ) ),
211*cdf0e10cSrcweir         UNO_QUERY_THROW );
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir     return Reference<XNode>( xBuilder->newDocument(), UNO_QUERY_THROW );
214*cdf0e10cSrcweir }
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir Reference<XNode> lcl_createElement( SvXMLImport& rImport,
217*cdf0e10cSrcweir                                     sal_uInt16 nPrefix,
218*cdf0e10cSrcweir                                     const OUString rLocalName,
219*cdf0e10cSrcweir                                     Reference<XNode> xParent)
220*cdf0e10cSrcweir {
221*cdf0e10cSrcweir     DBG_ASSERT( xParent.is(), "need parent node" );
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir     Reference<XDocument> xDocument = xParent->getOwnerDocument();
224*cdf0e10cSrcweir     DBG_ASSERT( xDocument.is(), "no XDocument found!" );
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir     // TODO: come up with proper way of handling namespaces; re-creating the
227*cdf0e10cSrcweir     // namespace from the key is NOT a good idea, and will not work for
228*cdf0e10cSrcweir     // multiple prefixes for the same namespace. Fortunately, those are rare.
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir     Reference<XElement> xElement;
231*cdf0e10cSrcweir     switch( nPrefix )
232*cdf0e10cSrcweir     {
233*cdf0e10cSrcweir     case XML_NAMESPACE_NONE:
234*cdf0e10cSrcweir         // no namespace: use local name
235*cdf0e10cSrcweir         xElement = xDocument->createElement( rLocalName );
236*cdf0e10cSrcweir         break;
237*cdf0e10cSrcweir     case XML_NAMESPACE_XMLNS:
238*cdf0e10cSrcweir     case XML_NAMESPACE_UNKNOWN:
239*cdf0e10cSrcweir         // both cases are illegal; raise warning (and use only local name)
240*cdf0e10cSrcweir         xElement = xDocument->createElement( rLocalName );
241*cdf0e10cSrcweir         {
242*cdf0e10cSrcweir             Sequence<OUString> aSeq(1);
243*cdf0e10cSrcweir             aSeq[0] = rLocalName;
244*cdf0e10cSrcweir             rImport.SetError(
245*cdf0e10cSrcweir                 XMLERROR_FLAG_WARNING | XMLERROR_NAMESPACE_TROUBLE, aSeq );
246*cdf0e10cSrcweir         }
247*cdf0e10cSrcweir         break;
248*cdf0e10cSrcweir     default:
249*cdf0e10cSrcweir         // We are only given the prefix and the local name; thus we have to ask
250*cdf0e10cSrcweir         // the namespace map to create a qualified name for us. Technically,
251*cdf0e10cSrcweir         // this is a bug, since this will fail for multiple prefixes used for
252*cdf0e10cSrcweir         // the same namespace.
253*cdf0e10cSrcweir         xElement = xDocument->createElementNS(
254*cdf0e10cSrcweir             rImport.GetNamespaceMap().GetNameByKey( nPrefix ),
255*cdf0e10cSrcweir             rImport.GetNamespaceMap().GetQNameByKey( nPrefix, rLocalName ) );
256*cdf0e10cSrcweir         break;
257*cdf0e10cSrcweir     }
258*cdf0e10cSrcweir     DBG_ASSERT( xElement.is(), "can't create element" );
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir     // add new element to parent and return
261*cdf0e10cSrcweir     Reference<XNode> xNode( xElement, UNO_QUERY_THROW );
262*cdf0e10cSrcweir     xParent->appendChild( xNode );
263*cdf0e10cSrcweir     return xNode;
264*cdf0e10cSrcweir }
265