1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
23 * export of all variable related text fields (and database display field)
25 #include "txtvfldi.hxx"
26 #include <xmloff/xmltoken.hxx>
27 #include <xmloff/txtimp.hxx>
28 #include <xmloff/xmlnumi.hxx>
29 #include "xmloff/xmlnmspe.hxx"
30 #include <xmloff/nmspmap.hxx>
31 #include "xmloff/i18nmap.hxx"
32 #include <xmloff/xmlimp.hxx>
33 #include <xmloff/xmluconv.hxx>
34 #include <xmloff/xmlement.hxx>
35 #include <com/sun/star/text/SetVariableType.hpp>
36 #include <com/sun/star/text/XTextField.hpp>
37 #include <com/sun/star/text/XDependentTextField.hpp>
38 #include <com/sun/star/text/XTextFieldsSupplier.hpp>
39 #include <com/sun/star/beans/XPropertySet.hpp>
40 #include <com/sun/star/beans/XPropertySetInfo.hpp>
41 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
42 #include <com/sun/star/xml/sax/XAttributeList.hpp>
44 #include <sax/tools/converter.hxx>
46 #include <rtl/ustring.hxx>
48 #include <tools/debug.hxx>
52 static const sal_Char sAPI_textfield_prefix
[] = "com.sun.star.text.TextField.";
53 static const sal_Char sAPI_fieldmaster_prefix
[] = "com.sun.star.text.FieldMaster.";
54 static const sal_Char sAPI_input
[] = "Input";
55 static const sal_Char sAPI_input_user
[] = "InputUser";
56 static const sal_Char sAPI_get_expression
[] = "GetExpression";
57 static const sal_Char sAPI_set_expression
[] = "SetExpression";
58 static const sal_Char sAPI_user
[] = "User";
59 static const sal_Char sAPI_table_formula
[] = "TableFormula";
60 static const sal_Char sAPI_database
[] = "com.sun.star.text.TextField.Database";
61 static const sal_Char sAPI_fieldmaster_database
[] = "com.sun.star.text.FieldMaster.Database";
64 static const sal_Char sAPI_hint
[] = "Hint";
65 static const sal_Char sAPI_help
[] = "Help";
66 static const sal_Char sAPI_tooltip
[] = "Tooltip";
67 static const sal_Char sAPI_content
[] = "Content";
68 static const sal_Char sAPI_sub_type
[] = "SubType";
69 static const sal_Char sAPI_is_expression
[] = "IsExpression";
70 static const sal_Char sAPI_is_input
[] = "Input";
71 static const sal_Char sAPI_is_show_formula
[] = "IsShowFormula";
72 static const sal_Char sAPI_numbering_type
[] = "NumberingType";
73 static const sal_Char sAPI_number_format
[] = "NumberFormat";
74 static const sal_Char sAPI_name
[] = "Name";
75 static const sal_Char sAPI_numbering_separator
[] = "NumberingSeparator";
76 static const sal_Char sAPI_chapter_numbering_level
[]= "ChapterNumberingLevel";
77 static const sal_Char sAPI_value
[] = "Value";
78 static const sal_Char sAPI_is_visible
[] = "IsVisible";
79 static const sal_Char sAPI_variable_subtype
[] = "VariableSubtype";
80 static const sal_Char sAPI_data_column_name
[] = "DataColumnName";
81 static const sal_Char sAPI_is_data_base_format
[] = "DataBaseFormat";
82 static const sal_Char sAPI_current_presentation
[] = "CurrentPresentation";
83 static const sal_Char sAPI_sequence_value
[] = "SequenceValue";
84 static const sal_Char sAPI_is_fixed_language
[] = "IsFixedLanguage";
88 using namespace ::com::sun::star
;
89 using namespace ::com::sun::star::uno
;
90 using namespace ::com::sun::star::beans
;
91 using namespace ::com::sun::star::text
;
92 using namespace ::com::sun::star::style
;
93 using namespace ::xmloff::token
;
98 // XMLVarFieldImportContext: superclass for all variable related fields
101 TYPEINIT1( XMLVarFieldImportContext
, XMLTextFieldImportContext
);
103 XMLVarFieldImportContext::XMLVarFieldImportContext(
104 SvXMLImport
& rImport
, XMLTextImportHelper
& rHlp
,
105 const sal_Char
* pServiceName
, sal_uInt16 nPrfx
,
106 const OUString
& rLocalName
,
107 sal_Bool bFormula
, sal_Bool bFormulaDefault
,
108 sal_Bool bDescription
, sal_Bool bHelp
, sal_Bool bHint
, sal_Bool bVisible
,
109 sal_Bool bIsDisplayFormula
,
110 sal_Bool bType
, sal_Bool bStyle
, sal_Bool bValue
,
111 sal_Bool bPresentation
) :
112 XMLTextFieldImportContext(rImport
, rHlp
, pServiceName
, nPrfx
, rLocalName
),
113 sPropertyContent(sAPI_content
),
114 sPropertyHint(sAPI_hint
),
115 sPropertyHelp(sAPI_help
),
116 sPropertyTooltip(sAPI_tooltip
),
117 sPropertyIsVisible(sAPI_is_visible
),
118 sPropertyIsDisplayFormula(sAPI_is_show_formula
),
119 sPropertyCurrentPresentation(sAPI_current_presentation
),
120 aValueHelper(rImport
, rHlp
, bType
, bStyle
, bValue
, sal_False
),
121 bDisplayFormula(sal_False
),
122 bDisplayNone(sal_False
),
124 bFormulaOK(sal_False
),
125 bDescriptionOK(sal_False
),
128 bDisplayOK(sal_False
),
129 bSetFormula(bFormula
),
130 bSetFormulaDefault(bFormulaDefault
),
131 bSetDescription(bDescription
),
134 bSetVisible(bVisible
),
135 bSetDisplayFormula(bIsDisplayFormula
),
136 bSetPresentation(bPresentation
)
140 void XMLVarFieldImportContext::ProcessAttribute(
141 sal_uInt16 nAttrToken
,
142 const OUString
& sAttrValue
)
146 case XML_TOK_TEXTFIELD_NAME
:
149 bValid
= sal_True
; // we assume: field with name is valid!
151 case XML_TOK_TEXTFIELD_DESCRIPTION
:
152 sDescription
= sAttrValue
;
153 bDescriptionOK
= sal_True
;
155 case XML_TOK_TEXTFIELD_HELP
:
159 case XML_TOK_TEXTFIELD_HINT
:
163 case XML_TOK_TEXTFIELD_FORMULA
:
166 sal_uInt16 nPrefix
= GetImport().GetNamespaceMap().
167 _GetKeyByAttrName( sAttrValue
, &sTmp
, sal_False
);
168 if( XML_NAMESPACE_OOOW
== nPrefix
)
171 bFormulaOK
= sal_True
;
174 sFormula
= sAttrValue
;
177 case XML_TOK_TEXTFIELD_DISPLAY
:
178 if (IsXMLToken(sAttrValue
, XML_FORMULA
))
180 bDisplayFormula
= sal_True
;
181 bDisplayNone
= sal_False
;
182 bDisplayOK
= sal_True
;
184 else if (IsXMLToken(sAttrValue
, XML_VALUE
))
186 bDisplayFormula
= sal_False
;
187 bDisplayNone
= sal_False
;
188 bDisplayOK
= sal_True
;
190 else if (IsXMLToken(sAttrValue
, XML_NONE
))
192 bDisplayFormula
= sal_False
;
193 bDisplayNone
= sal_True
;
194 bDisplayOK
= sal_True
;
196 DBG_ASSERT(!(bDisplayFormula
&& bDisplayNone
),
197 "illegal display values");
200 // delegate all others to value helper
201 aValueHelper
.ProcessAttribute(nAttrToken
, sAttrValue
);
206 void XMLVarFieldImportContext::PrepareField(
207 const Reference
<XPropertySet
> & xPropertySet
)
209 // bSetName: not implemented
213 if (!bFormulaOK
&& bSetFormulaDefault
)
215 sFormula
= GetContent();
216 bFormulaOK
= sal_True
;
223 xPropertySet
->setPropertyValue(sPropertyContent
, aAny
);
227 if (bSetDescription
&& bDescriptionOK
)
230 aAny
<<= sDescription
;
231 xPropertySet
->setPropertyValue(sPropertyHint
, aAny
);
234 if (bSetHelp
&& bHelpOK
)
238 xPropertySet
->setPropertyValue(sPropertyHelp
, aAny
);
241 if (bSetHint
&& bHintOK
)
245 xPropertySet
->setPropertyValue(sPropertyTooltip
, aAny
);
248 if (bSetVisible
&& bDisplayOK
)
251 sal_Bool bTmp
= ! (bDisplayNone
&& bDisplayOK
);
252 aAny
.setValue( &bTmp
, ::getBooleanCppuType());
253 xPropertySet
->setPropertyValue(sPropertyIsVisible
, aAny
);
256 // workaround for #no-bug#: display formula by default
257 if (xPropertySet
->getPropertySetInfo()->
258 hasPropertyByName(sPropertyIsDisplayFormula
) &&
261 bDisplayFormula
= sal_False
;
262 bSetDisplayFormula
= sal_True
;
266 if (bSetDisplayFormula
)
269 sal_Bool bTmp
= bDisplayFormula
&& bDisplayOK
;
270 aAny
.setValue( &bTmp
, ::getBooleanCppuType());
271 xPropertySet
->setPropertyValue(sPropertyIsDisplayFormula
, aAny
);
274 // delegate to value helper
275 aValueHelper
.SetDefault(GetContent());
276 aValueHelper
.PrepareField(xPropertySet
);
278 // finally, set the curren presentation
279 if (bSetPresentation
)
282 aAny
<<= GetContent();
283 xPropertySet
->setPropertyValue(sPropertyCurrentPresentation
, aAny
);
292 // variable set fields
295 TYPEINIT1( XMLSetVarFieldImportContext
, XMLVarFieldImportContext
);
297 XMLSetVarFieldImportContext::XMLSetVarFieldImportContext(
298 SvXMLImport
& rImport
, XMLTextImportHelper
& rHlp
,
299 const sal_Char
* pServiceName
, sal_uInt16 nPrfx
,
300 const OUString
& rLocalName
, VarType eVarType
,
301 sal_Bool bFormula
, sal_Bool bFormulaDefault
,
302 sal_Bool bDescription
, sal_Bool bHelp
, sal_Bool bHint
, sal_Bool bVisible
, sal_Bool bIsDisplayFormula
,
303 sal_Bool bType
, sal_Bool bStyle
, sal_Bool bValue
, sal_Bool bPresentation
) :
304 XMLVarFieldImportContext(rImport
, rHlp
, pServiceName
,
306 bFormula
, bFormulaDefault
,
307 bDescription
, bHelp
, bHint
, bVisible
, bIsDisplayFormula
,
308 bType
, bStyle
, bValue
, bPresentation
),
313 void XMLSetVarFieldImportContext::EndElement()
315 // should we call PrepareField on the field, or rather on it's master?
316 // currently: call on field (just like superclass)
317 // possible alternatives: call on master
318 // call field or master depending on variable
319 // PrepareMaster() in addition to PrepareField()
321 DBG_ASSERT(!GetServiceName().isEmpty(), "no service name for element!");
325 DBG_ASSERT(!GetName().isEmpty(), "variable name needed!");
328 Reference
<XPropertySet
> xMaster
;
329 if (FindFieldMaster(xMaster
))
331 // create field/Service
332 Reference
<XPropertySet
> xPropSet
;
333 if (CreateField(xPropSet
, OUString(sAPI_textfield_prefix
) + GetServiceName()))
335 Reference
<XDependentTextField
> xDepTextField(xPropSet
, UNO_QUERY
);
336 if (xDepTextField
.is())
338 // attach field to field master
339 xDepTextField
->attachTextFieldMaster(xMaster
);
341 // attach field to document
342 Reference
<XTextContent
> xTextContent(xPropSet
, UNO_QUERY
);
343 if (xTextContent
.is())
346 // insert, set field properties and exit!
347 GetImportHelper().InsertTextContent(xTextContent
);
348 PrepareField(xPropSet
);
349 } catch (lang::IllegalArgumentException
& /*e*/)
351 // ignore e: #i54023#
360 // above: exit on success; so for all error cases we end up here!
361 // write element content
362 GetImportHelper().InsertString(GetContent());
365 sal_Bool
XMLSetVarFieldImportContext::FindFieldMaster(
366 Reference
<XPropertySet
> & xMaster
)
368 // currently: delegate to XMLVariableDeclImportContext;
369 // shoud eventually go here
370 return XMLVariableDeclImportContext::FindFieldMaster(xMaster
,
383 TYPEINIT1( XMLSequenceFieldImportContext
, XMLSetVarFieldImportContext
);
385 XMLSequenceFieldImportContext::XMLSequenceFieldImportContext(
386 SvXMLImport
& rImport
, XMLTextImportHelper
& rHlp
,
387 sal_uInt16 nPrfx
, const OUString
& rLocalName
) :
388 XMLSetVarFieldImportContext(rImport
, rHlp
, sAPI_set_expression
,
389 nPrfx
, rLocalName
, VarTypeSequence
,
392 sal_False
, sal_False
, sal_False
, sal_False
,
394 sal_False
, sal_False
, sal_False
, sal_True
),
396 sPropertyNumberFormat(sAPI_number_format
),
397 sPropertySequenceValue(sAPI_sequence_value
),
398 sNumFormat(OUString::valueOf(sal_Unicode('1'))),
399 sNumFormatSync(GetXMLToken(XML_FALSE
)),
400 bRefNameOK(sal_False
)
404 void XMLSequenceFieldImportContext::ProcessAttribute(
405 sal_uInt16 nAttrToken
, const OUString
& sAttrValue
)
409 case XML_TOK_TEXTFIELD_NUM_FORMAT
:
410 sNumFormat
= sAttrValue
;
412 case XML_TOK_TEXTFIELD_NUM_LETTER_SYNC
:
413 sNumFormatSync
= sAttrValue
;
415 case XML_TOK_TEXTFIELD_REF_NAME
:
416 sRefName
= sAttrValue
;
417 bRefNameOK
= sal_True
;
420 // delegate to super class (name, formula)
421 XMLSetVarFieldImportContext::ProcessAttribute(nAttrToken
,
427 void XMLSequenceFieldImportContext::PrepareField(
428 const Reference
<XPropertySet
> & xPropertySet
)
430 // delegate to super class (formula)
431 XMLSetVarFieldImportContext::PrepareField(xPropertySet
);
434 sal_Int16 nNumType
= NumberingType::ARABIC
;
435 GetImport().GetMM100UnitConverter().convertNumFormat( nNumType
, sNumFormat
, sNumFormatSync
);
438 xPropertySet
->setPropertyValue(sPropertyNumberFormat
, aAny
);
440 // handle reference name
443 aAny
= xPropertySet
->getPropertyValue(sPropertySequenceValue
);
444 sal_Int16 nValue
= 0;
446 GetImportHelper().InsertSequenceID(sRefName
, GetName(), nValue
);
453 // variable set field
456 TYPEINIT1( XMLVariableSetFieldImportContext
, XMLSetVarFieldImportContext
);
458 XMLVariableSetFieldImportContext::XMLVariableSetFieldImportContext(
459 SvXMLImport
& rImport
, XMLTextImportHelper
& rHlp
,
460 sal_uInt16 nPrfx
, const OUString
& rLocalName
) :
461 XMLSetVarFieldImportContext(rImport
, rHlp
, sAPI_set_expression
,
462 nPrfx
, rLocalName
, VarTypeSimple
,
463 // formula, value&type, style,
466 sal_False
, sal_False
, sal_False
,
468 sal_True
, sal_True
, sal_True
,
470 sPropertySubType(sAPI_sub_type
)
474 void XMLVariableSetFieldImportContext::PrepareField(
475 const Reference
<XPropertySet
> & xPropertySet
)
479 aAny
<<= (IsStringValue()? SetVariableType::STRING
: SetVariableType::VAR
);
480 xPropertySet
->setPropertyValue(sPropertySubType
, aAny
);
482 // the remainder is handled by super class
483 XMLSetVarFieldImportContext::PrepareField(xPropertySet
);
489 // variable input field
492 TYPEINIT1( XMLVariableInputFieldImportContext
, XMLSetVarFieldImportContext
);
494 XMLVariableInputFieldImportContext::XMLVariableInputFieldImportContext(
495 SvXMLImport
& rImport
, XMLTextImportHelper
& rHlp
, sal_uInt16 nPrfx
,
496 const OUString
& rLocalName
) :
497 XMLSetVarFieldImportContext(rImport
, rHlp
, sAPI_set_expression
,
498 nPrfx
, rLocalName
, VarTypeSimple
,
499 // description, display none/formula,
500 // value&type, style, formula
502 sal_True
, sal_True
, sal_True
,
504 sal_True
, sal_True
, sal_True
,
506 sPropertySubType(sAPI_sub_type
),
507 sPropertyIsInput(sAPI_is_input
)
511 void XMLVariableInputFieldImportContext::PrepareField(
512 const Reference
<XPropertySet
> & xPropertySet
)
514 // set type (input field)
516 sal_Bool bTrue
= sal_True
;
517 aAny
.setValue( &bTrue
, ::getBooleanCppuType() );
518 xPropertySet
->setPropertyValue(sPropertyIsInput
, aAny
);
521 aAny
<<= (IsStringValue()? SetVariableType::STRING
: SetVariableType::VAR
);
522 xPropertySet
->setPropertyValue(sPropertySubType
, aAny
);
524 // the remainder is handled by super class
525 XMLSetVarFieldImportContext::PrepareField(xPropertySet
);
534 TYPEINIT1( XMLUserFieldImportContext
, XMLSetVarFieldImportContext
);
536 XMLUserFieldImportContext::XMLUserFieldImportContext(
537 SvXMLImport
& rImport
, XMLTextImportHelper
& rHlp
,
538 sal_uInt16 nPrfx
, const OUString
& rLocalName
) :
539 XMLSetVarFieldImportContext(rImport
, rHlp
, sAPI_user
, nPrfx
,
540 rLocalName
, VarTypeUserField
,
541 // display none/formula, style
542 sal_False
, sal_False
,
543 sal_False
, sal_False
, sal_False
, sal_True
,
545 sal_False
, sal_True
, sal_False
,
556 TYPEINIT1( XMLUserFieldInputImportContext
, XMLVarFieldImportContext
);
558 // bug: doesn't work (SO API lacking)
559 XMLUserFieldInputImportContext::XMLUserFieldInputImportContext(
560 SvXMLImport
& rImport
, XMLTextImportHelper
& rHlp
, sal_uInt16 nPrfx
,
561 const OUString
& rLocalName
) :
562 XMLVarFieldImportContext(rImport
, rHlp
, sAPI_input_user
,
564 // description, style
565 sal_False
, sal_False
,
566 sal_True
, sal_False
, sal_False
,
567 sal_False
, sal_False
,
568 sal_False
/*???*/, sal_True
, sal_False
,
573 void XMLUserFieldInputImportContext::PrepareField(
574 const Reference
<XPropertySet
> & xPropertySet
)
578 xPropertySet
->setPropertyValue(sPropertyContent
, aAny
);
580 // delegate to super class
581 XMLVarFieldImportContext::PrepareField(xPropertySet
);
586 // variable get field
589 TYPEINIT1( XMLVariableGetFieldImportContext
, XMLVarFieldImportContext
);
591 XMLVariableGetFieldImportContext::XMLVariableGetFieldImportContext(
592 SvXMLImport
& rImport
, XMLTextImportHelper
& rHlp
,
593 sal_uInt16 nPrfx
, const OUString
& rLocalName
) :
594 XMLVarFieldImportContext(rImport
, rHlp
, sAPI_get_expression
,
596 // style, display formula
597 sal_False
, sal_False
,
598 sal_False
, sal_False
, sal_False
,
600 sal_True
, sal_True
, sal_False
,
602 sPropertySubType(sAPI_sub_type
)
606 void XMLVariableGetFieldImportContext::PrepareField(
607 const Reference
<XPropertySet
> & xPropertySet
)
612 xPropertySet
->setPropertyValue(sPropertyContent
, aAny
);
614 // the remainder is handled by super class
615 XMLVarFieldImportContext::PrepareField(xPropertySet
);
624 TYPEINIT1( XMLExpressionFieldImportContext
, XMLVarFieldImportContext
);
626 XMLExpressionFieldImportContext::XMLExpressionFieldImportContext(
627 SvXMLImport
& rImport
, XMLTextImportHelper
& rHlp
,
628 sal_uInt16 nPrfx
, const OUString
& rLocalName
) :
629 XMLVarFieldImportContext(rImport
, rHlp
, sAPI_get_expression
,
631 // formula, type, style, display formula
633 sal_False
, sal_False
, sal_False
,
635 sal_True
, sal_True
, sal_False
,
637 sPropertySubType(sAPI_sub_type
)
639 bValid
= sal_True
; // always valid
643 void XMLExpressionFieldImportContext::PrepareField(
644 const Reference
<XPropertySet
> & xPropertySet
)
646 sal_Int16 nSubType
= SetVariableType::FORMULA
;
649 xPropertySet
->setPropertyValue(sPropertySubType
, aAny
);
651 // delegate to super class
652 XMLVarFieldImportContext::PrepareField(xPropertySet
);
661 TYPEINIT1( XMLTextInputFieldImportContext
, XMLVarFieldImportContext
);
663 XMLTextInputFieldImportContext::XMLTextInputFieldImportContext(
664 SvXMLImport
& rImport
, XMLTextImportHelper
& rHlp
,
665 sal_uInt16 nPrfx
, const OUString
& sLocalName
) :
666 XMLVarFieldImportContext(rImport
, rHlp
, sAPI_input
,
669 sal_False
, sal_False
,
670 sal_True
, sal_True
, sal_True
,
671 sal_False
, sal_False
,
672 sal_False
, sal_False
, sal_False
,
674 sPropertyContent(sAPI_content
)
676 bValid
= sal_True
; // always valid
679 void XMLTextInputFieldImportContext::PrepareField(
680 const Reference
<XPropertySet
> & xPropertySet
)
682 XMLVarFieldImportContext::PrepareField(xPropertySet
);
685 aAny
<<= GetContent();
686 xPropertySet
->setPropertyValue(sPropertyContent
, aAny
);
691 // table formula field
694 TYPEINIT1( XMLTableFormulaImportContext
, XMLTextFieldImportContext
);
696 XMLTableFormulaImportContext::XMLTableFormulaImportContext(
697 SvXMLImport
& rImport
,
698 XMLTextImportHelper
& rHlp
,
700 const OUString
& rLocalName
) :
701 XMLTextFieldImportContext(rImport
, rHlp
, sAPI_table_formula
,
703 sPropertyIsShowFormula("IsShowFormula"),
704 sPropertyCurrentPresentation(
705 "CurrentPresentation"),
706 aValueHelper(rImport
, rHlp
, sal_False
, sal_True
, sal_False
, sal_True
),
708 bIsShowFormula(sal_False
)
712 XMLTableFormulaImportContext::~XMLTableFormulaImportContext()
716 void XMLTableFormulaImportContext::ProcessAttribute(
717 sal_uInt16 nAttrToken
,
718 const OUString
& sAttrValue
)
722 case XML_TOK_TEXTFIELD_FORMULA
:
723 aValueHelper
.ProcessAttribute( nAttrToken
, sAttrValue
);
724 bValid
= sal_True
; // we need a formula!
727 case XML_TOK_TEXTFIELD_DATA_STYLE_NAME
:
728 aValueHelper
.ProcessAttribute( nAttrToken
, sAttrValue
);
730 case XML_TOK_TEXTFIELD_DISPLAY
:
731 if ( sAttrValue
== "formula" )
732 bIsShowFormula
= sal_True
;
735 // unknown attribute -> ignore
740 void XMLTableFormulaImportContext::PrepareField(
741 const Reference
<XPropertySet
> & xPropertySet
)
743 // set format and formula
744 aValueHelper
.PrepareField( xPropertySet
);
748 // set 'show formula' and presentation
749 aAny
.setValue( &bIsShowFormula
, ::getBooleanCppuType() );
750 xPropertySet
->setPropertyValue( sPropertyIsShowFormula
, aAny
);
752 aAny
<<= GetContent();
753 xPropertySet
->setPropertyValue( sPropertyCurrentPresentation
, aAny
);
759 // variable declarations
761 // Should be adapted to XMLVarField-/XMLSetVarFieldImportContext scheme!
767 // declaration containter import (<variable/user-field/sequence-decls>)
770 TYPEINIT1( XMLVariableDeclsImportContext
, SvXMLImportContext
);
772 XMLVariableDeclsImportContext::XMLVariableDeclsImportContext(
773 SvXMLImport
& rImport
, XMLTextImportHelper
& rHlp
, sal_uInt16 nPrfx
,
774 const OUString
& rLocalName
, enum VarType eVarType
) :
775 SvXMLImportContext(rImport
, nPrfx
, rLocalName
),
776 eVarDeclsContextType(eVarType
),
781 SvXMLImportContext
* XMLVariableDeclsImportContext::CreateChildContext(
782 sal_uInt16 nPrefix
, const OUString
& rLocalName
,
783 const Reference
<xml::sax::XAttributeList
> & xAttrList
)
785 enum XMLTokenEnum eElementName
;
786 SvXMLImportContext
* pImportContext
= NULL
;
788 if( XML_NAMESPACE_TEXT
== nPrefix
)
790 switch (eVarDeclsContextType
)
792 case VarTypeSequence
:
793 eElementName
= XML_SEQUENCE_DECL
;
796 eElementName
= XML_VARIABLE_DECL
;
798 case VarTypeUserField
:
799 eElementName
= XML_USER_FIELD_DECL
;
802 OSL_FAIL("unknown field type!");
803 eElementName
= XML_SEQUENCE_DECL
;
807 if( IsXMLToken( rLocalName
, eElementName
) )
809 pImportContext
= new XMLVariableDeclImportContext(
810 GetImport(), rImportHelper
, nPrefix
, rLocalName
, xAttrList
,
811 eVarDeclsContextType
);
815 // if no context was created, use default context
816 if (NULL
== pImportContext
) {
817 pImportContext
= SvXMLImportContext::CreateChildContext(nPrefix
,
822 return pImportContext
;
828 // declaration import (<variable/user-field/sequence-decl> elements)
831 TYPEINIT1( XMLVariableDeclImportContext
, SvXMLImportContext
);
833 XMLVariableDeclImportContext::XMLVariableDeclImportContext(
834 SvXMLImport
& rImport
, XMLTextImportHelper
& rHlp
,
835 sal_uInt16 nPrfx
, const OUString
& rLocalName
,
836 const Reference
<xml::sax::XAttributeList
> & xAttrList
,
837 enum VarType eVarType
) :
838 SvXMLImportContext(rImport
, nPrfx
, rLocalName
),
839 // bug?? which properties for userfield/userfieldmaster
840 sPropertyName(sAPI_name
),
841 sPropertySubType(sAPI_sub_type
),
842 sPropertyNumberingLevel(sAPI_chapter_numbering_level
),
843 sPropertyNumberingSeparator(sAPI_numbering_separator
),
844 sPropertyIsExpression(sAPI_is_expression
),
845 aValueHelper(rImport
, rHlp
, sal_True
, sal_False
, sal_True
, sal_False
),
846 nNumLevel(-1), cSeparationChar('.')
848 if ( (XML_NAMESPACE_TEXT
== nPrfx
) &&
849 ( ( IsXMLToken( rLocalName
, XML_SEQUENCE_DECL
)) ||
850 ( IsXMLToken( rLocalName
, XML_VARIABLE_DECL
)) ||
851 ( IsXMLToken( rLocalName
, XML_USER_FIELD_DECL
)) )) {
853 // TODO: check validity (need name!)
856 sal_Int16 nLength
= xAttrList
->getLength();
857 for(sal_Int16 i
=0; i
<nLength
; i
++) {
860 sal_uInt16 nPrefix
= GetImport().GetNamespaceMap().
861 GetKeyByAttrName( xAttrList
->getNameByIndex(i
), &sLocalName
);
863 sal_uInt16 nToken
= rHlp
.
864 GetTextFieldAttrTokenMap().Get(nPrefix
, sLocalName
);
868 case XML_TOK_TEXTFIELD_NAME
:
869 sName
= xAttrList
->getValueByIndex(i
);
871 case XML_TOK_TEXTFIELD_NUMBERING_LEVEL
:
874 bool const bRet
= ::sax::Converter::convertNumber(
875 nLevel
, xAttrList
->getValueByIndex(i
), 0,
876 GetImport().GetTextImport()->GetChapterNumbering()->
880 nNumLevel
= static_cast< sal_Int8
>( nLevel
-1 ); // API numbers -1..9
884 case XML_TOK_TEXTFIELD_NUMBERING_SEPARATOR
:
886 (sal_Char
)xAttrList
->getValueByIndex(i
).toChar();
890 // delegate to value helper
891 aValueHelper
.ProcessAttribute(nToken
,
892 xAttrList
->getValueByIndex(i
));
897 Reference
<XPropertySet
> xFieldMaster
;
898 if (FindFieldMaster(xFieldMaster
, GetImport(), rHlp
,
901 // now we have a field master: process attributes!
906 case VarTypeSequence
:
908 xFieldMaster
->setPropertyValue(sPropertyNumberingLevel
, aAny
);
912 OUString
sStr(&cSeparationChar
, 1);
914 xFieldMaster
->setPropertyValue(
915 sPropertyNumberingSeparator
, aAny
);
920 // set string or non-string SubType (#93192#)
921 // The SubType was already set in the FindFieldMaster
922 // method, but it needs to be adjusted if it's a string.
923 aAny
<<= aValueHelper
.IsStringValue()
924 ? SetVariableType::STRING
: SetVariableType::VAR
;
925 xFieldMaster
->setPropertyValue(sPropertySubType
, aAny
);
928 case VarTypeUserField
:
930 sal_Bool bTmp
= !aValueHelper
.IsStringValue();
931 aAny
.setValue(&bTmp
, ::getBooleanCppuType());
932 xFieldMaster
->setPropertyValue(sPropertyIsExpression
, aAny
);
933 aValueHelper
.PrepareField(xFieldMaster
);
937 OSL_FAIL("unknown varfield type");
939 } // else: no field master found/constructed
940 } // else: no sequence-decl
945 sal_Bool
XMLVariableDeclImportContext::FindFieldMaster(
946 Reference
<XPropertySet
> & xMaster
, SvXMLImport
& rImport
,
947 XMLTextImportHelper
& rImportHelper
,
948 const OUString
& sVarName
, enum VarType eVarType
)
950 static sal_Int32 nCollisionCount
= 0;
953 // currently: no family in use! Use 0.
954 OUString sName
= rImportHelper
.GetRenameMap().Get(
955 sal::static_int_cast
< sal_uInt16
>(eVarType
), sVarName
);
957 // get text fields supplier and field masters
958 Reference
<XTextFieldsSupplier
> xTextFieldsSupp(rImport
.GetModel(),
960 Reference
<container::XNameAccess
> xFieldMasterNameAccess(
961 xTextFieldsSupp
->getTextFieldMasters(), UNO_QUERY
);
963 OUStringBuffer sBuffer
;
964 sBuffer
.appendAscii(sAPI_fieldmaster_prefix
);
965 sBuffer
.appendAscii(sAPI_set_expression
);
966 sBuffer
.appendAscii(".");
967 sBuffer
.append(sName
);
968 OUString sVarServiceName
= sBuffer
.makeStringAndClear();
970 sBuffer
.appendAscii(sAPI_fieldmaster_prefix
);
971 sBuffer
.appendAscii(sAPI_user
);
972 sBuffer
.appendAscii(".");
973 sBuffer
.append(sName
);
974 OUString sUserServiceName
= sBuffer
.makeStringAndClear();
976 if (xFieldMasterNameAccess
->hasByName(sVarServiceName
)) {
977 // variable field master already in document
979 Any aAny
= xFieldMasterNameAccess
->getByName(sVarServiceName
);
982 aAny
= xMaster
->getPropertyValue(
984 OUString(sAPI_sub_type
)
989 enum VarType eFMVarType
=
990 (SetVariableType::SEQUENCE
== nType
) ?
991 VarTypeSequence
: VarTypeSimple
;
993 if (eFMVarType
!= eVarType
)
997 // FIXME! cant find if name is taken already!!!!
1000 OUStringBuffer aBuf
;
1002 aBuf
.appendAscii("_renamed_");
1003 aBuf
.append(nCollisionCount
);
1004 sNew
= aBuf
.makeStringAndClear();
1006 rImportHelper
.GetRenameMap().Add(
1007 sal::static_int_cast
< sal_uInt16
>(eVarType
), sName
, sNew
);
1009 // call FindFieldMaster recursively to create new master
1010 return FindFieldMaster(xMaster
, rImport
, rImportHelper
,
1013 } else if (xFieldMasterNameAccess
->hasByName(sUserServiceName
)) {
1014 // user field: get field master
1015 Any aAny
= xFieldMasterNameAccess
->getByName(sUserServiceName
);
1018 if (VarTypeUserField
!= eVarType
) {
1019 // find new name that is not taken
1022 // FIXME! cant find if name is taken already!!!!
1025 OUStringBuffer aBuf
;
1027 aBuf
.appendAscii("_renamed_");
1028 aBuf
.append(nCollisionCount
);
1029 sNew
= aBuf
.makeStringAndClear();
1031 rImportHelper
.GetRenameMap().Add(
1032 sal::static_int_cast
< sal_uInt16
>(eVarType
), sName
, sNew
);
1034 // call FindFieldMaster recursively to create new master
1035 return FindFieldMaster(xMaster
, rImport
, rImportHelper
,
1039 // field name not used: create field master
1041 // import -> model is MultiServiceFactory -> createInstance
1042 Reference
<lang::XMultiServiceFactory
>
1043 xFactory(rImport
.GetModel(),UNO_QUERY
);
1044 if( xFactory
.is() ) {
1046 OUStringBuffer sService
;
1047 sService
.appendAscii(sAPI_fieldmaster_prefix
);
1048 sService
.appendAscii((eVarType
==VarTypeUserField
) ?
1049 sAPI_user
: sAPI_set_expression
);
1050 Reference
<XInterface
> xIfc
=
1051 xFactory
->createInstance( sService
.makeStringAndClear() );
1053 Reference
<XPropertySet
> xTmp( xIfc
, UNO_QUERY
);
1059 xMaster
->setPropertyValue(
1064 if (eVarType
!= VarTypeUserField
) {
1065 // set subtype for setexp field
1067 aAny
<<= ((eVarType
== VarTypeSimple
) ?
1068 SetVariableType::VAR
:
1069 SetVariableType::SEQUENCE
);
1070 xMaster
->setPropertyValue(
1072 OUString(sAPI_sub_type
)
1074 } // else : user field: no subtype
1084 DBG_ASSERT(xMaster
.is(), "no field master found!?!");
1090 // Database Display field import
1094 TYPEINIT1( XMLDatabaseDisplayImportContext
, XMLDatabaseFieldImportContext
);
1096 XMLDatabaseDisplayImportContext::XMLDatabaseDisplayImportContext(
1097 SvXMLImport
& rImport
, XMLTextImportHelper
& rHlp
, sal_uInt16 nPrfx
,
1098 const OUString
& rLocalName
) :
1099 XMLDatabaseFieldImportContext(rImport
, rHlp
, sAPI_database
,
1100 nPrfx
, rLocalName
, false),
1101 sPropertyColumnName(sAPI_data_column_name
),
1102 sPropertyDatabaseFormat(sAPI_is_data_base_format
),
1103 sPropertyCurrentPresentation(sAPI_current_presentation
),
1104 sPropertyIsVisible(sAPI_is_visible
),
1105 aValueHelper(rImport
, rHlp
, sal_False
, sal_True
, sal_False
, sal_False
),
1106 bColumnOK(sal_False
),
1107 bDisplay( sal_True
),
1108 bDisplayOK( sal_False
)
1112 void XMLDatabaseDisplayImportContext::ProcessAttribute(
1113 sal_uInt16 nAttrToken
, const OUString
& sAttrValue
)
1117 case XML_TOK_TEXTFIELD_COLUMN_NAME
:
1118 sColumnName
= sAttrValue
;
1119 bColumnOK
= sal_True
;
1121 case XML_TOK_TEXTFIELD_DISPLAY
:
1123 sal_Bool bNone
= IsXMLToken( sAttrValue
, XML_NONE
);
1124 sal_Bool bValue
= IsXMLToken( sAttrValue
, XML_VALUE
);
1126 bDisplayOK
= bNone
|| bValue
;
1129 case XML_TOK_TEXTFIELD_DATABASE_NAME
:
1130 case XML_TOK_TEXTFIELD_TABLE_NAME
:
1131 case XML_TOK_TEXTFIELD_TABLE_TYPE
:
1132 // handled by super class
1133 XMLDatabaseFieldImportContext::ProcessAttribute(nAttrToken
,
1137 // remainder handled by value helper
1138 aValueHelper
.ProcessAttribute(nAttrToken
, sAttrValue
);
1142 bValid
= bTableOK
&& bDatabaseOK
&& bColumnOK
;
1145 void XMLDatabaseDisplayImportContext::EndElement()
1147 // we have an EndElement of our own, because database fields need
1148 // to be attached to a field master before they can be inserted into
1149 // the document. Database stuff (database, table, column) all goes
1150 // to the field master, value & style go to the field.
1155 // so here goes: we start with the master
1156 Reference
<XPropertySet
> xMaster
;
1158 // create and prepare field master first
1159 if (CreateField(xMaster
,
1161 sAPI_fieldmaster_database
)))
1164 aAny
<<= sColumnName
;
1165 xMaster
->setPropertyValue(sPropertyColumnName
, aAny
);
1167 // fieldmaster takes database, table and column name
1168 XMLDatabaseFieldImportContext::PrepareField(xMaster
);
1171 Reference
<XPropertySet
> xField
;
1172 if (CreateField(xField
,
1176 // attach field master
1177 Reference
<XDependentTextField
> xDepField(xField
, UNO_QUERY
);
1180 // attach field to field master
1181 xDepField
->attachTextFieldMaster(xMaster
);
1183 // attach field to document
1184 Reference
<XTextContent
> xTextContent(xField
, UNO_QUERY
);
1185 if (xTextContent
.is())
1187 // insert, set field properties and exit!
1188 GetImportHelper().InsertTextContent(xTextContent
);
1190 // prepare field: format from database?
1191 sal_Bool bTmp
= !aValueHelper
.IsFormatOK();
1192 aAny
.setValue( &bTmp
, ::getBooleanCppuType() );
1193 xField
->setPropertyValue(sPropertyDatabaseFormat
,aAny
);
1195 // value, value-type and format done by value helper
1196 aValueHelper
.PrepareField(xField
);
1201 aAny
.setValue( &bDisplay
, ::getBooleanCppuType() );
1202 xField
->setPropertyValue(sPropertyIsVisible
, aAny
);
1206 aAny
<<= GetContent();
1207 xField
->setPropertyValue(sPropertyCurrentPresentation
,
1218 // above: exit on success; so for all error cases we end up here!
1219 // write element content
1220 GetImportHelper().InsertString(GetContent());
1225 // value import helper
1230 XML_VALUE_TYPE_STRING
,
1231 XML_VALUE_TYPE_FLOAT
,
1232 XML_VALUE_TYPE_CURRENCY
,
1233 XML_VALUE_TYPE_PERCENTAGE
,
1234 XML_VALUE_TYPE_DATE
,
1235 XML_VALUE_TYPE_TIME
,
1236 XML_VALUE_TYPE_BOOLEAN
1239 static SvXMLEnumMapEntry
const aValueTypeMap
[] =
1241 { XML_FLOAT
, XML_VALUE_TYPE_FLOAT
},
1242 { XML_CURRENCY
, XML_VALUE_TYPE_CURRENCY
},
1243 { XML_PERCENTAGE
, XML_VALUE_TYPE_PERCENTAGE
},
1244 { XML_DATE
, XML_VALUE_TYPE_DATE
},
1245 { XML_TIME
, XML_VALUE_TYPE_TIME
},
1246 { XML_BOOLEAN
, XML_VALUE_TYPE_BOOLEAN
},
1247 { XML_STRING
, XML_VALUE_TYPE_STRING
},
1248 { XML_TOKEN_INVALID
, 0 }
1251 XMLValueImportHelper::XMLValueImportHelper(
1252 SvXMLImport
& rImprt
,
1253 XMLTextImportHelper
& rHlp
,
1254 sal_Bool bType
, sal_Bool bStyle
, sal_Bool bValue
, sal_Bool bFormula
) :
1255 sPropertyContent(sAPI_content
),
1256 sPropertyValue(sAPI_value
),
1257 sPropertyNumberFormat(sAPI_number_format
),
1258 sPropertyIsFixedLanguage(sAPI_is_fixed_language
),
1265 bIsDefaultLanguage(sal_True
),
1267 bStringType(sal_False
),
1268 bFormatOK(sal_False
),
1270 bStringValueOK(sal_False
),
1271 bFloatValueOK(sal_False
),
1272 bFormulaOK(sal_False
),
1277 bSetFormula(bFormula
),
1279 bStringDefault(sal_True
),
1280 bFormulaDefault(sal_True
)
1284 XMLValueImportHelper::~XMLValueImportHelper()
1288 void XMLValueImportHelper::ProcessAttribute(
1289 sal_uInt16 nAttrToken
, const OUString
& sAttrValue
)
1293 case XML_TOK_TEXTFIELD_VALUE_TYPE
:
1296 sal_uInt16 nTmp
= 0;
1297 sal_Bool bRet
= SvXMLUnitConverter::convertEnum(
1298 nTmp
, sAttrValue
, aValueTypeMap
);
1301 ValueType eValueType
= (ValueType
)nTmp
;
1307 case XML_VALUE_TYPE_STRING
:
1308 bStringType
= sal_True
;
1310 case XML_VALUE_TYPE_FLOAT
:
1311 case XML_VALUE_TYPE_CURRENCY
:
1312 case XML_VALUE_TYPE_PERCENTAGE
:
1313 case XML_VALUE_TYPE_DATE
:
1314 case XML_VALUE_TYPE_TIME
:
1315 case XML_VALUE_TYPE_BOOLEAN
:
1316 bStringType
= sal_False
;
1320 OSL_FAIL("unknown value type");
1321 bTypeOK
= sal_False
;
1327 case XML_TOK_TEXTFIELD_VALUE
:
1330 bool const bRet
= ::sax::Converter::convertDouble(fTmp
,sAttrValue
);
1332 bFloatValueOK
= sal_True
;
1338 case XML_TOK_TEXTFIELD_TIME_VALUE
:
1342 ::sax::Converter::convertDuration(fTmp
, sAttrValue
);
1344 bFloatValueOK
= sal_True
;
1350 case XML_TOK_TEXTFIELD_DATE_VALUE
:
1353 sal_Bool bRet
= rImport
.GetMM100UnitConverter().
1354 convertDateTime(fTmp
,sAttrValue
);
1356 bFloatValueOK
= sal_True
;
1362 case XML_TOK_TEXTFIELD_BOOL_VALUE
:
1365 bool bRet
= ::sax::Converter::convertBool(bTmp
, sAttrValue
);
1367 bFloatValueOK
= sal_True
;
1368 fValue
= (bTmp
? 1.0 : 0.0);
1373 bRet
= ::sax::Converter::convertDouble(fTmp
, sAttrValue
);
1375 bFloatValueOK
= sal_True
;
1382 case XML_TOK_TEXTFIELD_STRING_VALUE
:
1383 sValue
= sAttrValue
;
1384 bStringValueOK
= sal_True
;
1387 case XML_TOK_TEXTFIELD_FORMULA
:
1390 sal_uInt16 nPrefix
= rImport
.GetNamespaceMap().
1391 _GetKeyByAttrName( sAttrValue
, &sTmp
, sal_False
);
1392 if( XML_NAMESPACE_OOOW
== nPrefix
)
1395 bFormulaOK
= sal_True
;
1398 sFormula
= sAttrValue
;
1402 case XML_TOK_TEXTFIELD_DATA_STYLE_NAME
:
1404 sal_Int32 nKey
= rHelper
.GetDataStyleKey(
1405 sAttrValue
, &bIsDefaultLanguage
);
1409 bFormatOK
= sal_True
;
1416 void XMLValueImportHelper::PrepareField(
1417 const Reference
<XPropertySet
> & xPropertySet
)
1423 // ??? how to set type?
1428 aAny
<<= (!bFormulaOK
&& bFormulaDefault
) ? sDefault
: sFormula
;
1429 xPropertySet
->setPropertyValue(sPropertyContent
, aAny
);
1433 if (bSetStyle
&& bFormatOK
)
1435 aAny
<<= nFormatKey
;
1436 xPropertySet
->setPropertyValue(sPropertyNumberFormat
, aAny
);
1438 if( xPropertySet
->getPropertySetInfo()->
1439 hasPropertyByName( sPropertyIsFixedLanguage
) )
1441 sal_Bool bIsFixedLanguage
= ! bIsDefaultLanguage
;
1442 aAny
.setValue( &bIsFixedLanguage
, ::getBooleanCppuType() );
1443 xPropertySet
->setPropertyValue( sPropertyIsFixedLanguage
, aAny
);
1447 // value: string or float
1452 aAny
<<= (!bStringValueOK
&& bStringDefault
) ? sDefault
: sValue
;
1453 xPropertySet
->setPropertyValue(sPropertyContent
, aAny
);
1458 xPropertySet
->setPropertyValue(sPropertyValue
, aAny
);
1463 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */