xref: /aoo42x/main/xmloff/source/text/txtvfldi.cxx (revision 63bba73c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_xmloff.hxx"
26 
27 /** @#file
28  *
29  *  export of all variable related text fields (and database display field)
30  */
31 #include "txtvfldi.hxx"
32 #include <xmloff/xmltoken.hxx>
33 #include <xmloff/txtimp.hxx>
34 #include <xmloff/xmlnumi.hxx>
35 #include "xmloff/xmlnmspe.hxx"
36 #include <xmloff/nmspmap.hxx>
37 #include "xmloff/i18nmap.hxx"
38 #include <xmloff/xmlimp.hxx>
39 #include <xmloff/xmluconv.hxx>
40 #include <xmloff/xmlement.hxx>
41 #include <com/sun/star/text/SetVariableType.hpp>
42 #include <com/sun/star/text/XTextField.hpp>
43 #include <com/sun/star/text/XDependentTextField.hpp>
44 #include <com/sun/star/text/XTextFieldsSupplier.hpp>
45 #include <com/sun/star/beans/XPropertySet.hpp>
46 #include <com/sun/star/beans/XPropertySetInfo.hpp>
47 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
48 #include <com/sun/star/xml/sax/XAttributeList.hpp>
49 
50 #ifndef _RTL_USTRING
51 #include <rtl/ustring.hxx>
52 #endif
53 #include <tools/debug.hxx>
54 
55 
56 // service names
57 static const sal_Char sAPI_textfield_prefix[]	= "com.sun.star.text.TextField.";
58 static const sal_Char sAPI_fieldmaster_prefix[] = "com.sun.star.text.FieldMaster.";
59 static const sal_Char sAPI_input[]				= "Input";
60 static const sal_Char sAPI_input_user[]			= "InputUser";
61 static const sal_Char sAPI_get_expression[] 	= "GetExpression";
62 static const sal_Char sAPI_set_expression[] 	= "SetExpression";
63 static const sal_Char sAPI_user[]				= "User";
64 static const sal_Char sAPI_table_formula[]      = "TableFormula";
65 static const sal_Char sAPI_database[]			= "com.sun.star.text.TextField.Database";
66 static const sal_Char sAPI_fieldmaster_database[] = "com.sun.star.text.FieldMaster.Database";
67 
68 // property names
69 static const sal_Char sAPI_hint[] 				= "Hint";
70 static const sal_Char sAPI_help[] 				= "Help";
71 static const sal_Char sAPI_tooltip[] 			= "Tooltip";
72 static const sal_Char sAPI_content[]			= "Content";
73 static const sal_Char sAPI_sub_type[]			= "SubType";
74 static const sal_Char sAPI_is_expression[]		= "IsExpression";
75 static const sal_Char sAPI_is_input[]			= "Input";
76 static const sal_Char sAPI_is_show_formula[]	= "IsShowFormula";
77 static const sal_Char sAPI_numbering_type[]		= "NumberingType";
78 static const sal_Char sAPI_number_format[]		= "NumberFormat";
79 static const sal_Char sAPI_name[]				= "Name";
80 static const sal_Char sAPI_numbering_separator[]	= "NumberingSeparator";
81 static const sal_Char sAPI_chapter_numbering_level[]= "ChapterNumberingLevel";
82 static const sal_Char sAPI_value[]				= "Value";
83 static const sal_Char sAPI_is_visible[]			= "IsVisible";
84 static const sal_Char sAPI_variable_subtype[]	= "VariableSubtype";
85 static const sal_Char sAPI_data_column_name[]	= "DataColumnName";
86 static const sal_Char sAPI_is_data_base_format[]	= "DataBaseFormat";
87 static const sal_Char sAPI_current_presentation[]	= "CurrentPresentation";
88 static const sal_Char sAPI_sequence_value[]		= "SequenceValue";
89 static const sal_Char sAPI_is_fixed_language[] = "IsFixedLanguage";
90 
91 
92 using ::rtl::OUString;
93 using ::rtl::OUStringBuffer;
94 
95 using namespace ::com::sun::star;
96 using namespace ::com::sun::star::uno;
97 using namespace ::com::sun::star::beans;
98 using namespace ::com::sun::star::text;
99 using namespace ::com::sun::star::style;
100 using namespace ::xmloff::token;
101 
102 
103 
104 //
105 // XMLVarFieldImportContext: superclass for all variable related fields
106 //
107 
108 TYPEINIT1( XMLVarFieldImportContext, XMLTextFieldImportContext );
109 
110 XMLVarFieldImportContext::XMLVarFieldImportContext(
111 	SvXMLImport& rImport, XMLTextImportHelper& rHlp,
112 	const sal_Char* pServiceName, sal_uInt16 nPrfx,
113 	const OUString& rLocalName,
114 	sal_Bool bName, sal_Bool bFormula, sal_Bool bFormulaDefault,
115 	sal_Bool bDescription, sal_Bool bHelp, sal_Bool bHint, sal_Bool bVisible,
116     sal_Bool bIsDisplayFormula,
117 	sal_Bool bType, sal_Bool bStyle, sal_Bool bValue,
118 	sal_Bool bPresentation) :
119 		XMLTextFieldImportContext(rImport, rHlp, pServiceName, nPrfx, rLocalName),
120 		sPropertyContent(RTL_CONSTASCII_USTRINGPARAM(sAPI_content)),
121 		sPropertyHint(RTL_CONSTASCII_USTRINGPARAM(sAPI_hint)),
122         sPropertyHelp(RTL_CONSTASCII_USTRINGPARAM(sAPI_help)),
123         sPropertyTooltip(RTL_CONSTASCII_USTRINGPARAM(sAPI_tooltip)),
124 		sPropertyIsVisible(RTL_CONSTASCII_USTRINGPARAM(sAPI_is_visible)),
125 		sPropertyIsDisplayFormula(RTL_CONSTASCII_USTRINGPARAM(sAPI_is_show_formula)),
126 		sPropertyCurrentPresentation(RTL_CONSTASCII_USTRINGPARAM(sAPI_current_presentation)),
127 		aValueHelper(rImport, rHlp, bType, bStyle, bValue, sal_False),
128 		bDisplayFormula(sal_False),
129 		bDisplayNone(sal_False),
130 		bNameOK(sal_False),
131 		bFormulaOK(sal_False),
132 		bDescriptionOK(sal_False),
133         bHelpOK(sal_False),
134         bHintOK(sal_False),
135 		bDisplayOK(sal_False),
136 		bSetName(bName),
137 		bSetFormula(bFormula),
138 		bSetFormulaDefault(bFormulaDefault),
139 		bSetDescription(bDescription),
140         bSetHelp(bHelp),
141         bSetHint(bHint),
142 		bSetVisible(bVisible),
143 		bSetDisplayFormula(bIsDisplayFormula),
144 		bSetPresentation(bPresentation)
145 {
146 }
147 
148 void XMLVarFieldImportContext::ProcessAttribute(
149 	sal_uInt16 nAttrToken,
150 	const OUString& sAttrValue )
151 {
152 	switch (nAttrToken)
153 		{
154 		case XML_TOK_TEXTFIELD_NAME:
155 			sName = sAttrValue;
156 			bNameOK = sal_True;
157 			bValid = sal_True;		// we assume: field with name is valid!
158 			break;
159 		case XML_TOK_TEXTFIELD_DESCRIPTION:
160 			sDescription = sAttrValue;
161 			bDescriptionOK = sal_True;
162 			break;
163         case XML_TOK_TEXTFIELD_HELP:
164             sHelp = sAttrValue;
165             bHelpOK = true;
166             break;
167         case XML_TOK_TEXTFIELD_HINT:
168             sHint = sAttrValue;
169             bHintOK = true;
170             break;
171 		case XML_TOK_TEXTFIELD_FORMULA:
172 			{
173 				OUString sTmp;
174 				sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
175 						_GetKeyByAttrName( sAttrValue, &sTmp, sal_False );
176 				if( XML_NAMESPACE_OOOW == nPrefix )
177 				{
178 					sFormula = sTmp;
179 					bFormulaOK = sal_True;
180 				}
181 				else
182 					sFormula = sAttrValue;
183 			}
184 			break;
185 		case XML_TOK_TEXTFIELD_DISPLAY:
186 			if (IsXMLToken(sAttrValue, XML_FORMULA))
187 			{
188 				bDisplayFormula = sal_True;
189 				bDisplayNone = sal_False;
190 				bDisplayOK = sal_True;
191 			}
192 			else if (IsXMLToken(sAttrValue, XML_VALUE))
193 			{
194 				bDisplayFormula = sal_False;
195 				bDisplayNone = sal_False;
196 				bDisplayOK = sal_True;
197 			}
198 			else if (IsXMLToken(sAttrValue, XML_NONE))
199 			{
200 				bDisplayFormula = sal_False;
201 				bDisplayNone = sal_True;
202 				bDisplayOK = sal_True;
203 			} // else: no change
204 			DBG_ASSERT(!(bDisplayFormula && bDisplayNone),
205 					   "illegal display values");
206 			break;
207 		default:
208 			// delegate all others to value helper
209 			aValueHelper.ProcessAttribute(nAttrToken, sAttrValue);
210 			break;
211 		}
212 }
213 
214 void XMLVarFieldImportContext::PrepareField(
215 	const Reference<XPropertySet> & xPropertySet)
216 {
217 	// bSetName: not implemented
218 
219 	if (bSetFormula)
220 	{
221 		if (!bFormulaOK && bSetFormulaDefault)
222 		{
223 			sFormula = GetContent();
224 			bFormulaOK = sal_True;
225 		}
226 
227 		if (bFormulaOK)
228 		{
229 			Any aAny;
230 			aAny <<= sFormula;
231 			xPropertySet->setPropertyValue(sPropertyContent, aAny);
232 		}
233 	}
234 
235 	if (bSetDescription && bDescriptionOK)
236 	{
237 		Any aAny;
238 		aAny <<= sDescription;
239 		xPropertySet->setPropertyValue(sPropertyHint, aAny);
240 	}
241 
242     if (bSetHelp && bHelpOK)
243     {
244 		Any aAny;
245 		aAny <<= sHelp;
246 		xPropertySet->setPropertyValue(sPropertyHelp, aAny);
247     }
248 
249     if (bSetHint && bHintOK)
250     {
251 		Any aAny;
252 		aAny <<= sHint;
253 		xPropertySet->setPropertyValue(sPropertyTooltip, aAny);
254     }
255 
256 	if (bSetVisible && bDisplayOK)
257 	{
258 		Any aAny;
259 		sal_Bool bTmp = ! (bDisplayNone && bDisplayOK);
260 		aAny.setValue( &bTmp, ::getBooleanCppuType());
261 		xPropertySet->setPropertyValue(sPropertyIsVisible, aAny);
262 	}
263 
264 	// workaround for #no-bug#: display formula by default
265 	if (xPropertySet->getPropertySetInfo()->
266 				hasPropertyByName(sPropertyIsDisplayFormula) &&
267 		!bSetDisplayFormula)
268 	{
269 		bDisplayFormula = sal_False;
270 		bSetDisplayFormula = sal_True;
271 	}
272 
273 
274 	if (bSetDisplayFormula)
275 	{
276 		Any aAny;
277 		sal_Bool bTmp = bDisplayFormula && bDisplayOK;
278 		aAny.setValue( &bTmp, ::getBooleanCppuType());
279 		xPropertySet->setPropertyValue(sPropertyIsDisplayFormula, aAny);
280 	}
281 
282 	// delegate to value helper
283 	aValueHelper.SetDefault(GetContent());
284 	aValueHelper.PrepareField(xPropertySet);
285 
286 	// finally, set the curren presentation
287 	if (bSetPresentation)
288 	{
289 		Any aAny;
290 		aAny <<= GetContent();
291 		xPropertySet->setPropertyValue(sPropertyCurrentPresentation, aAny);
292 	}
293 }
294 
295 
296 
297 
298 
299 //
300 // variable set fields
301 //
302 
303 TYPEINIT1( XMLSetVarFieldImportContext, XMLVarFieldImportContext );
304 
305 XMLSetVarFieldImportContext::XMLSetVarFieldImportContext(
306 	SvXMLImport& rImport, XMLTextImportHelper& rHlp,
307 	const sal_Char* pServiceName, sal_uInt16 nPrfx,
308 	const OUString& rLocalName, VarType eVarType,
309 	sal_Bool bName, sal_Bool bFormula, sal_Bool bFormulaDefault,
310 	sal_Bool bDescription, sal_Bool bHelp, sal_Bool bHint, sal_Bool bVisible, sal_Bool bIsDisplayFormula,
311 	sal_Bool bType, sal_Bool bStyle, sal_Bool bValue, sal_Bool bPresentation) :
312 		XMLVarFieldImportContext(rImport, rHlp, pServiceName,
313 								 nPrfx, rLocalName,
314 								 bName, bFormula, bFormulaDefault,
315 								 bDescription, bHelp, bHint, bVisible, bIsDisplayFormula,
316 								 bType, bStyle, bValue, bPresentation),
317 		eFieldType(eVarType)
318 {
319 }
320 
321 void XMLSetVarFieldImportContext::EndElement()
322 {
323 	// should we call PrepareField on the field, or rather on it's master?
324 	// currently: call on field (just like superclass)
325 	// possible alternatives: call on master
326 	// 						  call field or master depending on variable
327 	//						  PrepareMaster() in addition to PrepareField()
328 
329 	DBG_ASSERT(GetServiceName().getLength()>0, "no service name for element!");
330 
331 	if (bValid)
332 	{
333 		DBG_ASSERT(GetName().getLength()>0, "variable name needed!");
334 
335 		// find field master
336 		Reference<XPropertySet> xMaster;
337 		if (FindFieldMaster(xMaster))
338 		{
339 			// create field/Service
340 			Reference<XPropertySet> xPropSet;
341 			if (CreateField(xPropSet, OUString::createFromAscii(sAPI_textfield_prefix) + GetServiceName()))
342 			{
343 				Reference<XDependentTextField> xDepTextField(xPropSet, UNO_QUERY);
344 				if (xDepTextField.is())
345 				{
346 					// attach field to field master
347 					xDepTextField->attachTextFieldMaster(xMaster);
348 
349 					// attach field to document
350 					Reference<XTextContent> xTextContent(xPropSet, UNO_QUERY);
351 					if (xTextContent.is())
352 					{
353 						try {
354 						// insert, set field properties and exit!
355 						GetImportHelper().InsertTextContent(xTextContent);
356 						PrepareField(xPropSet);
357 						} catch (lang::IllegalArgumentException & /*e*/)
358 						{
359 							// ignore e: #i54023#
360 						};
361 						return;
362 					}
363 				}
364 			}
365 		}
366 	}
367 
368 	// above: exit on success; so for all error cases we end up here!
369 	// write element content
370 	GetImportHelper().InsertString(GetContent());
371 }
372 
373 sal_Bool XMLSetVarFieldImportContext::FindFieldMaster(
374 	Reference<XPropertySet> & xMaster)
375 {
376 	// currently: delegate to XMLVariableDeclImportContext;
377 	// shoud eventually go here
378 	return XMLVariableDeclImportContext::FindFieldMaster(xMaster,
379 														 GetImport(),
380 														 GetImportHelper(),
381 														 GetName(),
382 														 eFieldType);
383 }
384 
385 
386 
387 //
388 // sequence field
389 //
390 
391 TYPEINIT1( XMLSequenceFieldImportContext, XMLSetVarFieldImportContext );
392 
393 XMLSequenceFieldImportContext::XMLSequenceFieldImportContext(
394 	SvXMLImport& rImport, XMLTextImportHelper& rHlp,
395 	sal_uInt16 nPrfx, const OUString& rLocalName) :
396 		XMLSetVarFieldImportContext(rImport, rHlp, sAPI_set_expression,
397 									nPrfx, rLocalName, VarTypeSequence,
398 									// name, formula
399 									sal_True, sal_True, sal_True,
400 									sal_False, sal_False, sal_False, sal_False,
401                                     sal_False,
402 									sal_False, sal_False, sal_False, sal_True),
403 
404 		sPropertyNumberFormat(RTL_CONSTASCII_USTRINGPARAM(sAPI_number_format)),
405 		sPropertySequenceValue(RTL_CONSTASCII_USTRINGPARAM(sAPI_sequence_value)),
406 		sNumFormat(OUString::valueOf(sal_Unicode('1'))),
407 		sNumFormatSync(GetXMLToken(XML_FALSE)),
408 		bRefNameOK(sal_False)
409 {
410 }
411 
412 void XMLSequenceFieldImportContext::ProcessAttribute(
413 	sal_uInt16 nAttrToken, const OUString& sAttrValue )
414 {
415 	switch (nAttrToken)
416 	{
417 		case XML_TOK_TEXTFIELD_NUM_FORMAT:
418 			sNumFormat = sAttrValue;
419 			break;
420 		case XML_TOK_TEXTFIELD_NUM_LETTER_SYNC:
421 			sNumFormatSync = sAttrValue;
422 			break;
423 		case XML_TOK_TEXTFIELD_REF_NAME:
424 			sRefName = sAttrValue;
425 			bRefNameOK = sal_True;
426 			break;
427 		default:
428 			// delegate to super class (name, formula)
429 			XMLSetVarFieldImportContext::ProcessAttribute(nAttrToken,
430 														  sAttrValue);
431 			break;
432 	} // switch
433 }
434 
435 void XMLSequenceFieldImportContext::PrepareField(
436 	const Reference<XPropertySet> & xPropertySet)
437 {
438 	// delegate to super class (formula)
439 	XMLSetVarFieldImportContext::PrepareField(xPropertySet);
440 
441 	// set format
442 	sal_Int16 nNumType = NumberingType::ARABIC;
443 	GetImport().GetMM100UnitConverter().convertNumFormat( nNumType, sNumFormat, sNumFormatSync );
444 	Any aAny;
445 	aAny <<= nNumType;
446 	xPropertySet->setPropertyValue(sPropertyNumberFormat, aAny);
447 
448 	// handle reference name
449 	if (bRefNameOK)
450 	{
451 		aAny = xPropertySet->getPropertyValue(sPropertySequenceValue);
452 		sal_Int16 nValue = 0;
453 		aAny >>= nValue;
454 		GetImportHelper().InsertSequenceID(sRefName, GetName(), nValue);
455 	}
456 }
457 
458 
459 
460 //
461 // variable set field
462 //
463 
464 TYPEINIT1( XMLVariableSetFieldImportContext, XMLSetVarFieldImportContext );
465 
466 XMLVariableSetFieldImportContext::XMLVariableSetFieldImportContext(
467 	SvXMLImport& rImport, XMLTextImportHelper& rHlp,
468 	sal_uInt16 nPrfx, const OUString& rLocalName) :
469 		XMLSetVarFieldImportContext(rImport, rHlp, sAPI_set_expression,
470 									nPrfx, rLocalName, VarTypeSimple,
471 									// name, formula, value&type, style,
472 									// display none
473 									sal_True, sal_True, sal_True,
474 									sal_False, sal_False, sal_False,
475                                     sal_True, sal_False,
476 									sal_True, sal_True, sal_True,
477 									sal_True),
478 		sPropertySubType(RTL_CONSTASCII_USTRINGPARAM(sAPI_sub_type))
479 {
480 }
481 
482 void XMLVariableSetFieldImportContext::PrepareField(
483 		const Reference<XPropertySet> & xPropertySet)
484 {
485 	// set type
486 	Any aAny;
487 	aAny <<= (IsStringValue()? SetVariableType::STRING : SetVariableType::VAR);
488 	xPropertySet->setPropertyValue(sPropertySubType, aAny);
489 
490 	// the remainder is handled by super class
491 	XMLSetVarFieldImportContext::PrepareField(xPropertySet);
492 }
493 
494 
495 
496 //
497 // variable input field
498 //
499 
500 TYPEINIT1( XMLVariableInputFieldImportContext, XMLSetVarFieldImportContext );
501 
502 XMLVariableInputFieldImportContext::XMLVariableInputFieldImportContext(
503 	SvXMLImport& rImport, XMLTextImportHelper& rHlp, sal_uInt16 nPrfx,
504 	const OUString& rLocalName) :
505 		XMLSetVarFieldImportContext(rImport, rHlp, sAPI_set_expression,
506 									nPrfx, rLocalName, VarTypeSimple,
507 									// name, description, display none/formula,
508 									// value&type, style, formula
509 									sal_True, sal_True, sal_True,
510 									sal_True, sal_True, sal_True,
511                                     sal_True, sal_False,
512 									sal_True, sal_True, sal_True,
513 									sal_True),
514 		sPropertySubType(RTL_CONSTASCII_USTRINGPARAM(sAPI_sub_type)),
515 		sPropertyIsInput(RTL_CONSTASCII_USTRINGPARAM(sAPI_is_input))
516 {
517 }
518 
519 void XMLVariableInputFieldImportContext::PrepareField(
520 		const Reference<XPropertySet> & xPropertySet)
521 {
522 	// set type (input field)
523 	Any aAny;
524 	sal_Bool bTrue = sal_True;
525 	aAny.setValue( &bTrue, ::getBooleanCppuType() );
526 	xPropertySet->setPropertyValue(sPropertyIsInput, aAny);
527 
528 	// set type
529 	aAny <<= (IsStringValue()? SetVariableType::STRING : SetVariableType::VAR);
530 	xPropertySet->setPropertyValue(sPropertySubType, aAny);
531 
532 	// the remainder is handled by super class
533 	XMLSetVarFieldImportContext::PrepareField(xPropertySet);
534 }
535 
536 
537 
538 //
539 // user field
540 //
541 
542 TYPEINIT1( XMLUserFieldImportContext, XMLSetVarFieldImportContext );
543 
544 XMLUserFieldImportContext::XMLUserFieldImportContext(
545 	SvXMLImport& rImport, XMLTextImportHelper& rHlp,
546 	sal_uInt16 nPrfx, const OUString& rLocalName) :
547 		XMLSetVarFieldImportContext(rImport, rHlp, sAPI_user, nPrfx,
548 									rLocalName, VarTypeUserField,
549 									// name, display none/formula, style
550 									sal_True, sal_False, sal_False,
551 									sal_False, sal_False, sal_False, sal_True,
552                                     sal_True,
553 									sal_False, sal_True, sal_False,
554 									sal_False)
555 {
556 }
557 
558 
559 
560 //
561 // user input field
562 //
563 
564 TYPEINIT1( XMLUserFieldInputImportContext, XMLVarFieldImportContext );
565 
566 // bug: doesn't work (SO API lacking)
567 XMLUserFieldInputImportContext::XMLUserFieldInputImportContext(
568 	SvXMLImport& rImport, XMLTextImportHelper& rHlp, sal_uInt16 nPrfx,
569 	const OUString& rLocalName) :
570 		XMLVarFieldImportContext(rImport, rHlp, sAPI_input_user,
571 								 nPrfx, rLocalName,
572 								 // name, description, style
573 								 sal_True, sal_False, sal_False,
574                                  sal_True, sal_False, sal_False,
575 								 sal_False, sal_False,
576 								 sal_False /*???*/, sal_True, sal_False,
577 								 sal_False)
578 {
579 }
580 
581 void XMLUserFieldInputImportContext::PrepareField(
582 	const Reference<XPropertySet> & xPropertySet)
583 {
584 	Any aAny;
585 	aAny <<= GetName();
586 	xPropertySet->setPropertyValue(sPropertyContent, aAny);
587 
588 	// delegate to super class
589 	XMLVarFieldImportContext::PrepareField(xPropertySet);
590 }
591 
592 
593 //
594 // variable get field
595 //
596 
597 TYPEINIT1( XMLVariableGetFieldImportContext, XMLVarFieldImportContext );
598 
599 XMLVariableGetFieldImportContext::XMLVariableGetFieldImportContext(
600 	SvXMLImport& rImport, XMLTextImportHelper& rHlp,
601 	sal_uInt16 nPrfx, const OUString& rLocalName) :
602 		XMLVarFieldImportContext(rImport, rHlp, sAPI_get_expression,
603 								 nPrfx, rLocalName,
604 								 // name, style, display formula
605 								 sal_True, sal_False, sal_False,
606 								 sal_False, sal_False, sal_False,
607                                  sal_False, sal_True,
608 								 sal_True, sal_True, sal_False,
609 								 sal_True),
610 		sPropertySubType(RTL_CONSTASCII_USTRINGPARAM(sAPI_sub_type))
611 {
612 }
613 
614 void XMLVariableGetFieldImportContext::PrepareField(
615 		const Reference<XPropertySet> & xPropertySet)
616 {
617 	// set name
618   	Any aAny;
619 	aAny <<= GetName();
620 	xPropertySet->setPropertyValue(sPropertyContent, aAny);
621 
622 	// the remainder is handled by super class
623 	XMLVarFieldImportContext::PrepareField(xPropertySet);
624 }
625 
626 
627 
628 //
629 // expression field
630 //
631 
632 TYPEINIT1( XMLExpressionFieldImportContext, XMLVarFieldImportContext );
633 
634 XMLExpressionFieldImportContext::XMLExpressionFieldImportContext(
635 	SvXMLImport& rImport, XMLTextImportHelper& rHlp,
636 	sal_uInt16 nPrfx, const OUString& rLocalName) :
637 		XMLVarFieldImportContext(rImport, rHlp, sAPI_get_expression,
638 								 nPrfx, rLocalName,
639 								 // formula, type, style, display formula
640 								 sal_False, sal_True, sal_True,
641 								 sal_False, sal_False, sal_False,
642                                  sal_False, sal_True,
643 								 sal_True, sal_True, sal_False,
644 								 sal_True),
645 		sPropertySubType(RTL_CONSTASCII_USTRINGPARAM(sAPI_sub_type))
646 {
647 	bValid = sal_True;	// always valid
648 }
649 
650 
651 void XMLExpressionFieldImportContext::PrepareField(
652 	const Reference<XPropertySet> & xPropertySet)
653 {
654 	sal_Int16 nSubType = SetVariableType::FORMULA;
655 	Any aAny;
656 	aAny <<= nSubType;
657 	xPropertySet->setPropertyValue(sPropertySubType, aAny);
658 
659 	// delegate to super class
660 	XMLVarFieldImportContext::PrepareField(xPropertySet);
661 }
662 
663 
664 
665 //
666 // text input field
667 //
668 
669 TYPEINIT1( XMLTextInputFieldImportContext, XMLVarFieldImportContext );
670 
671 XMLTextInputFieldImportContext::XMLTextInputFieldImportContext(
672 	SvXMLImport& rImport, XMLTextImportHelper& rHlp,
673 	sal_uInt16 nPrfx, const OUString& sLocalName) :
674 		XMLVarFieldImportContext(rImport, rHlp, sAPI_input,
675 								 nPrfx, sLocalName,
676 								 // description
677 								 sal_False, sal_False, sal_False,
678 								 sal_True, sal_True, sal_True,
679                                  sal_False, sal_False,
680 								 sal_False, sal_False, sal_False,
681 								 sal_False),
682 		sPropertyContent(RTL_CONSTASCII_USTRINGPARAM(sAPI_content))
683 {
684 	bValid = sal_True;	// always valid
685 }
686 
687 void XMLTextInputFieldImportContext::PrepareField(
688 	const Reference<XPropertySet> & xPropertySet)
689 {
690 	XMLVarFieldImportContext::PrepareField(xPropertySet);
691 
692 	Any aAny;
693 	aAny <<= GetContent();
694 	xPropertySet->setPropertyValue(sPropertyContent, aAny);
695 }
696 
697 
698 //
699 // table formula field
700 //
701 
702 TYPEINIT1( XMLTableFormulaImportContext, XMLTextFieldImportContext );
703 
704 XMLTableFormulaImportContext::XMLTableFormulaImportContext(
705     SvXMLImport& rImport,
706     XMLTextImportHelper& rHlp,
707     sal_uInt16 nPrfx,
708     const OUString& rLocalName) :
709         XMLTextFieldImportContext(rImport, rHlp, sAPI_table_formula,
710                                   nPrfx, rLocalName),
711         sPropertyIsShowFormula(RTL_CONSTASCII_USTRINGPARAM("IsShowFormula")),
712         sPropertyCurrentPresentation(
713             RTL_CONSTASCII_USTRINGPARAM("CurrentPresentation")),
714         aValueHelper(rImport, rHlp, sal_False, sal_True, sal_False, sal_True),
715         sFormula(),
716         bIsShowFormula(sal_False)
717 {
718 }
719 
720 XMLTableFormulaImportContext::~XMLTableFormulaImportContext()
721 {
722 }
723 
724 void XMLTableFormulaImportContext::ProcessAttribute(
725     sal_uInt16 nAttrToken,
726     const OUString& sAttrValue )
727 {
728     switch (nAttrToken)
729     {
730         case XML_TOK_TEXTFIELD_FORMULA:
731             aValueHelper.ProcessAttribute( nAttrToken, sAttrValue );
732             bValid = sal_True;  // we need a formula!
733             break;
734 
735         case XML_TOK_TEXTFIELD_DATA_STYLE_NAME:
736             aValueHelper.ProcessAttribute( nAttrToken, sAttrValue );
737             break;
738         case XML_TOK_TEXTFIELD_DISPLAY:
739             if ( sAttrValue.equalsAsciiL(
740                 RTL_CONSTASCII_STRINGPARAM("formula")) )
741                  bIsShowFormula = sal_True;
742             break;
743         default:
744             // unknown attribute -> ignore
745             break;
746     }
747 }
748 
749 void XMLTableFormulaImportContext::PrepareField(
750     const Reference<XPropertySet> & xPropertySet)
751 {
752     // set format and formula
753     aValueHelper.PrepareField( xPropertySet );
754 
755     Any aAny;
756 
757     // set 'show formula' and presentation
758     aAny.setValue( &bIsShowFormula, ::getBooleanCppuType() );
759     xPropertySet->setPropertyValue( sPropertyIsShowFormula, aAny );
760 
761 	aAny <<= GetContent();
762 	xPropertySet->setPropertyValue( sPropertyCurrentPresentation, aAny );
763 }
764 
765 
766 
767 //
768 // variable declarations
769 //
770 // Should be adapted to XMLVarField-/XMLSetVarFieldImportContext scheme!
771 //
772 
773 
774 
775 //
776 // declaration containter import (<variable/user-field/sequence-decls>)
777 //
778 
779 TYPEINIT1( XMLVariableDeclsImportContext, SvXMLImportContext );
780 
781 XMLVariableDeclsImportContext::XMLVariableDeclsImportContext(
782 	SvXMLImport& rImport, XMLTextImportHelper& rHlp, sal_uInt16 nPrfx,
783 	const OUString& rLocalName,	enum VarType eVarType) :
784 		SvXMLImportContext(rImport, nPrfx, rLocalName),
785 		eVarDeclsContextType(eVarType),
786 		rImportHelper(rHlp)
787 {
788 }
789 
790 SvXMLImportContext* XMLVariableDeclsImportContext::CreateChildContext(
791 	sal_uInt16 nPrefix, const OUString& rLocalName,
792 	const Reference<xml::sax::XAttributeList> & xAttrList )
793 {
794 	enum XMLTokenEnum eElementName;
795 	SvXMLImportContext* pImportContext = NULL;
796 
797 	if( XML_NAMESPACE_TEXT == nPrefix )
798     {
799 		switch (eVarDeclsContextType)
800         {
801             case VarTypeSequence:
802                 eElementName = XML_SEQUENCE_DECL;
803                 break;
804             case VarTypeSimple:
805                 eElementName = XML_VARIABLE_DECL;
806                 break;
807             case VarTypeUserField:
808                 eElementName = XML_USER_FIELD_DECL;
809                 break;
810             default:
811                 DBG_ERROR("unknown field type!");
812                 eElementName = XML_SEQUENCE_DECL;
813 				break;
814         }
815 
816 		if( IsXMLToken( rLocalName, eElementName ) )
817         {
818 			pImportContext = new XMLVariableDeclImportContext(
819 				GetImport(), rImportHelper, nPrefix, rLocalName, xAttrList,
820 				eVarDeclsContextType);
821 		}
822 	}
823 
824 	// if no context was created, use default context
825 	if (NULL == pImportContext) {
826 		pImportContext = SvXMLImportContext::CreateChildContext(nPrefix,
827 																rLocalName,
828 																xAttrList);
829 	}
830 
831 	return pImportContext;
832 }
833 
834 
835 
836 //
837 // declaration import (<variable/user-field/sequence-decl> elements)
838 //
839 
840 TYPEINIT1( XMLVariableDeclImportContext, SvXMLImportContext );
841 
842 XMLVariableDeclImportContext::XMLVariableDeclImportContext(
843 	SvXMLImport& rImport, XMLTextImportHelper& rHlp,
844 	sal_uInt16 nPrfx, const OUString& rLocalName,
845 	const Reference<xml::sax::XAttributeList> & xAttrList,
846 	enum VarType eVarType) :
847 		SvXMLImportContext(rImport, nPrfx, rLocalName),
848 		// bug?? which properties for userfield/userfieldmaster
849 		sPropertyName(RTL_CONSTASCII_USTRINGPARAM(sAPI_name)),
850 		sPropertySubType(RTL_CONSTASCII_USTRINGPARAM(sAPI_sub_type)),
851 		sPropertyNumberingLevel(RTL_CONSTASCII_USTRINGPARAM(sAPI_chapter_numbering_level)),
852 		sPropertyNumberingSeparator(RTL_CONSTASCII_USTRINGPARAM(sAPI_numbering_separator)),
853 		sPropertyIsExpression(RTL_CONSTASCII_USTRINGPARAM(sAPI_is_expression)),
854 		aValueHelper(rImport, rHlp, sal_True, sal_False, sal_True, sal_False),
855 		nNumLevel(-1), cSeparationChar('.')
856 {
857 	if ( (XML_NAMESPACE_TEXT == nPrfx) &&
858 		 ( ( IsXMLToken( rLocalName, XML_SEQUENCE_DECL )) ||
859 		   ( IsXMLToken( rLocalName, XML_VARIABLE_DECL)) ||
860 		   ( IsXMLToken( rLocalName, XML_USER_FIELD_DECL))    )) {
861 
862 		// TODO: check validity (need name!)
863 
864 		// parse attributes
865 		sal_Int16 nLength = xAttrList->getLength();
866 		for(sal_Int16 i=0; i<nLength; i++) {
867 
868 			OUString sLocalName;
869 			sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
870 				GetKeyByAttrName( xAttrList->getNameByIndex(i), &sLocalName );
871 
872 			sal_uInt16 nToken = rHlp.
873 				GetTextFieldAttrTokenMap().Get(nPrefix, sLocalName);
874 
875 			switch (nToken)
876 			{
877 				case XML_TOK_TEXTFIELD_NAME:
878 					sName = xAttrList->getValueByIndex(i);
879 					break;
880 				case XML_TOK_TEXTFIELD_NUMBERING_LEVEL:
881 				{
882 					sal_Int32 nLevel;
883 					sal_Bool bRet = SvXMLUnitConverter::convertNumber(
884 						nLevel, xAttrList->getValueByIndex(i), 0,
885 						GetImport().GetTextImport()->GetChapterNumbering()->
886 															   	getCount());
887 					if (bRet)
888 					{
889 						nNumLevel = static_cast< sal_Int8 >( nLevel-1 ); // API numbers -1..9
890 					}
891 					break;
892 				}
893 				case XML_TOK_TEXTFIELD_NUMBERING_SEPARATOR:
894 					cSeparationChar =
895 						(sal_Char)xAttrList->getValueByIndex(i).toChar();
896 					break;
897 
898 				default:
899 					// delegate to value helper
900 					aValueHelper.ProcessAttribute(nToken,
901 												  xAttrList->getValueByIndex(i));
902 					break;
903 			}
904 		}
905 
906 		Reference<XPropertySet> xFieldMaster;
907 		if (FindFieldMaster(xFieldMaster, GetImport(), rHlp,
908 							sName, eVarType))
909 		{
910 			// now we have a field master: process attributes!
911 			Any aAny;
912 
913 			switch (eVarType)
914 			{
915 			case VarTypeSequence:
916 				aAny <<= nNumLevel;
917 				xFieldMaster->setPropertyValue(sPropertyNumberingLevel, aAny);
918 
919 				if (nNumLevel >= 0)
920 				{
921 					OUString sStr(&cSeparationChar, 1);
922 					aAny <<= sStr;
923 					xFieldMaster->setPropertyValue(
924 						sPropertyNumberingSeparator, aAny);
925 				}
926 				break;
927 			case VarTypeSimple:
928                 {
929                     // set string or non-string SubType (#93192#)
930                     // The SubType was already set in the FindFieldMaster
931                     // method, but it needs to be adjusted if it's a string.
932                     aAny <<= aValueHelper.IsStringValue()
933                         ? SetVariableType::STRING : SetVariableType::VAR;
934                     xFieldMaster->setPropertyValue(sPropertySubType, aAny);
935                 }
936                 break;
937 			case VarTypeUserField:
938 			{
939 				sal_Bool bTmp = !aValueHelper.IsStringValue();
940 				aAny.setValue(&bTmp, ::getBooleanCppuType());
941 				xFieldMaster->setPropertyValue(sPropertyIsExpression, aAny);
942 				aValueHelper.PrepareField(xFieldMaster);
943 				break;
944 			}
945 			default:
946 				DBG_ERROR("unkown varfield type");
947 			} // switch
948 		} // else: no field master found/constructed
949 	} // else: no sequence-decl
950 }
951 
952 
953 
954 sal_Bool XMLVariableDeclImportContext::FindFieldMaster(
955 	Reference<XPropertySet> & xMaster, SvXMLImport& rImport,
956 	XMLTextImportHelper& rImportHelper,
957 	const OUString& sVarName, enum VarType eVarType)
958 {
959 	static sal_Int32 nCollisionCount = 0;
960 
961 	// rename field
962 	// currently: no family in use! Use 0.
963 	OUString sName = rImportHelper.GetRenameMap().Get(
964         sal::static_int_cast< sal_uInt16 >(eVarType), sVarName);
965 
966 	// get text fields supplier and field masters
967 	Reference<XTextFieldsSupplier> xTextFieldsSupp(rImport.GetModel(),
968 												   UNO_QUERY);
969 	Reference<container::XNameAccess> xFieldMasterNameAccess(
970 		xTextFieldsSupp->getTextFieldMasters(), UNO_QUERY);
971 
972 	OUStringBuffer sBuffer;
973 	sBuffer.appendAscii(sAPI_fieldmaster_prefix);
974 	sBuffer.appendAscii(sAPI_set_expression);
975 	sBuffer.appendAscii(".");
976 	sBuffer.append(sName);
977 	OUString sVarServiceName = sBuffer.makeStringAndClear();
978 
979 	sBuffer.appendAscii(sAPI_fieldmaster_prefix);
980 	sBuffer.appendAscii(sAPI_user);
981 	sBuffer.appendAscii(".");
982 	sBuffer.append(sName);
983 	OUString sUserServiceName = sBuffer.makeStringAndClear();
984 
985 	if (xFieldMasterNameAccess->hasByName(sVarServiceName)) {
986 		// variable field master already in document
987 
988 		Any aAny = xFieldMasterNameAccess->getByName(sVarServiceName);
989 		aAny >>= xMaster;
990 
991 		aAny = xMaster->getPropertyValue(
992 			// sPropertySubType
993 			OUString::createFromAscii(sAPI_sub_type)
994 			);
995 		sal_Int16 nType = 0;
996 		aAny >>= nType;
997 
998 		enum VarType eFMVarType =
999 			(SetVariableType::SEQUENCE == nType) ?
1000 				VarTypeSequence : VarTypeSimple;
1001 
1002 		if (eFMVarType != eVarType)
1003 		{
1004 			OUString sNew;
1005 
1006 			// FIXME! cant find if name is taken already!!!!
1007 
1008 			nCollisionCount++;
1009 			OUStringBuffer aBuf;
1010 			aBuf.append(sName);
1011 			aBuf.appendAscii("_renamed_");
1012 			aBuf.append(nCollisionCount);
1013 			sNew = aBuf.makeStringAndClear();
1014 
1015 			rImportHelper.GetRenameMap().Add(
1016                 sal::static_int_cast< sal_uInt16 >(eVarType), sName, sNew);
1017 
1018 			// call FindFieldMaster recursively to create new master
1019 			return FindFieldMaster(xMaster, rImport, rImportHelper,
1020 								   sNew, eVarType);
1021 		}
1022 	} else if (xFieldMasterNameAccess->hasByName(sUserServiceName)) {
1023 		// user field: get field master
1024 		Any aAny = xFieldMasterNameAccess->getByName(sUserServiceName);
1025 		aAny >>= xMaster;
1026 
1027 		if (VarTypeUserField != eVarType) {
1028 			// find new name that is not taken
1029 			OUString sNew;
1030 
1031 			// FIXME! cant find if name is taken already!!!!
1032 
1033 			nCollisionCount++;
1034 			OUStringBuffer aBuf;
1035 			aBuf.append(sName);
1036 			aBuf.appendAscii("_renamed_");
1037 			aBuf.append(nCollisionCount);
1038 			sNew = aBuf.makeStringAndClear();
1039 
1040 			rImportHelper.GetRenameMap().Add(
1041                 sal::static_int_cast< sal_uInt16 >(eVarType), sName, sNew);
1042 
1043 			// call FindFieldMaster recursively to create new master
1044 			return FindFieldMaster(xMaster, rImport, rImportHelper,
1045 								   sNew, eVarType);
1046 		}
1047 	} else {
1048 		// field name not used: create field master
1049 
1050 		// import -> model is MultiServiceFactory -> createInstance
1051 		Reference<lang::XMultiServiceFactory>
1052 			xFactory(rImport.GetModel(),UNO_QUERY);
1053 		if( xFactory.is() )	{
1054 
1055 			OUStringBuffer sService;
1056 			sService.appendAscii(sAPI_fieldmaster_prefix);
1057 			sService.appendAscii((eVarType==VarTypeUserField) ?
1058 								 sAPI_user : sAPI_set_expression);
1059 			Reference<XInterface> xIfc =
1060 				xFactory->createInstance( sService.makeStringAndClear() );
1061 			if (xIfc.is()) {
1062 				Reference<XPropertySet> xTmp( xIfc, UNO_QUERY );
1063 				xMaster = xTmp;
1064 
1065 				// set name
1066 				Any aAny;
1067 				aAny <<= sName;
1068 				xMaster->setPropertyValue(
1069 					// sPropertyName
1070 					OUString::createFromAscii(sAPI_name)
1071 					, aAny);
1072 
1073 				if (eVarType != VarTypeUserField) {
1074 					// set subtype for setexp field
1075 
1076 					aAny <<= ((eVarType == VarTypeSimple) ?
1077 							  SetVariableType::VAR :
1078 							  SetVariableType::SEQUENCE);
1079 					xMaster->setPropertyValue(
1080 						// sPropertySubType
1081 						OUString::createFromAscii(sAPI_sub_type)
1082 						, aAny);
1083 				} // else : user field: no subtype
1084 
1085 			} else {
1086 				return sal_False;
1087 			}
1088 		} else {
1089 			return sal_False;
1090 		}
1091 	}
1092 
1093 	DBG_ASSERT(xMaster.is(), "no field master found!?!");
1094 	return sal_True;
1095 }
1096 
1097 
1098 //
1099 // Database Display field import
1100 //
1101 
1102 
1103 TYPEINIT1( XMLDatabaseDisplayImportContext, XMLDatabaseFieldImportContext );
1104 
1105 XMLDatabaseDisplayImportContext::XMLDatabaseDisplayImportContext(
1106 	SvXMLImport& rImport, XMLTextImportHelper& rHlp, sal_uInt16 nPrfx,
1107 	const OUString& rLocalName) :
1108 		XMLDatabaseFieldImportContext(rImport, rHlp, sAPI_database,
1109 									  nPrfx, rLocalName, false),
1110 		sPropertyColumnName(RTL_CONSTASCII_USTRINGPARAM(sAPI_data_column_name)),
1111 		sPropertyDatabaseFormat(RTL_CONSTASCII_USTRINGPARAM(sAPI_is_data_base_format)),
1112 		sPropertyCurrentPresentation(RTL_CONSTASCII_USTRINGPARAM(sAPI_current_presentation)),
1113         sPropertyIsVisible(RTL_CONSTASCII_USTRINGPARAM(sAPI_is_visible)),
1114 		aValueHelper(rImport, rHlp, sal_False, sal_True, sal_False, sal_False),
1115 		bColumnOK(sal_False),
1116         bDisplay( sal_True ),
1117         bDisplayOK( sal_False )
1118 {
1119 }
1120 
1121 void XMLDatabaseDisplayImportContext::ProcessAttribute(
1122 	sal_uInt16 nAttrToken, const OUString& sAttrValue )
1123 {
1124 	switch (nAttrToken)
1125 	{
1126 		case XML_TOK_TEXTFIELD_COLUMN_NAME:
1127 			sColumnName = sAttrValue;
1128 			bColumnOK = sal_True;
1129 			break;
1130         case XML_TOK_TEXTFIELD_DISPLAY:
1131             {
1132                 sal_Bool bNone = IsXMLToken( sAttrValue, XML_NONE );
1133                 sal_Bool bValue = IsXMLToken( sAttrValue, XML_VALUE );
1134                 bDisplay = bValue;
1135                 bDisplayOK = bNone || bValue;
1136             }
1137             break;
1138 		case XML_TOK_TEXTFIELD_DATABASE_NAME:
1139 		case XML_TOK_TEXTFIELD_TABLE_NAME:
1140         case XML_TOK_TEXTFIELD_TABLE_TYPE:
1141 			// handled by super class
1142 			XMLDatabaseFieldImportContext::ProcessAttribute(nAttrToken,
1143 															sAttrValue);
1144 			break;
1145 		default:
1146 			// remainder handled by value helper
1147 			aValueHelper.ProcessAttribute(nAttrToken, sAttrValue);
1148 			break;
1149 	}
1150 
1151 	bValid = bTableOK && bDatabaseOK && bColumnOK;
1152 }
1153 
1154 void XMLDatabaseDisplayImportContext::EndElement()
1155 {
1156 	// we have an EndElement of our own, because database fields need
1157 	// to be attached to a field master before they can be inserted into
1158 	// the document. Database stuff (database, table, column) all goes
1159 	// to the field master, value & style go to the field.
1160 
1161 	if (bValid)
1162 	{
1163 
1164 		// so here goes: we start with the master
1165 		Reference<XPropertySet> xMaster;
1166 
1167 		// create and prepare field master first
1168 		if (CreateField(xMaster,
1169 						OUString(RTL_CONSTASCII_USTRINGPARAM(
1170 							sAPI_fieldmaster_database))))
1171 		{
1172 			Any aAny;
1173 			aAny <<= sColumnName;
1174 			xMaster->setPropertyValue(sPropertyColumnName, aAny);
1175 
1176 			// fieldmaster takes database, table and column name
1177 			XMLDatabaseFieldImportContext::PrepareField(xMaster);
1178 
1179 			// create field
1180 			Reference<XPropertySet> xField;
1181 			if (CreateField(xField,
1182 							OUString(RTL_CONSTASCII_USTRINGPARAM(
1183 								sAPI_database))))
1184 			{
1185 				// attach field master
1186 				Reference<XDependentTextField> xDepField(xField, UNO_QUERY);
1187 				if (xDepField.is())
1188 				{
1189 					// attach field to field master
1190 					xDepField->attachTextFieldMaster(xMaster);
1191 
1192 					// attach field to document
1193 					Reference<XTextContent> xTextContent(xField, UNO_QUERY);
1194 					if (xTextContent.is())
1195 					{
1196 						// insert, set field properties and exit!
1197 						GetImportHelper().InsertTextContent(xTextContent);
1198 
1199 						// prepare field: format from database?
1200 						sal_Bool bTmp = !aValueHelper.IsFormatOK();
1201 						aAny.setValue( &bTmp, ::getBooleanCppuType() );
1202 						xField->setPropertyValue(sPropertyDatabaseFormat,aAny);
1203 
1204 						// value, value-type and format done by value helper
1205 						aValueHelper.PrepareField(xField);
1206 
1207                         // visibility
1208                         if( bDisplayOK )
1209                         {
1210                             aAny.setValue( &bDisplay, ::getBooleanCppuType() );
1211                             xField->setPropertyValue(sPropertyIsVisible, aAny);
1212                         }
1213 
1214 						// set presentation
1215 						aAny <<= GetContent();
1216 						xField->setPropertyValue(sPropertyCurrentPresentation,
1217 													aAny);
1218 
1219 						// success!
1220 						return;
1221 					}
1222 				}
1223 			}
1224 		}
1225 	}
1226 
1227 	// above: exit on success; so for all error cases we end up here!
1228 	// write element content
1229 	GetImportHelper().InsertString(GetContent());
1230 }
1231 
1232 
1233 //
1234 // value import helper
1235 //
1236 
1237 enum ValueType
1238 {
1239 	XML_VALUE_TYPE_STRING,
1240 	XML_VALUE_TYPE_FLOAT,
1241 	XML_VALUE_TYPE_CURRENCY,
1242 	XML_VALUE_TYPE_PERCENTAGE,
1243 	XML_VALUE_TYPE_DATE,
1244 	XML_VALUE_TYPE_TIME,
1245 	XML_VALUE_TYPE_BOOLEAN
1246 };
1247 
1248 static SvXMLEnumMapEntry __READONLY_DATA aValueTypeMap[] =
1249 {
1250 	{ XML_FLOAT, 		XML_VALUE_TYPE_FLOAT },
1251 	{ XML_CURRENCY, 	XML_VALUE_TYPE_CURRENCY },
1252 	{ XML_PERCENTAGE, 	XML_VALUE_TYPE_PERCENTAGE },
1253 	{ XML_DATE, 		XML_VALUE_TYPE_DATE },
1254 	{ XML_TIME, 		XML_VALUE_TYPE_TIME },
1255 	{ XML_BOOLEAN, 	    XML_VALUE_TYPE_BOOLEAN },
1256 	{ XML_STRING, 		XML_VALUE_TYPE_STRING },
1257     { XML_TOKEN_INVALID, 0 }
1258 };
1259 
1260 XMLValueImportHelper::XMLValueImportHelper(
1261 	SvXMLImport& rImprt,
1262 	XMLTextImportHelper& rHlp,
1263 	sal_Bool bType, sal_Bool bStyle, sal_Bool bValue, sal_Bool bFormula) :
1264 		sPropertyContent(RTL_CONSTASCII_USTRINGPARAM(sAPI_content)),
1265 		sPropertyValue(RTL_CONSTASCII_USTRINGPARAM(sAPI_value)),
1266 		sPropertyNumberFormat(RTL_CONSTASCII_USTRINGPARAM(sAPI_number_format)),
1267         sPropertyIsFixedLanguage(RTL_CONSTASCII_USTRINGPARAM(sAPI_is_fixed_language)),
1268 
1269 		rImport(rImprt),
1270 		rHelper(rHlp),
1271 
1272 		fValue(0.0),
1273 		nFormatKey(0),
1274         bIsDefaultLanguage(sal_True),
1275 
1276 		bStringType(sal_False),
1277 		bFormatOK(sal_False),
1278 		bTypeOK(sal_False),
1279 		bStringValueOK(sal_False),
1280 		bFloatValueOK(sal_False),
1281 		bFormulaOK(sal_False),
1282 
1283 		bSetType(bType),
1284 		bSetValue(bValue),
1285 		bSetStyle(bStyle),
1286 		bSetFormula(bFormula),
1287 
1288 		bStringDefault(sal_True),
1289 		bFormulaDefault(sal_True)
1290 {
1291 }
1292 
1293 XMLValueImportHelper::~XMLValueImportHelper()
1294 {
1295 }
1296 
1297 void XMLValueImportHelper::ProcessAttribute(
1298 	sal_uInt16 nAttrToken, const OUString& sAttrValue )
1299 {
1300 	switch (nAttrToken)
1301 	{
1302 		case XML_TOK_TEXTFIELD_VALUE_TYPE:
1303 		{
1304 			// convert enum
1305 			sal_uInt16 nTmp = 0;
1306 			sal_Bool bRet = SvXMLUnitConverter::convertEnum(
1307 				nTmp, sAttrValue, aValueTypeMap);
1308 
1309 			if (bRet) {
1310 				ValueType eValueType = (ValueType)nTmp;
1311 
1312 				bTypeOK = sal_True;
1313 
1314 				switch (eValueType)
1315 				{
1316 					case XML_VALUE_TYPE_STRING:
1317 						bStringType = sal_True;
1318 						break;
1319 					case XML_VALUE_TYPE_FLOAT:
1320 					case XML_VALUE_TYPE_CURRENCY:
1321 					case XML_VALUE_TYPE_PERCENTAGE:
1322 					case XML_VALUE_TYPE_DATE:
1323 					case XML_VALUE_TYPE_TIME:
1324 					case XML_VALUE_TYPE_BOOLEAN:
1325 						bStringType = sal_False;
1326 						break;
1327 
1328 					default:
1329 						DBG_ERROR("unknown value type");
1330 						bTypeOK = sal_False;
1331 				}
1332 			}
1333 			break;
1334 		}
1335 
1336 		case XML_TOK_TEXTFIELD_VALUE:
1337 		{
1338 			double fTmp;
1339 			sal_Bool bRet = SvXMLUnitConverter::convertDouble(fTmp,sAttrValue);
1340 			if (bRet) {
1341 				bFloatValueOK = sal_True;
1342 				fValue = fTmp;
1343 			}
1344 			break;
1345 		}
1346 
1347 		case XML_TOK_TEXTFIELD_TIME_VALUE:
1348 		{
1349 			double fTmp;
1350 			sal_Bool bRet = SvXMLUnitConverter::convertTime(fTmp,sAttrValue);
1351 			if (bRet) {
1352 				bFloatValueOK = sal_True;
1353 				fValue = fTmp;
1354 			}
1355 			break;
1356 		}
1357 
1358 		case XML_TOK_TEXTFIELD_DATE_VALUE:
1359 		{
1360 			double fTmp;
1361 			sal_Bool bRet = rImport.GetMM100UnitConverter().
1362 				convertDateTime(fTmp,sAttrValue);
1363 			if (bRet) {
1364 				bFloatValueOK = sal_True;
1365 				fValue = fTmp;
1366 			}
1367 			break;
1368 		}
1369 
1370 		case XML_TOK_TEXTFIELD_BOOL_VALUE:
1371 		{
1372 			sal_Bool bTmp;
1373 			sal_Bool bRet = SvXMLUnitConverter::convertBool(bTmp,sAttrValue);
1374 			if (bRet) {
1375 				bFloatValueOK = sal_True;
1376 				fValue = (bTmp ? 1.0 : 0.0);
1377 			}
1378             else
1379             {
1380                 double fTmp;
1381                 bRet = SvXMLUnitConverter::convertDouble(fTmp,sAttrValue);
1382                 if (bRet) {
1383                     bFloatValueOK = sal_True;
1384                     fValue = fTmp;
1385                 }
1386             }
1387 			break;
1388 		}
1389 
1390 		case XML_TOK_TEXTFIELD_STRING_VALUE:
1391 			sValue = sAttrValue;
1392 			bStringValueOK = sal_True;
1393 			break;
1394 
1395 		case XML_TOK_TEXTFIELD_FORMULA:
1396 			{
1397 				OUString sTmp;
1398 				sal_uInt16 nPrefix = rImport.GetNamespaceMap().
1399 						_GetKeyByAttrName( sAttrValue, &sTmp, sal_False );
1400 				if( XML_NAMESPACE_OOOW == nPrefix )
1401 				{
1402 					sFormula = sTmp;
1403 					bFormulaOK = sal_True;
1404 				}
1405 				else
1406 					sFormula = sAttrValue;
1407 			}
1408 			break;
1409 
1410 		case XML_TOK_TEXTFIELD_DATA_STYLE_NAME:
1411 		{
1412 			sal_Int32 nKey = rHelper.GetDataStyleKey(
1413 										  sAttrValue, &bIsDefaultLanguage);
1414 			if (-1 != nKey)
1415 			{
1416 				nFormatKey = nKey;
1417 				bFormatOK = sal_True;
1418 			}
1419 			break;
1420 		}
1421 	} // switch
1422 }
1423 
1424 void XMLValueImportHelper::PrepareField(
1425 	const Reference<XPropertySet> & xPropertySet)
1426 {
1427     Any aAny;
1428 
1429 	if (bSetType)
1430 	{
1431 		// ??? how to set type?
1432 	}
1433 
1434 	if (bSetFormula)
1435 	{
1436 		aAny <<= (!bFormulaOK && bFormulaDefault) ? sDefault : sFormula;
1437 		xPropertySet->setPropertyValue(sPropertyContent, aAny);
1438 	}
1439 
1440 	// format/style
1441 	if (bSetStyle && bFormatOK)
1442 	{
1443 		aAny <<= nFormatKey;
1444 		xPropertySet->setPropertyValue(sPropertyNumberFormat, aAny);
1445 
1446         if( xPropertySet->getPropertySetInfo()->
1447                 hasPropertyByName( sPropertyIsFixedLanguage ) )
1448         {
1449             sal_Bool bIsFixedLanguage = ! bIsDefaultLanguage;
1450             aAny.setValue( &bIsFixedLanguage, ::getBooleanCppuType() );
1451             xPropertySet->setPropertyValue( sPropertyIsFixedLanguage, aAny );
1452         }
1453 	}
1454 
1455 	// value: string or float
1456 	if (bSetValue)
1457 	{
1458 		if (bStringType)
1459 		{
1460 			aAny <<= (!bStringValueOK && bStringDefault) ? sDefault : sValue;
1461 			xPropertySet->setPropertyValue(sPropertyContent, aAny);
1462 		}
1463 		else
1464 		{
1465 			aAny <<= fValue;
1466 			xPropertySet->setPropertyValue(sPropertyValue, aAny);
1467 		}
1468 	}
1469 }
1470 
1471