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