1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_extensions.hxx"
30 #include "xsddatatypes.hxx"
31 #include "formstrings.hxx"
32 
33 /** === begin UNO includes === **/
34 #include <com/sun/star/xsd/DataTypeClass.hpp>
35 #include <com/sun/star/xsd/XDataType.hpp>
36 #include <com/sun/star/beans/XPropertySet.hpp>
37 /** === end UNO includes === **/
38 #include <tools/debug.hxx>
39 
40 //........................................................................
41 namespace pcr
42 {
43 //........................................................................
44 
45     using namespace ::com::sun::star::uno;
46     using namespace ::com::sun::star::xsd;
47     using namespace ::com::sun::star::beans;
48 
49 	//====================================================================
50 	//= helper
51 	//====================================================================
52 	//--------------------------------------------------------------------
53     template< typename INTERFACE, typename ARGUMENT >
54     void setSave( INTERFACE* pObject, void ( SAL_CALL INTERFACE::*pSetter )( ARGUMENT ), ARGUMENT _rArg )
55     {
56         try
57         {
58             (pObject->*pSetter)( _rArg );
59         }
60         catch( const Exception& )
61         {
62             OSL_ENSURE( sal_False, "XSDDataType: setSave: caught an exception!" );
63         }
64     }
65 
66 	//--------------------------------------------------------------------
67     template< typename INTERFACE, typename ARGUMENT >
68     ARGUMENT getSave( INTERFACE* pObject, ARGUMENT ( SAL_CALL INTERFACE::*pGetter )( ) )
69     {
70         ARGUMENT aReturn = ARGUMENT();
71         try
72         {
73             aReturn = (pObject->*pGetter)( );
74         }
75         catch( const Exception& )
76         {
77             OSL_ENSURE( sal_False, "XSDDataType: getSave: caught an exception!" );
78         }
79         return aReturn;
80     }
81 
82     template< typename FACETTYPE >
83     FACETTYPE getFacet( const Reference< XPropertySet >& _rxFacets, const ::rtl::OUString& _rFacetName ) SAL_THROW(())
84     {
85         FACETTYPE aReturn;
86         try
87         {
88             OSL_VERIFY( _rxFacets->getPropertyValue( _rFacetName ) >>= aReturn );
89         }
90         catch( const Exception& )
91         {
92             OSL_ENSURE( sal_False, "XSDDataType: getFacet: caught an exception!" );
93         }
94         return aReturn;
95     }
96 
97     //====================================================================
98 	//= XSDDataType
99 	//====================================================================
100 	//--------------------------------------------------------------------
101     XSDDataType::XSDDataType( const Reference< XDataType >& _rxDataType )
102         :m_xDataType( _rxDataType )
103         ,m_refCount( 0 )
104     {
105         DBG_ASSERT( m_xDataType.is(), "XSDDataType::XSDDataType: invalid UNO object!" );
106         if ( m_xDataType.is() )
107             m_xFacetInfo = m_xDataType->getPropertySetInfo();
108     }
109 
110 	//--------------------------------------------------------------------
111     oslInterlockedCount SAL_CALL XSDDataType::acquire()
112     {
113         return osl_incrementInterlockedCount( &m_refCount );
114     }
115 
116 	//--------------------------------------------------------------------
117     oslInterlockedCount SAL_CALL XSDDataType::release()
118     {
119         if ( 0 == osl_decrementInterlockedCount( &m_refCount ) )
120         {
121            delete this;
122            return 0;
123         }
124         return m_refCount;
125     }
126 
127 	//--------------------------------------------------------------------
128     XSDDataType::~XSDDataType()
129     {
130     }
131 
132 	//--------------------------------------------------------------------
133     sal_Int16 XSDDataType::classify() const SAL_THROW(())
134     {
135         sal_Int16 nTypeClass = DataTypeClass::STRING;
136         try
137         {
138             if ( m_xDataType.is() )
139                 nTypeClass = m_xDataType->getTypeClass();
140         }
141         catch( const Exception& )
142         {
143         	OSL_ENSURE( sal_False, "XSDDataType::classify: caught an exception!" );
144         }
145         return nTypeClass;
146     }
147 
148 	//--------------------------------------------------------------------
149     bool XSDDataType::isBasicType() const SAL_THROW(())
150     {
151         return getSave( m_xDataType.get(), &XDataType::getIsBasic );
152     }
153 
154 	//--------------------------------------------------------------------
155     ::rtl::OUString XSDDataType::getName() const SAL_THROW(())
156     {
157         return getSave( m_xDataType.get(), &XDataType::getName );
158     }
159 
160      //--------------------------------------------------------------------
161     void XSDDataType::setFacet( const ::rtl::OUString& _rFacetName, const Any& _rValue ) SAL_THROW(())
162     {
163         try
164         {
165             m_xDataType->setPropertyValue( _rFacetName, _rValue );
166         }
167         catch( const Exception& )
168         {
169             OSL_ENSURE( sal_False, "XSDDataType::setFacet: caught an exception - sure this is the right data type class for this property?" );
170         }
171     }
172 
173     //--------------------------------------------------------------------
174     bool XSDDataType::hasFacet( const ::rtl::OUString& _rFacetName ) const SAL_THROW(())
175     {
176         bool bReturn = false;
177         try
178         {
179             bReturn = m_xFacetInfo.is() && m_xFacetInfo->hasPropertyByName( _rFacetName );
180         }
181         catch( const Exception& )
182         {
183             OSL_ENSURE( sal_False, "XSDDataType::hasFacet: caught an exception!" );
184         }
185         return bReturn;
186     }
187     //--------------------------------------------------------------------
188     Any XSDDataType::getFacet( const ::rtl::OUString& _rFacetName ) SAL_THROW(())
189     {
190         Any aReturn;
191         try
192         {
193             aReturn = m_xDataType->getPropertyValue( _rFacetName );
194         }
195         catch( const Exception& )
196         {
197             OSL_ENSURE( sal_False, "XSDDataType::getFacet: caught an exception - sure this is the right data type class for this property?" );
198         }
199         return aReturn;
200     }
201 
202     //--------------------------------------------------------------------
203     namespace
204     {
205         void lcl_copyProperties( const Reference< XPropertySet >& _rxSource, const Reference< XPropertySet >& _rxDest )
206         {
207             Reference< XPropertySetInfo > xSourceInfo;
208             if ( _rxSource.is() )
209                 xSourceInfo = _rxSource->getPropertySetInfo();
210             Reference< XPropertySetInfo > xDestInfo;
211             if ( _rxDest.is() )
212                 xDestInfo = _rxDest->getPropertySetInfo();
213             OSL_ENSURE( xSourceInfo.is() && xDestInfo.is(), "lcl_copyProperties: invalid property set( info)s!" );
214             if ( !xSourceInfo.is() || !xDestInfo.is() )
215                 return;
216 
217             Sequence< Property > aProperties( xSourceInfo->getProperties() );
218             const Property* pProperties = aProperties.getConstArray();
219             const Property* pPropertiesEnd = pProperties + aProperties.getLength();
220             for ( ; pProperties != pPropertiesEnd; ++pProperties )
221             {
222                 if ( xDestInfo->hasPropertyByName( pProperties->Name ) )
223                     _rxDest->setPropertyValue( pProperties->Name, _rxSource->getPropertyValue( pProperties->Name ) );
224             }
225         }
226     }
227 
228     //--------------------------------------------------------------------
229     void XSDDataType::copyFacetsFrom( const ::rtl::Reference< XSDDataType >& _pSourceType )
230     {
231         OSL_ENSURE( _pSourceType.is(), "XSDDataType::copyFacetsFrom: invalid source type!" );
232         if ( !_pSourceType.is() )
233             return;
234 
235         try
236         {
237             Reference< XPropertySet > xSource( _pSourceType->getUnoDataType(), UNO_QUERY );
238             Reference< XPropertySet > xDest( getUnoDataType(), UNO_QUERY );
239             lcl_copyProperties( xSource, xDest );
240         }
241         catch( const Exception& )
242         {
243         	OSL_ENSURE( sal_False, "XSDDataType::copyFacetsFrom: caught an exception!" );
244         }
245     }
246 
247 //........................................................................
248 } // namespace pcr
249 //........................................................................
250 
251