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