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