xref: /aoo41x/main/xmloff/source/style/xmlprmap.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmloff.hxx"
30 #include <tools/debug.hxx>
31 #include <xmloff/xmlprhdl.hxx>
32 #include "xmlbahdl.hxx"
33 #include <xmloff/xmlprmap.hxx>
34 #include <xmloff/xmltypes.hxx>
35 #include <com/sun/star/beans/XPropertySet.hpp>
36 #include <com/sun/star/beans/XPropertyState.hpp>
37 #include <com/sun/star/uno/Any.hxx>
38 #include <xmloff/xmltoken.hxx>
39 
40 
41 using namespace ::std;
42 using ::rtl::OUString;
43 using ::rtl::OUStringBuffer;
44 
45 using namespace ::com::sun::star::uno;
46 using namespace ::com::sun::star::beans;
47 using ::xmloff::token::GetXMLToken;
48 
49 XMLPropertySetMapperEntry_Impl::XMLPropertySetMapperEntry_Impl(
50 	const XMLPropertyMapEntry& rMapEntry,
51 	const UniReference< XMLPropertyHandlerFactory >& rFactory ) :
52 	sXMLAttributeName( GetXMLToken(rMapEntry.meXMLName) ),
53 	sAPIPropertyName( OUString(rMapEntry.msApiName, rMapEntry.nApiNameLength,
54                                RTL_TEXTENCODING_ASCII_US ) ),
55 	nXMLNameSpace( rMapEntry.mnNameSpace ),
56 	nType( rMapEntry.mnType ),
57 	nContextId( rMapEntry.mnContextId ),
58     nEarliestODFVersionForExport( rMapEntry.mnEarliestODFVersionForExport ),
59 	pHdl( rFactory->GetPropertyHandler( rMapEntry.mnType & MID_FLAG_MASK ) )
60 {
61 }
62 
63 XMLPropertySetMapperEntry_Impl::XMLPropertySetMapperEntry_Impl(
64 		const XMLPropertySetMapperEntry_Impl& rEntry ) :
65 	sXMLAttributeName( rEntry.sXMLAttributeName),
66 	sAPIPropertyName( rEntry.sAPIPropertyName),
67 	nXMLNameSpace( rEntry.nXMLNameSpace),
68 	nType( rEntry.nType),
69 	nContextId( rEntry.nContextId),
70     nEarliestODFVersionForExport( rEntry.nEarliestODFVersionForExport ),
71 	pHdl( rEntry.pHdl)
72 {
73 	DBG_ASSERT( pHdl, "Unknown XML property type handler!" );
74 }
75 
76 ///////////////////////////////////////////////////////////////////////////
77 //
78 // Ctor
79 //
80 XMLPropertySetMapper::XMLPropertySetMapper(
81 		const XMLPropertyMapEntry* pEntries,
82 		const UniReference< XMLPropertyHandlerFactory >& rFactory )
83 {
84 	aHdlFactories.push_back( rFactory );
85 	if( pEntries )
86 	{
87 		const XMLPropertyMapEntry* pIter = pEntries;
88 
89 		// count entries
90 		while( pIter->msApiName )
91 		{
92 			XMLPropertySetMapperEntry_Impl aEntry( *pIter, rFactory );
93 			aMapEntries.push_back( aEntry );
94 			pIter++;
95 		}
96 	}
97 }
98 
99 XMLPropertySetMapper::~XMLPropertySetMapper()
100 {
101 }
102 
103 void XMLPropertySetMapper::AddMapperEntry(
104 	const UniReference < XMLPropertySetMapper >& rMapper )
105 {
106 	for( vector < UniReference < XMLPropertyHandlerFactory > >::iterator
107 			aFIter = rMapper->aHdlFactories.begin();
108 		 aFIter != rMapper->aHdlFactories.end();
109 		 aFIter++ )
110 	{
111 		aHdlFactories.push_back( *aFIter );
112 	}
113 
114 	for( vector < XMLPropertySetMapperEntry_Impl >::iterator
115 			aEIter = rMapper->aMapEntries.begin();
116 		 aEIter != rMapper->aMapEntries.end();
117 		 aEIter++ )
118 	{
119 		aMapEntries.push_back( *aEIter );
120 	}
121 }
122 
123 ///////////////////////////////////////////////////////////////////////////
124 //
125 // Export a Property
126 //
127 sal_Bool XMLPropertySetMapper::exportXML(
128 		OUString& rStrExpValue,
129 		const XMLPropertyState& rProperty,
130 		const SvXMLUnitConverter& rUnitConverter ) const
131 {
132 	sal_Bool bRet = sal_False;
133 
134 	const XMLPropertyHandler* pHdl = GetPropertyHandler( rProperty.mnIndex );
135 
136 	DBG_ASSERT( pHdl, "Unknown XML Type!" );
137 	if( pHdl )
138 		bRet = pHdl->exportXML( rStrExpValue, rProperty.maValue,
139 								rUnitConverter );
140 
141 	return bRet;
142 }
143 
144 ///////////////////////////////////////////////////////////////////////////
145 //
146 // Import a Property
147 //
148 sal_Bool XMLPropertySetMapper::importXML(
149 		const OUString& rStrImpValue,
150 		XMLPropertyState& rProperty,
151 		const SvXMLUnitConverter& rUnitConverter ) const
152 {
153 	sal_Bool bRet = sal_False;
154 
155 	const XMLPropertyHandler* pHdl = GetPropertyHandler( rProperty.mnIndex );
156 
157 	if( pHdl )
158 		bRet = pHdl->importXML( rStrImpValue, rProperty.maValue,
159 								rUnitConverter );
160 
161 	return bRet;
162 }
163 
164 ///////////////////////////////////////////////////////////////////////////
165 //
166 // Search for the given name and the namespace in the list and return
167 // the index of the entry
168 // If there is no matching entry the method returns -1
169 //
170 sal_Int32 XMLPropertySetMapper::GetEntryIndex(
171 		sal_uInt16 nNamespace,
172 		const OUString& rStrName,
173 		sal_uInt32 nPropType,
174 		sal_Int32 nStartAt /* = -1 */ ) const
175 {
176 	sal_Int32 nEntries = GetEntryCount();
177 	sal_Int32 nIndex= nStartAt == - 1? 0 : nStartAt+1;
178 
179     if ( nEntries )
180     {
181 	    do
182 	    {
183 		    const XMLPropertySetMapperEntry_Impl& rEntry = aMapEntries[nIndex];
184 		    if( (!nPropType || nPropType == rEntry.GetPropType()) &&
185 			    rEntry.nXMLNameSpace == nNamespace &&
186 			    rStrName == rEntry.sXMLAttributeName )
187 			    return nIndex;
188 		    else
189 			    nIndex++;
190 
191 	    } while( nIndex<nEntries );
192     }
193 
194 	return -1;
195 }
196 
197 
198 /** searches for an entry that matches the given api name, namespace and local name or -1 if nothing found */
199 sal_Int32 XMLPropertySetMapper::FindEntryIndex(
200 		const sal_Char* sApiName,
201 		sal_uInt16 nNameSpace,
202 		const OUString& sXMLName ) const
203 {
204 	sal_Int32 nIndex = 0;
205 	sal_Int32 nEntries = GetEntryCount();
206 
207 	do
208 	{
209 		const XMLPropertySetMapperEntry_Impl& rEntry = aMapEntries[nIndex];
210 		if( rEntry.nXMLNameSpace == nNameSpace &&
211 			rEntry.sXMLAttributeName.equals( sXMLName ) &&
212 			0 == rEntry.sAPIPropertyName.compareToAscii( sApiName ) )
213 			return nIndex;
214 		else
215 			nIndex++;
216 
217 	} while( nIndex < nEntries );
218 
219 	return -1;
220 }
221 
222 sal_Int32 XMLPropertySetMapper::FindEntryIndex( const sal_Int16 nContextId ) const
223 {
224 	sal_Int32 nIndex = 0;
225 	const sal_Int32 nEntries = GetEntryCount();
226 
227     if ( nEntries )
228     {
229 	    do
230 	    {
231 		    const XMLPropertySetMapperEntry_Impl& rEntry = aMapEntries[nIndex];
232 		    if( rEntry.nContextId == nContextId )
233 			    return nIndex;
234 		    else
235 			    nIndex++;
236 
237 	    } while( nIndex < nEntries );
238     }
239 
240 	return -1;
241 }
242 
243 void XMLPropertySetMapper::RemoveEntry( sal_Int32 nIndex )
244 {
245     const sal_Int32 nEntries = GetEntryCount();
246     if( nIndex>=nEntries || nIndex<0 )
247         return;
248     vector < XMLPropertySetMapperEntry_Impl >::iterator aEIter = aMapEntries.begin();
249     for( sal_Int32 nN=0; nN<nIndex; nN++ )
250         aEIter++;
251     aMapEntries.erase( aEIter );
252 }
253