merge the formfield patch from ooo-build
[ooovba.git] / unoxml / source / dom / document.cxx
blob7b7920e52d36b821ae24b19365078c75f4a9f76b
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: document.cxx,v $
10 * $Revision: 1.15 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include <com/sun/star/uno/Sequence.h>
33 #include "document.hxx"
34 #include "attr.hxx"
35 #include "element.hxx"
36 #include "cdatasection.hxx"
37 #include "documentfragment.hxx"
38 #include "text.hxx"
39 #include "cdatasection.hxx"
40 #include "comment.hxx"
41 #include "processinginstruction.hxx"
42 #include "entityreference.hxx"
43 #include "documenttype.hxx"
44 #include "elementlist.hxx"
45 #include "domimplementation.hxx"
47 #include "../events/event.hxx"
48 #include "../events/mutationevent.hxx"
49 #include "../events/uievent.hxx"
50 #include "../events/mouseevent.hxx"
52 #include <string.h>
54 #include <com/sun/star/xml/sax/FastToken.hpp>
55 #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
57 namespace DOM
59 void CDocument::addnode(xmlNodePtr aNode)
61 if (aNode != (xmlNodePtr)m_aDocPtr)
63 Reference< XNode >* nref = new Reference< XNode >(CNode::get(aNode));
64 m_aNodeRefList.push_back(nref);
68 CDocument::~CDocument()
70 Reference< XNode >* pRef;
71 nodereflist_t::const_iterator r = m_aNodeRefList.begin();
72 while (r!=m_aNodeRefList.end())
74 pRef = *r;
75 delete pRef;
76 r++;
79 // get rid of leftover instances, if anybody still holds a
80 // reference to one of these, it will be invalid!
82 CNode* aNode = 0;
83 nodelist_t::const_iterator i = m_aNodeList.begin();
84 while (i!=m_aNodeList.end())
86 aNode = CNode::get(*i, sal_False);
87 if (aNode != 0)
89 // CNode::remove(*i);
90 // delete will remove
91 delete aNode;
93 i++;
97 xmlFreeDoc(m_aDocPtr);
101 CDocument::CDocument(xmlDocPtr aDocPtr):
102 m_aNodeRefList(),
103 m_aDocPtr(aDocPtr),
104 m_streamListeners()
106 // init node base
107 m_aNodeType = NodeType_DOCUMENT_NODE;
108 init_node((xmlNodePtr)m_aDocPtr);
111 void SAL_CALL CDocument::saxify(
112 const Reference< XDocumentHandler >& i_xHandler) {
113 i_xHandler->startDocument();
114 for (xmlNodePtr pChild = m_aNodePtr->children;
115 pChild != 0; pChild = pChild->next) {
116 CNode * pNode = CNode::get(pChild);
117 OSL_ENSURE(pNode != 0, "CNode::get returned 0");
118 pNode->saxify(i_xHandler);
120 i_xHandler->endDocument();
123 void SAL_CALL CDocument::fastSaxify( Context& rContext ) {
124 rContext.mxDocHandler->startDocument();
125 for (xmlNodePtr pChild = m_aNodePtr->children;
126 pChild != 0; pChild = pChild->next) {
127 CNode * pNode = CNode::get(pChild);
128 OSL_ENSURE(pNode != 0, "CNode::get returned 0");
129 pNode->fastSaxify(rContext);
131 rContext.mxDocHandler->endDocument();
134 void SAL_CALL CDocument::addListener(const Reference< XStreamListener >& aListener )
135 throw (RuntimeException)
137 m_streamListeners.insert(aListener);
140 void SAL_CALL CDocument::removeListener(const Reference< XStreamListener >& aListener )
141 throw (RuntimeException)
143 m_streamListeners.erase(aListener);
146 // IO context functions for libxml2 interaction
147 typedef struct {
148 Reference< XOutputStream > stream;
149 bool allowClose;
150 } IOContext;
152 extern "C" {
153 // write callback
154 // int xmlOutputWriteCallback (void * context, const char * buffer, int len)
155 static int writeCallback(void *context, const char* buffer, int len){
156 // create a sequence and write it to the stream
157 IOContext *pContext = static_cast<IOContext*>(context);
158 Sequence<sal_Int8> bs(reinterpret_cast<const sal_Int8*>(buffer), len);
159 pContext->stream->writeBytes(bs);
160 return len;
163 // clsoe callback
164 //int xmlOutputCloseCallback (void * context)
165 static int closeCallback(void *context)
167 IOContext *pContext = static_cast<IOContext*>(context);
168 if (pContext->allowClose) {
169 pContext->stream->closeOutput();
171 return 0;
173 } // extern "C"
175 void SAL_CALL CDocument::start()
176 throw (RuntimeException)
178 if (! m_rOutputStream.is()) return;
180 // notify listners about start
181 listenerlist_t::const_iterator iter1 = m_streamListeners.begin();
182 while (iter1 != m_streamListeners.end()) {
183 Reference< XStreamListener > aListener = *iter1;
184 aListener->started();
185 iter1++;
188 // setup libxml IO and write data to output stream
189 IOContext ioctx = {m_rOutputStream, false};
190 xmlOutputBufferPtr pOut = xmlOutputBufferCreateIO(
191 writeCallback, closeCallback, &ioctx, NULL);
192 xmlSaveFileTo(pOut, m_aNodePtr->doc, NULL);
194 // call listeners
195 listenerlist_t::const_iterator iter2 = m_streamListeners.begin();
196 while (iter2 != m_streamListeners.end()) {
197 Reference< XStreamListener > aListener = *iter2;
198 aListener->closed();
199 iter2++;
204 void SAL_CALL CDocument::terminate()
205 throw (RuntimeException)
207 // not supported
210 void SAL_CALL CDocument::setOutputStream( const Reference< XOutputStream >& aStream )
211 throw (RuntimeException)
213 m_rOutputStream = aStream;
216 Reference< XOutputStream > SAL_CALL CDocument::getOutputStream() throw (RuntimeException)
218 return m_rOutputStream;
221 // Creates an Attr of the given name.
222 Reference< XAttr > SAL_CALL CDocument::createAttribute(const OUString& name)
223 throw (RuntimeException, DOMException)
225 OString o1 = OUStringToOString(name, RTL_TEXTENCODING_UTF8);
226 xmlChar *xName = (xmlChar*)o1.getStr();
227 return Reference< XAttr >(static_cast< CAttr* >(
228 CNode::get((xmlNodePtr)xmlNewDocProp(m_aDocPtr, xName, NULL))));
231 // Creates an attribute of the given qualified name and namespace URI.
232 Reference< XAttr > SAL_CALL CDocument::createAttributeNS(
233 const OUString& ns, const OUString& qname)
234 throw (RuntimeException, DOMException)
237 // libxml does not allow a NS definition to be attached to an
238 // attribute node - which is a good thing, since namespaces are
239 // only defined as parts of element nodes
240 // thus, we create a temporary element node which carries the ns definition
241 // and is removed/merged as soon as the attribute gets append to it's
242 // actual parent
243 sal_Int32 i = qname.indexOf(':');
244 OString oPrefix, oName, oUri;
245 xmlChar *xPrefix, *xName, *xUri;
246 if (i != -1)
248 oPrefix = OUStringToOString(qname.copy(0, i), RTL_TEXTENCODING_UTF8);
249 xPrefix = (xmlChar*)oPrefix.getStr();
250 oName = OUStringToOString(qname.copy(i+1, qname.getLength()-i-1), RTL_TEXTENCODING_UTF8);
252 else
254 xPrefix = (xmlChar*)"";
255 oName = OUStringToOString(qname, RTL_TEXTENCODING_UTF8);
257 xName = (xmlChar*)oName.getStr();
258 oUri = OUStringToOString(ns, RTL_TEXTENCODING_UTF8);
259 xUri = (xmlChar*)oUri.getStr();
261 // create the carrier node
262 xmlNodePtr pNode = xmlNewDocNode(m_aDocPtr, NULL, (xmlChar*)"__private", NULL);
263 xmlNsPtr pNs = xmlNewNs(pNode, xUri, xPrefix);
264 xmlAttrPtr pAttr = xmlNewNsProp(pNode, pNs, xName, NULL);
265 return Reference< XAttr >(static_cast< CAttr* >(CNode::get((xmlNodePtr)pAttr)));
268 // Creates a CDATASection node whose value is the specified string.
269 Reference< XCDATASection > SAL_CALL CDocument::createCDATASection(const OUString& data)
270 throw (RuntimeException)
272 xmlChar *xData = (xmlChar*)OUStringToOString(data, RTL_TEXTENCODING_UTF8).getStr();
273 xmlNodePtr pText = xmlNewCDataBlock(m_aDocPtr, xData, strlen((char*)xData));
274 return Reference< XCDATASection >(static_cast< CCDATASection* >(CNode::get(pText)));
277 // Creates a Comment node given the specified string.
278 Reference< XComment > SAL_CALL CDocument::createComment(const OUString& data)
279 throw (RuntimeException)
281 OString o1 = OUStringToOString(data, RTL_TEXTENCODING_UTF8);
282 xmlChar *xData = (xmlChar*)o1.getStr();
283 xmlNodePtr pComment = xmlNewDocComment(m_aDocPtr, xData);
284 return Reference< XComment >(static_cast< CComment* >(CNode::get(pComment)));
287 //Creates an empty DocumentFragment object.
288 Reference< XDocumentFragment > SAL_CALL CDocument::createDocumentFragment()
289 throw (RuntimeException)
291 xmlNodePtr pFrag = xmlNewDocFragment(m_aDocPtr);
292 return Reference< XDocumentFragment >(static_cast< CDocumentFragment* >(CNode::get(pFrag)));
295 // Creates an element of the type specified.
296 Reference< XElement > SAL_CALL CDocument::createElement(const OUString& tagName)
297 throw (RuntimeException, DOMException)
299 OString o1 = OUStringToOString(tagName, RTL_TEXTENCODING_UTF8);
300 xmlChar *xName = (xmlChar*)o1.getStr();
301 xmlNodePtr aNodePtr = xmlNewDocNode(m_aDocPtr, NULL, xName, NULL);
302 return Reference< XElement >(static_cast< CElement* >(CNode::get(aNodePtr)));
305 // Creates an element of the given qualified name and namespace URI.
306 Reference< XElement > SAL_CALL CDocument::createElementNS(
307 const OUString& ns, const OUString& qname)
308 throw (RuntimeException, DOMException)
310 sal_Int32 i = qname.indexOf(':');
311 if (ns.getLength() == 0) throw RuntimeException();
312 xmlChar *xPrefix;
313 xmlChar *xName;
314 OString o1, o2, o3;
315 if ( i != -1) {
316 o1 = OUStringToOString(qname.copy(0, i), RTL_TEXTENCODING_UTF8);
317 xPrefix = (xmlChar*)o1.getStr();
318 o2 = OUStringToOString(qname.copy(i+1, qname.getLength()-i-1), RTL_TEXTENCODING_UTF8);
319 xName = (xmlChar*)o2.getStr();
320 } else {
321 // default prefix
322 xPrefix = (xmlChar*)"";
323 o2 = OUStringToOString(qname, RTL_TEXTENCODING_UTF8);
324 xName = (xmlChar*)o2.getStr();
326 o3 = OUStringToOString(ns, RTL_TEXTENCODING_UTF8);
327 xmlChar *xUri = (xmlChar*)o3.getStr();
329 // xmlNsPtr aNsPtr = xmlNewReconciledNs?
330 // xmlNsPtr aNsPtr = xmlNewGlobalNs?
331 xmlNodePtr aNodePtr = xmlNewDocNode(m_aDocPtr, NULL, xName, NULL);
332 xmlNsPtr pNs = xmlNewNs(aNodePtr, xUri, xPrefix);
333 xmlSetNs(aNodePtr, pNs);
334 return Reference< XElement >(static_cast< CElement* >(CNode::get(aNodePtr)));
337 //Creates an EntityReference object.
338 Reference< XEntityReference > SAL_CALL CDocument::createEntityReference(const OUString& name)
339 throw (RuntimeException, DOMException)
341 OString o1 = OUStringToOString(name, RTL_TEXTENCODING_UTF8);
342 xmlChar *xName = (xmlChar*)o1.getStr();
343 xmlNodePtr aNodePtr = xmlNewReference(m_aDocPtr, xName);
344 return Reference< XEntityReference >(static_cast< CEntityReference* >(CNode::get(aNodePtr)));
347 // Creates a ProcessingInstruction node given the specified name and
348 // data strings.
349 Reference< XProcessingInstruction > SAL_CALL CDocument::createProcessingInstruction(
350 const OUString& target, const OUString& data)
351 throw (RuntimeException, DOMException)
353 OString o1 = OUStringToOString(target, RTL_TEXTENCODING_UTF8);
354 xmlChar *xTarget = (xmlChar*)o1.getStr();
355 OString o2 = OUStringToOString(data, RTL_TEXTENCODING_UTF8);
356 xmlChar *xData = (xmlChar*)o2.getStr();
357 xmlNodePtr aNodePtr = xmlNewPI(xTarget, xData);
358 aNodePtr->doc = m_aDocPtr;
359 return Reference< XProcessingInstruction >(static_cast< CProcessingInstruction* >(CNode::get(aNodePtr)));
362 // Creates a Text node given the specified string.
363 Reference< XText > SAL_CALL CDocument::createTextNode(const OUString& data)
364 throw (RuntimeException)
366 OString o1 = OUStringToOString(data, RTL_TEXTENCODING_UTF8);
367 xmlChar *xData = (xmlChar*)o1.getStr();
368 xmlNodePtr aNodePtr = xmlNewDocText(m_aDocPtr, xData);
369 return Reference< XText >(static_cast< CText* >(CNode::get(aNodePtr)));
372 // The Document Type Declaration (see DocumentType) associated with this
373 // document.
374 Reference< XDocumentType > SAL_CALL CDocument::getDoctype()
375 throw (RuntimeException)
377 // find the doc type
378 xmlNodePtr cur = m_aDocPtr->children;
379 while (cur != NULL)
381 if (cur->type == XML_DOCUMENT_TYPE_NODE || cur->type == XML_DTD_NODE)
382 break;
384 return Reference< XDocumentType >(static_cast< CDocumentType* >(CNode::get(cur)));
387 /// get the pointer to the root element node of the document
388 static xmlNodePtr SAL_CALL _getDocumentRootPtr(xmlDocPtr i_pDocument) {
389 // find the document element
390 xmlNodePtr cur = i_pDocument->children;
391 while (cur != NULL)
393 if (cur->type == XML_ELEMENT_NODE)
394 break;
395 cur = cur->next;
397 return cur;
400 // This is a convenience attribute that allows direct access to the child
401 // node that is the root element of the document.
402 Reference< XElement > SAL_CALL CDocument::getDocumentElement()
403 throw (RuntimeException)
405 xmlNodePtr cur = _getDocumentRootPtr(m_aDocPtr);
406 return Reference< XElement >(static_cast< CElement* >(CNode::get(cur)));
409 static xmlNodePtr _search_element_by_id(const xmlNodePtr cur, const xmlChar* id)
412 if (cur == NULL)
413 return NULL;
414 // look in current node
415 if (cur->type == XML_ELEMENT_NODE)
417 xmlAttrPtr a = cur->properties;
418 while (a != NULL)
420 if (a->atype == XML_ATTRIBUTE_ID) {
421 if (strcmp((char*)a->children->content, (char*)id) == 0)
422 return cur;
424 a = a->next;
427 // look in children
428 xmlNodePtr result = _search_element_by_id(cur->children, id);
429 if (result != NULL)
430 return result;
431 result = _search_element_by_id(cur->next, id);
432 return result;
435 // Returns the Element whose ID is given by elementId.
436 Reference< XElement > SAL_CALL CDocument::getElementById(const OUString& elementId)
437 throw (RuntimeException)
439 // search the tree for an element with the given ID
440 OString o1 = OUStringToOString(elementId, RTL_TEXTENCODING_UTF8);
441 xmlChar *xId = (xmlChar*)o1.getStr();
442 xmlNodePtr pStart = CNode::getNodePtr(getDocumentElement().get());
443 xmlNodePtr aNodePtr = _search_element_by_id(pStart, xId);
444 return Reference< XElement >(static_cast< CElement* >(CNode::get(aNodePtr)));
448 Reference< XNodeList > SAL_CALL CDocument::getElementsByTagName(const OUString& tagname)
449 throw (RuntimeException)
451 // build a list
452 return Reference< XNodeList >(
453 new CElementList(static_cast< CElement* >(
454 this->getDocumentElement().get()), tagname));
457 Reference< XNodeList > SAL_CALL CDocument::getElementsByTagNameNS(
458 const OUString& namespaceURI, const OUString& localName)
459 throw (RuntimeException)
461 return Reference< XNodeList >(
462 new CElementList(static_cast< CElement* >(
463 this->getDocumentElement().get()), namespaceURI, localName));
466 Reference< XDOMImplementation > SAL_CALL CDocument::getImplementation()
467 throw (RuntimeException)
469 // XXX
470 return Reference< XDOMImplementation >(CDOMImplementation::get());
473 // helper function to recall import for siblings
474 static Reference< XNode > _import_siblings (
475 const Reference< XNode > aNode, const Reference< XNode> parent, CDocument* pTarget)
477 Reference< XNode > sibling = aNode;
478 Reference< XNode > tmp;
479 Reference< XNode > firstImported;
480 while (sibling.is())
482 tmp = pTarget->importNode(sibling, sal_True);
483 parent->appendChild(tmp);
484 if (!firstImported.is())
485 firstImported = tmp;
486 sibling = sibling->getNextSibling();
488 return firstImported;
491 Reference< XNode > SAL_CALL CDocument::importNode(
492 const Reference< XNode >& importedNode, sal_Bool deep)
493 throw (RuntimeException, DOMException)
495 // this node could be from another memory model
496 // only use uno interfaces to access is!!!
498 // allready in doc?
499 if ( importedNode->getOwnerDocument() ==
500 Reference< XDocument>(static_cast< CDocument* >(CNode::get((xmlNodePtr)m_aDocPtr))))
501 return importedNode;
503 Reference< XNode > aNode;
504 NodeType aNodeType = importedNode->getNodeType();
505 switch (aNodeType)
507 case NodeType_ATTRIBUTE_NODE:
509 Reference< XAttr > attr(importedNode, UNO_QUERY);
510 Reference< XAttr > newAttr = createAttribute(attr->getName());
511 newAttr->setValue(attr->getValue());
512 aNode.set(newAttr, UNO_QUERY);
513 break;
515 case NodeType_CDATA_SECTION_NODE:
517 Reference< XCDATASection > cdata(importedNode, UNO_QUERY);
518 Reference< XCDATASection > newCdata = createCDATASection(cdata->getData());
519 aNode.set(newCdata, UNO_QUERY);
520 break;
522 case NodeType_COMMENT_NODE:
524 Reference< XComment > comment(importedNode, UNO_QUERY);
525 Reference< XComment > newComment = createComment(comment->getData());
526 aNode.set(newComment, UNO_QUERY);
527 break;
529 case NodeType_DOCUMENT_FRAGMENT_NODE:
531 Reference< XDocumentFragment > frag(importedNode, UNO_QUERY);
532 Reference< XDocumentFragment > newFrag = createDocumentFragment();
533 aNode.set(newFrag, UNO_QUERY);
534 break;
536 case NodeType_ELEMENT_NODE:
538 Reference< XElement > element(importedNode, UNO_QUERY);
539 OUString aNsUri = importedNode->getNamespaceURI();
540 OUString aNsPrefix = importedNode->getPrefix();
541 OUString aQName = element->getTagName();
542 Reference< XElement > newElement;
543 if (aNsUri.getLength() > 0)
546 if (aNsPrefix.getLength() > 0)
547 aQName = aNsPrefix + OUString::createFromAscii(":") + aQName;
548 newElement = createElementNS(aNsUri, aQName);
550 else
551 newElement = createElement(aQName);
553 // get attributes
554 if (element->hasAttributes())
556 Reference< XNamedNodeMap > attribs = element->getAttributes();
557 Reference< XAttr > curAttr;
558 for (sal_Int32 i = 0; i < attribs->getLength(); i++)
560 curAttr = Reference< XAttr >(attribs->item(i), UNO_QUERY);
561 OUString aAttrUri = curAttr->getNamespaceURI();
562 OUString aAttrPrefix = curAttr->getPrefix();
563 OUString aAttrName = curAttr->getName();
564 if (aAttrUri.getLength() > 0)
566 if (aAttrPrefix.getLength() > 0)
567 aAttrName = aAttrPrefix + OUString::createFromAscii(":") + aAttrName;
568 newElement->setAttributeNS(aAttrUri, aAttrName, curAttr->getValue());
570 else
571 newElement->setAttribute(aAttrName, curAttr->getValue());
574 aNode.set(newElement, UNO_QUERY);
575 break;
577 case NodeType_ENTITY_REFERENCE_NODE:
579 Reference< XEntityReference > ref(importedNode, UNO_QUERY);
580 Reference< XEntityReference > newRef(createEntityReference(ref->getNodeName()));
581 aNode.set(newRef, UNO_QUERY);
582 break;
584 case NodeType_PROCESSING_INSTRUCTION_NODE:
586 Reference< XProcessingInstruction > pi(importedNode, UNO_QUERY);
587 Reference< XProcessingInstruction > newPi(
588 createProcessingInstruction(pi->getTarget(), pi->getData()));
589 aNode.set(newPi, UNO_QUERY);
590 break;
592 case NodeType_TEXT_NODE:
594 Reference< XText > text(importedNode, UNO_QUERY);
595 Reference< XText > newText(createTextNode(text->getData()));
596 aNode.set(newText, UNO_QUERY);
597 break;
599 case NodeType_ENTITY_NODE:
600 case NodeType_DOCUMENT_NODE:
601 case NodeType_DOCUMENT_TYPE_NODE:
602 case NodeType_NOTATION_NODE:
603 default:
604 // can't be imported
605 throw RuntimeException();
608 if (deep)
610 // get children and import them
611 Reference< XNode > child = importedNode->getFirstChild();
612 if (child.is())
614 _import_siblings(child, aNode, this);
618 /* DOMNodeInsertedIntoDocument
619 * Fired when a node is being inserted into a document,
620 * either through direct insertion of the Node or insertion of a
621 * subtree in which it is contained. This event is dispatched after
622 * the insertion has taken place. The target of this event is the node
623 * being inserted. If the Node is being directly inserted the DOMNodeInserted
624 * event will fire before the DOMNodeInsertedIntoDocument event.
625 * Bubbles: No
626 * Cancelable: No
627 * Context Info: None
629 if (aNode.is())
631 Reference< XDocumentEvent > docevent(getOwnerDocument(), UNO_QUERY);
632 Reference< XMutationEvent > event(docevent->createEvent(
633 OUString::createFromAscii("DOMNodeInsertedIntoDocument")), UNO_QUERY);
634 event->initMutationEvent(OUString::createFromAscii("DOMNodeInsertedIntoDocument")
635 , sal_True, sal_False, Reference< XNode >(),
636 OUString(), OUString(), OUString(), (AttrChangeType)0 );
637 dispatchEvent(Reference< XEvent >(event, UNO_QUERY));
640 return aNode;
642 OUString SAL_CALL CDocument::getNodeName()throw (RuntimeException)
644 return OUString::createFromAscii("#document");
646 OUString SAL_CALL CDocument::getNodeValue() throw (RuntimeException)
648 return OUString();
651 Reference< XEvent > SAL_CALL CDocument::createEvent(const OUString& aType) throw (RuntimeException)
653 events::CEvent *pEvent = 0;
654 if (
655 aType.compareToAscii("DOMSubtreeModified") == 0||
656 aType.compareToAscii("DOMNodeInserted") == 0||
657 aType.compareToAscii("DOMNodeRemoved") == 0||
658 aType.compareToAscii("DOMNodeRemovedFromDocument") == 0||
659 aType.compareToAscii("DOMNodeInsertedIntoDocument") == 0||
660 aType.compareToAscii("DOMAttrModified") == 0||
661 aType.compareToAscii("DOMCharacterDataModified") == 0)
663 pEvent = new events::CMutationEvent;
665 } else if (
666 aType.compareToAscii("DOMFocusIn") == 0||
667 aType.compareToAscii("DOMFocusOut") == 0||
668 aType.compareToAscii("DOMActivate") == 0)
670 pEvent = new events::CUIEvent;
671 } else if (
672 aType.compareToAscii("click") == 0||
673 aType.compareToAscii("mousedown") == 0||
674 aType.compareToAscii("mouseup") == 0||
675 aType.compareToAscii("mouseover") == 0||
676 aType.compareToAscii("mousemove") == 0||
677 aType.compareToAscii("mouseout") == 0 )
679 pEvent = new events::CMouseEvent;
681 else // generic event
683 pEvent = new events::CEvent;
685 return Reference< XEvent >(pEvent);
688 // ::com::sun::star::xml::sax::XSAXSerializable
689 void SAL_CALL CDocument::serialize(
690 const Reference< XDocumentHandler >& i_xHandler,
691 const Sequence< beans::StringPair >& i_rNamespaces)
692 throw (RuntimeException, SAXException)
694 // add new namespaces to root node
695 xmlNodePtr pRoot = _getDocumentRootPtr(m_aDocPtr);
696 if (0 != pRoot) {
697 const beans::StringPair * pSeq = i_rNamespaces.getConstArray();
698 for (const beans::StringPair *pNsDef = pSeq;
699 pNsDef < pSeq + i_rNamespaces.getLength(); ++pNsDef) {
700 OString prefix = OUStringToOString(pNsDef->First,
701 RTL_TEXTENCODING_UTF8);
702 OString href = OUStringToOString(pNsDef->Second,
703 RTL_TEXTENCODING_UTF8);
704 // this will only add the ns if it does not exist already
705 xmlNewNs(pRoot, reinterpret_cast<const xmlChar*>(href.getStr()),
706 reinterpret_cast<const xmlChar*>(prefix.getStr()));
708 // eliminate duplicate namespace declarations
709 _nscleanup(pRoot->children, pRoot);
711 saxify(i_xHandler);
714 // ::com::sun::star::xml::sax::XFastSAXSerializable
715 void SAL_CALL CDocument::fastSerialize( const Reference< XFastDocumentHandler >& i_xHandler,
716 const Reference< XFastTokenHandler >& i_xTokenHandler,
717 const Sequence< beans::StringPair >& i_rNamespaces,
718 const Sequence< beans::Pair< rtl::OUString, sal_Int32 > >& i_rRegisterNamespaces )
719 throw (SAXException, RuntimeException)
721 // add new namespaces to root node
722 xmlNodePtr pRoot = _getDocumentRootPtr(m_aDocPtr);
723 if (0 != pRoot) {
724 const beans::StringPair * pSeq = i_rNamespaces.getConstArray();
725 for (const beans::StringPair *pNsDef = pSeq;
726 pNsDef < pSeq + i_rNamespaces.getLength(); ++pNsDef) {
727 OString prefix = OUStringToOString(pNsDef->First,
728 RTL_TEXTENCODING_UTF8);
729 OString href = OUStringToOString(pNsDef->Second,
730 RTL_TEXTENCODING_UTF8);
731 // this will only add the ns if it does not exist already
732 xmlNewNs(pRoot, reinterpret_cast<const xmlChar*>(href.getStr()),
733 reinterpret_cast<const xmlChar*>(prefix.getStr()));
735 // eliminate duplicate namespace declarations
736 _nscleanup(pRoot->children, pRoot);
739 Context aContext(i_xHandler,
740 i_xTokenHandler);
742 // register namespace ids
743 const beans::Pair<OUString,sal_Int32>* pSeq = i_rRegisterNamespaces.getConstArray();
744 for (const beans::Pair<OUString,sal_Int32>* pNs = pSeq;
745 pNs < pSeq + i_rRegisterNamespaces.getLength(); ++pNs)
747 OSL_ENSURE(pNs->Second >= FastToken::NAMESPACE,
748 "CDocument::fastSerialize(): invalid NS token id");
749 aContext.maNamespaceMap[ pNs->First ] = pNs->Second;
752 fastSaxify(aContext);