LCOV - code coverage report
Current view: top level - extensions/source/propctrlr - xsdvalidationhelper.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 1 155 0.6 %
Date: 2014-11-03 Functions: 2 17 11.8 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      20             : #include "xsdvalidationhelper.hxx"
      21             : #include "xsddatatypes.hxx"
      22             : #include "formstrings.hxx"
      23             : 
      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 <tools/diagnose_ex.h>
      32             : 
      33             : 
      34             : namespace pcr
      35             : {
      36             : 
      37             : 
      38             :     using namespace ::com::sun::star;
      39             :     using namespace ::com::sun::star::uno;
      40             :     using namespace ::com::sun::star::beans;
      41             :     using namespace ::com::sun::star::xsd;
      42             :     using namespace ::com::sun::star::util;
      43             :     using namespace ::com::sun::star::lang;
      44             :     using namespace ::com::sun::star::xforms;
      45             : 
      46             :     namespace NumberFormat = ::com::sun::star::util::NumberFormat;
      47             : 
      48             : 
      49             :     //= XSDValidationHelper
      50             : 
      51             : 
      52           0 :     XSDValidationHelper::XSDValidationHelper( ::osl::Mutex& _rMutex, const Reference< XPropertySet >& _rxIntrospectee, const Reference< frame::XModel >& _rxContextDocument )
      53             :         :EFormsHelper( _rMutex, _rxIntrospectee, _rxContextDocument )
      54           0 :         ,m_bInspectingFormattedField( false )
      55             :     {
      56             :         try
      57             :         {
      58           0 :             Reference< XPropertySetInfo > xPSI;
      59           0 :             Reference< XServiceInfo >     xSI( _rxIntrospectee, UNO_QUERY );
      60           0 :             if ( m_xControlModel.is() )
      61           0 :                 xPSI = m_xControlModel->getPropertySetInfo();
      62           0 :             if  (   xPSI.is()
      63           0 :                 &&  xPSI->hasPropertyByName( PROPERTY_FORMATKEY )
      64           0 :                 &&  xPSI->hasPropertyByName( PROPERTY_FORMATSSUPPLIER )
      65           0 :                 &&  xSI.is()
      66           0 :                 &&  xSI->supportsService( SERVICE_COMPONENT_FORMATTEDFIELD )
      67             :                 )
      68           0 :                 m_bInspectingFormattedField = true;
      69             :         }
      70           0 :         catch( const Exception& )
      71             :         {
      72             :             OSL_FAIL( "XSDValidationHelper::XSDValidationHelper: caught an exception while examining the introspectee!" );
      73             :         }
      74           0 :     }
      75             : 
      76             : 
      77           0 :     void XSDValidationHelper::getAvailableDataTypeNames( ::std::vector< OUString >& /* [out] */ _rNames ) const
      78             :     {
      79           0 :         _rNames.resize( 0 );
      80             : 
      81             :         try
      82             :         {
      83           0 :             Reference< XDataTypeRepository > xRepository = getDataTypeRepository();
      84           0 :             Sequence< OUString > aElements;
      85           0 :             if ( xRepository.is() )
      86           0 :                 aElements = xRepository->getElementNames();
      87             : 
      88           0 :             _rNames.resize( aElements.getLength() );
      89           0 :             ::std::copy( aElements.getConstArray(), aElements.getConstArray() + aElements.getLength(), _rNames.begin() );
      90             :         }
      91           0 :         catch( const Exception& )
      92             :         {
      93             :             OSL_FAIL( "XSDValidationHelper::getAvailableDataTypeNames: caught an exception!" );
      94             :         }
      95           0 :     }
      96             : 
      97             : 
      98           0 :     Reference< XDataTypeRepository > XSDValidationHelper::getDataTypeRepository() const
      99             :     {
     100           0 :         Reference< XDataTypeRepository > xRepository;
     101             : 
     102           0 :         Reference< xforms::XModel > xModel( getCurrentFormModel( ) );
     103           0 :         if ( xModel.is() )
     104           0 :             xRepository = xModel->getDataTypeRepository();
     105             : 
     106           0 :         return xRepository;
     107             :     }
     108             : 
     109             : 
     110           0 :     Reference< XDataTypeRepository > XSDValidationHelper::getDataTypeRepository( const OUString& _rModelName ) const
     111             :     {
     112           0 :         Reference< XDataTypeRepository > xRepository;
     113             : 
     114           0 :         Reference< xforms::XModel > xModel( getFormModelByName( _rModelName ) );
     115           0 :         if ( xModel.is() )
     116           0 :             xRepository = xModel->getDataTypeRepository();
     117             : 
     118           0 :         return xRepository;
     119             :     }
     120             : 
     121             : 
     122           0 :     Reference< XDataType > XSDValidationHelper::getDataType( const OUString& _rName ) const
     123             :     {
     124           0 :         Reference< XDataType > xDataType;
     125             : 
     126           0 :         if ( !_rName.isEmpty() )
     127             :         {
     128           0 :             Reference< XDataTypeRepository > xRepository = getDataTypeRepository();
     129           0 :             if ( xRepository.is() )
     130           0 :                 xDataType = xRepository->getDataType( _rName );
     131             :         }
     132           0 :         return xDataType;
     133             :     }
     134             : 
     135             : 
     136           0 :     OUString XSDValidationHelper::getValidatingDataTypeName( ) const
     137             :     {
     138           0 :         OUString sDataTypeName;
     139             :         try
     140             :         {
     141           0 :             Reference< XPropertySet > xBinding( getCurrentBinding() );
     142             :             // it's allowed here to not (yet) have a binding
     143           0 :             if ( xBinding.is() )
     144             :             {
     145           0 :                 OSL_VERIFY( xBinding->getPropertyValue( PROPERTY_XSD_DATA_TYPE ) >>= sDataTypeName );
     146           0 :             }
     147             :         }
     148           0 :         catch( const Exception& )
     149             :         {
     150             :             OSL_FAIL( "XSDValidationHelper::getValidatingDataTypeName: caught an exception!" );
     151             :         }
     152           0 :         return sDataTypeName;
     153             :     }
     154             : 
     155             : 
     156           0 :     ::rtl::Reference< XSDDataType > XSDValidationHelper::getDataTypeByName( const OUString& _rName ) const
     157             :     {
     158           0 :         ::rtl::Reference< XSDDataType > pReturn;
     159             : 
     160             :         try
     161             :         {
     162           0 :             Reference< XDataType > xValidatedAgainst;
     163             : 
     164           0 :             if ( !_rName.isEmpty() )
     165           0 :                 xValidatedAgainst = getDataType( _rName );
     166             : 
     167           0 :             if ( xValidatedAgainst.is() )
     168           0 :                 pReturn = new XSDDataType( xValidatedAgainst );
     169             :         }
     170           0 :         catch( const Exception& )
     171             :         {
     172             :             OSL_FAIL( "XSDValidationHelper::getDataTypeByName: caught an exception!" );
     173             :         }
     174             : 
     175           0 :         return pReturn;
     176             :     }
     177             : 
     178             : 
     179           0 :     ::rtl::Reference< XSDDataType > XSDValidationHelper::getValidatingDataType( ) const
     180             :     {
     181           0 :         return getDataTypeByName( getValidatingDataTypeName() );
     182             :     }
     183             : 
     184             : 
     185           0 :     bool XSDValidationHelper::cloneDataType( const ::rtl::Reference< XSDDataType >& _pDataType, const OUString& _rNewName ) const
     186             :     {
     187             :         OSL_ENSURE( _pDataType.is(), "XSDValidationHelper::removeDataTypeFromRepository: invalid data type!" );
     188           0 :         if ( !_pDataType.is() )
     189           0 :             return false;
     190             : 
     191             :         try
     192             :         {
     193           0 :             Reference< XDataTypeRepository > xRepository( getDataTypeRepository() );
     194             :             OSL_ENSURE( xRepository.is(), "XSDValidationHelper::removeDataTypeFromRepository: invalid data type repository!" );
     195           0 :             if ( !xRepository.is() )
     196           0 :                 return false;
     197             : 
     198           0 :             Reference< XDataType > xDataType( _pDataType->getUnoDataType() );
     199             :             OSL_ENSURE( xDataType.is(), "XSDValidationHelper::removeDataTypeFromRepository: invalid data type (II)!" );
     200           0 :             if ( !xDataType.is() )
     201           0 :                 return false;
     202             : 
     203           0 :             xRepository->cloneDataType( xDataType->getName(), _rNewName );
     204             :         }
     205           0 :         catch( const Exception& )
     206             :         {
     207             :             OSL_FAIL( "XSDValidationHelper::cloneDataType: caught an exception!" );
     208             :         }
     209           0 :         return true;
     210             :     }
     211             : 
     212             : 
     213           0 :     bool XSDValidationHelper::removeDataTypeFromRepository( const OUString& _rName ) const
     214             :     {
     215             :         try
     216             :         {
     217           0 :             Reference< XDataTypeRepository > xRepository( getDataTypeRepository() );
     218             :             OSL_ENSURE( xRepository.is(), "XSDValidationHelper::removeDataTypeFromRepository: invalid data type repository!" );
     219           0 :             if ( !xRepository.is() )
     220           0 :                 return false;
     221             : 
     222           0 :             if ( !xRepository->hasByName( _rName ) )
     223             :             {
     224             :                 OSL_FAIL( "XSDValidationHelper::removeDataTypeFromRepository: invalid repository and/or data type!" );
     225           0 :                 return false;
     226             :             }
     227             : 
     228           0 :             xRepository->revokeDataType( _rName );
     229             :         }
     230           0 :         catch( const Exception& )
     231             :         {
     232             :             OSL_FAIL( "XSDValidationHelper::removeDataTypeFromRepository: caught an exception!" );
     233           0 :             return false;
     234             :         }
     235           0 :         return true;
     236             :     }
     237             : 
     238             : 
     239           0 :     void XSDValidationHelper::setValidatingDataTypeByName( const OUString& _rName ) const
     240             :     {
     241             :         try
     242             :         {
     243           0 :             Reference< XPropertySet > xBinding( getCurrentBinding() );
     244             :             OSL_ENSURE( xBinding.is(), "XSDValidationHelper::setValidatingDataTypeByName: no active binding - how this?" );
     245             : 
     246           0 :             if ( xBinding.is() )
     247             :             {
     248             :                 // get the old data type - this is necessary for notifying property changes
     249           0 :                 OUString sOldDataTypeName;
     250           0 :                 OSL_VERIFY( xBinding->getPropertyValue( PROPERTY_XSD_DATA_TYPE ) >>= sOldDataTypeName );
     251           0 :                 Reference< XPropertySet > xOldType;
     252           0 :                 try { xOldType.set(getDataType( sOldDataTypeName ), css::uno::UNO_QUERY); } catch( const Exception& ) { }
     253             : 
     254             :                 // set the new data type name
     255           0 :                 xBinding->setPropertyValue( PROPERTY_XSD_DATA_TYPE, makeAny( _rName ) );
     256             : 
     257             :                 // retrieve the new data type object
     258           0 :                 Reference< XPropertySet > xNewType( getDataType( _rName ), UNO_QUERY );
     259             : 
     260             :                 // fire any changes in the properties which result from this new type
     261           0 :                 std::set< OUString > aFilter; aFilter.insert( static_cast<const OUString&>(PROPERTY_NAME) );
     262           0 :                 firePropertyChanges( xOldType, xNewType, aFilter );
     263             : 
     264             :                 // fire the change in the Data Type property
     265           0 :                 OUString sNewDataTypeName;
     266           0 :                 OSL_VERIFY( xBinding->getPropertyValue( PROPERTY_XSD_DATA_TYPE ) >>= sNewDataTypeName );
     267           0 :                 firePropertyChange( PROPERTY_XSD_DATA_TYPE, makeAny( sOldDataTypeName ), makeAny( sNewDataTypeName ) );
     268           0 :             }
     269             :         }
     270           0 :         catch( const Exception& )
     271             :         {
     272             :             DBG_UNHANDLED_EXCEPTION();
     273             :         }
     274           0 :     }
     275             : 
     276             : 
     277           0 :     void XSDValidationHelper::copyDataType( const OUString& _rFromModel, const OUString& _rToModel,
     278             :                 const OUString& _rDataTypeName ) const
     279             :     {
     280           0 :         if ( _rFromModel == _rToModel )
     281             :             // nothing to do (me thinks)
     282           0 :             return;
     283             : 
     284             :         try
     285             :         {
     286           0 :             Reference< XDataTypeRepository > xFromRepository, xToRepository;
     287           0 :             if ( !_rFromModel.isEmpty() )
     288           0 :                 xFromRepository = getDataTypeRepository( _rFromModel );
     289           0 :             if ( !_rToModel.isEmpty() )
     290           0 :                 xToRepository = getDataTypeRepository( _rToModel );
     291             : 
     292           0 :             if ( !xFromRepository.is() || !xToRepository.is() )
     293           0 :                 return;
     294             : 
     295           0 :             if ( !xFromRepository->hasByName( _rDataTypeName ) || xToRepository->hasByName( _rDataTypeName ) )
     296             :                 // not existent in the source, or already existent (by name) in the destination
     297           0 :                 return;
     298             : 
     299             :             // determine the built-in type belonging to the source type
     300           0 :             ::rtl::Reference< XSDDataType > pSourceType = new XSDDataType( xFromRepository->getDataType( _rDataTypeName ) );
     301           0 :             OUString sTargetBaseType = getBasicTypeNameForClass( pSourceType->classify(), xToRepository );
     302             : 
     303             :             // create the target type
     304           0 :             Reference< XDataType > xTargetType = xToRepository->cloneDataType( sTargetBaseType, _rDataTypeName );
     305           0 :             ::rtl::Reference< XSDDataType > pTargetType = new XSDDataType( xTargetType );
     306             : 
     307             :             // copy the facets
     308           0 :             pTargetType->copyFacetsFrom( pSourceType );
     309             :         }
     310           0 :         catch( const Exception& )
     311             :         {
     312             :             OSL_FAIL( "XSDValidationHelper::copyDataType: caught an exception!" );
     313             :         }
     314             :     }
     315             : 
     316             : 
     317           0 :     void XSDValidationHelper::findDefaultFormatForIntrospectee()
     318             :     {
     319             :         try
     320             :         {
     321           0 :             ::rtl::Reference< XSDDataType > xDataType = getValidatingDataType();
     322           0 :             if ( xDataType.is() )
     323             :             {
     324             :                 // find a NumberFormat type corresponding to the DataTypeClass
     325           0 :                 sal_Int16 nNumberFormatType = NumberFormat::NUMBER;
     326           0 :                 switch ( xDataType->classify() )
     327             :                 {
     328             :                 case DataTypeClass::DATETIME:
     329           0 :                     nNumberFormatType = NumberFormat::DATETIME;
     330           0 :                     break;
     331             :                 case DataTypeClass::DATE:
     332           0 :                     nNumberFormatType = NumberFormat::DATE;
     333           0 :                     break;
     334             :                 case DataTypeClass::TIME:
     335           0 :                     nNumberFormatType = NumberFormat::TIME;
     336           0 :                     break;
     337             :                 case DataTypeClass::STRING:
     338             :                 case DataTypeClass::anyURI:
     339             :                 case DataTypeClass::QName:
     340             :                 case DataTypeClass::NOTATION:
     341           0 :                     nNumberFormatType = NumberFormat::TEXT;
     342           0 :                     break;
     343             :                 }
     344             : 
     345             :                 // get the number formatter from the introspectee
     346           0 :                 Reference< XNumberFormatsSupplier > xSupplier;
     347           0 :                 Reference< XNumberFormatTypes > xFormatTypes;
     348           0 :                 OSL_VERIFY( m_xControlModel->getPropertyValue( PROPERTY_FORMATSSUPPLIER ) >>= xSupplier );
     349           0 :                 if ( xSupplier.is() )
     350           0 :                     xFormatTypes.set(xSupplier->getNumberFormats(), css::uno::UNO_QUERY);
     351             :                 OSL_ENSURE( xFormatTypes.is(), "XSDValidationHelper::findDefaultFormatForIntrospectee: no number formats for the introspectee!" );
     352           0 :                 if ( !xFormatTypes.is() )
     353           0 :                     return;
     354             : 
     355             :                 // and the standard format for the given NumberFormat type
     356           0 :                 sal_Int32 nDesiredFormat = xFormatTypes->getStandardFormat( nNumberFormatType, SvtSysLocale().GetLanguageTag().getLocale() );
     357             : 
     358             :                 // set this at the introspectee
     359           0 :                 m_xControlModel->setPropertyValue( PROPERTY_FORMATKEY, makeAny( nDesiredFormat ) );
     360           0 :             }
     361             :         }
     362           0 :         catch( const Exception& )
     363             :         {
     364             :             OSL_FAIL( "XSDValidationHelper::findDefaultFormatForIntrospectee: caught an exception!" );
     365             :         }
     366             :     }
     367             : 
     368             : 
     369           0 :     OUString XSDValidationHelper::getBasicTypeNameForClass( sal_Int16 _nClass ) const
     370             :     {
     371           0 :         return getBasicTypeNameForClass( _nClass, getDataTypeRepository() );
     372             :     }
     373             : 
     374             : 
     375           0 :     OUString XSDValidationHelper::getBasicTypeNameForClass( sal_Int16 _nClass, Reference< XDataTypeRepository > _rxRepository ) const
     376             :     {
     377           0 :         OUString sReturn;
     378             :         OSL_ENSURE( _rxRepository.is(), "XSDValidationHelper::getBasicTypeNameForClass: invalid repository!" );
     379           0 :         if ( !_rxRepository.is() )
     380           0 :             return sReturn;
     381             : 
     382             :         try
     383             :         {
     384           0 :             Reference< XDataType > xDataType = _rxRepository->getBasicDataType( _nClass );
     385             :             OSL_ENSURE( xDataType.is(), "XSDValidationHelper::getBasicTypeNameForClass: invalid data type returned!" );
     386           0 :             if ( xDataType.is() )
     387           0 :                 sReturn = xDataType->getName();
     388             :         }
     389           0 :         catch( const Exception& )
     390             :         {
     391             :             OSL_FAIL( "XSDValidationHelper::getBasicTypeNameForClass: caught an exception!" );
     392             :         }
     393             : 
     394           0 :         return sReturn;
     395             :     }
     396             : 
     397             : 
     398          12 : } // namespace pcr
     399             : 
     400             : 
     401             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10