LibreOffice Module xmloff (master)  1
xformsimport.cxx
Go to the documentation of this file.
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 "XFormsModelContext.hxx"
22 #include <utility>
23 #include <xmloff/xformsimport.hxx>
24 #include <com/sun/star/uno/Reference.hxx>
25 #include <com/sun/star/beans/XPropertySet.hpp>
26 #include <com/sun/star/form/binding/XValueBinding.hpp>
27 #include <com/sun/star/form/binding/XBindableValue.hpp>
28 #include <com/sun/star/form/binding/XListEntrySource.hpp>
29 #include <com/sun/star/form/binding/XListEntrySink.hpp>
30 #include <com/sun/star/form/submission/XSubmission.hpp>
31 #include <com/sun/star/form/submission/XSubmissionSupplier.hpp>
32 #include <com/sun/star/container/XNameAccess.hpp>
33 #include <rtl/ustring.hxx>
34 #include "xformsapi.hxx"
36 #include <tools/diagnose_ex.h>
37 
38 using std::pair;
40 using com::sun::star::uno::Exception;
41 using com::sun::star::uno::UNO_QUERY;
42 using com::sun::star::uno::UNO_QUERY_THROW;
43 using com::sun::star::uno::UNO_SET_THROW;
46 using com::sun::star::beans::XPropertySetInfo;
47 using com::sun::star::beans::PropertyValue;
48 using com::sun::star::frame::XModel;
49 using com::sun::star::container::XNameAccess;
50 using com::sun::star::form::binding::XValueBinding;
51 using com::sun::star::form::binding::XBindableValue;
52 using com::sun::star::form::binding::XListEntrySource;
53 using com::sun::star::form::binding::XListEntrySink;
54 using com::sun::star::form::submission::XSubmission;
55 using com::sun::star::form::submission::XSubmissionSupplier;
56 
58  SvXMLImport& rImport )
59 {
60  return new XFormsModelContext( rImport );
61 }
62 
64  const pair<Reference<XPropertySet>, OUString>& aPair)
65 {
66  Reference<XBindableValue> xBindable(
67  aPair.first,
68  UNO_QUERY );
69  Reference<XValueBinding> xBinding(
70  xforms_findXFormsBinding( xModel, aPair.second ),
71  UNO_QUERY );
72 
73  if( xBindable.is() && xBinding.is() )
74  {
75  try
76  {
77  xBindable->setValueBinding( xBinding );
78  }
79  catch( const Exception& )
80  {
81  // ignore problems during binding
82  // TODO: call XML error handling
83  }
84  }
85 }
86 
88  const ::pair<Reference<XPropertySet>, OUString>& aPair)
89 {
90  Reference<XListEntrySink> xListEntrySink(
91  aPair.first,
92  UNO_QUERY );
93  Reference<XListEntrySource> xListEntrySource(
94  xforms_findXFormsBinding( xModel, aPair.second ),
95  UNO_QUERY );
96 
97  if( xListEntrySink.is() && xListEntrySource.is() )
98  {
99  try
100  {
101  xListEntrySink->setListEntrySource( xListEntrySource );
102  }
103  catch( const Exception& )
104  {
105  // ignore problems during binding
106  // TODO: call XML error handling
107  }
108  }
109 }
110 
112  const pair<Reference<XPropertySet>, OUString>& aPair)
113 {
114  Reference<XSubmissionSupplier> xSubmissionSupp( aPair.first, UNO_QUERY );
115  Reference<XSubmission> xSubmission(
116  xforms_findXFormsSubmission( xModel, aPair.second ),
117  UNO_QUERY );
118 
119  if( xSubmissionSupp.is() && xSubmission.is() )
120  {
121  try
122  {
123  xSubmissionSupp->setSubmission( xSubmission );
124  }
125  catch( const Exception& )
126  {
127  // ignore problems during binding
128  // TODO: call XML error handling
129  }
130  }
131 }
132 
134 {
135  OSL_PRECOND( _rXForms.is(), "applyXFormsSettings: invalid XForms container!" );
136  if ( !_rXForms.is() )
137  return;
138 
139  ::comphelper::NamedValueCollection aSettings( _rSettings );
140  Reference< XNameAccess > xModelSettings( aSettings.get( "XFormModels" ), UNO_QUERY );
141  if ( !xModelSettings.is() )
142  {
143  OSL_FAIL( "applyXFormsSettings: wrong type for the XFormModels settings!" );
144  return;
145  }
146 
147  try
148  {
149  const Sequence< OUString > aSettingsForModels( xModelSettings->getElementNames() );
150  for ( auto const & modelName : aSettingsForModels )
151  {
152  // the settings for this particular model
153  Sequence< PropertyValue > aModelSettings;
154  OSL_VERIFY( xModelSettings->getByName( modelName ) >>= aModelSettings );
155 
156  // the model itself
157  if ( !_rXForms->hasByName( modelName ) )
158  {
159  OSL_FAIL( "applyXFormsSettings: have settings for a non-existent XForms model!" );
160  continue;
161  }
162 
163  // propagate the settings, being tolerant by omitting properties which are not supported
164  Reference< XPropertySet > xModelProps( _rXForms->getByName( modelName ), UNO_QUERY_THROW );
165  Reference< XPropertySetInfo > xModelPSI( xModelProps->getPropertySetInfo(), UNO_SET_THROW );
166 
167  for ( auto const & setting : std::as_const(aModelSettings) )
168  {
169  if ( !xModelPSI->hasPropertyByName( setting.Name ) )
170  {
171  OSL_FAIL( "applyXFormsSettings: non-existent model property!" );
172  continue;
173  }
174 
175  xModelProps->setPropertyValue( setting.Name, setting.Value );
176  }
177  }
178  }
179  catch( const Exception& )
180  {
181  DBG_UNHANDLED_EXCEPTION("xmloff");
182  }
183 }
184 
185 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void applyXFormsSettings(const Reference< XNameAccess > &_rXForms, const Sequence< PropertyValue > &_rSettings)
void bindXFormsValueBinding(Reference< XModel > const &xModel, const pair< Reference< XPropertySet >, OUString > &aPair)
void bindXFormsListBinding(Reference< XModel > const &xModel, const ::pair< Reference< XPropertySet >, OUString > &aPair)
Reference< XPropertySet > xforms_findXFormsSubmission(Reference< frame::XModel > const &xDocument, const OUString &rBindingID)
Definition: xformsapi.cxx:160
#define DBG_UNHANDLED_EXCEPTION(...)
Reference< XPropertySet > xforms_findXFormsBinding(Reference< frame::XModel > const &xDocument, const OUString &rBindingID)
Definition: xformsapi.cxx:153
class SAL_NO_VTABLE XPropertySet
void bindXFormsSubmission(Reference< XModel > const &xModel, const pair< Reference< XPropertySet >, OUString > &aPair)
This class deliberately does not support XWeak, to improve performance when loading large documents...
Definition: xmlictxt.hxx:45
import the xforms:model element
const css::uno::Any & get(const char *_pAsciiValueName) const
SvXMLImportContext * createXFormsModelContext(SvXMLImport &rImport)
create import context for xforms:model element.