1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "xsdvalidationhelper.hxx"
21 #include "xsddatatypes.hxx"
22 #include "formstrings.hxx"
24 #include <com/sun/star/lang/XServiceInfo.hpp>
25 #include <com/sun/star/xsd/DataTypeClass.hpp>
26 #include <com/sun/star/util/NumberFormat.hpp>
27 #include <com/sun/star/util/XNumberFormatTypes.hpp>
28 #include <com/sun/star/util/XNumberFormatsSupplier.hpp>
29 #include <com/sun/star/xforms/XDataTypeRepository.hpp>
30 #include <unotools/syslocale.hxx>
31 #include <i18nlangtag/languagetag.hxx>
32 #include <tools/diagnose_ex.h>
39 using namespace ::com::sun::star
;
40 using namespace ::com::sun::star::uno
;
41 using namespace ::com::sun::star::beans
;
42 using namespace ::com::sun::star::xsd
;
43 using namespace ::com::sun::star::util
;
44 using namespace ::com::sun::star::lang
;
45 using namespace ::com::sun::star::xforms
;
47 namespace NumberFormat
= ::com::sun::star::util::NumberFormat
;
50 //= XSDValidationHelper
53 XSDValidationHelper::XSDValidationHelper( ::osl::Mutex
& _rMutex
, const Reference
< XPropertySet
>& _rxIntrospectee
, const Reference
< frame::XModel
>& _rxContextDocument
)
54 :EFormsHelper( _rMutex
, _rxIntrospectee
, _rxContextDocument
)
55 ,m_bInspectingFormattedField( false )
59 Reference
< XPropertySetInfo
> xPSI
;
60 Reference
< XServiceInfo
> xSI( _rxIntrospectee
, UNO_QUERY
);
61 if ( m_xControlModel
.is() )
62 xPSI
= m_xControlModel
->getPropertySetInfo();
64 && xPSI
->hasPropertyByName( PROPERTY_FORMATKEY
)
65 && xPSI
->hasPropertyByName( PROPERTY_FORMATSSUPPLIER
)
67 && xSI
->supportsService( SERVICE_COMPONENT_FORMATTEDFIELD
)
69 m_bInspectingFormattedField
= true;
71 catch( const Exception
& )
73 OSL_FAIL( "XSDValidationHelper::XSDValidationHelper: caught an exception while examining the introspectee!" );
78 void XSDValidationHelper::getAvailableDataTypeNames( std::vector
< OUString
>& /* [out] */ _rNames
) const
84 Reference
< XDataTypeRepository
> xRepository
= getDataTypeRepository();
85 Sequence
< OUString
> aElements
;
86 if ( xRepository
.is() )
87 aElements
= xRepository
->getElementNames();
89 _rNames
.resize( aElements
.getLength() );
90 std::copy( aElements
.begin(), aElements
.end(), _rNames
.begin() );
92 catch( const Exception
& )
94 OSL_FAIL( "XSDValidationHelper::getAvailableDataTypeNames: caught an exception!" );
99 Reference
< XDataTypeRepository
> XSDValidationHelper::getDataTypeRepository() const
101 Reference
< XDataTypeRepository
> xRepository
;
103 Reference
< xforms::XModel
> xModel( getCurrentFormModel( ) );
105 xRepository
= xModel
->getDataTypeRepository();
111 Reference
< XDataTypeRepository
> XSDValidationHelper::getDataTypeRepository( const OUString
& _rModelName
) const
113 Reference
< XDataTypeRepository
> xRepository
;
115 Reference
< xforms::XModel
> xModel( getFormModelByName( _rModelName
) );
117 xRepository
= xModel
->getDataTypeRepository();
123 Reference
< XDataType
> XSDValidationHelper::getDataType( const OUString
& _rName
) const
125 Reference
< XDataType
> xDataType
;
127 if ( !_rName
.isEmpty() )
129 Reference
< XDataTypeRepository
> xRepository
= getDataTypeRepository();
130 if ( xRepository
.is() )
131 xDataType
= xRepository
->getDataType( _rName
);
137 OUString
XSDValidationHelper::getValidatingDataTypeName( ) const
139 OUString sDataTypeName
;
142 Reference
< XPropertySet
> xBinding( getCurrentBinding() );
143 // it's allowed here to not (yet) have a binding
146 OSL_VERIFY( xBinding
->getPropertyValue( PROPERTY_XSD_DATA_TYPE
) >>= sDataTypeName
);
149 catch( const Exception
& )
151 OSL_FAIL( "XSDValidationHelper::getValidatingDataTypeName: caught an exception!" );
153 return sDataTypeName
;
157 ::rtl::Reference
< XSDDataType
> XSDValidationHelper::getDataTypeByName( const OUString
& _rName
) const
159 ::rtl::Reference
< XSDDataType
> pReturn
;
163 Reference
< XDataType
> xValidatedAgainst
;
165 if ( !_rName
.isEmpty() )
166 xValidatedAgainst
= getDataType( _rName
);
168 if ( xValidatedAgainst
.is() )
169 pReturn
= new XSDDataType( xValidatedAgainst
);
171 catch( const Exception
& )
173 OSL_FAIL( "XSDValidationHelper::getDataTypeByName: caught an exception!" );
180 ::rtl::Reference
< XSDDataType
> XSDValidationHelper::getValidatingDataType( ) const
182 return getDataTypeByName( getValidatingDataTypeName() );
186 bool XSDValidationHelper::cloneDataType( const ::rtl::Reference
< XSDDataType
>& _pDataType
, const OUString
& _rNewName
) const
188 OSL_ENSURE( _pDataType
.is(), "XSDValidationHelper::removeDataTypeFromRepository: invalid data type!" );
189 if ( !_pDataType
.is() )
194 Reference
< XDataTypeRepository
> xRepository( getDataTypeRepository() );
195 OSL_ENSURE( xRepository
.is(), "XSDValidationHelper::removeDataTypeFromRepository: invalid data type repository!" );
196 if ( !xRepository
.is() )
199 Reference
< XDataType
> xDataType( _pDataType
->getUnoDataType() );
200 OSL_ENSURE( xDataType
.is(), "XSDValidationHelper::removeDataTypeFromRepository: invalid data type (II)!" );
201 if ( !xDataType
.is() )
204 xRepository
->cloneDataType( xDataType
->getName(), _rNewName
);
206 catch( const Exception
& )
208 OSL_FAIL( "XSDValidationHelper::cloneDataType: caught an exception!" );
214 bool XSDValidationHelper::removeDataTypeFromRepository( const OUString
& _rName
) const
218 Reference
< XDataTypeRepository
> xRepository( getDataTypeRepository() );
219 OSL_ENSURE( xRepository
.is(), "XSDValidationHelper::removeDataTypeFromRepository: invalid data type repository!" );
220 if ( !xRepository
.is() )
223 if ( !xRepository
->hasByName( _rName
) )
225 OSL_FAIL( "XSDValidationHelper::removeDataTypeFromRepository: invalid repository and/or data type!" );
229 xRepository
->revokeDataType( _rName
);
231 catch( const Exception
& )
233 OSL_FAIL( "XSDValidationHelper::removeDataTypeFromRepository: caught an exception!" );
240 void XSDValidationHelper::setValidatingDataTypeByName( const OUString
& _rName
) const
244 Reference
< XPropertySet
> xBinding( getCurrentBinding() );
245 OSL_ENSURE( xBinding
.is(), "XSDValidationHelper::setValidatingDataTypeByName: no active binding - how this?" );
249 // get the old data type - this is necessary for notifying property changes
250 OUString sOldDataTypeName
;
251 OSL_VERIFY( xBinding
->getPropertyValue( PROPERTY_XSD_DATA_TYPE
) >>= sOldDataTypeName
);
252 Reference
< XPropertySet
> xOldType
;
254 xOldType
= getDataType( sOldDataTypeName
);
255 } catch( const Exception
& ) { }
257 // set the new data type name
258 xBinding
->setPropertyValue( PROPERTY_XSD_DATA_TYPE
, makeAny( _rName
) );
260 // retrieve the new data type object
261 Reference
< XPropertySet
> xNewType
= getDataType( _rName
);
263 // fire any changes in the properties which result from this new type
264 std::set
< OUString
> aFilter
; aFilter
.insert( PROPERTY_NAME
);
265 firePropertyChanges( xOldType
, xNewType
, aFilter
);
267 // fire the change in the Data Type property
268 OUString sNewDataTypeName
;
269 OSL_VERIFY( xBinding
->getPropertyValue( PROPERTY_XSD_DATA_TYPE
) >>= sNewDataTypeName
);
270 firePropertyChange( PROPERTY_XSD_DATA_TYPE
, makeAny( sOldDataTypeName
), makeAny( sNewDataTypeName
) );
273 catch( const Exception
& )
275 DBG_UNHANDLED_EXCEPTION("extensions.propctrlr");
280 void XSDValidationHelper::copyDataType( const OUString
& _rFromModel
, const OUString
& _rToModel
,
281 const OUString
& _rDataTypeName
) const
283 if ( _rFromModel
== _rToModel
)
284 // nothing to do (me thinks)
289 Reference
< XDataTypeRepository
> xFromRepository
, xToRepository
;
290 if ( !_rFromModel
.isEmpty() )
291 xFromRepository
= getDataTypeRepository( _rFromModel
);
292 if ( !_rToModel
.isEmpty() )
293 xToRepository
= getDataTypeRepository( _rToModel
);
295 if ( !xFromRepository
.is() || !xToRepository
.is() )
298 if ( !xFromRepository
->hasByName( _rDataTypeName
) || xToRepository
->hasByName( _rDataTypeName
) )
299 // not existent in the source, or already existent (by name) in the destination
302 // determine the built-in type belonging to the source type
303 ::rtl::Reference
< XSDDataType
> pSourceType
= new XSDDataType( xFromRepository
->getDataType( _rDataTypeName
) );
304 OUString sTargetBaseType
= getBasicTypeNameForClass( pSourceType
->classify(), xToRepository
);
306 // create the target type
307 Reference
< XDataType
> xTargetType
= xToRepository
->cloneDataType( sTargetBaseType
, _rDataTypeName
);
308 ::rtl::Reference
< XSDDataType
> pTargetType
= new XSDDataType( xTargetType
);
311 pTargetType
->copyFacetsFrom( pSourceType
);
313 catch( const Exception
& )
315 OSL_FAIL( "XSDValidationHelper::copyDataType: caught an exception!" );
320 void XSDValidationHelper::findDefaultFormatForIntrospectee()
324 ::rtl::Reference
< XSDDataType
> xDataType
= getValidatingDataType();
325 if ( xDataType
.is() )
327 // find a NumberFormat type corresponding to the DataTypeClass
328 sal_Int16 nNumberFormatType
= NumberFormat::NUMBER
;
329 switch ( xDataType
->classify() )
331 case DataTypeClass::DATETIME
:
332 nNumberFormatType
= NumberFormat::DATETIME
;
334 case DataTypeClass::DATE
:
335 nNumberFormatType
= NumberFormat::DATE
;
337 case DataTypeClass::TIME
:
338 nNumberFormatType
= NumberFormat::TIME
;
340 case DataTypeClass::STRING
:
341 case DataTypeClass::anyURI
:
342 case DataTypeClass::QName
:
343 case DataTypeClass::NOTATION
:
344 nNumberFormatType
= NumberFormat::TEXT
;
348 // get the number formatter from the introspectee
349 Reference
< XNumberFormatsSupplier
> xSupplier
;
350 Reference
< XNumberFormatTypes
> xFormatTypes
;
351 OSL_VERIFY( m_xControlModel
->getPropertyValue( PROPERTY_FORMATSSUPPLIER
) >>= xSupplier
);
352 if ( xSupplier
.is() )
353 xFormatTypes
.set(xSupplier
->getNumberFormats(), css::uno::UNO_QUERY
);
354 OSL_ENSURE( xFormatTypes
.is(), "XSDValidationHelper::findDefaultFormatForIntrospectee: no number formats for the introspectee!" );
355 if ( !xFormatTypes
.is() )
358 // and the standard format for the given NumberFormat type
359 sal_Int32 nDesiredFormat
= xFormatTypes
->getStandardFormat( nNumberFormatType
, SvtSysLocale().GetLanguageTag().getLocale() );
361 // set this at the introspectee
362 m_xControlModel
->setPropertyValue( PROPERTY_FORMATKEY
, makeAny( nDesiredFormat
) );
365 catch( const Exception
& )
367 OSL_FAIL( "XSDValidationHelper::findDefaultFormatForIntrospectee: caught an exception!" );
372 OUString
XSDValidationHelper::getBasicTypeNameForClass( sal_Int16 _nClass
) const
374 return getBasicTypeNameForClass( _nClass
, getDataTypeRepository() );
378 OUString
XSDValidationHelper::getBasicTypeNameForClass( sal_Int16 _nClass
, const Reference
< XDataTypeRepository
>& _rxRepository
)
381 OSL_ENSURE( _rxRepository
.is(), "XSDValidationHelper::getBasicTypeNameForClass: invalid repository!" );
382 if ( !_rxRepository
.is() )
387 Reference
< XDataType
> xDataType
= _rxRepository
->getBasicDataType( _nClass
);
388 OSL_ENSURE( xDataType
.is(), "XSDValidationHelper::getBasicTypeNameForClass: invalid data type returned!" );
389 if ( xDataType
.is() )
390 sReturn
= xDataType
->getName();
392 catch( const Exception
& )
394 OSL_FAIL( "XSDValidationHelper::getBasicTypeNameForClass: caught an exception!" );
404 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */