Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #include <string.h>
30 : : #include <com/sun/star/xml/AttributeData.hpp>
31 : : #include <rtl/ustrbuf.hxx>
32 : : #include <comphelper/servicehelper.hxx>
33 : : #include <limits.h>
34 : :
35 : : #include <xmloff/xmlcnimp.hxx>
36 : :
37 : : #include "xmloff/unoatrcn.hxx"
38 : :
39 : : using ::rtl::OUString;
40 : : using ::rtl::OUStringBuffer;
41 : :
42 : : using namespace ::com::sun::star;
43 : :
44 : : // --------------------------------------------------------------------
45 : : // Interface implementation
46 : : // --------------------------------------------------------------------
47 : :
48 : : #define IMPL ((AttrContainerImpl*)mpData)
49 : :
50 : 1 : uno::Reference< uno::XInterface > SvUnoAttributeContainer_CreateInstance()
51 : : {
52 [ + - ]: 1 : return *(new SvUnoAttributeContainer);
53 : : }
54 : :
55 : 490 : SvUnoAttributeContainer::SvUnoAttributeContainer( SvXMLAttrContainerData* pContainer)
56 : 490 : : mpContainer( pContainer )
57 : : {
58 [ + + ]: 490 : if( mpContainer == NULL )
59 [ + - ][ + - ]: 1 : mpContainer = new SvXMLAttrContainerData;
60 : 490 : }
61 : :
62 : 490 : SvUnoAttributeContainer::~SvUnoAttributeContainer()
63 : : {
64 [ + - ][ + - ]: 490 : delete mpContainer;
65 [ - + ]: 980 : }
66 : :
67 : : // container::XElementAccess
68 : 0 : uno::Type SAL_CALL SvUnoAttributeContainer::getElementType(void)
69 : : throw( uno::RuntimeException )
70 : : {
71 : 0 : return ::getCppuType((const xml::AttributeData*)0);
72 : : }
73 : :
74 : 0 : sal_Bool SAL_CALL SvUnoAttributeContainer::hasElements(void)
75 : : throw( uno::RuntimeException )
76 : : {
77 : 0 : return mpContainer->GetAttrCount() != 0;
78 : : }
79 : :
80 : 31 : sal_uInt16 SvUnoAttributeContainer::getIndexByName(const OUString& aName ) const
81 : : {
82 : 31 : const sal_uInt16 nAttrCount = mpContainer->GetAttrCount();
83 : :
84 : 31 : sal_Int32 nPos = aName.indexOf( sal_Unicode(':') );
85 [ - + ]: 31 : if( nPos == -1L )
86 : : {
87 [ # # ]: 0 : for( sal_uInt16 nAttr = 0; nAttr < nAttrCount; nAttr++ )
88 : : {
89 [ # # ]: 0 : if( mpContainer->GetAttrLName(nAttr) == aName &&
[ # # # # ]
[ # # ]
90 [ # # ][ # # ]: 0 : mpContainer->GetAttrPrefix(nAttr).isEmpty() )
[ # # ]
91 : 0 : return nAttr;
92 : : }
93 : : }
94 : : else
95 : : {
96 : 31 : const OUString aPrefix( aName.copy( 0L, nPos ) );
97 : 31 : const OUString aLName( aName.copy( nPos+1L ) );
98 : :
99 [ + + ]: 31 : for( sal_uInt16 nAttr = 0; nAttr < nAttrCount; nAttr++ )
100 : : {
101 [ + - ]: 45 : if( mpContainer->GetAttrLName(nAttr) == aLName &&
[ + - + - ]
[ + - ]
102 [ + - ][ + - ]: 30 : mpContainer->GetAttrPrefix(nAttr) == aPrefix )
[ # # ]
103 : 15 : return nAttr;
104 [ + + ][ + + ]: 31 : }
105 : : }
106 : :
107 : 31 : return USHRT_MAX;
108 : : }
109 : :
110 : : namespace
111 : : {
112 : : class theSvUnoAttributeContainerUnoTunnelId : public rtl::Static< UnoTunnelIdInit, theSvUnoAttributeContainerUnoTunnelId> {};
113 : : }
114 : :
115 : 40 : const ::com::sun::star::uno::Sequence< sal_Int8 > & SvUnoAttributeContainer::getUnoTunnelId() throw()
116 : : {
117 : 40 : return theSvUnoAttributeContainerUnoTunnelId::get().getSeq();
118 : : }
119 : :
120 : 20 : sal_Int64 SAL_CALL SvUnoAttributeContainer::getSomething( const ::com::sun::star::uno::Sequence< sal_Int8 >& rId ) throw(::com::sun::star::uno::RuntimeException)
121 : : {
122 [ + - + - ]: 40 : if( rId.getLength() == 16 && 0 == memcmp( getUnoTunnelId().getConstArray(),
[ + - ]
123 : 20 : rId.getConstArray(), 16 ) )
124 : : {
125 : 20 : return sal::static_int_cast<sal_Int64>(reinterpret_cast<sal_uIntPtr>(this));
126 : : }
127 : 20 : return 0;
128 : : }
129 : :
130 : : // container::XNameAccess
131 : 15 : uno::Any SAL_CALL SvUnoAttributeContainer::getByName(const OUString& aName)
132 : : throw( container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException )
133 : : {
134 [ + - ]: 15 : sal_uInt16 nAttr = getIndexByName(aName );
135 : :
136 [ - + ]: 15 : if( nAttr == USHRT_MAX )
137 [ # # ]: 0 : throw container::NoSuchElementException();
138 : :
139 : 15 : xml::AttributeData aData;
140 [ + - ]: 15 : aData.Namespace = mpContainer->GetAttrNamespace(nAttr);
141 : 15 : aData.Type = OUString("CDATA");
142 [ + - ]: 15 : aData.Value = mpContainer->GetAttrValue(nAttr);
143 : :
144 : 15 : uno::Any aAny;
145 [ + - ]: 15 : aAny <<= aData;
146 : 15 : return aAny;
147 : : }
148 : :
149 : 78 : uno::Sequence< OUString > SAL_CALL SvUnoAttributeContainer::getElementNames(void) throw( uno::RuntimeException )
150 : : {
151 : 78 : const sal_uInt16 nAttrCount = mpContainer->GetAttrCount();
152 : :
153 : 78 : uno::Sequence< OUString > aElementNames( (sal_Int32)nAttrCount );
154 [ + - ]: 78 : OUString *pNames = aElementNames.getArray();
155 : :
156 [ + + ]: 108 : for( sal_uInt16 nAttr = 0; nAttr < nAttrCount; nAttr++ )
157 : : {
158 [ + - ][ + - ]: 30 : OUStringBuffer sBuffer( mpContainer->GetAttrPrefix(nAttr) );
159 [ + - ]: 30 : if( sBuffer.getLength() != 0L )
160 [ + - ]: 30 : sBuffer.append( (sal_Unicode)':' );
161 [ + - ][ + - ]: 30 : sBuffer.append( mpContainer->GetAttrLName(nAttr) );
162 [ + - ]: 30 : *pNames++ = sBuffer.makeStringAndClear();
163 : 30 : }
164 : :
165 : 78 : return aElementNames;
166 : : }
167 : :
168 : 0 : sal_Bool SAL_CALL SvUnoAttributeContainer::hasByName(const OUString& aName) throw( uno::RuntimeException )
169 : : {
170 : 0 : return getIndexByName(aName ) != USHRT_MAX;
171 : : }
172 : :
173 : : // container::XNameReplace
174 : 0 : void SAL_CALL SvUnoAttributeContainer::replaceByName(const OUString& aName, const uno::Any& aElement)
175 : : throw( lang::IllegalArgumentException, container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException )
176 : : {
177 [ # # ][ # # ]: 0 : if( aElement.hasValue() && aElement.getValueType() == ::getCppuType((const xml::AttributeData*)0) )
[ # # ]
178 : : {
179 : 0 : sal_uInt16 nAttr = getIndexByName(aName );
180 [ # # ]: 0 : if( nAttr == USHRT_MAX )
181 [ # # ]: 0 : throw container::NoSuchElementException();
182 : :
183 : 0 : xml::AttributeData* pData = (xml::AttributeData*)aElement.getValue();
184 : :
185 : 0 : sal_Int32 nPos = aName.indexOf( sal_Unicode(':') );
186 [ # # ]: 0 : if( nPos != -1L )
187 : : {
188 : 0 : const OUString aPrefix( aName.copy( 0L, nPos ));
189 : 0 : const OUString aLName( aName.copy( nPos+1L ));
190 : :
191 [ # # ]: 0 : if( pData->Namespace.isEmpty() )
192 : : {
193 [ # # ][ # # ]: 0 : if( mpContainer->SetAt( nAttr, aPrefix, aLName, pData->Value ) )
194 : : return;
195 : : }
196 : : else
197 : : {
198 [ # # ][ # # ]: 0 : if( mpContainer->SetAt( nAttr, aPrefix, pData->Namespace, aLName, pData->Value ) )
199 : : return;
200 [ # # ][ # # ]: 0 : }
201 : : }
202 : : else
203 : : {
204 [ # # ]: 0 : if( pData->Namespace.isEmpty() )
205 : : {
206 [ # # ]: 0 : if( mpContainer->SetAt( nAttr, aName, pData->Value ) )
207 : 0 : return;
208 : : }
209 : : }
210 : : }
211 : :
212 [ # # ]: 0 : throw lang::IllegalArgumentException();
213 : : }
214 : :
215 : : // container::XNameContainer
216 : 16 : void SAL_CALL SvUnoAttributeContainer::insertByName(const OUString& aName, const uno::Any& aElement)
217 : : throw( lang::IllegalArgumentException, container::ElementExistException, lang::WrappedTargetException, uno::RuntimeException )
218 : : {
219 [ + - ][ - + ]: 16 : if( !aElement.hasValue() || aElement.getValueType() != ::getCppuType((const xml::AttributeData*)0) )
[ - + ]
220 [ # # ]: 0 : throw lang::IllegalArgumentException();
221 : :
222 : 16 : sal_uInt16 nAttr = getIndexByName(aName );
223 [ - + ]: 16 : if( nAttr != USHRT_MAX )
224 [ # # ]: 0 : throw container::ElementExistException();
225 : :
226 : 16 : xml::AttributeData* pData = (xml::AttributeData*)aElement.getValue();
227 : :
228 : 16 : sal_Int32 nPos = aName.indexOf( sal_Unicode(':') );
229 [ + - ]: 16 : if( nPos != -1L )
230 : : {
231 : 16 : const OUString aPrefix( aName.copy( 0L, nPos ));
232 : 16 : const OUString aLName( aName.copy( nPos+1L ));
233 : :
234 [ - + ]: 16 : if( pData->Namespace.isEmpty() )
235 : : {
236 [ # # ][ # # ]: 0 : if( mpContainer->AddAttr( aPrefix, aLName, pData->Value ) )
237 : : return;
238 : : }
239 : : else
240 : : {
241 [ + - ][ + - ]: 16 : if( mpContainer->AddAttr( aPrefix, pData->Namespace, aLName, pData->Value ) )
242 : : return;
243 [ + - ][ - + ]: 16 : }
244 : : }
245 : : else
246 : : {
247 [ # # ]: 0 : if( pData->Namespace.isEmpty() )
248 : : {
249 [ # # ]: 0 : if( mpContainer->AddAttr( aName, pData->Value ) )
250 : 16 : return;
251 : : }
252 : : }
253 : : }
254 : :
255 : 0 : void SAL_CALL SvUnoAttributeContainer::removeByName(const OUString& Name)
256 : : throw( container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException )
257 : : {
258 : 0 : sal_uInt16 nAttr = getIndexByName(Name);
259 [ # # ]: 0 : if( nAttr == USHRT_MAX )
260 [ # # ]: 0 : throw container::NoSuchElementException();
261 : :
262 : 0 : mpContainer->Remove( nAttr );
263 : 0 : }
264 : :
265 : : //XServiceInfo
266 : 0 : OUString SAL_CALL SvUnoAttributeContainer::getImplementationName(void) throw( uno::RuntimeException )
267 : : {
268 : 0 : return OUString( "SvUnoAttributeContainer" );
269 : : }
270 : :
271 : 0 : uno::Sequence< OUString > SvUnoAttributeContainer::getSupportedServiceNames(void)
272 : : throw( uno::RuntimeException )
273 : : {
274 : 0 : OUString aSN( OUString( "com.sun.star.xml.AttributeContainer" ) );
275 [ # # ]: 0 : uno::Sequence< OUString > aNS( &aSN, 1L );
276 : 0 : return aNS;
277 : : }
278 : :
279 : 0 : sal_Bool SvUnoAttributeContainer::supportsService(const OUString& ServiceName)
280 : : throw( uno::RuntimeException )
281 : : {
282 [ # # ]: 0 : const uno::Sequence < OUString > aServiceNames( getSupportedServiceNames() );
283 : 0 : const OUString* pNames = aServiceNames.getConstArray();
284 : 0 : sal_Int32 nCount = aServiceNames.getLength();
285 [ # # ]: 0 : while( nCount-- )
286 : : {
287 [ # # ]: 0 : if( *pNames++ == ServiceName )
288 : 0 : return sal_True;
289 : : }
290 : :
291 [ # # ]: 0 : return sal_False;
292 : : }
293 : :
294 : :
295 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|