1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: txtvfldi.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmloff.hxx"
36 * export of all variable related text fields (and database display field)
38 #include "txtvfldi.hxx"
39 #include <xmloff/xmltoken.hxx>
40 #include <xmloff/txtimp.hxx>
41 #include <xmloff/xmlnumi.hxx>
42 #include "xmlnmspe.hxx"
43 #include <xmloff/nmspmap.hxx>
44 #include "i18nmap.hxx"
45 #include <xmloff/xmlimp.hxx>
46 #include <xmloff/xmluconv.hxx>
47 #include <xmloff/xmlement.hxx>
48 #include <com/sun/star/text/SetVariableType.hpp>
49 #include <com/sun/star/text/XTextField.hpp>
50 #include <com/sun/star/text/XDependentTextField.hpp>
51 #include <com/sun/star/text/XTextFieldsSupplier.hpp>
52 #include <com/sun/star/beans/XPropertySet.hpp>
53 #include <com/sun/star/beans/XPropertySetInfo.hpp>
54 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
55 #include <com/sun/star/xml/sax/XAttributeList.hpp>
58 #include <rtl/ustring.hxx>
60 #include <tools/debug.hxx>
64 static const sal_Char sAPI_textfield_prefix
[] = "com.sun.star.text.TextField.";
65 static const sal_Char sAPI_fieldmaster_prefix
[] = "com.sun.star.text.FieldMaster.";
66 static const sal_Char sAPI_input
[] = "Input";
67 static const sal_Char sAPI_input_user
[] = "InputUser";
68 static const sal_Char sAPI_get_expression
[] = "GetExpression";
69 static const sal_Char sAPI_set_expression
[] = "SetExpression";
70 static const sal_Char sAPI_user
[] = "User";
71 static const sal_Char sAPI_table_formula
[] = "TableFormula";
72 static const sal_Char sAPI_database
[] = "com.sun.star.text.TextField.Database";
73 static const sal_Char sAPI_fieldmaster_database
[] = "com.sun.star.text.FieldMaster.Database";
76 static const sal_Char sAPI_hint
[] = "Hint";
77 static const sal_Char sAPI_help
[] = "Help";
78 static const sal_Char sAPI_tooltip
[] = "Tooltip";
79 static const sal_Char sAPI_content
[] = "Content";
80 static const sal_Char sAPI_sub_type
[] = "SubType";
81 static const sal_Char sAPI_is_expression
[] = "IsExpression";
82 static const sal_Char sAPI_is_input
[] = "Input";
83 static const sal_Char sAPI_is_show_formula
[] = "IsShowFormula";
84 static const sal_Char sAPI_numbering_type
[] = "NumberingType";
85 static const sal_Char sAPI_number_format
[] = "NumberFormat";
86 static const sal_Char sAPI_name
[] = "Name";
87 static const sal_Char sAPI_numbering_separator
[] = "NumberingSeparator";
88 static const sal_Char sAPI_chapter_numbering_level
[]= "ChapterNumberingLevel";
89 static const sal_Char sAPI_value
[] = "Value";
90 static const sal_Char sAPI_is_visible
[] = "IsVisible";
91 static const sal_Char sAPI_variable_subtype
[] = "VariableSubtype";
92 static const sal_Char sAPI_data_column_name
[] = "DataColumnName";
93 static const sal_Char sAPI_is_data_base_format
[] = "DataBaseFormat";
94 static const sal_Char sAPI_current_presentation
[] = "CurrentPresentation";
95 static const sal_Char sAPI_sequence_value
[] = "SequenceValue";
96 static const sal_Char sAPI_is_fixed_language
[] = "IsFixedLanguage";
99 using ::rtl::OUString
;
100 using ::rtl::OUStringBuffer
;
102 using namespace ::com::sun::star
;
103 using namespace ::com::sun::star::uno
;
104 using namespace ::com::sun::star::beans
;
105 using namespace ::com::sun::star::text
;
106 using namespace ::com::sun::star::style
;
107 using namespace ::xmloff::token
;
112 // XMLVarFieldImportContext: superclass for all variable related fields
115 TYPEINIT1( XMLVarFieldImportContext
, XMLTextFieldImportContext
);
117 XMLVarFieldImportContext::XMLVarFieldImportContext(
118 SvXMLImport
& rImport
, XMLTextImportHelper
& rHlp
,
119 const sal_Char
* pServiceName
, sal_uInt16 nPrfx
,
120 const OUString
& rLocalName
,
121 sal_Bool bName
, sal_Bool bFormula
, sal_Bool bFormulaDefault
,
122 sal_Bool bDescription
, sal_Bool bHelp
, sal_Bool bHint
, sal_Bool bVisible
,
123 sal_Bool bIsDisplayFormula
,
124 sal_Bool bType
, sal_Bool bStyle
, sal_Bool bValue
,
125 sal_Bool bPresentation
) :
126 XMLTextFieldImportContext(rImport
, rHlp
, pServiceName
, nPrfx
, rLocalName
),
127 sPropertyContent(RTL_CONSTASCII_USTRINGPARAM(sAPI_content
)),
128 sPropertyHint(RTL_CONSTASCII_USTRINGPARAM(sAPI_hint
)),
129 sPropertyHelp(RTL_CONSTASCII_USTRINGPARAM(sAPI_help
)),
130 sPropertyTooltip(RTL_CONSTASCII_USTRINGPARAM(sAPI_tooltip
)),
131 sPropertyIsVisible(RTL_CONSTASCII_USTRINGPARAM(sAPI_is_visible
)),
132 sPropertyIsDisplayFormula(RTL_CONSTASCII_USTRINGPARAM(sAPI_is_show_formula
)),
133 sPropertyCurrentPresentation(RTL_CONSTASCII_USTRINGPARAM(sAPI_current_presentation
)),
134 aValueHelper(rImport
, rHlp
, bType
, bStyle
, bValue
, sal_False
),
135 bDisplayFormula(sal_False
),
136 bDisplayNone(sal_False
),
138 bFormulaOK(sal_False
),
139 bDescriptionOK(sal_False
),
142 bDisplayOK(sal_False
),
144 bSetFormula(bFormula
),
145 bSetFormulaDefault(bFormulaDefault
),
146 bSetDescription(bDescription
),
149 bSetVisible(bVisible
),
150 bSetDisplayFormula(bIsDisplayFormula
),
151 bSetPresentation(bPresentation
)
155 void XMLVarFieldImportContext::ProcessAttribute(
156 sal_uInt16 nAttrToken
,
157 const OUString
& sAttrValue
)
161 case XML_TOK_TEXTFIELD_NAME
:
164 bValid
= sal_True
; // we assume: field with name is valid!
166 case XML_TOK_TEXTFIELD_DESCRIPTION
:
167 sDescription
= sAttrValue
;
168 bDescriptionOK
= sal_True
;
170 case XML_TOK_TEXTFIELD_HELP
:
174 case XML_TOK_TEXTFIELD_HINT
:
178 case XML_TOK_TEXTFIELD_FORMULA
:
181 sal_uInt16 nPrefix
= GetImport().GetNamespaceMap().
182 _GetKeyByAttrName( sAttrValue
, &sTmp
, sal_False
);
183 if( XML_NAMESPACE_OOOW
== nPrefix
)
186 bFormulaOK
= sal_True
;
189 sFormula
= sAttrValue
;
192 case XML_TOK_TEXTFIELD_DISPLAY
:
193 if (IsXMLToken(sAttrValue
, XML_FORMULA
))
195 bDisplayFormula
= sal_True
;
196 bDisplayNone
= sal_False
;
197 bDisplayOK
= sal_True
;
199 else if (IsXMLToken(sAttrValue
, XML_VALUE
))
201 bDisplayFormula
= sal_False
;
202 bDisplayNone
= sal_False
;
203 bDisplayOK
= sal_True
;
205 else if (IsXMLToken(sAttrValue
, XML_NONE
))
207 bDisplayFormula
= sal_False
;
208 bDisplayNone
= sal_True
;
209 bDisplayOK
= sal_True
;
211 DBG_ASSERT(!(bDisplayFormula
&& bDisplayNone
),
212 "illegal display values");
215 // delegate all others to value helper
216 aValueHelper
.ProcessAttribute(nAttrToken
, sAttrValue
);
221 void XMLVarFieldImportContext::PrepareField(
222 const Reference
<XPropertySet
> & xPropertySet
)
224 // bSetName: not implemented
228 if (!bFormulaOK
&& bSetFormulaDefault
)
230 sFormula
= GetContent();
231 bFormulaOK
= sal_True
;
238 xPropertySet
->setPropertyValue(sPropertyContent
, aAny
);
242 if (bSetDescription
&& bDescriptionOK
)
245 aAny
<<= sDescription
;
246 xPropertySet
->setPropertyValue(sPropertyHint
, aAny
);
249 if (bSetHelp
&& bHelpOK
)
253 xPropertySet
->setPropertyValue(sPropertyHelp
, aAny
);
256 if (bSetHint
&& bHintOK
)
260 xPropertySet
->setPropertyValue(sPropertyTooltip
, aAny
);
263 if (bSetVisible
&& bDisplayOK
)
266 sal_Bool bTmp
= ! (bDisplayNone
&& bDisplayOK
);
267 aAny
.setValue( &bTmp
, ::getBooleanCppuType());
268 xPropertySet
->setPropertyValue(sPropertyIsVisible
, aAny
);
271 // workaround for #no-bug#: display formula by default
272 if (xPropertySet
->getPropertySetInfo()->
273 hasPropertyByName(sPropertyIsDisplayFormula
) &&
276 bDisplayFormula
= sal_False
;
277 bSetDisplayFormula
= sal_True
;
281 if (bSetDisplayFormula
)
284 sal_Bool bTmp
= bDisplayFormula
&& bDisplayOK
;
285 aAny
.setValue( &bTmp
, ::getBooleanCppuType());
286 xPropertySet
->setPropertyValue(sPropertyIsDisplayFormula
, aAny
);
289 // delegate to value helper
290 aValueHelper
.SetDefault(GetContent());
291 aValueHelper
.PrepareField(xPropertySet
);
293 // finally, set the curren presentation
294 if (bSetPresentation
)
297 aAny
<<= GetContent();
298 xPropertySet
->setPropertyValue(sPropertyCurrentPresentation
, aAny
);
307 // variable set fields
310 TYPEINIT1( XMLSetVarFieldImportContext
, XMLVarFieldImportContext
);
312 XMLSetVarFieldImportContext::XMLSetVarFieldImportContext(
313 SvXMLImport
& rImport
, XMLTextImportHelper
& rHlp
,
314 const sal_Char
* pServiceName
, sal_uInt16 nPrfx
,
315 const OUString
& rLocalName
, VarType eVarType
,
316 sal_Bool bName
, sal_Bool bFormula
, sal_Bool bFormulaDefault
,
317 sal_Bool bDescription
, sal_Bool bHelp
, sal_Bool bHint
, sal_Bool bVisible
, sal_Bool bIsDisplayFormula
,
318 sal_Bool bType
, sal_Bool bStyle
, sal_Bool bValue
, sal_Bool bPresentation
) :
319 XMLVarFieldImportContext(rImport
, rHlp
, pServiceName
,
321 bName
, bFormula
, bFormulaDefault
,
322 bDescription
, bHelp
, bHint
, bVisible
, bIsDisplayFormula
,
323 bType
, bStyle
, bValue
, bPresentation
),
328 void XMLSetVarFieldImportContext::EndElement()
330 // should we call PrepareField on the field, or rather on it's master?
331 // currently: call on field (just like superclass)
332 // possible alternatives: call on master
333 // call field or master depending on variable
334 // PrepareMaster() in addition to PrepareField()
336 DBG_ASSERT(GetServiceName().getLength()>0, "no service name for element!");
340 DBG_ASSERT(GetName().getLength()>0, "variable name needed!");
343 Reference
<XPropertySet
> xMaster
;
344 if (FindFieldMaster(xMaster
))
346 // create field/Service
347 Reference
<XPropertySet
> xPropSet
;
348 if (CreateField(xPropSet
, OUString::createFromAscii(sAPI_textfield_prefix
) + GetServiceName()))
350 Reference
<XDependentTextField
> xDepTextField(xPropSet
, UNO_QUERY
);
351 if (xDepTextField
.is())
353 // attach field to field master
354 xDepTextField
->attachTextFieldMaster(xMaster
);
356 // attach field to document
357 Reference
<XTextContent
> xTextContent(xPropSet
, UNO_QUERY
);
358 if (xTextContent
.is())
361 // insert, set field properties and exit!
362 GetImportHelper().InsertTextContent(xTextContent
);
363 PrepareField(xPropSet
);
364 } catch (lang::IllegalArgumentException
& /*e*/)
366 // ignore e: #i54023#
375 // above: exit on success; so for all error cases we end up here!
376 // write element content
377 GetImportHelper().InsertString(GetContent());
380 sal_Bool
XMLSetVarFieldImportContext::FindFieldMaster(
381 Reference
<XPropertySet
> & xMaster
)
383 // currently: delegate to XMLVariableDeclImportContext;
384 // shoud eventually go here
385 return XMLVariableDeclImportContext::FindFieldMaster(xMaster
,
398 TYPEINIT1( XMLSequenceFieldImportContext
, XMLSetVarFieldImportContext
);
400 XMLSequenceFieldImportContext::XMLSequenceFieldImportContext(
401 SvXMLImport
& rImport
, XMLTextImportHelper
& rHlp
,
402 sal_uInt16 nPrfx
, const OUString
& rLocalName
) :
403 XMLSetVarFieldImportContext(rImport
, rHlp
, sAPI_set_expression
,
404 nPrfx
, rLocalName
, VarTypeSequence
,
406 sal_True
, sal_True
, sal_True
,
407 sal_False
, sal_False
, sal_False
, sal_False
,
409 sal_False
, sal_False
, sal_False
, sal_True
),
411 sPropertyNumberFormat(RTL_CONSTASCII_USTRINGPARAM(sAPI_number_format
)),
412 sPropertySequenceValue(RTL_CONSTASCII_USTRINGPARAM(sAPI_sequence_value
)),
413 sNumFormat(OUString::valueOf(sal_Unicode('1'))),
414 sNumFormatSync(GetXMLToken(XML_FALSE
)),
415 bRefNameOK(sal_False
)
419 void XMLSequenceFieldImportContext::ProcessAttribute(
420 sal_uInt16 nAttrToken
, const OUString
& sAttrValue
)
424 case XML_TOK_TEXTFIELD_NUM_FORMAT
:
425 sNumFormat
= sAttrValue
;
427 case XML_TOK_TEXTFIELD_NUM_LETTER_SYNC
:
428 sNumFormatSync
= sAttrValue
;
430 case XML_TOK_TEXTFIELD_REF_NAME
:
431 sRefName
= sAttrValue
;
432 bRefNameOK
= sal_True
;
435 // delegate to super class (name, formula)
436 XMLSetVarFieldImportContext::ProcessAttribute(nAttrToken
,
442 void XMLSequenceFieldImportContext::PrepareField(
443 const Reference
<XPropertySet
> & xPropertySet
)
445 // delegate to super class (formula)
446 XMLSetVarFieldImportContext::PrepareField(xPropertySet
);
449 sal_Int16 nNumType
= NumberingType::ARABIC
;
450 GetImport().GetMM100UnitConverter().convertNumFormat( nNumType
, sNumFormat
, sNumFormatSync
);
453 xPropertySet
->setPropertyValue(sPropertyNumberFormat
, aAny
);
455 // handle reference name
458 aAny
= xPropertySet
->getPropertyValue(sPropertySequenceValue
);
459 sal_Int16 nValue
= 0;
461 GetImportHelper().InsertSequenceID(sRefName
, GetName(), nValue
);
468 // variable set field
471 TYPEINIT1( XMLVariableSetFieldImportContext
, XMLSetVarFieldImportContext
);
473 XMLVariableSetFieldImportContext::XMLVariableSetFieldImportContext(
474 SvXMLImport
& rImport
, XMLTextImportHelper
& rHlp
,
475 sal_uInt16 nPrfx
, const OUString
& rLocalName
) :
476 XMLSetVarFieldImportContext(rImport
, rHlp
, sAPI_set_expression
,
477 nPrfx
, rLocalName
, VarTypeSimple
,
478 // name, formula, value&type, style,
480 sal_True
, sal_True
, sal_True
,
481 sal_False
, sal_False
, sal_False
,
483 sal_True
, sal_True
, sal_True
,
485 sPropertySubType(RTL_CONSTASCII_USTRINGPARAM(sAPI_sub_type
))
489 void XMLVariableSetFieldImportContext::PrepareField(
490 const Reference
<XPropertySet
> & xPropertySet
)
494 aAny
<<= (IsStringValue()? SetVariableType::STRING
: SetVariableType::VAR
);
495 xPropertySet
->setPropertyValue(sPropertySubType
, aAny
);
497 // the remainder is handled by super class
498 XMLSetVarFieldImportContext::PrepareField(xPropertySet
);
504 // variable input field
507 TYPEINIT1( XMLVariableInputFieldImportContext
, XMLSetVarFieldImportContext
);
509 XMLVariableInputFieldImportContext::XMLVariableInputFieldImportContext(
510 SvXMLImport
& rImport
, XMLTextImportHelper
& rHlp
, sal_uInt16 nPrfx
,
511 const OUString
& rLocalName
) :
512 XMLSetVarFieldImportContext(rImport
, rHlp
, sAPI_set_expression
,
513 nPrfx
, rLocalName
, VarTypeSimple
,
514 // name, description, display none/formula,
515 // value&type, style, formula
516 sal_True
, sal_True
, sal_True
,
517 sal_True
, sal_True
, sal_True
,
519 sal_True
, sal_True
, sal_True
,
521 sPropertySubType(RTL_CONSTASCII_USTRINGPARAM(sAPI_sub_type
)),
522 sPropertyIsInput(RTL_CONSTASCII_USTRINGPARAM(sAPI_is_input
))
526 void XMLVariableInputFieldImportContext::PrepareField(
527 const Reference
<XPropertySet
> & xPropertySet
)
529 // set type (input field)
531 sal_Bool bTrue
= sal_True
;
532 aAny
.setValue( &bTrue
, ::getBooleanCppuType() );
533 xPropertySet
->setPropertyValue(sPropertyIsInput
, aAny
);
536 aAny
<<= (IsStringValue()? SetVariableType::STRING
: SetVariableType::VAR
);
537 xPropertySet
->setPropertyValue(sPropertySubType
, aAny
);
539 // the remainder is handled by super class
540 XMLSetVarFieldImportContext::PrepareField(xPropertySet
);
549 TYPEINIT1( XMLUserFieldImportContext
, XMLSetVarFieldImportContext
);
551 XMLUserFieldImportContext::XMLUserFieldImportContext(
552 SvXMLImport
& rImport
, XMLTextImportHelper
& rHlp
,
553 sal_uInt16 nPrfx
, const OUString
& rLocalName
) :
554 XMLSetVarFieldImportContext(rImport
, rHlp
, sAPI_user
, nPrfx
,
555 rLocalName
, VarTypeUserField
,
556 // name, display none/formula, style
557 sal_True
, sal_False
, sal_False
,
558 sal_False
, sal_False
, sal_False
, sal_True
,
560 sal_False
, sal_True
, sal_False
,
571 TYPEINIT1( XMLUserFieldInputImportContext
, XMLVarFieldImportContext
);
573 // bug: doesn't work (SO API lacking)
574 XMLUserFieldInputImportContext::XMLUserFieldInputImportContext(
575 SvXMLImport
& rImport
, XMLTextImportHelper
& rHlp
, sal_uInt16 nPrfx
,
576 const OUString
& rLocalName
) :
577 XMLVarFieldImportContext(rImport
, rHlp
, sAPI_input_user
,
579 // name, description, style
580 sal_True
, sal_False
, sal_False
,
581 sal_True
, sal_False
, sal_False
,
582 sal_False
, sal_False
,
583 sal_False
/*???*/, sal_True
, sal_False
,
588 void XMLUserFieldInputImportContext::PrepareField(
589 const Reference
<XPropertySet
> & xPropertySet
)
593 xPropertySet
->setPropertyValue(sPropertyContent
, aAny
);
595 // delegate to super class
596 XMLVarFieldImportContext::PrepareField(xPropertySet
);
601 // variable get field
604 TYPEINIT1( XMLVariableGetFieldImportContext
, XMLVarFieldImportContext
);
606 XMLVariableGetFieldImportContext::XMLVariableGetFieldImportContext(
607 SvXMLImport
& rImport
, XMLTextImportHelper
& rHlp
,
608 sal_uInt16 nPrfx
, const OUString
& rLocalName
) :
609 XMLVarFieldImportContext(rImport
, rHlp
, sAPI_get_expression
,
611 // name, style, display formula
612 sal_True
, sal_False
, sal_False
,
613 sal_False
, sal_False
, sal_False
,
615 sal_True
, sal_True
, sal_False
,
617 sPropertySubType(RTL_CONSTASCII_USTRINGPARAM(sAPI_sub_type
))
621 void XMLVariableGetFieldImportContext::PrepareField(
622 const Reference
<XPropertySet
> & xPropertySet
)
627 xPropertySet
->setPropertyValue(sPropertyContent
, aAny
);
629 // the remainder is handled by super class
630 XMLVarFieldImportContext::PrepareField(xPropertySet
);
639 TYPEINIT1( XMLExpressionFieldImportContext
, XMLVarFieldImportContext
);
641 XMLExpressionFieldImportContext::XMLExpressionFieldImportContext(
642 SvXMLImport
& rImport
, XMLTextImportHelper
& rHlp
,
643 sal_uInt16 nPrfx
, const OUString
& rLocalName
) :
644 XMLVarFieldImportContext(rImport
, rHlp
, sAPI_get_expression
,
646 // formula, type, style, display formula
647 sal_False
, sal_True
, sal_True
,
648 sal_False
, sal_False
, sal_False
,
650 sal_True
, sal_True
, sal_False
,
652 sPropertySubType(RTL_CONSTASCII_USTRINGPARAM(sAPI_sub_type
))
654 bValid
= sal_True
; // always valid
658 void XMLExpressionFieldImportContext::PrepareField(
659 const Reference
<XPropertySet
> & xPropertySet
)
661 sal_Int16 nSubType
= SetVariableType::FORMULA
;
664 xPropertySet
->setPropertyValue(sPropertySubType
, aAny
);
666 // delegate to super class
667 XMLVarFieldImportContext::PrepareField(xPropertySet
);
676 TYPEINIT1( XMLTextInputFieldImportContext
, XMLVarFieldImportContext
);
678 XMLTextInputFieldImportContext::XMLTextInputFieldImportContext(
679 SvXMLImport
& rImport
, XMLTextImportHelper
& rHlp
,
680 sal_uInt16 nPrfx
, const OUString
& sLocalName
) :
681 XMLVarFieldImportContext(rImport
, rHlp
, sAPI_input
,
684 sal_False
, sal_False
, sal_False
,
685 sal_True
, sal_True
, sal_True
,
686 sal_False
, sal_False
,
687 sal_False
, sal_False
, sal_False
,
689 sPropertyContent(RTL_CONSTASCII_USTRINGPARAM(sAPI_content
))
691 bValid
= sal_True
; // always valid
694 void XMLTextInputFieldImportContext::PrepareField(
695 const Reference
<XPropertySet
> & xPropertySet
)
697 XMLVarFieldImportContext::PrepareField(xPropertySet
);
700 aAny
<<= GetContent();
701 xPropertySet
->setPropertyValue(sPropertyContent
, aAny
);
706 // table formula field
709 TYPEINIT1( XMLTableFormulaImportContext
, XMLTextFieldImportContext
);
711 XMLTableFormulaImportContext::XMLTableFormulaImportContext(
712 SvXMLImport
& rImport
,
713 XMLTextImportHelper
& rHlp
,
715 const OUString
& rLocalName
) :
716 XMLTextFieldImportContext(rImport
, rHlp
, sAPI_table_formula
,
718 sPropertyIsShowFormula(RTL_CONSTASCII_USTRINGPARAM("IsShowFormula")),
719 sPropertyCurrentPresentation(
720 RTL_CONSTASCII_USTRINGPARAM("CurrentPresentation")),
721 aValueHelper(rImport
, rHlp
, sal_False
, sal_True
, sal_False
, sal_True
),
723 bIsShowFormula(sal_False
)
727 XMLTableFormulaImportContext::~XMLTableFormulaImportContext()
731 void XMLTableFormulaImportContext::ProcessAttribute(
732 sal_uInt16 nAttrToken
,
733 const OUString
& sAttrValue
)
737 case XML_TOK_TEXTFIELD_FORMULA
:
738 aValueHelper
.ProcessAttribute( nAttrToken
, sAttrValue
);
739 bValid
= sal_True
; // we need a formula!
742 case XML_TOK_TEXTFIELD_DATA_STYLE_NAME
:
743 aValueHelper
.ProcessAttribute( nAttrToken
, sAttrValue
);
745 case XML_TOK_TEXTFIELD_DISPLAY
:
746 if ( sAttrValue
.equalsAsciiL(
747 RTL_CONSTASCII_STRINGPARAM("formula")) )
748 bIsShowFormula
= sal_True
;
751 // unknown attribute -> ignore
756 void XMLTableFormulaImportContext::PrepareField(
757 const Reference
<XPropertySet
> & xPropertySet
)
759 // set format and formula
760 aValueHelper
.PrepareField( xPropertySet
);
764 // set 'show formula' and presentation
765 aAny
.setValue( &bIsShowFormula
, ::getBooleanCppuType() );
766 xPropertySet
->setPropertyValue( sPropertyIsShowFormula
, aAny
);
768 aAny
<<= GetContent();
769 xPropertySet
->setPropertyValue( sPropertyCurrentPresentation
, aAny
);
775 // variable declarations
777 // Should be adapted to XMLVarField-/XMLSetVarFieldImportContext scheme!
783 // declaration containter import (<variable/user-field/sequence-decls>)
786 TYPEINIT1( XMLVariableDeclsImportContext
, SvXMLImportContext
);
788 XMLVariableDeclsImportContext::XMLVariableDeclsImportContext(
789 SvXMLImport
& rImport
, XMLTextImportHelper
& rHlp
, sal_uInt16 nPrfx
,
790 const OUString
& rLocalName
, enum VarType eVarType
) :
791 SvXMLImportContext(rImport
, nPrfx
, rLocalName
),
792 eVarDeclsContextType(eVarType
),
797 SvXMLImportContext
* XMLVariableDeclsImportContext::CreateChildContext(
798 sal_uInt16 nPrefix
, const OUString
& rLocalName
,
799 const Reference
<xml::sax::XAttributeList
> & xAttrList
)
801 enum XMLTokenEnum eElementName
;
802 SvXMLImportContext
* pImportContext
= NULL
;
804 if( XML_NAMESPACE_TEXT
== nPrefix
)
806 switch (eVarDeclsContextType
)
808 case VarTypeSequence
:
809 eElementName
= XML_SEQUENCE_DECL
;
812 eElementName
= XML_VARIABLE_DECL
;
814 case VarTypeUserField
:
815 eElementName
= XML_USER_FIELD_DECL
;
818 DBG_ERROR("unknown field type!");
819 eElementName
= XML_SEQUENCE_DECL
;
823 if( IsXMLToken( rLocalName
, eElementName
) )
825 pImportContext
= new XMLVariableDeclImportContext(
826 GetImport(), rImportHelper
, nPrefix
, rLocalName
, xAttrList
,
827 eVarDeclsContextType
);
831 // if no context was created, use default context
832 if (NULL
== pImportContext
) {
833 pImportContext
= SvXMLImportContext::CreateChildContext(nPrefix
,
838 return pImportContext
;
844 // declaration import (<variable/user-field/sequence-decl> elements)
847 TYPEINIT1( XMLVariableDeclImportContext
, SvXMLImportContext
);
849 XMLVariableDeclImportContext::XMLVariableDeclImportContext(
850 SvXMLImport
& rImport
, XMLTextImportHelper
& rHlp
,
851 sal_uInt16 nPrfx
, const OUString
& rLocalName
,
852 const Reference
<xml::sax::XAttributeList
> & xAttrList
,
853 enum VarType eVarType
) :
854 SvXMLImportContext(rImport
, nPrfx
, rLocalName
),
855 // bug?? which properties for userfield/userfieldmaster
856 sPropertyName(RTL_CONSTASCII_USTRINGPARAM(sAPI_name
)),
857 sPropertySubType(RTL_CONSTASCII_USTRINGPARAM(sAPI_sub_type
)),
858 sPropertyNumberingLevel(RTL_CONSTASCII_USTRINGPARAM(sAPI_chapter_numbering_level
)),
859 sPropertyNumberingSeparator(RTL_CONSTASCII_USTRINGPARAM(sAPI_numbering_separator
)),
860 sPropertyIsExpression(RTL_CONSTASCII_USTRINGPARAM(sAPI_is_expression
)),
861 aValueHelper(rImport
, rHlp
, sal_True
, sal_False
, sal_True
, sal_False
),
862 nNumLevel(-1), cSeparationChar('.')
864 if ( (XML_NAMESPACE_TEXT
== nPrfx
) &&
865 ( ( IsXMLToken( rLocalName
, XML_SEQUENCE_DECL
)) ||
866 ( IsXMLToken( rLocalName
, XML_VARIABLE_DECL
)) ||
867 ( IsXMLToken( rLocalName
, XML_USER_FIELD_DECL
)) )) {
869 // TODO: check validity (need name!)
872 sal_Int16 nLength
= xAttrList
->getLength();
873 for(sal_Int16 i
=0; i
<nLength
; i
++) {
876 sal_uInt16 nPrefix
= GetImport().GetNamespaceMap().
877 GetKeyByAttrName( xAttrList
->getNameByIndex(i
), &sLocalName
);
879 sal_uInt16 nToken
= rHlp
.
880 GetTextFieldAttrTokenMap().Get(nPrefix
, sLocalName
);
884 case XML_TOK_TEXTFIELD_NAME
:
885 sName
= xAttrList
->getValueByIndex(i
);
887 case XML_TOK_TEXTFIELD_NUMBERING_LEVEL
:
890 sal_Bool bRet
= SvXMLUnitConverter::convertNumber(
891 nLevel
, xAttrList
->getValueByIndex(i
), 0,
892 GetImport().GetTextImport()->GetChapterNumbering()->
896 nNumLevel
= static_cast< sal_Int8
>( nLevel
-1 ); // API numbers -1..9
900 case XML_TOK_TEXTFIELD_NUMBERING_SEPARATOR
:
902 (sal_Char
)xAttrList
->getValueByIndex(i
).toChar();
906 // delegate to value helper
907 aValueHelper
.ProcessAttribute(nToken
,
908 xAttrList
->getValueByIndex(i
));
913 Reference
<XPropertySet
> xFieldMaster
;
914 if (FindFieldMaster(xFieldMaster
, GetImport(), rHlp
,
917 // now we have a field master: process attributes!
922 case VarTypeSequence
:
924 xFieldMaster
->setPropertyValue(sPropertyNumberingLevel
, aAny
);
928 OUString
sStr(&cSeparationChar
, 1);
930 xFieldMaster
->setPropertyValue(
931 sPropertyNumberingSeparator
, aAny
);
936 // set string or non-string SubType (#93192#)
937 // The SubType was already set in the FindFieldMaster
938 // method, but it needs to be adjusted if it's a string.
939 aAny
<<= aValueHelper
.IsStringValue()
940 ? SetVariableType::STRING
: SetVariableType::VAR
;
941 xFieldMaster
->setPropertyValue(sPropertySubType
, aAny
);
944 case VarTypeUserField
:
946 sal_Bool bTmp
= !aValueHelper
.IsStringValue();
947 aAny
.setValue(&bTmp
, ::getBooleanCppuType());
948 xFieldMaster
->setPropertyValue(sPropertyIsExpression
, aAny
);
949 aValueHelper
.PrepareField(xFieldMaster
);
953 DBG_ERROR("unkown varfield type");
955 } // else: no field master found/constructed
956 } // else: no sequence-decl
961 sal_Bool
XMLVariableDeclImportContext::FindFieldMaster(
962 Reference
<XPropertySet
> & xMaster
, SvXMLImport
& rImport
,
963 XMLTextImportHelper
& rImportHelper
,
964 const OUString
& sVarName
, enum VarType eVarType
)
966 static sal_Int32 nCollisionCount
= 0;
969 // currently: no family in use! Use 0.
970 OUString sName
= rImportHelper
.GetRenameMap().Get(
971 sal::static_int_cast
< USHORT
>(eVarType
), sVarName
);
973 // get text fields supplier and field masters
974 Reference
<XTextFieldsSupplier
> xTextFieldsSupp(rImport
.GetModel(),
976 Reference
<container::XNameAccess
> xFieldMasterNameAccess(
977 xTextFieldsSupp
->getTextFieldMasters(), UNO_QUERY
);
979 OUStringBuffer sBuffer
;
980 sBuffer
.appendAscii(sAPI_fieldmaster_prefix
);
981 sBuffer
.appendAscii(sAPI_set_expression
);
982 sBuffer
.appendAscii(".");
983 sBuffer
.append(sName
);
984 OUString sVarServiceName
= sBuffer
.makeStringAndClear();
986 sBuffer
.appendAscii(sAPI_fieldmaster_prefix
);
987 sBuffer
.appendAscii(sAPI_user
);
988 sBuffer
.appendAscii(".");
989 sBuffer
.append(sName
);
990 OUString sUserServiceName
= sBuffer
.makeStringAndClear();
992 if (xFieldMasterNameAccess
->hasByName(sVarServiceName
)) {
993 // variable field master already in document
995 Any aAny
= xFieldMasterNameAccess
->getByName(sVarServiceName
);
998 aAny
= xMaster
->getPropertyValue(
1000 OUString::createFromAscii(sAPI_sub_type
)
1002 sal_Int16 nType
= 0;
1005 enum VarType eFMVarType
=
1006 (SetVariableType::SEQUENCE
== nType
) ?
1007 VarTypeSequence
: VarTypeSimple
;
1009 if (eFMVarType
!= eVarType
)
1013 // FIXME! cant find if name is taken already!!!!
1016 OUStringBuffer aBuf
;
1018 aBuf
.appendAscii("_renamed_");
1019 aBuf
.append(nCollisionCount
);
1020 sNew
= aBuf
.makeStringAndClear();
1022 rImportHelper
.GetRenameMap().Add(
1023 sal::static_int_cast
< USHORT
>(eVarType
), sName
, sNew
);
1025 // call FindFieldMaster recursively to create new master
1026 return FindFieldMaster(xMaster
, rImport
, rImportHelper
,
1029 } else if (xFieldMasterNameAccess
->hasByName(sUserServiceName
)) {
1030 // user field: get field master
1031 Any aAny
= xFieldMasterNameAccess
->getByName(sUserServiceName
);
1034 if (VarTypeUserField
!= eVarType
) {
1035 // find new name that is not taken
1038 // FIXME! cant find if name is taken already!!!!
1041 OUStringBuffer aBuf
;
1043 aBuf
.appendAscii("_renamed_");
1044 aBuf
.append(nCollisionCount
);
1045 sNew
= aBuf
.makeStringAndClear();
1047 rImportHelper
.GetRenameMap().Add(
1048 sal::static_int_cast
< USHORT
>(eVarType
), sName
, sNew
);
1050 // call FindFieldMaster recursively to create new master
1051 return FindFieldMaster(xMaster
, rImport
, rImportHelper
,
1055 // field name not used: create field master
1057 // import -> model is MultiServiceFactory -> createInstance
1058 Reference
<lang::XMultiServiceFactory
>
1059 xFactory(rImport
.GetModel(),UNO_QUERY
);
1060 if( xFactory
.is() ) {
1062 OUStringBuffer sService
;
1063 sService
.appendAscii(sAPI_fieldmaster_prefix
);
1064 sService
.appendAscii((eVarType
==VarTypeUserField
) ?
1065 sAPI_user
: sAPI_set_expression
);
1066 Reference
<XInterface
> xIfc
=
1067 xFactory
->createInstance( sService
.makeStringAndClear() );
1069 Reference
<XPropertySet
> xTmp( xIfc
, UNO_QUERY
);
1075 xMaster
->setPropertyValue(
1077 OUString::createFromAscii(sAPI_name
)
1080 if (eVarType
!= VarTypeUserField
) {
1081 // set subtype for setexp field
1083 aAny
<<= ((eVarType
== VarTypeSimple
) ?
1084 SetVariableType::VAR
:
1085 SetVariableType::SEQUENCE
);
1086 xMaster
->setPropertyValue(
1088 OUString::createFromAscii(sAPI_sub_type
)
1090 } // else : user field: no subtype
1100 DBG_ASSERT(xMaster
.is(), "no field master found!?!");
1106 // Database Display field import
1110 TYPEINIT1( XMLDatabaseDisplayImportContext
, XMLDatabaseFieldImportContext
);
1112 XMLDatabaseDisplayImportContext::XMLDatabaseDisplayImportContext(
1113 SvXMLImport
& rImport
, XMLTextImportHelper
& rHlp
, sal_uInt16 nPrfx
,
1114 const OUString
& rLocalName
) :
1115 XMLDatabaseFieldImportContext(rImport
, rHlp
, sAPI_database
,
1116 nPrfx
, rLocalName
, false),
1117 sPropertyColumnName(RTL_CONSTASCII_USTRINGPARAM(sAPI_data_column_name
)),
1118 sPropertyDatabaseFormat(RTL_CONSTASCII_USTRINGPARAM(sAPI_is_data_base_format
)),
1119 sPropertyCurrentPresentation(RTL_CONSTASCII_USTRINGPARAM(sAPI_current_presentation
)),
1120 sPropertyIsVisible(RTL_CONSTASCII_USTRINGPARAM(sAPI_is_visible
)),
1121 aValueHelper(rImport
, rHlp
, sal_False
, sal_True
, sal_False
, sal_False
),
1122 bColumnOK(sal_False
),
1123 bDisplay( sal_True
),
1124 bDisplayOK( sal_False
)
1128 void XMLDatabaseDisplayImportContext::ProcessAttribute(
1129 sal_uInt16 nAttrToken
, const OUString
& sAttrValue
)
1133 case XML_TOK_TEXTFIELD_COLUMN_NAME
:
1134 sColumnName
= sAttrValue
;
1135 bColumnOK
= sal_True
;
1137 case XML_TOK_TEXTFIELD_DISPLAY
:
1139 sal_Bool bNone
= IsXMLToken( sAttrValue
, XML_NONE
);
1140 sal_Bool bValue
= IsXMLToken( sAttrValue
, XML_VALUE
);
1142 bDisplayOK
= bNone
|| bValue
;
1145 case XML_TOK_TEXTFIELD_DATABASE_NAME
:
1146 case XML_TOK_TEXTFIELD_TABLE_NAME
:
1147 case XML_TOK_TEXTFIELD_TABLE_TYPE
:
1148 // handled by super class
1149 XMLDatabaseFieldImportContext::ProcessAttribute(nAttrToken
,
1153 // remainder handled by value helper
1154 aValueHelper
.ProcessAttribute(nAttrToken
, sAttrValue
);
1158 bValid
= bTableOK
&& bDatabaseOK
&& bColumnOK
;
1161 void XMLDatabaseDisplayImportContext::EndElement()
1163 // we have an EndElement of our own, because database fields need
1164 // to be attached to a field master before they can be inserted into
1165 // the document. Database stuff (database, table, column) all goes
1166 // to the field master, value & style go to the field.
1171 // so here goes: we start with the master
1172 Reference
<XPropertySet
> xMaster
;
1174 // create and prepare field master first
1175 if (CreateField(xMaster
,
1176 OUString(RTL_CONSTASCII_USTRINGPARAM(
1177 sAPI_fieldmaster_database
))))
1180 aAny
<<= sColumnName
;
1181 xMaster
->setPropertyValue(sPropertyColumnName
, aAny
);
1183 // fieldmaster takes database, table and column name
1184 XMLDatabaseFieldImportContext::PrepareField(xMaster
);
1187 Reference
<XPropertySet
> xField
;
1188 if (CreateField(xField
,
1189 OUString(RTL_CONSTASCII_USTRINGPARAM(
1192 // attach field master
1193 Reference
<XDependentTextField
> xDepField(xField
, UNO_QUERY
);
1196 // attach field to field master
1197 xDepField
->attachTextFieldMaster(xMaster
);
1199 // attach field to document
1200 Reference
<XTextContent
> xTextContent(xField
, UNO_QUERY
);
1201 if (xTextContent
.is())
1203 // insert, set field properties and exit!
1204 GetImportHelper().InsertTextContent(xTextContent
);
1206 // prepare field: format from database?
1207 sal_Bool bTmp
= !aValueHelper
.IsFormatOK();
1208 aAny
.setValue( &bTmp
, ::getBooleanCppuType() );
1209 xField
->setPropertyValue(sPropertyDatabaseFormat
,aAny
);
1211 // value, value-type and format done by value helper
1212 aValueHelper
.PrepareField(xField
);
1217 aAny
.setValue( &bDisplay
, ::getBooleanCppuType() );
1218 xField
->setPropertyValue(sPropertyIsVisible
, aAny
);
1222 aAny
<<= GetContent();
1223 xField
->setPropertyValue(sPropertyCurrentPresentation
,
1234 // above: exit on success; so for all error cases we end up here!
1235 // write element content
1236 GetImportHelper().InsertString(GetContent());
1241 // value import helper
1246 XML_VALUE_TYPE_STRING
,
1247 XML_VALUE_TYPE_FLOAT
,
1248 XML_VALUE_TYPE_CURRENCY
,
1249 XML_VALUE_TYPE_PERCENTAGE
,
1250 XML_VALUE_TYPE_DATE
,
1251 XML_VALUE_TYPE_TIME
,
1252 XML_VALUE_TYPE_BOOLEAN
1255 static SvXMLEnumMapEntry __READONLY_DATA aValueTypeMap
[] =
1257 { XML_FLOAT
, XML_VALUE_TYPE_FLOAT
},
1258 { XML_CURRENCY
, XML_VALUE_TYPE_CURRENCY
},
1259 { XML_PERCENTAGE
, XML_VALUE_TYPE_PERCENTAGE
},
1260 { XML_DATE
, XML_VALUE_TYPE_DATE
},
1261 { XML_TIME
, XML_VALUE_TYPE_TIME
},
1262 { XML_BOOLEAN
, XML_VALUE_TYPE_BOOLEAN
},
1263 { XML_STRING
, XML_VALUE_TYPE_STRING
},
1264 { XML_TOKEN_INVALID
, 0 }
1267 XMLValueImportHelper::XMLValueImportHelper(
1268 SvXMLImport
& rImprt
,
1269 XMLTextImportHelper
& rHlp
,
1270 sal_Bool bType
, sal_Bool bStyle
, sal_Bool bValue
, sal_Bool bFormula
) :
1271 sPropertyContent(RTL_CONSTASCII_USTRINGPARAM(sAPI_content
)),
1272 sPropertyValue(RTL_CONSTASCII_USTRINGPARAM(sAPI_value
)),
1273 sPropertyNumberFormat(RTL_CONSTASCII_USTRINGPARAM(sAPI_number_format
)),
1274 sPropertyIsFixedLanguage(RTL_CONSTASCII_USTRINGPARAM(sAPI_is_fixed_language
)),
1281 bIsDefaultLanguage(sal_True
),
1283 bStringType(sal_False
),
1284 bFormatOK(sal_False
),
1286 bStringValueOK(sal_False
),
1287 bFloatValueOK(sal_False
),
1288 bFormulaOK(sal_False
),
1293 bSetFormula(bFormula
),
1295 bStringDefault(sal_True
),
1296 bFormulaDefault(sal_True
)
1300 XMLValueImportHelper::~XMLValueImportHelper()
1304 void XMLValueImportHelper::ProcessAttribute(
1305 sal_uInt16 nAttrToken
, const OUString
& sAttrValue
)
1309 case XML_TOK_TEXTFIELD_VALUE_TYPE
:
1312 sal_uInt16 nTmp
= 0;
1313 sal_Bool bRet
= SvXMLUnitConverter::convertEnum(
1314 nTmp
, sAttrValue
, aValueTypeMap
);
1317 ValueType eValueType
= (ValueType
)nTmp
;
1323 case XML_VALUE_TYPE_STRING
:
1324 bStringType
= sal_True
;
1326 case XML_VALUE_TYPE_FLOAT
:
1327 case XML_VALUE_TYPE_CURRENCY
:
1328 case XML_VALUE_TYPE_PERCENTAGE
:
1329 case XML_VALUE_TYPE_DATE
:
1330 case XML_VALUE_TYPE_TIME
:
1331 case XML_VALUE_TYPE_BOOLEAN
:
1332 bStringType
= sal_False
;
1336 DBG_ERROR("unknown value type");
1337 bTypeOK
= sal_False
;
1343 case XML_TOK_TEXTFIELD_VALUE
:
1346 sal_Bool bRet
= SvXMLUnitConverter::convertDouble(fTmp
,sAttrValue
);
1348 bFloatValueOK
= sal_True
;
1354 case XML_TOK_TEXTFIELD_TIME_VALUE
:
1357 sal_Bool bRet
= SvXMLUnitConverter::convertTime(fTmp
,sAttrValue
);
1359 bFloatValueOK
= sal_True
;
1365 case XML_TOK_TEXTFIELD_DATE_VALUE
:
1368 sal_Bool bRet
= rImport
.GetMM100UnitConverter().
1369 convertDateTime(fTmp
,sAttrValue
);
1371 bFloatValueOK
= sal_True
;
1377 case XML_TOK_TEXTFIELD_BOOL_VALUE
:
1380 sal_Bool bRet
= SvXMLUnitConverter::convertBool(bTmp
,sAttrValue
);
1382 bFloatValueOK
= sal_True
;
1383 fValue
= (bTmp
? 1.0 : 0.0);
1388 bRet
= SvXMLUnitConverter::convertDouble(fTmp
,sAttrValue
);
1390 bFloatValueOK
= sal_True
;
1397 case XML_TOK_TEXTFIELD_STRING_VALUE
:
1398 sValue
= sAttrValue
;
1399 bStringValueOK
= sal_True
;
1402 case XML_TOK_TEXTFIELD_FORMULA
:
1405 sal_uInt16 nPrefix
= rImport
.GetNamespaceMap().
1406 _GetKeyByAttrName( sAttrValue
, &sTmp
, sal_False
);
1407 if( XML_NAMESPACE_OOOW
== nPrefix
)
1410 bFormulaOK
= sal_True
;
1413 sFormula
= sAttrValue
;
1417 case XML_TOK_TEXTFIELD_DATA_STYLE_NAME
:
1419 sal_Int32 nKey
= rHelper
.GetDataStyleKey(
1420 sAttrValue
, &bIsDefaultLanguage
);
1424 bFormatOK
= sal_True
;
1431 void XMLValueImportHelper::PrepareField(
1432 const Reference
<XPropertySet
> & xPropertySet
)
1438 // ??? how to set type?
1443 aAny
<<= (!bFormulaOK
&& bFormulaDefault
) ? sDefault
: sFormula
;
1444 xPropertySet
->setPropertyValue(sPropertyContent
, aAny
);
1448 if (bSetStyle
&& bFormatOK
)
1450 aAny
<<= nFormatKey
;
1451 xPropertySet
->setPropertyValue(sPropertyNumberFormat
, aAny
);
1453 if( xPropertySet
->getPropertySetInfo()->
1454 hasPropertyByName( sPropertyIsFixedLanguage
) )
1456 sal_Bool bIsFixedLanguage
= ! bIsDefaultLanguage
;
1457 aAny
.setValue( &bIsFixedLanguage
, ::getBooleanCppuType() );
1458 xPropertySet
->setPropertyValue( sPropertyIsFixedLanguage
, aAny
);
1462 // value: string or float
1467 aAny
<<= (!bStringValueOK
&& bStringDefault
) ? sDefault
: sValue
;
1468 xPropertySet
->setPropertyValue(sPropertyContent
, aAny
);
1473 xPropertySet
->setPropertyValue(sPropertyValue
, aAny
);