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 :
21 : #include "datatyperepository.hxx"
22 : #include "datatypes.hxx"
23 : #include "frm_resource.hrc"
24 : #include "frm_resource.hxx"
25 : #include "frm_strings.hxx"
26 : #include "property.hrc"
27 :
28 : #include <tools/debug.hxx>
29 : #include <comphelper/enumhelper.hxx>
30 :
31 : #include <functional>
32 : #include <algorithm>
33 : #include <o3tl/compat_functional.hxx>
34 :
35 :
36 : namespace xforms
37 : {
38 :
39 :
40 : using ::com::sun::star::uno::Reference;
41 : using ::com::sun::star::uno::RuntimeException;
42 : using ::com::sun::star::uno::Any;
43 : using ::com::sun::star::uno::Type;
44 : using ::com::sun::star::uno::makeAny;
45 : using ::com::sun::star::uno::Sequence;
46 : using ::com::sun::star::util::VetoException;
47 : using ::com::sun::star::container::NoSuchElementException;
48 : using ::com::sun::star::container::ElementExistException;
49 : using ::com::sun::star::container::XEnumeration;
50 : using ::com::sun::star::lang::WrappedTargetException;
51 : using ::com::sun::star::xsd::XDataType;
52 : using namespace frm;
53 :
54 :
55 : //= ODataTypeRepository
56 :
57 :
58 0 : ODataTypeRepository::ODataTypeRepository( )
59 : {
60 :
61 : // insert some basic types
62 0 : OUString sName( FRM_RES_STRING( RID_STR_DATATYPE_STRING ) );
63 0 : m_aRepository[ sName ] = new OStringType( sName, ::com::sun::star::xsd::DataTypeClass::STRING );
64 :
65 0 : sName = FRM_RES_STRING( RID_STR_DATATYPE_URL );
66 0 : m_aRepository[ sName ] = new OStringType( sName, ::com::sun::star::xsd::DataTypeClass::anyURI );
67 :
68 0 : sName = FRM_RES_STRING( RID_STR_DATATYPE_BOOLEAN );
69 0 : m_aRepository[ sName ] = new OBooleanType( sName );
70 :
71 0 : sName = FRM_RES_STRING( RID_STR_DATATYPE_DECIMAL );
72 0 : m_aRepository[ sName ] = new ODecimalType( sName, ::com::sun::star::xsd::DataTypeClass::DECIMAL );
73 :
74 0 : sName = FRM_RES_STRING( RID_STR_DATATYPE_FLOAT );
75 0 : m_aRepository[ sName ] = new ODecimalType( sName, ::com::sun::star::xsd::DataTypeClass::FLOAT );
76 :
77 0 : sName = FRM_RES_STRING( RID_STR_DATATYPE_DOUBLE );
78 0 : m_aRepository[ sName ] = new ODecimalType( sName, ::com::sun::star::xsd::DataTypeClass::DOUBLE );
79 :
80 0 : sName = FRM_RES_STRING( RID_STR_DATATYPE_DATE );
81 0 : m_aRepository[ sName ] = new ODateType( sName );
82 :
83 0 : sName = FRM_RES_STRING( RID_STR_DATATYPE_TIME );
84 0 : m_aRepository[ sName ] = new OTimeType( sName );
85 :
86 0 : sName = FRM_RES_STRING( RID_STR_DATATYPE_DATETIME );
87 0 : m_aRepository[ sName ] = new ODateTimeType( sName );
88 :
89 0 : sName = FRM_RES_STRING( RID_STR_DATATYPE_YEAR );
90 0 : m_aRepository[ sName ] = new OShortIntegerType( sName, ::com::sun::star::xsd::DataTypeClass::gYear );
91 :
92 0 : sName = FRM_RES_STRING( RID_STR_DATATYPE_MONTH );
93 0 : m_aRepository[ sName ] = new OShortIntegerType( sName, ::com::sun::star::xsd::DataTypeClass::gMonth );
94 :
95 0 : sName = FRM_RES_STRING( RID_STR_DATATYPE_DAY );
96 0 : m_aRepository[ sName ] = new OShortIntegerType( sName, ::com::sun::star::xsd::DataTypeClass::gDay );
97 0 : }
98 :
99 :
100 0 : ODataTypeRepository::~ODataTypeRepository( )
101 : {
102 0 : }
103 :
104 :
105 0 : ODataTypeRepository::Repository::iterator ODataTypeRepository::implLocate( const OUString& _rName, bool _bAllowMiss ) SAL_THROW( ( NoSuchElementException ) )
106 : {
107 0 : Repository::iterator aTypePos = m_aRepository.find( _rName );
108 0 : if ( aTypePos == m_aRepository.end() && !_bAllowMiss )
109 0 : throw NoSuchElementException( OUString(), *this );
110 :
111 0 : return aTypePos;
112 : }
113 :
114 :
115 0 : Reference< XDataType > SAL_CALL ODataTypeRepository::getBasicDataType( sal_Int16 dataTypeClass ) throw (NoSuchElementException, RuntimeException, std::exception)
116 : {
117 0 : Reference< XDataType > xReturn;
118 :
119 0 : for ( Repository::const_iterator lookup = m_aRepository.begin();
120 0 : ( lookup != m_aRepository.end() ) && ! xReturn.is();
121 : ++lookup
122 : )
123 : {
124 0 : if ( lookup->second->getIsBasic() && ( lookup->second->getTypeClass() == dataTypeClass ) )
125 0 : xReturn = lookup->second.get();
126 : }
127 :
128 0 : if ( !xReturn.is() )
129 0 : throw NoSuchElementException( OUString(), *this );
130 :
131 0 : return xReturn;
132 : }
133 :
134 :
135 0 : Reference< XDataType > SAL_CALL ODataTypeRepository::cloneDataType( const OUString& sourceName, const OUString& newName ) throw (NoSuchElementException, ElementExistException, RuntimeException, std::exception)
136 : {
137 0 : ::osl::MutexGuard aGuard( m_aMutex );
138 :
139 0 : Repository::iterator aTypePos = implLocate( newName, true );
140 0 : if ( aTypePos != m_aRepository.end() )
141 0 : throw ElementExistException( OUString(), *this );
142 :
143 0 : aTypePos = implLocate( sourceName );
144 0 : OXSDDataType* pClone = aTypePos->second->clone( newName );
145 0 : m_aRepository[ newName ] = pClone;
146 :
147 0 : return pClone;
148 : }
149 :
150 :
151 0 : void SAL_CALL ODataTypeRepository::revokeDataType( const OUString& typeName ) throw (NoSuchElementException, VetoException, RuntimeException, std::exception)
152 : {
153 0 : ::osl::MutexGuard aGuard( m_aMutex );
154 :
155 0 : Repository::iterator aTypePos = implLocate( typeName );
156 0 : if ( aTypePos->second->getIsBasic() )
157 0 : throw VetoException("This is a built-in type and cannot be removed.", *this );
158 : // TODO: localize this error message
159 :
160 0 : m_aRepository.erase( aTypePos );
161 0 : }
162 :
163 :
164 0 : Reference< XDataType > SAL_CALL ODataTypeRepository::getDataType( const OUString& typeName ) throw (NoSuchElementException, RuntimeException, std::exception)
165 : {
166 0 : ::osl::MutexGuard aGuard( m_aMutex );
167 0 : return implLocate( typeName, false )->second.get();
168 : }
169 :
170 :
171 :
172 0 : Reference< XEnumeration > SAL_CALL ODataTypeRepository::createEnumeration( ) throw (RuntimeException, std::exception)
173 : {
174 0 : return new ::comphelper::OEnumerationByName( this );
175 : }
176 :
177 :
178 0 : Any SAL_CALL ODataTypeRepository::getByName( const OUString& aName ) throw (NoSuchElementException, WrappedTargetException, RuntimeException, std::exception)
179 : {
180 0 : return makeAny( getDataType( aName ) );
181 : }
182 :
183 :
184 0 : Sequence< OUString > SAL_CALL ODataTypeRepository::getElementNames( ) throw (RuntimeException, std::exception)
185 : {
186 0 : ::osl::MutexGuard aGuard( m_aMutex );
187 :
188 0 : Sequence< OUString > aNames( m_aRepository.size() );
189 : ::std::transform(
190 : m_aRepository.begin(),
191 : m_aRepository.end(),
192 : aNames.getArray(),
193 : ::o3tl::select1st< Repository::value_type >()
194 0 : );
195 0 : return aNames;
196 : }
197 :
198 :
199 0 : sal_Bool SAL_CALL ODataTypeRepository::hasByName( const OUString& aName ) throw (RuntimeException, std::exception)
200 : {
201 0 : ::osl::MutexGuard aGuard( m_aMutex );
202 0 : return m_aRepository.find( aName ) != m_aRepository.end();
203 : }
204 :
205 :
206 0 : Type SAL_CALL ODataTypeRepository::getElementType( ) throw (RuntimeException, std::exception)
207 : {
208 0 : return ::getCppuType( static_cast< Reference< XDataType >* >( NULL ) );
209 : }
210 :
211 :
212 0 : sal_Bool SAL_CALL ODataTypeRepository::hasElements( ) throw (RuntimeException, std::exception)
213 : {
214 0 : return !m_aRepository.empty();
215 : }
216 :
217 :
218 : // type specific implementation of registerProperties, using explicit
219 : // template instantiations
220 :
221 : template<>
222 0 : void OValueLimitedType<com::sun::star::util::Date>::registerProperties()
223 : {
224 0 : OValueLimitedType_Base::registerProperties();
225 :
226 0 : REGISTER_VOID_PROP( XSD_MAX_INCLUSIVE_DATE, m_aMaxInclusive, ValueType );
227 0 : REGISTER_VOID_PROP( XSD_MAX_EXCLUSIVE_DATE, m_aMaxExclusive, ValueType );
228 0 : REGISTER_VOID_PROP( XSD_MIN_INCLUSIVE_DATE, m_aMinInclusive, ValueType );
229 0 : REGISTER_VOID_PROP( XSD_MIN_EXCLUSIVE_DATE, m_aMinExclusive, ValueType );
230 0 : }
231 :
232 : template<>
233 0 : void OValueLimitedType<com::sun::star::util::Time>::registerProperties()
234 : {
235 0 : OValueLimitedType_Base::registerProperties();
236 :
237 0 : REGISTER_VOID_PROP( XSD_MAX_INCLUSIVE_TIME, m_aMaxInclusive, ValueType );
238 0 : REGISTER_VOID_PROP( XSD_MAX_EXCLUSIVE_TIME, m_aMaxExclusive, ValueType );
239 0 : REGISTER_VOID_PROP( XSD_MIN_INCLUSIVE_TIME, m_aMinInclusive, ValueType );
240 0 : REGISTER_VOID_PROP( XSD_MIN_EXCLUSIVE_TIME, m_aMinExclusive, ValueType );
241 0 : }
242 :
243 : template<>
244 0 : void OValueLimitedType<com::sun::star::util::DateTime>::registerProperties()
245 : {
246 0 : OValueLimitedType_Base::registerProperties();
247 :
248 0 : REGISTER_VOID_PROP( XSD_MAX_INCLUSIVE_DATE_TIME, m_aMaxInclusive, ValueType );
249 0 : REGISTER_VOID_PROP( XSD_MAX_EXCLUSIVE_DATE_TIME, m_aMaxExclusive, ValueType );
250 0 : REGISTER_VOID_PROP( XSD_MIN_INCLUSIVE_DATE_TIME, m_aMinInclusive, ValueType );
251 0 : REGISTER_VOID_PROP( XSD_MIN_EXCLUSIVE_DATE_TIME, m_aMinExclusive, ValueType );
252 0 : }
253 :
254 : template<>
255 0 : void OValueLimitedType<double>::registerProperties()
256 : {
257 0 : OValueLimitedType_Base::registerProperties();
258 :
259 0 : REGISTER_VOID_PROP( XSD_MAX_INCLUSIVE_DOUBLE, m_aMaxInclusive, ValueType );
260 0 : REGISTER_VOID_PROP( XSD_MAX_EXCLUSIVE_DOUBLE, m_aMaxExclusive, ValueType );
261 0 : REGISTER_VOID_PROP( XSD_MIN_INCLUSIVE_DOUBLE, m_aMinInclusive, ValueType );
262 0 : REGISTER_VOID_PROP( XSD_MIN_EXCLUSIVE_DOUBLE, m_aMinExclusive, ValueType );
263 0 : }
264 :
265 : template<>
266 0 : void OValueLimitedType<sal_Int16>::registerProperties()
267 : {
268 0 : OValueLimitedType_Base::registerProperties();
269 :
270 0 : REGISTER_VOID_PROP( XSD_MAX_INCLUSIVE_INT, m_aMaxInclusive, ValueType );
271 0 : REGISTER_VOID_PROP( XSD_MAX_EXCLUSIVE_INT, m_aMaxExclusive, ValueType );
272 0 : REGISTER_VOID_PROP( XSD_MIN_INCLUSIVE_INT, m_aMinInclusive, ValueType );
273 0 : REGISTER_VOID_PROP( XSD_MIN_EXCLUSIVE_INT, m_aMinExclusive, ValueType );
274 0 : }
275 :
276 : } // namespace xforms
277 :
278 :
279 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|