xref: /aoo42x/main/xmloff/source/style/xmlprmap.cxx (revision cdf0e10c)
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 <xmloff/xmlprhdl.hxx>
32*cdf0e10cSrcweir #include "xmlbahdl.hxx"
33*cdf0e10cSrcweir #include <xmloff/xmlprmap.hxx>
34*cdf0e10cSrcweir #include <xmloff/xmltypes.hxx>
35*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertyState.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/uno/Any.hxx>
38*cdf0e10cSrcweir #include <xmloff/xmltoken.hxx>
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir using namespace ::std;
42*cdf0e10cSrcweir using ::rtl::OUString;
43*cdf0e10cSrcweir using ::rtl::OUStringBuffer;
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
46*cdf0e10cSrcweir using namespace ::com::sun::star::beans;
47*cdf0e10cSrcweir using ::xmloff::token::GetXMLToken;
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir XMLPropertySetMapperEntry_Impl::XMLPropertySetMapperEntry_Impl(
50*cdf0e10cSrcweir 	const XMLPropertyMapEntry& rMapEntry,
51*cdf0e10cSrcweir 	const UniReference< XMLPropertyHandlerFactory >& rFactory ) :
52*cdf0e10cSrcweir 	sXMLAttributeName( GetXMLToken(rMapEntry.meXMLName) ),
53*cdf0e10cSrcweir 	sAPIPropertyName( OUString(rMapEntry.msApiName, rMapEntry.nApiNameLength,
54*cdf0e10cSrcweir                                RTL_TEXTENCODING_ASCII_US ) ),
55*cdf0e10cSrcweir 	nXMLNameSpace( rMapEntry.mnNameSpace ),
56*cdf0e10cSrcweir 	nType( rMapEntry.mnType ),
57*cdf0e10cSrcweir 	nContextId( rMapEntry.mnContextId ),
58*cdf0e10cSrcweir     nEarliestODFVersionForExport( rMapEntry.mnEarliestODFVersionForExport ),
59*cdf0e10cSrcweir 	pHdl( rFactory->GetPropertyHandler( rMapEntry.mnType & MID_FLAG_MASK ) )
60*cdf0e10cSrcweir {
61*cdf0e10cSrcweir }
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir XMLPropertySetMapperEntry_Impl::XMLPropertySetMapperEntry_Impl(
64*cdf0e10cSrcweir 		const XMLPropertySetMapperEntry_Impl& rEntry ) :
65*cdf0e10cSrcweir 	sXMLAttributeName( rEntry.sXMLAttributeName),
66*cdf0e10cSrcweir 	sAPIPropertyName( rEntry.sAPIPropertyName),
67*cdf0e10cSrcweir 	nXMLNameSpace( rEntry.nXMLNameSpace),
68*cdf0e10cSrcweir 	nType( rEntry.nType),
69*cdf0e10cSrcweir 	nContextId( rEntry.nContextId),
70*cdf0e10cSrcweir     nEarliestODFVersionForExport( rEntry.nEarliestODFVersionForExport ),
71*cdf0e10cSrcweir 	pHdl( rEntry.pHdl)
72*cdf0e10cSrcweir {
73*cdf0e10cSrcweir 	DBG_ASSERT( pHdl, "Unknown XML property type handler!" );
74*cdf0e10cSrcweir }
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////
77*cdf0e10cSrcweir //
78*cdf0e10cSrcweir // Ctor
79*cdf0e10cSrcweir //
80*cdf0e10cSrcweir XMLPropertySetMapper::XMLPropertySetMapper(
81*cdf0e10cSrcweir 		const XMLPropertyMapEntry* pEntries,
82*cdf0e10cSrcweir 		const UniReference< XMLPropertyHandlerFactory >& rFactory )
83*cdf0e10cSrcweir {
84*cdf0e10cSrcweir 	aHdlFactories.push_back( rFactory );
85*cdf0e10cSrcweir 	if( pEntries )
86*cdf0e10cSrcweir 	{
87*cdf0e10cSrcweir 		const XMLPropertyMapEntry* pIter = pEntries;
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir 		// count entries
90*cdf0e10cSrcweir 		while( pIter->msApiName )
91*cdf0e10cSrcweir 		{
92*cdf0e10cSrcweir 			XMLPropertySetMapperEntry_Impl aEntry( *pIter, rFactory );
93*cdf0e10cSrcweir 			aMapEntries.push_back( aEntry );
94*cdf0e10cSrcweir 			pIter++;
95*cdf0e10cSrcweir 		}
96*cdf0e10cSrcweir 	}
97*cdf0e10cSrcweir }
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir XMLPropertySetMapper::~XMLPropertySetMapper()
100*cdf0e10cSrcweir {
101*cdf0e10cSrcweir }
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir void XMLPropertySetMapper::AddMapperEntry(
104*cdf0e10cSrcweir 	const UniReference < XMLPropertySetMapper >& rMapper )
105*cdf0e10cSrcweir {
106*cdf0e10cSrcweir 	for( vector < UniReference < XMLPropertyHandlerFactory > >::iterator
107*cdf0e10cSrcweir 			aFIter = rMapper->aHdlFactories.begin();
108*cdf0e10cSrcweir 		 aFIter != rMapper->aHdlFactories.end();
109*cdf0e10cSrcweir 		 aFIter++ )
110*cdf0e10cSrcweir 	{
111*cdf0e10cSrcweir 		aHdlFactories.push_back( *aFIter );
112*cdf0e10cSrcweir 	}
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir 	for( vector < XMLPropertySetMapperEntry_Impl >::iterator
115*cdf0e10cSrcweir 			aEIter = rMapper->aMapEntries.begin();
116*cdf0e10cSrcweir 		 aEIter != rMapper->aMapEntries.end();
117*cdf0e10cSrcweir 		 aEIter++ )
118*cdf0e10cSrcweir 	{
119*cdf0e10cSrcweir 		aMapEntries.push_back( *aEIter );
120*cdf0e10cSrcweir 	}
121*cdf0e10cSrcweir }
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////
124*cdf0e10cSrcweir //
125*cdf0e10cSrcweir // Export a Property
126*cdf0e10cSrcweir //
127*cdf0e10cSrcweir sal_Bool XMLPropertySetMapper::exportXML(
128*cdf0e10cSrcweir 		OUString& rStrExpValue,
129*cdf0e10cSrcweir 		const XMLPropertyState& rProperty,
130*cdf0e10cSrcweir 		const SvXMLUnitConverter& rUnitConverter ) const
131*cdf0e10cSrcweir {
132*cdf0e10cSrcweir 	sal_Bool bRet = sal_False;
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir 	const XMLPropertyHandler* pHdl = GetPropertyHandler( rProperty.mnIndex );
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir 	DBG_ASSERT( pHdl, "Unknown XML Type!" );
137*cdf0e10cSrcweir 	if( pHdl )
138*cdf0e10cSrcweir 		bRet = pHdl->exportXML( rStrExpValue, rProperty.maValue,
139*cdf0e10cSrcweir 								rUnitConverter );
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir 	return bRet;
142*cdf0e10cSrcweir }
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////
145*cdf0e10cSrcweir //
146*cdf0e10cSrcweir // Import a Property
147*cdf0e10cSrcweir //
148*cdf0e10cSrcweir sal_Bool XMLPropertySetMapper::importXML(
149*cdf0e10cSrcweir 		const OUString& rStrImpValue,
150*cdf0e10cSrcweir 		XMLPropertyState& rProperty,
151*cdf0e10cSrcweir 		const SvXMLUnitConverter& rUnitConverter ) const
152*cdf0e10cSrcweir {
153*cdf0e10cSrcweir 	sal_Bool bRet = sal_False;
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir 	const XMLPropertyHandler* pHdl = GetPropertyHandler( rProperty.mnIndex );
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir 	if( pHdl )
158*cdf0e10cSrcweir 		bRet = pHdl->importXML( rStrImpValue, rProperty.maValue,
159*cdf0e10cSrcweir 								rUnitConverter );
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir 	return bRet;
162*cdf0e10cSrcweir }
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////
165*cdf0e10cSrcweir //
166*cdf0e10cSrcweir // Search for the given name and the namespace in the list and return
167*cdf0e10cSrcweir // the index of the entry
168*cdf0e10cSrcweir // If there is no matching entry the method returns -1
169*cdf0e10cSrcweir //
170*cdf0e10cSrcweir sal_Int32 XMLPropertySetMapper::GetEntryIndex(
171*cdf0e10cSrcweir 		sal_uInt16 nNamespace,
172*cdf0e10cSrcweir 		const OUString& rStrName,
173*cdf0e10cSrcweir 		sal_uInt32 nPropType,
174*cdf0e10cSrcweir 		sal_Int32 nStartAt /* = -1 */ ) const
175*cdf0e10cSrcweir {
176*cdf0e10cSrcweir 	sal_Int32 nEntries = GetEntryCount();
177*cdf0e10cSrcweir 	sal_Int32 nIndex= nStartAt == - 1? 0 : nStartAt+1;
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir     if ( nEntries )
180*cdf0e10cSrcweir     {
181*cdf0e10cSrcweir 	    do
182*cdf0e10cSrcweir 	    {
183*cdf0e10cSrcweir 		    const XMLPropertySetMapperEntry_Impl& rEntry = aMapEntries[nIndex];
184*cdf0e10cSrcweir 		    if( (!nPropType || nPropType == rEntry.GetPropType()) &&
185*cdf0e10cSrcweir 			    rEntry.nXMLNameSpace == nNamespace &&
186*cdf0e10cSrcweir 			    rStrName == rEntry.sXMLAttributeName )
187*cdf0e10cSrcweir 			    return nIndex;
188*cdf0e10cSrcweir 		    else
189*cdf0e10cSrcweir 			    nIndex++;
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir 	    } while( nIndex<nEntries );
192*cdf0e10cSrcweir     }
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir 	return -1;
195*cdf0e10cSrcweir }
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir /** searches for an entry that matches the given api name, namespace and local name or -1 if nothing found */
199*cdf0e10cSrcweir sal_Int32 XMLPropertySetMapper::FindEntryIndex(
200*cdf0e10cSrcweir 		const sal_Char* sApiName,
201*cdf0e10cSrcweir 		sal_uInt16 nNameSpace,
202*cdf0e10cSrcweir 		const OUString& sXMLName ) const
203*cdf0e10cSrcweir {
204*cdf0e10cSrcweir 	sal_Int32 nIndex = 0;
205*cdf0e10cSrcweir 	sal_Int32 nEntries = GetEntryCount();
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir 	do
208*cdf0e10cSrcweir 	{
209*cdf0e10cSrcweir 		const XMLPropertySetMapperEntry_Impl& rEntry = aMapEntries[nIndex];
210*cdf0e10cSrcweir 		if( rEntry.nXMLNameSpace == nNameSpace &&
211*cdf0e10cSrcweir 			rEntry.sXMLAttributeName.equals( sXMLName ) &&
212*cdf0e10cSrcweir 			0 == rEntry.sAPIPropertyName.compareToAscii( sApiName ) )
213*cdf0e10cSrcweir 			return nIndex;
214*cdf0e10cSrcweir 		else
215*cdf0e10cSrcweir 			nIndex++;
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir 	} while( nIndex < nEntries );
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir 	return -1;
220*cdf0e10cSrcweir }
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir sal_Int32 XMLPropertySetMapper::FindEntryIndex( const sal_Int16 nContextId ) const
223*cdf0e10cSrcweir {
224*cdf0e10cSrcweir 	sal_Int32 nIndex = 0;
225*cdf0e10cSrcweir 	const sal_Int32 nEntries = GetEntryCount();
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir     if ( nEntries )
228*cdf0e10cSrcweir     {
229*cdf0e10cSrcweir 	    do
230*cdf0e10cSrcweir 	    {
231*cdf0e10cSrcweir 		    const XMLPropertySetMapperEntry_Impl& rEntry = aMapEntries[nIndex];
232*cdf0e10cSrcweir 		    if( rEntry.nContextId == nContextId )
233*cdf0e10cSrcweir 			    return nIndex;
234*cdf0e10cSrcweir 		    else
235*cdf0e10cSrcweir 			    nIndex++;
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir 	    } while( nIndex < nEntries );
238*cdf0e10cSrcweir     }
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir 	return -1;
241*cdf0e10cSrcweir }
242*cdf0e10cSrcweir 
243*cdf0e10cSrcweir void XMLPropertySetMapper::RemoveEntry( sal_Int32 nIndex )
244*cdf0e10cSrcweir {
245*cdf0e10cSrcweir     const sal_Int32 nEntries = GetEntryCount();
246*cdf0e10cSrcweir     if( nIndex>=nEntries || nIndex<0 )
247*cdf0e10cSrcweir         return;
248*cdf0e10cSrcweir     vector < XMLPropertySetMapperEntry_Impl >::iterator aEIter = aMapEntries.begin();
249*cdf0e10cSrcweir     for( sal_Int32 nN=0; nN<nIndex; nN++ )
250*cdf0e10cSrcweir         aEIter++;
251*cdf0e10cSrcweir     aMapEntries.erase( aEIter );
252*cdf0e10cSrcweir }
253