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 :
30 : using namespace ::osl;
31 : using namespace ::com::sun::star;
32 : using namespace ::xmloff::token;
33 :
34 1224858 : struct SvXMLTagAttribute_Impl
35 : {
36 590002 : SvXMLTagAttribute_Impl( const OUString &rName,
37 : const OUString &rValue )
38 : : sName(rName),
39 590002 : sValue(rValue)
40 : {
41 590002 : }
42 :
43 633834 : SvXMLTagAttribute_Impl( const SvXMLTagAttribute_Impl& r ) :
44 : sName(r.sName),
45 633834 : sValue(r.sValue)
46 : {
47 633834 : }
48 :
49 : OUString sName;
50 : OUString sValue;
51 : };
52 :
53 65225 : struct SvXMLAttributeList_Impl
54 : {
55 63271 : SvXMLAttributeList_Impl()
56 63271 : {
57 : // performance improvement during adding
58 63271 : vecAttribute.reserve(20);
59 63271 : }
60 :
61 786 : SvXMLAttributeList_Impl( const SvXMLAttributeList_Impl& r ) :
62 786 : vecAttribute( r.vecAttribute )
63 : {
64 786 : }
65 :
66 : ::std::vector<struct SvXMLTagAttribute_Impl> vecAttribute;
67 : typedef ::std::vector<struct SvXMLTagAttribute_Impl>::size_type size_type;
68 : };
69 :
70 :
71 :
72 357664 : sal_Int16 SAL_CALL SvXMLAttributeList::getLength(void) throw( ::com::sun::star::uno::RuntimeException, std::exception )
73 : {
74 357664 : return sal::static_int_cast< sal_Int16 >(m_pImpl->vecAttribute.size());
75 : }
76 :
77 :
78 786 : SvXMLAttributeList::SvXMLAttributeList( const SvXMLAttributeList &r ) :
79 : cppu::WeakImplHelper3<com::sun::star::xml::sax::XAttributeList, com::sun::star::util::XCloneable, com::sun::star::lang::XUnoTunnel>(r),
80 786 : m_pImpl( new SvXMLAttributeList_Impl( *r.m_pImpl ) )
81 : {
82 786 : }
83 :
84 4445 : SvXMLAttributeList::SvXMLAttributeList( const uno::Reference<
85 : xml::sax::XAttributeList> & rAttrList )
86 4445 : : sType( GetXMLToken(XML_CDATA) )
87 : {
88 4445 : m_pImpl = new SvXMLAttributeList_Impl;
89 :
90 : SvXMLAttributeList* pImpl =
91 4445 : SvXMLAttributeList::getImplementation( rAttrList );
92 :
93 4445 : if( pImpl )
94 1168 : *m_pImpl = *(pImpl->m_pImpl);
95 : else
96 3277 : AppendAttributeList( rAttrList );
97 4445 : }
98 :
99 753700 : OUString SAL_CALL SvXMLAttributeList::getNameByIndex(sal_Int16 i) throw( ::com::sun::star::uno::RuntimeException, std::exception )
100 : {
101 753700 : return ( static_cast< SvXMLAttributeList_Impl::size_type >( i ) < m_pImpl->vecAttribute.size() ) ? m_pImpl->vecAttribute[i].sName : OUString();
102 : }
103 :
104 :
105 2084 : OUString SAL_CALL SvXMLAttributeList::getTypeByIndex(sal_Int16) throw( ::com::sun::star::uno::RuntimeException, std::exception )
106 : {
107 2084 : return sType;
108 : }
109 :
110 612855 : OUString SAL_CALL SvXMLAttributeList::getValueByIndex(sal_Int16 i) throw( ::com::sun::star::uno::RuntimeException, std::exception )
111 : {
112 612855 : return ( static_cast< SvXMLAttributeList_Impl::size_type >( i ) < m_pImpl->vecAttribute.size() ) ? m_pImpl->vecAttribute[i].sValue : OUString();
113 : }
114 :
115 0 : OUString SAL_CALL SvXMLAttributeList::getTypeByName( const OUString& ) throw( ::com::sun::star::uno::RuntimeException, std::exception )
116 : {
117 0 : return sType;
118 : }
119 :
120 8690 : OUString SAL_CALL SvXMLAttributeList::getValueByName(const OUString& sName) throw( ::com::sun::star::uno::RuntimeException, std::exception )
121 : {
122 8690 : ::std::vector<struct SvXMLTagAttribute_Impl>::iterator ii = m_pImpl->vecAttribute.begin();
123 :
124 42012 : for( ; ii != m_pImpl->vecAttribute.end() ; ++ii ) {
125 37626 : if( (*ii).sName == sName ) {
126 4304 : return (*ii).sValue;
127 : }
128 : }
129 4386 : return OUString();
130 : }
131 :
132 :
133 786 : uno::Reference< ::com::sun::star::util::XCloneable > SvXMLAttributeList::createClone() throw( ::com::sun::star::uno::RuntimeException, std::exception )
134 : {
135 786 : uno::Reference< ::com::sun::star::util::XCloneable > r = new SvXMLAttributeList( *this );
136 786 : return r;
137 : }
138 :
139 :
140 58826 : SvXMLAttributeList::SvXMLAttributeList()
141 58826 : : sType( GetXMLToken(XML_CDATA) )
142 : {
143 58826 : m_pImpl = new SvXMLAttributeList_Impl;
144 58826 : }
145 :
146 :
147 :
148 192171 : SvXMLAttributeList::~SvXMLAttributeList()
149 : {
150 64057 : delete m_pImpl;
151 128114 : }
152 :
153 :
154 562082 : void SvXMLAttributeList::AddAttribute( const OUString &sName ,
155 : const OUString &sValue )
156 : {
157 562082 : m_pImpl->vecAttribute.push_back( SvXMLTagAttribute_Impl( sName , sValue ) );
158 562082 : }
159 :
160 214420 : void SvXMLAttributeList::Clear()
161 : {
162 214420 : m_pImpl->vecAttribute.clear();
163 :
164 : OSL_ASSERT( ! getLength() );
165 214420 : }
166 :
167 6962 : void SvXMLAttributeList::RemoveAttribute( const OUString& sName )
168 : {
169 6962 : ::std::vector<struct SvXMLTagAttribute_Impl>::iterator ii = m_pImpl->vecAttribute.begin();
170 :
171 23134 : for( ; ii != m_pImpl->vecAttribute.end() ; ++ii ) {
172 19062 : if( (*ii).sName == sName ) {
173 2890 : m_pImpl->vecAttribute.erase( ii );
174 2890 : break;
175 : }
176 : }
177 6962 : }
178 :
179 4835 : void SvXMLAttributeList::AppendAttributeList( const uno::Reference< ::com::sun::star::xml::sax::XAttributeList > &r )
180 : {
181 : OSL_ASSERT( r.is() );
182 :
183 4835 : sal_Int16 nMax = r->getLength();
184 : SvXMLAttributeList_Impl::size_type nTotalSize =
185 4835 : m_pImpl->vecAttribute.size() + nMax;
186 4835 : m_pImpl->vecAttribute.reserve( nTotalSize );
187 :
188 32755 : for( sal_Int16 i = 0 ; i < nMax ; ++i ) {
189 : m_pImpl->vecAttribute.push_back( SvXMLTagAttribute_Impl(
190 27920 : r->getNameByIndex( i ) ,
191 55840 : r->getValueByIndex( i )));
192 : }
193 :
194 : OSL_ASSERT( nTotalSize == (SvXMLAttributeList_Impl::size_type)getLength());
195 4835 : }
196 :
197 2796 : void SvXMLAttributeList::SetValueByIndex( sal_Int16 i,
198 : const OUString& rValue )
199 : {
200 2796 : if( static_cast< SvXMLAttributeList_Impl::size_type >( i )
201 2796 : < m_pImpl->vecAttribute.size() )
202 : {
203 2796 : m_pImpl->vecAttribute[i].sValue = rValue;
204 : }
205 2796 : }
206 :
207 134 : void SvXMLAttributeList::RemoveAttributeByIndex( sal_Int16 i )
208 : {
209 134 : if( static_cast< SvXMLAttributeList_Impl::size_type >( i )
210 134 : < m_pImpl->vecAttribute.size() )
211 134 : m_pImpl->vecAttribute.erase( m_pImpl->vecAttribute.begin() + i );
212 134 : }
213 :
214 646 : void SvXMLAttributeList::RenameAttributeByIndex( sal_Int16 i,
215 : const OUString& rNewName )
216 : {
217 646 : if( static_cast< SvXMLAttributeList_Impl::size_type >( i )
218 646 : < m_pImpl->vecAttribute.size() )
219 : {
220 646 : m_pImpl->vecAttribute[i].sName = rNewName;
221 : }
222 646 : }
223 :
224 0 : sal_Int16 SvXMLAttributeList::GetIndexByName( const OUString& rName ) const
225 : {
226 : ::std::vector<struct SvXMLTagAttribute_Impl>::iterator ii =
227 0 : m_pImpl->vecAttribute.begin();
228 :
229 0 : for( sal_Int16 nIndex=0; ii!=m_pImpl->vecAttribute.end(); ++ii, ++nIndex )
230 : {
231 0 : if( (*ii).sName == rName )
232 : {
233 0 : return nIndex;
234 : }
235 : }
236 0 : return -1;
237 : }
238 :
239 : namespace
240 : {
241 : class theSvXMLAttributeListUnoTunnelId : public rtl::Static< UnoTunnelIdInit, theSvXMLAttributeListUnoTunnelId> {};
242 : }
243 :
244 : // XUnoTunnel & co
245 2490 : const uno::Sequence< sal_Int8 > & SvXMLAttributeList::getUnoTunnelId() throw()
246 : {
247 2490 : return theSvXMLAttributeListUnoTunnelId::get().getSeq();
248 : }
249 :
250 4445 : SvXMLAttributeList* SvXMLAttributeList::getImplementation( uno::Reference< uno::XInterface > xInt ) throw()
251 : {
252 4445 : uno::Reference< lang::XUnoTunnel > xUT( xInt, uno::UNO_QUERY );
253 4445 : if( xUT.is() )
254 : {
255 : return
256 : reinterpret_cast<SvXMLAttributeList*>(
257 : sal::static_int_cast<sal_IntPtr>(
258 1322 : xUT->getSomething( SvXMLAttributeList::getUnoTunnelId())));
259 : }
260 : else
261 3123 : return NULL;
262 : }
263 :
264 : // XUnoTunnel
265 1168 : sal_Int64 SAL_CALL SvXMLAttributeList::getSomething( const uno::Sequence< sal_Int8 >& rId )
266 : throw( uno::RuntimeException, std::exception )
267 : {
268 3504 : if( rId.getLength() == 16 && 0 == memcmp( getUnoTunnelId().getConstArray(),
269 2336 : rId.getConstArray(), 16 ) )
270 : {
271 1168 : return sal::static_int_cast<sal_Int64>(reinterpret_cast<sal_uIntPtr>(this));
272 : }
273 0 : return 0;
274 : }
275 :
276 :
277 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|