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