1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "elementlist.hxx"
24 #include <element.hxx>
25 #include <document.hxx>
31 static xmlChar
* lcl_initXmlString(OUString
const& rString
)
34 OUStringToOString(rString
, RTL_TEXTENCODING_UTF8
);
35 xmlChar
*const pRet
= new xmlChar
[os
.getLength() + 1];
36 strcpy(reinterpret_cast<char*>(pRet
), os
.getStr());
40 CElementList::CElementList(::rtl::Reference
<CElement
> const& pElement
,
41 ::osl::Mutex
& rMutex
,
42 OUString
const& rName
, OUString
const*const pURI
)
43 : m_pElement(pElement
)
45 , m_pName(lcl_initXmlString(rName
))
46 , m_pURI((pURI
) ? lcl_initXmlString(*pURI
) : 0)
49 if (m_pElement
.is()) {
50 registerListener(*m_pElement
);
54 void CElementList::registerListener(CElement
& rElement
)
57 Reference
< XEventTarget
> const xTarget(
58 static_cast<XElement
*>(& rElement
), UNO_QUERY_THROW
);
59 sal_Bool capture
= sal_False
;
60 xTarget
->addEventListener("DOMSubtreeModified",
61 Reference
< XEventListener
>(this), capture
);
62 } catch (const Exception
&e
){
63 OString
aMsg("Exception caught while registering NodeList as listener:\n");
64 aMsg
+= OUStringToOString(e
.Message
, RTL_TEXTENCODING_ASCII_US
);
65 OSL_FAIL(aMsg
.getStr());
69 void CElementList::buildlist(xmlNodePtr pNode
, sal_Bool start
)
71 // bail out if no rebuild is needed
77 m_nodevector
.erase(m_nodevector
.begin(), m_nodevector
.end());
78 m_bRebuild
= false; // don't rebuild until tree is mutated
82 while (pNode
!= NULL
)
84 if (pNode
->type
== XML_ELEMENT_NODE
&&
85 (strcmp((char*)pNode
->name
, (char*)m_pName
.get()) == 0))
88 m_nodevector
.push_back(pNode
);
90 if (pNode
->ns
!= NULL
&& (0 ==
91 strcmp((char*)pNode
->ns
->href
, (char*)m_pURI
.get())))
93 m_nodevector
.push_back(pNode
);
97 if (pNode
->children
!= NULL
) buildlist(pNode
->children
, sal_False
);
99 if (!start
) pNode
= pNode
->next
;
100 else break; // fold back
105 The number of nodes in the list.
107 sal_Int32 SAL_CALL
CElementList::getLength() throw (RuntimeException
)
109 ::osl::MutexGuard
const g(m_rMutex
);
111 if (!m_pElement
.is()) { return 0; }
113 // this has to be 'live'
114 buildlist(m_pElement
->GetNodePtr());
115 return m_nodevector
.size();
118 Returns the indexth item in the collection.
120 Reference
< XNode
> SAL_CALL
CElementList::item(sal_Int32 index
)
121 throw (RuntimeException
)
123 if (index
< 0) throw RuntimeException();
125 ::osl::MutexGuard
const g(m_rMutex
);
127 if (!m_pElement
.is()) { return 0; }
129 buildlist(m_pElement
->GetNodePtr());
130 if (m_nodevector
.size() <= static_cast<size_t>(index
)) {
131 throw RuntimeException();
133 Reference
< XNode
> const xRet(
134 m_pElement
->GetOwnerDocument().GetCNode(m_nodevector
[index
]).get());
138 // tree mutations can change the list
139 void SAL_CALL
CElementList::handleEvent(Reference
< XEvent
> const&)
140 throw (RuntimeException
)
142 ::osl::MutexGuard
const g(m_rMutex
);
148 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */