xref: /AOO41X/main/unoxml/source/dom/childlist.cxx (revision e9cbe144f2ea8c6fdc1a6527ef692f8296608908)
1*e9cbe144SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*e9cbe144SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*e9cbe144SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*e9cbe144SAndrew Rist  * distributed with this work for additional information
6*e9cbe144SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*e9cbe144SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*e9cbe144SAndrew Rist  * "License"); you may not use this file except in compliance
9*e9cbe144SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*e9cbe144SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*e9cbe144SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*e9cbe144SAndrew Rist  * software distributed under the License is distributed on an
15*e9cbe144SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*e9cbe144SAndrew Rist  * KIND, either express or implied.  See the License for the
17*e9cbe144SAndrew Rist  * specific language governing permissions and limitations
18*e9cbe144SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*e9cbe144SAndrew Rist  *************************************************************/
21*e9cbe144SAndrew Rist 
22*e9cbe144SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include <childlist.hxx>
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <libxml/tree.h>
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include <node.hxx>
29cdf0e10cSrcweir #include <document.hxx>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir 
32cdf0e10cSrcweir namespace DOM
33cdf0e10cSrcweir {
CChildList(::rtl::Reference<CNode> const & pBase,::osl::Mutex & rMutex)34cdf0e10cSrcweir     CChildList::CChildList(::rtl::Reference<CNode> const& pBase,
35cdf0e10cSrcweir                 ::osl::Mutex & rMutex)
36cdf0e10cSrcweir         : m_pNode(pBase)
37cdf0e10cSrcweir         , m_rMutex(rMutex)
38cdf0e10cSrcweir     {
39cdf0e10cSrcweir     }
40cdf0e10cSrcweir 
41cdf0e10cSrcweir     /**
42cdf0e10cSrcweir     The number of nodes in the list.
43cdf0e10cSrcweir     */
getLength()44cdf0e10cSrcweir     sal_Int32 SAL_CALL CChildList::getLength() throw (RuntimeException)
45cdf0e10cSrcweir     {
46cdf0e10cSrcweir         ::osl::MutexGuard const g(m_rMutex);
47cdf0e10cSrcweir 
48cdf0e10cSrcweir         sal_Int32 length = 0;
49cdf0e10cSrcweir         if (m_pNode != NULL)
50cdf0e10cSrcweir         {
51cdf0e10cSrcweir             xmlNodePtr cur = m_pNode->GetNodePtr();
52cdf0e10cSrcweir             if (0 != cur) {
53cdf0e10cSrcweir                 cur = cur->children;
54cdf0e10cSrcweir             }
55cdf0e10cSrcweir             while (cur != NULL)
56cdf0e10cSrcweir             {
57cdf0e10cSrcweir                 length++;
58cdf0e10cSrcweir                 cur = cur->next;
59cdf0e10cSrcweir             }
60cdf0e10cSrcweir         }
61cdf0e10cSrcweir         return length;
62cdf0e10cSrcweir 
63cdf0e10cSrcweir     }
64cdf0e10cSrcweir     /**
65cdf0e10cSrcweir     Returns the indexth item in the collection.
66cdf0e10cSrcweir     */
item(sal_Int32 index)67cdf0e10cSrcweir     Reference< XNode > SAL_CALL CChildList::item(sal_Int32 index)
68cdf0e10cSrcweir         throw (RuntimeException)
69cdf0e10cSrcweir     {
70cdf0e10cSrcweir         ::osl::MutexGuard const g(m_rMutex);
71cdf0e10cSrcweir 
72cdf0e10cSrcweir         if (m_pNode != NULL)
73cdf0e10cSrcweir         {
74cdf0e10cSrcweir             xmlNodePtr cur = m_pNode->GetNodePtr();
75cdf0e10cSrcweir             if (0 != cur) {
76cdf0e10cSrcweir                 cur = cur->children;
77cdf0e10cSrcweir             }
78cdf0e10cSrcweir             while (cur != NULL)
79cdf0e10cSrcweir             {
80cdf0e10cSrcweir                 if (index-- == 0) {
81cdf0e10cSrcweir                     return Reference< XNode >(
82cdf0e10cSrcweir                             m_pNode->GetOwnerDocument().GetCNode(cur).get());
83cdf0e10cSrcweir                 }
84cdf0e10cSrcweir                 cur = cur->next;
85cdf0e10cSrcweir             }
86cdf0e10cSrcweir         }
87cdf0e10cSrcweir         return 0;
88cdf0e10cSrcweir     }
89cdf0e10cSrcweir }
90