1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
32 * export of all variable related text fields (and database display field)
34 #include "txtvfldi.hxx"
35 #include <xmloff/xmltoken.hxx>
36 #include <xmloff/txtimp.hxx>
37 #include <xmloff/xmlnumi.hxx>
38 #include "xmloff/xmlnmspe.hxx"
39 #include <xmloff/nmspmap.hxx>
40 #include "xmloff/i18nmap.hxx"
41 #include <xmloff/xmlimp.hxx>
42 #include <xmloff/xmluconv.hxx>
43 #include <xmloff/xmlement.hxx>
44 #include <com/sun/star/text/SetVariableType.hpp>
45 #include <com/sun/star/text/XTextField.hpp>
46 #include <com/sun/star/text/XDependentTextField.hpp>
47 #include <com/sun/star/text/XTextFieldsSupplier.hpp>
48 #include <com/sun/star/beans/XPropertySet.hpp>
49 #include <com/sun/star/beans/XPropertySetInfo.hpp>
50 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
51 #include <com/sun/star/xml/sax/XAttributeList.hpp>
53 #include <sax/tools/converter.hxx>
55 #include <rtl/ustring.hxx>
57 #include <tools/debug.hxx>
61 static const sal_Char sAPI_textfield_prefix
[] = "com.sun.star.text.TextField.";
62 static const sal_Char sAPI_fieldmaster_prefix
[] = "com.sun.star.text.FieldMaster.";
63 static const sal_Char sAPI_input
[] = "Input";
64 static const sal_Char sAPI_input_user
[] = "InputUser";
65 static const sal_Char sAPI_get_expression
[] = "GetExpression";
66 static const sal_Char sAPI_set_expression
[] = "SetExpression";
67 static const sal_Char sAPI_user
[] = "User";
68 static const sal_Char sAPI_table_formula
[] = "TableFormula";
69 static const sal_Char sAPI_database
[] = "com.sun.star.text.TextField.Database";
70 static const sal_Char sAPI_fieldmaster_database
[] = "com.sun.star.text.FieldMaster.Database";
73 static const sal_Char sAPI_hint
[] = "Hint";
74 static const sal_Char sAPI_help
[] = "Help";
75 static const sal_Char sAPI_tooltip
[] = "Tooltip";
76 static const sal_Char sAPI_content
[] = "Content";
77 static const sal_Char sAPI_sub_type
[] = "SubType";
78 static const sal_Char sAPI_is_expression
[] = "IsExpression";
79 static const sal_Char sAPI_is_input
[] = "Input";
80 static const sal_Char sAPI_is_show_formula
[] = "IsShowFormula";
81 static const sal_Char sAPI_numbering_type
[] = "NumberingType";
82 static const sal_Char sAPI_number_format
[] = "NumberFormat";
83 static const sal_Char sAPI_name
[] = "Name";
84 static const sal_Char sAPI_numbering_separator
[] = "NumberingSeparator";
85 static const sal_Char sAPI_chapter_numbering_level
[]= "ChapterNumberingLevel";
86 static const sal_Char sAPI_value
[] = "Value";
87 static const sal_Char sAPI_is_visible
[] = "IsVisible";
88 static const sal_Char sAPI_variable_subtype
[] = "VariableSubtype";
89 static const sal_Char sAPI_data_column_name
[] = "DataColumnName";
90 static const sal_Char sAPI_is_data_base_format
[] = "DataBaseFormat";
91 static const sal_Char sAPI_current_presentation
[] = "CurrentPresentation";
92 static const sal_Char sAPI_sequence_value
[] = "SequenceValue";
93 static const sal_Char sAPI_is_fixed_language
[] = "IsFixedLanguage";
96 using ::rtl::OUString
;
97 using ::rtl::OUStringBuffer
;
99 using namespace ::com::sun::star
;
100 using namespace ::com::sun::star::uno
;
101 using namespace ::com::sun::star::beans
;
102 using namespace ::com::sun::star::text
;
103 using namespace ::com::sun::star::style
;
104 using namespace ::xmloff::token
;
109 // XMLVarFieldImportContext: superclass for all variable related fields
112 TYPEINIT1( XMLVarFieldImportContext
, XMLTextFieldImportContext
);
114 XMLVarFieldImportContext::XMLVarFieldImportContext(
115 SvXMLImport
& rImport
, XMLTextImportHelper
& rHlp
,
116 const sal_Char
* pServiceName
, sal_uInt16 nPrfx
,
117 const OUString
& rLocalName
,
118 sal_Bool bName
, sal_Bool bFormula
, sal_Bool bFormulaDefault
,
119 sal_Bool bDescription
, sal_Bool bHelp
, sal_Bool bHint
, sal_Bool bVisible
,
120 sal_Bool bIsDisplayFormula
,
121 sal_Bool bType
, sal_Bool bStyle
, sal_Bool bValue
,
122 sal_Bool bPresentation
) :
123 XMLTextFieldImportContext(rImport
, rHlp
, pServiceName
, nPrfx
, rLocalName
),
124 sPropertyContent(sAPI_content
),
125 sPropertyHint(sAPI_hint
),
126 sPropertyHelp(sAPI_help
),
127 sPropertyTooltip(sAPI_tooltip
),
128 sPropertyIsVisible(sAPI_is_visible
),
129 sPropertyIsDisplayFormula(sAPI_is_show_formula
),
130 sPropertyCurrentPresentation(sAPI_current_presentation
),
131 aValueHelper(rImport
, rHlp
, bType
, bStyle
, bValue
, sal_False
),
132 bDisplayFormula(sal_False
),
133 bDisplayNone(sal_False
),
135 bFormulaOK(sal_False
),
136 bDescriptionOK(sal_False
),
139 bDisplayOK(sal_False
),
141 bSetFormula(bFormula
),
142 bSetFormulaDefault(bFormulaDefault
),
143 bSetDescription(bDescription
),
146 bSetVisible(bVisible
),
147 bSetDisplayFormula(bIsDisplayFormula
),
148 bSetPresentation(bPresentation
)
152 void XMLVarFieldImportContext::ProcessAttribute(
153 sal_uInt16 nAttrToken
,
154 const OUString
& sAttrValue
)
158 case XML_TOK_TEXTFIELD_NAME
:
161 bValid
= sal_True
; // we assume: field with name is valid!
163 case XML_TOK_TEXTFIELD_DESCRIPTION
:
164 sDescription
= sAttrValue
;
165 bDescriptionOK
= sal_True
;
167 case XML_TOK_TEXTFIELD_HELP
:
171 case XML_TOK_TEXTFIELD_HINT
:
175 case XML_TOK_TEXTFIELD_FORMULA
:
178 sal_uInt16 nPrefix
= GetImport().GetNamespaceMap().
179 _GetKeyByAttrName( sAttrValue
, &sTmp
, sal_False
);
180 if( XML_NAMESPACE_OOOW
== nPrefix
)
183 bFormulaOK
= sal_True
;
186 sFormula
= sAttrValue
;
189 case XML_TOK_TEXTFIELD_DISPLAY
:
190 if (IsXMLToken(sAttrValue
, XML_FORMULA
))
192 bDisplayFormula
= sal_True
;
193 bDisplayNone
= sal_False
;
194 bDisplayOK
= sal_True
;
196 else if (IsXMLToken(sAttrValue
, XML_VALUE
))
198 bDisplayFormula
= sal_False
;
199 bDisplayNone
= sal_False
;
200 bDisplayOK
= sal_True
;
202 else if (IsXMLToken(sAttrValue
, XML_NONE
))
204 bDisplayFormula
= sal_False
;
205 bDisplayNone
= sal_True
;
206 bDisplayOK
= sal_True
;
208 DBG_ASSERT(!(bDisplayFormula
&& bDisplayNone
),
209 "illegal display values");
212 // delegate all others to value helper
213 aValueHelper
.ProcessAttribute(nAttrToken
, sAttrValue
);
218 void XMLVarFieldImportContext::PrepareField(
219 const Reference
<XPropertySet
> & xPropertySet
)
221 // bSetName: not implemented
225 if (!bFormulaOK
&& bSetFormulaDefault
)
227 sFormula
= GetContent();
228 bFormulaOK
= sal_True
;
235 xPropertySet
->setPropertyValue(sPropertyContent
, aAny
);
239 if (bSetDescription
&& bDescriptionOK
)
242 aAny
<<= sDescription
;
243 xPropertySet
->setPropertyValue(sPropertyHint
, aAny
);
246 if (bSetHelp
&& bHelpOK
)
250 xPropertySet
->setPropertyValue(sPropertyHelp
, aAny
);
253 if (bSetHint
&& bHintOK
)
257 xPropertySet
->setPropertyValue(sPropertyTooltip
, aAny
);
260 if (bSetVisible
&& bDisplayOK
)
263 sal_Bool bTmp
= ! (bDisplayNone
&& bDisplayOK
);
264 aAny
.setValue( &bTmp
, ::getBooleanCppuType());
265 xPropertySet
->setPropertyValue(sPropertyIsVisible
, aAny
);
268 // workaround for #no-bug#: display formula by default
269 if (xPropertySet
->getPropertySetInfo()->
270 hasPropertyByName(sPropertyIsDisplayFormula
) &&
273 bDisplayFormula
= sal_False
;
274 bSetDisplayFormula
= sal_True
;
278 if (bSetDisplayFormula
)
281 sal_Bool bTmp
= bDisplayFormula
&& bDisplayOK
;
282 aAny
.setValue( &bTmp
, ::getBooleanCppuType());
283 xPropertySet
->setPropertyValue(sPropertyIsDisplayFormula
, aAny
);
286 // delegate to value helper
287 aValueHelper
.SetDefault(GetContent());
288 aValueHelper
.PrepareField(xPropertySet
);
290 // finally, set the curren presentation
291 if (bSetPresentation
)
294 aAny
<<= GetContent();
295 xPropertySet
->setPropertyValue(sPropertyCurrentPresentation
, aAny
);
304 // variable set fields
307 TYPEINIT1( XMLSetVarFieldImportContext
, XMLVarFieldImportContext
);
309 XMLSetVarFieldImportContext::XMLSetVarFieldImportContext(
310 SvXMLImport
& rImport
, XMLTextImportHelper
& rHlp
,
311 const sal_Char
* pServiceName
, sal_uInt16 nPrfx
,
312 const OUString
& rLocalName
, VarType eVarType
,
313 sal_Bool bName
, sal_Bool bFormula
, sal_Bool bFormulaDefault
,
314 sal_Bool bDescription
, sal_Bool bHelp
, sal_Bool bHint
, sal_Bool bVisible
, sal_Bool bIsDisplayFormula
,
315 sal_Bool bType
, sal_Bool bStyle
, sal_Bool bValue
, sal_Bool bPresentation
) :
316 XMLVarFieldImportContext(rImport
, rHlp
, pServiceName
,
318 bName
, bFormula
, bFormulaDefault
,
319 bDescription
, bHelp
, bHint
, bVisible
, bIsDisplayFormula
,
320 bType
, bStyle
, bValue
, bPresentation
),
325 void XMLSetVarFieldImportContext::EndElement()
327 // should we call PrepareField on the field, or rather on it's master?
328 // currently: call on field (just like superclass)
329 // possible alternatives: call on master
330 // call field or master depending on variable
331 // PrepareMaster() in addition to PrepareField()
333 DBG_ASSERT(!GetServiceName().isEmpty(), "no service name for element!");
337 DBG_ASSERT(!GetName().isEmpty(), "variable name needed!");
340 Reference
<XPropertySet
> xMaster
;
341 if (FindFieldMaster(xMaster
))
343 // create field/Service
344 Reference
<XPropertySet
> xPropSet
;
345 if (CreateField(xPropSet
, OUString(sAPI_textfield_prefix
) + GetServiceName()))
347 Reference
<XDependentTextField
> xDepTextField(xPropSet
, UNO_QUERY
);
348 if (xDepTextField
.is())
350 // attach field to field master
351 xDepTextField
->attachTextFieldMaster(xMaster
);
353 // attach field to document
354 Reference
<XTextContent
> xTextContent(xPropSet
, UNO_QUERY
);
355 if (xTextContent
.is())
358 // insert, set field properties and exit!
359 GetImportHelper().InsertTextContent(xTextContent
);
360 PrepareField(xPropSet
);
361 } catch (lang::IllegalArgumentException
& /*e*/)
363 // ignore e: #i54023#
372 // above: exit on success; so for all error cases we end up here!
373 // write element content
374 GetImportHelper().InsertString(GetContent());
377 sal_Bool
XMLSetVarFieldImportContext::FindFieldMaster(
378 Reference
<XPropertySet
> & xMaster
)
380 // currently: delegate to XMLVariableDeclImportContext;
381 // shoud eventually go here
382 return XMLVariableDeclImportContext::FindFieldMaster(xMaster
,
395 TYPEINIT1( XMLSequenceFieldImportContext
, XMLSetVarFieldImportContext
);
397 XMLSequenceFieldImportContext::XMLSequenceFieldImportContext(
398 SvXMLImport
& rImport
, XMLTextImportHelper
& rHlp
,
399 sal_uInt16 nPrfx
, const OUString
& rLocalName
) :
400 XMLSetVarFieldImportContext(rImport
, rHlp
, sAPI_set_expression
,
401 nPrfx
, rLocalName
, VarTypeSequence
,
403 sal_True
, sal_True
, sal_True
,
404 sal_False
, sal_False
, sal_False
, sal_False
,
406 sal_False
, sal_False
, sal_False
, sal_True
),
408 sPropertyNumberFormat(sAPI_number_format
),
409 sPropertySequenceValue(sAPI_sequence_value
),
410 sNumFormat(OUString::valueOf(sal_Unicode('1'))),
411 sNumFormatSync(GetXMLToken(XML_FALSE
)),
412 bRefNameOK(sal_False
)
416 void XMLSequenceFieldImportContext::ProcessAttribute(
417 sal_uInt16 nAttrToken
, const OUString
& sAttrValue
)
421 case XML_TOK_TEXTFIELD_NUM_FORMAT
:
422 sNumFormat
= sAttrValue
;
424 case XML_TOK_TEXTFIELD_NUM_LETTER_SYNC
:
425 sNumFormatSync
= sAttrValue
;
427 case XML_TOK_TEXTFIELD_REF_NAME
:
428 sRefName
= sAttrValue
;
429 bRefNameOK
= sal_True
;
432 // delegate to super class (name, formula)
433 XMLSetVarFieldImportContext::ProcessAttribute(nAttrToken
,
439 void XMLSequenceFieldImportContext::PrepareField(
440 const Reference
<XPropertySet
> & xPropertySet
)
442 // delegate to super class (formula)
443 XMLSetVarFieldImportContext::PrepareField(xPropertySet
);
446 sal_Int16 nNumType
= NumberingType::ARABIC
;
447 GetImport().GetMM100UnitConverter().convertNumFormat( nNumType
, sNumFormat
, sNumFormatSync
);
450 xPropertySet
->setPropertyValue(sPropertyNumberFormat
, aAny
);
452 // handle reference name
455 aAny
= xPropertySet
->getPropertyValue(sPropertySequenceValue
);
456 sal_Int16 nValue
= 0;
458 GetImportHelper().InsertSequenceID(sRefName
, GetName(), nValue
);
465 // variable set field
468 TYPEINIT1( XMLVariableSetFieldImportContext
, XMLSetVarFieldImportContext
);
470 XMLVariableSetFieldImportContext::XMLVariableSetFieldImportContext(
471 SvXMLImport
& rImport
, XMLTextImportHelper
& rHlp
,
472 sal_uInt16 nPrfx
, const OUString
& rLocalName
) :
473 XMLSetVarFieldImportContext(rImport
, rHlp
, sAPI_set_expression
,
474 nPrfx
, rLocalName
, VarTypeSimple
,
475 // name, formula, value&type, style,
477 sal_True
, sal_True
, sal_True
,
478 sal_False
, sal_False
, sal_False
,
480 sal_True
, sal_True
, sal_True
,
482 sPropertySubType(sAPI_sub_type
)
486 void XMLVariableSetFieldImportContext::PrepareField(
487 const Reference
<XPropertySet
> & xPropertySet
)
491 aAny
<<= (IsStringValue()? SetVariableType::STRING
: SetVariableType::VAR
);
492 xPropertySet
->setPropertyValue(sPropertySubType
, aAny
);
494 // the remainder is handled by super class
495 XMLSetVarFieldImportContext::PrepareField(xPropertySet
);
501 // variable input field
504 TYPEINIT1( XMLVariableInputFieldImportContext
, XMLSetVarFieldImportContext
);
506 XMLVariableInputFieldImportContext::XMLVariableInputFieldImportContext(
507 SvXMLImport
& rImport
, XMLTextImportHelper
& rHlp
, sal_uInt16 nPrfx
,
508 const OUString
& rLocalName
) :
509 XMLSetVarFieldImportContext(rImport
, rHlp
, sAPI_set_expression
,
510 nPrfx
, rLocalName
, VarTypeSimple
,
511 // name, description, display none/formula,
512 // value&type, style, formula
513 sal_True
, sal_True
, sal_True
,
514 sal_True
, sal_True
, sal_True
,
516 sal_True
, sal_True
, sal_True
,
518 sPropertySubType(sAPI_sub_type
),
519 sPropertyIsInput(sAPI_is_input
)
523 void XMLVariableInputFieldImportContext::PrepareField(
524 const Reference
<XPropertySet
> & xPropertySet
)
526 // set type (input field)
528 sal_Bool bTrue
= sal_True
;
529 aAny
.setValue( &bTrue
, ::getBooleanCppuType() );
530 xPropertySet
->setPropertyValue(sPropertyIsInput
, aAny
);
533 aAny
<<= (IsStringValue()? SetVariableType::STRING
: SetVariableType::VAR
);
534 xPropertySet
->setPropertyValue(sPropertySubType
, aAny
);
536 // the remainder is handled by super class
537 XMLSetVarFieldImportContext::PrepareField(xPropertySet
);
546 TYPEINIT1( XMLUserFieldImportContext
, XMLSetVarFieldImportContext
);
548 XMLUserFieldImportContext::XMLUserFieldImportContext(
549 SvXMLImport
& rImport
, XMLTextImportHelper
& rHlp
,
550 sal_uInt16 nPrfx
, const OUString
& rLocalName
) :
551 XMLSetVarFieldImportContext(rImport
, rHlp
, sAPI_user
, nPrfx
,
552 rLocalName
, VarTypeUserField
,
553 // name, display none/formula, style
554 sal_True
, sal_False
, sal_False
,
555 sal_False
, sal_False
, sal_False
, sal_True
,
557 sal_False
, sal_True
, sal_False
,
568 TYPEINIT1( XMLUserFieldInputImportContext
, XMLVarFieldImportContext
);
570 // bug: doesn't work (SO API lacking)
571 XMLUserFieldInputImportContext::XMLUserFieldInputImportContext(
572 SvXMLImport
& rImport
, XMLTextImportHelper
& rHlp
, sal_uInt16 nPrfx
,
573 const OUString
& rLocalName
) :
574 XMLVarFieldImportContext(rImport
, rHlp
, sAPI_input_user
,
576 // name, description, style
577 sal_True
, sal_False
, sal_False
,
578 sal_True
, sal_False
, sal_False
,
579 sal_False
, sal_False
,
580 sal_False
/*???*/, sal_True
, sal_False
,
585 void XMLUserFieldInputImportContext::PrepareField(
586 const Reference
<XPropertySet
> & xPropertySet
)
590 xPropertySet
->setPropertyValue(sPropertyContent
, aAny
);
592 // delegate to super class
593 XMLVarFieldImportContext::PrepareField(xPropertySet
);
598 // variable get field
601 TYPEINIT1( XMLVariableGetFieldImportContext
, XMLVarFieldImportContext
);
603 XMLVariableGetFieldImportContext::XMLVariableGetFieldImportContext(
604 SvXMLImport
& rImport
, XMLTextImportHelper
& rHlp
,
605 sal_uInt16 nPrfx
, const OUString
& rLocalName
) :
606 XMLVarFieldImportContext(rImport
, rHlp
, sAPI_get_expression
,
608 // name, style, display formula
609 sal_True
, sal_False
, sal_False
,
610 sal_False
, sal_False
, sal_False
,
612 sal_True
, sal_True
, sal_False
,
614 sPropertySubType(sAPI_sub_type
)
618 void XMLVariableGetFieldImportContext::PrepareField(
619 const Reference
<XPropertySet
> & xPropertySet
)
624 xPropertySet
->setPropertyValue(sPropertyContent
, aAny
);
626 // the remainder is handled by super class
627 XMLVarFieldImportContext::PrepareField(xPropertySet
);
636 TYPEINIT1( XMLExpressionFieldImportContext
, XMLVarFieldImportContext
);
638 XMLExpressionFieldImportContext::XMLExpressionFieldImportContext(
639 SvXMLImport
& rImport
, XMLTextImportHelper
& rHlp
,
640 sal_uInt16 nPrfx
, const OUString
& rLocalName
) :
641 XMLVarFieldImportContext(rImport
, rHlp
, sAPI_get_expression
,
643 // formula, type, style, display formula
644 sal_False
, sal_True
, sal_True
,
645 sal_False
, sal_False
, sal_False
,
647 sal_True
, sal_True
, sal_False
,
649 sPropertySubType(sAPI_sub_type
)
651 bValid
= sal_True
; // always valid
655 void XMLExpressionFieldImportContext::PrepareField(
656 const Reference
<XPropertySet
> & xPropertySet
)
658 sal_Int16 nSubType
= SetVariableType::FORMULA
;
661 xPropertySet
->setPropertyValue(sPropertySubType
, aAny
);
663 // delegate to super class
664 XMLVarFieldImportContext::PrepareField(xPropertySet
);
673 TYPEINIT1( XMLTextInputFieldImportContext
, XMLVarFieldImportContext
);
675 XMLTextInputFieldImportContext::XMLTextInputFieldImportContext(
676 SvXMLImport
& rImport
, XMLTextImportHelper
& rHlp
,
677 sal_uInt16 nPrfx
, const OUString
& sLocalName
) :
678 XMLVarFieldImportContext(rImport
, rHlp
, sAPI_input
,
681 sal_False
, sal_False
, sal_False
,
682 sal_True
, sal_True
, sal_True
,
683 sal_False
, sal_False
,
684 sal_False
, sal_False
, sal_False
,
686 sPropertyContent(sAPI_content
)
688 bValid
= sal_True
; // always valid
691 void XMLTextInputFieldImportContext::PrepareField(
692 const Reference
<XPropertySet
> & xPropertySet
)
694 XMLVarFieldImportContext::PrepareField(xPropertySet
);
697 aAny
<<= GetContent();
698 xPropertySet
->setPropertyValue(sPropertyContent
, aAny
);
703 // table formula field
706 TYPEINIT1( XMLTableFormulaImportContext
, XMLTextFieldImportContext
);
708 XMLTableFormulaImportContext::XMLTableFormulaImportContext(
709 SvXMLImport
& rImport
,
710 XMLTextImportHelper
& rHlp
,
712 const OUString
& rLocalName
) :
713 XMLTextFieldImportContext(rImport
, rHlp
, sAPI_table_formula
,
715 sPropertyIsShowFormula("IsShowFormula"),
716 sPropertyCurrentPresentation(
717 "CurrentPresentation"),
718 aValueHelper(rImport
, rHlp
, sal_False
, sal_True
, sal_False
, sal_True
),
720 bIsShowFormula(sal_False
)
724 XMLTableFormulaImportContext::~XMLTableFormulaImportContext()
728 void XMLTableFormulaImportContext::ProcessAttribute(
729 sal_uInt16 nAttrToken
,
730 const OUString
& sAttrValue
)
734 case XML_TOK_TEXTFIELD_FORMULA
:
735 aValueHelper
.ProcessAttribute( nAttrToken
, sAttrValue
);
736 bValid
= sal_True
; // we need a formula!
739 case XML_TOK_TEXTFIELD_DATA_STYLE_NAME
:
740 aValueHelper
.ProcessAttribute( nAttrToken
, sAttrValue
);
742 case XML_TOK_TEXTFIELD_DISPLAY
:
743 if ( sAttrValue
== "formula" )
744 bIsShowFormula
= sal_True
;
747 // unknown attribute -> ignore
752 void XMLTableFormulaImportContext::PrepareField(
753 const Reference
<XPropertySet
> & xPropertySet
)
755 // set format and formula
756 aValueHelper
.PrepareField( xPropertySet
);
760 // set 'show formula' and presentation
761 aAny
.setValue( &bIsShowFormula
, ::getBooleanCppuType() );
762 xPropertySet
->setPropertyValue( sPropertyIsShowFormula
, aAny
);
764 aAny
<<= GetContent();
765 xPropertySet
->setPropertyValue( sPropertyCurrentPresentation
, aAny
);
771 // variable declarations
773 // Should be adapted to XMLVarField-/XMLSetVarFieldImportContext scheme!
779 // declaration containter import (<variable/user-field/sequence-decls>)
782 TYPEINIT1( XMLVariableDeclsImportContext
, SvXMLImportContext
);
784 XMLVariableDeclsImportContext::XMLVariableDeclsImportContext(
785 SvXMLImport
& rImport
, XMLTextImportHelper
& rHlp
, sal_uInt16 nPrfx
,
786 const OUString
& rLocalName
, enum VarType eVarType
) :
787 SvXMLImportContext(rImport
, nPrfx
, rLocalName
),
788 eVarDeclsContextType(eVarType
),
793 SvXMLImportContext
* XMLVariableDeclsImportContext::CreateChildContext(
794 sal_uInt16 nPrefix
, const OUString
& rLocalName
,
795 const Reference
<xml::sax::XAttributeList
> & xAttrList
)
797 enum XMLTokenEnum eElementName
;
798 SvXMLImportContext
* pImportContext
= NULL
;
800 if( XML_NAMESPACE_TEXT
== nPrefix
)
802 switch (eVarDeclsContextType
)
804 case VarTypeSequence
:
805 eElementName
= XML_SEQUENCE_DECL
;
808 eElementName
= XML_VARIABLE_DECL
;
810 case VarTypeUserField
:
811 eElementName
= XML_USER_FIELD_DECL
;
814 OSL_FAIL("unknown field type!");
815 eElementName
= XML_SEQUENCE_DECL
;
819 if( IsXMLToken( rLocalName
, eElementName
) )
821 pImportContext
= new XMLVariableDeclImportContext(
822 GetImport(), rImportHelper
, nPrefix
, rLocalName
, xAttrList
,
823 eVarDeclsContextType
);
827 // if no context was created, use default context
828 if (NULL
== pImportContext
) {
829 pImportContext
= SvXMLImportContext::CreateChildContext(nPrefix
,
834 return pImportContext
;
840 // declaration import (<variable/user-field/sequence-decl> elements)
843 TYPEINIT1( XMLVariableDeclImportContext
, SvXMLImportContext
);
845 XMLVariableDeclImportContext::XMLVariableDeclImportContext(
846 SvXMLImport
& rImport
, XMLTextImportHelper
& rHlp
,
847 sal_uInt16 nPrfx
, const OUString
& rLocalName
,
848 const Reference
<xml::sax::XAttributeList
> & xAttrList
,
849 enum VarType eVarType
) :
850 SvXMLImportContext(rImport
, nPrfx
, rLocalName
),
851 // bug?? which properties for userfield/userfieldmaster
852 sPropertyName(sAPI_name
),
853 sPropertySubType(sAPI_sub_type
),
854 sPropertyNumberingLevel(sAPI_chapter_numbering_level
),
855 sPropertyNumberingSeparator(sAPI_numbering_separator
),
856 sPropertyIsExpression(sAPI_is_expression
),
857 aValueHelper(rImport
, rHlp
, sal_True
, sal_False
, sal_True
, sal_False
),
858 nNumLevel(-1), cSeparationChar('.')
860 if ( (XML_NAMESPACE_TEXT
== nPrfx
) &&
861 ( ( IsXMLToken( rLocalName
, XML_SEQUENCE_DECL
)) ||
862 ( IsXMLToken( rLocalName
, XML_VARIABLE_DECL
)) ||
863 ( IsXMLToken( rLocalName
, XML_USER_FIELD_DECL
)) )) {
865 // TODO: check validity (need name!)
868 sal_Int16 nLength
= xAttrList
->getLength();
869 for(sal_Int16 i
=0; i
<nLength
; i
++) {
872 sal_uInt16 nPrefix
= GetImport().GetNamespaceMap().
873 GetKeyByAttrName( xAttrList
->getNameByIndex(i
), &sLocalName
);
875 sal_uInt16 nToken
= rHlp
.
876 GetTextFieldAttrTokenMap().Get(nPrefix
, sLocalName
);
880 case XML_TOK_TEXTFIELD_NAME
:
881 sName
= xAttrList
->getValueByIndex(i
);
883 case XML_TOK_TEXTFIELD_NUMBERING_LEVEL
:
886 bool const bRet
= ::sax::Converter::convertNumber(
887 nLevel
, xAttrList
->getValueByIndex(i
), 0,
888 GetImport().GetTextImport()->GetChapterNumbering()->
892 nNumLevel
= static_cast< sal_Int8
>( nLevel
-1 ); // API numbers -1..9
896 case XML_TOK_TEXTFIELD_NUMBERING_SEPARATOR
:
898 (sal_Char
)xAttrList
->getValueByIndex(i
).toChar();
902 // delegate to value helper
903 aValueHelper
.ProcessAttribute(nToken
,
904 xAttrList
->getValueByIndex(i
));
909 Reference
<XPropertySet
> xFieldMaster
;
910 if (FindFieldMaster(xFieldMaster
, GetImport(), rHlp
,
913 // now we have a field master: process attributes!
918 case VarTypeSequence
:
920 xFieldMaster
->setPropertyValue(sPropertyNumberingLevel
, aAny
);
924 OUString
sStr(&cSeparationChar
, 1);
926 xFieldMaster
->setPropertyValue(
927 sPropertyNumberingSeparator
, aAny
);
932 // set string or non-string SubType (#93192#)
933 // The SubType was already set in the FindFieldMaster
934 // method, but it needs to be adjusted if it's a string.
935 aAny
<<= aValueHelper
.IsStringValue()
936 ? SetVariableType::STRING
: SetVariableType::VAR
;
937 xFieldMaster
->setPropertyValue(sPropertySubType
, aAny
);
940 case VarTypeUserField
:
942 sal_Bool bTmp
= !aValueHelper
.IsStringValue();
943 aAny
.setValue(&bTmp
, ::getBooleanCppuType());
944 xFieldMaster
->setPropertyValue(sPropertyIsExpression
, aAny
);
945 aValueHelper
.PrepareField(xFieldMaster
);
949 OSL_FAIL("unkown varfield type");
951 } // else: no field master found/constructed
952 } // else: no sequence-decl
957 sal_Bool
XMLVariableDeclImportContext::FindFieldMaster(
958 Reference
<XPropertySet
> & xMaster
, SvXMLImport
& rImport
,
959 XMLTextImportHelper
& rImportHelper
,
960 const OUString
& sVarName
, enum VarType eVarType
)
962 static sal_Int32 nCollisionCount
= 0;
965 // currently: no family in use! Use 0.
966 OUString sName
= rImportHelper
.GetRenameMap().Get(
967 sal::static_int_cast
< sal_uInt16
>(eVarType
), sVarName
);
969 // get text fields supplier and field masters
970 Reference
<XTextFieldsSupplier
> xTextFieldsSupp(rImport
.GetModel(),
972 Reference
<container::XNameAccess
> xFieldMasterNameAccess(
973 xTextFieldsSupp
->getTextFieldMasters(), UNO_QUERY
);
975 OUStringBuffer sBuffer
;
976 sBuffer
.appendAscii(sAPI_fieldmaster_prefix
);
977 sBuffer
.appendAscii(sAPI_set_expression
);
978 sBuffer
.appendAscii(".");
979 sBuffer
.append(sName
);
980 OUString sVarServiceName
= sBuffer
.makeStringAndClear();
982 sBuffer
.appendAscii(sAPI_fieldmaster_prefix
);
983 sBuffer
.appendAscii(sAPI_user
);
984 sBuffer
.appendAscii(".");
985 sBuffer
.append(sName
);
986 OUString sUserServiceName
= sBuffer
.makeStringAndClear();
988 if (xFieldMasterNameAccess
->hasByName(sVarServiceName
)) {
989 // variable field master already in document
991 Any aAny
= xFieldMasterNameAccess
->getByName(sVarServiceName
);
994 aAny
= xMaster
->getPropertyValue(
996 OUString(sAPI_sub_type
)
1001 enum VarType eFMVarType
=
1002 (SetVariableType::SEQUENCE
== nType
) ?
1003 VarTypeSequence
: VarTypeSimple
;
1005 if (eFMVarType
!= eVarType
)
1009 // FIXME! cant find if name is taken already!!!!
1012 OUStringBuffer aBuf
;
1014 aBuf
.appendAscii("_renamed_");
1015 aBuf
.append(nCollisionCount
);
1016 sNew
= aBuf
.makeStringAndClear();
1018 rImportHelper
.GetRenameMap().Add(
1019 sal::static_int_cast
< sal_uInt16
>(eVarType
), sName
, sNew
);
1021 // call FindFieldMaster recursively to create new master
1022 return FindFieldMaster(xMaster
, rImport
, rImportHelper
,
1025 } else if (xFieldMasterNameAccess
->hasByName(sUserServiceName
)) {
1026 // user field: get field master
1027 Any aAny
= xFieldMasterNameAccess
->getByName(sUserServiceName
);
1030 if (VarTypeUserField
!= eVarType
) {
1031 // find new name that is not taken
1034 // FIXME! cant find if name is taken already!!!!
1037 OUStringBuffer aBuf
;
1039 aBuf
.appendAscii("_renamed_");
1040 aBuf
.append(nCollisionCount
);
1041 sNew
= aBuf
.makeStringAndClear();
1043 rImportHelper
.GetRenameMap().Add(
1044 sal::static_int_cast
< sal_uInt16
>(eVarType
), sName
, sNew
);
1046 // call FindFieldMaster recursively to create new master
1047 return FindFieldMaster(xMaster
, rImport
, rImportHelper
,
1051 // field name not used: create field master
1053 // import -> model is MultiServiceFactory -> createInstance
1054 Reference
<lang::XMultiServiceFactory
>
1055 xFactory(rImport
.GetModel(),UNO_QUERY
);
1056 if( xFactory
.is() ) {
1058 OUStringBuffer sService
;
1059 sService
.appendAscii(sAPI_fieldmaster_prefix
);
1060 sService
.appendAscii((eVarType
==VarTypeUserField
) ?
1061 sAPI_user
: sAPI_set_expression
);
1062 Reference
<XInterface
> xIfc
=
1063 xFactory
->createInstance( sService
.makeStringAndClear() );
1065 Reference
<XPropertySet
> xTmp( xIfc
, UNO_QUERY
);
1071 xMaster
->setPropertyValue(
1076 if (eVarType
!= VarTypeUserField
) {
1077 // set subtype for setexp field
1079 aAny
<<= ((eVarType
== VarTypeSimple
) ?
1080 SetVariableType::VAR
:
1081 SetVariableType::SEQUENCE
);
1082 xMaster
->setPropertyValue(
1084 OUString(sAPI_sub_type
)
1086 } // else : user field: no subtype
1096 DBG_ASSERT(xMaster
.is(), "no field master found!?!");
1102 // Database Display field import
1106 TYPEINIT1( XMLDatabaseDisplayImportContext
, XMLDatabaseFieldImportContext
);
1108 XMLDatabaseDisplayImportContext::XMLDatabaseDisplayImportContext(
1109 SvXMLImport
& rImport
, XMLTextImportHelper
& rHlp
, sal_uInt16 nPrfx
,
1110 const OUString
& rLocalName
) :
1111 XMLDatabaseFieldImportContext(rImport
, rHlp
, sAPI_database
,
1112 nPrfx
, rLocalName
, false),
1113 sPropertyColumnName(sAPI_data_column_name
),
1114 sPropertyDatabaseFormat(sAPI_is_data_base_format
),
1115 sPropertyCurrentPresentation(sAPI_current_presentation
),
1116 sPropertyIsVisible(sAPI_is_visible
),
1117 aValueHelper(rImport
, rHlp
, sal_False
, sal_True
, sal_False
, sal_False
),
1118 bColumnOK(sal_False
),
1119 bDisplay( sal_True
),
1120 bDisplayOK( sal_False
)
1124 void XMLDatabaseDisplayImportContext::ProcessAttribute(
1125 sal_uInt16 nAttrToken
, const OUString
& sAttrValue
)
1129 case XML_TOK_TEXTFIELD_COLUMN_NAME
:
1130 sColumnName
= sAttrValue
;
1131 bColumnOK
= sal_True
;
1133 case XML_TOK_TEXTFIELD_DISPLAY
:
1135 sal_Bool bNone
= IsXMLToken( sAttrValue
, XML_NONE
);
1136 sal_Bool bValue
= IsXMLToken( sAttrValue
, XML_VALUE
);
1138 bDisplayOK
= bNone
|| bValue
;
1141 case XML_TOK_TEXTFIELD_DATABASE_NAME
:
1142 case XML_TOK_TEXTFIELD_TABLE_NAME
:
1143 case XML_TOK_TEXTFIELD_TABLE_TYPE
:
1144 // handled by super class
1145 XMLDatabaseFieldImportContext::ProcessAttribute(nAttrToken
,
1149 // remainder handled by value helper
1150 aValueHelper
.ProcessAttribute(nAttrToken
, sAttrValue
);
1154 bValid
= bTableOK
&& bDatabaseOK
&& bColumnOK
;
1157 void XMLDatabaseDisplayImportContext::EndElement()
1159 // we have an EndElement of our own, because database fields need
1160 // to be attached to a field master before they can be inserted into
1161 // the document. Database stuff (database, table, column) all goes
1162 // to the field master, value & style go to the field.
1167 // so here goes: we start with the master
1168 Reference
<XPropertySet
> xMaster
;
1170 // create and prepare field master first
1171 if (CreateField(xMaster
,
1173 sAPI_fieldmaster_database
)))
1176 aAny
<<= sColumnName
;
1177 xMaster
->setPropertyValue(sPropertyColumnName
, aAny
);
1179 // fieldmaster takes database, table and column name
1180 XMLDatabaseFieldImportContext::PrepareField(xMaster
);
1183 Reference
<XPropertySet
> xField
;
1184 if (CreateField(xField
,
1188 // attach field master
1189 Reference
<XDependentTextField
> xDepField(xField
, UNO_QUERY
);
1192 // attach field to field master
1193 xDepField
->attachTextFieldMaster(xMaster
);
1195 // attach field to document
1196 Reference
<XTextContent
> xTextContent(xField
, UNO_QUERY
);
1197 if (xTextContent
.is())
1199 // insert, set field properties and exit!
1200 GetImportHelper().InsertTextContent(xTextContent
);
1202 // prepare field: format from database?
1203 sal_Bool bTmp
= !aValueHelper
.IsFormatOK();
1204 aAny
.setValue( &bTmp
, ::getBooleanCppuType() );
1205 xField
->setPropertyValue(sPropertyDatabaseFormat
,aAny
);
1207 // value, value-type and format done by value helper
1208 aValueHelper
.PrepareField(xField
);
1213 aAny
.setValue( &bDisplay
, ::getBooleanCppuType() );
1214 xField
->setPropertyValue(sPropertyIsVisible
, aAny
);
1218 aAny
<<= GetContent();
1219 xField
->setPropertyValue(sPropertyCurrentPresentation
,
1230 // above: exit on success; so for all error cases we end up here!
1231 // write element content
1232 GetImportHelper().InsertString(GetContent());
1237 // value import helper
1242 XML_VALUE_TYPE_STRING
,
1243 XML_VALUE_TYPE_FLOAT
,
1244 XML_VALUE_TYPE_CURRENCY
,
1245 XML_VALUE_TYPE_PERCENTAGE
,
1246 XML_VALUE_TYPE_DATE
,
1247 XML_VALUE_TYPE_TIME
,
1248 XML_VALUE_TYPE_BOOLEAN
1251 static SvXMLEnumMapEntry
const aValueTypeMap
[] =
1253 { XML_FLOAT
, XML_VALUE_TYPE_FLOAT
},
1254 { XML_CURRENCY
, XML_VALUE_TYPE_CURRENCY
},
1255 { XML_PERCENTAGE
, XML_VALUE_TYPE_PERCENTAGE
},
1256 { XML_DATE
, XML_VALUE_TYPE_DATE
},
1257 { XML_TIME
, XML_VALUE_TYPE_TIME
},
1258 { XML_BOOLEAN
, XML_VALUE_TYPE_BOOLEAN
},
1259 { XML_STRING
, XML_VALUE_TYPE_STRING
},
1260 { XML_TOKEN_INVALID
, 0 }
1263 XMLValueImportHelper::XMLValueImportHelper(
1264 SvXMLImport
& rImprt
,
1265 XMLTextImportHelper
& rHlp
,
1266 sal_Bool bType
, sal_Bool bStyle
, sal_Bool bValue
, sal_Bool bFormula
) :
1267 sPropertyContent(sAPI_content
),
1268 sPropertyValue(sAPI_value
),
1269 sPropertyNumberFormat(sAPI_number_format
),
1270 sPropertyIsFixedLanguage(sAPI_is_fixed_language
),
1277 bIsDefaultLanguage(sal_True
),
1279 bStringType(sal_False
),
1280 bFormatOK(sal_False
),
1282 bStringValueOK(sal_False
),
1283 bFloatValueOK(sal_False
),
1284 bFormulaOK(sal_False
),
1289 bSetFormula(bFormula
),
1291 bStringDefault(sal_True
),
1292 bFormulaDefault(sal_True
)
1296 XMLValueImportHelper::~XMLValueImportHelper()
1300 void XMLValueImportHelper::ProcessAttribute(
1301 sal_uInt16 nAttrToken
, const OUString
& sAttrValue
)
1305 case XML_TOK_TEXTFIELD_VALUE_TYPE
:
1308 sal_uInt16 nTmp
= 0;
1309 sal_Bool bRet
= SvXMLUnitConverter::convertEnum(
1310 nTmp
, sAttrValue
, aValueTypeMap
);
1313 ValueType eValueType
= (ValueType
)nTmp
;
1319 case XML_VALUE_TYPE_STRING
:
1320 bStringType
= sal_True
;
1322 case XML_VALUE_TYPE_FLOAT
:
1323 case XML_VALUE_TYPE_CURRENCY
:
1324 case XML_VALUE_TYPE_PERCENTAGE
:
1325 case XML_VALUE_TYPE_DATE
:
1326 case XML_VALUE_TYPE_TIME
:
1327 case XML_VALUE_TYPE_BOOLEAN
:
1328 bStringType
= sal_False
;
1332 OSL_FAIL("unknown value type");
1333 bTypeOK
= sal_False
;
1339 case XML_TOK_TEXTFIELD_VALUE
:
1342 bool const bRet
= ::sax::Converter::convertDouble(fTmp
,sAttrValue
);
1344 bFloatValueOK
= sal_True
;
1350 case XML_TOK_TEXTFIELD_TIME_VALUE
:
1354 ::sax::Converter::convertDuration(fTmp
, sAttrValue
);
1356 bFloatValueOK
= sal_True
;
1362 case XML_TOK_TEXTFIELD_DATE_VALUE
:
1365 sal_Bool bRet
= rImport
.GetMM100UnitConverter().
1366 convertDateTime(fTmp
,sAttrValue
);
1368 bFloatValueOK
= sal_True
;
1374 case XML_TOK_TEXTFIELD_BOOL_VALUE
:
1377 bool bRet
= ::sax::Converter::convertBool(bTmp
, sAttrValue
);
1379 bFloatValueOK
= sal_True
;
1380 fValue
= (bTmp
? 1.0 : 0.0);
1385 bRet
= ::sax::Converter::convertDouble(fTmp
, sAttrValue
);
1387 bFloatValueOK
= sal_True
;
1394 case XML_TOK_TEXTFIELD_STRING_VALUE
:
1395 sValue
= sAttrValue
;
1396 bStringValueOK
= sal_True
;
1399 case XML_TOK_TEXTFIELD_FORMULA
:
1402 sal_uInt16 nPrefix
= rImport
.GetNamespaceMap().
1403 _GetKeyByAttrName( sAttrValue
, &sTmp
, sal_False
);
1404 if( XML_NAMESPACE_OOOW
== nPrefix
)
1407 bFormulaOK
= sal_True
;
1410 sFormula
= sAttrValue
;
1414 case XML_TOK_TEXTFIELD_DATA_STYLE_NAME
:
1416 sal_Int32 nKey
= rHelper
.GetDataStyleKey(
1417 sAttrValue
, &bIsDefaultLanguage
);
1421 bFormatOK
= sal_True
;
1428 void XMLValueImportHelper::PrepareField(
1429 const Reference
<XPropertySet
> & xPropertySet
)
1435 // ??? how to set type?
1440 aAny
<<= (!bFormulaOK
&& bFormulaDefault
) ? sDefault
: sFormula
;
1441 xPropertySet
->setPropertyValue(sPropertyContent
, aAny
);
1445 if (bSetStyle
&& bFormatOK
)
1447 aAny
<<= nFormatKey
;
1448 xPropertySet
->setPropertyValue(sPropertyNumberFormat
, aAny
);
1450 if( xPropertySet
->getPropertySetInfo()->
1451 hasPropertyByName( sPropertyIsFixedLanguage
) )
1453 sal_Bool bIsFixedLanguage
= ! bIsDefaultLanguage
;
1454 aAny
.setValue( &bIsFixedLanguage
, ::getBooleanCppuType() );
1455 xPropertySet
->setPropertyValue( sPropertyIsFixedLanguage
, aAny
);
1459 // value: string or float
1464 aAny
<<= (!bStringValueOK
&& bStringDefault
) ? sDefault
: sValue
;
1465 xPropertySet
->setPropertyValue(sPropertyContent
, aAny
);
1470 xPropertySet
->setPropertyValue(sPropertyValue
, aAny
);
1475 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */