nss: upgrade to release 3.73
[LibreOffice.git] / xmloff / source / xforms / xformsapi.cxx
blob3fbccbec4f75887ce400c3cc92357f32ebc60fd4
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/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>
34 #include <comphelper/processfactory.hxx>
35 #include <sal/log.hxx>
36 #include <tools/diagnose_ex.h>
38 #include <xmloff/xmltoken.hxx>
39 #include <xmloff/namespacemap.hxx>
40 #include <xmloff/xmlnamespace.hxx>
41 #include <xmloff/xmltkmap.hxx>
43 using com::sun::star::uno::Reference;
44 using com::sun::star::uno::Sequence;
45 using com::sun::star::uno::UNO_QUERY;
46 using com::sun::star::uno::UNO_QUERY_THROW;
47 using com::sun::star::beans::XPropertySet;
48 using com::sun::star::container::XNameAccess;
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 SAL_WARN_IF( !bSuccess, "xmloff", "can't import model" );
97 static Reference<XPropertySet> lcl_findXFormsBindingOrSubmission(
98 Reference<frame::XModel> const & xDocument,
99 const OUString& rBindingID,
100 bool bBinding )
102 // find binding by iterating over all models, and look for the
103 // given binding ID
105 Reference<XPropertySet> xRet;
108 // get supplier
109 Reference<XFormsSupplier> xSupplier( xDocument, UNO_QUERY );
110 if( xSupplier.is() )
112 // get XForms models
113 Reference<XNameContainer> xForms = xSupplier->getXForms();
114 if( xForms.is() )
116 // iterate over all models
117 const Sequence<OUString> aNames = xForms->getElementNames();
118 for( const auto& rName : aNames )
120 Reference<xforms::XModel2> xModel(
121 xForms->getByName( rName ), UNO_QUERY );
122 if( xModel.is() )
124 // ask model for bindings
125 Reference<XNameAccess> xBindings(
126 bBinding
127 ? xModel->getBindings()
128 : xModel->getSubmissions(),
129 UNO_QUERY_THROW );
131 // finally, ask binding for name
132 if( xBindings->hasByName( rBindingID ) )
133 xRet.set( xBindings->getByName( rBindingID ),
134 UNO_QUERY );
137 if (xRet.is())
138 break;
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> const & xDocument,
155 const OUString& rBindingID )
157 return lcl_findXFormsBindingOrSubmission( xDocument, rBindingID, true );
160 Reference<XPropertySet> xforms_findXFormsSubmission(
161 Reference<frame::XModel> const & xDocument,
162 const OUString& rBindingID )
164 return lcl_findXFormsBindingOrSubmission( xDocument, rBindingID, false );
167 void xforms_setValue( Reference<XPropertySet> const & 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 const 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>& xRepository,
194 const SvXMLNamespaceMap& rNamespaceMap,
195 const OUString& rXMLName )
197 // translate name into token for local name
198 OUString sLocalName;
199 sal_uInt16 nPrefix = rNamespaceMap.GetKeyByAttrValueQName(rXMLName, &sLocalName);
200 static const SvXMLTokenMap aMap( aTypes );
201 sal_uInt16 nToken = aMap.Get( nPrefix, sLocalName );
203 sal_uInt16 nTypeClass = css::xsd::DataTypeClass::STRING;
204 if( nToken != XML_TOK_UNKNOWN )
206 // we found an XSD name: then get the proper API name for it
207 SAL_WARN_IF( !xRepository.is(), "xmloff", "can't find type without repository");
208 switch( nToken )
210 case XML_STRING:
211 nTypeClass = css::xsd::DataTypeClass::STRING;
212 break;
213 case XML_ANYURI:
214 nTypeClass = css::xsd::DataTypeClass::anyURI;
215 break;
216 case XML_DECIMAL:
217 nTypeClass = css::xsd::DataTypeClass::DECIMAL;
218 break;
219 case XML_DOUBLE:
220 nTypeClass = css::xsd::DataTypeClass::DOUBLE;
221 break;
222 case XML_FLOAT:
223 nTypeClass = css::xsd::DataTypeClass::FLOAT;
224 break;
225 case XML_BOOLEAN:
226 nTypeClass = css::xsd::DataTypeClass::BOOLEAN;
227 break;
228 case XML_DATETIME_XSD:
229 nTypeClass = css::xsd::DataTypeClass::DATETIME;
230 break;
231 case XML_DATE:
232 nTypeClass = css::xsd::DataTypeClass::DATE;
233 break;
234 case XML_TIME:
235 nTypeClass = css::xsd::DataTypeClass::TIME;
236 break;
237 case XML_YEAR:
238 nTypeClass = css::xsd::DataTypeClass::gYear;
239 break;
240 case XML_DAY:
241 nTypeClass = css::xsd::DataTypeClass::gDay;
242 break;
243 case XML_MONTH:
244 nTypeClass = css::xsd::DataTypeClass::gMonth;
245 break;
247 /* data types not yet supported:
248 nTypeClass = css::xsd::DataTypeClass::DURATION;
249 nTypeClass = css::xsd::DataTypeClass::gYearMonth;
250 nTypeClass = css::xsd::DataTypeClass::gMonthDay;
251 nTypeClass = css::xsd::DataTypeClass::hexBinary;
252 nTypeClass = css::xsd::DataTypeClass::base64Binary;
253 nTypeClass = css::xsd::DataTypeClass::QName;
254 nTypeClass = css::xsd::DataTypeClass::NOTATION;
259 return nTypeClass;
263 OUString xforms_getTypeName(
264 const Reference<XDataTypeRepository>& xRepository,
265 const SvXMLNamespaceMap& rNamespaceMap,
266 const OUString& rXMLName )
268 OUString sLocalName;
269 sal_uInt16 nPrefix = rNamespaceMap.GetKeyByAttrValueQName(rXMLName, &sLocalName);
270 static const SvXMLTokenMap aMap( aTypes );
271 sal_uInt16 nToken = aMap.Get( nPrefix, sLocalName );
272 return ( nToken == XML_TOK_UNKNOWN )
273 ? rXMLName
274 : xforms_getBasicTypeName( xRepository, rNamespaceMap, rXMLName );
277 OUString xforms_getBasicTypeName(
278 const Reference<XDataTypeRepository>& xRepository,
279 const SvXMLNamespaceMap& rNamespaceMap,
280 const OUString& rXMLName )
282 OUString sTypeName = rXMLName;
285 sTypeName =
286 xRepository->getBasicDataType(
287 xforms_getTypeClass( xRepository, rNamespaceMap, rXMLName ) )
288 ->getName();
290 catch( const Exception& )
292 TOOLS_WARN_EXCEPTION("xmloff", "exception during type creation");
294 return sTypeName;
297 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */