fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / xmloff / source / xforms / xformsapi.cxx
blob9f1b32ce5c990e6b00b145be33ddf47d30a4efd3
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 .
21 #include "xformsapi.hxx"
23 #include <com/sun/star/uno/Reference.hxx>
24 #include <com/sun/star/beans/XPropertySet.hpp>
25 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
26 #include <com/sun/star/container/XNameAccess.hpp>
27 #include <com/sun/star/xforms/XFormsSupplier.hpp>
28 #include <com/sun/star/xforms/XDataTypeRepository.hpp>
29 #include <com/sun/star/xforms/Model.hpp>
30 #include <com/sun/star/xforms/XModel2.hpp>
31 #include <com/sun/star/container/XNameContainer.hpp>
32 #include <com/sun/star/xsd/DataTypeClass.hpp>
34 #include <comphelper/processfactory.hxx>
35 #include <tools/debug.hxx>
37 #include <xmloff/xmltoken.hxx>
38 #include <xmloff/nmspmap.hxx>
39 #include <xmloff/xmlnmspe.hxx>
40 #include <xmloff/xmltkmap.hxx>
42 using com::sun::star::uno::Reference;
43 using com::sun::star::uno::Sequence;
44 using com::sun::star::uno::UNO_QUERY;
45 using com::sun::star::uno::UNO_QUERY_THROW;
46 using com::sun::star::beans::XPropertySet;
47 using com::sun::star::container::XNameAccess;
48 using com::sun::star::lang::XMultiServiceFactory;
49 using com::sun::star::xforms::XFormsSupplier;
50 using com::sun::star::xforms::XDataTypeRepository;
51 using com::sun::star::xforms::Model;
52 using com::sun::star::xforms::XModel2;
53 using com::sun::star::container::XNameContainer;
54 using com::sun::star::uno::makeAny;
55 using com::sun::star::uno::Any;
56 using com::sun::star::uno::Exception;
58 using namespace com::sun::star;
59 using namespace xmloff::token;
61 Reference<XModel2> xforms_createXFormsModel()
63 Reference<XModel2> xModel = Model::create( comphelper::getProcessComponentContext() );
65 return xModel;
68 void xforms_addXFormsModel(
69 const Reference<frame::XModel>& xDocument,
70 const Reference<xforms::XModel2>& xModel )
72 bool bSuccess = false;
73 try
75 Reference<XFormsSupplier> xSupplier( xDocument, UNO_QUERY );
76 if( xSupplier.is() )
78 Reference<XNameContainer> xForms = xSupplier->getXForms();
79 if( xForms.is() )
81 OUString sName;
82 xModel->getPropertyValue("ID") >>= sName;
83 xForms->insertByName( sName, makeAny( xModel ) );
84 bSuccess = true;
88 catch( const Exception& )
90 ; // no success!
93 // TODO: implement proper error handling
94 DBG_ASSERT( bSuccess, "can't import model" );
95 (void)bSuccess;
98 static Reference<XPropertySet> lcl_findXFormsBindingOrSubmission(
99 Reference<frame::XModel>& xDocument,
100 const OUString& rBindingID,
101 bool bBinding )
103 // find binding by iterating over all models, and look for the
104 // given binding ID
106 Reference<XPropertySet> xRet;
109 // get supplier
110 Reference<XFormsSupplier> xSupplier( xDocument, UNO_QUERY );
111 if( xSupplier.is() )
113 // get XForms models
114 Reference<XNameContainer> xForms = xSupplier->getXForms();
115 if( xForms.is() )
117 // iterate over all models
118 Sequence<OUString> aNames = xForms->getElementNames();
119 const OUString* pNames = aNames.getConstArray();
120 sal_Int32 nNames = aNames.getLength();
121 for( sal_Int32 n = 0; (n < nNames) && !xRet.is(); n++ )
123 Reference<xforms::XModel2> xModel(
124 xForms->getByName( pNames[n] ), UNO_QUERY );
125 if( xModel.is() )
127 // ask model for bindings
128 Reference<XNameAccess> xBindings(
129 bBinding
130 ? xModel->getBindings()
131 : xModel->getSubmissions(),
132 UNO_QUERY_THROW );
134 // finally, ask binding for name
135 if( xBindings->hasByName( rBindingID ) )
136 xRet.set( xBindings->getByName( rBindingID ),
137 UNO_QUERY );
143 catch( const Exception& )
145 ; // no success!
148 // TODO: if (!xRet.is()) rImport.SetError(...);
150 return xRet;
153 Reference<XPropertySet> xforms_findXFormsBinding(
154 Reference<frame::XModel>& xDocument,
155 const OUString& rBindingID )
157 return lcl_findXFormsBindingOrSubmission( xDocument, rBindingID, true );
160 Reference<XPropertySet> xforms_findXFormsSubmission(
161 Reference<frame::XModel>& xDocument,
162 const OUString& rBindingID )
164 return lcl_findXFormsBindingOrSubmission( xDocument, rBindingID, false );
167 void xforms_setValue( Reference<XPropertySet>& xPropertySet,
168 const OUString& rName,
169 const Any rAny )
171 xPropertySet->setPropertyValue( rName, rAny );
174 #define TOKEN_MAP_ENTRY(NAMESPACE,TOKEN) { XML_NAMESPACE_##NAMESPACE, xmloff::token::XML_##TOKEN, xmloff::token::XML_##TOKEN }
175 static SvXMLTokenMapEntry aTypes[] =
177 TOKEN_MAP_ENTRY( XSD, STRING ),
178 TOKEN_MAP_ENTRY( XSD, DECIMAL ),
179 TOKEN_MAP_ENTRY( XSD, DOUBLE ),
180 TOKEN_MAP_ENTRY( XSD, FLOAT ),
181 TOKEN_MAP_ENTRY( XSD, BOOLEAN ),
182 TOKEN_MAP_ENTRY( XSD, ANYURI ),
183 TOKEN_MAP_ENTRY( XSD, DATETIME_XSD ),
184 TOKEN_MAP_ENTRY( XSD, DATE ),
185 TOKEN_MAP_ENTRY( XSD, TIME ),
186 TOKEN_MAP_ENTRY( XSD, YEAR ),
187 TOKEN_MAP_ENTRY( XSD, MONTH ),
188 TOKEN_MAP_ENTRY( XSD, DAY ),
189 XML_TOKEN_MAP_END
192 sal_uInt16 xforms_getTypeClass(
193 const Reference<XDataTypeRepository>&
194 #ifdef DBG_UTIL
195 xRepository
196 #endif
198 const SvXMLNamespaceMap& rNamespaceMap,
199 const OUString& rXMLName )
201 // translate name into token for local name
202 OUString sLocalName;
203 sal_uInt16 nPrefix = rNamespaceMap.GetKeyByAttrName(rXMLName, &sLocalName);
204 SvXMLTokenMap aMap( aTypes );
205 sal_uInt16 mnToken = aMap.Get( nPrefix, sLocalName );
207 sal_uInt16 nTypeClass = com::sun::star::xsd::DataTypeClass::STRING;
208 if( mnToken != XML_TOK_UNKNOWN )
210 // we found an XSD name: then get the proper API name for it
211 DBG_ASSERT( xRepository.is(), "can't find type without repository");
212 switch( mnToken )
214 case XML_STRING:
215 nTypeClass = com::sun::star::xsd::DataTypeClass::STRING;
216 break;
217 case XML_ANYURI:
218 nTypeClass = com::sun::star::xsd::DataTypeClass::anyURI;
219 break;
220 case XML_DECIMAL:
221 nTypeClass = com::sun::star::xsd::DataTypeClass::DECIMAL;
222 break;
223 case XML_DOUBLE:
224 nTypeClass = com::sun::star::xsd::DataTypeClass::DOUBLE;
225 break;
226 case XML_FLOAT:
227 nTypeClass = com::sun::star::xsd::DataTypeClass::FLOAT;
228 break;
229 case XML_BOOLEAN:
230 nTypeClass = com::sun::star::xsd::DataTypeClass::BOOLEAN;
231 break;
232 case XML_DATETIME_XSD:
233 nTypeClass = com::sun::star::xsd::DataTypeClass::DATETIME;
234 break;
235 case XML_DATE:
236 nTypeClass = com::sun::star::xsd::DataTypeClass::DATE;
237 break;
238 case XML_TIME:
239 nTypeClass = com::sun::star::xsd::DataTypeClass::TIME;
240 break;
241 case XML_YEAR:
242 nTypeClass = com::sun::star::xsd::DataTypeClass::gYear;
243 break;
244 case XML_DAY:
245 nTypeClass = com::sun::star::xsd::DataTypeClass::gDay;
246 break;
247 case XML_MONTH:
248 nTypeClass = com::sun::star::xsd::DataTypeClass::gMonth;
249 break;
251 /* data types not yet supported:
252 nTypeClass = com::sun::star::xsd::DataTypeClass::DURATION;
253 nTypeClass = com::sun::star::xsd::DataTypeClass::gYearMonth;
254 nTypeClass = com::sun::star::xsd::DataTypeClass::gMonthDay;
255 nTypeClass = com::sun::star::xsd::DataTypeClass::hexBinary;
256 nTypeClass = com::sun::star::xsd::DataTypeClass::base64Binary;
257 nTypeClass = com::sun::star::xsd::DataTypeClass::QName;
258 nTypeClass = com::sun::star::xsd::DataTypeClass::NOTATION;
263 return nTypeClass;
267 OUString xforms_getTypeName(
268 const Reference<XDataTypeRepository>& xRepository,
269 const SvXMLNamespaceMap& rNamespaceMap,
270 const OUString& rXMLName )
272 OUString sLocalName;
273 sal_uInt16 nPrefix = rNamespaceMap.GetKeyByAttrName(rXMLName, &sLocalName);
274 SvXMLTokenMap aMap( aTypes );
275 sal_uInt16 mnToken = aMap.Get( nPrefix, sLocalName );
276 return ( mnToken == XML_TOK_UNKNOWN )
277 ? rXMLName
278 : xforms_getBasicTypeName( xRepository, rNamespaceMap, rXMLName );
281 OUString xforms_getBasicTypeName(
282 const Reference<XDataTypeRepository>& xRepository,
283 const SvXMLNamespaceMap& rNamespaceMap,
284 const OUString& rXMLName )
286 OUString sTypeName = rXMLName;
289 sTypeName =
290 xRepository->getBasicDataType(
291 xforms_getTypeClass( xRepository, rNamespaceMap, rXMLName ) )
292 ->getName();
294 catch( const Exception& )
296 OSL_FAIL( "exception during type creation" );
298 return sTypeName;
301 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */