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 .
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() );
68 void xforms_addXFormsModel(
69 const Reference
<frame::XModel
>& xDocument
,
70 const Reference
<xforms::XModel2
>& xModel
)
72 bool bSuccess
= false;
75 Reference
<XFormsSupplier
> xSupplier( xDocument
, UNO_QUERY
);
78 Reference
<XNameContainer
> xForms
= xSupplier
->getXForms();
82 xModel
->getPropertyValue("ID") >>= sName
;
83 xForms
->insertByName( sName
, makeAny( xModel
) );
88 catch( const Exception
& )
93 // TODO: implement proper error handling
94 DBG_ASSERT( bSuccess
, "can't import model" );
98 static Reference
<XPropertySet
> lcl_findXFormsBindingOrSubmission(
99 Reference
<frame::XModel
>& xDocument
,
100 const OUString
& rBindingID
,
103 // find binding by iterating over all models, and look for the
106 Reference
<XPropertySet
> xRet
;
110 Reference
<XFormsSupplier
> xSupplier( xDocument
, UNO_QUERY
);
114 Reference
<XNameContainer
> xForms
= xSupplier
->getXForms();
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
);
127 // ask model for bindings
128 Reference
<XNameAccess
> xBindings(
130 ? xModel
->getBindings()
131 : xModel
->getSubmissions(),
134 // finally, ask binding for name
135 if( xBindings
->hasByName( rBindingID
) )
136 xRet
.set( xBindings
->getByName( rBindingID
),
143 catch( const Exception
& )
148 // TODO: if (!xRet.is()) rImport.SetError(...);
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
,
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
),
192 sal_uInt16
xforms_getTypeClass(
193 const Reference
<XDataTypeRepository
>&
198 const SvXMLNamespaceMap
& rNamespaceMap
,
199 const OUString
& rXMLName
)
201 // translate name into token for local name
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");
215 nTypeClass
= com::sun::star::xsd::DataTypeClass::STRING
;
218 nTypeClass
= com::sun::star::xsd::DataTypeClass::anyURI
;
221 nTypeClass
= com::sun::star::xsd::DataTypeClass::DECIMAL
;
224 nTypeClass
= com::sun::star::xsd::DataTypeClass::DOUBLE
;
227 nTypeClass
= com::sun::star::xsd::DataTypeClass::FLOAT
;
230 nTypeClass
= com::sun::star::xsd::DataTypeClass::BOOLEAN
;
232 case XML_DATETIME_XSD
:
233 nTypeClass
= com::sun::star::xsd::DataTypeClass::DATETIME
;
236 nTypeClass
= com::sun::star::xsd::DataTypeClass::DATE
;
239 nTypeClass
= com::sun::star::xsd::DataTypeClass::TIME
;
242 nTypeClass
= com::sun::star::xsd::DataTypeClass::gYear
;
245 nTypeClass
= com::sun::star::xsd::DataTypeClass::gDay
;
248 nTypeClass
= com::sun::star::xsd::DataTypeClass::gMonth
;
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;
267 OUString
xforms_getTypeName(
268 const Reference
<XDataTypeRepository
>& xRepository
,
269 const SvXMLNamespaceMap
& rNamespaceMap
,
270 const OUString
& rXMLName
)
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
)
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
;
290 xRepository
->getBasicDataType(
291 xforms_getTypeClass( xRepository
, rNamespaceMap
, rXMLName
) )
294 catch( const Exception
& )
296 OSL_FAIL( "exception during type creation" );
301 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */