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