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 <osl/mutex.hxx>
31*cdf0e10cSrcweir #include <xmloff/xmltoken.hxx>
32*cdf0e10cSrcweir #include <rtl/uuid.h>
33*cdf0e10cSrcweir #include <rtl/memory.h>
34*cdf0e10cSrcweir #include <xmloff/attrlist.hxx>
35*cdf0e10cSrcweir #include "MutableAttrList.hxx"
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir using ::rtl::OUString;
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir using namespace ::osl;
40*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
41*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
42*cdf0e10cSrcweir using namespace ::com::sun::star::util;
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir SvXMLAttributeList *XMLMutableAttributeList::GetMutableAttrList()
45*cdf0e10cSrcweir {
46*cdf0e10cSrcweir 	if( !m_pMutableAttrList )
47*cdf0e10cSrcweir 	{
48*cdf0e10cSrcweir 		m_pMutableAttrList = new SvXMLAttributeList( m_xAttrList );
49*cdf0e10cSrcweir 		m_xAttrList = m_pMutableAttrList;
50*cdf0e10cSrcweir 	}
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir 	return m_pMutableAttrList;
53*cdf0e10cSrcweir }
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir XMLMutableAttributeList::XMLMutableAttributeList() :
56*cdf0e10cSrcweir 	m_pMutableAttrList( new SvXMLAttributeList )
57*cdf0e10cSrcweir {
58*cdf0e10cSrcweir 	m_xAttrList = m_pMutableAttrList;
59*cdf0e10cSrcweir }
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir XMLMutableAttributeList::XMLMutableAttributeList( const Reference<
62*cdf0e10cSrcweir 		XAttributeList> & rAttrList, sal_Bool bClone ) :
63*cdf0e10cSrcweir 	m_xAttrList( rAttrList.is() ? rAttrList : new SvXMLAttributeList ),
64*cdf0e10cSrcweir 	m_pMutableAttrList( 0 )
65*cdf0e10cSrcweir {
66*cdf0e10cSrcweir 	if( bClone )
67*cdf0e10cSrcweir 		GetMutableAttrList();
68*cdf0e10cSrcweir }
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir XMLMutableAttributeList::~XMLMutableAttributeList()
72*cdf0e10cSrcweir {
73*cdf0e10cSrcweir 	m_xAttrList = 0;
74*cdf0e10cSrcweir }
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir // XUnoTunnel & co
78*cdf0e10cSrcweir const Sequence< sal_Int8 > & XMLMutableAttributeList::getUnoTunnelId() throw()
79*cdf0e10cSrcweir {
80*cdf0e10cSrcweir 	static Sequence< sal_Int8 > * pSeq = 0;
81*cdf0e10cSrcweir 	if( !pSeq )
82*cdf0e10cSrcweir 	{
83*cdf0e10cSrcweir 		Guard< Mutex > aGuard( Mutex::getGlobalMutex() );
84*cdf0e10cSrcweir 		if( !pSeq )
85*cdf0e10cSrcweir 		{
86*cdf0e10cSrcweir 			static Sequence< sal_Int8 > aSeq( 16 );
87*cdf0e10cSrcweir 			rtl_createUuid( (sal_uInt8*)aSeq.getArray(), 0, sal_True );
88*cdf0e10cSrcweir 			pSeq = &aSeq;
89*cdf0e10cSrcweir 		}
90*cdf0e10cSrcweir 	}
91*cdf0e10cSrcweir 	return *pSeq;
92*cdf0e10cSrcweir }
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir // XUnoTunnel
95*cdf0e10cSrcweir sal_Int64 SAL_CALL XMLMutableAttributeList::getSomething(
96*cdf0e10cSrcweir 		const Sequence< sal_Int8 >& rId )
97*cdf0e10cSrcweir 	throw( RuntimeException )
98*cdf0e10cSrcweir {
99*cdf0e10cSrcweir 	if( rId.getLength() == 16 &&
100*cdf0e10cSrcweir 		0 == rtl_compareMemory( getUnoTunnelId().getConstArray(),
101*cdf0e10cSrcweir 								rId.getConstArray(), 16 ) )
102*cdf0e10cSrcweir 	{
103*cdf0e10cSrcweir 		return sal::static_int_cast<sal_Int64>(reinterpret_cast<sal_uIntPtr>(this));
104*cdf0e10cSrcweir 	}
105*cdf0e10cSrcweir 	return 0;
106*cdf0e10cSrcweir }
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir sal_Int16 SAL_CALL XMLMutableAttributeList::getLength(void)
109*cdf0e10cSrcweir 		throw( RuntimeException )
110*cdf0e10cSrcweir {
111*cdf0e10cSrcweir 	return m_xAttrList->getLength();
112*cdf0e10cSrcweir }
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir OUString SAL_CALL XMLMutableAttributeList::getNameByIndex(sal_Int16 i)
116*cdf0e10cSrcweir 		throw( RuntimeException )
117*cdf0e10cSrcweir {
118*cdf0e10cSrcweir 	return m_xAttrList->getNameByIndex( i );
119*cdf0e10cSrcweir }
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir OUString SAL_CALL XMLMutableAttributeList::getTypeByIndex(sal_Int16 i)
123*cdf0e10cSrcweir 		throw( RuntimeException )
124*cdf0e10cSrcweir {
125*cdf0e10cSrcweir 	return m_xAttrList->getTypeByIndex( i );
126*cdf0e10cSrcweir }
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir OUString SAL_CALL  XMLMutableAttributeList::getValueByIndex(sal_Int16 i)
129*cdf0e10cSrcweir 	throw( RuntimeException )
130*cdf0e10cSrcweir {
131*cdf0e10cSrcweir 	return m_xAttrList->getValueByIndex( i );
132*cdf0e10cSrcweir }
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir OUString SAL_CALL XMLMutableAttributeList::getTypeByName(
135*cdf0e10cSrcweir 		const OUString& rName )
136*cdf0e10cSrcweir 		throw( RuntimeException )
137*cdf0e10cSrcweir {
138*cdf0e10cSrcweir 	return m_xAttrList->getTypeByName( rName );
139*cdf0e10cSrcweir }
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir OUString SAL_CALL XMLMutableAttributeList::getValueByName(
142*cdf0e10cSrcweir 		const OUString& rName)
143*cdf0e10cSrcweir 		throw( RuntimeException )
144*cdf0e10cSrcweir {
145*cdf0e10cSrcweir 	return m_xAttrList->getValueByName( rName );
146*cdf0e10cSrcweir }
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir Reference< XCloneable > XMLMutableAttributeList::createClone()
150*cdf0e10cSrcweir 		throw( RuntimeException )
151*cdf0e10cSrcweir {
152*cdf0e10cSrcweir 	// A cloned list will be a read only list!
153*cdf0e10cSrcweir 	Reference< XCloneable >  r = new SvXMLAttributeList( m_xAttrList );
154*cdf0e10cSrcweir 	return r;
155*cdf0e10cSrcweir }
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir void XMLMutableAttributeList::SetValueByIndex( sal_Int16 i,
158*cdf0e10cSrcweir 											   const ::rtl::OUString& rValue )
159*cdf0e10cSrcweir {
160*cdf0e10cSrcweir 	GetMutableAttrList()->SetValueByIndex( i, rValue );
161*cdf0e10cSrcweir }
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir void XMLMutableAttributeList::AddAttribute( const OUString &rName ,
164*cdf0e10cSrcweir 											const OUString &rValue )
165*cdf0e10cSrcweir {
166*cdf0e10cSrcweir 	GetMutableAttrList()->AddAttribute( rName, rValue );
167*cdf0e10cSrcweir }
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir void XMLMutableAttributeList::RemoveAttributeByIndex( sal_Int16 i )
170*cdf0e10cSrcweir {
171*cdf0e10cSrcweir 	GetMutableAttrList()->RemoveAttributeByIndex( i );
172*cdf0e10cSrcweir }
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir void XMLMutableAttributeList::RenameAttributeByIndex( sal_Int16 i,
175*cdf0e10cSrcweir 												  	const OUString& rNewName )
176*cdf0e10cSrcweir {
177*cdf0e10cSrcweir 	GetMutableAttrList()->RenameAttributeByIndex( i, rNewName );
178*cdf0e10cSrcweir }
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir void XMLMutableAttributeList::AppendAttributeList(
181*cdf0e10cSrcweir 		const Reference< ::com::sun::star::xml::sax::XAttributeList >& r )
182*cdf0e10cSrcweir {
183*cdf0e10cSrcweir 	GetMutableAttrList()->AppendAttributeList( r );
184*cdf0e10cSrcweir }
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir sal_Int16 XMLMutableAttributeList::GetIndexByName( const OUString& rName ) const
187*cdf0e10cSrcweir {
188*cdf0e10cSrcweir 	sal_Int16 nIndex = -1;
189*cdf0e10cSrcweir 	if( m_pMutableAttrList )
190*cdf0e10cSrcweir 	{
191*cdf0e10cSrcweir 		nIndex = m_pMutableAttrList->GetIndexByName( rName );
192*cdf0e10cSrcweir 	}
193*cdf0e10cSrcweir 	else
194*cdf0e10cSrcweir 	{
195*cdf0e10cSrcweir 		sal_Int16 nCount = m_xAttrList->getLength();
196*cdf0e10cSrcweir 		for( sal_Int16 i=0; nIndex==-1 && i<nCount ; ++i )
197*cdf0e10cSrcweir 		{
198*cdf0e10cSrcweir 			if( m_xAttrList->getNameByIndex(i) == rName )
199*cdf0e10cSrcweir 				nIndex = i;
200*cdf0e10cSrcweir 		}
201*cdf0e10cSrcweir 	}
202*cdf0e10cSrcweir 	return nIndex;
203*cdf0e10cSrcweir }
204