xref: /AOO41X/main/xmloff/source/forms/attriblistmerge.cxx (revision 63bba73cc51e0afb45f8a8d578158724bb5afee8)
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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_xmloff.hxx"
26 #include "attriblistmerge.hxx"
27 
28 //.........................................................................
29 namespace xmloff
30 {
31 //.........................................................................
32 
33     using namespace ::com::sun::star::uno;
34     using namespace ::com::sun::star::xml;
35 
36     //=====================================================================
37     //= OAttribListMerger
38     //=====================================================================
39     //---------------------------------------------------------------------
addList(const Reference<sax::XAttributeList> & _rxList)40     void OAttribListMerger::addList(const Reference< sax::XAttributeList >& _rxList)
41     {
42         OSL_ENSURE(_rxList.is(), "OAttribListMerger::addList: invalid list!");
43         if (_rxList.is())
44             m_aLists.push_back(_rxList);
45     }
46 
47     //---------------------------------------------------------------------
seekToIndex(sal_Int16 _nGlobalIndex,Reference<sax::XAttributeList> & _rSubList,sal_Int16 & _rLocalIndex)48     sal_Bool OAttribListMerger::seekToIndex(sal_Int16 _nGlobalIndex, Reference< sax::XAttributeList >& _rSubList, sal_Int16& _rLocalIndex)
49     {
50         sal_Int16 nLeftOver = _nGlobalIndex;
51         ConstAttributeListArrayIterator aLookupSublist = m_aLists.begin();
52 
53         for ( ; (aLookupSublist != m_aLists.end()) && (nLeftOver >= (*aLookupSublist)->getLength());
54                 ++aLookupSublist
55             )
56             nLeftOver = nLeftOver - (*aLookupSublist)->getLength();
57 
58         if (aLookupSublist == m_aLists.end())
59         {
60             OSL_ENSURE(sal_False, "OAttribListMerger::seekToIndex: invalid index!");
61             return sal_False;
62         }
63         _rSubList = *aLookupSublist;
64         _rLocalIndex = nLeftOver;
65         return sal_True;
66     }
67 
68     //---------------------------------------------------------------------
seekToName(const::rtl::OUString & _rName,Reference<sax::XAttributeList> & _rSubList,sal_Int16 & _rLocalIndex)69     sal_Bool OAttribListMerger::seekToName(const ::rtl::OUString& _rName, Reference< sax::XAttributeList >& _rSubList, sal_Int16& _rLocalIndex)
70     {
71         for (   ConstAttributeListArrayIterator aLookupSublist = m_aLists.begin();
72                 aLookupSublist != m_aLists.end();
73                 ++aLookupSublist
74             )
75             for (sal_Int16 i=0; i<(*aLookupSublist)->getLength(); ++i)
76                 if ((*aLookupSublist)->getNameByIndex(i) == _rName)
77                 {
78                     _rSubList = *aLookupSublist;
79                     _rLocalIndex = i;
80                     return sal_True;
81                 }
82 
83         OSL_ENSURE(sal_False, "OAttribListMerger::seekToName: did not find the name!");
84         return sal_False;
85     }
86 
87     //---------------------------------------------------------------------
getLength()88     sal_Int16 SAL_CALL OAttribListMerger::getLength(  ) throw(RuntimeException)
89     {
90         sal_Int16 nCount = 0;
91         for (   ConstAttributeListArrayIterator aAccumulate = m_aLists.begin();
92                 aAccumulate != m_aLists.end();
93                 ++aAccumulate
94             )
95             nCount = nCount + (*aAccumulate)->getLength();
96         return nCount;
97     }
98 
99     //---------------------------------------------------------------------
getNameByIndex(sal_Int16 i)100     ::rtl::OUString SAL_CALL OAttribListMerger::getNameByIndex( sal_Int16 i ) throw(RuntimeException)
101     {
102         Reference< sax::XAttributeList > xSubList;
103         sal_Int16 nLocalIndex;
104 
105         if (!seekToIndex(i, xSubList, nLocalIndex))
106             return ::rtl::OUString();
107 
108         return xSubList->getNameByIndex(nLocalIndex);
109     }
110 
111     //---------------------------------------------------------------------
getTypeByIndex(sal_Int16 i)112     ::rtl::OUString SAL_CALL OAttribListMerger::getTypeByIndex( sal_Int16 i ) throw(RuntimeException)
113     {
114         Reference< sax::XAttributeList > xSubList;
115         sal_Int16 nLocalIndex;
116 
117         if (!seekToIndex(i, xSubList, nLocalIndex))
118             return ::rtl::OUString();
119 
120         return xSubList->getTypeByIndex(nLocalIndex);
121     }
122 
123     //---------------------------------------------------------------------
getTypeByName(const::rtl::OUString & _rName)124     ::rtl::OUString SAL_CALL OAttribListMerger::getTypeByName( const ::rtl::OUString& _rName ) throw(RuntimeException)
125     {
126         Reference< sax::XAttributeList > xSubList;
127         sal_Int16 nLocalIndex;
128 
129         if (!seekToName(_rName, xSubList, nLocalIndex))
130             return ::rtl::OUString();
131 
132         // though we're in getTypeByName here, we reroute this to the getTypeByIndex of the sub list,
133         // assuming that this is faster
134         return xSubList->getTypeByIndex(nLocalIndex);
135     }
136 
137     //---------------------------------------------------------------------
getValueByIndex(sal_Int16 i)138     ::rtl::OUString SAL_CALL OAttribListMerger::getValueByIndex( sal_Int16 i ) throw(RuntimeException)
139     {
140         Reference< sax::XAttributeList > xSubList;
141         sal_Int16 nLocalIndex;
142 
143         if (!seekToIndex(i, xSubList, nLocalIndex))
144             return ::rtl::OUString();
145 
146         return xSubList->getValueByIndex(nLocalIndex);
147     }
148 
149     //---------------------------------------------------------------------
getValueByName(const::rtl::OUString & _rName)150     ::rtl::OUString SAL_CALL OAttribListMerger::getValueByName( const ::rtl::OUString& _rName ) throw(RuntimeException)
151     {
152         Reference< sax::XAttributeList > xSubList;
153         sal_Int16 nLocalIndex;
154 
155         if (!seekToName(_rName, xSubList, nLocalIndex))
156             return ::rtl::OUString();
157 
158         // though we're in getValueByName here, we reroute this to the getValueByIndex of the sub list,
159         // assuming that this is faster
160         return xSubList->getValueByIndex(nLocalIndex);
161     }
162 
163 //.........................................................................
164 }   // namespace xmloff
165 //.........................................................................
166 
167