1  /**************************************************************
2   *
3   * Licensed to the Apache Software Foundation (ASF) under one
4   * or more contributor license agreements.  See the NOTICE file
5   * distributed with this work for additional information
6   * regarding copyright ownership.  The ASF licenses this file
7   * to you under the Apache License, Version 2.0 (the
8   * "License"); you may not use this file except in compliance
9   * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   *
20   *************************************************************/
21  
22  
23  
24  // MARKER(update_precomp.py): autogen include statement, do not remove
25  #include "precompiled_xmloff.hxx"
26  
27  #include "XFormsModelContext.hxx"
28  #include <vector>
29  #include <utility>
30  #include "xmloff/xformsimport.hxx"
31  #include <com/sun/star/uno/Reference.hxx>
32  #include <com/sun/star/beans/XPropertySet.hpp>
33  #include <com/sun/star/form/binding/XValueBinding.hpp>
34  #include <com/sun/star/form/binding/XBindableValue.hpp>
35  #include <com/sun/star/form/binding/XListEntrySource.hpp>
36  #include <com/sun/star/form/binding/XListEntrySink.hpp>
37  #include <com/sun/star/form/submission/XSubmission.hpp>
38  #include <com/sun/star/form/submission/XSubmissionSupplier.hpp>
39  #include <com/sun/star/container/XNameAccess.hpp>
40  #include <rtl/ustring.hxx>
41  #include <xformsapi.hxx>
42  #include <comphelper/namedvaluecollection.hxx>
43  #include <tools/diagnose_ex.h>
44  
45  using std::pair;
46  using com::sun::star::uno::Reference;
47  using com::sun::star::uno::Exception;
48  using com::sun::star::uno::UNO_QUERY;
49  using com::sun::star::uno::UNO_QUERY_THROW;
50  using com::sun::star::uno::UNO_SET_THROW;
51  using com::sun::star::uno::Sequence;
52  using com::sun::star::beans::XPropertySet;
53  using com::sun::star::beans::XPropertySetInfo;
54  using com::sun::star::beans::PropertyValue;
55  using com::sun::star::frame::XModel;
56  using com::sun::star::container::XNameAccess;
57  using com::sun::star::form::binding::XValueBinding;
58  using com::sun::star::form::binding::XBindableValue;
59  using com::sun::star::form::binding::XListEntrySource;
60  using com::sun::star::form::binding::XListEntrySink;
61  using com::sun::star::form::submission::XSubmission;
62  using com::sun::star::form::submission::XSubmissionSupplier;
63  using rtl::OUString;
64  
createXFormsModelContext(SvXMLImport & rImport,sal_uInt16 nPrefix,const rtl::OUString & rLocalName)65  SvXMLImportContext* createXFormsModelContext(
66      SvXMLImport& rImport,
67      sal_uInt16 nPrefix,
68      const rtl::OUString& rLocalName )
69  {
70      return new XFormsModelContext( rImport, nPrefix, rLocalName );
71  }
72  
bindXFormsValueBinding(Reference<XModel> xModel,pair<Reference<XPropertySet>,OUString> aPair)73  void bindXFormsValueBinding(
74  	Reference<XModel> xModel,
75      pair<Reference<XPropertySet>,OUString> aPair )
76  {
77      Reference<XBindableValue> xBindable(
78          aPair.first,
79          UNO_QUERY );
80      Reference<XValueBinding> xBinding(
81          lcl_findXFormsBinding( xModel, aPair.second ),
82          UNO_QUERY );
83  
84      if( xBindable.is() && xBinding.is() )
85      {
86          try
87          {
88              xBindable->setValueBinding( xBinding );
89          }
90          catch( const Exception& )
91          {
92              // ignore problems during binding
93              // TODO: call XML error handling
94          }
95      }
96  }
97  
bindXFormsListBinding(Reference<XModel> xModel,::pair<Reference<XPropertySet>,OUString> aPair)98  void bindXFormsListBinding(
99  	Reference<XModel> xModel,
100      ::pair<Reference<XPropertySet>,OUString> aPair )
101  {
102      Reference<XListEntrySink> xListEntrySink(
103          aPair.first,
104          UNO_QUERY );
105      Reference<XListEntrySource> xListEntrySource(
106          lcl_findXFormsBinding( xModel, aPair.second ),
107          UNO_QUERY );
108  
109      if( xListEntrySink.is() && xListEntrySource.is() )
110      {
111          try
112          {
113              xListEntrySink->setListEntrySource( xListEntrySource );
114          }
115          catch( const Exception& )
116          {
117              // ignore problems during binding
118              // TODO: call XML error handling
119          }
120      }
121  }
122  
bindXFormsSubmission(Reference<XModel> xModel,pair<Reference<XPropertySet>,OUString> aPair)123  void bindXFormsSubmission(
124      Reference<XModel> xModel,
125      pair<Reference<XPropertySet>,OUString> aPair )
126  {
127      Reference<XSubmissionSupplier> xSubmissionSupp( aPair.first, UNO_QUERY );
128      Reference<XSubmission> xSubmission(
129          lcl_findXFormsSubmission( xModel, aPair.second ),
130          UNO_QUERY );
131  
132      if( xSubmissionSupp.is() && xSubmission.is() )
133      {
134          try
135          {
136              xSubmissionSupp->setSubmission( xSubmission );
137          }
138          catch( const Exception& )
139          {
140              // ignore problems during binding
141              // TODO: call XML error handling
142          }
143      }
144  }
145  
applyXFormsSettings(const Reference<XNameAccess> & _rXForms,const Sequence<PropertyValue> & _rSettings)146  void applyXFormsSettings( const Reference< XNameAccess >& _rXForms, const Sequence< PropertyValue >& _rSettings )
147  {
148      OSL_PRECOND( _rXForms.is(), "applyXFormsSettings: invalid XForms container!" );
149      if ( !_rXForms.is() )
150          return;
151  
152      ::comphelper::NamedValueCollection aSettings( _rSettings );
153      Reference< XNameAccess > xModelSettings( aSettings.get( "XFormModels" ), UNO_QUERY );
154      if ( !xModelSettings.is() )
155      {
156          OSL_ENSURE( false, "applyXFormsSettings: wrong type for the XFormModels settings!" );
157          return;
158      }
159  
160      try
161      {
162          Sequence< ::rtl::OUString > aSettingsForModels( xModelSettings->getElementNames() );
163          for (   const ::rtl::OUString* pModelName = aSettingsForModels.getConstArray();
164                  pModelName != aSettingsForModels.getConstArray() + aSettingsForModels.getLength();
165                  ++pModelName
166              )
167          {
168              // the settings for this particular model
169              Sequence< PropertyValue > aModelSettings;
170              OSL_VERIFY( xModelSettings->getByName( *pModelName ) >>= aModelSettings );
171  
172              // the model itself
173              if ( !_rXForms->hasByName( *pModelName ) )
174              {
175                  OSL_ENSURE( false, "applyXFormsSettings: have settings for a non-existent XForms model!" );
176                  continue;
177              }
178  
179              // propagate the settings, being tolerant by omitting properties which are not supported
180              Reference< XPropertySet > xModelProps( _rXForms->getByName( *pModelName ), UNO_QUERY_THROW );
181              Reference< XPropertySetInfo > xModelPSI( xModelProps->getPropertySetInfo(), UNO_SET_THROW );
182  
183              for (   const PropertyValue* pSetting = aModelSettings.getConstArray();
184                      pSetting != aModelSettings.getConstArray() + aModelSettings.getLength();
185                      ++pSetting
186                  )
187              {
188                  if ( !xModelPSI->hasPropertyByName( pSetting->Name ) )
189                  {
190                      OSL_ENSURE( false, "applyXFormsSettings: non-existent model property!" );
191                      continue;
192                  }
193  
194                  xModelProps->setPropertyValue( pSetting->Name, pSetting->Value );
195              }
196          }
197      }
198      catch( const Exception& )
199      {
200      	DBG_UNHANDLED_EXCEPTION();
201      }
202  }
203