bump product version to 5.0.4.1
[LibreOffice.git] / xmloff / source / forms / elementimport.cxx
blob3c03e0bc3ad01315c6b954a0777f8189111c48f9
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 <xmloff/xmlimp.hxx>
22 #include <xmloff/nmspmap.hxx>
23 #include "strings.hxx"
24 #include "callbacks.hxx"
25 #include "attriblistmerge.hxx"
26 #include <xmloff/xmlnmspe.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/text/XText.hpp>
36 #include <com/sun/star/util/XCloneable.hpp>
37 #include <com/sun/star/util/Duration.hpp>
38 #include <com/sun/star/form/FormComponentType.hpp>
39 #include <com/sun/star/awt/ImagePosition.hpp>
40 #include <com/sun/star/beans/XMultiPropertySet.hpp>
41 #include <com/sun/star/beans/XPropertyContainer.hpp>
42 #include <com/sun/star/beans/PropertyAttribute.hpp>
44 #include <sax/tools/converter.hxx>
45 #include <tools/urlobj.hxx>
46 #include <tools/diagnose_ex.h>
47 #include <rtl/strbuf.hxx>
48 #include <comphelper/extract.hxx>
49 #include <comphelper/types.hxx>
51 #include <algorithm>
52 #include <functional>
54 namespace xmloff
57 using namespace ::xmloff::token;
58 using namespace ::com::sun::star;
59 using namespace ::com::sun::star::uno;
60 using namespace ::com::sun::star::awt;
61 using namespace ::com::sun::star::container;
62 using namespace ::com::sun::star::beans;
63 using namespace ::com::sun::star::script;
64 using namespace ::com::sun::star::lang;
65 using namespace ::com::sun::star::form;
66 using namespace ::com::sun::star::xml;
67 using namespace ::com::sun::star::util;
68 using namespace ::com::sun::star::text;
69 using namespace ::comphelper;
70 using ::com::sun::star::xml::sax::XAttributeList;
72 #define PROPID_VALUE 1
73 #define PROPID_CURRENT_VALUE 2
74 #define PROPID_MIN_VALUE 3
75 #define PROPID_MAX_VALUE 4
77 struct PropertyValueLess
79 bool operator()(const PropertyValue& _rLeft, const PropertyValue& _rRight)
81 return _rLeft.Name < _rRight.Name;
85 template <class ELEMENT>
86 void pushBackSequenceElement(Sequence< ELEMENT >& _rContainer, const ELEMENT& _rElement)
88 sal_Int32 nLen = _rContainer.getLength();
89 _rContainer.realloc(nLen + 1);
90 _rContainer[nLen] = _rElement;
93 //= OElementNameMap
94 OElementNameMap::MapString2Element OElementNameMap::s_sElementTranslations;
96 const OControlElement::ElementType& operator ++(OControlElement::ElementType& _e)
98 OControlElement::ElementType e = _e;
99 sal_Int32 nAsInt = static_cast<sal_Int32>(e);
100 _e = static_cast<OControlElement::ElementType>( ++nAsInt );
101 return _e;
104 OControlElement::ElementType OElementNameMap::getElementType(const OUString& _rName)
106 if ( s_sElementTranslations.empty() )
107 { // initialize
108 for (ElementType eType=(ElementType)0; eType<UNKNOWN; ++eType)
109 s_sElementTranslations[OUString::createFromAscii(getElementName(eType))] = eType;
111 MapString2Element::const_iterator aPos = s_sElementTranslations.find(_rName);
112 if (s_sElementTranslations.end() != aPos)
113 return aPos->second;
115 return UNKNOWN;
118 //= OElementImport
119 OElementImport::OElementImport(OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager, sal_uInt16 _nPrefix, const OUString& _rName,
120 const Reference< XNameContainer >& _rxParentContainer)
121 :OPropertyImport(_rImport, _nPrefix, _rName)
122 ,m_rFormImport(_rImport)
123 ,m_rEventManager(_rEventManager)
124 ,m_pStyleElement( NULL )
125 ,m_xParentContainer(_rxParentContainer)
126 ,m_bImplicitGenericAttributeHandling( true )
128 OSL_ENSURE(m_xParentContainer.is(), "OElementImport::OElementImport: invalid parent container!");
131 OElementImport::~OElementImport()
135 OUString OElementImport::determineDefaultServiceName() const
137 return OUString();
140 void OElementImport::StartElement(const Reference< XAttributeList >& _rxAttrList)
142 ENTER_LOG_CONTEXT( "xmloff::OElementImport - importing one element" );
144 const SvXMLNamespaceMap& rMap = m_rContext.getGlobalContext().GetNamespaceMap();
145 const OUString sImplNameAttribute = rMap.GetQNameByKey( XML_NAMESPACE_FORM, GetXMLToken( XML_CONTROL_IMPLEMENTATION ) );
146 const OUString sControlImplementation = _rxAttrList->getValueByName( sImplNameAttribute );
148 // retrieve the service name
149 if ( !sControlImplementation.isEmpty() )
151 OUString sOOoImplementationName;
152 const sal_uInt16 nImplPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName( sControlImplementation, &sOOoImplementationName );
153 m_sServiceName = ( nImplPrefix == XML_NAMESPACE_OOO ) ? sOOoImplementationName : sControlImplementation;
156 if ( m_sServiceName.isEmpty() )
157 m_sServiceName = determineDefaultServiceName();
159 // create the object *now*. This allows setting properties in the various handleAttribute methods.
160 // (Though currently not all code is migrated to this pattern, most attributes are still handled
161 // by remembering the value (via implPushBackPropertyValue), and setting the correct property value
162 // later (in OControlImport::StartElement).)
163 m_xElement = createElement();
164 if ( m_xElement.is() )
165 m_xInfo = m_xElement->getPropertySetInfo();
167 // call the base class
168 OPropertyImport::StartElement( _rxAttrList );
171 SvXMLImportContext* OElementImport::CreateChildContext(sal_uInt16 _nPrefix, const OUString& _rLocalName,
172 const Reference< XAttributeList >& _rxAttrList)
174 if( token::IsXMLToken(_rLocalName, token::XML_EVENT_LISTENERS) && (XML_NAMESPACE_OFFICE == _nPrefix))
175 return new OFormEventsImportContext(m_rFormImport.getGlobalContext(), _nPrefix, _rLocalName, *this);
177 return OPropertyImport::CreateChildContext(_nPrefix, _rLocalName, _rxAttrList);
180 void OElementImport::EndElement()
182 OSL_ENSURE(m_xElement.is(), "OElementImport::EndElement: invalid element created!");
183 if (!m_xElement.is())
184 return;
186 // apply the non-generic properties
187 implApplySpecificProperties();
189 // set the generic properties
190 implApplyGenericProperties();
192 // set the style properties
193 if ( m_pStyleElement && m_xElement.is() )
195 Reference< XPropertySet > xPropTranslation =
196 new OGridColumnPropertyTranslator( Reference< XMultiPropertySet >( m_xElement, UNO_QUERY ) );
197 const_cast< XMLTextStyleContext* >( m_pStyleElement )->FillPropertySet( xPropTranslation );
199 const OUString sNumberStyleName = m_pStyleElement->GetDataStyleName( );
200 if ( !sNumberStyleName.isEmpty() )
201 // the style also has a number (sub) style
202 m_rContext.applyControlNumberStyle( m_xElement, sNumberStyleName );
205 // insert the element into the parent container
206 if (m_sName.isEmpty())
208 OSL_FAIL("OElementImport::EndElement: did not find a name attribute!");
209 m_sName = implGetDefaultName();
212 if (m_xParentContainer.is())
213 m_xParentContainer->insertByName(m_sName, makeAny(m_xElement));
215 LEAVE_LOG_CONTEXT( );
218 void OElementImport::implApplySpecificProperties()
220 if ( m_aValues.empty() )
221 return;
223 // set all the properties we collected
224 #if OSL_DEBUG_LEVEL > 0
225 // check if the object has all the properties
226 // (We do this in the non-pro version only. Doing it all the time would be much to expensive)
227 if ( m_xInfo.is() )
229 PropertyValueArray::const_iterator aEnd = m_aValues.end();
230 for ( PropertyValueArray::iterator aCheck = m_aValues.begin();
231 aCheck != aEnd;
232 ++aCheck
235 OSL_ENSURE(m_xInfo->hasPropertyByName(aCheck->Name),
236 OStringBuffer("OElementImport::implApplySpecificProperties: read a property (").
237 append(OUStringToOString(aCheck->Name, RTL_TEXTENCODING_ASCII_US)).
238 append(") which does not exist on the element!").getStr());
241 #endif
243 // set the properties
244 const Reference< XMultiPropertySet > xMultiProps(m_xElement, UNO_QUERY);
245 bool bSuccess = false;
246 if (xMultiProps.is())
248 // translate our properties so that the XMultiPropertySet can handle them
250 // sort our property value array so that we can use it in a setPropertyValues
251 ::std::sort( m_aValues.begin(), m_aValues.end(), PropertyValueLess());
253 // the names
254 Sequence< OUString > aNames(m_aValues.size());
255 OUString* pNames = aNames.getArray();
256 // the values
257 Sequence< Any > aValues(m_aValues.size());
258 Any* pValues = aValues.getArray();
259 // copy
261 PropertyValueArray::iterator aEnd = m_aValues.end();
262 for ( PropertyValueArray::iterator aPropValues = m_aValues.begin();
263 aPropValues != aEnd;
264 ++aPropValues, ++pNames, ++pValues
267 *pNames = aPropValues->Name;
268 *pValues = aPropValues->Value;
273 xMultiProps->setPropertyValues(aNames, aValues);
274 bSuccess = true;
276 catch(const Exception&)
278 OSL_FAIL("OElementImport::implApplySpecificProperties: could not set the properties (using the XMultiPropertySet)!");
279 DBG_UNHANDLED_EXCEPTION();
283 if (!bSuccess)
284 { // no XMultiPropertySet or setting all properties at once failed
285 PropertyValueArray::iterator aEnd = m_aValues.end();
286 for ( PropertyValueArray::iterator aPropValues = m_aValues.begin();
287 aPropValues != aEnd;
288 ++aPropValues
291 // this try/catch here is expensive, but because this is just a fallback which should normally not be
292 // used it's acceptable this way ...
295 m_xElement->setPropertyValue(aPropValues->Name, aPropValues->Value);
297 catch(const Exception&)
299 OSL_FAIL(OStringBuffer("OElementImport::implApplySpecificProperties: could not set the property \"").
300 append(OUStringToOString(aPropValues->Name, RTL_TEXTENCODING_ASCII_US)).
301 append("\"!").getStr());
302 DBG_UNHANDLED_EXCEPTION();
308 void OElementImport::implApplyGenericProperties()
310 if ( m_aGenericValues.empty() )
311 return;
313 Reference< XPropertyContainer > xDynamicProperties( m_xElement, UNO_QUERY );
315 PropertyValueArray::iterator aEnd = m_aGenericValues.end();
316 for ( PropertyValueArray::iterator aPropValues =
317 m_aGenericValues.begin();
318 aPropValues != aEnd;
319 ++aPropValues
322 // check property type for numeric types before setting
323 // the property
326 // if such a property does not yet exist at the element, create it if necessary
327 const bool bExistentProperty = m_xInfo->hasPropertyByName( aPropValues->Name );
328 if ( !bExistentProperty )
330 if ( !xDynamicProperties.is() )
332 #if OSL_DEBUG_LEVEL > 0
333 OString aMessage( "OElementImport::implApplyGenericProperties: encountered an unknown property (" );
334 aMessage += OUStringToOString( aPropValues->Name, RTL_TEXTENCODING_ASCII_US );
335 aMessage += "), but component is no PropertyBag!";
336 OSL_FAIL( aMessage.getStr() );
337 #endif
338 continue;
341 xDynamicProperties->addProperty(
342 aPropValues->Name,
343 PropertyAttribute::BOUND | PropertyAttribute::REMOVABLE,
344 aPropValues->Value
347 // re-fetch the PropertySetInfo
348 m_xInfo = m_xElement->getPropertySetInfo();
351 // determine the type of the value (source for the following conversion)
352 TypeClass eValueTypeClass = aPropValues->Value.getValueTypeClass();
353 const bool bValueIsSequence = TypeClass_SEQUENCE == eValueTypeClass;
354 if ( bValueIsSequence )
356 uno::Type aSimpleType( getSequenceElementType( aPropValues->Value.getValueType() ) );
357 eValueTypeClass = aSimpleType.getTypeClass();
360 // determine the type of the property (target for the following conversion)
361 const Property aProperty( m_xInfo->getPropertyByName( aPropValues->Name ) );
362 TypeClass ePropTypeClass = aProperty.Type.getTypeClass();
363 const bool bPropIsSequence = TypeClass_SEQUENCE == ePropTypeClass;
364 if( bPropIsSequence )
366 uno::Type aSimpleType( ::comphelper::getSequenceElementType( aProperty.Type ) );
367 ePropTypeClass = aSimpleType.getTypeClass();
370 if ( bPropIsSequence != bValueIsSequence )
372 OSL_FAIL( "OElementImport::implImportGenericProperties: either both value and property should be a sequence, or none of them!" );
373 continue;
376 if ( bValueIsSequence )
378 OSL_ENSURE( eValueTypeClass == TypeClass_ANY,
379 "OElementImport::implApplyGenericProperties: only ANYs should have been imported as generic list property!" );
380 // (OPropertyImport should produce only Sequencer< Any >, since it cannot know the real type
382 OSL_ENSURE( ePropTypeClass == TypeClass_SHORT,
383 "OElementImport::implApplyGenericProperties: conversion to sequences other than 'sequence< short >' not implemented, yet!" );
385 Sequence< Any > aXMLValueList;
386 aPropValues->Value >>= aXMLValueList;
387 Sequence< sal_Int16 > aPropertyValueList( aXMLValueList.getLength() );
389 const Any* pXMLValue = aXMLValueList.getConstArray();
390 sal_Int16* pPropValue = aPropertyValueList.getArray();
392 for ( sal_Int32 i=0; i<aXMLValueList.getLength(); ++i, ++pXMLValue, ++pPropValue )
394 // only value sequences of numeric types implemented so far.
395 double nVal( 0 );
396 OSL_VERIFY( *pXMLValue >>= nVal );
397 *pPropValue = static_cast< sal_Int16 >( nVal );
400 aPropValues->Value <<= aPropertyValueList;
402 else if ( ePropTypeClass != eValueTypeClass )
404 switch ( eValueTypeClass )
406 case TypeClass_DOUBLE:
408 double nVal = 0;
409 aPropValues->Value >>= nVal;
410 switch( ePropTypeClass )
412 case TypeClass_BYTE:
413 aPropValues->Value <<= static_cast< sal_Int8 >( nVal );
414 break;
415 case TypeClass_SHORT:
416 aPropValues->Value <<= static_cast< sal_Int16 >( nVal );
417 break;
418 case TypeClass_UNSIGNED_SHORT:
419 aPropValues->Value <<= static_cast< sal_uInt16 >( nVal );
420 break;
421 case TypeClass_LONG:
422 case TypeClass_ENUM:
423 aPropValues->Value <<= static_cast< sal_Int32 >( nVal );
424 break;
425 case TypeClass_UNSIGNED_LONG:
426 aPropValues->Value <<= static_cast< sal_uInt32 >( nVal );
427 break;
428 case TypeClass_UNSIGNED_HYPER:
429 aPropValues->Value <<= static_cast< sal_uInt64 >( nVal );
430 break;
431 case TypeClass_HYPER:
432 aPropValues->Value <<= static_cast< sal_Int64 >( nVal );
433 break;
434 default:
435 OSL_FAIL( "OElementImport::implImportGenericProperties: unsupported value type!" );
436 break;
439 break;
440 default:
441 OSL_FAIL( "OElementImport::implImportGenericProperties: non-double values not supported!" );
442 break;
446 m_xElement->setPropertyValue( aPropValues->Name, aPropValues->Value );
448 catch(const Exception&)
450 OSL_FAIL(OStringBuffer("OElementImport::EndElement: could not set the property \"").
451 append(OUStringToOString(aPropValues->Name, RTL_TEXTENCODING_ASCII_US)).
452 append("\"!").getStr());
453 DBG_UNHANDLED_EXCEPTION();
458 OUString OElementImport::implGetDefaultName() const
460 // no optimization here. If this method gets called, the XML stream did not contain a name for the
461 // element, which is a heavy error. So in this case we don't care for performance
462 static const char sUnnamedName[] = "unnamed";
463 OSL_ENSURE(m_xParentContainer.is(), "OElementImport::implGetDefaultName: no parent container!");
464 if (!m_xParentContainer.is())
465 return OUString(sUnnamedName);
466 Sequence< OUString > aNames = m_xParentContainer->getElementNames();
468 OUString sReturn;
469 const OUString* pNames = NULL;
470 const OUString* pNamesEnd = aNames.getConstArray() + aNames.getLength();
471 for (sal_Int32 i=0; i<32768; ++i) // the limit is nearly arbitrary ...
473 // assemble the new name (suggestion)
474 sReturn = sUnnamedName;
475 sReturn += OUString::number(i);
476 // check the existence (this is the bad performance part ....)
477 for (pNames = aNames.getConstArray(); pNames<pNamesEnd; ++pNames)
479 if (*pNames == sReturn)
481 break;
484 if (pNames<pNamesEnd)
485 // found the name
486 continue;
487 return sReturn;
489 OSL_FAIL("OElementImport::implGetDefaultName: did not find a free name!");
490 return OUString(sUnnamedName);
493 PropertyGroups::const_iterator OElementImport::impl_matchPropertyGroup( const PropertyGroups& i_propertyGroups ) const
495 ENSURE_OR_RETURN( m_xInfo.is(), "OElementImport::impl_matchPropertyGroup: no property set info!", i_propertyGroups.end() );
497 for ( PropertyGroups::const_iterator group = i_propertyGroups.begin();
498 group != i_propertyGroups.end();
499 ++group
502 bool missingProp = false;
503 for ( PropertyDescriptionList::const_iterator prop = group->begin();
504 prop != group->end();
505 ++prop
508 if ( !m_xInfo->hasPropertyByName( (*prop)->propertyName ) )
510 missingProp = true;
511 break;
515 if ( missingProp )
516 // try next group
517 continue;
519 return group;
522 return i_propertyGroups.end();
525 bool OElementImport::tryGenericAttribute( sal_uInt16 _nNamespaceKey, const OUString& _rLocalName, const OUString& _rValue )
527 // the generic approach (which I hope all props will be migrated to, on the medium term): property handlers
528 const AttributeDescription attribute( metadata::getAttributeDescription( _nNamespaceKey, _rLocalName ) );
529 if ( attribute.attributeToken != XML_TOKEN_INVALID )
531 PropertyGroups propertyGroups;
532 metadata::getPropertyGroupList( attribute, propertyGroups );
533 const PropertyGroups::const_iterator pos = impl_matchPropertyGroup( propertyGroups );
534 if ( pos == propertyGroups.end() )
535 return false;
539 const PropertyDescriptionList& rProperties( *pos );
540 const PropertyDescription* first = *rProperties.begin();
541 if ( !first )
543 SAL_WARN( "xmloff.forms", "OElementImport::handleAttribute: invalid property description!" );
544 break;
547 const PPropertyHandler handler = (*first->factory)( first->propertyId );
548 if ( !handler.get() )
550 SAL_WARN( "xmloff.forms", "OElementImport::handleAttribute: invalid property handler!" );
551 break;
554 PropertyValues aValues;
555 for ( PropertyDescriptionList::const_iterator propDesc = rProperties.begin();
556 propDesc != rProperties.end();
557 ++propDesc
560 aValues[ (*propDesc)->propertyId ] = Any();
562 if ( handler->getPropertyValues( _rValue, aValues ) )
564 for ( PropertyDescriptionList::const_iterator propDesc = rProperties.begin();
565 propDesc != rProperties.end();
566 ++propDesc
569 implPushBackPropertyValue( (*propDesc)->propertyName, aValues[ (*propDesc)->propertyId ] );
573 while ( false );
575 // handled
576 return true;
578 return false;
581 bool OElementImport::handleAttribute(sal_uInt16 _nNamespaceKey, const OUString& _rLocalName, const OUString& _rValue)
583 if ( token::IsXMLToken( _rLocalName, token::XML_CONTROL_IMPLEMENTATION ) )
584 // ignore this, it has already been handled in OElementImport::StartElement
585 return true;
587 if ( token::IsXMLToken( _rLocalName, token::XML_NAME ) )
589 if ( m_sName.isEmpty() )
590 // remember the name for later use in EndElement
591 m_sName = _rValue;
592 return true;
595 // maybe it's the style attribute?
596 if ( token::IsXMLToken( _rLocalName, token::XML_TEXT_STYLE_NAME ) )
598 const SvXMLStyleContext* pStyleContext = m_rContext.getStyleElement( _rValue );
599 OSL_ENSURE( pStyleContext, "OElementImport::handleAttribute: do not know the style!" );
600 // remember the element for later usage.
601 m_pStyleElement = PTR_CAST( XMLTextStyleContext, pStyleContext );
602 return true;
605 if ( m_bImplicitGenericAttributeHandling )
606 if ( tryGenericAttribute( _nNamespaceKey, _rLocalName, _rValue ) )
607 return true;
609 // let the base class handle it
610 return OPropertyImport::handleAttribute(_nNamespaceKey, _rLocalName, _rValue);
613 Reference< XPropertySet > OElementImport::createElement()
615 Reference< XPropertySet > xReturn;
616 if (!m_sServiceName.isEmpty())
618 Reference< XComponentContext > xContext = m_rFormImport.getGlobalContext().GetComponentContext();
619 Reference< XInterface > xPure = xContext->getServiceManager()->createInstanceWithContext(m_sServiceName, xContext);
620 OSL_ENSURE(xPure.is(),
621 OStringBuffer("OElementImport::createElement: service factory gave me no object (service name: ").append(OUStringToOString(m_sServiceName, RTL_TEXTENCODING_ASCII_US)).append(")!").getStr());
622 xReturn = Reference< XPropertySet >(xPure, UNO_QUERY);
624 else
625 OSL_FAIL("OElementImport::createElement: no service name to create an element!");
627 return xReturn;
630 void OElementImport::registerEvents(const Sequence< ScriptEventDescriptor >& _rEvents)
632 OSL_ENSURE(m_xElement.is(), "OElementImport::registerEvents: no element to register events for!");
633 m_rEventManager.registerEvents(m_xElement, _rEvents);
636 void OElementImport::simulateDefaultedAttribute(const sal_Char* _pAttributeName, const OUString& _rPropertyName, const sal_Char* _pAttributeDefault)
638 OSL_ENSURE( m_xInfo.is(), "OPropertyImport::simulateDefaultedAttribute: the component should be more gossipy about it's properties!" );
640 if ( !m_xInfo.is() || m_xInfo->hasPropertyByName( _rPropertyName ) )
642 OUString sLocalAttrName = OUString::createFromAscii(_pAttributeName);
643 if ( !encounteredAttribute( sLocalAttrName ) )
644 OSL_VERIFY( handleAttribute( XML_NAMESPACE_FORM, sLocalAttrName, OUString::createFromAscii( _pAttributeDefault ) ) );
648 //= OControlImport
649 OControlImport::OControlImport(OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager, sal_uInt16 _nPrefix, const OUString& _rName,
650 const Reference< XNameContainer >& _rxParentContainer)
651 :OElementImport(_rImport, _rEventManager, _nPrefix, _rName, _rxParentContainer)
652 ,m_eElementType(OControlElement::UNKNOWN)
654 disableImplicitGenericAttributeHandling();
657 OControlImport::OControlImport(OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager, sal_uInt16 _nPrefix, const OUString& _rName,
658 const Reference< XNameContainer >& _rxParentContainer, OControlElement::ElementType _eType)
659 :OElementImport(_rImport, _rEventManager, _nPrefix, _rName, _rxParentContainer)
660 ,m_eElementType(_eType)
662 disableImplicitGenericAttributeHandling();
665 OUString OControlImport::determineDefaultServiceName() const
667 const sal_Char* pServiceName = NULL;
668 switch ( m_eElementType )
670 case OControlElement::TEXT:
671 case OControlElement::TEXT_AREA:
672 case OControlElement::PASSWORD: pServiceName = "com.sun.star.form.component.TextField"; break;
673 case OControlElement::FILE: pServiceName = "com.sun.star.form.component.FileControl"; break;
674 case OControlElement::FORMATTED_TEXT: pServiceName = "com.sun.star.form.component.FormattedField"; break;
675 case OControlElement::FIXED_TEXT: pServiceName = "com.sun.star.form.component.FixedText"; break;
676 case OControlElement::COMBOBOX: pServiceName = "com.sun.star.form.component.ComboBox"; break;
677 case OControlElement::LISTBOX: pServiceName = "com.sun.star.form.component.ListBox"; break;
678 case OControlElement::BUTTON: pServiceName = "com.sun.star.form.component.CommandButton"; break;
679 case OControlElement::IMAGE: pServiceName = "com.sun.star.form.component.ImageButton"; break;
680 case OControlElement::CHECKBOX: pServiceName = "com.sun.star.form.component.CheckBox"; break;
681 case OControlElement::RADIO: pServiceName = "com.sun.star.form.component.RadioButton"; break;
682 case OControlElement::FRAME: pServiceName = "com.sun.star.form.component.GroupBox"; break;
683 case OControlElement::IMAGE_FRAME: pServiceName = "com.sun.star.form.component.DatabaseImageControl"; break;
684 case OControlElement::HIDDEN: pServiceName = "com.sun.star.form.component.HiddenControl"; break;
685 case OControlElement::GRID: pServiceName = "com.sun.star.form.component.GridControl"; break;
686 case OControlElement::VALUERANGE: pServiceName = "com.sun.star.form.component.ScrollBar"; break;
687 case OControlElement::TIME: pServiceName = "com.sun.star.form.component.TimeField"; break;
688 case OControlElement::DATE: pServiceName = "com.sun.star.form.component.DateField"; break;
689 default: break;
691 if ( pServiceName != NULL )
692 return OUString::createFromAscii( pServiceName );
693 return OUString();
696 void OControlImport::addOuterAttributes(const Reference< XAttributeList >& _rxOuterAttribs)
698 OSL_ENSURE(!m_xOuterAttributes.is(), "OControlImport::addOuterAttributes: already have these attributes!");
699 m_xOuterAttributes = _rxOuterAttribs;
702 bool OControlImport::handleAttribute(sal_uInt16 _nNamespaceKey, const OUString& _rLocalName, const OUString& _rValue)
704 static const sal_Char* pLinkedCellAttributeName = OAttributeMetaData::getBindingAttributeName(BA_LINKED_CELL);
706 if (IsXMLToken(_rLocalName, XML_ID))
707 { // it's the control id
708 if (XML_NAMESPACE_XML == _nNamespaceKey)
710 m_sControlId = _rValue;
712 else if (XML_NAMESPACE_FORM == _nNamespaceKey)
714 if (m_sControlId.isEmpty())
716 m_sControlId = _rValue;
719 return true;
722 if ( _rLocalName.equalsAscii( pLinkedCellAttributeName ) )
723 { // it's the address of a spreadsheet cell
724 m_sBoundCellAddress = _rValue;
725 return true;
728 if ( _nNamespaceKey == XML_NAMESPACE_XFORMS && IsXMLToken( _rLocalName, XML_BIND ) )
730 m_sBindingID = _rValue;
731 return true;
734 if ( _nNamespaceKey == XML_NAMESPACE_FORM && IsXMLToken( _rLocalName, XML_XFORMS_LIST_SOURCE ) )
736 m_sListBindingID = _rValue;
737 return true;
740 if ( ( ( _nNamespaceKey == XML_NAMESPACE_FORM )
741 && IsXMLToken( _rLocalName, XML_XFORMS_SUBMISSION )
743 || ( ( _nNamespaceKey == XML_NAMESPACE_XFORMS )
744 && IsXMLToken( _rLocalName, XML_SUBMISSION )
748 m_sSubmissionID = _rValue;
749 return true;
752 if ( OElementImport::tryGenericAttribute( _nNamespaceKey, _rLocalName, _rValue ) )
753 return true;
755 static const sal_Char* pValueAttributeName = OAttributeMetaData::getCommonControlAttributeName(CCA_VALUE);
756 static const sal_Char* pCurrentValueAttributeName = OAttributeMetaData::getCommonControlAttributeName(CCA_CURRENT_VALUE);
757 static const sal_Char* pMinValueAttributeName = OAttributeMetaData::getSpecialAttributeName(SCA_MIN_VALUE);
758 static const sal_Char* pMaxValueAttributeName = OAttributeMetaData::getSpecialAttributeName(SCA_MAX_VALUE);
759 static const sal_Char* pRepeatDelayAttributeName = OAttributeMetaData::getSpecialAttributeName( SCA_REPEAT_DELAY );
761 sal_Int32 nHandle = -1;
762 if ( _rLocalName.equalsAscii( pValueAttributeName ) )
763 nHandle = PROPID_VALUE;
764 else if ( _rLocalName.equalsAscii( pCurrentValueAttributeName ) )
765 nHandle = PROPID_CURRENT_VALUE;
766 else if ( _rLocalName.equalsAscii( pMinValueAttributeName ) )
767 nHandle = PROPID_MIN_VALUE;
768 else if ( _rLocalName.equalsAscii( pMaxValueAttributeName ) )
769 nHandle = PROPID_MAX_VALUE;
770 if ( nHandle != -1 )
772 // for the moment, simply remember the name and the value
773 PropertyValue aProp;
774 aProp.Name = _rLocalName;
775 aProp.Handle = nHandle;
776 aProp.Value <<= _rValue;
777 m_aValueProperties.push_back(aProp);
778 return true;
781 if ( _rLocalName.equalsAscii( pRepeatDelayAttributeName ) )
783 util::Duration aDuration;
784 if (::sax::Converter::convertDuration(aDuration, _rValue))
786 PropertyValue aProp;
787 aProp.Name = PROPERTY_REPEAT_DELAY;
788 sal_Int32 const nMS =
789 ((aDuration.Hours * 60 + aDuration.Minutes) * 60
790 + aDuration.Seconds) * 1000 + aDuration.NanoSeconds/1000000;
791 aProp.Value <<= nMS;
793 implPushBackPropertyValue(aProp);
795 return true;
798 return OElementImport::handleAttribute( _nNamespaceKey, _rLocalName, _rValue );
801 void OControlImport::StartElement(const Reference< XAttributeList >& _rxAttrList)
803 ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XAttributeList > xAttributes;
804 if( m_xOuterAttributes.is() )
806 // merge the attribute lists
807 OAttribListMerger* pMerger = new OAttribListMerger;
808 // our own one
809 pMerger->addList(_rxAttrList);
810 // and the ones of our enclosing element
811 pMerger->addList(m_xOuterAttributes);
812 xAttributes = pMerger;
814 else
816 xAttributes = _rxAttrList;
819 // let the base class handle all the attributes
820 OElementImport::StartElement(xAttributes);
822 if ( !m_aValueProperties.empty() && m_xElement.is())
824 // get the property set info
825 if (!m_xInfo.is())
827 OSL_FAIL("OControlImport::StartElement: no PropertySetInfo!");
828 return;
831 const sal_Char* pValueProperty = NULL;
832 const sal_Char* pCurrentValueProperty = NULL;
833 const sal_Char* pMinValueProperty = NULL;
834 const sal_Char* pMaxValueProperty = NULL;
836 bool bRetrievedValues = false;
837 bool bRetrievedValueLimits = false;
839 // get the class id of our element
840 sal_Int16 nClassId = FormComponentType::CONTROL;
841 m_xElement->getPropertyValue(PROPERTY_CLASSID) >>= nClassId;
843 // translate the value properties we collected in handleAttributes
844 PropertyValueArray::iterator aEnd = m_aValueProperties.end();
845 for ( PropertyValueArray::iterator aValueProps = m_aValueProperties.begin();
846 aValueProps != aEnd;
847 ++aValueProps
850 bool bSuccess = false;
851 switch (aValueProps->Handle)
853 case PROPID_VALUE:
854 case PROPID_CURRENT_VALUE:
856 // get the property names
857 if (!bRetrievedValues)
859 getValuePropertyNames(m_eElementType, nClassId, pCurrentValueProperty, pValueProperty);
860 if ( !pCurrentValueProperty && !pValueProperty )
862 SAL_WARN( "xmloff.forms", "OControlImport::StartElement: illegal value property names!" );
863 break;
866 bRetrievedValues = true;
868 if ( PROPID_VALUE == aValueProps->Handle && !pValueProperty )
870 SAL_WARN( "xmloff.forms", "OControlImport::StartElement: the control does not have a value property!");
871 break;
874 if ( PROPID_CURRENT_VALUE == aValueProps->Handle && !pCurrentValueProperty )
876 SAL_WARN( "xmloff.forms", "OControlImport::StartElement: the control does not have a current-value property!");
877 break;
880 // transfer the name
881 if (PROPID_VALUE == aValueProps->Handle)
882 aValueProps->Name = OUString::createFromAscii(pValueProperty);
883 else
884 aValueProps->Name = OUString::createFromAscii(pCurrentValueProperty);
885 bSuccess = true;
887 break;
888 case PROPID_MIN_VALUE:
889 case PROPID_MAX_VALUE:
891 // get the property names
892 if (!bRetrievedValueLimits)
894 getValueLimitPropertyNames(nClassId, pMinValueProperty, pMaxValueProperty);
895 if ( !pMinValueProperty || !pMaxValueProperty )
897 SAL_WARN( "xmloff.forms", "OControlImport::StartElement: illegal value limit property names!" );
898 break;
901 bRetrievedValueLimits = true;
903 OSL_ENSURE((PROPID_MIN_VALUE != aValueProps->Handle) || pMinValueProperty,
904 "OControlImport::StartElement: the control does not have a value property!");
905 OSL_ENSURE((PROPID_MAX_VALUE != aValueProps->Handle) || pMaxValueProperty,
906 "OControlImport::StartElement: the control does not have a current-value property!");
908 // transfer the name
909 if (PROPID_MIN_VALUE == aValueProps->Handle)
910 aValueProps->Name = OUString::createFromAscii(pMinValueProperty);
911 else
912 aValueProps->Name = OUString::createFromAscii(pMaxValueProperty);
913 bSuccess = true;
915 break;
918 if ( !bSuccess )
919 continue;
921 // translate the value
922 implTranslateValueProperty(m_xInfo, *aValueProps);
923 // add the property to the base class' array
924 implPushBackPropertyValue(*aValueProps);
929 void OControlImport::implTranslateValueProperty(const Reference< XPropertySetInfo >& _rxPropInfo,
930 PropertyValue& _rPropValue)
932 OSL_ENSURE(_rxPropInfo->hasPropertyByName(_rPropValue.Name),
933 "OControlImport::implTranslateValueProperty: invalid property name!");
935 // retrieve the type of the property
936 Property aProp = _rxPropInfo->getPropertyByName(_rPropValue.Name);
937 // the untranslated string value as read in handleAttribute
938 OUString sValue;
939 #if OSL_DEBUG_LEVEL > 0
940 bool bSuccess =
941 #endif
942 _rPropValue.Value >>= sValue;
943 OSL_ENSURE(bSuccess, "OControlImport::implTranslateValueProperty: supposed to be called with non-translated string values!");
945 if (TypeClass_ANY == aProp.Type.getTypeClass())
947 // we have exactly 2 properties where this type class is allowed:
948 OSL_ENSURE(
949 _rPropValue.Name != PROPERTY_EFFECTIVE_VALUE
950 || _rPropValue.Name != PROPERTY_EFFECTIVE_DEFAULT,
951 "OControlImport::implTranslateValueProperty: invalid property type/name combination!");
953 // Both properties are allowed to have a double or a string value,
954 // so first try to convert the string into a number
955 double nValue;
956 if (::sax::Converter::convertDouble(nValue, sValue))
957 _rPropValue.Value <<= nValue;
958 else
959 _rPropValue.Value <<= sValue;
961 else
962 _rPropValue.Value = PropertyConversion::convertString(aProp.Type, sValue);
965 void OControlImport::EndElement()
967 OSL_ENSURE(m_xElement.is(), "OControlImport::EndElement: invalid control!");
968 if ( !m_xElement.is() )
969 return;
971 // register our control with it's id
972 if (!m_sControlId.isEmpty())
973 m_rFormImport.registerControlId(m_xElement, m_sControlId);
974 // it's allowed to have no control id. In this case we're importing a column
976 // one more pre-work to do:
977 // when we set default values, then by definition the respective value is set
978 // to this default value, too. This means if the sequence contains for example
979 // a DefaultText value, then the Text will be affected by this, too.
980 // In case the Text is not part of the property sequence (or occurs _before_
981 // the DefaultText, which can happen for other value/default-value property names),
982 // this means that the Text (the value property) is incorrectly imported.
984 bool bRestoreValuePropertyValue = false;
985 Any aValuePropertyValue;
987 sal_Int16 nClassId = FormComponentType::CONTROL;
990 // get the class id of our element
991 m_xElement->getPropertyValue(PROPERTY_CLASSID) >>= nClassId;
993 catch( const Exception& )
995 OSL_FAIL( "OControlImport::EndElement: caught an exception while retrieving the class id!" );
996 DBG_UNHANDLED_EXCEPTION();
999 const sal_Char* pValueProperty = NULL;
1000 const sal_Char* pDefaultValueProperty = NULL;
1001 getRuntimeValuePropertyNames(m_eElementType, nClassId, pValueProperty, pDefaultValueProperty);
1002 if ( pDefaultValueProperty && pValueProperty )
1004 bool bNonDefaultValuePropertyValue = false;
1005 // is the "value property" part of the sequence?
1007 // look up this property in our sequence
1008 PropertyValueArray::iterator aEnd = m_aValues.end();
1009 for ( PropertyValueArray::iterator aCheck = m_aValues.begin();
1010 ( aCheck != aEnd );
1011 ++aCheck
1014 if ( aCheck->Name.equalsAscii( pDefaultValueProperty ) )
1015 bRestoreValuePropertyValue = true;
1016 else if ( aCheck->Name.equalsAscii( pValueProperty ) )
1018 bNonDefaultValuePropertyValue = true;
1019 // we need to restore the value property we found here, nothing else
1020 aValuePropertyValue = aCheck->Value;
1024 if ( bRestoreValuePropertyValue && !bNonDefaultValuePropertyValue )
1026 // found it -> need to remember (and restore) the "value property value", which is not set explicitly
1029 aValuePropertyValue = m_xElement->getPropertyValue( OUString::createFromAscii( pValueProperty ) );
1031 catch( const Exception& )
1033 OSL_FAIL( "OControlImport::EndElement: caught an exception while retrieving the current value property!" );
1034 DBG_UNHANDLED_EXCEPTION();
1039 // let the base class set all the values
1040 OElementImport::EndElement();
1042 // restore the "value property value", if necessary
1043 if ( bRestoreValuePropertyValue && pValueProperty )
1047 m_xElement->setPropertyValue( OUString::createFromAscii( pValueProperty ), aValuePropertyValue );
1049 catch( const Exception& )
1051 OSL_FAIL( "OControlImport::EndElement: caught an exception while restoring the value property!" );
1052 DBG_UNHANDLED_EXCEPTION();
1056 // the external cell binding, if applicable
1057 if ( m_xElement.is() && !m_sBoundCellAddress.isEmpty() )
1058 doRegisterCellValueBinding( m_sBoundCellAddress );
1060 // XForms binding, if applicable
1061 if ( m_xElement.is() && !m_sBindingID.isEmpty() )
1062 doRegisterXFormsValueBinding( m_sBindingID );
1064 // XForms list binding, if applicable
1065 if ( m_xElement.is() && !m_sListBindingID.isEmpty() )
1066 doRegisterXFormsListBinding( m_sListBindingID );
1068 // XForms submission, if applicable
1069 if ( m_xElement.is() && !m_sSubmissionID.isEmpty() )
1070 doRegisterXFormsSubmission( m_sSubmissionID );
1073 void OControlImport::doRegisterCellValueBinding( const OUString& _rBoundCellAddress )
1075 OSL_PRECOND( m_xElement.is(), "OControlImport::doRegisterCellValueBinding: invalid element!" );
1076 OSL_PRECOND( !_rBoundCellAddress.isEmpty(),
1077 "OControlImport::doRegisterCellValueBinding: invalid address!" );
1079 m_rContext.registerCellValueBinding( m_xElement, _rBoundCellAddress );
1082 void OControlImport::doRegisterXFormsValueBinding( const OUString& _rBindingID )
1084 OSL_PRECOND( m_xElement.is(), "need element" );
1085 OSL_PRECOND( !_rBindingID.isEmpty(), "binding ID is not valid" );
1087 m_rContext.registerXFormsValueBinding( m_xElement, _rBindingID );
1090 void OControlImport::doRegisterXFormsListBinding( const OUString& _rBindingID )
1092 OSL_PRECOND( m_xElement.is(), "need element" );
1093 OSL_PRECOND( !_rBindingID.isEmpty(), "binding ID is not valid" );
1095 m_rContext.registerXFormsListBinding( m_xElement, _rBindingID );
1098 void OControlImport::doRegisterXFormsSubmission( const OUString& _rSubmissionID )
1100 OSL_PRECOND( m_xElement.is(), "need element" );
1101 OSL_PRECOND( !_rSubmissionID.isEmpty(), "binding ID is not valid" );
1103 m_rContext.registerXFormsSubmission( m_xElement, _rSubmissionID );
1106 Reference< XPropertySet > OControlImport::createElement()
1108 const Reference<XPropertySet> xPropSet = OElementImport::createElement();
1109 if ( xPropSet.is() )
1111 m_xInfo = xPropSet->getPropertySetInfo();
1112 if ( m_xInfo.is() && m_xInfo->hasPropertyByName(PROPERTY_ALIGN) )
1114 Any aValue;
1115 xPropSet->setPropertyValue(PROPERTY_ALIGN,aValue);
1118 return xPropSet;
1121 //= OImagePositionImport
1122 OImagePositionImport::OImagePositionImport( OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager,
1123 sal_uInt16 _nPrefix, const OUString& _rName, const Reference< XNameContainer >& _rxParentContainer,
1124 OControlElement::ElementType _eType )
1125 :OControlImport( _rImport, _rEventManager, _nPrefix, _rName, _rxParentContainer, _eType )
1126 ,m_nImagePosition( -1 )
1127 ,m_nImageAlign( 0 )
1128 ,m_bHaveImagePosition( false )
1132 bool OImagePositionImport::handleAttribute( sal_uInt16 _nNamespaceKey, const OUString& _rLocalName,
1133 const OUString& _rValue )
1135 if ( _rLocalName == GetXMLToken( XML_IMAGE_POSITION ) )
1137 OSL_VERIFY( PropertyConversion::convertString(
1138 cppu::UnoType<decltype(m_nImagePosition)>::get(),
1139 _rValue, OEnumMapper::getEnumMap( OEnumMapper::epImagePosition )
1140 ) >>= m_nImagePosition );
1141 m_bHaveImagePosition = true;
1142 return true;
1145 if ( _rLocalName == GetXMLToken( XML_IMAGE_ALIGN ) )
1147 OSL_VERIFY( PropertyConversion::convertString(
1148 cppu::UnoType<decltype(m_nImageAlign)>::get(),
1149 _rValue, OEnumMapper::getEnumMap( OEnumMapper::epImageAlign )
1150 ) >>= m_nImageAlign );
1151 return true;
1154 return OControlImport::handleAttribute( _nNamespaceKey, _rLocalName, _rValue );
1157 void OImagePositionImport::StartElement(const Reference< XAttributeList >& _rxAttrList)
1159 OControlImport::StartElement( _rxAttrList );
1161 if ( m_bHaveImagePosition )
1163 sal_Int16 nUnoImagePosition = ImagePosition::Centered;
1164 if ( m_nImagePosition >= 0 )
1166 OSL_ENSURE( ( m_nImagePosition <= 3 ) && ( m_nImageAlign >= 0 ) && ( m_nImageAlign < 3 ),
1167 "OImagePositionImport::StartElement: unknown image align and/or position!" );
1168 nUnoImagePosition = m_nImagePosition * 3 + m_nImageAlign;
1171 PropertyValue aImagePosition;
1172 aImagePosition.Name = PROPERTY_IMAGE_POSITION;
1173 aImagePosition.Value <<= nUnoImagePosition;
1174 implPushBackPropertyValue( aImagePosition );
1178 //= OReferredControlImport
1179 OReferredControlImport::OReferredControlImport(
1180 OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager, sal_uInt16 _nPrefix, const OUString& _rName,
1181 const Reference< XNameContainer >& _rxParentContainer,
1182 OControlElement::ElementType )
1183 :OControlImport(_rImport, _rEventManager, _nPrefix, _rName, _rxParentContainer)
1187 void OReferredControlImport::StartElement(const Reference< XAttributeList >& _rxAttrList)
1189 OControlImport::StartElement(_rxAttrList);
1191 // the base class should have created the control, so we can register it
1192 if ( !m_sReferringControls.isEmpty() )
1193 m_rFormImport.registerControlReferences(m_xElement, m_sReferringControls);
1196 bool OReferredControlImport::handleAttribute(sal_uInt16 _nNamespaceKey, const OUString& _rLocalName,
1197 const OUString& _rValue)
1199 static const char * s_sReferenceAttributeName = OAttributeMetaData::getCommonControlAttributeName(CCA_FOR);
1200 if (_rLocalName.equalsAscii(s_sReferenceAttributeName))
1202 m_sReferringControls = _rValue;
1203 return true;
1205 return OControlImport::handleAttribute(_nNamespaceKey, _rLocalName, _rValue);
1208 //= OPasswordImport
1209 OPasswordImport::OPasswordImport(OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager, sal_uInt16 _nPrefix, const OUString& _rName,
1210 const Reference< XNameContainer >& _rxParentContainer, OControlElement::ElementType _eType)
1211 :OControlImport(_rImport, _rEventManager, _nPrefix, _rName, _rxParentContainer, _eType)
1215 bool OPasswordImport::handleAttribute(sal_uInt16 _nNamespaceKey, const OUString& _rLocalName, const OUString& _rValue)
1217 static const char * s_sEchoCharAttributeName = OAttributeMetaData::getSpecialAttributeName(SCA_ECHO_CHAR);
1218 if (_rLocalName.equalsAscii(s_sEchoCharAttributeName))
1220 // need a special handling for the EchoChar property
1221 PropertyValue aEchoChar;
1222 aEchoChar.Name = PROPERTY_ECHOCHAR;
1223 OSL_ENSURE(_rValue.getLength() == 1, "OPasswordImport::handleAttribute: invalid echo char attribute!");
1224 // we ourself should not have written values other than of length 1
1225 if (_rValue.getLength() >= 1)
1226 aEchoChar.Value <<= (sal_Int16)_rValue[0];
1227 else
1228 aEchoChar.Value <<= (sal_Int16)0;
1229 implPushBackPropertyValue(aEchoChar);
1230 return true;
1232 return OControlImport::handleAttribute(_nNamespaceKey, _rLocalName, _rValue);
1235 //= ORadioImport
1236 ORadioImport::ORadioImport(OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager, sal_uInt16 _nPrefix, const OUString& _rName,
1237 const Reference< XNameContainer >& _rxParentContainer, OControlElement::ElementType _eType)
1238 :OImagePositionImport( _rImport, _rEventManager, _nPrefix, _rName, _rxParentContainer, _eType )
1242 bool ORadioImport::handleAttribute(sal_uInt16 _nNamespaceKey, const OUString& _rLocalName, const OUString& _rValue)
1244 // need special handling for the State & CurrentState properties:
1245 // they're stored as booleans, but expected to be int16 properties
1246 static const sal_Char* pCurrentSelectedAttributeName = OAttributeMetaData::getCommonControlAttributeName(CCA_CURRENT_SELECTED);
1247 static const sal_Char* pSelectedAttributeName = OAttributeMetaData::getCommonControlAttributeName(CCA_SELECTED);
1248 if ( _rLocalName.equalsAscii( pCurrentSelectedAttributeName )
1249 || _rLocalName.equalsAscii( pSelectedAttributeName )
1252 const OAttribute2Property::AttributeAssignment* pProperty = m_rContext.getAttributeMap().getAttributeTranslation(_rLocalName);
1253 OSL_ENSURE(pProperty, "ORadioImport::handleAttribute: invalid property map!");
1254 if (pProperty)
1256 const Any aBooleanValue( PropertyConversion::convertString(pProperty->aPropertyType, _rValue, pProperty->pEnumMap) );
1258 // create and store a new PropertyValue
1259 PropertyValue aNewValue;
1260 aNewValue.Name = pProperty->sPropertyName;
1261 aNewValue.Value <<= (sal_Int16)::cppu::any2bool(aBooleanValue);
1263 implPushBackPropertyValue(aNewValue);
1265 return true;
1267 return OImagePositionImport::handleAttribute( _nNamespaceKey, _rLocalName, _rValue );
1270 //= OURLReferenceImport
1271 OURLReferenceImport::OURLReferenceImport(OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager, sal_uInt16 _nPrefix, const OUString& _rName,
1272 const Reference< XNameContainer >& _rxParentContainer,
1273 OControlElement::ElementType _eType)
1274 :OImagePositionImport(_rImport, _rEventManager, _nPrefix, _rName, _rxParentContainer, _eType)
1278 bool OURLReferenceImport::handleAttribute(sal_uInt16 _nNamespaceKey, const OUString& _rLocalName, const OUString& _rValue)
1280 static const sal_Char* s_pTargetLocationAttributeName = OAttributeMetaData::getCommonControlAttributeName( CCA_TARGET_LOCATION );
1281 static const sal_Char* s_pImageDataAttributeName = OAttributeMetaData::getCommonControlAttributeName( CCA_IMAGE_DATA );
1283 // need to make the URL absolute if
1284 // * it's the image-data attribute
1285 // * it's the target-location attribute, and we're dealign with an object which has the respective property
1286 bool bMakeAbsolute =
1287 _rLocalName.equalsAscii( s_pImageDataAttributeName )
1288 || ( _rLocalName.equalsAscii( s_pTargetLocationAttributeName )
1289 && ( ( OControlElement::BUTTON == m_eElementType )
1290 || ( OControlElement::IMAGE == m_eElementType )
1294 if ( bMakeAbsolute && !_rValue.isEmpty() )
1296 // make a global URL out of the local one
1297 OUString sAdjustedValue;
1298 // only resolve image related url
1299 // we don't want say form url targets to be resolved
1300 // using ResolveGraphicObjectURL
1301 if ( _rLocalName.equalsAscii( s_pImageDataAttributeName ) )
1302 sAdjustedValue = m_rContext.getGlobalContext().ResolveGraphicObjectURL( _rValue, false );
1303 else
1304 sAdjustedValue = m_rContext.getGlobalContext().GetAbsoluteReference( _rValue );
1305 return OImagePositionImport::handleAttribute( _nNamespaceKey, _rLocalName, sAdjustedValue );
1308 return OImagePositionImport::handleAttribute( _nNamespaceKey, _rLocalName, _rValue );
1311 //= OButtonImport
1312 OButtonImport::OButtonImport(OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager, sal_uInt16 _nPrefix, const OUString& _rName,
1313 const Reference< XNameContainer >& _rxParentContainer,
1314 OControlElement::ElementType _eType)
1315 :OURLReferenceImport(_rImport, _rEventManager, _nPrefix, _rName, _rxParentContainer, _eType)
1317 enableTrackAttributes();
1320 void OButtonImport::StartElement(const Reference< XAttributeList >& _rxAttrList)
1322 OURLReferenceImport::StartElement(_rxAttrList);
1324 // handle the target-frame attribute
1325 simulateDefaultedAttribute(OAttributeMetaData::getCommonControlAttributeName(CCA_TARGET_FRAME), PROPERTY_TARGETFRAME, "_blank");
1328 //= OValueRangeImport
1329 OValueRangeImport::OValueRangeImport( OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager, sal_uInt16 _nPrefix, const OUString& _rName,
1330 const Reference< XNameContainer >& _rxParentContainer, OControlElement::ElementType _eType )
1331 :OControlImport( _rImport, _rEventManager, _nPrefix, _rName, _rxParentContainer, _eType )
1332 ,m_nStepSizeValue( 1 )
1337 bool OValueRangeImport::handleAttribute( sal_uInt16 _nNamespaceKey, const OUString& _rLocalName, const OUString& _rValue )
1339 if ( _rLocalName.equalsAscii( OAttributeMetaData::getSpecialAttributeName( SCA_STEP_SIZE ) ) )
1341 ::sax::Converter::convertNumber( m_nStepSizeValue, _rValue );
1342 return true;
1344 return OControlImport::handleAttribute( _nNamespaceKey, _rLocalName, _rValue );
1347 void OValueRangeImport::StartElement( const Reference< XAttributeList >& _rxAttrList )
1349 OControlImport::StartElement( _rxAttrList );
1351 if ( m_xInfo.is() )
1353 if ( m_xInfo->hasPropertyByName( PROPERTY_SPIN_INCREMENT ) )
1354 m_xElement->setPropertyValue( PROPERTY_SPIN_INCREMENT, makeAny( m_nStepSizeValue ) );
1355 else if ( m_xInfo->hasPropertyByName( PROPERTY_LINE_INCREMENT ) )
1356 m_xElement->setPropertyValue( PROPERTY_LINE_INCREMENT, makeAny( m_nStepSizeValue ) );
1360 //= OTextLikeImport
1361 OTextLikeImport::OTextLikeImport(OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager, sal_uInt16 _nPrefix, const OUString& _rName,
1362 const Reference< XNameContainer >& _rxParentContainer,
1363 OControlElement::ElementType _eType)
1364 :OControlImport(_rImport, _rEventManager, _nPrefix, _rName, _rxParentContainer, _eType)
1365 ,m_bEncounteredTextPara( false )
1367 enableTrackAttributes();
1370 SvXMLImportContext* OTextLikeImport::CreateChildContext( sal_uInt16 _nPrefix, const OUString& _rLocalName,
1371 const Reference< XAttributeList >& _rxAttrList )
1373 if ( ( XML_NAMESPACE_TEXT == _nPrefix ) && _rLocalName.equalsIgnoreAsciiCase("p") )
1375 OSL_ENSURE( m_eElementType == OControlElement::TEXT_AREA,
1376 "OTextLikeImport::CreateChildContext: text paragraphs in a non-text-area?" );
1378 if ( m_eElementType == OControlElement::TEXT_AREA )
1380 Reference< XText > xTextElement( m_xElement, UNO_QUERY );
1381 if ( xTextElement.is() )
1383 rtl::Reference < XMLTextImportHelper > xTextImportHelper( m_rContext.getGlobalContext().GetTextImport() );
1385 if ( !m_xCursor.is() )
1387 m_xOldCursor = xTextImportHelper->GetCursor();
1388 m_xCursor = xTextElement->createTextCursor();
1390 if ( m_xCursor.is() )
1391 xTextImportHelper->SetCursor( m_xCursor );
1393 if ( m_xCursor.is() )
1395 m_bEncounteredTextPara = true;
1396 return xTextImportHelper->CreateTextChildContext( m_rContext.getGlobalContext(), _nPrefix, _rLocalName, _rxAttrList );
1399 else
1401 // in theory, we could accumulate all the text portions (without formatting),
1402 // and set it as Text property at the model ...
1407 return OControlImport::CreateChildContext( _nPrefix, _rLocalName, _rxAttrList );
1410 void OTextLikeImport::StartElement(const Reference< XAttributeList >& _rxAttrList)
1412 OControlImport::StartElement(_rxAttrList);
1414 // handle the convert-empty-to-null attribute, whose default is different from the property default
1415 // unfortunately, different classes are imported by this class ('cause they're represented by the
1416 // same XML element), though not all of them know this property.
1417 // So we have to do a check ...
1418 if (m_xElement.is() && m_xInfo.is() && m_xInfo->hasPropertyByName(PROPERTY_EMPTY_IS_NULL) )
1419 simulateDefaultedAttribute(OAttributeMetaData::getDatabaseAttributeName(DA_CONVERT_EMPTY), PROPERTY_EMPTY_IS_NULL, "false");
1422 struct EqualHandle : public ::std::unary_function< PropertyValue, bool >
1424 const sal_Int32 m_nHandle;
1425 EqualHandle( sal_Int32 _nHandle ) : m_nHandle( _nHandle ) { }
1427 inline bool operator()( const PropertyValue& _rProp )
1429 return _rProp.Handle == m_nHandle;
1433 void OTextLikeImport::removeRedundantCurrentValue()
1435 if ( m_bEncounteredTextPara )
1437 // In case the text is written in the text:p elements, we need to ignore what we read as
1438 // current-value attribute, since it's redundant.
1439 // fortunately, OElementImport tagged the value property with the PROPID_CURRENT_VALUE
1440 // handle, so we do not need to determine the name of our value property here
1441 // (normally, it should be "Text", since no other controls than the edit field should
1442 // have the text:p elements)
1443 PropertyValueArray::iterator aValuePropertyPos = ::std::find_if(
1444 m_aValues.begin(),
1445 m_aValues.end(),
1446 EqualHandle( PROPID_CURRENT_VALUE )
1448 if ( aValuePropertyPos != m_aValues.end() )
1450 OSL_ENSURE( aValuePropertyPos->Name == PROPERTY_TEXT, "OTextLikeImport::EndElement: text:p was present, but our value property is *not* 'Text'!" );
1451 if ( aValuePropertyPos->Name == PROPERTY_TEXT )
1453 ::std::copy(
1454 aValuePropertyPos + 1,
1455 m_aValues.end(),
1456 aValuePropertyPos
1458 m_aValues.resize( m_aValues.size() - 1 );
1462 // additionally, we need to set the "RichText" property of our element to sal_True
1463 // (the presence of the text:p is used as indicator for the value of the RichText property)
1464 bool bHasRichTextProperty = false;
1465 if ( m_xInfo.is() )
1466 bHasRichTextProperty = m_xInfo->hasPropertyByName( PROPERTY_RICH_TEXT );
1467 OSL_ENSURE( bHasRichTextProperty, "OTextLikeImport::EndElement: text:p, but no rich text control?" );
1468 if ( bHasRichTextProperty )
1469 m_xElement->setPropertyValue( PROPERTY_RICH_TEXT, makeAny( true ) );
1471 // Note that we do *not* set the RichText property (in case our element has one) to sal_False here
1472 // since this is the default of this property, anyway.
1475 struct EqualName : public ::std::unary_function< PropertyValue, bool >
1477 const OUString m_sName;
1478 EqualName( const OUString& _rName ) : m_sName( _rName ) { }
1480 inline bool operator()( const PropertyValue& _rProp )
1482 return _rProp.Name == m_sName;
1486 void OTextLikeImport::adjustDefaultControlProperty()
1488 // In OpenOffice.org 2.0, we changed the implementation of the css.form.component.TextField (the model of a text field control),
1489 // so that it now uses another default control. So if we encounter a text field where the *old* default
1490 // control property is writing, we are not allowed to use it
1491 PropertyValueArray::iterator aDefaultControlPropertyPos = ::std::find_if(
1492 m_aValues.begin(),
1493 m_aValues.end(),
1494 EqualName( OUString( "DefaultControl" ) )
1496 if ( aDefaultControlPropertyPos != m_aValues.end() )
1498 OUString sDefaultControl;
1499 OSL_VERIFY( aDefaultControlPropertyPos->Value >>= sDefaultControl );
1500 if ( sDefaultControl == "stardiv.one.form.control.Edit" )
1502 // complete remove this property value from the array. Today's "default value" of the "DefaultControl"
1503 // property is sufficient
1504 ::std::copy(
1505 aDefaultControlPropertyPos + 1,
1506 m_aValues.end(),
1507 aDefaultControlPropertyPos
1509 m_aValues.resize( m_aValues.size() - 1 );
1514 void OTextLikeImport::EndElement()
1516 removeRedundantCurrentValue();
1517 adjustDefaultControlProperty();
1519 // let the base class do the stuff
1520 OControlImport::EndElement();
1522 // some cleanups
1523 rtl::Reference < XMLTextImportHelper > xTextImportHelper( m_rContext.getGlobalContext().GetTextImport() );
1524 if ( m_xCursor.is() )
1526 // delete the newline which has been imported errornously
1527 // TODO (fs): stole this code somewhere - why don't we fix the text import??
1528 m_xCursor->gotoEnd( sal_False );
1529 m_xCursor->goLeft( 1, sal_True );
1530 m_xCursor->setString( OUString() );
1532 // reset cursor
1533 xTextImportHelper->ResetCursor();
1536 if ( m_xOldCursor.is() )
1537 xTextImportHelper->SetCursor( m_xOldCursor );
1541 //= OListAndComboImport
1542 OListAndComboImport::OListAndComboImport(OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager, sal_uInt16 _nPrefix, const OUString& _rName,
1543 const Reference< XNameContainer >& _rxParentContainer,
1544 OControlElement::ElementType _eType)
1545 :OControlImport(_rImport, _rEventManager, _nPrefix, _rName, _rxParentContainer, _eType)
1546 ,m_nEmptyListItems( 0 )
1547 ,m_nEmptyValueItems( 0 )
1548 ,m_bEncounteredLSAttrib( false )
1549 ,m_bLinkWithIndexes( false )
1551 if (OControlElement::COMBOBOX == m_eElementType)
1552 enableTrackAttributes();
1555 SvXMLImportContext* OListAndComboImport::CreateChildContext(sal_uInt16 _nPrefix, const OUString& _rLocalName,
1556 const Reference< XAttributeList >& _rxAttrList)
1558 // is it the "option" sub tag of a listbox ?
1559 static const char s_sOptionElementName[] = "option";
1560 if (s_sOptionElementName == _rLocalName)
1561 return new OListOptionImport(GetImport(), _nPrefix, _rLocalName, this);
1563 // is it the "item" sub tag of a combobox ?
1564 static const char s_sItemElementName[] = "item";
1565 if (s_sItemElementName == _rLocalName)
1566 return new OComboItemImport(GetImport(), _nPrefix, _rLocalName, this);
1568 // everything else
1569 return OControlImport::CreateChildContext(_nPrefix, _rLocalName, _rxAttrList);
1572 void OListAndComboImport::StartElement(const Reference< XAttributeList >& _rxAttrList)
1574 m_bLinkWithIndexes = false;
1576 OControlImport::StartElement(_rxAttrList);
1578 if (OControlElement::COMBOBOX == m_eElementType)
1580 // for the auto-completion
1581 // the attribute default does not equal the property default, so in case we did not read this attribute,
1582 // we have to simulate it
1583 simulateDefaultedAttribute( OAttributeMetaData::getSpecialAttributeName( SCA_AUTOMATIC_COMPLETION ), PROPERTY_AUTOCOMPLETE, "false");
1585 // same for the convert-empty-to-null attribute, which's default is different from the property default
1586 simulateDefaultedAttribute( OAttributeMetaData::getDatabaseAttributeName( DA_CONVERT_EMPTY ), PROPERTY_EMPTY_IS_NULL, "false");
1590 void OListAndComboImport::EndElement()
1592 // append the list source property the properties sequence of our importer
1593 // the string item list
1594 PropertyValue aItemList;
1595 aItemList.Name = PROPERTY_STRING_ITEM_LIST;
1596 aItemList.Value <<= m_aListSource;
1597 implPushBackPropertyValue(aItemList);
1599 if (OControlElement::LISTBOX == m_eElementType)
1601 OSL_ENSURE((m_aListSource.getLength() + m_nEmptyListItems) == (m_aValueList.getLength() + m_nEmptyValueItems),
1602 "OListAndComboImport::EndElement: inconsistence between labels and values!");
1604 if ( !m_bEncounteredLSAttrib )
1606 // the value sequence
1607 PropertyValue aValueList;
1608 aValueList.Name = PROPERTY_LISTSOURCE;
1609 aValueList.Value <<= m_aValueList;
1610 implPushBackPropertyValue(aValueList);
1613 // the select sequence
1614 PropertyValue aSelected;
1615 aSelected.Name = PROPERTY_SELECT_SEQ;
1616 aSelected.Value <<= m_aSelectedSeq;
1617 implPushBackPropertyValue(aSelected);
1619 // the default select sequence
1620 PropertyValue aDefaultSelected;
1621 aDefaultSelected.Name = PROPERTY_DEFAULT_SELECT_SEQ;
1622 aDefaultSelected.Value <<= m_aDefaultSelectedSeq;
1623 implPushBackPropertyValue(aDefaultSelected);
1626 OControlImport::EndElement();
1628 // the external list source, if applicable
1629 if ( m_xElement.is() && !m_sCellListSource.isEmpty() )
1630 m_rContext.registerCellRangeListSource( m_xElement, m_sCellListSource );
1633 void OListAndComboImport::doRegisterCellValueBinding( const OUString& _rBoundCellAddress )
1635 OUString sBoundCellAddress( _rBoundCellAddress );
1636 if ( m_bLinkWithIndexes )
1638 // This is a HACK. We register a string which is no valid address, but allows
1639 // (somewhere else) to determine that a non-standard binding should be created.
1640 // This hack is acceptable for OOo 1.1.1, since the file format for value
1641 // bindings of form controls is to be changed afterwards, anyway.
1642 sBoundCellAddress += OUString( ":index" );
1645 OControlImport::doRegisterCellValueBinding( sBoundCellAddress );
1648 bool OListAndComboImport::handleAttribute(sal_uInt16 _nNamespaceKey, const OUString& _rLocalName, const OUString& _rValue)
1650 static const sal_Char* pListSourceAttributeName = OAttributeMetaData::getDatabaseAttributeName(DA_LIST_SOURCE);
1651 if ( _rLocalName.equalsAscii(pListSourceAttributeName) )
1653 PropertyValue aListSource;
1654 aListSource.Name = PROPERTY_LISTSOURCE;
1656 // it's the ListSource attribute
1657 m_bEncounteredLSAttrib = true;
1658 if ( OControlElement::COMBOBOX == m_eElementType )
1660 aListSource.Value <<= _rValue;
1662 else
1664 // a listbox which has a list-source attribute must have a list-source-type of something
1665 // not equal to ValueList.
1666 // In this case, the list-source value is simply the one and only element of the ListSource property.
1667 Sequence< OUString > aListSourcePropValue( 1 );
1668 aListSourcePropValue[0] = _rValue;
1669 aListSource.Value <<= aListSourcePropValue;
1672 implPushBackPropertyValue( aListSource );
1673 return true;
1676 if ( _rLocalName.equalsAscii( OAttributeMetaData::getBindingAttributeName( BA_LIST_CELL_RANGE ) ) )
1678 m_sCellListSource = _rValue;
1679 return true;
1682 if ( _rLocalName.equalsAscii( OAttributeMetaData::getBindingAttributeName( BA_LIST_LINKING_TYPE ) ) )
1684 sal_Int16 nLinkageType = 0;
1685 PropertyConversion::convertString(
1686 ::cppu::UnoType<sal_Int16>::get(),
1687 _rValue,
1688 OEnumMapper::getEnumMap( OEnumMapper::epListLinkageType )
1689 ) >>= nLinkageType;
1691 m_bLinkWithIndexes = ( nLinkageType != 0 );
1692 return true;
1695 return OControlImport::handleAttribute(_nNamespaceKey, _rLocalName, _rValue);
1698 void OListAndComboImport::implPushBackLabel(const OUString& _rLabel)
1700 OSL_ENSURE(!m_nEmptyListItems, "OListAndComboImport::implPushBackValue: label list is already done!");
1701 if (!m_nEmptyListItems)
1702 pushBackSequenceElement(m_aListSource, _rLabel);
1705 void OListAndComboImport::implPushBackValue(const OUString& _rValue)
1707 OSL_ENSURE(!m_nEmptyValueItems, "OListAndComboImport::implPushBackValue: value list is already done!");
1708 if (!m_nEmptyValueItems)
1710 OSL_ENSURE( !m_bEncounteredLSAttrib, "OListAndComboImport::implPushBackValue: invalid structure! Did you save this document with a version prior SRC641 m?" );
1711 // We already had the list-source attribute, which means that the ListSourceType is
1712 // not ValueList, which means that the ListSource should contain only one string in
1713 // the first element of the sequence
1714 // All other values in the file are invalid
1716 pushBackSequenceElement( m_aValueList, _rValue );
1720 void OListAndComboImport::implEmptyLabelFound()
1722 ++m_nEmptyListItems;
1725 void OListAndComboImport::implEmptyValueFound()
1727 ++m_nEmptyValueItems;
1730 void OListAndComboImport::implSelectCurrentItem()
1732 OSL_ENSURE((m_aListSource.getLength() + m_nEmptyListItems) == (m_aValueList.getLength() + m_nEmptyValueItems),
1733 "OListAndComboImport::implSelectCurrentItem: inconsistence between labels and values!");
1735 sal_Int16 nItemNumber = (sal_Int16)(m_aListSource.getLength() - 1 + m_nEmptyListItems);
1736 pushBackSequenceElement(m_aSelectedSeq, nItemNumber);
1739 void OListAndComboImport::implDefaultSelectCurrentItem()
1741 OSL_ENSURE((m_aListSource.getLength() + m_nEmptyListItems) == (m_aValueList.getLength() + m_nEmptyValueItems),
1742 "OListAndComboImport::implDefaultSelectCurrentItem: inconsistence between labels and values!");
1744 sal_Int16 nItemNumber = (sal_Int16)(m_aListSource.getLength() - 1 + m_nEmptyListItems);
1745 pushBackSequenceElement(m_aDefaultSelectedSeq, nItemNumber);
1748 //= OListOptionImport
1749 OListOptionImport::OListOptionImport(SvXMLImport& _rImport, sal_uInt16 _nPrefix, const OUString& _rName,
1750 const OListAndComboImportRef& _rListBox)
1751 :SvXMLImportContext(_rImport, _nPrefix, _rName)
1752 ,m_xListBoxImport(_rListBox)
1756 void OListOptionImport::StartElement(const Reference< XAttributeList >& _rxAttrList)
1758 // the label and the value
1759 const SvXMLNamespaceMap& rMap = GetImport().GetNamespaceMap();
1760 const OUString sLabelAttribute = rMap.GetQNameByKey(
1761 GetPrefix(), OUString("label"));
1762 const OUString sValueAttribute = rMap.GetQNameByKey(
1763 GetPrefix(), OUString("value"));
1765 // the label attribute
1766 OUString sValue = _rxAttrList->getValueByName(sLabelAttribute);
1767 bool bNonexistentAttribute = false;
1768 if (sValue.isEmpty())
1769 if (_rxAttrList->getTypeByName(sLabelAttribute).isEmpty())
1770 // this attribute does not really exist
1771 bNonexistentAttribute = true;
1773 if (bNonexistentAttribute)
1774 m_xListBoxImport->implEmptyLabelFound();
1775 else
1776 m_xListBoxImport->implPushBackLabel( sValue );
1778 // the value attribute
1779 sValue = _rxAttrList->getValueByName(sValueAttribute);
1780 bNonexistentAttribute = false;
1781 if (sValue.isEmpty())
1782 if (_rxAttrList->getTypeByName(sValueAttribute).isEmpty())
1783 // this attribute does not really exist
1784 bNonexistentAttribute = true;
1786 if (bNonexistentAttribute)
1787 m_xListBoxImport->implEmptyValueFound();
1788 else
1789 m_xListBoxImport->implPushBackValue( sValue );
1791 // the current-selected and selected
1792 const OUString sSelectedAttribute = rMap.GetQNameByKey(
1793 GetPrefix(), OUString::createFromAscii(OAttributeMetaData::getCommonControlAttributeName(CCA_CURRENT_SELECTED)));
1794 const OUString sDefaultSelectedAttribute = rMap.GetQNameByKey(
1795 GetPrefix(), OUString::createFromAscii(OAttributeMetaData::getCommonControlAttributeName(CCA_SELECTED)));
1797 // propagate the selected flag
1798 bool bSelected(false);
1799 ::sax::Converter::convertBool(bSelected,
1800 _rxAttrList->getValueByName(sSelectedAttribute));
1801 if (bSelected)
1802 m_xListBoxImport->implSelectCurrentItem();
1804 // same for the default selected
1805 bool bDefaultSelected(false);
1806 ::sax::Converter::convertBool(bDefaultSelected,
1807 _rxAttrList->getValueByName(sDefaultSelectedAttribute));
1808 if (bDefaultSelected)
1809 m_xListBoxImport->implDefaultSelectCurrentItem();
1811 SvXMLImportContext::StartElement(_rxAttrList);
1814 //= OComboItemImport
1815 OComboItemImport::OComboItemImport(SvXMLImport& _rImport, sal_uInt16 _nPrefix, const OUString& _rName,
1816 const OListAndComboImportRef& _rListBox)
1817 :SvXMLImportContext(_rImport, _nPrefix, _rName)
1818 ,m_xListBoxImport(_rListBox)
1822 void OComboItemImport::StartElement(const Reference< XAttributeList >& _rxAttrList)
1824 const OUString sLabelAttributeName = GetImport().GetNamespaceMap().GetQNameByKey(
1825 GetPrefix(), OUString::createFromAscii(OAttributeMetaData::getCommonControlAttributeName(CCA_LABEL)));
1826 m_xListBoxImport->implPushBackLabel(_rxAttrList->getValueByName(sLabelAttributeName));
1828 SvXMLImportContext::StartElement(_rxAttrList);
1831 //= OColumnWrapperImport
1832 OColumnWrapperImport::OColumnWrapperImport(OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager, sal_uInt16 _nPrefix, const OUString& _rName,
1833 const Reference< XNameContainer >& _rxParentContainer)
1834 :SvXMLImportContext(_rImport.getGlobalContext(), _nPrefix, _rName)
1835 ,m_xParentContainer(_rxParentContainer)
1836 ,m_rFormImport(_rImport)
1837 ,m_rEventManager(_rEventManager)
1840 SvXMLImportContext* OColumnWrapperImport::CreateChildContext(sal_uInt16 _nPrefix, const OUString& _rLocalName,
1841 const Reference< XAttributeList >&)
1843 OControlImport* pReturn = implCreateChildContext(_nPrefix, _rLocalName, OElementNameMap::getElementType(_rLocalName));
1844 if (pReturn)
1846 OSL_ENSURE(m_xOwnAttributes.is(), "OColumnWrapperImport::CreateChildContext: had no form:column element!");
1847 pReturn->addOuterAttributes(m_xOwnAttributes);
1849 return pReturn;
1851 void OColumnWrapperImport::StartElement(const Reference< XAttributeList >& _rxAttrList)
1853 OSL_ENSURE(!m_xOwnAttributes.is(), "OColumnWrapperImport::StartElement: already have the cloned list!");
1855 // clone the attributes
1856 Reference< XCloneable > xCloneList(_rxAttrList, UNO_QUERY);
1857 OSL_ENSURE(xCloneList.is(), "OColumnWrapperImport::StartElement: AttributeList not cloneable!");
1858 if ( xCloneList.is() )
1859 m_xOwnAttributes = Reference< XAttributeList >(xCloneList->createClone(), UNO_QUERY);
1860 OSL_ENSURE(m_xOwnAttributes.is(), "OColumnWrapperImport::StartElement: no cloned list!");
1863 OControlImport* OColumnWrapperImport::implCreateChildContext(
1864 sal_uInt16 _nPrefix, const OUString& _rLocalName,
1865 OControlElement::ElementType _eType)
1867 OSL_ENSURE( (OControlElement::TEXT == _eType)
1868 || (OControlElement::TEXT_AREA == _eType)
1869 || (OControlElement::FORMATTED_TEXT == _eType)
1870 || (OControlElement::CHECKBOX == _eType)
1871 || (OControlElement::LISTBOX == _eType)
1872 || (OControlElement::COMBOBOX == _eType)
1873 || (OControlElement::TIME == _eType)
1874 || (OControlElement::DATE == _eType),
1875 "OColumnWrapperImport::implCreateChildContext: invalid or unrecognized sub element!");
1877 switch (_eType)
1879 case OControlElement::COMBOBOX:
1880 case OControlElement::LISTBOX:
1881 return new OColumnImport<OListAndComboImport>(m_rFormImport, m_rEventManager, _nPrefix, _rLocalName, m_xParentContainer, _eType );
1883 case OControlElement::PASSWORD:
1884 return new OColumnImport<OPasswordImport>(m_rFormImport, m_rEventManager, _nPrefix, _rLocalName, m_xParentContainer, _eType );
1886 case OControlElement::TEXT:
1887 case OControlElement::TEXT_AREA:
1888 case OControlElement::FORMATTED_TEXT:
1889 return new OColumnImport< OTextLikeImport >( m_rFormImport, m_rEventManager, _nPrefix, _rLocalName, m_xParentContainer, _eType );
1891 default:
1892 return new OColumnImport<OControlImport>(m_rFormImport, m_rEventManager, _nPrefix, _rLocalName, m_xParentContainer, _eType );
1896 //= OGridImport
1897 OGridImport::OGridImport(OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager, sal_uInt16 _nPrefix, const OUString& _rName,
1898 const Reference< XNameContainer >& _rxParentContainer,
1899 OControlElement::ElementType _eType)
1900 :OGridImport_Base(_rImport, _rEventManager, _nPrefix, _rName, _rxParentContainer, "column")
1902 setElementType(_eType);
1905 SvXMLImportContext* OGridImport::implCreateControlWrapper(sal_uInt16 _nPrefix, const OUString& _rLocalName)
1907 return new OColumnWrapperImport(m_rFormImport, *this, _nPrefix, _rLocalName, m_xMeAsContainer);
1910 //= OFormImport
1911 OFormImport::OFormImport(OFormLayerXMLImport_Impl& _rImport, IEventAttacherManager& _rEventManager, sal_uInt16 _nPrefix, const OUString& _rName,
1912 const Reference< XNameContainer >& _rxParentContainer)
1913 :OFormImport_Base(_rImport, _rEventManager, _nPrefix, _rName, _rxParentContainer, "control")
1915 enableTrackAttributes();
1918 SvXMLImportContext* OFormImport::CreateChildContext(sal_uInt16 _nPrefix, const OUString& _rLocalName,
1919 const Reference< XAttributeList >& _rxAttrList)
1921 if( token::IsXMLToken(_rLocalName, token::XML_FORM) )
1922 return new OFormImport( m_rFormImport, *this, _nPrefix, _rLocalName,
1923 m_xMeAsContainer);
1924 else if ( token::IsXMLToken(_rLocalName, token::XML_CONNECTION_RESOURCE) )
1925 return new OXMLDataSourceImport(GetImport(), _nPrefix, _rLocalName, _rxAttrList,m_xElement);
1926 else if( (token::IsXMLToken(_rLocalName, token::XML_EVENT_LISTENERS) &&
1927 (XML_NAMESPACE_OFFICE == _nPrefix)) ||
1928 token::IsXMLToken( _rLocalName, token::XML_PROPERTIES) )
1929 return OElementImport::CreateChildContext( _nPrefix, _rLocalName,
1930 _rxAttrList );
1931 else
1932 return implCreateChildContext( _nPrefix, _rLocalName,
1933 OElementNameMap::getElementType(_rLocalName) );
1936 void OFormImport::StartElement(const Reference< XAttributeList >& _rxAttrList)
1938 m_rFormImport.enterEventContext();
1939 OFormImport_Base::StartElement(_rxAttrList);
1941 // handle the target-frame attribute
1942 simulateDefaultedAttribute(OAttributeMetaData::getCommonControlAttributeName(CCA_TARGET_FRAME), PROPERTY_TARGETFRAME, "_blank");
1945 void OFormImport::EndElement()
1947 OFormImport_Base::EndElement();
1948 m_rFormImport.leaveEventContext();
1951 SvXMLImportContext* OFormImport::implCreateControlWrapper(sal_uInt16 _nPrefix, const OUString& _rLocalName)
1953 OSL_ENSURE( false, "illegal call to OFormImport::implCreateControlWrapper" );
1954 return new SvXMLImportContext(GetImport(), _nPrefix, _rLocalName );
1957 bool OFormImport::handleAttribute(sal_uInt16 _nNamespaceKey, const OUString& _rLocalName, const OUString& _rValue)
1959 // handle the master/details field attributes (they're way too special to let the OPropertyImport handle them)
1960 static const char* s_sMasterFieldsAttributeName = OAttributeMetaData::getFormAttributeName(faMasterFields);
1961 static const char* s_sDetailFieldsAttributeName = OAttributeMetaData::getFormAttributeName(faDetailFiels);
1963 if ( _rLocalName.equalsAscii(s_sMasterFieldsAttributeName) )
1965 implTranslateStringListProperty(PROPERTY_MASTERFIELDS, _rValue);
1966 return true;
1969 if ( _rLocalName.equalsAscii(s_sDetailFieldsAttributeName) )
1971 implTranslateStringListProperty(PROPERTY_DETAILFIELDS, _rValue);
1972 return true;
1975 return OFormImport_Base::handleAttribute(_nNamespaceKey, _rLocalName, _rValue);
1978 void OFormImport::implTranslateStringListProperty(const OUString& _rPropertyName, const OUString& _rValue)
1980 PropertyValue aProp;
1981 aProp.Name = _rPropertyName;
1983 Sequence< OUString > aList;
1985 // split up the value string
1986 if (!_rValue.isEmpty())
1988 // For the moment, we build a vector instead of a Sequence. It's easier to handle because of it's
1989 // push_back method
1990 ::std::vector< OUString > aElements;
1991 // estimate the number of tokens
1992 sal_Int32 nEstimate = 0, nLength = _rValue.getLength();
1993 const sal_Unicode* pChars = _rValue.getStr();
1994 for (sal_Int32 i=0; i<nLength; ++i, ++pChars)
1995 if (*pChars == ',')
1996 ++nEstimate;
1997 aElements.reserve(nEstimate + 1);
1998 // that's the worst case. If the string contains the separator character _quoted_, we reserved to much ...
2000 sal_Int32 nElementStart = 0;
2001 sal_Int32 nNextSep = 0;
2002 sal_Int32 nElementLength;
2003 OUString sElement;
2006 // extract the current element
2007 nNextSep = ::sax::Converter::indexOfComma(
2008 _rValue, nElementStart);
2009 if (-1 == nNextSep)
2010 nNextSep = nLength;
2011 sElement = _rValue.copy(nElementStart, nNextSep - nElementStart);
2013 nElementLength = sElement.getLength();
2014 // when writing the sequence, we quoted the single elements with " characters
2015 OSL_ENSURE( sElement.startsWith("\"") && sElement.endsWith("\""),
2016 "OFormImport::implTranslateStringListProperty: invalid quoted element name.");
2017 sElement = sElement.copy(1, nElementLength - 2);
2019 aElements.push_back(sElement);
2021 // swith to the next element
2022 nElementStart = 1 + nNextSep;
2024 while (nElementStart < nLength);
2026 aList = Sequence< OUString >(aElements.data(), aElements.size());
2028 else
2030 OSL_FAIL("OFormImport::implTranslateStringListProperty: invalid value (empty)!");
2033 aProp.Value <<= aList;
2035 // add the property to the base class' array
2036 implPushBackPropertyValue(aProp);
2038 //= OXMLDataSourceImport
2039 OXMLDataSourceImport::OXMLDataSourceImport(
2040 SvXMLImport& _rImport
2041 ,sal_uInt16 nPrfx
2042 , const OUString& _sLocalName
2043 ,const Reference< ::com::sun::star::xml::sax::XAttributeList > & _xAttrList
2044 ,const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _xElement) :
2045 SvXMLImportContext( _rImport, nPrfx, _sLocalName )
2047 OSL_ENSURE(_xAttrList.is(),"Attribute list is NULL!");
2048 const SvXMLNamespaceMap& rMap = _rImport.GetNamespaceMap();
2050 sal_Int16 nLength = (_xElement.is() && _xAttrList.is()) ? _xAttrList->getLength() : 0;
2051 for(sal_Int16 i = 0; i < nLength; ++i)
2053 OUString sLocalName;
2054 OUString sAttrName = _xAttrList->getNameByIndex( i );
2055 sal_uInt16 nPrefix = rMap.GetKeyByAttrName( sAttrName, &sLocalName );
2057 if ( ( nPrefix == OAttributeMetaData::getCommonControlAttributeNamespace( CCA_TARGET_LOCATION ) )
2058 && ( sLocalName.equalsAscii( OAttributeMetaData::getCommonControlAttributeName( CCA_TARGET_LOCATION ) ) )
2061 OUString sValue = _xAttrList->getValueByIndex( i );
2063 INetURLObject aURL(sValue);
2064 if ( aURL.GetProtocol() == INetProtocol::File )
2065 _xElement->setPropertyValue(PROPERTY_DATASOURCENAME,makeAny(sValue));
2066 else
2067 _xElement->setPropertyValue(PROPERTY_URL,makeAny(sValue)); // the url is the "sdbc:" string
2068 break;
2072 OControlImport* OFormImport::implCreateChildContext(
2073 sal_uInt16 _nPrefix, const OUString& _rLocalName,
2074 OControlElement::ElementType _eType )
2076 switch (_eType)
2078 case OControlElement::TEXT:
2079 case OControlElement::TEXT_AREA:
2080 case OControlElement::FORMATTED_TEXT:
2081 return new OTextLikeImport(m_rFormImport, *this, _nPrefix, _rLocalName, m_xMeAsContainer, _eType);
2083 case OControlElement::BUTTON:
2084 case OControlElement::IMAGE:
2085 case OControlElement::IMAGE_FRAME:
2086 return new OButtonImport( m_rFormImport, *this, _nPrefix, _rLocalName, m_xMeAsContainer, _eType );
2088 case OControlElement::COMBOBOX:
2089 case OControlElement::LISTBOX:
2090 return new OListAndComboImport(m_rFormImport, *this, _nPrefix, _rLocalName, m_xMeAsContainer, _eType);
2092 case OControlElement::RADIO:
2093 return new ORadioImport(m_rFormImport, *this, _nPrefix, _rLocalName, m_xMeAsContainer, _eType);
2095 case OControlElement::CHECKBOX:
2096 return new OImagePositionImport(m_rFormImport, *this, _nPrefix, _rLocalName, m_xMeAsContainer, _eType);
2098 case OControlElement::PASSWORD:
2099 return new OPasswordImport(m_rFormImport, *this, _nPrefix, _rLocalName, m_xMeAsContainer, _eType);
2101 case OControlElement::FRAME:
2102 case OControlElement::FIXED_TEXT:
2103 return new OReferredControlImport(m_rFormImport, *this, _nPrefix, _rLocalName, m_xMeAsContainer, _eType);
2105 case OControlElement::GRID:
2106 return new OGridImport(m_rFormImport, *this, _nPrefix, _rLocalName, m_xMeAsContainer, _eType);
2108 case OControlElement::VALUERANGE:
2109 return new OValueRangeImport( m_rFormImport, *this, _nPrefix, _rLocalName, m_xMeAsContainer, _eType );
2111 default:
2112 return new OControlImport(m_rFormImport, *this, _nPrefix, _rLocalName, m_xMeAsContainer, _eType);
2116 OUString OFormImport::determineDefaultServiceName() const
2118 return OUString("com.sun.star.form.component.Form");
2121 } // namespace xmloff
2123 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */