Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
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/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
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 .
18 : : */
19 : :
20 : : #include "elementlist.hxx"
21 : :
22 : : #include <string.h>
23 : :
24 : : #include <element.hxx>
25 : : #include <document.hxx>
26 : :
27 : :
28 : : namespace DOM
29 : : {
30 : :
31 : 26 : static xmlChar* lcl_initXmlString(::rtl::OUString const& rString)
32 : : {
33 : : ::rtl::OString const os =
34 [ + - ]: 26 : ::rtl::OUStringToOString(rString, RTL_TEXTENCODING_UTF8);
35 [ + - ]: 26 : xmlChar *const pRet = new xmlChar[os.getLength() + 1];
36 : 26 : strcpy(reinterpret_cast<char*>(pRet), os.getStr());
37 : 26 : return pRet;
38 : : }
39 : :
40 : 18 : CElementList::CElementList(::rtl::Reference<CElement> const& pElement,
41 : : ::osl::Mutex & rMutex,
42 : : OUString const& rName, OUString const*const pURI)
43 : : : m_pElement(pElement)
44 : : , m_rMutex(rMutex)
45 : : , m_pName(lcl_initXmlString(rName))
46 : : , m_pURI((pURI) ? lcl_initXmlString(*pURI) : 0)
47 [ + - ][ + + ]: 18 : , m_bRebuild(true)
[ + - ][ + - ]
48 : : {
49 [ + + ]: 18 : if (m_pElement.is()) {
50 [ + - ]: 14 : registerListener(*m_pElement);
51 : : }
52 : 18 : }
53 : :
54 : 14 : void CElementList::registerListener(CElement & rElement)
55 : : {
56 : : try {
57 : : Reference< XEventTarget > const xTarget(
58 [ + - ]: 14 : static_cast<XElement*>(& rElement), UNO_QUERY_THROW);
59 : 14 : sal_Bool capture = sal_False;
60 [ + - ]: 14 : xTarget->addEventListener("DOMSubtreeModified",
61 [ + - ][ + - ]: 14 : Reference< XEventListener >(this), capture);
62 [ # # ]: 0 : } catch (const Exception &e){
63 : 0 : OString aMsg("Exception caught while registering NodeList as listener:\n");
64 [ # # ]: 0 : aMsg += OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US);
65 : 0 : OSL_FAIL(aMsg.getStr());
66 : : }
67 : 14 : }
68 : :
69 : 80 : void CElementList::buildlist(xmlNodePtr pNode, sal_Bool start)
70 : : {
71 : : // bail out if no rebuild is needed
72 [ + + ]: 80 : if (start) {
73 [ + + ]: 54 : if (!m_bRebuild)
74 : : {
75 : 80 : return;
76 : : } else {
77 : 24 : m_nodevector.erase(m_nodevector.begin(), m_nodevector.end());
78 : 24 : m_bRebuild = false; // don't rebuild until tree is mutated
79 : : }
80 : : }
81 : :
82 [ + + ]: 80 : while (pNode != NULL )
83 : : {
84 [ + - + + ]: 108 : if (pNode->type == XML_ELEMENT_NODE &&
[ + + ]
85 : 54 : (strcmp((char*)pNode->name, (char*)m_pName.get()) == 0))
86 : : {
87 [ + + ]: 32 : if (!m_pURI) {
88 : 24 : m_nodevector.push_back(pNode);
89 : : } else {
90 [ + + + - ]: 12 : if (pNode->ns != NULL && (0 ==
[ + + ]
91 : 4 : strcmp((char*)pNode->ns->href, (char*)m_pURI.get())))
92 : : {
93 : 4 : m_nodevector.push_back(pNode);
94 : : }
95 : : }
96 : : }
97 [ + + ]: 54 : if (pNode->children != NULL) buildlist(pNode->children, sal_False);
98 : :
99 [ + + ]: 54 : if (!start) pNode = pNode->next;
100 : 24 : else break; // fold back
101 : : }
102 : : }
103 : :
104 : : /**
105 : : The number of nodes in the list.
106 : : */
107 : 28 : sal_Int32 SAL_CALL CElementList::getLength() throw (RuntimeException)
108 : : {
109 [ + - ]: 28 : ::osl::MutexGuard const g(m_rMutex);
110 : :
111 [ + + ]: 28 : if (!m_pElement.is()) { return 0; }
112 : :
113 : : // this has to be 'live'
114 [ + - ]: 24 : buildlist(m_pElement->GetNodePtr());
115 [ + - ]: 28 : return m_nodevector.size();
116 : : }
117 : : /**
118 : : Returns the indexth item in the collection.
119 : : */
120 : 30 : Reference< XNode > SAL_CALL CElementList::item(sal_Int32 index)
121 : : throw (RuntimeException)
122 : : {
123 [ - + ][ # # ]: 30 : if (index < 0) throw RuntimeException();
124 : :
125 [ + - ]: 30 : ::osl::MutexGuard const g(m_rMutex);
126 : :
127 [ - + ][ # # ]: 30 : if (!m_pElement.is()) { return 0; }
128 : :
129 [ + - ]: 30 : buildlist(m_pElement->GetNodePtr());
130 [ + + ]: 30 : if (m_nodevector.size() <= static_cast<size_t>(index)) {
131 [ + - ]: 2 : throw RuntimeException();
132 : : }
133 : : Reference< XNode > const xRet(
134 [ + - ][ + - ]: 28 : m_pElement->GetOwnerDocument().GetCNode(m_nodevector[index]).get());
[ + - ][ + - ]
135 [ + - ]: 30 : return xRet;
136 : : }
137 : :
138 : : // tree mutations can change the list
139 : 82 : void SAL_CALL CElementList::handleEvent(Reference< XEvent > const&)
140 : : throw (RuntimeException)
141 : : {
142 [ + - ]: 82 : ::osl::MutexGuard const g(m_rMutex);
143 : :
144 [ + - ]: 82 : m_bRebuild = true;
145 : 82 : }
146 : : }
147 : :
148 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|