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