LibreOffice Module xmloff (master) 1
xformsapi.cxx
Go to the documentation of this file.
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 "xformsapi.hxx"
22
23#include <com/sun/star/frame/XModel.hpp>
24#include <com/sun/star/uno/Reference.hxx>
25#include <com/sun/star/beans/XPropertySet.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>
33
35#include <sal/log.hxx>
37
38#include <xmloff/xmltoken.hxx>
41#include <xmloff/xmltkmap.hxx>
42
45using com::sun::star::uno::UNO_QUERY;
46using com::sun::star::uno::UNO_QUERY_THROW;
48using com::sun::star::container::XNameAccess;
49using com::sun::star::xforms::XFormsSupplier;
50using com::sun::star::xforms::XDataTypeRepository;
51using com::sun::star::xforms::Model;
52using com::sun::star::xforms::XModel2;
53using com::sun::star::container::XNameContainer;
54using com::sun::star::uno::Any;
55using com::sun::star::uno::Exception;
56
57using namespace com::sun::star;
58using namespace xmloff::token;
59
61{
63
64 return xModel;
65}
66
68 const Reference<frame::XModel>& xDocument,
69 const Reference<xforms::XModel2>& xModel )
70{
71 bool bSuccess = false;
72 try
73 {
74 Reference<XFormsSupplier> xSupplier( xDocument, UNO_QUERY );
75 if( xSupplier.is() )
76 {
77 Reference<XNameContainer> xForms = xSupplier->getXForms();
78 if( xForms.is() )
79 {
80 OUString sName;
81 xModel->getPropertyValue("ID") >>= sName;
82 xForms->insertByName( sName, Any( xModel ) );
83 bSuccess = true;
84 }
85 }
86 }
87 catch( const Exception& )
88 {
89 ; // no success!
90 }
91
92 // TODO: implement proper error handling
93 SAL_WARN_IF( !bSuccess, "xmloff", "can't import model" );
94}
95
97 Reference<frame::XModel> const & xDocument,
98 const OUString& rBindingID,
99 bool bBinding )
100{
101 // find binding by iterating over all models, and look for the
102 // given binding ID
103
105 try
106 {
107 // get supplier
108 Reference<XFormsSupplier> xSupplier( xDocument, UNO_QUERY );
109 if( xSupplier.is() )
110 {
111 // get XForms models
112 Reference<XNameContainer> xForms = xSupplier->getXForms();
113 if( xForms.is() )
114 {
115 // iterate over all models
116 const Sequence<OUString> aNames = xForms->getElementNames();
117 for( const auto& rName : aNames )
118 {
120 xForms->getByName( rName ), UNO_QUERY );
121 if( xModel.is() )
122 {
123 // ask model for bindings
124 Reference<XNameAccess> xBindings(
125 bBinding
126 ? xModel->getBindings()
127 : xModel->getSubmissions(),
128 UNO_QUERY_THROW );
129
130 // finally, ask binding for name
131 if( xBindings->hasByName( rBindingID ) )
132 xRet.set( xBindings->getByName( rBindingID ),
133 UNO_QUERY );
134 }
135
136 if (xRet.is())
137 break;
138 }
139 }
140 }
141 }
142 catch( const Exception& )
143 {
144 ; // no success!
145 }
146
147 // TODO: if (!xRet.is()) rImport.SetError(...);
148
149 return xRet;
150}
151
153 Reference<frame::XModel> const & xDocument,
154 const OUString& rBindingID )
155{
156 return lcl_findXFormsBindingOrSubmission( xDocument, rBindingID, true );
157}
158
160 Reference<frame::XModel> const & xDocument,
161 const OUString& rBindingID )
162{
163 return lcl_findXFormsBindingOrSubmission( xDocument, rBindingID, false );
164}
165
167 const OUString& rName,
168 const Any& rAny )
169{
170 xPropertySet->setPropertyValue( rName, rAny );
171}
172
174{
188};
189
191 const Reference<XDataTypeRepository>& xRepository,
192 const SvXMLNamespaceMap& rNamespaceMap,
193 const OUString& rXMLName )
194{
195 // translate name into token for local name
196 OUString sLocalName;
197 sal_uInt16 nPrefix = rNamespaceMap.GetKeyByAttrValueQName(rXMLName, &sLocalName);
198 static const SvXMLTokenMap aMap( aTypes );
199 sal_uInt16 nToken = aMap.Get( nPrefix, sLocalName );
200
201 sal_uInt16 nTypeClass = css::xsd::DataTypeClass::STRING;
202 if( nToken != XML_TOK_UNKNOWN )
203 {
204 // we found an XSD name: then get the proper API name for it
205 SAL_WARN_IF( !xRepository.is(), "xmloff", "can't find type without repository");
206 switch( nToken )
207 {
208 case XML_STRING:
209 nTypeClass = css::xsd::DataTypeClass::STRING;
210 break;
211 case XML_ANYURI:
212 nTypeClass = css::xsd::DataTypeClass::anyURI;
213 break;
214 case XML_DECIMAL:
215 nTypeClass = css::xsd::DataTypeClass::DECIMAL;
216 break;
217 case XML_DOUBLE:
218 nTypeClass = css::xsd::DataTypeClass::DOUBLE;
219 break;
220 case XML_FLOAT:
221 nTypeClass = css::xsd::DataTypeClass::FLOAT;
222 break;
223 case XML_BOOLEAN:
224 nTypeClass = css::xsd::DataTypeClass::BOOLEAN;
225 break;
226 case XML_DATETIME_XSD:
227 nTypeClass = css::xsd::DataTypeClass::DATETIME;
228 break;
229 case XML_DATE:
230 nTypeClass = css::xsd::DataTypeClass::DATE;
231 break;
232 case XML_TIME:
233 nTypeClass = css::xsd::DataTypeClass::TIME;
234 break;
235 case XML_YEAR:
236 nTypeClass = css::xsd::DataTypeClass::gYear;
237 break;
238 case XML_DAY:
239 nTypeClass = css::xsd::DataTypeClass::gDay;
240 break;
241 case XML_MONTH:
242 nTypeClass = css::xsd::DataTypeClass::gMonth;
243 break;
244
245 /* data types not yet supported:
246 nTypeClass = css::xsd::DataTypeClass::DURATION;
247 nTypeClass = css::xsd::DataTypeClass::gYearMonth;
248 nTypeClass = css::xsd::DataTypeClass::gMonthDay;
249 nTypeClass = css::xsd::DataTypeClass::hexBinary;
250 nTypeClass = css::xsd::DataTypeClass::base64Binary;
251 nTypeClass = css::xsd::DataTypeClass::QName;
252 nTypeClass = css::xsd::DataTypeClass::NOTATION;
253 */
254 }
255 }
256
257 return nTypeClass;
258}
259
260
262 const Reference<XDataTypeRepository>& xRepository,
263 const SvXMLNamespaceMap& rNamespaceMap,
264 const OUString& rXMLName )
265{
266 OUString sLocalName;
267 sal_uInt16 nPrefix = rNamespaceMap.GetKeyByAttrValueQName(rXMLName, &sLocalName);
268 static const SvXMLTokenMap aMap( aTypes );
269 sal_uInt16 nToken = aMap.Get( nPrefix, sLocalName );
270 return ( nToken == XML_TOK_UNKNOWN )
271 ? rXMLName
272 : xforms_getBasicTypeName( xRepository, rNamespaceMap, rXMLName );
273}
274
276 const Reference<XDataTypeRepository>& xRepository,
277 const SvXMLNamespaceMap& rNamespaceMap,
278 const OUString& rXMLName )
279{
280 OUString sTypeName = rXMLName;
281 try
282 {
283 sTypeName =
284 xRepository->getBasicDataType(
285 xforms_getTypeClass( xRepository, rNamespaceMap, rXMLName ) )
286 ->getName();
287 }
288 catch( const Exception& )
289 {
290 TOOLS_WARN_EXCEPTION("xmloff", "exception during type creation");
291 }
292 return sTypeName;
293}
294
295/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt16 GetKeyByAttrValueQName(const OUString &rAttrName, OUString *pLocalName) const
#define TOOLS_WARN_EXCEPTION(area, stream)
OUString sName
#define SAL_WARN_IF(condition, area, stream)
@ Exception
class SAL_NO_VTABLE XPropertySet
Reference< XComponentContext > getProcessComponentContext()
Handling of tokens in XML:
HashMap_OWString_Interface aMap
DefTokenId nToken
Reference< XModel > xModel
void xforms_setValueAny(Reference< XPropertySet > const &xPropertySet, const OUString &rName, const Any &rAny)
Definition: xformsapi.cxx:166
static Reference< XPropertySet > lcl_findXFormsBindingOrSubmission(Reference< frame::XModel > const &xDocument, const OUString &rBindingID, bool bBinding)
Definition: xformsapi.cxx:96
Reference< XModel2 > xforms_createXFormsModel()
Definition: xformsapi.cxx:60
void xforms_addXFormsModel(const Reference< frame::XModel > &xDocument, const Reference< xforms::XModel2 > &xModel)
Definition: xformsapi.cxx:67
OUString xforms_getTypeName(const Reference< XDataTypeRepository > &xRepository, const SvXMLNamespaceMap &rNamespaceMap, const OUString &rXMLName)
Definition: xformsapi.cxx:261
const SvXMLTokenMapEntry aTypes[]
Definition: xformsapi.cxx:173
sal_uInt16 xforms_getTypeClass(const Reference< XDataTypeRepository > &xRepository, const SvXMLNamespaceMap &rNamespaceMap, const OUString &rXMLName)
Definition: xformsapi.cxx:190
OUString xforms_getBasicTypeName(const Reference< XDataTypeRepository > &xRepository, const SvXMLNamespaceMap &rNamespaceMap, const OUString &rXMLName)
Definition: xformsapi.cxx:275
Reference< XPropertySet > xforms_findXFormsSubmission(Reference< frame::XModel > const &xDocument, const OUString &rBindingID)
Definition: xformsapi.cxx:159
Reference< XPropertySet > xforms_findXFormsBinding(Reference< frame::XModel > const &xDocument, const OUString &rBindingID)
Definition: xformsapi.cxx:152
constexpr sal_uInt16 XML_NAMESPACE_XSD
#define XML_TOKEN_MAP_END
Definition: xmltkmap.hxx:33
#define XML_TOK_UNKNOWN
Definition: xmltkmap.hxx:32