xref: /AOO41X/main/unoxml/source/dom/element.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 #include <element.hxx>
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include <string.h>
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #include <boost/shared_ptr.hpp>
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include <com/sun/star/xml/sax/FastToken.hdl>
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir #include <comphelper/attributelist.hxx>
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir #include <node.hxx>
41*cdf0e10cSrcweir #include <attr.hxx>
42*cdf0e10cSrcweir #include <elementlist.hxx>
43*cdf0e10cSrcweir #include <attributesmap.hxx>
44*cdf0e10cSrcweir #include <document.hxx>
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir #include "../events/mutationevent.hxx"
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir namespace DOM
50*cdf0e10cSrcweir {
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir     CElement::CElement(CDocument const& rDocument, ::osl::Mutex const& rMutex,
53*cdf0e10cSrcweir             xmlNodePtr const pNode)
54*cdf0e10cSrcweir         : CElement_Base(rDocument, rMutex, NodeType_ELEMENT_NODE, pNode)
55*cdf0e10cSrcweir     {
56*cdf0e10cSrcweir     }
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir     void CElement::saxify(const Reference< XDocumentHandler >& i_xHandler)
59*cdf0e10cSrcweir     {
60*cdf0e10cSrcweir         if (!i_xHandler.is()) throw RuntimeException();
61*cdf0e10cSrcweir         comphelper::AttributeList *pAttrs =
62*cdf0e10cSrcweir             new comphelper::AttributeList();
63*cdf0e10cSrcweir         OUString type = OUString::createFromAscii("");
64*cdf0e10cSrcweir         // add namespace definitions to attributes
65*cdf0e10cSrcweir         for (xmlNsPtr pNs = m_aNodePtr->nsDef; pNs != 0; pNs = pNs->next) {
66*cdf0e10cSrcweir             const xmlChar *pPrefix = pNs->prefix;
67*cdf0e10cSrcweir             OUString prefix(reinterpret_cast<const sal_Char*>(pPrefix),
68*cdf0e10cSrcweir                 strlen(reinterpret_cast<const char*>(pPrefix)),
69*cdf0e10cSrcweir                 RTL_TEXTENCODING_UTF8);
70*cdf0e10cSrcweir             OUString name = (prefix.equalsAscii(""))
71*cdf0e10cSrcweir                 ? OUString::createFromAscii("xmlns")
72*cdf0e10cSrcweir                 : OUString::createFromAscii("xmlns:") + prefix;
73*cdf0e10cSrcweir             const xmlChar *pHref = pNs->href;
74*cdf0e10cSrcweir             OUString val(reinterpret_cast<const sal_Char*>(pHref),
75*cdf0e10cSrcweir                 strlen(reinterpret_cast<const char*>(pHref)),
76*cdf0e10cSrcweir                 RTL_TEXTENCODING_UTF8);
77*cdf0e10cSrcweir             pAttrs->AddAttribute(name, type, val);
78*cdf0e10cSrcweir         }
79*cdf0e10cSrcweir         // add attributes
80*cdf0e10cSrcweir         for (xmlAttrPtr pAttr = m_aNodePtr->properties;
81*cdf0e10cSrcweir                         pAttr != 0; pAttr = pAttr->next) {
82*cdf0e10cSrcweir             ::rtl::Reference<CNode> const pNode = GetOwnerDocument().GetCNode(
83*cdf0e10cSrcweir                     reinterpret_cast<xmlNodePtr>(pAttr));
84*cdf0e10cSrcweir             OSL_ENSURE(pNode != 0, "CNode::get returned 0");
85*cdf0e10cSrcweir             OUString prefix = pNode->getPrefix();
86*cdf0e10cSrcweir             OUString name = (prefix.getLength() == 0)
87*cdf0e10cSrcweir                 ? pNode->getLocalName()
88*cdf0e10cSrcweir                 : prefix + OUString(static_cast<sal_Unicode>(':')) + pNode->getLocalName();
89*cdf0e10cSrcweir             OUString val  = pNode->getNodeValue();
90*cdf0e10cSrcweir             pAttrs->AddAttribute(name, type, val);
91*cdf0e10cSrcweir         }
92*cdf0e10cSrcweir         OUString prefix = getPrefix();
93*cdf0e10cSrcweir         OUString name = (prefix.getLength() == 0)
94*cdf0e10cSrcweir             ? getLocalName()
95*cdf0e10cSrcweir             : prefix + OUString(static_cast<sal_Unicode>(':')) + getLocalName();
96*cdf0e10cSrcweir         Reference< XAttributeList > xAttrList(pAttrs);
97*cdf0e10cSrcweir         i_xHandler->startElement(name, xAttrList);
98*cdf0e10cSrcweir         // recurse
99*cdf0e10cSrcweir         for (xmlNodePtr pChild = m_aNodePtr->children;
100*cdf0e10cSrcweir                         pChild != 0; pChild = pChild->next) {
101*cdf0e10cSrcweir             ::rtl::Reference<CNode> const pNode(
102*cdf0e10cSrcweir                     GetOwnerDocument().GetCNode(pChild));
103*cdf0e10cSrcweir             OSL_ENSURE(pNode != 0, "CNode::get returned 0");
104*cdf0e10cSrcweir             pNode->saxify(i_xHandler);
105*cdf0e10cSrcweir         }
106*cdf0e10cSrcweir         i_xHandler->endElement(name);
107*cdf0e10cSrcweir     }
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir     void CElement::fastSaxify( Context& i_rContext )
110*cdf0e10cSrcweir     {
111*cdf0e10cSrcweir         if (!i_rContext.mxDocHandler.is()) throw RuntimeException();
112*cdf0e10cSrcweir         pushContext(i_rContext);
113*cdf0e10cSrcweir         addNamespaces(i_rContext,m_aNodePtr);
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir         // add attributes
116*cdf0e10cSrcweir         i_rContext.mxAttribList->clear();
117*cdf0e10cSrcweir         for (xmlAttrPtr pAttr = m_aNodePtr->properties;
118*cdf0e10cSrcweir                         pAttr != 0; pAttr = pAttr->next) {
119*cdf0e10cSrcweir             ::rtl::Reference<CNode> const pNode = GetOwnerDocument().GetCNode(
120*cdf0e10cSrcweir                     reinterpret_cast<xmlNodePtr>(pAttr));
121*cdf0e10cSrcweir             OSL_ENSURE(pNode != 0, "CNode::get returned 0");
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir             const xmlChar* xName = pAttr->name;
124*cdf0e10cSrcweir             sal_Int32 nAttributeToken=FastToken::DONTKNOW;
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir             if( pAttr->ns && strlen((char*)pAttr->ns->prefix) )
127*cdf0e10cSrcweir                 nAttributeToken = getTokenWithPrefix( i_rContext,
128*cdf0e10cSrcweir                                                       (sal_Char*)pAttr->ns->prefix,
129*cdf0e10cSrcweir                                                       (sal_Char*)xName );
130*cdf0e10cSrcweir             else
131*cdf0e10cSrcweir                 nAttributeToken = getToken( i_rContext, (sal_Char*)xName );
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir             if( nAttributeToken != FastToken::DONTKNOW )
134*cdf0e10cSrcweir                 i_rContext.mxAttribList->add( nAttributeToken,
135*cdf0e10cSrcweir                                               OUStringToOString(pNode->getNodeValue(),
136*cdf0e10cSrcweir                                                                 RTL_TEXTENCODING_UTF8));
137*cdf0e10cSrcweir         }
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir         const xmlChar* xPrefix = m_aNodePtr->ns ? m_aNodePtr->ns->prefix : (const xmlChar*)"";
140*cdf0e10cSrcweir         const xmlChar* xName = m_aNodePtr->name;
141*cdf0e10cSrcweir         sal_Int32 nElementToken=FastToken::DONTKNOW;
142*cdf0e10cSrcweir         if( strlen((char*)xPrefix) )
143*cdf0e10cSrcweir             nElementToken = getTokenWithPrefix( i_rContext, (sal_Char*)xPrefix, (sal_Char*)xName );
144*cdf0e10cSrcweir         else
145*cdf0e10cSrcweir             nElementToken = getToken( i_rContext, (sal_Char*)xName );
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir         Reference<XFastContextHandler> xParentHandler(i_rContext.mxCurrentHandler);
148*cdf0e10cSrcweir         try
149*cdf0e10cSrcweir         {
150*cdf0e10cSrcweir             Reference< XFastAttributeList > xAttr( i_rContext.mxAttribList.get() );
151*cdf0e10cSrcweir             if( nElementToken == FastToken::DONTKNOW )
152*cdf0e10cSrcweir             {
153*cdf0e10cSrcweir                 const OUString aNamespace;
154*cdf0e10cSrcweir                 const OUString aElementName( (sal_Char*)xPrefix,
155*cdf0e10cSrcweir                                              strlen((char*)xPrefix),
156*cdf0e10cSrcweir                                              RTL_TEXTENCODING_UTF8 );
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir                 if( xParentHandler.is() )
159*cdf0e10cSrcweir                     i_rContext.mxCurrentHandler = xParentHandler->createUnknownChildContext( aNamespace, aElementName, xAttr );
160*cdf0e10cSrcweir                 else
161*cdf0e10cSrcweir                     i_rContext.mxCurrentHandler = i_rContext.mxDocHandler->createUnknownChildContext( aNamespace, aElementName, xAttr );
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir                 if( i_rContext.mxCurrentHandler.is() )
164*cdf0e10cSrcweir                     i_rContext.mxCurrentHandler->startUnknownElement( aNamespace, aElementName, xAttr );
165*cdf0e10cSrcweir             }
166*cdf0e10cSrcweir             else
167*cdf0e10cSrcweir             {
168*cdf0e10cSrcweir                 if( xParentHandler.is() )
169*cdf0e10cSrcweir                     i_rContext.mxCurrentHandler = xParentHandler->createFastChildContext( nElementToken, xAttr );
170*cdf0e10cSrcweir                 else
171*cdf0e10cSrcweir                     i_rContext.mxCurrentHandler = i_rContext.mxDocHandler->createFastChildContext( nElementToken, xAttr );
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir                 if( i_rContext.mxCurrentHandler.is() )
174*cdf0e10cSrcweir                     i_rContext.mxCurrentHandler->startFastElement( nElementToken, xAttr );
175*cdf0e10cSrcweir             }
176*cdf0e10cSrcweir         }
177*cdf0e10cSrcweir         catch( Exception& )
178*cdf0e10cSrcweir         {}
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir         // recurse
181*cdf0e10cSrcweir         for (xmlNodePtr pChild = m_aNodePtr->children;
182*cdf0e10cSrcweir                         pChild != 0; pChild = pChild->next) {
183*cdf0e10cSrcweir             ::rtl::Reference<CNode> const pNode(
184*cdf0e10cSrcweir                     GetOwnerDocument().GetCNode(pChild));
185*cdf0e10cSrcweir             OSL_ENSURE(pNode != 0, "CNode::get returned 0");
186*cdf0e10cSrcweir             pNode->fastSaxify(i_rContext);
187*cdf0e10cSrcweir         }
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir 		if( i_rContext.mxCurrentHandler.is() ) try
190*cdf0e10cSrcweir 		{
191*cdf0e10cSrcweir 			if( nElementToken != FastToken::DONTKNOW )
192*cdf0e10cSrcweir 				i_rContext.mxCurrentHandler->endFastElement( nElementToken );
193*cdf0e10cSrcweir 			else
194*cdf0e10cSrcweir             {
195*cdf0e10cSrcweir                 const OUString aNamespace;
196*cdf0e10cSrcweir                 const OUString aElementName( (sal_Char*)xPrefix,
197*cdf0e10cSrcweir                                              strlen((char*)xPrefix),
198*cdf0e10cSrcweir                                              RTL_TEXTENCODING_UTF8 );
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir 				i_rContext.mxCurrentHandler->endUnknownElement( aNamespace, aElementName );
201*cdf0e10cSrcweir             }
202*cdf0e10cSrcweir 		}
203*cdf0e10cSrcweir 		catch( Exception& )
204*cdf0e10cSrcweir 		{}
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir         // restore after children have been processed
207*cdf0e10cSrcweir         i_rContext.mxCurrentHandler = xParentHandler;
208*cdf0e10cSrcweir         popContext(i_rContext);
209*cdf0e10cSrcweir     }
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir     bool CElement::IsChildTypeAllowed(NodeType const nodeType)
212*cdf0e10cSrcweir     {
213*cdf0e10cSrcweir         switch (nodeType) {
214*cdf0e10cSrcweir             case NodeType_ELEMENT_NODE:
215*cdf0e10cSrcweir             case NodeType_TEXT_NODE:
216*cdf0e10cSrcweir             case NodeType_COMMENT_NODE:
217*cdf0e10cSrcweir             case NodeType_PROCESSING_INSTRUCTION_NODE:
218*cdf0e10cSrcweir             case NodeType_CDATA_SECTION_NODE:
219*cdf0e10cSrcweir             case NodeType_ENTITY_REFERENCE_NODE:
220*cdf0e10cSrcweir                 return true;
221*cdf0e10cSrcweir             case NodeType_ATTRIBUTE_NODE:
222*cdf0e10cSrcweir                 /* this is not relly allowed by the DOM spec, but this
223*cdf0e10cSrcweir                    implementation has evidently supported it (by special case
224*cdf0e10cSrcweir                    handling, so the attribute does not actually become a child)
225*cdf0e10cSrcweir                    so allow it for backward compatiblity */
226*cdf0e10cSrcweir                 return true;
227*cdf0e10cSrcweir             default:
228*cdf0e10cSrcweir                 return false;
229*cdf0e10cSrcweir         }
230*cdf0e10cSrcweir     }
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir 
233*cdf0e10cSrcweir     /**
234*cdf0e10cSrcweir 		Retrieves an attribute value by name.
235*cdf0e10cSrcweir 		return empty string if attribute is not set
236*cdf0e10cSrcweir     */
237*cdf0e10cSrcweir     OUString SAL_CALL CElement::getAttribute(OUString const& name)
238*cdf0e10cSrcweir         throw (RuntimeException)
239*cdf0e10cSrcweir     {
240*cdf0e10cSrcweir         ::osl::MutexGuard const g(m_rMutex);
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir         if (0 == m_aNodePtr) {
243*cdf0e10cSrcweir             return ::rtl::OUString();
244*cdf0e10cSrcweir         }
245*cdf0e10cSrcweir         // search properties
246*cdf0e10cSrcweir         OString o1 = OUStringToOString(name, RTL_TEXTENCODING_UTF8);
247*cdf0e10cSrcweir         ::boost::shared_ptr<xmlChar const> const pValue(
248*cdf0e10cSrcweir             xmlGetProp(m_aNodePtr, (xmlChar*)o1.getStr()), xmlFree);
249*cdf0e10cSrcweir         OUString const ret( (pValue)
250*cdf0e10cSrcweir             ?   OUString(reinterpret_cast<sal_Char const*>(pValue.get()),
251*cdf0e10cSrcweir                         strlen(reinterpret_cast<char const*>(pValue.get())),
252*cdf0e10cSrcweir                         RTL_TEXTENCODING_UTF8)
253*cdf0e10cSrcweir             :   OUString() );
254*cdf0e10cSrcweir         return ret;
255*cdf0e10cSrcweir     }
256*cdf0e10cSrcweir 
257*cdf0e10cSrcweir     /**
258*cdf0e10cSrcweir     Retrieves an attribute node by name.
259*cdf0e10cSrcweir     */
260*cdf0e10cSrcweir     Reference< XAttr > SAL_CALL CElement::getAttributeNode(OUString const& name)
261*cdf0e10cSrcweir         throw (RuntimeException)
262*cdf0e10cSrcweir     {
263*cdf0e10cSrcweir         ::osl::MutexGuard const g(m_rMutex);
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir         if (0 == m_aNodePtr) {
266*cdf0e10cSrcweir             return 0;
267*cdf0e10cSrcweir         }
268*cdf0e10cSrcweir         OString o1 = OUStringToOString(name, RTL_TEXTENCODING_UTF8);
269*cdf0e10cSrcweir         xmlChar const*const pName =
270*cdf0e10cSrcweir             reinterpret_cast<xmlChar const*>(o1.getStr());
271*cdf0e10cSrcweir         xmlAttrPtr const pAttr = xmlHasProp(m_aNodePtr, pName);
272*cdf0e10cSrcweir         if (0 == pAttr) {
273*cdf0e10cSrcweir             return 0;
274*cdf0e10cSrcweir         }
275*cdf0e10cSrcweir         Reference< XAttr > const xRet(
276*cdf0e10cSrcweir             static_cast< XNode* >(GetOwnerDocument().GetCNode(
277*cdf0e10cSrcweir                     reinterpret_cast<xmlNodePtr>(pAttr)).get()),
278*cdf0e10cSrcweir             UNO_QUERY_THROW);
279*cdf0e10cSrcweir         return xRet;
280*cdf0e10cSrcweir     }
281*cdf0e10cSrcweir 
282*cdf0e10cSrcweir     /**
283*cdf0e10cSrcweir     Retrieves an Attr node by local name and namespace URI.
284*cdf0e10cSrcweir     */
285*cdf0e10cSrcweir     Reference< XAttr > SAL_CALL CElement::getAttributeNodeNS(
286*cdf0e10cSrcweir             const OUString& namespaceURI, const OUString& localName)
287*cdf0e10cSrcweir         throw (RuntimeException)
288*cdf0e10cSrcweir     {
289*cdf0e10cSrcweir         ::osl::MutexGuard const g(m_rMutex);
290*cdf0e10cSrcweir 
291*cdf0e10cSrcweir         if (0 == m_aNodePtr) {
292*cdf0e10cSrcweir             return 0;
293*cdf0e10cSrcweir         }
294*cdf0e10cSrcweir         OString o1 = OUStringToOString(localName, RTL_TEXTENCODING_UTF8);
295*cdf0e10cSrcweir         xmlChar const*const pName =
296*cdf0e10cSrcweir             reinterpret_cast<xmlChar const*>(o1.getStr());
297*cdf0e10cSrcweir         OString o2 = OUStringToOString(namespaceURI, RTL_TEXTENCODING_UTF8);
298*cdf0e10cSrcweir         xmlChar const*const pNS =
299*cdf0e10cSrcweir             reinterpret_cast<xmlChar const*>(o2.getStr());
300*cdf0e10cSrcweir         xmlAttrPtr const pAttr = xmlHasNsProp(m_aNodePtr, pName, pNS);
301*cdf0e10cSrcweir         if (0 == pAttr) {
302*cdf0e10cSrcweir             return 0;
303*cdf0e10cSrcweir         }
304*cdf0e10cSrcweir         Reference< XAttr > const xRet(
305*cdf0e10cSrcweir             static_cast< XNode* >(GetOwnerDocument().GetCNode(
306*cdf0e10cSrcweir                     reinterpret_cast<xmlNodePtr>(pAttr)).get()),
307*cdf0e10cSrcweir             UNO_QUERY_THROW);
308*cdf0e10cSrcweir         return xRet;
309*cdf0e10cSrcweir     }
310*cdf0e10cSrcweir 
311*cdf0e10cSrcweir     /**
312*cdf0e10cSrcweir     Retrieves an attribute value by local name and namespace URI.
313*cdf0e10cSrcweir 	return empty string if attribute is not set
314*cdf0e10cSrcweir     */
315*cdf0e10cSrcweir     OUString SAL_CALL
316*cdf0e10cSrcweir     CElement::getAttributeNS(
317*cdf0e10cSrcweir             OUString const& namespaceURI, OUString const& localName)
318*cdf0e10cSrcweir         throw (RuntimeException)
319*cdf0e10cSrcweir     {
320*cdf0e10cSrcweir         ::osl::MutexGuard const g(m_rMutex);
321*cdf0e10cSrcweir 
322*cdf0e10cSrcweir         if (0 == m_aNodePtr) {
323*cdf0e10cSrcweir             return ::rtl::OUString();
324*cdf0e10cSrcweir         }
325*cdf0e10cSrcweir         OString o1 = OUStringToOString(localName, RTL_TEXTENCODING_UTF8);
326*cdf0e10cSrcweir         xmlChar const*const pName =
327*cdf0e10cSrcweir             reinterpret_cast<xmlChar const*>(o1.getStr());
328*cdf0e10cSrcweir         OString o2 = OUStringToOString(namespaceURI, RTL_TEXTENCODING_UTF8);
329*cdf0e10cSrcweir         xmlChar const*const pNS =
330*cdf0e10cSrcweir             reinterpret_cast<xmlChar const*>(o2.getStr());
331*cdf0e10cSrcweir         ::boost::shared_ptr<xmlChar const> const pValue(
332*cdf0e10cSrcweir                 xmlGetNsProp(m_aNodePtr, pName, pNS), xmlFree);
333*cdf0e10cSrcweir         if (0 == pValue) {
334*cdf0e10cSrcweir             return ::rtl::OUString();
335*cdf0e10cSrcweir         }
336*cdf0e10cSrcweir         OUString const ret(reinterpret_cast<sal_Char const*>(pValue.get()),
337*cdf0e10cSrcweir                         strlen(reinterpret_cast<char const*>(pValue.get())),
338*cdf0e10cSrcweir                         RTL_TEXTENCODING_UTF8);
339*cdf0e10cSrcweir         return ret;
340*cdf0e10cSrcweir     }
341*cdf0e10cSrcweir 
342*cdf0e10cSrcweir     /**
343*cdf0e10cSrcweir     Returns a NodeList of all descendant Elements with a given tag name,
344*cdf0e10cSrcweir     in the order in which they are
345*cdf0e10cSrcweir     encountered in a preorder traversal of this Element tree.
346*cdf0e10cSrcweir     */
347*cdf0e10cSrcweir     Reference< XNodeList > SAL_CALL
348*cdf0e10cSrcweir     CElement::getElementsByTagName(OUString const& rLocalName)
349*cdf0e10cSrcweir         throw (RuntimeException)
350*cdf0e10cSrcweir     {
351*cdf0e10cSrcweir         ::osl::MutexGuard const g(m_rMutex);
352*cdf0e10cSrcweir 
353*cdf0e10cSrcweir         Reference< XNodeList > const xList(
354*cdf0e10cSrcweir                 new CElementList(this, m_rMutex, rLocalName));
355*cdf0e10cSrcweir         return xList;
356*cdf0e10cSrcweir     }
357*cdf0e10cSrcweir 
358*cdf0e10cSrcweir     /**
359*cdf0e10cSrcweir     Returns a NodeList of all the descendant Elements with a given local
360*cdf0e10cSrcweir     name and namespace URI in the order in which they are encountered in
361*cdf0e10cSrcweir     a preorder traversal of this Element tree.
362*cdf0e10cSrcweir     */
363*cdf0e10cSrcweir     Reference< XNodeList > SAL_CALL
364*cdf0e10cSrcweir     CElement::getElementsByTagNameNS(
365*cdf0e10cSrcweir             OUString const& rNamespaceURI, OUString const& rLocalName)
366*cdf0e10cSrcweir         throw (RuntimeException)
367*cdf0e10cSrcweir     {
368*cdf0e10cSrcweir         ::osl::MutexGuard const g(m_rMutex);
369*cdf0e10cSrcweir 
370*cdf0e10cSrcweir         Reference< XNodeList > const xList(
371*cdf0e10cSrcweir             new CElementList(this, m_rMutex, rLocalName, &rNamespaceURI));
372*cdf0e10cSrcweir         return xList;
373*cdf0e10cSrcweir     }
374*cdf0e10cSrcweir 
375*cdf0e10cSrcweir     /**
376*cdf0e10cSrcweir     The name of the element.
377*cdf0e10cSrcweir     */
378*cdf0e10cSrcweir     OUString SAL_CALL CElement::getTagName()
379*cdf0e10cSrcweir         throw (RuntimeException)
380*cdf0e10cSrcweir     {
381*cdf0e10cSrcweir         ::osl::MutexGuard const g(m_rMutex);
382*cdf0e10cSrcweir 
383*cdf0e10cSrcweir         if (0 == m_aNodePtr) {
384*cdf0e10cSrcweir             return ::rtl::OUString();
385*cdf0e10cSrcweir         }
386*cdf0e10cSrcweir         OUString const ret((sal_Char*)m_aNodePtr->name,
387*cdf0e10cSrcweir                 strlen((char*)m_aNodePtr->name), RTL_TEXTENCODING_UTF8);
388*cdf0e10cSrcweir         return ret;
389*cdf0e10cSrcweir     }
390*cdf0e10cSrcweir 
391*cdf0e10cSrcweir 
392*cdf0e10cSrcweir     /**
393*cdf0e10cSrcweir     Returns true when an attribute with a given name is specified on this
394*cdf0e10cSrcweir     element or has a default value, false otherwise.
395*cdf0e10cSrcweir     */
396*cdf0e10cSrcweir     sal_Bool SAL_CALL CElement::hasAttribute(OUString const& name)
397*cdf0e10cSrcweir         throw (RuntimeException)
398*cdf0e10cSrcweir     {
399*cdf0e10cSrcweir         ::osl::MutexGuard const g(m_rMutex);
400*cdf0e10cSrcweir 
401*cdf0e10cSrcweir         OString o1 = OUStringToOString(name, RTL_TEXTENCODING_UTF8);
402*cdf0e10cSrcweir         xmlChar *xName = (xmlChar*)o1.getStr();
403*cdf0e10cSrcweir         return (m_aNodePtr != NULL && xmlHasProp(m_aNodePtr, xName) != NULL);
404*cdf0e10cSrcweir     }
405*cdf0e10cSrcweir 
406*cdf0e10cSrcweir     /**
407*cdf0e10cSrcweir     Returns true when an attribute with a given local name and namespace
408*cdf0e10cSrcweir     URI is specified on this element or has a default value, false otherwise.
409*cdf0e10cSrcweir     */
410*cdf0e10cSrcweir     sal_Bool SAL_CALL CElement::hasAttributeNS(
411*cdf0e10cSrcweir             OUString const& namespaceURI, OUString const& localName)
412*cdf0e10cSrcweir         throw (RuntimeException)
413*cdf0e10cSrcweir     {
414*cdf0e10cSrcweir         ::osl::MutexGuard const g(m_rMutex);
415*cdf0e10cSrcweir 
416*cdf0e10cSrcweir         OString o1 = OUStringToOString(localName, RTL_TEXTENCODING_UTF8);
417*cdf0e10cSrcweir         xmlChar *xName = (xmlChar*)o1.getStr();
418*cdf0e10cSrcweir         OString o2 = OUStringToOString(namespaceURI, RTL_TEXTENCODING_UTF8);
419*cdf0e10cSrcweir         xmlChar *xNs = (xmlChar*)o2.getStr();
420*cdf0e10cSrcweir         return (m_aNodePtr != NULL && xmlHasNsProp(m_aNodePtr, xName, xNs) != NULL);
421*cdf0e10cSrcweir     }
422*cdf0e10cSrcweir 
423*cdf0e10cSrcweir     /**
424*cdf0e10cSrcweir     Removes an attribute by name.
425*cdf0e10cSrcweir     */
426*cdf0e10cSrcweir     void SAL_CALL CElement::removeAttribute(OUString const& name)
427*cdf0e10cSrcweir         throw (RuntimeException, DOMException)
428*cdf0e10cSrcweir     {
429*cdf0e10cSrcweir         ::osl::MutexGuard const g(m_rMutex);
430*cdf0e10cSrcweir 
431*cdf0e10cSrcweir         if (0 == m_aNodePtr) {
432*cdf0e10cSrcweir             return;
433*cdf0e10cSrcweir         }
434*cdf0e10cSrcweir         OString o1 = OUStringToOString(name, RTL_TEXTENCODING_UTF8);
435*cdf0e10cSrcweir         xmlChar const*const pName =
436*cdf0e10cSrcweir             reinterpret_cast<xmlChar const*>(o1.getStr());
437*cdf0e10cSrcweir         xmlAttrPtr const pAttr = xmlHasProp(m_aNodePtr, pName);
438*cdf0e10cSrcweir         if (0 == xmlUnsetProp(m_aNodePtr, pName)) {
439*cdf0e10cSrcweir             ::rtl::Reference<CNode> const pCNode(GetOwnerDocument().GetCNode(
440*cdf0e10cSrcweir                     reinterpret_cast<xmlNodePtr>(pAttr), false));
441*cdf0e10cSrcweir             if (pCNode.is()) {
442*cdf0e10cSrcweir                 pCNode->invalidate(); // freed by xmlUnsetProp
443*cdf0e10cSrcweir             }
444*cdf0e10cSrcweir         }
445*cdf0e10cSrcweir     }
446*cdf0e10cSrcweir 
447*cdf0e10cSrcweir     /**
448*cdf0e10cSrcweir     Removes an attribute by local name and namespace URI.
449*cdf0e10cSrcweir     */
450*cdf0e10cSrcweir     void SAL_CALL CElement::removeAttributeNS(
451*cdf0e10cSrcweir             OUString const& namespaceURI, OUString const& localName)
452*cdf0e10cSrcweir         throw (RuntimeException, DOMException)
453*cdf0e10cSrcweir     {
454*cdf0e10cSrcweir         ::osl::MutexGuard const g(m_rMutex);
455*cdf0e10cSrcweir 
456*cdf0e10cSrcweir         if (0 == m_aNodePtr) {
457*cdf0e10cSrcweir             return;
458*cdf0e10cSrcweir         }
459*cdf0e10cSrcweir         OString o1 = OUStringToOString(localName, RTL_TEXTENCODING_UTF8);
460*cdf0e10cSrcweir         xmlChar const*const pName =
461*cdf0e10cSrcweir             reinterpret_cast<xmlChar const*>(o1.getStr());
462*cdf0e10cSrcweir         OString o2 = OUStringToOString(namespaceURI, RTL_TEXTENCODING_UTF8);
463*cdf0e10cSrcweir         xmlChar const*const pURI =
464*cdf0e10cSrcweir             reinterpret_cast<xmlChar const*>(o2.getStr());
465*cdf0e10cSrcweir         xmlNsPtr const pNs =
466*cdf0e10cSrcweir             xmlSearchNsByHref(m_aNodePtr->doc, m_aNodePtr, pURI);
467*cdf0e10cSrcweir         xmlAttrPtr const pAttr = xmlHasNsProp(m_aNodePtr, pName, pURI);
468*cdf0e10cSrcweir         if (0 == xmlUnsetNsProp(m_aNodePtr, pNs, pName)) {
469*cdf0e10cSrcweir             ::rtl::Reference<CNode> const pCNode(GetOwnerDocument().GetCNode(
470*cdf0e10cSrcweir                     reinterpret_cast<xmlNodePtr>(pAttr), false));
471*cdf0e10cSrcweir             if (pCNode.is()) {
472*cdf0e10cSrcweir                 pCNode->invalidate(); // freed by xmlUnsetNsProp
473*cdf0e10cSrcweir             }
474*cdf0e10cSrcweir         }
475*cdf0e10cSrcweir     }
476*cdf0e10cSrcweir 
477*cdf0e10cSrcweir     /**
478*cdf0e10cSrcweir     Removes the specified attribute node.
479*cdf0e10cSrcweir     */
480*cdf0e10cSrcweir     Reference< XAttr > SAL_CALL
481*cdf0e10cSrcweir     CElement::removeAttributeNode(Reference< XAttr > const& oldAttr)
482*cdf0e10cSrcweir         throw (RuntimeException, DOMException)
483*cdf0e10cSrcweir     {
484*cdf0e10cSrcweir         ::osl::MutexGuard const g(m_rMutex);
485*cdf0e10cSrcweir 
486*cdf0e10cSrcweir         if (0 == m_aNodePtr) {
487*cdf0e10cSrcweir             return 0;
488*cdf0e10cSrcweir         }
489*cdf0e10cSrcweir 
490*cdf0e10cSrcweir         ::rtl::Reference<CNode> const pCNode(
491*cdf0e10cSrcweir             CNode::GetImplementation(Reference<XNode>(oldAttr.get())));
492*cdf0e10cSrcweir         if (!pCNode.is()) { throw RuntimeException(); }
493*cdf0e10cSrcweir 
494*cdf0e10cSrcweir         xmlNodePtr const pNode = pCNode->GetNodePtr();
495*cdf0e10cSrcweir         xmlAttrPtr const pAttr = (xmlAttrPtr) pNode;
496*cdf0e10cSrcweir         if (!pAttr) { throw RuntimeException(); }
497*cdf0e10cSrcweir 
498*cdf0e10cSrcweir         if (pAttr->parent != m_aNodePtr)
499*cdf0e10cSrcweir         {
500*cdf0e10cSrcweir             DOMException e;
501*cdf0e10cSrcweir             e.Code = DOMExceptionType_HIERARCHY_REQUEST_ERR;
502*cdf0e10cSrcweir             throw e;
503*cdf0e10cSrcweir         }
504*cdf0e10cSrcweir         if (pAttr->doc != m_aNodePtr->doc)
505*cdf0e10cSrcweir         {
506*cdf0e10cSrcweir             DOMException e;
507*cdf0e10cSrcweir             e.Code = DOMExceptionType_WRONG_DOCUMENT_ERR;
508*cdf0e10cSrcweir             throw e;
509*cdf0e10cSrcweir         }
510*cdf0e10cSrcweir 
511*cdf0e10cSrcweir         Reference< XAttr > aAttr;
512*cdf0e10cSrcweir         if (oldAttr->getNamespaceURI().getLength() > 0) {
513*cdf0e10cSrcweir             ::rtl::OUStringBuffer qname(oldAttr->getPrefix());
514*cdf0e10cSrcweir             if (0 != qname.getLength()) {
515*cdf0e10cSrcweir                 qname.append(sal_Unicode(':'));
516*cdf0e10cSrcweir             }
517*cdf0e10cSrcweir             qname.append(oldAttr->getName());
518*cdf0e10cSrcweir             aAttr = GetOwnerDocument().createAttributeNS(
519*cdf0e10cSrcweir                 oldAttr->getNamespaceURI(), qname.makeStringAndClear());
520*cdf0e10cSrcweir         } else {
521*cdf0e10cSrcweir             aAttr = GetOwnerDocument().createAttribute(oldAttr->getName());
522*cdf0e10cSrcweir         }
523*cdf0e10cSrcweir         aAttr->setValue(oldAttr->getValue());
524*cdf0e10cSrcweir         xmlRemoveProp(pAttr);
525*cdf0e10cSrcweir         pCNode->invalidate(); // freed by xmlRemoveProp
526*cdf0e10cSrcweir 
527*cdf0e10cSrcweir         return aAttr;
528*cdf0e10cSrcweir     }
529*cdf0e10cSrcweir 
530*cdf0e10cSrcweir     /**
531*cdf0e10cSrcweir     Adds a new attribute node.
532*cdf0e10cSrcweir     */
533*cdf0e10cSrcweir     Reference< XAttr >
534*cdf0e10cSrcweir     CElement::setAttributeNode_Impl_Lock(
535*cdf0e10cSrcweir             Reference< XAttr > const& xNewAttr, bool const bNS)
536*cdf0e10cSrcweir     {
537*cdf0e10cSrcweir         if (xNewAttr->getOwnerDocument() != getOwnerDocument()) {
538*cdf0e10cSrcweir             DOMException e;
539*cdf0e10cSrcweir             e.Code = DOMExceptionType_WRONG_DOCUMENT_ERR;
540*cdf0e10cSrcweir             throw e;
541*cdf0e10cSrcweir         }
542*cdf0e10cSrcweir 
543*cdf0e10cSrcweir         ::osl::ClearableMutexGuard guard(m_rMutex);
544*cdf0e10cSrcweir 
545*cdf0e10cSrcweir         if (0 == m_aNodePtr) {
546*cdf0e10cSrcweir             throw RuntimeException();
547*cdf0e10cSrcweir         }
548*cdf0e10cSrcweir 
549*cdf0e10cSrcweir         // get the implementation
550*cdf0e10cSrcweir         CAttr *const pCAttr = dynamic_cast<CAttr*>(
551*cdf0e10cSrcweir                 CNode::GetImplementation(xNewAttr));
552*cdf0e10cSrcweir         if (!pCAttr) { throw RuntimeException(); }
553*cdf0e10cSrcweir         xmlAttrPtr const pAttr =
554*cdf0e10cSrcweir             reinterpret_cast<xmlAttrPtr>(pCAttr->GetNodePtr());
555*cdf0e10cSrcweir         if (!pAttr) { throw RuntimeException(); }
556*cdf0e10cSrcweir 
557*cdf0e10cSrcweir         // check whether the attribute is not in use by another element
558*cdf0e10cSrcweir         if (pAttr->parent) {
559*cdf0e10cSrcweir             DOMException e;
560*cdf0e10cSrcweir             e.Code = DOMExceptionType_INUSE_ATTRIBUTE_ERR;
561*cdf0e10cSrcweir             throw e;
562*cdf0e10cSrcweir         }
563*cdf0e10cSrcweir 
564*cdf0e10cSrcweir         xmlAttrPtr res = NULL;
565*cdf0e10cSrcweir         xmlChar const*const pContent(
566*cdf0e10cSrcweir                 (pAttr->children) ? pAttr->children->content : 0);
567*cdf0e10cSrcweir 
568*cdf0e10cSrcweir         if (bNS) {
569*cdf0e10cSrcweir             xmlNsPtr const pNs( pCAttr->GetNamespace(m_aNodePtr) );
570*cdf0e10cSrcweir             res = xmlNewNsProp(m_aNodePtr, pNs, pAttr->name, pContent);
571*cdf0e10cSrcweir         } else {
572*cdf0e10cSrcweir             res = xmlNewProp(m_aNodePtr, pAttr->name, pContent);
573*cdf0e10cSrcweir         }
574*cdf0e10cSrcweir 
575*cdf0e10cSrcweir         // get the new attr node
576*cdf0e10cSrcweir         Reference< XAttr > const xAttr(
577*cdf0e10cSrcweir             static_cast< XNode* >(GetOwnerDocument().GetCNode(
578*cdf0e10cSrcweir                     reinterpret_cast<xmlNodePtr>(res)).get()),
579*cdf0e10cSrcweir             UNO_QUERY_THROW);
580*cdf0e10cSrcweir 
581*cdf0e10cSrcweir         // attribute adition event
582*cdf0e10cSrcweir         // dispatch DOMAttrModified event
583*cdf0e10cSrcweir         Reference< XDocumentEvent > docevent(getOwnerDocument(), UNO_QUERY);
584*cdf0e10cSrcweir         Reference< XMutationEvent > event(docevent->createEvent(
585*cdf0e10cSrcweir             OUString::createFromAscii("DOMAttrModified")), UNO_QUERY);
586*cdf0e10cSrcweir         event->initMutationEvent(OUString::createFromAscii("DOMAttrModified"),
587*cdf0e10cSrcweir             sal_True, sal_False, Reference< XNode >(xAttr, UNO_QUERY),
588*cdf0e10cSrcweir             OUString(), xAttr->getValue(), xAttr->getName(),
589*cdf0e10cSrcweir             AttrChangeType_ADDITION);
590*cdf0e10cSrcweir 
591*cdf0e10cSrcweir         guard.clear(); // release mutex before calling event handlers
592*cdf0e10cSrcweir 
593*cdf0e10cSrcweir         dispatchEvent(Reference< XEvent >(event, UNO_QUERY));
594*cdf0e10cSrcweir         dispatchSubtreeModified();
595*cdf0e10cSrcweir 
596*cdf0e10cSrcweir         return xAttr;
597*cdf0e10cSrcweir     }
598*cdf0e10cSrcweir 
599*cdf0e10cSrcweir     Reference< XAttr >
600*cdf0e10cSrcweir     CElement::setAttributeNode(const Reference< XAttr >& newAttr)
601*cdf0e10cSrcweir         throw (RuntimeException, DOMException)
602*cdf0e10cSrcweir     {
603*cdf0e10cSrcweir         return setAttributeNode_Impl_Lock(newAttr, false);
604*cdf0e10cSrcweir     }
605*cdf0e10cSrcweir 
606*cdf0e10cSrcweir     /**
607*cdf0e10cSrcweir     Adds a new attribute.
608*cdf0e10cSrcweir     */
609*cdf0e10cSrcweir     Reference< XAttr >
610*cdf0e10cSrcweir     CElement::setAttributeNodeNS(const Reference< XAttr >& newAttr)
611*cdf0e10cSrcweir         throw (RuntimeException, DOMException)
612*cdf0e10cSrcweir     {
613*cdf0e10cSrcweir         return setAttributeNode_Impl_Lock(newAttr, true);
614*cdf0e10cSrcweir     }
615*cdf0e10cSrcweir 
616*cdf0e10cSrcweir     /**
617*cdf0e10cSrcweir     Adds a new attribute.
618*cdf0e10cSrcweir     */
619*cdf0e10cSrcweir     void SAL_CALL
620*cdf0e10cSrcweir     CElement::setAttribute(OUString const& name, OUString const& value)
621*cdf0e10cSrcweir         throw (RuntimeException, DOMException)
622*cdf0e10cSrcweir     {
623*cdf0e10cSrcweir         ::osl::ClearableMutexGuard guard(m_rMutex);
624*cdf0e10cSrcweir 
625*cdf0e10cSrcweir         OString o1 = OUStringToOString(name, RTL_TEXTENCODING_UTF8);
626*cdf0e10cSrcweir         xmlChar *xName = (xmlChar*)o1.getStr();
627*cdf0e10cSrcweir         OString o2 = OUStringToOString(value, RTL_TEXTENCODING_UTF8);
628*cdf0e10cSrcweir         xmlChar *xValue = (xmlChar*)o2.getStr();
629*cdf0e10cSrcweir 
630*cdf0e10cSrcweir         if (0 == m_aNodePtr) {
631*cdf0e10cSrcweir             throw RuntimeException();
632*cdf0e10cSrcweir         }
633*cdf0e10cSrcweir         OUString oldValue;
634*cdf0e10cSrcweir         AttrChangeType aChangeType = AttrChangeType_MODIFICATION;
635*cdf0e10cSrcweir         ::boost::shared_ptr<xmlChar const> const pOld(
636*cdf0e10cSrcweir             xmlGetProp(m_aNodePtr, xName), xmlFree);
637*cdf0e10cSrcweir         if (pOld == NULL) {
638*cdf0e10cSrcweir             aChangeType = AttrChangeType_ADDITION;
639*cdf0e10cSrcweir             xmlNewProp(m_aNodePtr, xName, xValue);
640*cdf0e10cSrcweir         } else {
641*cdf0e10cSrcweir             oldValue = OUString(reinterpret_cast<sal_Char const*>(pOld.get()),
642*cdf0e10cSrcweir                         strlen(reinterpret_cast<char const*>(pOld.get())),
643*cdf0e10cSrcweir                         RTL_TEXTENCODING_UTF8);
644*cdf0e10cSrcweir             xmlSetProp(m_aNodePtr, xName, xValue);
645*cdf0e10cSrcweir         }
646*cdf0e10cSrcweir 
647*cdf0e10cSrcweir         // dispatch DOMAttrModified event
648*cdf0e10cSrcweir         Reference< XDocumentEvent > docevent(getOwnerDocument(), UNO_QUERY);
649*cdf0e10cSrcweir         Reference< XMutationEvent > event(docevent->createEvent(
650*cdf0e10cSrcweir             OUString::createFromAscii("DOMAttrModified")), UNO_QUERY);
651*cdf0e10cSrcweir         event->initMutationEvent(OUString::createFromAscii("DOMAttrModified"),
652*cdf0e10cSrcweir             sal_True, sal_False,
653*cdf0e10cSrcweir             Reference< XNode >(getAttributeNode(name), UNO_QUERY),
654*cdf0e10cSrcweir             oldValue, value, name, aChangeType);
655*cdf0e10cSrcweir 
656*cdf0e10cSrcweir         guard.clear(); // release mutex before calling event handlers
657*cdf0e10cSrcweir         dispatchEvent(Reference< XEvent >(event, UNO_QUERY));
658*cdf0e10cSrcweir         dispatchSubtreeModified();
659*cdf0e10cSrcweir     }
660*cdf0e10cSrcweir 
661*cdf0e10cSrcweir     /**
662*cdf0e10cSrcweir     Adds a new attribute.
663*cdf0e10cSrcweir     */
664*cdf0e10cSrcweir     void SAL_CALL
665*cdf0e10cSrcweir     CElement::setAttributeNS(OUString const& namespaceURI,
666*cdf0e10cSrcweir             OUString const& qualifiedName, OUString const& value)
667*cdf0e10cSrcweir         throw (RuntimeException, DOMException)
668*cdf0e10cSrcweir     {
669*cdf0e10cSrcweir         if (namespaceURI.getLength() == 0) throw RuntimeException();
670*cdf0e10cSrcweir 
671*cdf0e10cSrcweir         ::osl::ClearableMutexGuard guard(m_rMutex);
672*cdf0e10cSrcweir 
673*cdf0e10cSrcweir         OString o1, o2, o3, o4, o5;
674*cdf0e10cSrcweir         xmlChar *xPrefix = NULL;
675*cdf0e10cSrcweir         xmlChar *xLName = NULL;
676*cdf0e10cSrcweir         o1 = OUStringToOString(qualifiedName, RTL_TEXTENCODING_UTF8);
677*cdf0e10cSrcweir         xmlChar *xQName = (xmlChar*)o1.getStr();
678*cdf0e10cSrcweir         sal_Int32 idx = qualifiedName.indexOf(':');
679*cdf0e10cSrcweir         if (idx != -1)
680*cdf0e10cSrcweir         {
681*cdf0e10cSrcweir             o2 = OUStringToOString(
682*cdf0e10cSrcweir                 qualifiedName.copy(0,idx),
683*cdf0e10cSrcweir                 RTL_TEXTENCODING_UTF8);
684*cdf0e10cSrcweir             xPrefix = (xmlChar*)o2.getStr();
685*cdf0e10cSrcweir             o3 = OUStringToOString(
686*cdf0e10cSrcweir                 qualifiedName.copy(idx+1),
687*cdf0e10cSrcweir                 RTL_TEXTENCODING_UTF8);
688*cdf0e10cSrcweir             xLName = (xmlChar*)o3.getStr();
689*cdf0e10cSrcweir         }  else {
690*cdf0e10cSrcweir             xPrefix = (xmlChar*)"";
691*cdf0e10cSrcweir             xLName = xQName;
692*cdf0e10cSrcweir         }
693*cdf0e10cSrcweir         o4 = OUStringToOString(namespaceURI, RTL_TEXTENCODING_UTF8);
694*cdf0e10cSrcweir         o5 = OUStringToOString(value, RTL_TEXTENCODING_UTF8);
695*cdf0e10cSrcweir         xmlChar *xURI= (xmlChar*)o4.getStr();
696*cdf0e10cSrcweir         xmlChar *xValue = (xmlChar*)o5.getStr();
697*cdf0e10cSrcweir 
698*cdf0e10cSrcweir         if (0 == m_aNodePtr) {
699*cdf0e10cSrcweir             throw RuntimeException();
700*cdf0e10cSrcweir         }
701*cdf0e10cSrcweir 
702*cdf0e10cSrcweir         //find the right namespace
703*cdf0e10cSrcweir         xmlNsPtr pNs = xmlSearchNs(m_aNodePtr->doc, m_aNodePtr, xPrefix);
704*cdf0e10cSrcweir         // if no namespace found, create a new one
705*cdf0e10cSrcweir         if (pNs == NULL) {
706*cdf0e10cSrcweir             pNs = xmlNewNs(m_aNodePtr, xURI, xPrefix);
707*cdf0e10cSrcweir         }
708*cdf0e10cSrcweir 
709*cdf0e10cSrcweir         if (strcmp((char*)pNs->href, (char*)xURI) != 0) {
710*cdf0e10cSrcweir             // ambiguous ns prefix
711*cdf0e10cSrcweir             throw RuntimeException();
712*cdf0e10cSrcweir         }
713*cdf0e10cSrcweir 
714*cdf0e10cSrcweir         // found namespace matches
715*cdf0e10cSrcweir 
716*cdf0e10cSrcweir         OUString oldValue;
717*cdf0e10cSrcweir         AttrChangeType aChangeType = AttrChangeType_MODIFICATION;
718*cdf0e10cSrcweir         ::boost::shared_ptr<xmlChar const> const pOld(
719*cdf0e10cSrcweir                 xmlGetNsProp(m_aNodePtr, xLName, pNs->href), xmlFree);
720*cdf0e10cSrcweir         if (pOld == NULL) {
721*cdf0e10cSrcweir             aChangeType = AttrChangeType_ADDITION;
722*cdf0e10cSrcweir             xmlNewNsProp(m_aNodePtr, pNs, xLName, xValue);
723*cdf0e10cSrcweir         } else {
724*cdf0e10cSrcweir             oldValue = OUString(reinterpret_cast<sal_Char const*>(pOld.get()),
725*cdf0e10cSrcweir                         strlen(reinterpret_cast<char const*>(pOld.get())),
726*cdf0e10cSrcweir                         RTL_TEXTENCODING_UTF8);
727*cdf0e10cSrcweir             xmlSetNsProp(m_aNodePtr, pNs, xLName, xValue);
728*cdf0e10cSrcweir         }
729*cdf0e10cSrcweir         // dispatch DOMAttrModified event
730*cdf0e10cSrcweir         Reference< XDocumentEvent > docevent(getOwnerDocument(), UNO_QUERY);
731*cdf0e10cSrcweir         Reference< XMutationEvent > event(docevent->createEvent(
732*cdf0e10cSrcweir             OUString::createFromAscii("DOMAttrModified")), UNO_QUERY);
733*cdf0e10cSrcweir         event->initMutationEvent(
734*cdf0e10cSrcweir             OUString::createFromAscii("DOMAttrModified"),
735*cdf0e10cSrcweir             sal_True, sal_False,
736*cdf0e10cSrcweir             Reference< XNode >(getAttributeNodeNS(namespaceURI, OUString((char*)xLName, strlen((char*)xLName), RTL_TEXTENCODING_UTF8)), UNO_QUERY),
737*cdf0e10cSrcweir             oldValue, value, qualifiedName, aChangeType);
738*cdf0e10cSrcweir 
739*cdf0e10cSrcweir         guard.clear(); // release mutex before calling event handlers
740*cdf0e10cSrcweir         dispatchEvent(Reference< XEvent >(event, UNO_QUERY));
741*cdf0e10cSrcweir         dispatchSubtreeModified();
742*cdf0e10cSrcweir     }
743*cdf0e10cSrcweir 
744*cdf0e10cSrcweir     Reference< XNamedNodeMap > SAL_CALL
745*cdf0e10cSrcweir     CElement::getAttributes() throw (RuntimeException)
746*cdf0e10cSrcweir     {
747*cdf0e10cSrcweir         ::osl::MutexGuard const g(m_rMutex);
748*cdf0e10cSrcweir 
749*cdf0e10cSrcweir         Reference< XNamedNodeMap > const xMap(
750*cdf0e10cSrcweir                 new CAttributesMap(this, m_rMutex));
751*cdf0e10cSrcweir         return xMap;
752*cdf0e10cSrcweir     }
753*cdf0e10cSrcweir 
754*cdf0e10cSrcweir     OUString SAL_CALL CElement::getNodeName()throw (RuntimeException)
755*cdf0e10cSrcweir     {
756*cdf0e10cSrcweir         return getLocalName();
757*cdf0e10cSrcweir     }
758*cdf0e10cSrcweir 
759*cdf0e10cSrcweir     OUString SAL_CALL CElement::getLocalName()throw (RuntimeException)
760*cdf0e10cSrcweir     {
761*cdf0e10cSrcweir         ::osl::MutexGuard const g(m_rMutex);
762*cdf0e10cSrcweir 
763*cdf0e10cSrcweir         OUString aName;
764*cdf0e10cSrcweir         if (m_aNodePtr != NULL)
765*cdf0e10cSrcweir         {
766*cdf0e10cSrcweir             const xmlChar* xName = m_aNodePtr->name;
767*cdf0e10cSrcweir             aName = OUString((const sal_Char*)xName, strlen((const char*)xName), RTL_TEXTENCODING_UTF8);
768*cdf0e10cSrcweir         }
769*cdf0e10cSrcweir         return aName;
770*cdf0e10cSrcweir     }
771*cdf0e10cSrcweir 
772*cdf0e10cSrcweir     OUString SAL_CALL CElement::getNodeValue() throw (RuntimeException)
773*cdf0e10cSrcweir     {
774*cdf0e10cSrcweir         return OUString();
775*cdf0e10cSrcweir     }
776*cdf0e10cSrcweir 
777*cdf0e10cSrcweir     void SAL_CALL CElement::setElementName(const OUString& aName)
778*cdf0e10cSrcweir         throw (RuntimeException, DOMException)
779*cdf0e10cSrcweir     {
780*cdf0e10cSrcweir         if ((aName.getLength() <= 0) ||
781*cdf0e10cSrcweir             (0 <= aName.indexOf(OUString::createFromAscii(":"))))
782*cdf0e10cSrcweir         {
783*cdf0e10cSrcweir             DOMException e;
784*cdf0e10cSrcweir             e.Code = DOMExceptionType_INVALID_CHARACTER_ERR;
785*cdf0e10cSrcweir             throw e;
786*cdf0e10cSrcweir         }
787*cdf0e10cSrcweir 
788*cdf0e10cSrcweir         ::osl::MutexGuard const g(m_rMutex);
789*cdf0e10cSrcweir 
790*cdf0e10cSrcweir         if (0 == m_aNodePtr) {
791*cdf0e10cSrcweir             throw RuntimeException();
792*cdf0e10cSrcweir         }
793*cdf0e10cSrcweir         OString oName = OUStringToOString(aName, RTL_TEXTENCODING_UTF8);
794*cdf0e10cSrcweir         xmlChar *xName = (xmlChar*)oName.getStr();
795*cdf0e10cSrcweir         xmlNodeSetName(m_aNodePtr, xName);
796*cdf0e10cSrcweir     }
797*cdf0e10cSrcweir 
798*cdf0e10cSrcweir }
799