bump product version to 7.6.3.2-android
[LibreOffice.git] / xmloff / source / forms / elementimport.cxx
blobfdb5508a523292aa741f20cd16f73d353e726c8a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include "elementimport.hxx"
21 #include <utility>
22 #include <xmloff/xmlimp.hxx>
23 #include <xmloff/namespacemap.hxx>
24 #include "strings.hxx"
25 #include "callbacks.hxx"
26 #include <xmloff/xmlnamespace.hxx>
27 #include "eventimport.hxx"
28 #include <xmloff/txtstyli.hxx>
29 #include "formenums.hxx"
30 #include <xmloff/xmltoken.hxx>
31 #include "gridcolumnproptranslator.hxx"
32 #include "property_description.hxx"
33 #include "property_meta_data.hxx"
35 #include <com/sun/star/uno/XComponentContext.hpp>
36 #include <com/sun/star/text/XText.hpp>
37 #include <com/sun/star/util/XCloneable.hpp>
38 #include <com/sun/star/util/Duration.hpp>
39 #include <com/sun/star/form/FormComponentType.hpp>
40 #include <com/sun/star/awt/ImagePosition.hpp>
41 #include <com/sun/star/beans/XMultiPropertySet.hpp>
42 #include <com/sun/star/beans/XPropertyContainer.hpp>
43 #include <com/sun/star/beans/PropertyAttribute.hpp>
45 #include <sax/tools/converter.hxx>
46 #include <tools/urlobj.hxx>
47 #include <comphelper/diagnose_ex.hxx>
48 #include <rtl/strbuf.hxx>
49 #include <sal/log.hxx>
50 #include <comphelper/extract.hxx>
51 #include <comphelper/types.hxx>
52 #include <comphelper/sequence.hxx>
53 #include <o3tl/string_view.hxx>
55 #include <algorithm>
57 namespace xmloff
60 using namespace ::xmloff::token;
61 using namespace ::com::sun::star;
62 using namespace ::com::sun::star::uno;
63 using namespace ::com::sun::star::awt;
64 using namespace ::com::sun::star::container;
65 using namespace ::com::sun::star::beans;
66 using namespace ::com::sun::star::script;
67 using namespace ::com::sun::star::lang;
68 using namespace ::com::sun::star::form;
69 using namespace ::com::sun::star::xml;
70 using namespace ::com::sun::star::xml::sax;
71 using namespace ::com::sun::star::util;
72 using namespace ::com::sun::star::text;
73 using namespace ::comphelper;
75 #define PROPID_VALUE 1
76 #define PROPID_CURRENT_VALUE 2
77 #define PROPID_MIN_VALUE 3
78 #define PROPID_MAX_VALUE 4
80 namespace {
82 struct PropertyValueLess
84 bool operator()(const PropertyValue& _rLeft, const PropertyValue& _rRight)
86 return _rLeft.Name < _rRight.Name;
92 //= OElementNameMap
93 std::map<sal_Int32, OControlElement::ElementType> OElementNameMap::s_sElementTranslations2;
95 const OControlElement::ElementType& operator ++(OControlElement::ElementType& _e)
97 OControlElement::ElementType e = _e;
98 sal_Int32 nAsInt = static_cast<sal_Int32>(e);
99 _e = static_cast<OControlElement::ElementType>( ++nAsInt );
100 return _e;
103 OControlElement::ElementType OElementNameMap::getElementType(sal_Int32 nElement)
105 if ( s_sElementTranslations2.empty() )
106 { // initialize
107 for (ElementType eType=ElementType(0); eType<UNKNOWN; ++eType)
108 s_sElementTranslations2[getElementToken(eType)] = eType;
110 auto aPos = s_sElementTranslations2.find(nElement & TOKEN_MASK);
111 if (s_sElementTranslations2.end() != aPos)
112 return aPos->second;
114 return UNKNOWN;
117 //= OElementImport
118 OElementImport::OElementImport(OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager,
119 const Reference< XNameContainer >& _rxParentContainer)
120 :OPropertyImport(_rImport)
121 ,m_rFormImport(_rImport)
122 ,m_rEventManager(_rEventManager)
123 ,m_pStyleElement( nullptr )
124 ,m_xParentContainer(_rxParentContainer)
125 ,m_bImplicitGenericAttributeHandling( true )
127 OSL_ENSURE(m_xParentContainer.is(), "OElementImport::OElementImport: invalid parent container!");
130 OElementImport::~OElementImport()
134 OUString OElementImport::determineDefaultServiceName() const
136 return OUString();
139 void OElementImport::startFastElement(sal_Int32 nElement, const Reference< css::xml::sax::XFastAttributeList >& _rxAttrList)
141 ENTER_LOG_CONTEXT( "xmloff::OElementImport - importing one element" );
143 const OUString sControlImplementation = _rxAttrList->getOptionalValue( XML_ELEMENT(FORM, XML_CONTROL_IMPLEMENTATION) );
145 // retrieve the service name
146 if ( !sControlImplementation.isEmpty() )
148 OUString sOOoImplementationName;
149 const sal_uInt16 nImplPrefix = GetImport().GetNamespaceMap().GetKeyByAttrValueQName( sControlImplementation, &sOOoImplementationName );
150 m_sServiceName = ( nImplPrefix == XML_NAMESPACE_OOO ) ? sOOoImplementationName : sControlImplementation;
153 if ( m_sServiceName.isEmpty() )
154 m_sServiceName = determineDefaultServiceName();
156 // create the object *now*. This allows setting properties in the various handleAttribute methods.
157 // (Though currently not all code is migrated to this pattern, most attributes are still handled
158 // by remembering the value (via implPushBackPropertyValue), and setting the correct property value
159 // later (in OControlImport::StartElement).)
160 m_xElement = createElement();
161 if ( m_xElement.is() )
162 m_xInfo = m_xElement->getPropertySetInfo();
164 // call the base class
165 OPropertyImport::startFastElement( nElement, _rxAttrList );
168 css::uno::Reference< css::xml::sax::XFastContextHandler > OElementImport::createFastChildContext(
169 sal_Int32 nElement,
170 const css::uno::Reference< css::xml::sax::XFastAttributeList >& _rxAttrList )
172 if( nElement == XML_ELEMENT(OFFICE, XML_EVENT_LISTENERS) )
173 return new OFormEventsImportContext(m_rFormImport.getGlobalContext(), *this);
175 return OPropertyImport::createFastChildContext(nElement, _rxAttrList);
178 void OElementImport::endFastElement(sal_Int32 )
180 OSL_ENSURE(m_xElement.is(), "OElementImport::EndElement: invalid element created!");
181 if (!m_xElement.is())
182 return;
184 // apply the non-generic properties
185 implApplySpecificProperties();
187 // set the generic properties
188 implApplyGenericProperties();
190 // set the style properties
191 if ( m_pStyleElement && m_xElement.is() )
193 Reference< XPropertySet > xPropTranslation =
194 new OGridColumnPropertyTranslator( Reference< XMultiPropertySet >( m_xElement, UNO_QUERY ) );
195 const_cast< XMLTextStyleContext* >( m_pStyleElement )->FillPropertySet( xPropTranslation );
197 const OUString sNumberStyleName = m_pStyleElement->GetDataStyleName( );
198 if ( !sNumberStyleName.isEmpty() )
199 // the style also has a number (sub) style
200 m_rContext.applyControlNumberStyle( m_xElement, sNumberStyleName );
203 // insert the element into the parent container
204 if (m_sName.isEmpty())
206 OSL_FAIL("OElementImport::EndElement: did not find a name attribute!");
207 m_sName = implGetDefaultName();
210 if (m_xParentContainer.is())
211 m_xParentContainer->insertByName(m_sName, Any(m_xElement));
213 LEAVE_LOG_CONTEXT( );
216 void OElementImport::implApplySpecificProperties()
218 if ( m_aValues.empty() )
219 return;
221 // set all the properties we collected
222 #if OSL_DEBUG_LEVEL > 0
223 // check if the object has all the properties
224 // (We do this in the non-pro version only. Doing it all the time would be too much expensive)
225 if ( m_xInfo.is() )
227 for ( const auto& rCheck : m_aValues )
229 OSL_ENSURE(m_xInfo->hasPropertyByName(rCheck.Name),
230 OStringBuffer("OElementImport::implApplySpecificProperties: read a property (" +
231 OUStringToOString(rCheck.Name, RTL_TEXTENCODING_ASCII_US) +
232 ") which does not exist on the element!").getStr());
235 #endif
237 // set the properties
238 const Reference< XMultiPropertySet > xMultiProps(m_xElement, UNO_QUERY);
239 bool bSuccess = false;
240 if (xMultiProps.is())
242 // translate our properties so that the XMultiPropertySet can handle them
244 // sort our property value array so that we can use it in a setPropertyValues
245 ::std::sort( m_aValues.begin(), m_aValues.end(), PropertyValueLess());
247 // the names
248 Sequence< OUString > aNames(m_aValues.size());
249 OUString* pNames = aNames.getArray();
250 // the values
251 Sequence< Any > aValues(m_aValues.size());
252 Any* pValues = aValues.getArray();
253 // copy
255 for ( const auto& rPropValues : m_aValues )
257 *pNames = rPropValues.Name;
258 *pValues = rPropValues.Value;
259 ++pNames;
260 ++pValues;
265 xMultiProps->setPropertyValues(aNames, aValues);
266 bSuccess = true;
268 catch(const Exception&)
270 DBG_UNHANDLED_EXCEPTION("xmloff.forms");
271 OSL_FAIL("OElementImport::implApplySpecificProperties: could not set the properties (using the XMultiPropertySet)!");
275 if (bSuccess)
276 return;
278 // no XMultiPropertySet or setting all properties at once failed
279 for ( const auto& rPropValues : m_aValues )
281 // this try/catch here is expensive, but because this is just a fallback which should normally not be
282 // used it's acceptable this way ...
285 m_xElement->setPropertyValue(rPropValues.Name, rPropValues.Value);
287 catch(const Exception&)
289 DBG_UNHANDLED_EXCEPTION("xmloff.forms");
290 OSL_FAIL(OStringBuffer("OElementImport::implApplySpecificProperties: could not set the property \"" +
291 OUStringToOString(rPropValues.Name, RTL_TEXTENCODING_ASCII_US) +
292 "\"!").getStr());
297 void OElementImport::implApplyGenericProperties()
299 if ( m_aGenericValues.empty() )
300 return;
302 Reference< XPropertyContainer > xDynamicProperties( m_xElement, UNO_QUERY );
304 // PropertyValueArray::iterator aEnd = m_aGenericValues.end();
305 for ( auto& rPropValues : m_aGenericValues )
307 // check property type for numeric types before setting
308 // the property
311 // if such a property does not yet exist at the element, create it if necessary
312 const bool bExistentProperty = m_xInfo->hasPropertyByName( rPropValues.Name );
313 if ( !bExistentProperty )
315 if ( !xDynamicProperties.is() )
317 SAL_WARN( "xmloff", "OElementImport::implApplyGenericProperties: encountered an unknown property ("
318 << rPropValues.Name << "), but component is no PropertyBag!");
319 continue;
322 xDynamicProperties->addProperty(
323 rPropValues.Name,
324 PropertyAttribute::BOUND | PropertyAttribute::REMOVABLE,
325 rPropValues.Value
328 // re-fetch the PropertySetInfo
329 m_xInfo = m_xElement->getPropertySetInfo();
332 // determine the type of the value (source for the following conversion)
333 TypeClass eValueTypeClass = rPropValues.Value.getValueTypeClass();
334 const bool bValueIsSequence = TypeClass_SEQUENCE == eValueTypeClass;
335 if ( bValueIsSequence )
337 uno::Type aSimpleType( getSequenceElementType( rPropValues.Value.getValueType() ) );
338 eValueTypeClass = aSimpleType.getTypeClass();
341 // determine the type of the property (target for the following conversion)
342 const Property aProperty( m_xInfo->getPropertyByName( rPropValues.Name ) );
343 TypeClass ePropTypeClass = aProperty.Type.getTypeClass();
344 const bool bPropIsSequence = TypeClass_SEQUENCE == ePropTypeClass;
345 if( bPropIsSequence )
347 uno::Type aSimpleType( ::comphelper::getSequenceElementType( aProperty.Type ) );
348 ePropTypeClass = aSimpleType.getTypeClass();
351 if ( bPropIsSequence != bValueIsSequence )
353 OSL_FAIL( "OElementImport::implImportGenericProperties: either both value and property should be a sequence, or none of them!" );
354 continue;
357 if ( bValueIsSequence )
359 Sequence< Any > aXMLValueList;
360 rPropValues.Value >>= aXMLValueList;
361 // just skip this part if empty sequence
362 if (!aXMLValueList.getLength())
363 continue;
365 Sequence< sal_Int16 > aPropertyValueList( aXMLValueList.getLength() );
367 SAL_WARN_IF( eValueTypeClass != TypeClass_ANY, "xmloff",
368 "OElementImport::implApplyGenericProperties: only ANYs should have been imported as generic list property!" );
369 // (OPropertyImport should produce only Sequencer< Any >, since it cannot know the real type
371 SAL_WARN_IF( ePropTypeClass != TypeClass_SHORT, "xmloff",
372 "OElementImport::implApplyGenericProperties: conversion to sequences other than 'sequence< short >' not implemented, yet!" );
375 std::transform(std::cbegin(aXMLValueList), std::cend(aXMLValueList), aPropertyValueList.getArray(),
376 [](const Any& rXMLValue) -> sal_Int16 {
377 // only value sequences of numeric types implemented so far.
378 double nVal( 0 );
379 OSL_VERIFY( rXMLValue >>= nVal );
380 return static_cast< sal_Int16 >( nVal );
383 rPropValues.Value <<= aPropertyValueList;
385 else if ( ePropTypeClass != eValueTypeClass )
387 switch ( eValueTypeClass )
389 case TypeClass_DOUBLE:
391 double nVal = 0;
392 rPropValues.Value >>= nVal;
393 switch( ePropTypeClass )
395 case TypeClass_BYTE:
396 rPropValues.Value <<= static_cast< sal_Int8 >( nVal );
397 break;
398 case TypeClass_SHORT:
399 rPropValues.Value <<= static_cast< sal_Int16 >( nVal );
400 break;
401 case TypeClass_UNSIGNED_SHORT:
402 rPropValues.Value <<= static_cast< sal_uInt16 >( nVal );
403 break;
404 case TypeClass_LONG:
405 case TypeClass_ENUM:
406 rPropValues.Value <<= static_cast< sal_Int32 >( nVal );
407 break;
408 case TypeClass_UNSIGNED_LONG:
409 rPropValues.Value <<= static_cast< sal_uInt32 >( nVal );
410 break;
411 case TypeClass_UNSIGNED_HYPER:
412 rPropValues.Value <<= static_cast< sal_uInt64 >( nVal );
413 break;
414 case TypeClass_HYPER:
415 rPropValues.Value <<= static_cast< sal_Int64 >( nVal );
416 break;
417 default:
418 OSL_FAIL( "OElementImport::implImportGenericProperties: unsupported value type!" );
419 break;
422 break;
423 default:
424 OSL_FAIL( "OElementImport::implImportGenericProperties: non-double values not supported!" );
425 break;
429 m_xElement->setPropertyValue( rPropValues.Name, rPropValues.Value );
431 catch(const Exception&)
433 DBG_UNHANDLED_EXCEPTION("xmloff.forms");
434 OSL_FAIL(OStringBuffer("OElementImport::EndElement: could not set the property \"" +
435 OUStringToOString(rPropValues.Name, RTL_TEXTENCODING_ASCII_US) +
436 "\"!").getStr());
441 OUString OElementImport::implGetDefaultName() const
443 // no optimization here. If this method gets called, the XML stream did not contain a name for the
444 // element, which is a heavy error. So in this case we don't care for performance
445 static constexpr OUStringLiteral sUnnamedName = u"unnamed";
446 OSL_ENSURE(m_xParentContainer.is(), "OElementImport::implGetDefaultName: no parent container!");
447 if (!m_xParentContainer.is())
448 return sUnnamedName;
449 Sequence< OUString > aNames = m_xParentContainer->getElementNames();
451 for (sal_Int32 i=0; i<32768; ++i) // the limit is nearly arbitrary...
453 // assemble the new name (suggestion)
454 OUString sReturn = sUnnamedName + OUString::number(i);
455 // check the existence (this is the bad performance part...)
456 if (comphelper::findValue(aNames, sReturn) == -1)
457 // not found the name
458 return sReturn;
460 OSL_FAIL("OElementImport::implGetDefaultName: did not find a free name!");
461 return sUnnamedName;
464 PropertyGroups::const_iterator OElementImport::impl_matchPropertyGroup( const PropertyGroups& i_propertyGroups ) const
466 ENSURE_OR_RETURN( m_xInfo.is(), "OElementImport::impl_matchPropertyGroup: no property set info!", i_propertyGroups.end() );
468 return std::find_if(i_propertyGroups.cbegin(), i_propertyGroups.cend(), [&](const PropertyDescriptionList& rGroup) {
469 return std::all_of(rGroup.cbegin(), rGroup.cend(), [&](const PropertyDescription* prop) {
470 return m_xInfo->hasPropertyByName( prop->propertyName );
475 bool OElementImport::tryGenericAttribute( sal_Int32 nElement, const OUString& _rValue )
477 // the generic approach (which I hope all props will be migrated to, on the medium term): property handlers
478 const AttributeDescription attribute( metadata::getAttributeDescription( nElement ) );
479 if ( attribute.attributeToken != XML_TOKEN_INVALID )
481 PropertyGroups propertyGroups;
482 metadata::getPropertyGroupList( attribute, propertyGroups );
483 const PropertyGroups::const_iterator pos = impl_matchPropertyGroup( propertyGroups );
484 if ( pos == propertyGroups.end() )
485 return false;
489 const PropertyDescriptionList& rProperties( *pos );
490 const PropertyDescription* first = *rProperties.begin();
491 if ( !first )
493 SAL_WARN( "xmloff.forms", "OElementImport::handleAttribute: invalid property description!" );
494 break;
497 const PPropertyHandler handler = (*first->factory)( first->propertyId );
498 if ( !handler )
500 SAL_WARN( "xmloff.forms", "OElementImport::handleAttribute: invalid property handler!" );
501 break;
504 PropertyValues aValues;
505 for ( const auto& propDesc : rProperties )
507 aValues[ propDesc->propertyId ] = Any();
509 if ( handler->getPropertyValues( _rValue, aValues ) )
511 for ( const auto& propDesc : rProperties )
513 implPushBackPropertyValue( propDesc->propertyName, aValues[ propDesc->propertyId ] );
517 while ( false );
519 // handled
520 return true;
522 return false;
525 bool OElementImport::handleAttribute(sal_Int32 nElement, const OUString& _rValue)
527 auto nLocal = nElement & TOKEN_MASK;
528 if ( nLocal == XML_CONTROL_IMPLEMENTATION )
529 // ignore this, it has already been handled in OElementImport::StartElement
530 return true;
532 if ( nLocal == XML_NAME )
534 if ( m_sName.isEmpty() )
535 // remember the name for later use in EndElement
536 m_sName = _rValue;
537 return true;
540 // maybe it's the style attribute?
541 if ( nLocal == XML_TEXT_STYLE_NAME )
543 const SvXMLStyleContext* pStyleContext = m_rContext.getStyleElement( _rValue );
544 OSL_ENSURE( pStyleContext, "OElementImport::handleAttribute: do not know the style!" );
545 // remember the element for later usage.
546 m_pStyleElement = dynamic_cast<const XMLTextStyleContext*>( pStyleContext );
547 return true;
550 if ( m_bImplicitGenericAttributeHandling )
551 if ( tryGenericAttribute( nElement, _rValue ) )
552 return true;
554 // let the base class handle it
555 return OPropertyImport::handleAttribute( nElement, _rValue);
558 Reference< XPropertySet > OElementImport::createElement()
560 Reference< XPropertySet > xReturn;
561 if (!m_sServiceName.isEmpty())
563 Reference< XComponentContext > xContext = m_rFormImport.getGlobalContext().GetComponentContext();
564 Reference< XInterface > xPure = xContext->getServiceManager()->createInstanceWithContext(m_sServiceName, xContext);
565 OSL_ENSURE(xPure.is(),
566 OStringBuffer("OElementImport::createElement: service factory gave me no object (service name: " +
567 OUStringToOString(m_sServiceName, RTL_TEXTENCODING_ASCII_US) +
568 ")!").getStr());
569 xReturn.set(xPure, UNO_QUERY);
571 else
572 OSL_FAIL("OElementImport::createElement: no service name to create an element!");
574 return xReturn;
577 void OElementImport::registerEvents(const Sequence< ScriptEventDescriptor >& _rEvents)
579 OSL_ENSURE(m_xElement.is(), "OElementImport::registerEvents: no element to register events for!");
580 m_rEventManager.registerEvents(m_xElement, _rEvents);
583 void OElementImport::simulateDefaultedAttribute(sal_Int32 nElement, const OUString& _rPropertyName, const char* _pAttributeDefault)
585 OSL_ENSURE( m_xInfo.is(), "OPropertyImport::simulateDefaultedAttribute: the component should be more gossipy about it's properties!" );
587 if ( !m_xInfo.is() || m_xInfo->hasPropertyByName( _rPropertyName ) )
589 if ( !encounteredAttribute( nElement ) )
590 OSL_VERIFY( handleAttribute( XML_ELEMENT(FORM, (nElement & TOKEN_MASK)), OUString::createFromAscii( _pAttributeDefault ) ) );
594 //= OControlImport
595 OControlImport::OControlImport(OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager,
596 const Reference< XNameContainer >& _rxParentContainer)
597 :OElementImport(_rImport, _rEventManager, _rxParentContainer)
598 ,m_eElementType(OControlElement::UNKNOWN)
600 disableImplicitGenericAttributeHandling();
603 OControlImport::OControlImport(OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager,
604 const Reference< XNameContainer >& _rxParentContainer, OControlElement::ElementType _eType)
605 :OElementImport(_rImport, _rEventManager, _rxParentContainer)
606 ,m_eElementType(_eType)
608 disableImplicitGenericAttributeHandling();
611 OUString OControlImport::determineDefaultServiceName() const
613 const char* pServiceName = nullptr;
614 switch ( m_eElementType )
616 case OControlElement::TEXT:
617 case OControlElement::TEXT_AREA:
618 case OControlElement::PASSWORD: pServiceName = "com.sun.star.form.component.TextField"; break;
619 case OControlElement::FILE: pServiceName = "com.sun.star.form.component.FileControl"; break;
620 case OControlElement::FORMATTED_TEXT: pServiceName = "com.sun.star.form.component.FormattedField"; break;
621 case OControlElement::FIXED_TEXT: pServiceName = "com.sun.star.form.component.FixedText"; break;
622 case OControlElement::COMBOBOX: pServiceName = "com.sun.star.form.component.ComboBox"; break;
623 case OControlElement::LISTBOX: pServiceName = "com.sun.star.form.component.ListBox"; break;
624 case OControlElement::BUTTON: pServiceName = "com.sun.star.form.component.CommandButton"; break;
625 case OControlElement::IMAGE: pServiceName = "com.sun.star.form.component.ImageButton"; break;
626 case OControlElement::CHECKBOX: pServiceName = "com.sun.star.form.component.CheckBox"; break;
627 case OControlElement::RADIO: pServiceName = "com.sun.star.form.component.RadioButton"; break;
628 case OControlElement::FRAME: pServiceName = "com.sun.star.form.component.GroupBox"; break;
629 case OControlElement::IMAGE_FRAME: pServiceName = "com.sun.star.form.component.DatabaseImageControl"; break;
630 case OControlElement::HIDDEN: pServiceName = "com.sun.star.form.component.HiddenControl"; break;
631 case OControlElement::GRID: pServiceName = "com.sun.star.form.component.GridControl"; break;
632 case OControlElement::VALUERANGE: pServiceName = "com.sun.star.form.component.ScrollBar"; break;
633 case OControlElement::TIME: pServiceName = "com.sun.star.form.component.TimeField"; break;
634 case OControlElement::DATE: pServiceName = "com.sun.star.form.component.DateField"; break;
635 default: break;
637 if ( pServiceName != nullptr )
638 return OUString::createFromAscii( pServiceName );
639 return OUString();
642 void OControlImport::addOuterAttributes(const Reference< XFastAttributeList >& _rxOuterAttribs)
644 OSL_ENSURE(!m_xOuterAttributes.is(), "OControlImport::addOuterAttributes: already have these attributes!");
645 m_xOuterAttributes = _rxOuterAttribs;
648 bool OControlImport::handleAttribute(sal_Int32 nElement, const OUString& _rValue)
650 static sal_Int32 nLinkedCellAttributeName = OAttributeMetaData::getBindingAttributeToken(BAFlags::LinkedCell);
652 if ((nElement & TOKEN_MASK) == XML_ID)
653 { // it's the control id
654 if (IsTokenInNamespace(nElement, XML_NAMESPACE_XML))
656 m_sControlId = _rValue;
658 else if (IsTokenInNamespace(nElement, XML_NAMESPACE_FORM))
660 if (m_sControlId.isEmpty())
662 m_sControlId = _rValue;
665 return true;
668 if ( (nElement & TOKEN_MASK) == nLinkedCellAttributeName )
669 { // it's the address of a spreadsheet cell
670 m_sBoundCellAddress = _rValue;
671 return true;
674 if ( nElement == XML_ELEMENT(XFORMS, XML_BIND ) )
676 m_sBindingID = _rValue;
677 return true;
680 if ( nElement == XML_ELEMENT(FORM, XML_XFORMS_LIST_SOURCE) )
682 m_sListBindingID = _rValue;
683 return true;
686 if ( nElement == XML_ELEMENT(FORM, XML_XFORMS_SUBMISSION)
687 || nElement == XML_ELEMENT(XFORMS, XML_SUBMISSION) )
689 m_sSubmissionID = _rValue;
690 return true;
693 if ( OElementImport::tryGenericAttribute( nElement, _rValue ) )
694 return true;
696 static const sal_Int32 nValueAttributeName = OAttributeMetaData::getCommonControlAttributeToken(CCAFlags::Value);
697 static const sal_Int32 nCurrentValueAttributeName = OAttributeMetaData::getCommonControlAttributeToken(CCAFlags::CurrentValue);
698 static const sal_Int32 nMinValueAttributeName = OAttributeMetaData::getSpecialAttributeToken(SCAFlags::MinValue);
699 static const sal_Int32 nMaxValueAttributeName = OAttributeMetaData::getSpecialAttributeToken(SCAFlags::MaxValue);
700 static const sal_Int32 nRepeatDelayAttributeName = OAttributeMetaData::getSpecialAttributeToken( SCAFlags::RepeatDelay );
702 sal_Int32 nHandle = -1;
703 if ( (nElement & TOKEN_MASK) == nValueAttributeName )
704 nHandle = PROPID_VALUE;
705 else if ( (nElement & TOKEN_MASK) == nCurrentValueAttributeName )
706 nHandle = PROPID_CURRENT_VALUE;
707 else if ( (nElement & TOKEN_MASK) == nMinValueAttributeName )
708 nHandle = PROPID_MIN_VALUE;
709 else if ( (nElement & TOKEN_MASK) == nMaxValueAttributeName )
710 nHandle = PROPID_MAX_VALUE;
711 if ( nHandle != -1 )
713 // for the moment, simply remember the name and the value
714 PropertyValue aProp;
715 aProp.Name = SvXMLImport::getNameFromToken(nElement);
716 aProp.Handle = nHandle;
717 aProp.Value <<= _rValue;
718 m_aValueProperties.push_back(aProp);
719 return true;
722 if ( (nElement & TOKEN_MASK) == nRepeatDelayAttributeName )
724 util::Duration aDuration;
725 if (::sax::Converter::convertDuration(aDuration, _rValue))
727 PropertyValue aProp;
728 aProp.Name = PROPERTY_REPEAT_DELAY;
729 sal_Int32 const nMS =
730 ((aDuration.Hours * 60 + aDuration.Minutes) * 60
731 + aDuration.Seconds) * 1000 + aDuration.NanoSeconds/1000000;
732 aProp.Value <<= nMS;
734 implPushBackPropertyValue(aProp);
736 return true;
739 return OElementImport::handleAttribute( nElement, _rValue );
742 void OControlImport::startFastElement(sal_Int32 nElement, const Reference< css::xml::sax::XFastAttributeList >& _rxAttrList)
744 css::uno::Reference< css::xml::sax::XFastAttributeList > xMergedAttributes;
745 if( m_xOuterAttributes.is() )
747 // merge the attribute lists, our own one
748 rtl::Reference<sax_fastparser::FastAttributeList> xMerger(new sax_fastparser::FastAttributeList(_rxAttrList));
749 // and the ones of our enclosing element
750 xMerger->add(m_xOuterAttributes);
751 xMergedAttributes = xMerger.get();
753 else
755 xMergedAttributes = _rxAttrList;
758 // let the base class handle all the attributes
759 OElementImport::startFastElement(nElement, xMergedAttributes);
761 if ( m_aValueProperties.empty() || !m_xElement.is())
762 return;
764 // get the property set info
765 if (!m_xInfo.is())
767 OSL_FAIL("OControlImport::StartElement: no PropertySetInfo!");
768 return;
771 OUString pValueProperty;
772 OUString pCurrentValueProperty;
773 OUString pMinValueProperty;
774 OUString pMaxValueProperty;
776 bool bRetrievedValues = false;
777 bool bRetrievedValueLimits = false;
779 // get the class id of our element
780 sal_Int16 nClassId = FormComponentType::CONTROL;
781 m_xElement->getPropertyValue(PROPERTY_CLASSID) >>= nClassId;
783 // translate the value properties we collected in handleAttributes
784 for ( auto& rValueProps : m_aValueProperties )
786 bool bSuccess = false;
787 switch (rValueProps.Handle)
789 case PROPID_VALUE:
790 case PROPID_CURRENT_VALUE:
792 // get the property names
793 if (!bRetrievedValues)
795 getValuePropertyNames(m_eElementType, nClassId, pCurrentValueProperty, pValueProperty);
796 if ( pCurrentValueProperty.isEmpty() && pValueProperty.isEmpty() )
798 SAL_WARN( "xmloff.forms", "OControlImport::StartElement: illegal value property names!" );
799 break;
802 bRetrievedValues = true;
804 if ( PROPID_VALUE == rValueProps.Handle && pValueProperty.isEmpty() )
806 SAL_WARN( "xmloff.forms", "OControlImport::StartElement: the control does not have a value property!");
807 break;
810 if ( PROPID_CURRENT_VALUE == rValueProps.Handle && pCurrentValueProperty.isEmpty() )
812 SAL_WARN( "xmloff.forms", "OControlImport::StartElement: the control does not have a current-value property!");
813 break;
816 // transfer the name
817 if (PROPID_VALUE == rValueProps.Handle)
818 rValueProps.Name = pValueProperty;
819 else
820 rValueProps.Name = pCurrentValueProperty;
821 bSuccess = true;
823 break;
824 case PROPID_MIN_VALUE:
825 case PROPID_MAX_VALUE:
827 // get the property names
828 if (!bRetrievedValueLimits)
830 getValueLimitPropertyNames(nClassId, pMinValueProperty, pMaxValueProperty);
831 if ( pMinValueProperty.isEmpty() || pMaxValueProperty.isEmpty() )
833 SAL_WARN( "xmloff.forms", "OControlImport::StartElement: illegal value limit property names!" );
834 break;
837 bRetrievedValueLimits = true;
839 OSL_ENSURE((PROPID_MIN_VALUE != rValueProps.Handle) || !pMinValueProperty.isEmpty(),
840 "OControlImport::StartElement: the control does not have a value property!");
841 OSL_ENSURE((PROPID_MAX_VALUE != rValueProps.Handle) || !pMaxValueProperty.isEmpty(),
842 "OControlImport::StartElement: the control does not have a current-value property!");
844 // transfer the name
845 if (PROPID_MIN_VALUE == rValueProps.Handle)
846 rValueProps.Name = pMinValueProperty;
847 else
848 rValueProps.Name = pMaxValueProperty;
849 bSuccess = true;
851 break;
854 if ( !bSuccess )
855 continue;
857 // translate the value
858 implTranslateValueProperty(m_xInfo, rValueProps);
859 // add the property to the base class' array
860 implPushBackPropertyValue(rValueProps);
865 void OControlImport::implTranslateValueProperty(const Reference< XPropertySetInfo >& _rxPropInfo,
866 PropertyValue& _rPropValue)
868 OSL_ENSURE(_rxPropInfo->hasPropertyByName(_rPropValue.Name),
869 "OControlImport::implTranslateValueProperty: invalid property name!");
871 // retrieve the type of the property
872 Property aProp = _rxPropInfo->getPropertyByName(_rPropValue.Name);
873 // the untranslated string value as read in handleAttribute
874 OUString sValue;
875 bool bSuccess = _rPropValue.Value >>= sValue;
876 OSL_ENSURE(bSuccess, "OControlImport::implTranslateValueProperty: supposed to be called with non-translated string values!");
878 if (TypeClass_ANY == aProp.Type.getTypeClass())
880 // we have exactly 2 properties where this type class is allowed:
881 SAL_WARN_IF(
882 _rPropValue.Name != PROPERTY_EFFECTIVE_VALUE
883 && _rPropValue.Name != PROPERTY_EFFECTIVE_DEFAULT, "xmloff",
884 "OControlImport::implTranslateValueProperty: invalid property type/name combination, Any and " << _rPropValue.Name);
886 // Both properties are allowed to have a double or a string value,
887 // so first try to convert the string into a number
888 double nValue;
889 if (::sax::Converter::convertDouble(nValue, sValue))
890 _rPropValue.Value <<= nValue;
891 else
892 _rPropValue.Value <<= sValue;
894 else
895 _rPropValue.Value = PropertyConversion::convertString(aProp.Type, sValue);
898 void OControlImport::endFastElement(sal_Int32 nElement)
900 OSL_ENSURE(m_xElement.is(), "OControlImport::EndElement: invalid control!");
901 if ( !m_xElement.is() )
902 return;
904 // register our control with its id
905 if (!m_sControlId.isEmpty())
906 m_rFormImport.registerControlId(m_xElement, m_sControlId);
907 // it's allowed to have no control id. In this case we're importing a column
909 // one more pre-work to do:
910 // when we set default values, then by definition the respective value is set
911 // to this default value, too. This means if the sequence contains for example
912 // a DefaultText value, then the Text will be affected by this, too.
913 // In case the Text is not part of the property sequence (or occurs _before_
914 // the DefaultText, which can happen for other value/default-value property names),
915 // this means that the Text (the value property) is incorrectly imported.
917 bool bRestoreValuePropertyValue = false;
918 Any aValuePropertyValue;
920 sal_Int16 nClassId = FormComponentType::CONTROL;
923 // get the class id of our element
924 m_xElement->getPropertyValue(PROPERTY_CLASSID) >>= nClassId;
926 catch( const Exception& )
928 TOOLS_WARN_EXCEPTION("xmloff.forms",
929 "caught an exception while retrieving the class id!");
932 OUString pValueProperty;
933 OUString pDefaultValueProperty;
934 getRuntimeValuePropertyNames(m_eElementType, nClassId, pValueProperty, pDefaultValueProperty);
935 if ( !pDefaultValueProperty.isEmpty() && !pValueProperty.isEmpty() )
937 bool bNonDefaultValuePropertyValue = false;
938 // is the "value property" part of the sequence?
940 // look up this property in our sequence
941 for ( const auto& rCheck : m_aValues )
943 if ( rCheck.Name == pDefaultValueProperty )
944 bRestoreValuePropertyValue = true;
945 else if ( rCheck.Name == pValueProperty )
947 bNonDefaultValuePropertyValue = true;
948 // we need to restore the value property we found here, nothing else
949 aValuePropertyValue = rCheck.Value;
953 if ( bRestoreValuePropertyValue && !bNonDefaultValuePropertyValue )
955 // found it -> need to remember (and restore) the "value property value", which is not set explicitly
958 aValuePropertyValue = m_xElement->getPropertyValue( pValueProperty );
960 catch( const Exception& )
962 TOOLS_WARN_EXCEPTION(
963 "xmloff.forms",
964 "caught an exception while retrieving the current value property!");
969 // let the base class set all the values
970 OElementImport::endFastElement(nElement);
972 // restore the "value property value", if necessary
973 if ( bRestoreValuePropertyValue && !pValueProperty.isEmpty() )
977 m_xElement->setPropertyValue( pValueProperty, aValuePropertyValue );
979 catch( const Exception& )
981 TOOLS_WARN_EXCEPTION("xmloff.forms",
982 "caught an exception while restoring the value property!");
986 // the external cell binding, if applicable
987 if ( m_xElement.is() && !m_sBoundCellAddress.isEmpty() )
988 doRegisterCellValueBinding( m_sBoundCellAddress );
990 // XForms binding, if applicable
991 if ( m_xElement.is() && !m_sBindingID.isEmpty() )
992 doRegisterXFormsValueBinding( m_sBindingID );
994 // XForms list binding, if applicable
995 if ( m_xElement.is() && !m_sListBindingID.isEmpty() )
996 doRegisterXFormsListBinding( m_sListBindingID );
998 // XForms submission, if applicable
999 if ( m_xElement.is() && !m_sSubmissionID.isEmpty() )
1000 doRegisterXFormsSubmission( m_sSubmissionID );
1003 void OControlImport::doRegisterCellValueBinding( const OUString& _rBoundCellAddress )
1005 OSL_PRECOND( m_xElement.is(), "OControlImport::doRegisterCellValueBinding: invalid element!" );
1006 OSL_PRECOND( !_rBoundCellAddress.isEmpty(),
1007 "OControlImport::doRegisterCellValueBinding: invalid address!" );
1009 m_rContext.registerCellValueBinding( m_xElement, _rBoundCellAddress );
1012 void OControlImport::doRegisterXFormsValueBinding( const OUString& _rBindingID )
1014 OSL_PRECOND( m_xElement.is(), "need element" );
1015 OSL_PRECOND( !_rBindingID.isEmpty(), "binding ID is not valid" );
1017 m_rContext.registerXFormsValueBinding( m_xElement, _rBindingID );
1020 void OControlImport::doRegisterXFormsListBinding( const OUString& _rBindingID )
1022 OSL_PRECOND( m_xElement.is(), "need element" );
1023 OSL_PRECOND( !_rBindingID.isEmpty(), "binding ID is not valid" );
1025 m_rContext.registerXFormsListBinding( m_xElement, _rBindingID );
1028 void OControlImport::doRegisterXFormsSubmission( const OUString& _rSubmissionID )
1030 OSL_PRECOND( m_xElement.is(), "need element" );
1031 OSL_PRECOND( !_rSubmissionID.isEmpty(), "binding ID is not valid" );
1033 m_rContext.registerXFormsSubmission( m_xElement, _rSubmissionID );
1036 Reference< XPropertySet > OControlImport::createElement()
1038 const Reference<XPropertySet> xPropSet = OElementImport::createElement();
1039 if ( xPropSet.is() )
1041 m_xInfo = xPropSet->getPropertySetInfo();
1042 if ( m_xInfo.is() && m_xInfo->hasPropertyByName(PROPERTY_ALIGN) )
1044 Any aValue;
1045 xPropSet->setPropertyValue(PROPERTY_ALIGN,aValue);
1048 return xPropSet;
1051 //= OImagePositionImport
1052 OImagePositionImport::OImagePositionImport( OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager,
1053 const Reference< XNameContainer >& _rxParentContainer,
1054 OControlElement::ElementType _eType )
1055 :OControlImport( _rImport, _rEventManager, _rxParentContainer, _eType )
1056 ,m_nImagePosition( -1 )
1057 ,m_nImageAlign( 0 )
1058 ,m_bHaveImagePosition( false )
1062 bool OImagePositionImport::handleAttribute( sal_Int32 nElement,
1063 const OUString& _rValue )
1065 static const sal_Int32 s_nImageDataAttributeName = OAttributeMetaData::getCommonControlAttributeToken(CCAFlags::ImageData);
1067 if ( (nElement & TOKEN_MASK) == s_nImageDataAttributeName)
1069 m_xGraphic = m_rContext.getGlobalContext().loadGraphicByURL(_rValue);
1070 return true;
1072 else if ( (nElement & TOKEN_MASK) == XML_IMAGE_POSITION )
1074 OSL_VERIFY( PropertyConversion::convertString(
1075 cppu::UnoType<decltype(m_nImagePosition)>::get(),
1076 _rValue, aImagePositionMap
1077 ) >>= m_nImagePosition );
1078 m_bHaveImagePosition = true;
1079 return true;
1081 else if ( (nElement & TOKEN_MASK) == XML_IMAGE_ALIGN )
1083 OSL_VERIFY( PropertyConversion::convertString(
1084 cppu::UnoType<decltype(m_nImageAlign)>::get(),
1085 _rValue, aImageAlignMap
1086 ) >>= m_nImageAlign );
1087 return true;
1090 return OControlImport::handleAttribute( nElement, _rValue );
1093 void OImagePositionImport::startFastElement(sal_Int32 nElement, const Reference< XFastAttributeList >& _rxAttrList)
1095 OControlImport::startFastElement( nElement, _rxAttrList );
1097 if (m_xGraphic.is())
1099 PropertyValue aGraphicProperty;
1100 aGraphicProperty.Name = PROPERTY_GRAPHIC;
1101 aGraphicProperty.Value <<= m_xGraphic;
1102 implPushBackPropertyValue(aGraphicProperty);
1104 if ( !m_bHaveImagePosition )
1105 return;
1107 sal_Int16 nUnoImagePosition = ImagePosition::Centered;
1108 if ( m_nImagePosition >= 0 )
1110 OSL_ENSURE( ( m_nImagePosition <= 3 ) && ( m_nImageAlign >= 0 ) && ( m_nImageAlign < 3 ),
1111 "OImagePositionImport::StartElement: unknown image align and/or position!" );
1112 nUnoImagePosition = m_nImagePosition * 3 + m_nImageAlign;
1115 PropertyValue aImagePosition;
1116 aImagePosition.Name = PROPERTY_IMAGE_POSITION;
1117 aImagePosition.Value <<= nUnoImagePosition;
1118 implPushBackPropertyValue( aImagePosition );
1121 //= OReferredControlImport
1122 OReferredControlImport::OReferredControlImport(
1123 OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager,
1124 const Reference< XNameContainer >& _rxParentContainer )
1125 :OControlImport(_rImport, _rEventManager, _rxParentContainer)
1129 void OReferredControlImport::startFastElement(sal_Int32 nElement, const Reference< XFastAttributeList >& _rxAttrList)
1131 OControlImport::startFastElement(nElement, _rxAttrList);
1133 // the base class should have created the control, so we can register it
1134 if ( !m_sReferringControls.isEmpty() )
1135 m_rFormImport.registerControlReferences(m_xElement, m_sReferringControls);
1138 bool OReferredControlImport::handleAttribute(sal_Int32 nElement,
1139 const OUString& _rValue)
1141 static const sal_Int32 s_nReferenceAttributeName = OAttributeMetaData::getCommonControlAttributeToken(CCAFlags::For);
1142 if ((nElement & TOKEN_MASK) == s_nReferenceAttributeName)
1144 m_sReferringControls = _rValue;
1145 return true;
1147 return OControlImport::handleAttribute(nElement, _rValue);
1150 //= OPasswordImport
1151 OPasswordImport::OPasswordImport(OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager,
1152 const Reference< XNameContainer >& _rxParentContainer, OControlElement::ElementType _eType)
1153 :OControlImport(_rImport, _rEventManager, _rxParentContainer, _eType)
1157 bool OPasswordImport::handleAttribute(sal_Int32 nElement, const OUString& _rValue)
1159 static const sal_Int32 s_nEchoCharAttributeName = OAttributeMetaData::getSpecialAttributeToken(SCAFlags::EchoChar);
1160 if ((nElement & TOKEN_MASK) == s_nEchoCharAttributeName)
1162 // need a special handling for the EchoChar property
1163 PropertyValue aEchoChar;
1164 aEchoChar.Name = PROPERTY_ECHOCHAR;
1165 OSL_ENSURE(_rValue.getLength() == 1, "OPasswordImport::handleAttribute: invalid echo char attribute!");
1166 // we ourself should not have written values other than of length 1
1167 if (_rValue.getLength() >= 1)
1168 aEchoChar.Value <<= static_cast<sal_Int16>(_rValue[0]);
1169 else
1170 aEchoChar.Value <<= sal_Int16(0);
1171 implPushBackPropertyValue(aEchoChar);
1172 return true;
1174 return OControlImport::handleAttribute(nElement, _rValue);
1177 //= ORadioImport
1178 ORadioImport::ORadioImport(OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager,
1179 const Reference< XNameContainer >& _rxParentContainer, OControlElement::ElementType _eType)
1180 :OImagePositionImport( _rImport, _rEventManager, _rxParentContainer, _eType )
1184 bool ORadioImport::handleAttribute(sal_Int32 nElement, const OUString& _rValue)
1186 // need special handling for the State & CurrentState properties:
1187 // they're stored as booleans, but expected to be int16 properties
1188 static const sal_Int32 nCurrentSelectedAttributeName = OAttributeMetaData::getCommonControlAttributeToken(CCAFlags::CurrentSelected);
1189 static const sal_Int32 nSelectedAttributeName = OAttributeMetaData::getCommonControlAttributeToken(CCAFlags::Selected);
1190 if ( (nElement & TOKEN_MASK) == nCurrentSelectedAttributeName
1191 || (nElement & TOKEN_MASK) == nSelectedAttributeName
1194 const OAttribute2Property::AttributeAssignment* pProperty = m_rContext.getAttributeMap().getAttributeTranslation(nElement & TOKEN_MASK);
1195 assert(pProperty && "ORadioImport::handleAttribute: invalid property map!");
1196 if (pProperty)
1198 const Any aBooleanValue( PropertyConversion::convertString(pProperty->aPropertyType, _rValue, pProperty->pEnumMap) );
1200 // create and store a new PropertyValue
1201 PropertyValue aNewValue;
1202 aNewValue.Name = pProperty->sPropertyName;
1203 aNewValue.Value <<= static_cast<sal_Int16>(::cppu::any2bool(aBooleanValue));
1205 implPushBackPropertyValue(aNewValue);
1207 return true;
1209 return OImagePositionImport::handleAttribute( nElement, _rValue );
1212 //= OURLReferenceImport
1213 OURLReferenceImport::OURLReferenceImport(OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager,
1214 const Reference< XNameContainer >& _rxParentContainer,
1215 OControlElement::ElementType _eType)
1216 :OImagePositionImport(_rImport, _rEventManager, _rxParentContainer, _eType)
1220 bool OURLReferenceImport::handleAttribute(sal_Int32 nElement, const OUString& _rValue)
1222 static const sal_Int32 s_nTargetLocationAttributeName = OAttributeMetaData::getCommonControlAttributeToken( CCAFlags::TargetLocation );
1223 static const sal_Int32 s_nImageDataAttributeName = OAttributeMetaData::getCommonControlAttributeToken( CCAFlags::ImageData );
1225 // need to make the URL absolute if
1226 // * it's the image-data attribute
1227 // * it's the target-location attribute, and we're dealing with an object which has the respective property
1228 bool bMakeAbsolute =
1229 (nElement & TOKEN_MASK) == s_nImageDataAttributeName
1230 || ( (nElement & TOKEN_MASK) == s_nTargetLocationAttributeName
1231 && ( ( OControlElement::BUTTON == m_eElementType )
1232 || ( OControlElement::IMAGE == m_eElementType )
1236 if (bMakeAbsolute && !_rValue.isEmpty())
1238 OUString sAdjustedValue = _rValue;
1239 if ((nElement & TOKEN_MASK) != s_nImageDataAttributeName)
1240 sAdjustedValue = m_rContext.getGlobalContext().GetAbsoluteReference( _rValue );
1241 return OImagePositionImport::handleAttribute( nElement, sAdjustedValue );
1244 return OImagePositionImport::handleAttribute( nElement, _rValue );
1247 //= OButtonImport
1248 OButtonImport::OButtonImport(OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager,
1249 const Reference< XNameContainer >& _rxParentContainer,
1250 OControlElement::ElementType _eType)
1251 :OURLReferenceImport(_rImport, _rEventManager, _rxParentContainer, _eType)
1253 enableTrackAttributes();
1256 void OButtonImport::startFastElement(sal_Int32 nElement, const Reference< XFastAttributeList >& _rxAttrList)
1258 OURLReferenceImport::startFastElement(nElement, _rxAttrList);
1260 // handle the target-frame attribute
1261 simulateDefaultedAttribute(OAttributeMetaData::getCommonControlAttributeToken(CCAFlags::TargetFrame), PROPERTY_TARGETFRAME, "_blank");
1264 //= OValueRangeImport
1265 OValueRangeImport::OValueRangeImport( OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager,
1266 const Reference< XNameContainer >& _rxParentContainer, OControlElement::ElementType _eType )
1267 :OControlImport( _rImport, _rEventManager, _rxParentContainer, _eType )
1268 ,m_nStepSizeValue( 1 )
1273 bool OValueRangeImport::handleAttribute( sal_Int32 nElement, const OUString& _rValue )
1275 if ( (nElement & TOKEN_MASK) == OAttributeMetaData::getSpecialAttributeToken( SCAFlags::StepSize ) )
1277 ::sax::Converter::convertNumber( m_nStepSizeValue, _rValue );
1278 return true;
1280 return OControlImport::handleAttribute( nElement, _rValue );
1283 void OValueRangeImport::startFastElement( sal_Int32 nElement, const Reference< XFastAttributeList >& _rxAttrList )
1285 OControlImport::startFastElement( nElement, _rxAttrList );
1287 if ( m_xInfo.is() )
1289 if ( m_xInfo->hasPropertyByName( PROPERTY_SPIN_INCREMENT ) )
1290 m_xElement->setPropertyValue( PROPERTY_SPIN_INCREMENT, Any( m_nStepSizeValue ) );
1291 else if ( m_xInfo->hasPropertyByName( PROPERTY_LINE_INCREMENT ) )
1292 m_xElement->setPropertyValue( PROPERTY_LINE_INCREMENT, Any( m_nStepSizeValue ) );
1296 //= OTextLikeImport
1297 OTextLikeImport::OTextLikeImport(OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager,
1298 const Reference< XNameContainer >& _rxParentContainer,
1299 OControlElement::ElementType _eType)
1300 :OControlImport(_rImport, _rEventManager, _rxParentContainer, _eType)
1301 ,m_bEncounteredTextPara( false )
1303 enableTrackAttributes();
1306 css::uno::Reference< css::xml::sax::XFastContextHandler > OTextLikeImport::createFastChildContext(
1307 sal_Int32 nElement,
1308 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
1310 if ( nElement == XML_ELEMENT(TEXT, XML_P) )
1312 OSL_ENSURE( m_eElementType == OControlElement::TEXT_AREA,
1313 "OTextLikeImport::CreateChildContext: text paragraphs in a non-text-area?" );
1315 if ( m_eElementType == OControlElement::TEXT_AREA )
1317 Reference< XText > xTextElement( m_xElement, UNO_QUERY );
1318 if ( xTextElement.is() )
1320 rtl::Reference < XMLTextImportHelper > xTextImportHelper( m_rContext.getGlobalContext().GetTextImport() );
1322 if ( !m_xCursor.is() )
1324 m_xOldCursor = xTextImportHelper->GetCursor();
1325 m_xCursor = xTextElement->createTextCursor();
1327 if ( m_xCursor.is() )
1328 xTextImportHelper->SetCursor( m_xCursor );
1330 if ( m_xCursor.is() )
1332 m_bEncounteredTextPara = true;
1333 return xTextImportHelper->CreateTextChildContext( m_rContext.getGlobalContext(), nElement, xAttrList );
1336 else
1338 // in theory, we could accumulate all the text portions (without formatting),
1339 // and set it as Text property at the model ...
1344 return OControlImport::createFastChildContext( nElement, xAttrList );
1347 void OTextLikeImport::startFastElement(sal_Int32 nElement, const Reference< css::xml::sax::XFastAttributeList >& _rxAttrList)
1349 OControlImport::startFastElement(nElement, _rxAttrList);
1351 // handle the convert-empty-to-null attribute, whose default is different from the property default
1352 // unfortunately, different classes are imported by this class ('cause they're represented by the
1353 // same XML element), though not all of them know this property.
1354 // So we have to do a check ...
1355 if (m_xElement.is() && m_xInfo.is() && m_xInfo->hasPropertyByName(PROPERTY_EMPTY_IS_NULL) )
1356 simulateDefaultedAttribute(OAttributeMetaData::getDatabaseAttributeToken(DAFlags::ConvertEmpty), PROPERTY_EMPTY_IS_NULL, "false");
1359 namespace {
1361 struct EqualHandle
1363 const sal_Int32 m_nHandle;
1364 explicit EqualHandle( sal_Int32 _nHandle ) : m_nHandle( _nHandle ) { }
1366 bool operator()( const PropertyValue& _rProp )
1368 return _rProp.Handle == m_nHandle;
1374 void OTextLikeImport::removeRedundantCurrentValue()
1376 if ( !m_bEncounteredTextPara )
1377 return;
1379 // In case the text is written in the text:p elements, we need to ignore what we read as
1380 // current-value attribute, since it's redundant.
1381 // fortunately, OElementImport tagged the value property with the PROPID_CURRENT_VALUE
1382 // handle, so we do not need to determine the name of our value property here
1383 // (normally, it should be "Text", since no other controls than the edit field should
1384 // have the text:p elements)
1385 PropertyValueArray::iterator aValuePropertyPos = ::std::find_if(
1386 m_aValues.begin(),
1387 m_aValues.end(),
1388 EqualHandle( PROPID_CURRENT_VALUE )
1390 if ( aValuePropertyPos != m_aValues.end() )
1392 OSL_ENSURE( aValuePropertyPos->Name == PROPERTY_TEXT, "OTextLikeImport::EndElement: text:p was present, but our value property is *not* 'Text'!" );
1393 if ( aValuePropertyPos->Name == PROPERTY_TEXT )
1395 m_aValues.erase(aValuePropertyPos);
1399 // additionally, we need to set the "RichText" property of our element to sal_True
1400 // (the presence of the text:p is used as indicator for the value of the RichText property)
1401 bool bHasRichTextProperty = false;
1402 if ( m_xInfo.is() )
1403 bHasRichTextProperty = m_xInfo->hasPropertyByName( PROPERTY_RICH_TEXT );
1404 OSL_ENSURE( bHasRichTextProperty, "OTextLikeImport::EndElement: text:p, but no rich text control?" );
1405 if ( bHasRichTextProperty )
1406 m_xElement->setPropertyValue( PROPERTY_RICH_TEXT, Any( true ) );
1407 // Note that we do *not* set the RichText property (in case our element has one) to sal_False here
1408 // since this is the default of this property, anyway.
1411 namespace {
1413 struct EqualName
1415 const OUString & m_sName;
1416 explicit EqualName( const OUString& _rName ) : m_sName( _rName ) { }
1418 bool operator()( const PropertyValue& _rProp )
1420 return _rProp.Name == m_sName;
1426 void OTextLikeImport::adjustDefaultControlProperty()
1428 // In OpenOffice.org 2.0, we changed the implementation of the css.form.component.TextField (the model of a text field control),
1429 // so that it now uses another default control. So if we encounter a text field where the *old* default
1430 // control property is writing, we are not allowed to use it
1431 PropertyValueArray::iterator aDefaultControlPropertyPos = ::std::find_if(
1432 m_aValues.begin(),
1433 m_aValues.end(),
1434 EqualName( "DefaultControl" )
1436 if ( aDefaultControlPropertyPos != m_aValues.end() )
1438 OUString sDefaultControl;
1439 OSL_VERIFY( aDefaultControlPropertyPos->Value >>= sDefaultControl );
1440 if ( sDefaultControl == "stardiv.one.form.control.Edit" )
1442 // complete remove this property value from the array. Today's "default value" of the "DefaultControl"
1443 // property is sufficient
1444 m_aValues.erase(aDefaultControlPropertyPos);
1449 void OTextLikeImport::endFastElement(sal_Int32 nElement)
1451 removeRedundantCurrentValue();
1452 adjustDefaultControlProperty();
1454 // let the base class do the stuff
1455 OControlImport::endFastElement(nElement);
1457 // some cleanups
1458 rtl::Reference < XMLTextImportHelper > xTextImportHelper( m_rContext.getGlobalContext().GetTextImport() );
1459 if ( m_xCursor.is() )
1461 // delete the newline which has been imported erroneously
1462 // TODO (fs): stole this code somewhere - why don't we fix the text import??
1463 m_xCursor->gotoEnd( false );
1464 m_xCursor->goLeft( 1, true );
1465 m_xCursor->setString( OUString() );
1467 // reset cursor
1468 xTextImportHelper->ResetCursor();
1471 if ( m_xOldCursor.is() )
1472 xTextImportHelper->SetCursor( m_xOldCursor );
1476 //= OListAndComboImport
1477 OListAndComboImport::OListAndComboImport(OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager,
1478 const Reference< XNameContainer >& _rxParentContainer,
1479 OControlElement::ElementType _eType)
1480 :OControlImport(_rImport, _rEventManager, _rxParentContainer, _eType)
1481 ,m_nEmptyListItems( 0 )
1482 ,m_nEmptyValueItems( 0 )
1483 ,m_bEncounteredLSAttrib( false )
1484 ,m_bLinkWithIndexes( false )
1486 if (OControlElement::COMBOBOX == m_eElementType)
1487 enableTrackAttributes();
1490 css::uno::Reference< css::xml::sax::XFastContextHandler > OListAndComboImport::createFastChildContext(
1491 sal_Int32 nElement,
1492 const css::uno::Reference< css::xml::sax::XFastAttributeList >& _rxAttrList )
1494 // is it the "option" sub tag of a listbox ?
1495 if ((nElement & TOKEN_MASK) == XML_OPTION)
1496 return new OListOptionImport(GetImport(), this);
1498 // is it the "item" sub tag of a combobox ?
1499 if ((nElement & TOKEN_MASK) == XML_ITEM)
1500 return new OComboItemImport(GetImport(), this);
1502 // everything else
1503 return OControlImport::createFastChildContext(nElement, _rxAttrList);
1506 void OListAndComboImport::startFastElement(sal_Int32 nElement, const Reference< XFastAttributeList >& _rxAttrList)
1508 m_bLinkWithIndexes = false;
1510 OControlImport::startFastElement(nElement, _rxAttrList);
1512 if (OControlElement::COMBOBOX == m_eElementType)
1514 // for the auto-completion
1515 // the attribute default does not equal the property default, so in case we did not read this attribute,
1516 // we have to simulate it
1517 simulateDefaultedAttribute( OAttributeMetaData::getSpecialAttributeToken( SCAFlags::AutoCompletion ), PROPERTY_AUTOCOMPLETE, "false");
1519 // same for the convert-empty-to-null attribute, which's default is different from the property default
1520 simulateDefaultedAttribute( OAttributeMetaData::getDatabaseAttributeToken( DAFlags::ConvertEmpty ), PROPERTY_EMPTY_IS_NULL, "false");
1524 void OListAndComboImport::endFastElement(sal_Int32 nElement)
1526 // append the list source property the properties sequence of our importer
1527 // the string item list
1528 PropertyValue aItemList;
1529 aItemList.Name = PROPERTY_STRING_ITEM_LIST;
1530 aItemList.Value <<= comphelper::containerToSequence(m_aListSource);
1531 implPushBackPropertyValue(aItemList);
1533 if (OControlElement::LISTBOX == m_eElementType)
1535 OSL_ENSURE((m_aListSource.size() + m_nEmptyListItems) == (m_aValueList.size() + m_nEmptyValueItems),
1536 "OListAndComboImport::EndElement: inconsistence between labels and values!");
1538 if ( !m_bEncounteredLSAttrib )
1540 // the value sequence
1541 PropertyValue aValueList;
1542 aValueList.Name = PROPERTY_LISTSOURCE;
1543 aValueList.Value <<= comphelper::containerToSequence(m_aValueList);
1544 implPushBackPropertyValue(aValueList);
1547 // the select sequence
1548 PropertyValue aSelected;
1549 aSelected.Name = PROPERTY_SELECT_SEQ;
1550 aSelected.Value <<= comphelper::containerToSequence(m_aSelectedSeq);
1551 implPushBackPropertyValue(aSelected);
1553 // the default select sequence
1554 PropertyValue aDefaultSelected;
1555 aDefaultSelected.Name = PROPERTY_DEFAULT_SELECT_SEQ;
1556 aDefaultSelected.Value <<= comphelper::containerToSequence(m_aDefaultSelectedSeq);
1557 implPushBackPropertyValue(aDefaultSelected);
1560 OControlImport::endFastElement(nElement);
1562 // the external list source, if applicable
1563 if ( m_xElement.is() && !m_sCellListSource.isEmpty() )
1564 m_rContext.registerCellRangeListSource( m_xElement, m_sCellListSource );
1567 void OListAndComboImport::doRegisterCellValueBinding( const OUString& _rBoundCellAddress )
1569 OUString sBoundCellAddress( _rBoundCellAddress );
1570 if ( m_bLinkWithIndexes )
1572 // This is a HACK. We register a string which is no valid address, but allows
1573 // (somewhere else) to determine that a non-standard binding should be created.
1574 // This hack is acceptable for OOo 1.1.1, since the file format for value
1575 // bindings of form controls is to be changed afterwards, anyway.
1576 sBoundCellAddress += ":index";
1579 OControlImport::doRegisterCellValueBinding( sBoundCellAddress );
1582 bool OListAndComboImport::handleAttribute(sal_Int32 nElement, const OUString& _rValue)
1584 static const sal_Int32 nListSourceAttributeName = OAttributeMetaData::getDatabaseAttributeToken(DAFlags::ListSource);
1585 if ( (nElement & TOKEN_MASK) == nListSourceAttributeName )
1587 PropertyValue aListSource;
1588 aListSource.Name = PROPERTY_LISTSOURCE;
1590 // it's the ListSource attribute
1591 m_bEncounteredLSAttrib = true;
1592 if ( OControlElement::COMBOBOX == m_eElementType )
1594 aListSource.Value <<= _rValue;
1596 else
1598 // a listbox which has a list-source attribute must have a list-source-type of something
1599 // not equal to ValueList.
1600 // In this case, the list-source value is simply the one and only element of the ListSource property.
1601 Sequence<OUString> aListSourcePropValue { _rValue };
1602 aListSource.Value <<= aListSourcePropValue;
1605 implPushBackPropertyValue( aListSource );
1606 return true;
1609 if ( (nElement & TOKEN_MASK) == OAttributeMetaData::getBindingAttributeToken( BAFlags::ListCellRange ) )
1611 m_sCellListSource = _rValue;
1612 return true;
1615 if ( (nElement & TOKEN_MASK) == OAttributeMetaData::getBindingAttributeToken( BAFlags::ListLinkingType ) )
1617 sal_Int16 nLinkageType = 0;
1618 PropertyConversion::convertString(
1619 ::cppu::UnoType<sal_Int16>::get(),
1620 _rValue,
1621 aListLinkageMap
1622 ) >>= nLinkageType;
1624 m_bLinkWithIndexes = ( nLinkageType != 0 );
1625 return true;
1628 return OControlImport::handleAttribute(nElement, _rValue);
1631 void OListAndComboImport::implPushBackLabel(const OUString& _rLabel)
1633 OSL_ENSURE(!m_nEmptyListItems, "OListAndComboImport::implPushBackValue: label list is already done!");
1634 if (!m_nEmptyListItems)
1635 m_aListSource.push_back(_rLabel);
1638 void OListAndComboImport::implPushBackValue(const OUString& _rValue)
1640 OSL_ENSURE(!m_nEmptyValueItems, "OListAndComboImport::implPushBackValue: value list is already done!");
1641 if (!m_nEmptyValueItems)
1643 OSL_ENSURE( !m_bEncounteredLSAttrib, "OListAndComboImport::implPushBackValue: invalid structure! Did you save this document with a version prior SRC641 m?" );
1644 // We already had the list-source attribute, which means that the ListSourceType is
1645 // not ValueList, which means that the ListSource should contain only one string in
1646 // the first element of the sequence
1647 // All other values in the file are invalid
1649 m_aValueList.push_back( _rValue );
1653 void OListAndComboImport::implEmptyLabelFound()
1655 ++m_nEmptyListItems;
1658 void OListAndComboImport::implEmptyValueFound()
1660 ++m_nEmptyValueItems;
1663 void OListAndComboImport::implSelectCurrentItem()
1665 OSL_ENSURE((m_aListSource.size() + m_nEmptyListItems) == (m_aValueList.size() + m_nEmptyValueItems),
1666 "OListAndComboImport::implSelectCurrentItem: inconsistence between labels and values!");
1668 sal_Int16 nItemNumber = static_cast<sal_Int16>(m_aListSource.size() - 1 + m_nEmptyListItems);
1669 m_aSelectedSeq.push_back(nItemNumber);
1672 void OListAndComboImport::implDefaultSelectCurrentItem()
1674 OSL_ENSURE((m_aListSource.size() + m_nEmptyListItems) == (m_aValueList.size() + m_nEmptyValueItems),
1675 "OListAndComboImport::implDefaultSelectCurrentItem: inconsistence between labels and values!");
1677 sal_Int16 nItemNumber = static_cast<sal_Int16>(m_aListSource.size() - 1 + m_nEmptyListItems);
1678 m_aDefaultSelectedSeq.push_back(nItemNumber);
1681 //= OListOptionImport
1682 OListOptionImport::OListOptionImport(SvXMLImport& _rImport,
1683 OListAndComboImportRef _xListBox)
1684 :SvXMLImportContext(_rImport)
1685 ,m_xListBoxImport(std::move(_xListBox))
1689 void OListOptionImport::startFastElement(sal_Int32 nElement, const Reference< XFastAttributeList >& _rxAttrList)
1691 // the label and the value
1692 const sal_Int32 nLabelAttribute = (nElement & ~TOKEN_MASK) | XML_LABEL;
1693 const sal_Int32 nValueAttribute = (nElement & ~TOKEN_MASK) | XML_VALUE;
1695 // the label attribute
1696 OUString sValue = _rxAttrList->getOptionalValue(nLabelAttribute);
1697 bool bNonexistentAttribute = !_rxAttrList->hasAttribute(nLabelAttribute);
1699 if (bNonexistentAttribute)
1700 m_xListBoxImport->implEmptyLabelFound();
1701 else
1702 m_xListBoxImport->implPushBackLabel( sValue );
1704 // the value attribute
1705 sValue = _rxAttrList->getOptionalValue(nValueAttribute);
1706 bNonexistentAttribute = !_rxAttrList->hasAttribute(nValueAttribute);
1708 if (bNonexistentAttribute)
1709 m_xListBoxImport->implEmptyValueFound();
1710 else
1711 m_xListBoxImport->implPushBackValue( sValue );
1713 // the current-selected and selected
1714 const sal_Int32 nSelectedAttribute = (nElement & ~TOKEN_MASK) | OAttributeMetaData::getCommonControlAttributeToken(CCAFlags::CurrentSelected);
1715 const sal_Int32 nDefaultSelectedAttribute = (nElement & ~TOKEN_MASK) | OAttributeMetaData::getCommonControlAttributeToken(CCAFlags::Selected);
1717 // propagate the selected flag
1718 bool bSelected(false);
1719 (void)::sax::Converter::convertBool(bSelected,
1720 _rxAttrList->getOptionalValue(nSelectedAttribute));
1721 if (bSelected)
1722 m_xListBoxImport->implSelectCurrentItem();
1724 // same for the default selected
1725 bool bDefaultSelected(false);
1726 (void)::sax::Converter::convertBool(bDefaultSelected,
1727 _rxAttrList->getOptionalValue(nDefaultSelectedAttribute));
1728 if (bDefaultSelected)
1729 m_xListBoxImport->implDefaultSelectCurrentItem();
1732 //= OComboItemImport
1733 OComboItemImport::OComboItemImport(SvXMLImport& _rImport,
1734 OListAndComboImportRef _xListBox)
1735 :SvXMLImportContext(_rImport)
1736 ,m_xListBoxImport(std::move(_xListBox))
1740 void OComboItemImport::startFastElement(sal_Int32 nElement, const Reference< XFastAttributeList >& _rxAttrList)
1742 const sal_Int32 nLabelAttributeName = (nElement & ~TOKEN_MASK) |
1743 OAttributeMetaData::getCommonControlAttributeToken(CCAFlags::Label);
1744 m_xListBoxImport->implPushBackLabel(_rxAttrList->getOptionalValue(nLabelAttributeName));
1747 //= OColumnWrapperImport
1748 OColumnWrapperImport::OColumnWrapperImport(OFormLayerXMLImport_Impl& _rImport,
1749 IEventAttacherManager& _rEventManager, sal_Int32 /*nElement*/,
1750 const Reference< XNameContainer >& _rxParentContainer)
1751 :SvXMLImportContext(_rImport.getGlobalContext())
1752 ,m_xParentContainer(_rxParentContainer)
1753 ,m_rFormImport(_rImport)
1754 ,m_rEventManager(_rEventManager)
1757 css::uno::Reference< css::xml::sax::XFastContextHandler > OColumnWrapperImport::createFastChildContext(
1758 sal_Int32 nElement,
1759 const css::uno::Reference< css::xml::sax::XFastAttributeList >& )
1761 OControlImport* pReturn = implCreateChildContext(nElement, OElementNameMap::getElementType(nElement & TOKEN_MASK));
1762 if (pReturn)
1764 OSL_ENSURE(m_xOwnAttributes.is(), "OColumnWrapperImport::CreateChildContext: had no form:column element!");
1765 pReturn->addOuterAttributes(m_xOwnAttributes);
1767 return pReturn;
1769 void OColumnWrapperImport::startFastElement(sal_Int32 /*nElement*/, const Reference< XFastAttributeList >& _rxAttrList)
1771 OSL_ENSURE(!m_xOwnAttributes.is(), "OColumnWrapperImport::StartElement: already have the cloned list!");
1773 // clone the attributes
1774 Reference< XCloneable > xCloneList(_rxAttrList, UNO_QUERY_THROW);
1775 m_xOwnAttributes.set(xCloneList->createClone(), UNO_QUERY_THROW);
1778 OControlImport* OColumnWrapperImport::implCreateChildContext(
1779 sal_Int32 /*nElement*/,
1780 OControlElement::ElementType _eType)
1782 OSL_ENSURE( (OControlElement::TEXT == _eType)
1783 || (OControlElement::TEXT_AREA == _eType)
1784 || (OControlElement::FORMATTED_TEXT == _eType)
1785 || (OControlElement::CHECKBOX == _eType)
1786 || (OControlElement::LISTBOX == _eType)
1787 || (OControlElement::COMBOBOX == _eType)
1788 || (OControlElement::TIME == _eType)
1789 || (OControlElement::DATE == _eType),
1790 "OColumnWrapperImport::implCreateChildContext: invalid or unrecognized sub element!");
1792 switch (_eType)
1794 case OControlElement::COMBOBOX:
1795 case OControlElement::LISTBOX:
1796 return new OColumnImport<OListAndComboImport>(m_rFormImport, m_rEventManager, m_xParentContainer, _eType );
1798 case OControlElement::PASSWORD:
1799 return new OColumnImport<OPasswordImport>(m_rFormImport, m_rEventManager, m_xParentContainer, _eType );
1801 case OControlElement::TEXT:
1802 case OControlElement::TEXT_AREA:
1803 case OControlElement::FORMATTED_TEXT:
1804 return new OColumnImport< OTextLikeImport >( m_rFormImport, m_rEventManager, m_xParentContainer, _eType );
1806 default:
1807 return new OColumnImport<OControlImport>(m_rFormImport, m_rEventManager, m_xParentContainer, _eType );
1811 //= OGridImport
1812 OGridImport::OGridImport(OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager,
1813 const Reference< XNameContainer >& _rxParentContainer,
1814 OControlElement::ElementType _eType)
1815 :OControlImport(_rImport, _rEventManager, _rxParentContainer)
1817 setElementType(_eType);
1820 css::uno::Reference< css::xml::sax::XFastContextHandler > OGridImport::createFastChildContext(
1821 sal_Int32 nElement,
1822 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
1824 // maybe it's a sub control
1825 if ((nElement & TOKEN_MASK) == XML_COLUMN)
1827 if (m_xMeAsContainer.is())
1828 return new OColumnWrapperImport(m_rFormImport, *this, nElement, m_xMeAsContainer);
1829 else
1831 OSL_FAIL("OGridImport::CreateChildContext: don't have an element!");
1832 return nullptr;
1836 return OControlImport::createFastChildContext(nElement, xAttrList);
1839 void OGridImport::endFastElement(sal_Int32 nElement)
1841 OControlImport::endFastElement(nElement);
1843 // now that we have all children, attach the events
1844 css::uno::Reference< css::container::XIndexAccess > xIndexContainer(m_xMeAsContainer, css::uno::UNO_QUERY);
1845 if (xIndexContainer.is())
1846 ODefaultEventAttacherManager::setEvents(xIndexContainer);
1849 css::uno::Reference< css::beans::XPropertySet > OGridImport::createElement()
1851 // let the base class create the object
1852 css::uno::Reference< css::beans::XPropertySet > xReturn = OControlImport::createElement();
1853 if (!xReturn.is())
1854 return xReturn;
1856 // ensure that the object is a XNameContainer (we strongly need this for inserting child elements)
1857 m_xMeAsContainer.set(xReturn, css::uno::UNO_QUERY);
1858 if (!m_xMeAsContainer.is())
1860 OSL_FAIL("OContainerImport::createElement: invalid element (no XNameContainer) created!");
1861 xReturn.clear();
1864 return xReturn;
1867 //= OFormImport
1868 OFormImport::OFormImport(OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager,
1869 const Reference< XNameContainer >& _rxParentContainer)
1870 :OElementImport(_rImport, _rEventManager, _rxParentContainer)
1872 enableTrackAttributes();
1875 css::uno::Reference< css::xml::sax::XFastContextHandler > OFormImport::createFastChildContext(
1876 sal_Int32 nElement,
1877 const uno::Reference< xml::sax::XFastAttributeList>& _rxAttrList )
1879 auto nToken = (nElement & TOKEN_MASK);
1880 if( nToken == XML_FORM )
1881 return new OFormImport( m_rFormImport, *this, m_xMeAsContainer);
1882 else if ( nToken == XML_CONNECTION_RESOURCE )
1883 return new OXMLDataSourceImport(GetImport(), _rxAttrList, m_xElement);
1884 else if( nElement == XML_ELEMENT(OFFICE, XML_EVENT_LISTENERS) ||
1885 nToken == XML_PROPERTIES )
1886 return OElementImport::createFastChildContext( nElement, _rxAttrList );
1887 else
1889 OControlElement::ElementType eType = OElementNameMap::getElementType(nToken);
1890 switch (eType)
1892 case OControlElement::TEXT:
1893 case OControlElement::TEXT_AREA:
1894 case OControlElement::FORMATTED_TEXT:
1895 return new OTextLikeImport(m_rFormImport, *this, m_xMeAsContainer, eType);
1896 case OControlElement::GRID:
1897 return new OGridImport(m_rFormImport, *this, m_xMeAsContainer, eType);
1898 case OControlElement::COMBOBOX:
1899 case OControlElement::LISTBOX:
1900 return new OListAndComboImport(m_rFormImport, *this, m_xMeAsContainer, eType);
1901 case OControlElement::PASSWORD:
1902 return new OPasswordImport(m_rFormImport, *this, m_xMeAsContainer, eType);
1903 case OControlElement::BUTTON:
1904 case OControlElement::IMAGE:
1905 case OControlElement::IMAGE_FRAME:
1906 return new OButtonImport( m_rFormImport, *this, m_xMeAsContainer, eType );
1907 case OControlElement::RADIO:
1908 return new ORadioImport(m_rFormImport, *this, m_xMeAsContainer, eType);
1909 case OControlElement::CHECKBOX:
1910 return new OImagePositionImport(m_rFormImport, *this, m_xMeAsContainer, eType);
1911 case OControlElement::FRAME:
1912 case OControlElement::FIXED_TEXT:
1913 return new OReferredControlImport(m_rFormImport, *this, m_xMeAsContainer);
1914 case OControlElement::VALUERANGE:
1915 return new OValueRangeImport( m_rFormImport, *this, m_xMeAsContainer, eType );
1916 default:
1917 return new OControlImport(m_rFormImport, *this, m_xMeAsContainer, eType);
1922 void OFormImport::startFastElement(sal_Int32 nElement, const Reference< XFastAttributeList >& _rxAttrList)
1924 m_rFormImport.enterEventContext();
1925 OElementImport::startFastElement(nElement, _rxAttrList);
1927 // handle the target-frame attribute
1928 simulateDefaultedAttribute(OAttributeMetaData::getCommonControlAttributeToken(CCAFlags::TargetFrame), PROPERTY_TARGETFRAME, "_blank");
1931 void OFormImport::endFastElement(sal_Int32 nElement)
1933 OElementImport::endFastElement(nElement);
1935 // now that we have all children, attach the events
1936 css::uno::Reference< css::container::XIndexAccess > xIndexContainer(m_xMeAsContainer, css::uno::UNO_QUERY);
1937 if (xIndexContainer.is())
1938 ODefaultEventAttacherManager::setEvents(xIndexContainer);
1940 m_rFormImport.leaveEventContext();
1943 css::uno::Reference< css::beans::XPropertySet > OFormImport::createElement()
1945 // let the base class create the object
1946 css::uno::Reference< css::beans::XPropertySet > xReturn = OElementImport::createElement();
1947 if (!xReturn.is())
1948 return xReturn;
1950 // ensure that the object is a XNameContainer (we strongly need this for inserting child elements)
1951 m_xMeAsContainer.set(xReturn, css::uno::UNO_QUERY);
1952 if (!m_xMeAsContainer.is())
1954 OSL_FAIL("OContainerImport::createElement: invalid element (no XNameContainer) created!");
1955 xReturn.clear();
1958 return xReturn;
1961 bool OFormImport::handleAttribute(sal_Int32 nElement, const OUString& _rValue)
1963 // handle the master/details field attributes (they're way too special to let the OPropertyImport handle them)
1964 static const sal_Int32 s_nMasterFieldsAttributeName = OAttributeMetaData::getFormAttributeToken(faMasterFields);
1965 static const sal_Int32 s_nDetailFieldsAttributeName = OAttributeMetaData::getFormAttributeToken(faDetailFields);
1967 if ( (nElement & TOKEN_MASK) == s_nMasterFieldsAttributeName)
1969 implTranslateStringListProperty(PROPERTY_MASTERFIELDS, _rValue);
1970 return true;
1973 if ( (nElement & TOKEN_MASK) == s_nDetailFieldsAttributeName)
1975 implTranslateStringListProperty(PROPERTY_DETAILFIELDS, _rValue);
1976 return true;
1979 return OElementImport::handleAttribute(nElement, _rValue);
1982 void OFormImport::implTranslateStringListProperty(const OUString& _rPropertyName, const OUString& _rValue)
1984 PropertyValue aProp;
1985 aProp.Name = _rPropertyName;
1987 Sequence< OUString > aList;
1989 // split up the value string
1990 if (!_rValue.isEmpty())
1992 // For the moment, we build a vector instead of a Sequence. It's easier to handle because of its
1993 // push_back method
1994 ::std::vector< OUString > aElements;
1995 // estimate the number of tokens
1996 sal_Int32 nEstimate = 0, nLength = _rValue.getLength();
1997 const sal_Unicode* pChars = _rValue.getStr();
1998 for (sal_Int32 i=0; i<nLength; ++i, ++pChars)
1999 if (*pChars == ',')
2000 ++nEstimate;
2001 aElements.reserve(nEstimate + 1);
2002 // that's the worst case. If the string contains the separator character _quoted_, we reserved too much...
2004 sal_Int32 nElementStart = 0;
2005 sal_Int32 nNextSep = 0;
2008 // extract the current element
2009 nNextSep = ::sax::Converter::indexOfComma(
2010 _rValue, nElementStart);
2011 if (-1 == nNextSep)
2012 nNextSep = nLength;
2013 std::u16string_view sElement = _rValue.subView(nElementStart, nNextSep - nElementStart);
2015 size_t nElementLength = sElement.size();
2016 // when writing the sequence, we quoted the single elements with " characters
2017 OSL_ENSURE( o3tl::starts_with(sElement, u"\"") && o3tl::ends_with(sElement, u"\""),
2018 "OFormImport::implTranslateStringListProperty: invalid quoted element name.");
2019 sElement = sElement.substr(1, nElementLength - 2);
2021 aElements.push_back(OUString(sElement));
2023 // switch to the next element
2024 nElementStart = 1 + nNextSep;
2026 while (nElementStart < nLength);
2028 aList = Sequence< OUString >(aElements.data(), aElements.size());
2030 else
2032 OSL_FAIL("OFormImport::implTranslateStringListProperty: invalid value (empty)!");
2035 aProp.Value <<= aList;
2037 // add the property to the base class' array
2038 implPushBackPropertyValue(aProp);
2040 //= OXMLDataSourceImport
2041 OXMLDataSourceImport::OXMLDataSourceImport(
2042 SvXMLImport& _rImport
2043 ,const Reference< css::xml::sax::XFastAttributeList > & _xAttrList
2044 ,const css::uno::Reference< css::beans::XPropertySet >& _xElement) :
2045 SvXMLImportContext( _rImport)
2047 for( auto& aIter : sax_fastparser::castToFastAttributeList(_xAttrList) )
2049 if ( aIter.getToken() ==
2050 XML_ELEMENT(XLINK, OAttributeMetaData::getCommonControlAttributeToken( CCAFlags::TargetLocation ) ) )
2052 OUString sValue = aIter.toString();
2053 sValue = _rImport.GetAbsoluteReference(sValue);
2054 INetURLObject aURL(sValue);
2055 if ( aURL.GetProtocol() == INetProtocol::File )
2056 _xElement->setPropertyValue(PROPERTY_DATASOURCENAME,Any(sValue));
2057 else
2058 _xElement->setPropertyValue(PROPERTY_URL,Any(sValue)); // the url is the "sdbc:" string
2059 break;
2061 else
2062 SAL_WARN("xmloff", "unknown attribute " << SvXMLImport::getPrefixAndNameFromToken(aIter.getToken()) << "=" << aIter.toString());
2066 OUString OFormImport::determineDefaultServiceName() const
2068 return "com.sun.star.form.component.Form";
2071 } // namespace xmloff
2073 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */