Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include <string.h>
22 : #include <vector>
23 : #include <osl/mutex.hxx>
24 : #include <xmloff/xmltoken.hxx>
25 : #include <comphelper/servicehelper.hxx>
26 :
27 : #include <xmloff/attrlist.hxx>
28 :
29 : using ::rtl::OUString;
30 :
31 : using namespace ::osl;
32 : using namespace ::com::sun::star;
33 : using namespace ::xmloff::token;
34 :
35 179160 : struct SvXMLTagAttribute_Impl
36 : {
37 : SvXMLTagAttribute_Impl(){}
38 89360 : SvXMLTagAttribute_Impl( const OUString &rName,
39 : const OUString &rValue )
40 : : sName(rName),
41 89360 : sValue(rValue)
42 : {
43 89360 : }
44 :
45 89800 : SvXMLTagAttribute_Impl( const SvXMLTagAttribute_Impl& r ) :
46 : sName(r.sName),
47 89800 : sValue(r.sValue)
48 : {
49 89800 : }
50 :
51 : OUString sName;
52 : OUString sValue;
53 : };
54 :
55 1270 : struct SvXMLAttributeList_Impl
56 : {
57 1270 : SvXMLAttributeList_Impl()
58 1270 : {
59 : // performance improvement during adding
60 1270 : vecAttribute.reserve(20);
61 1270 : }
62 :
63 0 : SvXMLAttributeList_Impl( const SvXMLAttributeList_Impl& r ) :
64 0 : vecAttribute( r.vecAttribute )
65 : {
66 0 : }
67 :
68 : ::std::vector<struct SvXMLTagAttribute_Impl> vecAttribute;
69 : typedef ::std::vector<struct SvXMLTagAttribute_Impl>::size_type size_type;
70 : };
71 :
72 :
73 :
74 52590 : sal_Int16 SAL_CALL SvXMLAttributeList::getLength(void) throw( ::com::sun::star::uno::RuntimeException )
75 : {
76 52590 : return sal::static_int_cast< sal_Int16 >(m_pImpl->vecAttribute.size());
77 : }
78 :
79 :
80 0 : SvXMLAttributeList::SvXMLAttributeList( const SvXMLAttributeList &r ) :
81 : cppu::WeakImplHelper3<com::sun::star::xml::sax::XAttributeList, com::sun::star::util::XCloneable, com::sun::star::lang::XUnoTunnel>(r),
82 0 : m_pImpl( new SvXMLAttributeList_Impl( *r.m_pImpl ) )
83 : {
84 0 : }
85 :
86 90 : SvXMLAttributeList::SvXMLAttributeList( const uno::Reference<
87 : xml::sax::XAttributeList> & rAttrList )
88 90 : : sType( GetXMLToken(XML_CDATA) )
89 : {
90 90 : m_pImpl = new SvXMLAttributeList_Impl;
91 :
92 : SvXMLAttributeList* pImpl =
93 90 : SvXMLAttributeList::getImplementation( rAttrList );
94 :
95 90 : if( pImpl )
96 0 : *m_pImpl = *(pImpl->m_pImpl);
97 : else
98 90 : AppendAttributeList( rAttrList );
99 90 : }
100 :
101 82172 : OUString SAL_CALL SvXMLAttributeList::getNameByIndex(sal_Int16 i) throw( ::com::sun::star::uno::RuntimeException )
102 : {
103 82172 : return ( static_cast< SvXMLAttributeList_Impl::size_type >( i ) < m_pImpl->vecAttribute.size() ) ? m_pImpl->vecAttribute[i].sName : OUString();
104 : }
105 :
106 :
107 0 : OUString SAL_CALL SvXMLAttributeList::getTypeByIndex(sal_Int16) throw( ::com::sun::star::uno::RuntimeException )
108 : {
109 0 : return sType;
110 : }
111 :
112 82122 : OUString SAL_CALL SvXMLAttributeList::getValueByIndex(sal_Int16 i) throw( ::com::sun::star::uno::RuntimeException )
113 : {
114 82122 : return ( static_cast< SvXMLAttributeList_Impl::size_type >( i ) < m_pImpl->vecAttribute.size() ) ? m_pImpl->vecAttribute[i].sValue : OUString();
115 : }
116 :
117 0 : OUString SAL_CALL SvXMLAttributeList::getTypeByName( const OUString& ) throw( ::com::sun::star::uno::RuntimeException )
118 : {
119 0 : return sType;
120 : }
121 :
122 12 : OUString SAL_CALL SvXMLAttributeList::getValueByName(const OUString& sName) throw( ::com::sun::star::uno::RuntimeException )
123 : {
124 12 : ::std::vector<struct SvXMLTagAttribute_Impl>::iterator ii = m_pImpl->vecAttribute.begin();
125 :
126 72 : for( ; ii != m_pImpl->vecAttribute.end() ; ++ii ) {
127 72 : if( (*ii).sName == sName ) {
128 12 : return (*ii).sValue;
129 : }
130 : }
131 0 : return OUString();
132 : }
133 :
134 :
135 0 : uno::Reference< ::com::sun::star::util::XCloneable > SvXMLAttributeList::createClone() throw( ::com::sun::star::uno::RuntimeException )
136 : {
137 0 : uno::Reference< ::com::sun::star::util::XCloneable > r = new SvXMLAttributeList( *this );
138 0 : return r;
139 : }
140 :
141 :
142 1180 : SvXMLAttributeList::SvXMLAttributeList()
143 1180 : : sType( GetXMLToken(XML_CDATA) )
144 : {
145 1180 : m_pImpl = new SvXMLAttributeList_Impl;
146 1180 : }
147 :
148 :
149 :
150 3810 : SvXMLAttributeList::~SvXMLAttributeList()
151 : {
152 1270 : delete m_pImpl;
153 2540 : }
154 :
155 :
156 88678 : void SvXMLAttributeList::AddAttribute( const OUString &sName ,
157 : const OUString &sValue )
158 : {
159 88678 : m_pImpl->vecAttribute.push_back( SvXMLTagAttribute_Impl( sName , sValue ) );
160 88678 : }
161 :
162 57578 : void SvXMLAttributeList::Clear()
163 : {
164 57578 : m_pImpl->vecAttribute.clear();
165 :
166 : OSL_ASSERT( ! getLength() );
167 57578 : }
168 :
169 4 : void SvXMLAttributeList::RemoveAttribute( const OUString sName )
170 : {
171 4 : ::std::vector<struct SvXMLTagAttribute_Impl>::iterator ii = m_pImpl->vecAttribute.begin();
172 :
173 16 : for( ; ii != m_pImpl->vecAttribute.end() ; ++ii ) {
174 16 : if( (*ii).sName == sName ) {
175 4 : m_pImpl->vecAttribute.erase( ii );
176 4 : break;
177 : }
178 : }
179 4 : }
180 :
181 162 : void SvXMLAttributeList::AppendAttributeList( const uno::Reference< ::com::sun::star::xml::sax::XAttributeList > &r )
182 : {
183 : OSL_ASSERT( r.is() );
184 :
185 162 : sal_Int16 nMax = r->getLength();
186 : SvXMLAttributeList_Impl::size_type nTotalSize =
187 162 : m_pImpl->vecAttribute.size() + nMax;
188 162 : m_pImpl->vecAttribute.reserve( nTotalSize );
189 :
190 844 : for( sal_Int16 i = 0 ; i < nMax ; ++i ) {
191 : m_pImpl->vecAttribute.push_back( SvXMLTagAttribute_Impl(
192 682 : r->getNameByIndex( i ) ,
193 1364 : r->getValueByIndex( i )));
194 : }
195 :
196 : OSL_ASSERT( nTotalSize == (SvXMLAttributeList_Impl::size_type)getLength());
197 162 : }
198 :
199 12 : void SvXMLAttributeList::SetValueByIndex( sal_Int16 i,
200 : const ::rtl::OUString& rValue )
201 : {
202 24 : if( static_cast< SvXMLAttributeList_Impl::size_type >( i )
203 12 : < m_pImpl->vecAttribute.size() )
204 : {
205 12 : m_pImpl->vecAttribute[i].sValue = rValue;
206 : }
207 12 : }
208 :
209 0 : void SvXMLAttributeList::RemoveAttributeByIndex( sal_Int16 i )
210 : {
211 0 : if( static_cast< SvXMLAttributeList_Impl::size_type >( i )
212 0 : < m_pImpl->vecAttribute.size() )
213 0 : m_pImpl->vecAttribute.erase( m_pImpl->vecAttribute.begin() + i );
214 0 : }
215 :
216 0 : void SvXMLAttributeList::RenameAttributeByIndex( sal_Int16 i,
217 : const OUString& rNewName )
218 : {
219 0 : if( static_cast< SvXMLAttributeList_Impl::size_type >( i )
220 0 : < m_pImpl->vecAttribute.size() )
221 : {
222 0 : m_pImpl->vecAttribute[i].sName = rNewName;
223 : }
224 0 : }
225 :
226 0 : sal_Int16 SvXMLAttributeList::GetIndexByName( const OUString& rName ) const
227 : {
228 : ::std::vector<struct SvXMLTagAttribute_Impl>::iterator ii =
229 0 : m_pImpl->vecAttribute.begin();
230 :
231 0 : for( sal_Int16 nIndex=0; ii!=m_pImpl->vecAttribute.end(); ++ii, ++nIndex )
232 : {
233 0 : if( (*ii).sName == rName )
234 : {
235 0 : return nIndex;
236 : }
237 : }
238 0 : return -1;
239 : }
240 :
241 : namespace
242 : {
243 : class theSvXMLAttributeListUnoTunnelId : public rtl::Static< UnoTunnelIdInit, theSvXMLAttributeListUnoTunnelId> {};
244 : }
245 :
246 : // XUnoTunnel & co
247 2 : const uno::Sequence< sal_Int8 > & SvXMLAttributeList::getUnoTunnelId() throw()
248 : {
249 2 : return theSvXMLAttributeListUnoTunnelId::get().getSeq();
250 : }
251 :
252 90 : SvXMLAttributeList* SvXMLAttributeList::getImplementation( uno::Reference< uno::XInterface > xInt ) throw()
253 : {
254 90 : uno::Reference< lang::XUnoTunnel > xUT( xInt, uno::UNO_QUERY );
255 90 : if( xUT.is() )
256 : {
257 : return
258 : reinterpret_cast<SvXMLAttributeList*>(
259 : sal::static_int_cast<sal_IntPtr>(
260 2 : xUT->getSomething( SvXMLAttributeList::getUnoTunnelId())));
261 : }
262 : else
263 88 : return NULL;
264 : }
265 :
266 : // XUnoTunnel
267 0 : sal_Int64 SAL_CALL SvXMLAttributeList::getSomething( const uno::Sequence< sal_Int8 >& rId )
268 : throw( uno::RuntimeException )
269 : {
270 0 : if( rId.getLength() == 16 && 0 == memcmp( getUnoTunnelId().getConstArray(),
271 0 : rId.getConstArray(), 16 ) )
272 : {
273 0 : return sal::static_int_cast<sal_Int64>(reinterpret_cast<sal_uIntPtr>(this));
274 : }
275 0 : return 0;
276 : }
277 :
278 :
279 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|