Debian Codegraph @

xmloff/source/xforms/xformsexport.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 <xmloff/xformsexport.hxx>
22
23 #include "XFormsModelExport.hxx"
24
25 #include <xmloff/xmlexp.hxx>
26 #include <xmloff/xmltoken.hxx>
27 #include <xmloff/xmlnamespace.hxx>
28 #include <xmloff/namespacemap.hxx>
29 #include <DomExport.hxx>
30
31 #include <sax/tools/converter.hxx>
32
33 #include <comphelper/processfactory.hxx>
34 #include <comphelper/propertyvalue.hxx>
35
36 #include <comphelper/diagnose_ex.hxx>
37 #include <sal/log.hxx>
38 #include <com/sun/star/container/XIndexAccess.hpp>
39 #include <com/sun/star/container/XNameAccess.hpp>
40 #include <com/sun/star/document/NamedPropertyValues.hpp>
41 #include <com/sun/star/frame/XModel.hpp>
42 #include <com/sun/star/xml/dom/XDocument.hpp>
43 #include <com/sun/star/form/binding/XBindableValue.hpp>
44 #include <com/sun/star/form/binding/XListEntrySink.hpp>
45 #include <com/sun/star/form/submission/XSubmissionSupplier.hpp>
46 #include <com/sun/star/xforms/XModel.hpp>
47 #include <com/sun/star/xforms/XDataTypeRepository.hpp>
48 #include <com/sun/star/xforms/XFormsSupplier.hpp>
49 #include <com/sun/star/beans/PropertyValue.hpp>
50 #include <com/sun/star/container/XEnumerationAccess.hpp>
51 #include <com/sun/star/container/XEnumeration.hpp>
52 #include <com/sun/star/container/XNameContainer.hpp>
53 #include <com/sun/star/xsd/WhiteSpaceTreatment.hpp>
54 #include <com/sun/star/xsd/DataTypeClass.hpp>
55 #include <com/sun/star/util/Date.hpp>
56 #include <com/sun/star/util/Time.hpp>
57 #include <com/sun/star/util/DateTime.hpp>
58 #include <com/sun/star/util/Duration.hpp>
59
60 using namespace com::sun::star;
61 using namespace com::sun::star::uno;
62 using namespace xmloff::token;
63
64 using com::sun::star::beans::XPropertySet;
65 using com::sun::star::beans::XPropertySetInfo;
66 using com::sun::star::container::XIndexAccess;
67 using com::sun::star::container::XNameAccess;
68 using com::sun::star::container::XNameContainer;
69 using com::sun::star::container::XEnumerationAccess;
70 using com::sun::star::container::XEnumeration;
71 using com::sun::star::xml::dom::XDocument;
72 using com::sun::star::form::binding::XBindableValue;
73 using com::sun::star::form::binding::XListEntrySink;
74 using com::sun::star::form::submission::XSubmissionSupplier;
75 using com::sun::star::beans::PropertyValue;
76 using com::sun::star::xforms::XDataTypeRepository;
77 using com::sun::star::xforms::XFormsSupplier;
78 using com::sun::star::util::Duration;
79
80 void exportXForms( SvXMLExport& rExport )
81 {
82 Reference<XFormsSupplier> xSupplier( rExport.GetModel(), UNO_QUERY );
83 if( !xSupplier.is() )
84 return;
85
86 Reference<XNameContainer> xForms = xSupplier->getXForms();
87 if( xForms.is() )
88 {
89 const Sequence<OUString> aNames = xForms->getElementNames();
90
91 for( const auto& rName : aNames )
92 {
93 Reference<XPropertySet> xModel( xForms->getByName( rName ),
94 UNO_QUERY );
95 exportXFormsModel( rExport, xModel );
96 }
97 }
98 }
99
100
101 static void exportXFormsInstance( SvXMLExport&, const Sequence<PropertyValue>& );
102 static void exportXFormsBinding( SvXMLExport&, const Reference<XPropertySet>& );
103 static void exportXFormsSubmission( SvXMLExport&, const Reference<XPropertySet>& );
104 static void exportXFormsSchemas( SvXMLExport&, const Reference<css::xforms::XModel>& );
105
106
107 typedef OUString (*convert_t)( const Any& );
108
109 namespace {
110
111 struct ExportTable
112 {
113 OUString pPropertyName;
114 sal_uInt16 nNamespace;
115 sal_uInt16 nToken;
116 convert_t aConverter;
117 };
118
119 }
120
121 static void lcl_export( const Reference<XPropertySet>& rPropertySet,
122 SvXMLExport& rExport,
123 const ExportTable* pTable );
124
125 #define TABLE_END { u""_ustr, 0, 0, nullptr }
126
127 // any conversion functions
128 static OUString xforms_string( const Any& );
129 static OUString xforms_bool( const Any& );
130 static OUString xforms_whitespace( const Any& );
131 template<typename T, void (*FUNC)( OUStringBuffer&, T )> static OUString xforms_convert( const Any& );
132 template<typename T, void (*FUNC)( OUStringBuffer&, const T& )> static OUString xforms_convertRef( const Any& );
133
134 static void xforms_formatDate( OUStringBuffer& aBuffer, const util::Date& aDate );
135 static void xforms_formatTime( OUStringBuffer& aBuffer, const css::util::Time& aTime );
136 static void xforms_formatDateTime( OUStringBuffer& aBuffer, const util::DateTime& aDateTime );
137
138 static void convertNumber(OUStringBuffer & b, sal_Int32 n) {
139 b.append(n);
140 }
141
142 constexpr convert_t xforms_int32 = &xforms_convert<sal_Int32,&convertNumber>;
143 constexpr convert_t xforms_double = &xforms_convert<double,&::sax::Converter::convertDouble>;
144 constexpr convert_t xforms_dateTime = &xforms_convertRef<util::DateTime,&xforms_formatDateTime>;
145 constexpr convert_t xforms_date = &xforms_convertRef<util::Date,&xforms_formatDate>;
146 constexpr convert_t xforms_time = &xforms_convertRef<css::util::Time,&xforms_formatTime>;
147
148 // other functions
149 static OUString lcl_getXSDType( SvXMLExport const & rExport,
150 const Reference<XPropertySet>& xType );
151
152
153 // the model
154
155
156 constexpr ExportTable aXFormsModelTable[] =
157 {
158 { u"ID"_ustr, XML_NAMESPACE_NONE, xmloff::token::XML_ID, xforms_string },
159 { u"SchemaRef"_ustr, XML_NAMESPACE_NONE, xmloff::token::XML_SCHEMA, xforms_string },
160 TABLE_END
161 };
162
163 void exportXFormsModel( SvXMLExport& rExport,
164 const Reference<XPropertySet>& xModelPropSet )
165 {
166 // no model -> don't do anything!
167 Reference<css::xforms::XModel> xModel( xModelPropSet, UNO_QUERY );
168 if( ! xModel.is() || ! xModelPropSet.is() )
169 return;
170
171 lcl_export( xModelPropSet, rExport, aXFormsModelTable );
172 SvXMLElementExport aModelElement( rExport, XML_NAMESPACE_XFORMS, XML_MODEL,
173 true, true );
174
175 // instances
176 Reference<XIndexAccess> xInstances( xModel->getInstances(),
177 UNO_QUERY_THROW);
178 sal_Int32 nCount = xInstances->getCount();
179 sal_Int32 i = 0;
180 for( i = 0; i < nCount; i++ )
181 {
182 Sequence<PropertyValue> aInstance;
183 xInstances->getByIndex( i ) >>= aInstance;
184 exportXFormsInstance( rExport, aInstance );
185 }
186
187
188 // bindings
189 Reference<XIndexAccess> xBindings( xModel->getBindings(), UNO_QUERY_THROW);
190 nCount = xBindings->getCount();
191 for( i = 0; i < nCount; i++ )
192 {
193 Reference<XPropertySet> aBinding( xBindings->getByIndex( i ),
194 UNO_QUERY_THROW );
195 exportXFormsBinding( rExport, aBinding );
196 }
197
198 // submissions
199 Reference<XIndexAccess> xSubmissions( xModel->getSubmissions(),
200 UNO_QUERY_THROW );
201 nCount = xSubmissions->getCount();
202 for( i = 0; i < nCount; i++ )
203 {
204 Reference<XPropertySet> xSubmission( xSubmissions->getByIndex( i ),
205 UNO_QUERY_THROW );
206 exportXFormsSubmission( rExport, xSubmission );
207 }
208
209 // schemas
210 exportXFormsSchemas( rExport, xModel );
211 }
212
213
214 // the instance
215
216
217 void exportXFormsInstance( SvXMLExport& rExport,
218 const Sequence<PropertyValue>& xInstance )
219 {
220 OUString sId;
221 OUString sURL;
222 Reference<XDocument> xDoc;
223
224 for( const auto& rProp : xInstance )
225 {
226 OUString sName = rProp.Name;
227 const Any& rAny = rProp.Value;
228 if ( sName == "ID" )
229 rAny >>= sId;
230 else if ( sName == "URL" )
231 rAny >>= sURL;
232 else if ( sName == "Instance" )
233 rAny >>= xDoc;
234 }
235
236 if( !sId.isEmpty() )
237 rExport.AddAttribute( XML_NAMESPACE_NONE, XML_ID, sId );
238
239 if( !sURL.isEmpty() )
240 rExport.AddAttribute( XML_NAMESPACE_NONE, XML_SRC, sURL );
241
242 SvXMLElementExport aElem( rExport, XML_NAMESPACE_XFORMS, XML_INSTANCE,
243 true, true );
244 rExport.IgnorableWhitespace();
245 if( xDoc.is() )
246 {
247 exportDom( rExport, xDoc );
248 }
249 }
250
251
252 // the binding
253
254
255 constexpr ExportTable aXFormsBindingTable[] =
256 {
257 { u"BindingID"_ustr, XML_NAMESPACE_NONE, xmloff::token::XML_ID, xforms_string },
258 { u"BindingExpression"_ustr, XML_NAMESPACE_NONE, xmloff::token::XML_NODESET, xforms_string },
259 { u"ReadonlyExpression"_ustr, XML_NAMESPACE_NONE, xmloff::token::XML_READONLY, xforms_string },
260 { u"RelevantExpression"_ustr, XML_NAMESPACE_NONE, xmloff::token::XML_RELEVANT, xforms_string },
261 { u"RequiredExpression"_ustr, XML_NAMESPACE_NONE, xmloff::token::XML_REQUIRED, xforms_string },
262 { u"ConstraintExpression"_ustr, XML_NAMESPACE_NONE, xmloff::token::XML_CONSTRAINT, xforms_string },
263 { u"CalculateExpression"_ustr, XML_NAMESPACE_NONE, xmloff::token::XML_CALCULATE, xforms_string },
264 // type handled separately, for type name <-> XSD type conversion
265 // { "Type", XML_NAMESPACE_NONE, xmloff::token::XML_TYPE, xforms_string },
266 TABLE_END
267 };
268
269 void exportXFormsBinding( SvXMLExport& rExport,
270 const Reference<XPropertySet>& xBinding )
271 {
272 // name check; generate binding ID if necessary
273 {
274 OUString sName;
275 xBinding->getPropertyValue( u"BindingID"_ustr ) >>= sName;
276 if( sName.isEmpty() )
277 {
278 // if we don't have a name yet, generate one on the fly
279 sal_Int64 nId = reinterpret_cast<sal_uInt64>( xBinding.get() );
280 sName = "bind_" + OUString::number( nId , 16 );
281 xBinding->setPropertyValue( u"BindingID"_ustr, Any(sName));
282 }
283 }
284
285 lcl_export( xBinding, rExport, aXFormsBindingTable );
286
287 // handle type attribute
288 {
289 OUString sTypeName;
290 xBinding->getPropertyValue( u"Type"_ustr ) >>= sTypeName;
291
292 try
293 {
294 // now get type, and determine whether it's a standard type. If
295 // so, export the XSD name
296 Reference<css::xforms::XModel> xModel(
297 xBinding->getPropertyValue( u"Model"_ustr ),
298 UNO_QUERY );
299 Reference<XDataTypeRepository> xRepository(
300 xModel.is() ? xModel->getDataTypeRepository() : Reference<XDataTypeRepository>() );
301 if( xRepository.is() )
302 {
303 Reference<XPropertySet> xDataType =
304 xRepository->getDataType( sTypeName );
305
306 // if it's a basic data type, write out the XSD name
307 // for the XSD type class
308 bool bIsBasic = false;
309 xDataType->getPropertyValue( u"IsBasic"_ustr ) >>= bIsBasic;
310 if( bIsBasic )
311 sTypeName = lcl_getXSDType( rExport, xDataType );
312 }
313 }
314 catch( Exception& )
315 {
316 ; // ignore; just use typename
317 }
318
319 // now that we have the proper type name, write out the attribute
320 if( !sTypeName.isEmpty() )
321 {
322 rExport.AddAttribute( XML_NAMESPACE_NONE, XML_TYPE,
323 sTypeName );
324 }
325 }
326
327 // we need to ensure all the namespaces in the binding will work correctly.
328 // to do so, we will write out all missing namespace declarations.
329 const SvXMLNamespaceMap& rMap = rExport.GetNamespaceMap();
330 Reference<XNameAccess> xNamespaces(
331 xBinding->getPropertyValue( u"ModelNamespaces"_ustr ), UNO_QUERY);
332 if( xNamespaces.is() )
333 {
334 // iterate over Prefixes for this binding
335 const Sequence<OUString> aPrefixes = xNamespaces->getElementNames();
336 for( const OUString& rPrefix : aPrefixes )
337 {
338 OUString sURI;
339 xNamespaces->getByName( rPrefix ) >>= sURI;
340
341 // check whether prefix/URI pair is in map; else write declaration
342 // (we don't need to change the map, since this element has no
343 // other content)
344 sal_uInt16 nKey = rMap.GetKeyByPrefix( rPrefix );
345 if( nKey == XML_NAMESPACE_UNKNOWN ||
346 rMap.GetNameByKey( nKey ) != sURI )
347 {
348 // add declaration if it doesn't already exist
349 comphelper::AttributeList& rAttrList = rExport.GetAttrList();
350 OUString sName = "xmlns:" + rPrefix;
351 sal_Int16 nFound = rAttrList.GetIndexByName(sName);
352 // duplicate xmlns:script, http://openoffice.org/2000/script seen
353 assert(nFound == -1 || rAttrList.getValueByIndex(nFound) == sURI);
354 if (nFound != -1)
355 continue;
356 rAttrList.AddAttribute(sName, sURI);
357 }
358 }
359 }
360
361 SvXMLElementExport aElement( rExport, XML_NAMESPACE_XFORMS, XML_BIND,
362 true, true );
363 }
364
365
366 // the submission
367
368
369 constexpr ExportTable aXFormsSubmissionTable[] =
370 {
371 { u"ID"_ustr, XML_NAMESPACE_NONE, xmloff::token::XML_ID, xforms_string },
372 { u"Bind"_ustr, XML_NAMESPACE_NONE, xmloff::token::XML_BIND, xforms_string },
373 { u"Ref"_ustr, XML_NAMESPACE_NONE, xmloff::token::XML_REF, xforms_string },
374 { u"Action"_ustr, XML_NAMESPACE_NONE, xmloff::token::XML_ACTION, xforms_string },
375 { u"Method"_ustr, XML_NAMESPACE_NONE, xmloff::token::XML_METHOD, xforms_string },
376 { u"Version"_ustr, XML_NAMESPACE_NONE, xmloff::token::XML_VERSION, xforms_string },
377 { u"Indent"_ustr, XML_NAMESPACE_NONE, xmloff::token::XML_INDENT, xforms_bool },
378 { u"MediaType"_ustr, XML_NAMESPACE_NONE, xmloff::token::XML_MEDIATYPE, xforms_string },
379 { u"Encoding"_ustr, XML_NAMESPACE_NONE, xmloff::token::XML_ENCODING, xforms_string },
380 { u"OmitXmlDeclaration"_ustr, XML_NAMESPACE_NONE, xmloff::token::XML_OMIT_XML_DECLARATION, xforms_bool },
381 { u"Standalone"_ustr, XML_NAMESPACE_NONE, xmloff::token::XML_STANDALONE, xforms_bool },
382 { u"CDataSectionElement"_ustr, XML_NAMESPACE_NONE, xmloff::token::XML_CDATA_SECTION_ELEMENTS, xforms_string },
383 { u"Replace"_ustr, XML_NAMESPACE_NONE, xmloff::token::XML_REPLACE, xforms_string },
384 { u"Separator"_ustr, XML_NAMESPACE_NONE, xmloff::token::XML_SEPARATOR, xforms_string },
385 { u"IncludeNamespacePrefixes"_ustr, XML_NAMESPACE_NONE, xmloff::token::XML_INCLUDENAMESPACEPREFIXES, xforms_string },
386 TABLE_END
387 };
388
389 void exportXFormsSubmission( SvXMLExport& rExport,
390 const Reference<XPropertySet>& xSubmission )
391 {
392 lcl_export( xSubmission, rExport, aXFormsSubmissionTable );
393 SvXMLElementExport aElement( rExport, XML_NAMESPACE_XFORMS, XML_SUBMISSION,
394 true, true );
395 }
396
397
398 // export data types as XSD schema
399
400 constexpr ExportTable aDataTypeFacetTable[] =
401 {
402 { u"Length"_ustr, XML_NAMESPACE_XSD, xmloff::token::XML_LENGTH, xforms_int32 },
403 { u"MinLength"_ustr, XML_NAMESPACE_XSD, xmloff::token::XML_MINLENGTH, xforms_int32 },
404 { u"MaxLength"_ustr, XML_NAMESPACE_XSD, xmloff::token::XML_MAXLENGTH, xforms_int32 },
405 { u"MinInclusiveInt"_ustr, XML_NAMESPACE_XSD, xmloff::token::XML_MININCLUSIVE, xforms_int32 },
406 { u"MinExclusiveInt"_ustr, XML_NAMESPACE_XSD, xmloff::token::XML_MINEXCLUSIVE, xforms_int32 },
407 { u"MaxInclusiveInt"_ustr, XML_NAMESPACE_XSD, xmloff::token::XML_MAXINCLUSIVE, xforms_int32 },
408 { u"MaxExclusiveInt"_ustr, XML_NAMESPACE_XSD, xmloff::token::XML_MAXEXCLUSIVE, xforms_int32 },
409 { u"MinInclusiveDouble"_ustr, XML_NAMESPACE_XSD, xmloff::token::XML_MININCLUSIVE, xforms_double },
410 { u"MinExclusiveDouble"_ustr, XML_NAMESPACE_XSD, xmloff::token::XML_MINEXCLUSIVE, xforms_double },
411 { u"MaxInclusiveDouble"_ustr, XML_NAMESPACE_XSD, xmloff::token::XML_MAXINCLUSIVE, xforms_double },
412 { u"MaxExclusiveDouble"_ustr, XML_NAMESPACE_XSD, xmloff::token::XML_MAXEXCLUSIVE, xforms_double },
413 { u"MinInclusiveDate"_ustr, XML_NAMESPACE_XSD, xmloff::token::XML_MININCLUSIVE, xforms_date },
414 { u"MinExclusiveDate"_ustr, XML_NAMESPACE_XSD, xmloff::token::XML_MINEXCLUSIVE, xforms_date },
415 { u"MaxInclusiveDate"_ustr, XML_NAMESPACE_XSD, xmloff::token::XML_MAXINCLUSIVE, xforms_date },
416 { u"MaxExclusiveDate"_ustr, XML_NAMESPACE_XSD, xmloff::token::XML_MAXEXCLUSIVE, xforms_date },
417 { u"MinInclusiveTime"_ustr, XML_NAMESPACE_XSD, xmloff::token::XML_MININCLUSIVE, xforms_time },
418 { u"MinExclusiveTime"_ustr, XML_NAMESPACE_XSD, xmloff::token::XML_MINEXCLUSIVE, xforms_time },
419 { u"MaxInclusiveTime"_ustr, XML_NAMESPACE_XSD, xmloff::token::XML_MAXINCLUSIVE, xforms_time },
420 { u"MaxExclusiveTime"_ustr, XML_NAMESPACE_XSD, xmloff::token::XML_MAXEXCLUSIVE, xforms_time },
421 { u"MinInclusiveDateTime"_ustr, XML_NAMESPACE_XSD, xmloff::token::XML_MININCLUSIVE, xforms_dateTime },
422 { u"MinExclusiveDateTime"_ustr, XML_NAMESPACE_XSD, xmloff::token::XML_MINEXCLUSIVE, xforms_dateTime },
423 { u"MaxInclusiveDateTime"_ustr, XML_NAMESPACE_XSD, xmloff::token::XML_MAXINCLUSIVE, xforms_dateTime },
424 { u"MaxExclusiveDateTime"_ustr, XML_NAMESPACE_XSD, xmloff::token::XML_MAXEXCLUSIVE, xforms_dateTime },
425 { u"Pattern"_ustr, XML_NAMESPACE_XSD, xmloff::token::XML_PATTERN, xforms_string },
426 // ??? XML_ENUMERATION,
427 { u"WhiteSpace"_ustr, XML_NAMESPACE_XSD, xmloff::token::XML_WHITESPACE, xforms_whitespace },
428 { u"TotalDigits"_ustr, XML_NAMESPACE_XSD, xmloff::token::XML_TOTALDIGITS, xforms_int32 },
429 { u"FractionDigits"_ustr, XML_NAMESPACE_XSD, xmloff::token::XML_FRACTIONDIGITS, xforms_int32 },
430 TABLE_END
431 };
432
433 // export facets through table; use the same table as lcl_export does
434 static void lcl_exportDataTypeFacets( SvXMLExport& rExport,
435 const Reference<XPropertySet>& rPropertySet,
436 const ExportTable* pTable )
437 {
438 Reference<XPropertySetInfo> xInfo = rPropertySet->getPropertySetInfo();
439 for( const ExportTable* pCurrent = pTable;
440 !pCurrent->pPropertyName.isEmpty();
441 pCurrent++ )
442 {
443 OUString sName( pCurrent->pPropertyName );
444 if( xInfo->hasPropertyByName( sName ) )
445 {
446 OUString sValue = (*pCurrent->aConverter)(
447 rPropertySet->getPropertyValue( sName ) );
448
449 if( !sValue.isEmpty() )
450 {
451 rExport.AddAttribute( XML_NAMESPACE_NONE, XML_VALUE, sValue );
452 SvXMLElementExport aFacet(
453 rExport,
454 pCurrent->nNamespace,
455 static_cast<XMLTokenEnum>( pCurrent->nToken ),
456 true, true );
457 }
458 }
459 }
460 }
461
462 static OUString lcl_getXSDType( SvXMLExport const & rExport,
463 const Reference<XPropertySet>& xType )
464 {
465 // we use string as default...
466 XMLTokenEnum eToken = XML_STRING;
467
468 sal_uInt16 nDataTypeClass = 0;
469 xType->getPropertyValue( u"TypeClass"_ustr ) >>= nDataTypeClass;
470 switch( nDataTypeClass )
471 {
472 case css::xsd::DataTypeClass::STRING:
473 eToken = XML_STRING;
474 break;
475 case css::xsd::DataTypeClass::anyURI:
476 eToken = XML_ANYURI;
477 break;
478 case css::xsd::DataTypeClass::DECIMAL:
479 eToken = XML_DECIMAL;
480 break;
481 case css::xsd::DataTypeClass::DOUBLE:
482 eToken = XML_DOUBLE;
483 break;
484 case css::xsd::DataTypeClass::FLOAT:
485 eToken = XML_FLOAT;
486 break;
487 case css::xsd::DataTypeClass::BOOLEAN:
488 eToken = XML_BOOLEAN;
489 break;
490 case css::xsd::DataTypeClass::DATETIME:
491 eToken = XML_DATETIME_XSD;
492 break;
493 case css::xsd::DataTypeClass::TIME:
494 eToken = XML_TIME;
495 break;
496 case css::xsd::DataTypeClass::DATE:
497 eToken = XML_DATE;
498 break;
499 case css::xsd::DataTypeClass::gYear:
500 eToken = XML_YEAR;
501 break;
502 case css::xsd::DataTypeClass::gDay:
503 eToken = XML_DAY;
504 break;
505 case css::xsd::DataTypeClass::gMonth:
506 eToken = XML_MONTH;
507 break;
508 case css::xsd::DataTypeClass::DURATION:
509 case css::xsd::DataTypeClass::gYearMonth:
510 case css::xsd::DataTypeClass::gMonthDay:
511 case css::xsd::DataTypeClass::hexBinary:
512 case css::xsd::DataTypeClass::base64Binary:
513 case css::xsd::DataTypeClass::QName:
514 case css::xsd::DataTypeClass::NOTATION:
515 default:
516 OSL_FAIL( "unknown data type" );
517 }
518
519 return rExport.GetNamespaceMap().GetQNameByKey( XML_NAMESPACE_XSD,
520 GetXMLToken( eToken ) );
521 }
522
523 static void lcl_exportDataType( SvXMLExport& rExport,
524 const Reference<XPropertySet>& xType )
525 {
526 // we do not need to export basic types; exit if we have one
527 bool bIsBasic = false;
528 xType->getPropertyValue( u"IsBasic"_ustr ) >>= bIsBasic;
529 if( bIsBasic )
530 return;
531
532 // no basic type -> export
533
534 // <xsd:simpleType name="...">
535 OUString sName;
536 xType->getPropertyValue( u"Name"_ustr ) >>= sName;
537 rExport.AddAttribute( XML_NAMESPACE_NONE, XML_NAME, sName );
538 SvXMLElementExport aSimpleType( rExport,
539 XML_NAMESPACE_XSD, XML_SIMPLETYPE,
540 true, true );
541
542 // <xsd:restriction base="xsd:...">
543 rExport.AddAttribute( XML_NAMESPACE_NONE, XML_BASE,
544 lcl_getXSDType( rExport, xType ) );
545 SvXMLElementExport aRestriction( rExport,
546 XML_NAMESPACE_XSD,
547 XML_RESTRICTION,
548 true, true );
549
550 // export facets
551 lcl_exportDataTypeFacets( rExport,
552 xType,
553 aDataTypeFacetTable );
554 }
555
556 void exportXFormsSchemas( SvXMLExport& rExport,
557 const Reference<css::xforms::XModel>& xModel )
558 {
559 // TODO: for now, we'll fake this...
560 {
561 SvXMLElementExport aSchemaElem( rExport, XML_NAMESPACE_XSD, XML_SCHEMA,
562 true, true );
563
564 // now get data type repository, and export
565 Reference<XEnumerationAccess> xTypes = xModel->getDataTypeRepository();
566 if( xTypes.is() )
567 {
568 Reference<XEnumeration> xEnum = xTypes->createEnumeration();
569 SAL_WARN_IF( !xEnum.is(), "xmloff", "no enum?" );
570 while( xEnum->hasMoreElements() )
571 {
572 Reference<XPropertySet> xType( xEnum->nextElement(), UNO_QUERY );
573 lcl_exportDataType( rExport, xType );
574 }
575 }
576 }
577
578 // export other, 'foreign' schemas
579 Reference<XPropertySet> xPropSet( xModel, UNO_QUERY );
580 if( xPropSet.is() )
581 {
582 Reference<XDocument> xDocument(
583 xPropSet->getPropertyValue( u"ForeignSchema"_ustr ),
584 UNO_QUERY );
585
586 if( xDocument.is() )
587 exportDom( rExport, xDocument );
588 }
589 }
590
591
592 // helper functions
593
594
595 static void lcl_export( const Reference<XPropertySet>& rPropertySet,
596 SvXMLExport& rExport,
597 const ExportTable* pTable )
598 {
599 for( const ExportTable* pCurrent = pTable;
600 !pCurrent->pPropertyName.isEmpty();
601 pCurrent++ )
602 {
603 Any aAny = rPropertySet->getPropertyValue( pCurrent->pPropertyName );
604 OUString sValue = (*pCurrent->aConverter)( aAny );
605
606 if( !sValue.isEmpty() )
607 rExport.AddAttribute(
608 pCurrent->nNamespace,
609 static_cast<XMLTokenEnum>( pCurrent->nToken ),
610 sValue );
611 }
612 }
613
614
615 // any conversion functions
616
617
618 template<typename T, void (*FUNC)( OUStringBuffer&, T )>
619 OUString xforms_convert( const Any& rAny )
620 {
621 OUStringBuffer aBuffer;
622 T aData = T();
623 if( rAny >>= aData )
624 {
625 FUNC( aBuffer, aData );
626 }
627 return aBuffer.makeStringAndClear();
628 }
629
630 template<typename T, void (*FUNC)( OUStringBuffer&, const T& )>
631 OUString xforms_convertRef( const Any& rAny )
632 {
633 OUStringBuffer aBuffer;
634 T aData;
635 if( rAny >>= aData )
636 {
637 FUNC( aBuffer, aData );
638 }
639 return aBuffer.makeStringAndClear();
640 }
641
642 OUString xforms_string( const Any& rAny )
643 {
644 OUString aResult;
645 rAny >>= aResult;
646 return aResult;
647 }
648
649 OUString xforms_bool( const Any& rAny )
650 {
651 bool bResult = bool();
652 if( rAny >>= bResult )
653 return GetXMLToken( bResult ? XML_TRUE : XML_FALSE );
654 OSL_FAIL( "expected boolean value" );
655 return OUString();
656 }
657
658 void xforms_formatDate( OUStringBuffer& aBuffer, const util::Date& rDate )
659 {
660 aBuffer.append( OUString::number( rDate.Year ) +
661 "-" + OUString::number( rDate.Month ) +
662 "-" + OUString::number( rDate.Day ) );
663 }
664
665 void xforms_formatTime( OUStringBuffer& aBuffer, const css::util::Time& rTime )
666 {
667 Duration aDuration;
668 aDuration.Hours = rTime.Hours;
669 aDuration.Minutes = rTime.Minutes;
670 aDuration.Seconds = rTime.Seconds;
671 aDuration.NanoSeconds = rTime.NanoSeconds;
672 ::sax::Converter::convertDuration( aBuffer, aDuration );
673 }
674
675 void xforms_formatDateTime( OUStringBuffer& aBuffer, const util::DateTime& aDateTime )
676 {
677 ::sax::Converter::convertDateTime(aBuffer, aDateTime, nullptr);
678 }
679
680 OUString xforms_whitespace( const Any& rAny )
681 {
682 OUString sResult;
683 sal_uInt16 n = sal_uInt16();
684 if( rAny >>= n )
685 {
686 switch( n )
687 {
688 case css::xsd::WhiteSpaceTreatment::Preserve:
689 sResult = GetXMLToken( XML_PRESERVE );
690 break;
691 case css::xsd::WhiteSpaceTreatment::Replace:
692 sResult = GetXMLToken( XML_REPLACE );
693 break;
694 case css::xsd::WhiteSpaceTreatment::Collapse:
695 sResult = GetXMLToken( XML_COLLAPSE );
696 break;
697 }
698 }
699 return sResult;
700 }
701
702
703 /// return name of Binding
704 static OUString lcl_getXFormsBindName( const Reference<XPropertySet>& xBinding )
705 {
706 OUString sProp( u"BindingID"_ustr );
707
708 OUString sReturn;
709 if( xBinding.is() &&
710 xBinding->getPropertySetInfo()->hasPropertyByName( sProp ) )
711 {
712 xBinding->getPropertyValue( sProp ) >>= sReturn;
713 }
714 return sReturn;
715 }
716
717 // return name of binding
718 OUString getXFormsBindName( const Reference<XPropertySet>& xControl )
719 {
720 Reference<XBindableValue> xBindable( xControl, UNO_QUERY );
721 return xBindable.is()
722 ? lcl_getXFormsBindName(
723 Reference<XPropertySet>( xBindable->getValueBinding(), UNO_QUERY ))
724 : OUString();
725 }
726
727 // return name of list binding
728 OUString getXFormsListBindName( const Reference<XPropertySet>& xControl )
729 {
730 Reference<XListEntrySink> xListEntrySink( xControl, UNO_QUERY );
731 return xListEntrySink.is()
732 ? lcl_getXFormsBindName(
733 Reference<XPropertySet>( xListEntrySink->getListEntrySource(),
734 UNO_QUERY ) )
735 : OUString();
736 }
737
738 OUString getXFormsSubmissionName( const Reference<XPropertySet>& xBinding )
739 {
740 OUString sReturn;
741
742 Reference<XSubmissionSupplier> xSubmissionSupplier( xBinding, UNO_QUERY );
743 if( xSubmissionSupplier.is() )
744 {
745 Reference<XPropertySet> xPropertySet(
746 xSubmissionSupplier->getSubmission(), UNO_QUERY );
747 OUString sProp( u"ID"_ustr );
748 if( xPropertySet.is() &&
749 xPropertySet->getPropertySetInfo()->hasPropertyByName( sProp ) )
750 {
751 xPropertySet->getPropertyValue( sProp ) >>= sReturn;
752 }
753 }
754
755 return sReturn;
756 }
757
758 void getXFormsSettings( const Reference< XNameAccess >& _rXForms, Sequence< PropertyValue >& _out_rSettings )
759 {
760 _out_rSettings = Sequence< PropertyValue >();
761
762 OSL_PRECOND( _rXForms.is(), "getXFormsSettings: invalid XForms container!" );
763 if ( !_rXForms.is() )
764 return;
765
766 try
767 {
768 // we want to export some special properties of our XForms models as config-item-map-named,
769 // which implies we need a PropertyValue whose value is an XNameAccess, whose keys
770 // are the names of the XForm models, and which in turn provides named sequences of
771 // PropertyValues - which denote the actual property values of the given named model.
772
773 const Sequence< OUString > aModelNames( _rXForms->getElementNames() );
774
775 Reference< XNameContainer > xModelSettings = document::NamedPropertyValues::create( comphelper::getProcessComponentContext() );
776
777 for ( auto const & modelName : aModelNames )
778 {
779 Reference< XPropertySet > xModelProps( _rXForms->getByName( modelName ), UNO_QUERY_THROW );
780
781 static constexpr OUString sExternalData = u"ExternalData"_ustr;
782 Sequence<PropertyValue> aModelSettings{ comphelper::makePropertyValue(
783 sExternalData, xModelProps->getPropertyValue(sExternalData)) };
784
785 xModelSettings->insertByName( modelName, Any( aModelSettings ) );
786 }
787
788 if ( xModelSettings->hasElements() )
789 {
790 _out_rSettings = { comphelper::makePropertyValue(u"XFormModels"_ustr, xModelSettings) };
791 }
792 }
793 catch( const Exception& )
794 {
795 DBG_UNHANDLED_EXCEPTION("xmloff");
796 }
797 }
798
799 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */