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