Debian Codegraph @

xmloff/source/xforms/xformsapi.cxx

raw
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
34 #include <comphelper/processfactory.hxx>
35 #include <sal/log.hxx>
36 #include <comphelper/diagnose_ex.hxx>
37
38 #include <xmloff/xmltoken.hxx>
39 #include <xmloff/namespacemap.hxx>
40 #include <xmloff/xmlnamespace.hxx>
41 #include <xmloff/xmltkmap.hxx>
42
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::Any;
55 using com::sun::star::uno::Exception;
56
57 using namespace com::sun::star;
58 using namespace xmloff::token;
59
60 Reference<XModel2> xforms_createXFormsModel()
61 {
62 Reference<XModel2> xModel = Model::create( comphelper::getProcessComponentContext() );
63
64 return xModel;
65 }
66
67 void xforms_addXFormsModel(
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(u"ID"_ustr) >>= 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
96 static Reference<XPropertySet> lcl_findXFormsBindingOrSubmission(
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
104 Reference<XPropertySet> xRet;
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 {
119 Reference<xforms::XModel2> xModel(
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
152 Reference<XPropertySet> xforms_findXFormsBinding(
153 Reference<frame::XModel> const & xDocument,
154 const OUString& rBindingID )
155 {
156 return lcl_findXFormsBindingOrSubmission( xDocument, rBindingID, true );
157 }
158
159 Reference<XPropertySet> xforms_findXFormsSubmission(
160 Reference<frame::XModel> const & xDocument,
161 const OUString& rBindingID )
162 {
163 return lcl_findXFormsBindingOrSubmission( xDocument, rBindingID, false );
164 }
165
166 void xforms_setValueAny( Reference<XPropertySet> const & xPropertySet,
167 const OUString& rName,
168 const Any& rAny )
169 {
170 xPropertySet->setPropertyValue( rName, rAny );
171 }
172
173 const SvXMLTokenMapEntry aTypes[] =
174 {
175 { XML_NAMESPACE_XSD, xmloff::token::XML_STRING, xmloff::token::XML_STRING },
176 { XML_NAMESPACE_XSD, xmloff::token::XML_DECIMAL, xmloff::token::XML_DECIMAL },
177 { XML_NAMESPACE_XSD, xmloff::token::XML_DOUBLE, xmloff::token::XML_DOUBLE },
178 { XML_NAMESPACE_XSD, xmloff::token::XML_FLOAT, xmloff::token::XML_FLOAT },
179 { XML_NAMESPACE_XSD, xmloff::token::XML_BOOLEAN, xmloff::token::XML_BOOLEAN },
180 { XML_NAMESPACE_XSD, xmloff::token::XML_ANYURI, xmloff::token::XML_ANYURI },
181 { XML_NAMESPACE_XSD, xmloff::token::XML_DATETIME_XSD, xmloff::token::XML_DATETIME_XSD },
182 { XML_NAMESPACE_XSD, xmloff::token::XML_DATE, xmloff::token::XML_DATE },
183 { XML_NAMESPACE_XSD, xmloff::token::XML_TIME, xmloff::token::XML_TIME },
184 { XML_NAMESPACE_XSD, xmloff::token::XML_YEAR, xmloff::token::XML_YEAR },
185 { XML_NAMESPACE_XSD, xmloff::token::XML_MONTH, xmloff::token::XML_MONTH },
186 { XML_NAMESPACE_XSD, xmloff::token::XML_DAY, xmloff::token::XML_DAY },
187 XML_TOKEN_MAP_END
188 };
189
190 sal_uInt16 xforms_getTypeClass(
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
261 OUString xforms_getTypeName(
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
275 OUString xforms_getBasicTypeName(
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: */