xref: /trunk/main/xmloff/source/style/xmltabi.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 <com/sun/star/style/TabAlign.hpp>
31 #include <rtl/ustrbuf.hxx>
32 #include <xmloff/xmltkmap.hxx>
33 #include <xmloff/nmspmap.hxx>
34 #include "xmloff/xmlnmspe.hxx"
35 #include <xmloff/xmlimp.hxx>
36 #include <com/sun/star/style/TabStop.hpp>
37 #include <xmloff/xmltoken.hxx>
38 #include "xmloff/i18nmap.hxx"
39 #include <xmloff/xmluconv.hxx>
40 
41 #include "xmltabi.hxx"
42 
43 #define _SVSTDARR_USHORTS
44 #include <svl/svstdarr.hxx>
45 
46 using ::rtl::OUString;
47 using ::rtl::OUStringBuffer;
48 
49 using namespace ::com::sun::star;
50 using namespace ::xmloff::token;
51 
52 // ---
53 
54 enum SvXMLTokenMapAttrs
55 {
56 	XML_TOK_TABSTOP_POSITION,
57 	XML_TOK_TABSTOP_TYPE,
58 	XML_TOK_TABSTOP_CHAR,
59 	XML_TOK_TABSTOP_LEADER_STYLE,
60 	XML_TOK_TABSTOP_LEADER_TEXT,
61 	XML_TOK_TABSTOP_END=XML_TOK_UNKNOWN
62 };
63 
64 static __FAR_DATA SvXMLTokenMapEntry aTabsAttributesAttrTokenMap[] =
65 {
66 	{ XML_NAMESPACE_STYLE, XML_POSITION,	 XML_TOK_TABSTOP_POSITION },
67 	{ XML_NAMESPACE_STYLE, XML_TYPE,	  	 XML_TOK_TABSTOP_TYPE },
68 	{ XML_NAMESPACE_STYLE, XML_CHAR,	  	 XML_TOK_TABSTOP_CHAR },
69 	{ XML_NAMESPACE_STYLE, XML_LEADER_TEXT,  XML_TOK_TABSTOP_LEADER_TEXT },
70 	{ XML_NAMESPACE_STYLE, XML_LEADER_STYLE,  XML_TOK_TABSTOP_LEADER_STYLE },
71 	XML_TOKEN_MAP_END
72 };
73 
74 // ---
75 
76 class SvxXMLTabStopContext_Impl : public SvXMLImportContext
77 {
78 private:
79  	style::TabStop	aTabStop;
80 
81 public:
82 	TYPEINFO();
83 
84 	SvxXMLTabStopContext_Impl( SvXMLImport& rImport, sal_uInt16 nPrfx,
85 							   const OUString& rLName,
86 							   const uno::Reference< xml::sax::XAttributeList > & xAttrList );
87 
88 	virtual ~SvxXMLTabStopContext_Impl();
89 
90 	virtual SvXMLImportContext *CreateChildContext( sal_uInt16 nPrefix,
91 								   const OUString& rLocalName,
92 								   const uno::Reference< xml::sax::XAttributeList > & xAttrList );
93 
94 	const style::TabStop& getTabStop() const { return aTabStop; }
95 };
96 
97 TYPEINIT1( SvxXMLTabStopContext_Impl, SvXMLImportContext );
98 
99 SvxXMLTabStopContext_Impl::SvxXMLTabStopContext_Impl(
100 							   SvXMLImport& rImport, sal_uInt16 nPrfx,
101 							   const OUString& rLName,
102 							   const uno::Reference< xml::sax::XAttributeList > & xAttrList )
103 : SvXMLImportContext( rImport, nPrfx, rLName )
104 {
105 	aTabStop.Position = 0;
106 	aTabStop.Alignment = style::TabAlign_LEFT;
107 	aTabStop.DecimalChar = sal_Unicode( ',' );
108 	aTabStop.FillChar = sal_Unicode( ' ' );
109 	sal_Unicode cTextFillChar = 0;
110 
111 	SvXMLTokenMap aTokenMap( aTabsAttributesAttrTokenMap );
112 
113 	sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
114 	for( sal_Int16 i=0; i < nAttrCount; i++ )
115 	{
116 		const OUString& rAttrName = xAttrList->getNameByIndex( i );
117 		OUString aLocalName;
118 		sal_uInt16 nPrefix =
119 			GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName,
120 															&aLocalName );
121 		const OUString& rValue = xAttrList->getValueByIndex( i );
122 
123 		sal_Int32 nVal;
124 		switch( aTokenMap.Get( nPrefix, aLocalName ) )
125 		{
126 		case XML_TOK_TABSTOP_POSITION:
127 			if( GetImport().GetMM100UnitConverter().convertMeasure( nVal,
128 																	rValue ) )
129 				aTabStop.Position = nVal;
130 			break;
131 		case XML_TOK_TABSTOP_TYPE:
132 			if( IsXMLToken( rValue, XML_LEFT ) )
133 			{
134 				aTabStop.Alignment = style::TabAlign_LEFT;
135 			}
136 			else if( IsXMLToken( rValue, XML_RIGHT ) )
137 			{
138 				aTabStop.Alignment = style::TabAlign_RIGHT;
139 			}
140 			else if( IsXMLToken( rValue, XML_CENTER ) )
141 			{
142 				aTabStop.Alignment = style::TabAlign_CENTER;
143 			}
144 			else if( IsXMLToken( rValue, XML_CHAR ) )
145 			{
146 				aTabStop.Alignment = style::TabAlign_DECIMAL;
147 			}
148 			else if( IsXMLToken( rValue, XML_DEFAULT ) )
149 			{
150 				aTabStop.Alignment = style::TabAlign_DEFAULT;
151 			}
152 			break;
153 		case XML_TOK_TABSTOP_CHAR:
154 			if( 0 != rValue.getLength() )
155 				aTabStop.DecimalChar = rValue[0];
156 			break;
157 		case XML_TOK_TABSTOP_LEADER_STYLE:
158 			if( IsXMLToken( rValue, XML_NONE ) )
159 				aTabStop.FillChar = ' ';
160 			else if( IsXMLToken( rValue, XML_DOTTED ) )
161 				aTabStop.FillChar = '.';
162 			else
163 				aTabStop.FillChar = '_';
164 			break;
165 		case XML_TOK_TABSTOP_LEADER_TEXT:
166 			if( 0 != rValue.getLength() )
167 				cTextFillChar = rValue[0];
168 			break;
169 		}
170 	}
171 
172 	if( cTextFillChar != 0 && aTabStop.FillChar != ' ' )
173 		aTabStop.FillChar = cTextFillChar;
174 }
175 
176 SvxXMLTabStopContext_Impl::~SvxXMLTabStopContext_Impl()
177 {
178 }
179 
180 SvXMLImportContext *SvxXMLTabStopContext_Impl::CreateChildContext(
181 								   sal_uInt16 nPrefix,
182 								   const OUString& rLocalName,
183 								   const uno::Reference< xml::sax::XAttributeList > & )
184 {
185 	return new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
186 }
187 
188 
189 
190 
191 typedef SvxXMLTabStopContext_Impl *SvxXMLTabStopContext_Impl_ImplPtr;
192 SV_DECL_PTRARR( SvxXMLTabStopArray_Impl, SvxXMLTabStopContext_Impl_ImplPtr,	20, 5 )
193 
194 
195 // ---
196 
197 TYPEINIT1( SvxXMLTabStopImportContext, XMLElementPropertyContext );
198 
199 SvxXMLTabStopImportContext::SvxXMLTabStopImportContext(
200 								SvXMLImport& rImport, sal_uInt16 nPrfx,
201 								const OUString& rLName,
202 								const XMLPropertyState& rProp,
203 				 				::std::vector< XMLPropertyState > &rProps )
204 : XMLElementPropertyContext( rImport, nPrfx, rLName, rProp, rProps ),
205   mpTabStops( NULL )
206 {
207 }
208 
209 SvxXMLTabStopImportContext::~SvxXMLTabStopImportContext()
210 {
211 	if( mpTabStops )
212 	{
213 		sal_uInt16 nCount = mpTabStops->Count();
214 		while( nCount )
215 		{
216 			nCount--;
217 			SvxXMLTabStopContext_Impl *pTabStop = (*mpTabStops)[nCount];
218 			mpTabStops->Remove( nCount, 1 );
219 			pTabStop->ReleaseRef();
220 		}
221 	}
222 
223 	delete mpTabStops;
224 }
225 
226 SvXMLImportContext *SvxXMLTabStopImportContext::CreateChildContext(
227 								   sal_uInt16 nPrefix,
228 								   const OUString& rLocalName,
229 								   const uno::Reference< xml::sax::XAttributeList > & xAttrList )
230 {
231 	SvXMLImportContext *pContext = 0;
232 
233 	if( XML_NAMESPACE_STYLE == nPrefix && IsXMLToken( rLocalName, XML_TAB_STOP ) )
234 	{
235 		// create new tabstop import context
236 		SvxXMLTabStopContext_Impl *pTabStopContext =
237 			new SvxXMLTabStopContext_Impl( GetImport(), nPrefix, rLocalName,
238 										   xAttrList );
239 
240 		// add new tabstop to array of tabstops
241 		if( !mpTabStops )
242 			mpTabStops = new SvxXMLTabStopArray_Impl;
243 
244 		mpTabStops->Insert( pTabStopContext, mpTabStops->Count() );
245 		pTabStopContext->AddRef();
246 
247 		pContext = pTabStopContext;
248 	}
249 	else
250 	{
251 		pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
252 	}
253 
254 	return pContext;
255 }
256 
257 void SvxXMLTabStopImportContext::EndElement( )
258 {
259 	sal_uInt16 nCount = mpTabStops ? mpTabStops->Count() : 0;
260 	uno::Sequence< style::TabStop> aSeq( nCount );
261 
262 	if( mpTabStops )
263 	{
264 		sal_uInt16 nNewCount = 0;
265 
266 		style::TabStop* pTabStops = aSeq.getArray();
267 		for( sal_uInt16 i=0; i < nCount; i++ )
268 		{
269 			SvxXMLTabStopContext_Impl *pTabStopContext = (*mpTabStops)[i];
270 			const style::TabStop& rTabStop = pTabStopContext->getTabStop();
271 			sal_Bool bDflt = style::TabAlign_DEFAULT == rTabStop.Alignment;
272 			if( !bDflt || 0==i )
273 			{
274 				*pTabStops++ = pTabStopContext->getTabStop();
275 				nNewCount++;
276 			}
277 			if( bDflt && 0==i )
278 				break;
279 		}
280 
281 		if( nCount != nNewCount )
282 			aSeq.realloc( nNewCount );
283 	}
284 	aProp.maValue <<= aSeq;
285 
286 	SetInsert( sal_True );
287 	XMLElementPropertyContext::EndElement();
288 
289 }
290 
291 
292 
293 
294