xref: /aoo42x/main/unoxml/source/dom/elementlist.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include "elementlist.hxx"
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include <string.h>
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #include <element.hxx>
33*cdf0e10cSrcweir #include <document.hxx>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir namespace DOM
37*cdf0e10cSrcweir {
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir     static xmlChar* lcl_initXmlString(::rtl::OUString const& rString)
40*cdf0e10cSrcweir     {
41*cdf0e10cSrcweir         ::rtl::OString const os =
42*cdf0e10cSrcweir             ::rtl::OUStringToOString(rString, RTL_TEXTENCODING_UTF8);
43*cdf0e10cSrcweir         xmlChar *const pRet = new xmlChar[os.getLength() + 1];
44*cdf0e10cSrcweir         strcpy(reinterpret_cast<char*>(pRet), os.getStr());
45*cdf0e10cSrcweir         return pRet;
46*cdf0e10cSrcweir     }
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir     CElementList::CElementList(::rtl::Reference<CElement> const& pElement,
49*cdf0e10cSrcweir             ::osl::Mutex & rMutex,
50*cdf0e10cSrcweir             OUString const& rName, OUString const*const pURI)
51*cdf0e10cSrcweir         : m_pElement(pElement)
52*cdf0e10cSrcweir         , m_rMutex(rMutex)
53*cdf0e10cSrcweir         , m_pName(lcl_initXmlString(rName))
54*cdf0e10cSrcweir         , m_pURI((pURI) ? lcl_initXmlString(*pURI) : 0)
55*cdf0e10cSrcweir         , m_bRebuild(true)
56*cdf0e10cSrcweir     {
57*cdf0e10cSrcweir         if (m_pElement.is()) {
58*cdf0e10cSrcweir             registerListener(*m_pElement);
59*cdf0e10cSrcweir         }
60*cdf0e10cSrcweir     }
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir     void CElementList::registerListener(CElement & rElement)
63*cdf0e10cSrcweir     {
64*cdf0e10cSrcweir         try {
65*cdf0e10cSrcweir             Reference< XEventTarget > const xTarget(
66*cdf0e10cSrcweir                     static_cast<XElement*>(& rElement), UNO_QUERY_THROW);
67*cdf0e10cSrcweir             OUString aType = OUString::createFromAscii("DOMSubtreeModified");
68*cdf0e10cSrcweir             sal_Bool capture = sal_False;
69*cdf0e10cSrcweir             xTarget->addEventListener(aType,
70*cdf0e10cSrcweir                     Reference< XEventListener >(this), capture);
71*cdf0e10cSrcweir         } catch (Exception &e){
72*cdf0e10cSrcweir             OString aMsg("Exception caught while registering NodeList as listener:\n");
73*cdf0e10cSrcweir             aMsg += OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US);
74*cdf0e10cSrcweir             OSL_ENSURE(sal_False, aMsg.getStr());
75*cdf0e10cSrcweir         }
76*cdf0e10cSrcweir     }
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir     void CElementList::buildlist(xmlNodePtr pNode, sal_Bool start)
79*cdf0e10cSrcweir     {
80*cdf0e10cSrcweir         // bail out if no rebuild is needed
81*cdf0e10cSrcweir         if (start) {
82*cdf0e10cSrcweir             if (!m_bRebuild)
83*cdf0e10cSrcweir             {
84*cdf0e10cSrcweir                 return;
85*cdf0e10cSrcweir             } else {
86*cdf0e10cSrcweir                 m_nodevector.erase(m_nodevector.begin(), m_nodevector.end());
87*cdf0e10cSrcweir                 m_bRebuild = false; // don't rebuild until tree is mutated
88*cdf0e10cSrcweir             }
89*cdf0e10cSrcweir         }
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir         while (pNode != NULL )
92*cdf0e10cSrcweir         {
93*cdf0e10cSrcweir             if (pNode->type == XML_ELEMENT_NODE &&
94*cdf0e10cSrcweir                 (strcmp((char*)pNode->name, (char*)m_pName.get()) == 0))
95*cdf0e10cSrcweir             {
96*cdf0e10cSrcweir                 if (!m_pURI) {
97*cdf0e10cSrcweir                     m_nodevector.push_back(pNode);
98*cdf0e10cSrcweir                 } else {
99*cdf0e10cSrcweir                     if (pNode->ns != NULL && (0 ==
100*cdf0e10cSrcweir                          strcmp((char*)pNode->ns->href, (char*)m_pURI.get())))
101*cdf0e10cSrcweir                     {
102*cdf0e10cSrcweir                         m_nodevector.push_back(pNode);
103*cdf0e10cSrcweir                     }
104*cdf0e10cSrcweir                 }
105*cdf0e10cSrcweir             }
106*cdf0e10cSrcweir             if (pNode->children != NULL) buildlist(pNode->children, sal_False);
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir             if (!start) pNode = pNode->next;
109*cdf0e10cSrcweir             else break; // fold back
110*cdf0e10cSrcweir         }
111*cdf0e10cSrcweir     }
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir     /**
114*cdf0e10cSrcweir     The number of nodes in the list.
115*cdf0e10cSrcweir     */
116*cdf0e10cSrcweir     sal_Int32 SAL_CALL CElementList::getLength() throw (RuntimeException)
117*cdf0e10cSrcweir     {
118*cdf0e10cSrcweir         ::osl::MutexGuard const g(m_rMutex);
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir         if (!m_pElement.is()) { return 0; }
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir         // this has to be 'live'
123*cdf0e10cSrcweir         buildlist(m_pElement->GetNodePtr());
124*cdf0e10cSrcweir         return m_nodevector.size();
125*cdf0e10cSrcweir     }
126*cdf0e10cSrcweir     /**
127*cdf0e10cSrcweir     Returns the indexth item in the collection.
128*cdf0e10cSrcweir     */
129*cdf0e10cSrcweir     Reference< XNode > SAL_CALL CElementList::item(sal_Int32 index)
130*cdf0e10cSrcweir         throw (RuntimeException)
131*cdf0e10cSrcweir     {
132*cdf0e10cSrcweir         if (index < 0) throw RuntimeException();
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir         ::osl::MutexGuard const g(m_rMutex);
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir         if (!m_pElement.is()) { return 0; }
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir         buildlist(m_pElement->GetNodePtr());
139*cdf0e10cSrcweir         if (m_nodevector.size() <= static_cast<size_t>(index)) {
140*cdf0e10cSrcweir             throw RuntimeException();
141*cdf0e10cSrcweir         }
142*cdf0e10cSrcweir         Reference< XNode > const xRet(
143*cdf0e10cSrcweir             m_pElement->GetOwnerDocument().GetCNode(m_nodevector[index]).get());
144*cdf0e10cSrcweir         return xRet;
145*cdf0e10cSrcweir     }
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir     // tree mutations can change the list
148*cdf0e10cSrcweir     void SAL_CALL CElementList::handleEvent(Reference< XEvent > const&)
149*cdf0e10cSrcweir         throw (RuntimeException)
150*cdf0e10cSrcweir     {
151*cdf0e10cSrcweir         ::osl::MutexGuard const g(m_rMutex);
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir         m_bRebuild = true;
154*cdf0e10cSrcweir     }
155*cdf0e10cSrcweir }
156