1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: elementlist.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "elementlist.hxx"
38 CElementList::CElementList(const CElement
* aElement
, const OUString
& aName
)
39 : m_pElement(aElement
)
42 , m_bRebuild(sal_True
)
44 OString o1
= OUStringToOString(aName
, RTL_TEXTENCODING_UTF8
);
45 xName
= new xmlChar
[o1
.getLength()];
46 strcpy((char*)xName
, o1
.getStr());
47 registerListener(aElement
);
50 CElementList::CElementList(const CElement
* aElement
, const OUString
& aName
, const OUString
& aURI
)
51 : m_pElement(aElement
)
54 , m_bRebuild(sal_True
)
56 OString o1
= OUStringToOString(aName
, RTL_TEXTENCODING_UTF8
);
57 xName
= new xmlChar
[o1
.getLength()];
58 strcpy((char*)xName
, o1
.getStr());
59 OString o2
= OUStringToOString(aURI
, RTL_TEXTENCODING_UTF8
);
60 xURI
= new xmlChar
[o2
.getLength()];
61 strcpy((char*)xURI
, o2
.getStr());
62 registerListener(aElement
);
65 void CElementList::registerListener(const CElement
* pElement
)
69 Reference
< XNode
> xNode(CNode::get(static_cast<const CNode
*>(pElement
)->m_aNodePtr
));
70 Reference
< XEventTarget
> xTarget(xNode
, UNO_QUERY_THROW
);
71 OUString aType
= OUString::createFromAscii("DOMSubtreeModified");
72 sal_Bool capture
= sal_False
;
73 xTarget
->addEventListener(aType
, Reference
< XEventListener
>(this), capture
);
74 } catch (Exception
&e
){
75 OString
aMsg("Exception caught while registering NodeList as listener:\n");
76 aMsg
+= OUStringToOString(e
.Message
, RTL_TEXTENCODING_ASCII_US
);
77 OSL_ENSURE(sal_False
, aMsg
.getStr());
81 void CElementList::buildlist(xmlNodePtr pNode
, sal_Bool start
)
83 // bail out if no rebuild is needed
89 m_nodevector
.erase(m_nodevector
.begin(), m_nodevector
.end());
90 m_bRebuild
= sal_False
; // don't rebuild until tree is mutated
94 while (pNode
!= NULL
)
96 if (pNode
->type
== XML_ELEMENT_NODE
&& strcmp((char*)pNode
->name
, (char*)xName
)==0)
99 m_nodevector
.push_back(pNode
);
101 if (pNode
->ns
!= NULL
&& strcmp((char*)pNode
->ns
->href
, (char*)xURI
) == 0)
102 m_nodevector
.push_back(pNode
);
104 if (pNode
->children
!= NULL
) buildlist(pNode
->children
, sal_False
);
106 if (!start
) pNode
= pNode
->next
;
107 else break; // fold back
112 The number of nodes in the list.
114 sal_Int32 SAL_CALL
CElementList::getLength() throw (RuntimeException
)
116 // this has to be 'live'
117 buildlist(static_cast<const CNode
*>(m_pElement
)->m_aNodePtr
);
118 return m_nodevector
.size();
121 Returns the indexth item in the collection.
123 Reference
< XNode
> SAL_CALL
CElementList::item(sal_Int32 index
) throw (RuntimeException
)
125 if (index
< 0) throw RuntimeException();
126 buildlist(static_cast<const CNode
*>(m_pElement
)->m_aNodePtr
);
127 return Reference
< XNode
>(CNode::get(m_nodevector
[index
]));
130 // tree mutations can change the list
131 void SAL_CALL
CElementList::handleEvent(const Reference
< XEvent
>& evt
) throw (RuntimeException
)
133 Reference
< XEvent
> aEvent
= evt
;
134 m_bRebuild
= sal_True
;