LibreOffice Module xmloff (master)  1
xformsapi.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 "xformsapi.hxx"
22 
23 #include <com/sun/star/frame/XModel.hpp>
24 #include <com/sun/star/uno/Reference.hxx>
25 #include <com/sun/star/beans/XPropertySet.hpp>
26 #include <com/sun/star/container/XNameAccess.hpp>
27 #include <com/sun/star/xforms/XFormsSupplier.hpp>
28 #include <com/sun/star/xforms/XDataTypeRepository.hpp>
29 #include <com/sun/star/xforms/Model.hpp>
30 #include <com/sun/star/xforms/XModel2.hpp>
31 #include <com/sun/star/container/XNameContainer.hpp>
32 #include <com/sun/star/xsd/DataTypeClass.hpp>
33 
35 #include <sal/log.hxx>
36 #include <tools/diagnose_ex.h>
37 
38 #include <xmloff/xmltoken.hxx>
39 #include <xmloff/namespacemap.hxx>
40 #include <xmloff/xmlnamespace.hxx>
41 #include <xmloff/xmltkmap.hxx>
42 
45 using com::sun::star::uno::UNO_QUERY;
46 using com::sun::star::uno::UNO_QUERY_THROW;
48 using com::sun::star::container::XNameAccess;
49 using com::sun::star::xforms::XFormsSupplier;
50 using com::sun::star::xforms::XDataTypeRepository;
51 using com::sun::star::xforms::Model;
52 using com::sun::star::xforms::XModel2;
53 using com::sun::star::container::XNameContainer;
54 using com::sun::star::uno::Any;
55 using com::sun::star::uno::Exception;
56 
57 using namespace com::sun::star;
58 using namespace xmloff::token;
59 
61 {
63 
64  return xModel;
65 }
66 
68  const Reference<frame::XModel>& xDocument,
69  const Reference<xforms::XModel2>& xModel )
70 {
71  bool bSuccess = false;
72  try
73  {
74  Reference<XFormsSupplier> xSupplier( xDocument, UNO_QUERY );
75  if( xSupplier.is() )
76  {
77  Reference<XNameContainer> xForms = xSupplier->getXForms();
78  if( xForms.is() )
79  {
80  OUString sName;
81  xModel->getPropertyValue("ID") >>= sName;
82  xForms->insertByName( sName, Any( xModel ) );
83  bSuccess = true;
84  }
85  }
86  }
87  catch( const Exception& )
88  {
89  ; // no success!
90  }
91 
92  // TODO: implement proper error handling
93  SAL_WARN_IF( !bSuccess, "xmloff", "can't import model" );
94 }
95 
97  Reference<frame::XModel> const & xDocument,
98  const OUString& rBindingID,
99  bool bBinding )
100 {
101  // find binding by iterating over all models, and look for the
102  // given binding ID
103 
105  try
106  {
107  // get supplier
108  Reference<XFormsSupplier> xSupplier( xDocument, UNO_QUERY );
109  if( xSupplier.is() )
110  {
111  // get XForms models
112  Reference<XNameContainer> xForms = xSupplier->getXForms();
113  if( xForms.is() )
114  {
115  // iterate over all models
116  const Sequence<OUString> aNames = xForms->getElementNames();
117  for( const auto& rName : aNames )
118  {
120  xForms->getByName( rName ), UNO_QUERY );
121  if( xModel.is() )
122  {
123  // ask model for bindings
124  Reference<XNameAccess> xBindings(
125  bBinding
126  ? xModel->getBindings()
127  : xModel->getSubmissions(),
128  UNO_QUERY_THROW );
129 
130  // finally, ask binding for name
131  if( xBindings->hasByName( rBindingID ) )
132  xRet.set( xBindings->getByName( rBindingID ),
133  UNO_QUERY );
134  }
135 
136  if (xRet.is())
137  break;
138  }
139  }
140  }
141  }
142  catch( const Exception& )
143  {
144  ; // no success!
145  }
146 
147  // TODO: if (!xRet.is()) rImport.SetError(...);
148 
149  return xRet;
150 }
151 
153  Reference<frame::XModel> const & xDocument,
154  const OUString& rBindingID )
155 {
156  return lcl_findXFormsBindingOrSubmission( xDocument, rBindingID, true );
157 }
158 
160  Reference<frame::XModel> const & xDocument,
161  const OUString& rBindingID )
162 {
163  return lcl_findXFormsBindingOrSubmission( xDocument, rBindingID, false );
164 }
165 
166 void xforms_setValueAny( Reference<XPropertySet> const & xPropertySet,
167  const OUString& rName,
168  const Any& rAny )
169 {
170  xPropertySet->setPropertyValue( rName, rAny );
171 }
172 
173 #define TOKEN_MAP_ENTRY(NAMESPACE,TOKEN) { XML_NAMESPACE_##NAMESPACE, xmloff::token::XML_##TOKEN, xmloff::token::XML_##TOKEN }
175 {
176  TOKEN_MAP_ENTRY( XSD, STRING ),
177  TOKEN_MAP_ENTRY( XSD, DECIMAL ),
178  TOKEN_MAP_ENTRY( XSD, DOUBLE ),
179  TOKEN_MAP_ENTRY( XSD, FLOAT ),
180  TOKEN_MAP_ENTRY( XSD, BOOLEAN ),
181  TOKEN_MAP_ENTRY( XSD, ANYURI ),
182  TOKEN_MAP_ENTRY( XSD, DATETIME_XSD ),
183  TOKEN_MAP_ENTRY( XSD, DATE ),
184  TOKEN_MAP_ENTRY( XSD, TIME ),
185  TOKEN_MAP_ENTRY( XSD, YEAR ),
186  TOKEN_MAP_ENTRY( XSD, MONTH ),
187  TOKEN_MAP_ENTRY( XSD, DAY ),
189 };
190 
192  const Reference<XDataTypeRepository>& xRepository,
193  const SvXMLNamespaceMap& rNamespaceMap,
194  const OUString& rXMLName )
195 {
196  // translate name into token for local name
197  OUString sLocalName;
198  sal_uInt16 nPrefix = rNamespaceMap.GetKeyByAttrValueQName(rXMLName, &sLocalName);
199  static const SvXMLTokenMap aMap( aTypes );
200  sal_uInt16 nToken = aMap.Get( nPrefix, sLocalName );
201 
202  sal_uInt16 nTypeClass = css::xsd::DataTypeClass::STRING;
203  if( nToken != XML_TOK_UNKNOWN )
204  {
205  // we found an XSD name: then get the proper API name for it
206  SAL_WARN_IF( !xRepository.is(), "xmloff", "can't find type without repository");
207  switch( nToken )
208  {
209  case XML_STRING:
210  nTypeClass = css::xsd::DataTypeClass::STRING;
211  break;
212  case XML_ANYURI:
213  nTypeClass = css::xsd::DataTypeClass::anyURI;
214  break;
215  case XML_DECIMAL:
216  nTypeClass = css::xsd::DataTypeClass::DECIMAL;
217  break;
218  case XML_DOUBLE:
219  nTypeClass = css::xsd::DataTypeClass::DOUBLE;
220  break;
221  case XML_FLOAT:
222  nTypeClass = css::xsd::DataTypeClass::FLOAT;
223  break;
224  case XML_BOOLEAN:
225  nTypeClass = css::xsd::DataTypeClass::BOOLEAN;
226  break;
227  case XML_DATETIME_XSD:
228  nTypeClass = css::xsd::DataTypeClass::DATETIME;
229  break;
230  case XML_DATE:
231  nTypeClass = css::xsd::DataTypeClass::DATE;
232  break;
233  case XML_TIME:
234  nTypeClass = css::xsd::DataTypeClass::TIME;
235  break;
236  case XML_YEAR:
237  nTypeClass = css::xsd::DataTypeClass::gYear;
238  break;
239  case XML_DAY:
240  nTypeClass = css::xsd::DataTypeClass::gDay;
241  break;
242  case XML_MONTH:
243  nTypeClass = css::xsd::DataTypeClass::gMonth;
244  break;
245 
246  /* data types not yet supported:
247  nTypeClass = css::xsd::DataTypeClass::DURATION;
248  nTypeClass = css::xsd::DataTypeClass::gYearMonth;
249  nTypeClass = css::xsd::DataTypeClass::gMonthDay;
250  nTypeClass = css::xsd::DataTypeClass::hexBinary;
251  nTypeClass = css::xsd::DataTypeClass::base64Binary;
252  nTypeClass = css::xsd::DataTypeClass::QName;
253  nTypeClass = css::xsd::DataTypeClass::NOTATION;
254  */
255  }
256  }
257 
258  return nTypeClass;
259 }
260 
261 
263  const Reference<XDataTypeRepository>& xRepository,
264  const SvXMLNamespaceMap& rNamespaceMap,
265  const OUString& rXMLName )
266 {
267  OUString sLocalName;
268  sal_uInt16 nPrefix = rNamespaceMap.GetKeyByAttrValueQName(rXMLName, &sLocalName);
269  static const SvXMLTokenMap aMap( aTypes );
270  sal_uInt16 nToken = aMap.Get( nPrefix, sLocalName );
271  return ( nToken == XML_TOK_UNKNOWN )
272  ? rXMLName
273  : xforms_getBasicTypeName( xRepository, rNamespaceMap, rXMLName );
274 }
275 
277  const Reference<XDataTypeRepository>& xRepository,
278  const SvXMLNamespaceMap& rNamespaceMap,
279  const OUString& rXMLName )
280 {
281  OUString sTypeName = rXMLName;
282  try
283  {
284  sTypeName =
285  xRepository->getBasicDataType(
286  xforms_getTypeClass( xRepository, rNamespaceMap, rXMLName ) )
287  ->getName();
288  }
289  catch( const Exception& )
290  {
291  TOOLS_WARN_EXCEPTION("xmloff", "exception during type creation");
292  }
293  return sTypeName;
294 }
295 
296 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void xforms_setValueAny(Reference< XPropertySet > const &xPropertySet, const OUString &rName, const Any &rAny)
Definition: xformsapi.cxx:166
STRING
#define XML_TOK_UNKNOWN
Definition: xmltkmap.hxx:32
#define XML_TOKEN_MAP_END
Definition: xmltkmap.hxx:33
HashMap_OWString_Interface aMap
DATE
sal_uInt16 GetKeyByAttrValueQName(const OUString &rAttrName, OUString *pLocalName) const
const char * sName
Reference< XModel2 > xforms_createXFormsModel()
Definition: xformsapi.cxx:60
Reference< XPropertySet > xforms_findXFormsSubmission(Reference< frame::XModel > const &xDocument, const OUString &rBindingID)
Definition: xformsapi.cxx:159
Reference< XPropertySet > xforms_findXFormsBinding(Reference< frame::XModel > const &xDocument, const OUString &rBindingID)
Definition: xformsapi.cxx:152
OUString xforms_getBasicTypeName(const Reference< XDataTypeRepository > &xRepository, const SvXMLNamespaceMap &rNamespaceMap, const OUString &rXMLName)
Definition: xformsapi.cxx:276
#define TOOLS_WARN_EXCEPTION(area, stream)
class SAL_NO_VTABLE XPropertySet
void xforms_addXFormsModel(const Reference< frame::XModel > &xDocument, const Reference< xforms::XModel2 > &xModel)
Definition: xformsapi.cxx:67
sal_uInt16 Get(sal_uInt16 nPrefix, const OUString &rLName) const
Definition: xmltkmap.cxx:82
#define DOUBLE
const SvXMLTokenMapEntry aTypes[]
Definition: xformsapi.cxx:174
#define TOKEN_MAP_ENTRY(NAMESPACE, TOKEN)
Definition: xformsapi.cxx:173
DefTokenId nToken
#define SAL_WARN_IF(condition, area, stream)
Handling of tokens in XML:
Reference< XComponentContext > getProcessComponentContext()
OUString xforms_getTypeName(const Reference< XDataTypeRepository > &xRepository, const SvXMLNamespaceMap &rNamespaceMap, const OUString &rXMLName)
Definition: xformsapi.cxx:262
Reference< XModel > xModel
static Reference< XPropertySet > lcl_findXFormsBindingOrSubmission(Reference< frame::XModel > const &xDocument, const OUString &rBindingID, bool bBinding)
Definition: xformsapi.cxx:96
sal_uInt16 xforms_getTypeClass(const Reference< XDataTypeRepository > &xRepository, const SvXMLNamespaceMap &rNamespaceMap, const OUString &rXMLName)
Definition: xformsapi.cxx:191