1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmloff.hxx"
30 #include "attriblistmerge.hxx"
31 
32 //.........................................................................
33 namespace xmloff
34 {
35 //.........................................................................
36 
37 	using namespace ::com::sun::star::uno;
38 	using namespace ::com::sun::star::xml;
39 
40 	//=====================================================================
41 	//= OAttribListMerger
42 	//=====================================================================
43 	//---------------------------------------------------------------------
44 	void OAttribListMerger::addList(const Reference< sax::XAttributeList >& _rxList)
45 	{
46 		OSL_ENSURE(_rxList.is(), "OAttribListMerger::addList: invalid list!");
47 		if (_rxList.is())
48 			m_aLists.push_back(_rxList);
49 	}
50 
51 	//---------------------------------------------------------------------
52 	sal_Bool OAttribListMerger::seekToIndex(sal_Int16 _nGlobalIndex, Reference< sax::XAttributeList >& _rSubList, sal_Int16& _rLocalIndex)
53 	{
54 		sal_Int16 nLeftOver = _nGlobalIndex;
55 		ConstAttributeListArrayIterator aLookupSublist = m_aLists.begin();
56 
57 		for ( ; (aLookupSublist != m_aLists.end()) && (nLeftOver >= (*aLookupSublist)->getLength());
58 				++aLookupSublist
59 			)
60 			nLeftOver = nLeftOver - (*aLookupSublist)->getLength();
61 
62 		if (aLookupSublist == m_aLists.end())
63 		{
64 			OSL_ENSURE(sal_False, "OAttribListMerger::seekToIndex: invalid index!");
65 			return sal_False;
66 		}
67 		_rSubList = *aLookupSublist;
68 		_rLocalIndex = nLeftOver;
69 		return sal_True;
70 	}
71 
72 	//---------------------------------------------------------------------
73 	sal_Bool OAttribListMerger::seekToName(const ::rtl::OUString& _rName, Reference< sax::XAttributeList >& _rSubList, sal_Int16& _rLocalIndex)
74 	{
75 		for (	ConstAttributeListArrayIterator aLookupSublist = m_aLists.begin();
76 				aLookupSublist != m_aLists.end();
77 				++aLookupSublist
78 			)
79 			for	(sal_Int16 i=0; i<(*aLookupSublist)->getLength(); ++i)
80 				if ((*aLookupSublist)->getNameByIndex(i) == _rName)
81 				{
82 					_rSubList = *aLookupSublist;
83 					_rLocalIndex = i;
84 					return sal_True;
85 				}
86 
87 		OSL_ENSURE(sal_False, "OAttribListMerger::seekToName: did not find the name!");
88 		return sal_False;
89 	}
90 
91 	//---------------------------------------------------------------------
92 	sal_Int16 SAL_CALL OAttribListMerger::getLength(  ) throw(RuntimeException)
93 	{
94 		sal_Int16 nCount = 0;
95 		for (	ConstAttributeListArrayIterator aAccumulate = m_aLists.begin();
96 				aAccumulate != m_aLists.end();
97 				++aAccumulate
98 			)
99 			nCount = nCount + (*aAccumulate)->getLength();
100 		return nCount;
101 	}
102 
103 	//---------------------------------------------------------------------
104 	::rtl::OUString SAL_CALL OAttribListMerger::getNameByIndex( sal_Int16 i ) throw(RuntimeException)
105 	{
106 		Reference< sax::XAttributeList > xSubList;
107 		sal_Int16 nLocalIndex;
108 
109 		if (!seekToIndex(i, xSubList, nLocalIndex))
110 			return ::rtl::OUString();
111 
112 		return xSubList->getNameByIndex(nLocalIndex);
113 	}
114 
115 	//---------------------------------------------------------------------
116 	::rtl::OUString SAL_CALL OAttribListMerger::getTypeByIndex( sal_Int16 i ) throw(RuntimeException)
117 	{
118 		Reference< sax::XAttributeList > xSubList;
119 		sal_Int16 nLocalIndex;
120 
121 		if (!seekToIndex(i, xSubList, nLocalIndex))
122 			return ::rtl::OUString();
123 
124 		return xSubList->getTypeByIndex(nLocalIndex);
125 	}
126 
127 	//---------------------------------------------------------------------
128 	::rtl::OUString SAL_CALL OAttribListMerger::getTypeByName( const ::rtl::OUString& _rName ) throw(RuntimeException)
129 	{
130 		Reference< sax::XAttributeList > xSubList;
131 		sal_Int16 nLocalIndex;
132 
133 		if (!seekToName(_rName, xSubList, nLocalIndex))
134 			return ::rtl::OUString();
135 
136 		// though we're in getTypeByName here, we reroute this to the getTypeByIndex of the sub list,
137 		// assuming that this is faster
138 		return xSubList->getTypeByIndex(nLocalIndex);
139 	}
140 
141 	//---------------------------------------------------------------------
142 	::rtl::OUString SAL_CALL OAttribListMerger::getValueByIndex( sal_Int16 i ) throw(RuntimeException)
143 	{
144 		Reference< sax::XAttributeList > xSubList;
145 		sal_Int16 nLocalIndex;
146 
147 		if (!seekToIndex(i, xSubList, nLocalIndex))
148 			return ::rtl::OUString();
149 
150 		return xSubList->getValueByIndex(nLocalIndex);
151 	}
152 
153 	//---------------------------------------------------------------------
154 	::rtl::OUString SAL_CALL OAttribListMerger::getValueByName( const ::rtl::OUString& _rName ) throw(RuntimeException)
155 	{
156 		Reference< sax::XAttributeList > xSubList;
157 		sal_Int16 nLocalIndex;
158 
159 		if (!seekToName(_rName, xSubList, nLocalIndex))
160 			return ::rtl::OUString();
161 
162 		// though we're in getValueByName here, we reroute this to the getValueByIndex of the sub list,
163 		// assuming that this is faster
164 		return xSubList->getValueByIndex(nLocalIndex);
165 	}
166 
167 //.........................................................................
168 }	// namespace xmloff
169 //.........................................................................
170 
171